← Back
Editing: class-gdm-visibility.php
<?php namespace WPaaS; if ( ! defined( 'ABSPATH' ) ) { exit; } final class GDM_Visibility { use Helpers; /** * Length of the GoDaddy Digital Marketing free trial, in days. The banner is * only shown while the site is still within this window (approximated by the * site creation date). * * @var int */ const TRIAL_LENGTH_DAYS = 30; public function __construct() { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_js_scripts' ), - PHP_INT_MAX ); // Defer to `admin_init` so the translated banner strings in // register_banner() are not requested before the `init` action; loading // translations earlier triggers WP 6.7+'s "_load_textdomain_just_in_time // was called incorrectly" notice. admin_init fires after init (warning // avoided) and only in the admin, so the admin-only banner work is // skipped on front-end requests. Admin\Notice registers on admin_notices, // which fires after admin_init, so this does not change when it shows. add_action( 'admin_init', array( $this, 'register_banner' ) ); } /** * Register the GDM Visibility banner, or fall back to the Site Optimizer * banner once the GDM Visibility banner has been dismissed. * * @return void */ public function register_banner() { if ( ! is_admin() || ! Plugin::is_gd() ) { return; } $message = self::get_notice_text(); /** * Show the GDM Visibility banner only when we have something to route * with (get_notice_text() returns an empty string otherwise) and the * customer has not dismissed it. In either case fall back to the legacy * Site Optimizer banner. */ if ( empty( $message ) || self::is_dismissed( $message ) ) { new Admin\Notice( Site_Optimizer::get_notice_text(), array( 'notice-success' ), 'activate_plugins', true ); return; } new Admin\Notice( $message, array( 'notice-success' ), 'activate_plugins', true ); } /** * Provide the correct wording for the notice. * * @return string */ public static function get_notice_text() { $domain = wp_parse_url( home_url(), PHP_URL_HOST ); /** * Gate the GDM Visibility banner off (register_banner() then falls back to * the Site Optimizer banner) when either: * - the site is outside its Digital Marketing free-trial window, since the * banner's purpose is to get trialing customers into the trial; or * - we have no domain to route to Marketing with. * The CTA routes to Marketing keyed by the site's domain; Marketing resolves * which venture owns that domain and redirects to it. */ if ( ! self::is_within_trial_window() || empty( $domain ) ) { return ''; } // Build the Marketing host per environment: dev/test use the // `{env}-godaddy.com` hosts, prod uses `godaddy.com` (mirrors class-rum.php). $env = Plugin::get_env(); $host = in_array( $env, array( 'dev', 'test' ), true ) ? "marketing.{$env}-godaddy.com" : 'marketing.godaddy.com'; $link = esc_url( 'https://' . $host . '/mwp?domain=' . rawurlencode( $domain ) . '&source=wpadmin_banner' ); $title = esc_html__( 'Discover how visible your business is to AI search', 'gd-system-plugin' ); $message = esc_html__( 'See where you stand and how to improve with your GoDaddy Digital Marketing tools. ', 'gd-system-plugin' ); $message .= '<a href="' . $link . '" target="_blank" rel="noopener" id="check-my-ai-visibility-banner">' . esc_html__( 'Check my AI visibility', 'gd-system-plugin' ) . '</a>'; $format = ' <div> <div> <div><strong>' . $title . '</strong></div> ' . $message . ' </div> </div> '; return $format; } /** * Whether the site is still within the Digital Marketing free-trial window, * approximated by the site creation date. Returns false when GD_SITE_CREATED * is undefined, so we err on not showing the banner. * * @return bool */ private static function is_within_trial_window() { return self::is_site_created_after( strtotime( '-' . self::TRIAL_LENGTH_DAYS . ' days' ) ); } /** * Whether the given banner message has been dismissed. * * Matches how Admin\Notice derives its dismissal key: from the message after * the `wpaas_admin_notice_message` filter runs. Hashing the raw message here * would diverge from the key stored by wpaas-notice.js if that filter ever * alters the message. * * @param string $message * * @return bool */ private static function is_dismissed( $message ) { $filtered = (string) apply_filters( 'wpaas_admin_notice_message', $message ); return (bool) get_option( 'wpaas_dismissed_' . md5( $filtered ) ); } /** * Enqueue the CTA handler for whichever banner is being shown. * * @return void */ public function enqueue_js_scripts() { // Match the banner's capability gate so we don't load the CTA handler for // users who can't see the notice (Admin\Notice requires activate_plugins). if ( ! Plugin::is_gd() || ! current_user_can( 'activate_plugins' ) ) { return; } $message = self::get_notice_text(); if ( empty( $message ) || self::is_dismissed( $message ) ) { // The Site Optimizer fallback is showing; load its CTA handler. wp_enqueue_script( 'airo-site-optimizer', Plugin::assets_url( 'js/airo-site-optimizer.js' ), array( 'jquery' ), Plugin::version() ); return; } wp_enqueue_script( 'gdm-visibility', Plugin::assets_url( 'js/gdm-visibility.js' ), array( 'jquery' ), Plugin::version() ); } }
Save File
Cancel