register_hooks(); } private function register_hooks() { // Enqueue admin scripts and styles add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts']); // Add notices add_action('admin_footer', [$this, 'display_admin_notices']); } public function display_admin_notices() { $screen = get_current_screen(); if (!$screen || !isset($screen->id) || !in_array($screen->id, [ 'toplevel_page_wuadblockguard_settings', 'adblock-guard_page_wuadblockguard_demo_page', 'adblock-guard_page_wuadblockguard_review', 'adblock-guard_page_wuadblockguard_help' ])) { return; } // Warning if not enabled if (get_option('_wuadblockguard_enable') !== 'yes') { Notices::instance()->add_notice( 'wuadblockguard_disabled_notice', 'AdBlock Guard is currently disabled globally. Enable it under the [Detection options] tab. Disable overlays per user role if needed.', 'warning', false, // Non-persistent false, // Non-dismissible null // No action button ); } else { // Ensure at least one detection method is chosen if (empty(get_option('_wuadblockguard_fast_detection')) && empty(get_option('_wuadblockguard_custom_load_js_enable')) && empty(get_option('_wuadblockguard_network_detection')) && empty(get_option('_wuadblockguard_timing_check_enable')) && empty(get_option('_wuadblockguard_remote_detection'))) { Notices::instance()->add_notice( 'wuadblockguard_no_detection_notice', 'AdBlock Guard requires at least one detection method be selected in the [Detection options] tab.', 'error', false, // Non-persistent false, // Non-dismissible null // No action button ); } $default_devices = ['desktop', 'mobile_tablet']; $is_pro_license = (get_option('wuadblockguard_license_status', 'inactive') === 'valid'); // Free/expired licenses always behave as "both selected". if (!$is_pro_license) { $render_devices = $default_devices; $render_devices_enable = false; } else { $render_devices_enable = (get_option('_wuadblockguard_render_devices_enable') === 'yes'); $render_devices = get_option('_wuadblockguard_render_devices', $default_devices); $render_devices = is_array($render_devices) ? $render_devices : []; } $normalized_render_devices = []; foreach ($render_devices as $device) { if ($device === 'desktop') { $normalized_render_devices['desktop'] = true; continue; } // Backward compatibility with old three-option values. if ($device === 'mobile' || $device === 'tablet' || $device === 'mobile_tablet') { $normalized_render_devices['mobile_tablet'] = true; } } $render_devices = array_keys($normalized_render_devices); if ($render_devices_enable && empty($render_devices)) { Notices::instance()->add_notice( 'wuadblockguard_no_render_device_notice', 'AdBlock Guard requires at least one render target device in [Advanced settings] under [Show AdBlock Guard on these device types].', 'error', false, // Non-persistent false, // Non-dismissible null // No action button ); } } // Explicitly display notices to ensure rendering Notices::instance()->display_notices(); } public function enqueue_admin_scripts() { $screen = get_current_screen(); // Ensure $screen is valid if (!$screen) { return; } // Define the allowed screen IDs $allowed_screen_ids = [ 'toplevel_page_wuadblockguard_settings', 'adblock-guard_page_wuadblockguard_demo_page', 'adblock-guard_page_wuadblockguard_license_key', 'adblock-guard_page_wuadblockguard_system_check', ]; // If current screen isn't one of these, bail early if (!in_array($screen->id, $allowed_screen_ids, true)) { return; } // ---- Enqueues for MAIN SETTINGS PAGE ---- if ( strpos($screen->id, 'wuadblockguard_settings') !== false || strpos($screen->id, 'wuadblockguard_demo_page') !== false || strpos($screen->id, 'wuadblockguard_system_check') !== false || strpos($screen->id, 'wuadblockguard_license_key') !== false ) { // JS: admin-linking wp_enqueue_script( 'wuadblockguard_admin_js_linking', ADBLOCKGUARD_PLUGIN_URL . 'assets/js/admin-linking.js', ['wp-i18n'], ADBLOCKGUARD_VERSION, true ); // JS: form-dirty wp_enqueue_script( 'wuadblockguard_admin_form_dirty', ADBLOCKGUARD_PLUGIN_URL . 'assets/js/admin-form-dirty.js', ['wp-i18n'], ADBLOCKGUARD_VERSION, true ); // JS: main admin wp_enqueue_script( 'wuadblockguard_admin_js', ADBLOCKGUARD_PLUGIN_URL . 'assets/js/admin.js', ['wp-i18n'], ADBLOCKGUARD_VERSION, true ); // JS: admin notices wp_enqueue_script( 'wuadblockguard_admin_js_notices', ADBLOCKGUARD_PLUGIN_URL . 'assets/js/admin-notices.js', ['wp-i18n'], ADBLOCKGUARD_VERSION, true ); // JS: validation wp_enqueue_script( 'wuadblockguard_admin_validation_js', ADBLOCKGUARD_PLUGIN_URL . 'assets/js/admin-validation.js', ['wp-i18n'], ADBLOCKGUARD_VERSION, true ); // Thickbox add_thickbox(); // JS: demo wp_enqueue_script( 'wuadblockguard_admin_js_demo', ADBLOCKGUARD_PLUGIN_URL . 'assets/js/demo.js', ['wp-i18n'], ADBLOCKGUARD_VERSION, true ); $this->enableEncoding(); if (!defined('ADBLOCKGUARD_IS_PRO')) { wp_enqueue_script( 'wuadblockguard_free_js', ADBLOCKGUARD_PLUGIN_URL . 'assets/js/free.js', ['wp-i18n'], ADBLOCKGUARD_VERSION, true ); } else { wp_enqueue_script( 'wuadblockguard_paid_js', ADBLOCKGUARD_PLUGIN_URL . 'assets/js/paid.js', ['wp-i18n'], ADBLOCKGUARD_VERSION, true ); } wp_enqueue_style( 'wuadblockguard_admin_css', ADBLOCKGUARD_PLUGIN_URL . 'assets/css/admin.css', [], ADBLOCKGUARD_VERSION ); wp_enqueue_style( 'wuadblockguard_form_styles_css', ADBLOCKGUARD_PLUGIN_URL . 'assets/css/form-styles.css', [], ADBLOCKGUARD_VERSION ); } // ---- Localization ---- wp_localize_script( 'wuadblockguard_admin_js', 'customAdminData', [ 'proFeatureMessage' => 'This is a pro feature', ] ); } public function wuadblockguard_admin_notice($message, $type = 'warning', $dismissible = false) { add_action('admin_notices', function () use ($message, $type, $dismissible) { // Ensure the type is one of the allowed values $allowed_types = ['warning', 'error', 'info', 'success']; if (!in_array($type, $allowed_types)) { $type = 'warning'; } // Capitalize the first letter of the type for display $type_display = ucfirst($type); // Check if we are on one of the plugin's admin pages $screen = get_current_screen(); // Determine the class based on whether the notice is dismissible $class = $dismissible ? 'is-dismissible' : ''; if ($screen && isset($screen->id) && ( $screen->id === 'toplevel_page_wuadblockguard_settings' || $screen->id === 'adblock-guard_page_wuadblockguard_demo_page' || $screen->id === 'adblock-guard_page_wuadblockguard_review' || $screen->id === 'adblock-guard_page_wuadblockguard_help' )) { // Output the notice with the message echo sprintf( '

%3$s: %4$s

', esc_attr($type), esc_html($class), esc_html($type_display), wp_kses_post($message) ); } }); } private function enableEncoding() { if (LicenseChecker::getInstance()->isLicenseValid()) { if (!defined('ADBLOCKGUARD_IS_PRO')) { define('ADBLOCKGUARD_IS_PRO', true); } } } }