get_active_config();
if ( ! $active || $active === '__disabled__' ) {
return;
}
// Enqueue the main AdSense for Search script
wp_enqueue_script('google-adsense-search', 'https://www.google.com/adsense/search/ads.js', [], null, false); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters -- external Google CDN, version not applicable
$inline_tracking_script = "(function(g,o){g[o]=g[o]||function(){(g[o]['q']=g[o]['q']||[]).push(arguments)},g[o]['t']=1*new Date})(window,'_googCsa');";
wp_add_inline_script('google-adsense-search', $inline_tracking_script, 'after');
// Placeholder footer handle for dynamic inline scripts added during the_content filter
wp_register_script('adpagex-init', false, ['google-adsense-search'], false, true); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters
wp_enqueue_script('adpagex-init');
}
/**
* This function is no longer explicitly echoing scripts.
* Scripts intended for wp_body_open should be enqueued via wp_enqueue_script
* with the 'true' parameter for footer loading.
*/
public function insert_after_body()
{
// No direct echoing.
}
public function insert_ad_into_content($content)
{
static $adpagex_inserted = false;
if (!is_main_query() || !in_the_loop()) {
return $content;
}
$config = $this->get_active_config();
if (!$config || $config === '__disabled__') {
return $content;
}
if (isset($config['devices'])) {
$current_device = $this->detect_device();
if (!in_array($current_device, $config['devices'])) {
return $content;
}
}
if (in_array('shortcode', $config['insertion_type']) && has_shortcode($content, 'adpagex')) {
return $content;
}
if (!$this->should_display_on_current_page($config)) {
return $content;
}
// Prepare postQuery variable
$post_query_var = '';
if (is_single()) {
$related_keys = get_post_meta(get_the_ID(), 'related_keys', true);
if (empty($related_keys)) {
$related_keys = get_the_title();
}
$related_keys = trim(preg_replace('/[^a-zA-Z0-9, ]/', '', $related_keys));
$related_keys = esc_js($related_keys);
$style_id = get_post_meta(get_the_ID(), 'styleid', true);
$post_query_var = "var postQuery = '" . esc_js(trim($related_keys)) . "';";
} else {
$post_query_var = "var postQuery = '';";
}
$styleIdCustom = "var afsStyleId = '" . esc_js($style_id) . "';";
if($config['status'] == 'disabled') {
$post_query_var = "var postQuery = '';";
}
$rewarded_needs_readmore = isset($config['rewarded_enabled']) && $config['rewarded_enabled'] === '1'
&& (($config['rewarded_trigger_type'] ?? 'read_more') === 'read_more');
if ((isset($config['show_readmore']) && $config['show_readmore'] === 'yes') || $rewarded_needs_readmore) {
// Sử dụng readmore inserter để xử lý toàn bộ (bao gồm cả AFS script)
$this->add_readmore($content, $config);
return $content;
} else {
// Logic cũ cho trường hợp không dùng readmore
$ad_html_containers = '
';
$alignment = isset($config['alignment']) ? $config['alignment'] : 'default';
if ($alignment !== 'default') {
$ad_html_containers = '' . $ad_html_containers . '
';
}
$this->render_ad_script($config, $post_query_var, $styleIdCustom);
return $this->insert_at_position($content, wp_kses_post($ad_html_containers), $config);
}
}
public function add_readmore(&$content, $config)
{
// Kiểm tra class readmore inserter có tồn tại không
if (!class_exists('AdPageX_Inserter')) {
// Fallback: return content without readmore processing
return;
}
// Sử dụng readmore inserter class
$inserter = AdPageX_Inserter::get_instance();
// Chuẩn bị ad HTML containers
$ad_html_containers = '
';
$alignment = isset($config['alignment']) ? $config['alignment'] : 'default';
if ($alignment !== 'default') {
$ad_html_containers = '' . $ad_html_containers . '
';
}
// Prepare postQuery và styleId variables
$post_query_var = '';
$style_id = '';
if (is_single()) {
$related_keys = get_post_meta(get_the_ID(), 'related_keys', true);
if (empty($related_keys)) {
$related_keys = get_the_title();
}
$related_keys = trim(preg_replace('/[^a-zA-Z0-9, ]/', '', $related_keys));
$style_id = get_post_meta(get_the_ID(), 'styleid', true);
$post_query_var = "var postQuery = '" . esc_js(trim($related_keys)) . "';";
} else {
$post_query_var = "var postQuery = '';";
}
$styleIdCustom = "var afsStyleId = '" . esc_js($style_id) . "';";
if($config['status'] == 'disabled') {
$post_query_var = "var postQuery = '';";
}
// Render ad script (registers inline JS via wp_add_inline_script, no direct echo)
$this->render_ad_script($config, $post_query_var, $styleIdCustom);
// Kết hợp ad HTML và script
$full_ad_html = wp_kses_post($ad_html_containers);
// Force readmore mode so inserter always uses the with_readmore path
$config['show_readmore'] = 'yes';
// Process content với readmore và full ad HTML
$content = $inserter->process_content($content, $full_ad_html, $config);
}
private function get_active_config()
{
$active = get_option('adpagex_active_config');
$configs = get_option('adpagex_configurations', []);
if ($active === '__disabled__' || empty($active)) {
return !empty($configs) ? $configs[0] : '__disabled__';
}
foreach ($configs as $cfg) {
if ($cfg['id'] === $active) {
return $cfg;
}
}
return null;
}
private function should_display_on_current_page($config)
{
$enabled_on = isset($config['enabled_on']) ? $config['enabled_on'] : ['posts'];
if (is_single() && in_array('posts', $enabled_on)) {
return true;
}
if (is_front_page() && in_array('home', $enabled_on)) {
return true;
}
if (is_category() && in_array('category', $enabled_on)) {
return true;
}
if (is_page() && in_array('pages', $enabled_on)) {
return true;
}
if (is_search() && in_array('search', $enabled_on)) {
return true;
}
if ((is_tag() || is_archive()) && in_array('archive', $enabled_on)) {
return true;
}
return false;
}
/**
* Extracts only the raw JavaScript content (without ', '<\/script>', $js_content );
wp_add_inline_script( $init_handle, $js_sanitized );
} elseif (!empty($config['script_file'])) {
$ext_handle = 'adpagex-ext-' . sanitize_key($config['id']);
wp_enqueue_script($ext_handle, esc_url_raw($config['script_file']), [], null, true); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters
if (!empty($post_query_var) || !empty($styleIdCustom)) {
wp_add_inline_script($ext_handle, $post_query_var . $styleIdCustom, 'before');
}
} elseif (!empty($config['script'])) {
$raw_ads_js = str_replace( '', '<\/script>', $this->generate_afs_script( $config ) );
wp_add_inline_script( $init_handle, $post_query_var . $styleIdCustom . "\n" . $raw_ads_js );
}
}
}
/**
* PHP helper function to display ad by configuration ID
*
* @param string $id Configuration ID
* @return string Generated ad HTML (placeholder div)
*/
function adpagex($id) {
$core = AdPageX_Core::get_instance();
// Ensure 'php' type allows multiple insertions if needed, as per the updated get_ad_by_id logic
return $core->get_ad_by_id($id, 'php');
}