config = $config; $this->init_templates(); } private function init_templates() { $this->templates = array( 'google_analytics' => array( 'G-' => ' ', 'UA-' => ' ' ), 'google_tag_manager' => array( 'head' => ' ', 'body' => ' ' ), 'facebook_pixel' => ' ', 'google_ads' => ' ', 'microsoft_clarity' => ' ', 'hotjar' => ' ', 'tiktok_pixel' => ' ', 'linkedin_insight' => ' ', 'twitter_pixel' => ' ', 'pinterest_pixel' => ' ', 'snapchat_pixel' => ' ', 'google_optimize' => ' ', 'crazyegg' => ' ', 'mixpanel' => ' ', 'amplitude' => ' ', 'matomo' => ' ', ); } public function get_codes_for_position($position) { $cache_key = $position . '_' . wp_cache_get_last_changed('wptag_codes'); if (isset($this->cached_codes[$cache_key])) { return $this->cached_codes[$cache_key]; } $codes = array(); $all_settings = $this->config->get_settings(); if (empty($all_settings)) { return array(); } foreach ($all_settings as $service_key => $service_settings) { if (!$this->should_output_service($service_key, $service_settings, $position)) { continue; } $service_codes = $this->get_service_codes($service_key, $service_settings, $position); if (!empty($service_codes)) { $priority = intval($service_settings['priority'] ?? 10); if (!isset($codes[$priority])) { $codes[$priority] = array(); } foreach ($service_codes as $code) { if (!empty(trim($code))) { $codes[$priority][] = array( 'service' => $service_key, 'code' => $code, 'priority' => $priority ); } } } } ksort($codes); $output_codes = array(); foreach ($codes as $priority_codes) { foreach ($priority_codes as $code_data) { $output_codes[] = $code_data['code']; } } $this->cached_codes[$cache_key] = $output_codes; return $output_codes; } private function should_output_service($service_key, $service_settings, $position) { if (empty($service_settings['enabled'])) { return false; } if (!$this->check_position_condition($service_key, $service_settings, $position)) { return false; } if (!$this->check_device_condition($service_settings['device'] ?? 'all')) { return false; } if (!$this->check_page_conditions($service_settings['conditions'] ?? array())) { return false; } $service_config = $this->config->get_service_config($service_key); if (!$service_config) { return false; } if (!empty($service_settings['use_template'])) { $field_key = $service_config['field']; $id_value = trim($service_settings[$field_key] ?? ''); if (empty($id_value)) { return false; } } else { $custom_code = trim($service_settings['custom_code'] ?? ''); if (empty($custom_code)) { return false; } } return apply_filters('wptag_should_output_service', true, $service_key, $service_settings, $position); } private function check_position_condition($service_key, $service_settings, $position) { if ($service_key === 'google_tag_manager') { return ($position === 'head' || $position === 'body'); } return $service_settings['position'] === $position; } private function check_device_condition($device_setting) { if ($device_setting === 'all') { return true; } $is_mobile = wp_is_mobile(); if ($device_setting === 'mobile' && $is_mobile) { return true; } if ($device_setting === 'desktop' && !$is_mobile) { return true; } return false; } private function check_page_conditions($conditions) { if (empty($conditions)) { return true; } foreach ($conditions as $condition) { if (!$this->check_single_condition($condition)) { return false; } } return true; } private function check_single_condition($condition) { $type = $condition['type'] ?? ''; $value = $condition['value'] ?? ''; $operator = $condition['operator'] ?? 'is'; switch ($type) { case 'page_type': return $this->check_page_type_condition($value, $operator); case 'post_type': return $this->check_post_type_condition($value, $operator); case 'category': return $this->check_category_condition($value, $operator); case 'tag': return $this->check_tag_condition($value, $operator); case 'user_role': return $this->check_user_role_condition($value, $operator); default: return true; } } private function check_page_type_condition($value, $operator) { $current_page_type = $this->get_current_page_type(); if ($operator === 'is') { return $current_page_type === $value; } elseif ($operator === 'is_not') { return $current_page_type !== $value; } return true; } private function get_current_page_type() { if (is_home()) return 'home'; if (is_front_page()) return 'front_page'; if (is_single()) return 'single'; if (is_page()) return 'page'; if (is_category()) return 'category'; if (is_tag()) return 'tag'; if (is_archive()) return 'archive'; if (is_search()) return 'search'; if (is_404()) return '404'; return 'unknown'; } private function check_post_type_condition($value, $operator) { $post_type = get_post_type(); if ($operator === 'is') { return $post_type === $value; } elseif ($operator === 'is_not') { return $post_type !== $value; } return true; } private function check_category_condition($value, $operator) { if (is_category($value)) { return $operator === 'is'; } elseif (is_single()) { $has_category = has_category($value); return $operator === 'is' ? $has_category : !$has_category; } return $operator === 'is_not'; } private function check_tag_condition($value, $operator) { if (is_tag($value)) { return $operator === 'is'; } elseif (is_single()) { $has_tag = has_tag($value); return $operator === 'is' ? $has_tag : !$has_tag; } return $operator === 'is_not'; } private function check_user_role_condition($value, $operator) { $user = wp_get_current_user(); $has_role = in_array($value, $user->roles); return $operator === 'is' ? $has_role : !$has_role; } private function get_service_codes($service_key, $service_settings, $position) { if (!empty($service_settings['use_template'])) { return $this->get_template_codes($service_key, $service_settings, $position); } else { $custom_code = trim($service_settings['custom_code'] ?? ''); if (!empty($custom_code)) { return array($custom_code); } return array(); } } private function get_template_codes($service_key, $service_settings, $position) { $service_config = $this->config->get_service_config($service_key); if (!$service_config) { return array(); } $templates = $this->get_templates_for_service($service_key, $service_settings, $position); if (empty($templates)) { return array(); } $field_key = $service_config['field']; $id_value = $service_settings[$field_key] ?? ''; if (empty($id_value)) { return array(); } $codes = array(); foreach ($templates as $template) { $code = str_replace('{ID}', esc_attr($id_value), $template); $code = apply_filters('wptag_template_code', $code, $service_key, $service_settings, $position); if (!empty($code)) { $codes[] = $code; } } return $codes; } private function get_templates_for_service($service_key, $service_settings, $position) { if (!isset($this->templates[$service_key])) { return array(); } $template_data = $this->templates[$service_key]; if ($service_key === 'google_analytics') { $service_config = $this->config->get_service_config($service_key); $field_key = $service_config['field']; $id_value = $service_settings[$field_key] ?? ''; if (strpos($id_value, 'G-') === 0) { return array($template_data['G-']); } elseif (strpos($id_value, 'UA-') === 0) { return array($template_data['UA-']); } return array(); } if ($service_key === 'google_tag_manager') { if ($position === 'head' && isset($template_data['head'])) { return array($template_data['head']); } elseif ($position === 'body' && isset($template_data['body'])) { return array($template_data['body']); } return array(); } if (is_array($template_data)) { if (isset($template_data['default'])) { return array($template_data['default']); } return array(); } return array($template_data); } public function output_codes($position) { $codes = $this->get_codes_for_position($position); if (empty($codes)) { if (defined('WP_DEBUG') && WP_DEBUG && current_user_can('manage_options')) { echo "\n\n"; } return; } $output = "\n\n"; foreach ($codes as $index => $code) { $output .= $code; if ($index < count($codes) - 1) { $output .= "\n"; } } $output .= "\n\n"; $final_output = apply_filters('wptag_output_codes', $output, $position, $codes); if (defined('WP_DEBUG') && WP_DEBUG && current_user_can('manage_options')) { $debug_info = sprintf( "\n\n", count($codes), $position ); echo $debug_info; } echo $final_output; } public function clear_cache() { $this->cached_codes = array(); wp_cache_set_last_changed('wptag_codes'); } public function get_template_preview($service_key, $id_value) { $service_config = $this->config->get_service_config($service_key); if (!$service_config) { return ''; } $fake_settings = array( $service_config['field'] => $id_value, 'use_template' => true, 'enabled' => true, 'position' => 'head' ); $templates = $this->get_templates_for_service($service_key, $fake_settings, 'head'); if (empty($templates)) { return ''; } $preview_parts = array(); foreach ($templates as $template) { $code = str_replace('{ID}', esc_attr($id_value), $template); if (!empty($code)) { $preview_parts[] = $code; } } return implode("\n\n", $preview_parts); } public function get_service_stats() { $stats = array( 'enabled_services' => 0, 'total_codes' => 0, 'by_position' => array( 'head' => 0, 'body' => 0, 'footer' => 0 ), 'by_type' => array( 'template' => 0, 'custom' => 0 ) ); $all_settings = $this->config->get_settings(); foreach ($all_settings as $service_key => $service_settings) { if (!empty($service_settings['enabled'])) { $stats['enabled_services']++; $stats['total_codes']++; $position = $service_settings['position'] ?? 'head'; if (isset($stats['by_position'][$position])) { $stats['by_position'][$position]++; } if (!empty($service_settings['use_template'])) { $stats['by_type']['template']++; } else { $stats['by_type']['custom']++; } } } return $stats; } public function validate_template($service_key, $id_value) { $service_config = $this->config->get_service_config($service_key); if (!$service_config) { return false; } $fake_settings = array( $service_config['field'] => $id_value, 'use_template' => true ); $templates = $this->get_templates_for_service($service_key, $fake_settings, 'head'); return !empty($templates); } }