is_preview() && !get_option('adghost_is_active', false))) { return; } if (AdGhost_Helper::builder_preview()) { return; } if (!$this->is_preview() && AdGhost_Targeting::page_exclusion_reason() !== null) { return; } wp_enqueue_style('adghost-frontend', ADGHOST_PLUGIN_URL . 'assets/css/frontend.css', [], ADGHOST_VERSION); $label_color = AdGhost_Helper::ad_label_color(); if ($label_color !== '') { wp_add_inline_style('adghost-frontend', ':root{--adghost-label-color:' . $label_color . '}'); } $label_vars = ''; if (get_option('adghost_ad_label_bold', false)) { $label_vars .= '--adghost-label-weight:700;'; } $label_align = AdGhost_Helper::ad_label_align(); if ($label_align !== '') { $label_vars .= '--adghost-label-align:' . $label_align . ';'; } if ($label_vars !== '') { wp_add_inline_style('adghost-frontend', ':root{' . $label_vars . '}'); } if (AdGhost_AMP::is_amp()) { return; } $generator = new AdGhost_Script_Generator(); $config = $generator->build_config(); $device = $config !== null ? AdGhost_Targeting::device_parts((array) json_decode(wp_json_encode($config['formats']), true), $generator->format_device_rules()) : ['css' => '', 'js' => '']; $off = []; foreach (AdGhost_Helper::units() as $unit) { $family = ['anchor' => 'anchor', 'interstitial' => 'interstitial', 'reward' => 'rewarded'][AdGhost_Helper::get_unit_position($unit)] ?? null; if ($family !== null && AdGhost_Targeting::unit_exclusion_reason($unit) !== null) { $off[] = $family; } } if ($off) { $device['js'] .= '(function(c,o){c.formats=c.formats||{};o.forEach(function(k){c.formats[k]=c.formats[k]||{};c.formats[k].enabled=false;});})' . '(window.AdGhostConfig=window.AdGhostConfig||{},' . wp_json_encode(array_values(array_unique($off))) . ');'; } if ($device['css'] !== '') { wp_add_inline_style('adghost-frontend', $device['css']); } if ($this->is_preview()) { wp_enqueue_style('adghost-preview', ADGHOST_PLUGIN_URL . 'assets/css/preview.css', ['adghost-frontend'], ADGHOST_VERSION); if ($device['js'] !== '') { wp_register_script('adghost-devices', false, [], ADGHOST_VERSION, false); wp_enqueue_script('adghost-devices'); wp_add_inline_script('adghost-devices', $device['js']); } return; } // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion wp_enqueue_script('adghost-gpt', self::GPT_URL, [], null, ['strategy' => 'async', 'in_footer' => false]); $local_url = $generator->get_local_script_url(); $before = $device['js'] . (class_exists('AdGhost_Key_Values') ? AdGhost_Key_Values::page_script() : ''); if (AdGhost_Script_Generator::runtime_source() === 'custom' && $config !== null) { $build = AdGhost_Script_Generator::engine_build(); $before .= 'window.__ADGHOST_CONFIG__=' . wp_json_encode($config, JSON_HEX_TAG | JSON_HEX_AMP) . ';' . 'window.addEventListener("load",function(){var b=window.__ADGHOST_ENGINE_BUILD__;' . 'if(b&&b!==' . wp_json_encode($build) . '&&window.console){console.warn("[AdGhost] The self-hosted engine is from another plugin version. Download it again from AdGhost, Settings, General, Ad script.");}});'; $this->fallback_url = (string) $local_url; // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion wp_enqueue_script('adghost-runtime', AdGhost_Script_Generator::custom_runtime_url(), [], null, ['strategy' => 'async', 'in_footer' => false]); } elseif ($local_url) { // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion wp_enqueue_script('adghost-runtime', $local_url, [], null, ['strategy' => 'async', 'in_footer' => false]); } else { $local_script = $generator->generate_local_script(); if (!$local_script) { return; } wp_register_script('adghost-runtime', false, [], ADGHOST_VERSION, false); wp_enqueue_script('adghost-runtime'); wp_add_inline_script('adghost-runtime', $local_script); } if ($before !== '') { wp_add_inline_script('adghost-runtime', $before, 'before'); } } public function resource_hints($urls, $relation) { if (!in_array($relation, ['preconnect', 'dns-prefetch'], true) || is_admin() || !get_option('adghost_is_active', false) || $this->is_preview() || AdGhost_Targeting::page_exclusion_reason() !== null) { return $urls; } if ($relation === 'preconnect') { $urls[] = 'https://securepubads.g.doubleclick.net'; $urls[] = 'https://pagead2.googlesyndication.com'; } else { $urls[] = 'https://tpc.googlesyndication.com'; } return $urls; } public function runtime_tag($tag, $handle) { if ($handle === 'adghost-gpt' && strpos($tag, ' src=') !== false) { return preg_replace('/]*\ssrc=)/', '