plugin_name = $plugin_name; $this->version = $version; $this->options = $options; } /** * Return the domain to use in Google Preferred Source links. * Uses the saved custom_domain option when set, otherwise falls back to home_url(). */ private function get_google_domain() { $custom = isset( $this->options['custom_domain'] ) ? trim( $this->options['custom_domain'] ) : ''; if ( ! empty( $custom ) ) { return $custom; } return wp_parse_url( home_url(), PHP_URL_HOST ); } /** * Register the stylesheets for the public-facing side of the site. */ public function enqueue_styles() { // Only include the stylesheet if the banner is enabled if (isset($this->options['enabled']) && $this->options['enabled']) { wp_enqueue_style($this->plugin_name, ADASPRSO_PLUGIN_URL . 'public/css/add-as-preferred-source-public.css', array(), $this->version, 'all'); } } /** * Register the JavaScript for the public-facing side of the site. */ public function enqueue_scripts() { // Only include the script if the banner is enabled if (isset($this->options['enabled']) && $this->options['enabled']) { wp_enqueue_script($this->plugin_name, ADASPRSO_PLUGIN_URL . 'public/js/add-as-preferred-source-public.js', array(), $this->version, true); // Localize script with options wp_localize_script($this->plugin_name, 'adasprsoOptions', array( 'domain' => $this->get_google_domain(), 'bannerTrigger' => isset($this->options['banner_trigger']) ? $this->options['banner_trigger'] : 'immediate', 'bannerScrollPercent' => isset($this->options['banner_scroll_percent']) ? (int) $this->options['banner_scroll_percent'] : 50, 'bannerDelaySeconds' => isset($this->options['banner_delay_seconds']) ? (int) $this->options['banner_delay_seconds'] : 5, 'ajaxUrl' => admin_url('admin-ajax.php'), 'trackNonce' => wp_create_nonce('adasprso_track_nonce'), )); } } /** * Display the preferred source banner in the footer. * * @since 1.0.0 */ public function display_preferred_source_banner() { // Check if the banner is enabled if (!isset($this->options['enabled']) || !$this->options['enabled']) { return; } // Check device-specific settings $is_mobile = wp_is_mobile(); $show_on_mobile = isset($this->options['show_on_mobile']) ? $this->options['show_on_mobile'] : 1; $show_on_desktop = isset($this->options['show_on_desktop']) ? $this->options['show_on_desktop'] : 1; // Don't show if device settings don't allow it if (($is_mobile && !$show_on_mobile) || (!$is_mobile && !$show_on_desktop)) { return; } // Check display rules if (!$this->should_display()) { return; } // Load the banner template include_once ADASPRSO_PLUGIN_PATH . 'public/partials/add-as-preferred-source-public-display.php'; } /** * Determine whether the banner should be shown on the current page * based on the Display Rules settings. * * @since 1.1 * @return bool */ private function should_display() { $display_on = isset($this->options['display_on']) ? $this->options['display_on'] : 'everywhere'; // Broad context filter if ($display_on === 'posts_only' && !is_singular('post')) { return false; } if ($display_on === 'pages_only' && !is_page()) { return false; } if ($display_on === 'front_page_only' && !is_front_page()) { return false; } // Exclude specific post / page IDs $exclude_ids = $this->parse_ids(isset($this->options['exclude_post_ids']) ? $this->options['exclude_post_ids'] : ''); if (!empty($exclude_ids) && is_singular() && in_array(get_the_ID(), $exclude_ids, true)) { return false; } // Exclude categories $exclude_cats = isset($this->options['exclude_categories']) ? (array) $this->options['exclude_categories'] : array(); $exclude_cats = array_filter(array_map('intval', $exclude_cats)); if (!empty($exclude_cats) && is_singular('post') && has_category($exclude_cats)) { return false; } return true; } /** * Parse a comma-separated string of IDs into an array of integers. * * @since 1.1 * @param string $str * @return int[] */ private function parse_ids($str) { if (empty($str)) { return array(); } return array_values(array_filter(array_map('intval', explode(',', $str)))); } /** * Get processed banner text with website name replacement * * @since 1.0.0 * @return string The processed banner text. */ public function get_processed_banner_text() { $banner_text = isset($this->options['banner_text']) ? $this->options['banner_text'] : 'Add as a preferred source on Google'; $website_name = isset($this->options['website_name']) ? $this->options['website_name'] : get_bloginfo('name'); // Replace {website_name} placeholder with actual website name $processed_text = str_replace('{website_name}', $website_name, $banner_text); return $processed_text; } /** * Add the inline button to the content. * * @since 1.0.0 * @param string $content The content. * @return string The content with the inline button appended/prepended. */ public function add_inline_button_to_content($content) { // Check if inline button is enabled if (!isset($this->options['inline_enabled']) || !$this->options['inline_enabled']) { return $content; } // Only display on single posts/pages and in the main query if (!is_singular() || !is_main_query()) { return $content; } // Check Post Type $post_types = isset($this->options['inline_post_types']) ? $this->options['inline_post_types'] : array('post'); if (!in_array(get_post_type(), $post_types, true)) { return $content; } // Get Settings $position = isset($this->options['inline_position']) ? $this->options['inline_position'] : 'both'; $inline_text = isset($this->options['inline_text']) ? $this->options['inline_text'] : 'Stay connected via Google News'; $inline_subtitle = isset($this->options['inline_subtitle']) ? $this->options['inline_subtitle'] : 'Follow us for the latest travel updates and guides.'; $inline_bg_color = isset($this->options['inline_bg_color']) ? $this->options['inline_bg_color'] : '#ffffff'; $inline_text_color = isset($this->options['inline_text_color']) ? $this->options['inline_text_color'] : '#1c1e21'; $google_button_image = ADASPRSO_PLUGIN_URL . 'public/images/google-preferred-source-button.png'; // Generate Google Preferences URL using the configured (or auto-detected) domain $adasprso_google_url = 'https://www.google.com/preferences/source?q=' . urlencode( $this->get_google_domain() ); // Start Output Buffering ob_start(); include ADASPRSO_PLUGIN_PATH . 'public/partials/add-as-preferred-source-public-inline.php'; $button_html = ob_get_clean(); // Inject based on position if ($position === 'above') { $content = $button_html . $content; } elseif ($position === 'below') { $content = $content . $button_html; } elseif ($position === 'both') { $content = $button_html . $content . $button_html; } return $content; } }