*/ private static $sibling_default_modules = array( 'meta_tags', 'noindex', 'sitemap', 'redirects' ); /** * Memo of the crawler tokens Visibility handles, per request. * * @var array|null */ private static $agents = null; /** * Is the Visibility sibling present and loaded? * * @return bool */ public static function is_active() { return defined( 'NATIVE_AEO_PACK_VERSION' ); } /** * Public display name for the sibling, for notices. * * @return string */ public static function name() { return 'Visibility'; } /** * URL of the Visibility screen where its robots.txt options live. * * @return string Empty when the slug constant is not available. */ public static function settings_url() { if ( ! defined( 'NATIVE_AEO_PACK_SLUG' ) ) { return ''; } return admin_url( 'admin.php?page=' . rawurlencode( (string) NATIVE_AEO_PACK_SLUG ) . '&tab=discover' ); } /** * Thickbox URL to install Visibility from the plugin directory. * * @return string */ public static function install_url() { return admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . self::SIBLING_SLUG . '&TB_iframe=true&width=772&height=618' ); } /** * Read a single Visibility setting from a module group. * * @param string $group Module group (meta_tags, noindex, sitemap, modules). * @param string $key Key inside the group. * @param mixed $default Returned when the key is absent. * @return mixed */ public static function setting( $group, $key, $default = null ) { if ( class_exists( 'Native_AEO_Pack_Settings' ) && method_exists( 'Native_AEO_Pack_Settings', 'get' ) ) { return Native_AEO_Pack_Settings::get( $group, $key, $default ); } $option = get_option( self::SIBLING_OPTION, array() ); if ( is_array( $option ) && isset( $option[ $group ] ) && is_array( $option[ $group ] ) && array_key_exists( $key, $option[ $group ] ) ) { return $option[ $group ][ $key ]; } return $default; } /** * Is a top-level Visibility module enabled? * * @param string $module Module key (meta_tags, noindex, sitemap). * @return bool */ public static function module_enabled( $module ) { if ( class_exists( 'Native_AEO_Pack_Settings' ) && method_exists( 'Native_AEO_Pack_Settings', 'is_module_enabled' ) ) { return (bool) Native_AEO_Pack_Settings::is_module_enabled( $module ); } $option = get_option( self::SIBLING_OPTION, array() ); if ( is_array( $option ) && isset( $option['modules'][ $module ] ) ) { return ! empty( $option['modules'][ $module ] ); } return in_array( $module, self::$sibling_default_modules, true ); } /** * Is Visibility writing robots.txt rules for AI crawlers? * * @return bool */ public static function emits_robots_ai() { if ( ! self::is_active() || ! self::module_enabled( 'meta_tags' ) ) { return false; } $block_ai = (bool) self::setting( 'noindex', 'robots_block_ai', false ); $custom = trim( (string) self::setting( 'noindex', 'robots_custom', '' ) ); return $block_ai || '' !== $custom; } /** * Is Visibility managing the physical robots.txt on this site? * * Its physical mode only acts where it owns the root files (a single site, or * the main site of a subdirectory network with no other domains), so where it * does not, it is not doing it and nothing is yielded. * * @return bool */ public static function manages_physical_robots() { if ( ! self::is_active() || ! self::module_enabled( 'meta_tags' ) ) { return false; } if ( ! (bool) self::setting( 'noindex', 'robots_physical', false ) ) { return false; } if ( class_exists( 'Native_AEO_Pack_Robots_Txt' ) && method_exists( 'Native_AEO_Pack_Robots_Txt', 'physical_allowed' ) ) { return (bool) Native_AEO_Pack_Robots_Txt::physical_allowed(); } return ! is_multisite() || is_main_site(); } /** * Should this plugin yield a piece to Visibility? * * True only while Visibility is actually doing that piece. The * `ayudawp_acs_defer_to_visibility` filter (default true) lets a site keep * this plugin's own output even then. * * @param string $signal 'robots' or 'physical_robots'. * @return bool */ public static function should_defer( $signal ) { switch ( $signal ) { case 'robots': $emits = self::emits_robots_ai(); break; case 'physical_robots': $emits = self::manages_physical_robots(); break; default: $emits = false; break; } if ( ! $emits ) { return false; } /** * Filter whether AI Content Signals yields a piece to Visibility. * * Only fires while Visibility is actually doing that piece. Return false * to keep AI Content Signals emitting it anyway. * * @since 1.3.4 * * @param bool $defer Whether to yield to Visibility. Default true. * @param string $signal 'robots' or 'physical_robots'. */ return (bool) apply_filters( 'ayudawp_acs_defer_to_visibility', true, $signal ); } /** * Crawler tokens Visibility writes robots.txt rules for, while it does. * * The crawlers picked in its AI block, plus every User-agent named in its * free-form lines (except the wildcard). Empty when nothing is yielded. * * @return array */ public static function ceded_agents() { if ( null !== self::$agents ) { return self::$agents; } self::$agents = array(); if ( ! self::should_defer( 'robots' ) ) { return self::$agents; } $agents = array(); if ( (bool) self::setting( 'noindex', 'robots_block_ai', false ) ) { if ( class_exists( 'Native_AEO_Pack_Settings' ) && method_exists( 'Native_AEO_Pack_Settings', 'get_robots_ai_agents' ) ) { $agents = (array) Native_AEO_Pack_Settings::get_robots_ai_agents(); } else { $stored = self::setting( 'noindex', 'robots_ai_agents', array() ); $agents = is_array( $stored ) ? $stored : array(); } } $custom = (string) self::setting( 'noindex', 'robots_custom', '' ); foreach ( preg_split( '/\r\n|\r|\n/', $custom ) as $line ) { if ( preg_match( '/^\s*User-agent\s*:\s*(.+?)\s*$/i', (string) $line, $matches ) && '*' !== $matches[1] ) { $agents[] = $matches[1]; } } foreach ( $agents as $agent ) { if ( is_string( $agent ) && '' !== trim( $agent ) ) { self::$agents[] = trim( $agent ); } } self::$agents = array_values( array_unique( self::$agents ) ); return self::$agents; } /** * Is this crawler handled by Visibility right now? * * Compared without case, the way robots.txt parsers match user agents * (RFC 9309, 2.2.1). * * @param string $agent Crawler token as written in this plugin's settings. * @return bool */ public static function is_agent_ceded( $agent ) { $agent = strtolower( trim( (string) $agent ) ); if ( '' === $agent ) { return false; } foreach ( self::ceded_agents() as $ceded ) { if ( strtolower( $ceded ) === $agent ) { return true; } } return false; } /** * Forget the per-request memo (settings changes, tests). */ public static function reset() { self::$agents = null; } }