true, 'contact' => true, 'legal' => false, ]; const PRIVACY_SLUGS = ['privacy-policy', 'privacy', 'privacy-notice', 'privacy-statement', 'data-protection', 'datenschutz', 'datenschutzerklaerung', 'politica-de-privacidad', 'politique-de-confidentialite', 'informativa-privacy', 'privacybeleid']; const PRIVACY_TITLE = '/^(privacy( policy| notice| statement)?|data protection|datenschutz|pol[ií]tica de privacidad|politique de confidentialit[ée])\b/iu'; const CONTACT_SLUGS = ['contact', 'contact-us', 'contact-me', 'contacts', 'get-in-touch', 'kontakt', 'contacto', 'contatti', 'nous-contacter', 'rabta']; const LEGAL_SLUGS = ['terms', 'terms-of-service', 'terms-and-conditions', 'terms-of-use', 'disclaimer', 'legal', 'imprint', 'impressum', 'cookie-policy', 'dmca']; public function init() { add_action('add_meta_boxes', [$this, 'add_meta_box']); add_action('save_post', [$this, 'save_meta_box'], 10, 2); add_action('admin_init', function () { foreach (self::post_types() as $type) { add_filter("manage_{$type}_posts_columns", [$this, 'add_column']); add_action("manage_{$type}_posts_custom_column", [$this, 'render_column'], 10, 2); } }); add_action('admin_post_adghost_toggle_page_ads', [$this, 'handle_toggle']); } public static function post_types() { $types = get_post_types(['public' => true, 'show_ui' => true]); unset($types['attachment']); return array_values($types); } public static function rules() { $saved = get_option('adghost_no_ads_pages', null); if (!is_array($saved)) { return self::RULES; } $out = []; foreach (self::RULES as $key => $default) { $out[$key] = !empty($saved[$key]); } return $out; } public static function reason() { if (!is_singular()) { return null; } $post = get_queried_object(); if (!$post instanceof WP_Post) { return null; } if (get_post_meta($post->ID, AdGhost_Targeting::DISABLE_META, true)) { return 'Ads are switched off for this page'; } $rules = self::rules(); if ($rules['privacy'] && self::is_privacy_page($post)) { return 'This is the privacy policy page (Settings → Pages without ads)'; } if ($post->post_type === 'page') { if ($rules['contact'] && self::matches($post, self::CONTACT_SLUGS, '/^(contact|get in touch|kontakt|contacto)\b/i')) { return 'This is a contact page (Settings → Pages without ads)'; } if ($rules['legal'] && self::matches($post, self::LEGAL_SLUGS, '/^(terms|disclaimer|legal|imprint|impressum|cookie policy)\b/i')) { return 'This is a terms or legal page (Settings → Pages without ads)'; } } return null; } private static function is_privacy_page(WP_Post $post) { if ((int) get_option('wp_page_for_privacy_policy') === (int) $post->ID) { return true; } return $post->post_type === 'page' && self::matches($post, self::PRIVACY_SLUGS, self::PRIVACY_TITLE); } private static function matches(WP_Post $post, array $slugs, $title_pattern) { return in_array($post->post_name, $slugs, true) || preg_match($title_pattern, trim((string) $post->post_title)); } public static function affected_pages() { $out = []; $privacy = (int) get_option('wp_page_for_privacy_policy'); if ($privacy && get_post_status($privacy) === 'publish') { $out[$privacy] = ['id' => $privacy, 'title' => get_the_title($privacy), 'why' => 'privacy', 'edit' => (string) get_edit_post_link($privacy, 'raw')]; } $slugs = array_merge(self::PRIVACY_SLUGS, self::CONTACT_SLUGS, self::LEGAL_SLUGS); foreach (get_posts(['post_type' => 'page', 'post_status' => 'publish', 'post_name__in' => $slugs, 'numberposts' => 30]) as $page) { $why = in_array($page->post_name, self::PRIVACY_SLUGS, true) ? 'privacy' : (in_array($page->post_name, self::CONTACT_SLUGS, true) ? 'contact' : 'legal'); $out[$page->ID] = ['id' => $page->ID, 'title' => get_the_title($page), 'why' => $why, 'edit' => (string) get_edit_post_link($page->ID, 'raw')]; } foreach (get_posts(['post_type' => self::post_types(), 'post_status' => 'any', 'meta_key' => AdGhost_Targeting::DISABLE_META, 'meta_value' => '1', 'numberposts' => 50]) as $page) { // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value $out[$page->ID] = ['id' => $page->ID, 'title' => get_the_title($page), 'why' => 'manual', 'edit' => (string) get_edit_post_link($page->ID, 'raw')]; } return array_values($out); } public function add_meta_box() { foreach (self::post_types() as $type) { add_meta_box('adghost-page-ads', __('AdGhost', 'adghost-lite'), [$this, 'render_meta_box'], $type, 'side', 'high'); } } public function render_meta_box($post) { wp_nonce_field('adghost_page_ads', 'adghost_page_ads_nonce'); $off = (bool) get_post_meta($post->ID, AdGhost_Targeting::DISABLE_META, true); ?>
' . esc_html__('Off', 'adghost-lite') . '