*/ public static function defaults(): array { return array( // auto — decide from what is installed (recommended) // solo — we own everything, the other plugin is overruled // geo — they keep classic SEO, we add the AI layer // manual— use the ownership map below verbatim 'mode' => 'auto', 'enforce' => true, // Run the duplicate-tag firewall on rendered pages. 'ownership' => array( 'description' => 'airs', 'canonical' => 'airs', 'robots' => 'airs', 'open_graph' => 'airs', 'twitter' => 'airs', 'schema' => 'airs', 'hreflang' => 'airs', ), ); } public function register( Container $container ): void {} public function boot( Container $container ): void { $this->options = $container->get( Options::class ); add_action( 'rest_api_init', array( $this, 'routes' ) ); $settings = $this->settings(); if ( empty( $settings['enforce'] ) ) { return; } /* * Only worth the buffer when somebody else is actually writing tags — * but "somebody else" used to mean "one of the ten plugins we can name", * and that is the wrong test. A white-labelled build, a theme with its * own SEO fields or a niche plugin writes a canonical just as happily * and defines no constant we recognise; on those sites the firewall * never started and the customer got two canonicals. * * Three ways in now. A named plugin, as before. A previous scan that * found foreign tags, whoever wrote them. And an explicit choice of * "take over" or "per tag": those are statements about the page, not * about a plugin list, and honouring them cannot depend on our being * able to name the competitor. */ $mode = (string) $settings['mode']; if ( ! in_array( $mode, array( 'solo', 'manual' ), true ) && ! Detector::any_competitor() ) { return; } $arbiter = new HeadArbiter( $this->effective_ownership() ); // PHP_INT_MIN/MAX so we wrap every other wp_head callback, whatever // priority it registered at. Guard::action( 'wp_head', array( $arbiter, 'start' ), PHP_INT_MIN ); Guard::action( 'wp_head', array( $arbiter, 'finish' ), PHP_INT_MAX ); // Let the title filter agree with the ownership map: when the other // plugin owns the title we simply do not bid for it. if ( 'airs' !== ( $this->effective_ownership()['title'] ?? 'airs' ) ) { add_filter( 'airanksuite/seo/emit_title', '__return_false' ); } } /** * Does AI Rank Suite own this surface right now? * * Modules call this before emitting so a surface owned by the other plugin * never even gets rendered — cheaper, and safer, than stripping it later. */ public static function owns( string $surface ): bool { if ( in_array( $surface, self::OURS_ALONE, true ) ) { return true; } return 'other' !== ( self::ownership()[ $surface ] ?? 'airs' ); } /** * The ownership map after the mode preset is applied. * * Static because every module needs to consult it during boot, before any * particular module instance is reachable. * * @return array */ public static function ownership(): array { $settings = \AiRankSuite\Kernel::service( Options::class )->get( 'coexistence', self::defaults() ); $mode = (string) $settings['mode']; $map = (array) $settings['ownership']; $other = Detector::primary(); if ( 'auto' === $mode ) { /* * Deferring is only safe to a plugin we can name. * * "GEO layer only" means we stop emitting a description and trust * theirs to be there. That is a reasonable bet on Yoast; it is not * a bet worth taking on a writer we could not identify, because if * we are wrong the page ends up with no description at all. So an * unnamed competitor resolves to taking over instead — we emit, * the firewall strips their copy, and the page has exactly one of * everything either way. */ $mode = null === $other ? 'solo' : 'geo'; } if ( 'solo' === $mode ) { $map = array_fill_keys( array_keys( HeadArbiter::surfaces() ), 'airs' ); $map['title'] = 'airs'; return $map; } if ( 'geo' === $mode ) { // They keep everything classic. We keep structured data, because // FAQ/HowTo/speakable are what AI assistants actually read, and // "both" would double the @graph. $map = array( 'title' => 'other', 'description' => 'other', 'canonical' => 'other', 'robots' => 'other', 'open_graph' => 'other', 'twitter' => 'other', 'hreflang' => 'other', 'schema' => 'airs', ); /** * Filter the GEO-only ownership preset. * * @param array $map Surface => owner. */ return (array) apply_filters( 'airanksuite/coexistence/geo_preset', $map ); } $map['title'] = $map['title'] ?? 'airs'; return $map; } /** @return array */ private function settings(): array { $options = $this->options ?? \AiRankSuite\Kernel::service( Options::class ); return $options->get( 'coexistence', self::defaults() ); } /** Instance shim so REST callbacks read the same map the front end uses. */ public function effective_ownership(): array { return self::ownership(); } public function routes(): void { register_rest_route( self::NAMESPACE, '/coexistence', array( array( 'methods' => 'GET', 'callback' => array( $this, 'get_state' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ), array( 'methods' => 'PUT', 'callback' => array( $this, 'update' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'mode' => array( 'type' => 'string', 'enum' => array( 'auto', 'solo', 'geo', 'manual' ), ), 'enforce' => array( 'type' => 'boolean' ), 'ownership' => array( 'type' => 'object' ), ), ), ) ); register_rest_route( self::NAMESPACE, '/coexistence/scan', array( 'methods' => 'POST', 'callback' => array( $this, 'scan' ), // A scan fetches real pages — cap it. 'permission_callback' => static function () { if ( ! current_user_can( 'manage_options' ) ) { return self::forbidden(); } return self::spend( 'coexscan', 30 ); }, 'args' => array( 'url' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_url', ), ), ) ); } public function get_state(): WP_REST_Response { $surfaces = array(); foreach ( HeadArbiter::surfaces() as $id => $def ) { $surfaces[] = array( 'id' => $id, 'label' => $def['label'], 'desc' => $def['desc'], ); } array_unshift( $surfaces, array( 'id' => 'title', 'label' => __( 'Title tag', 'ai-rank-suite' ), 'desc' => __( 'The clickable headline in search results.', 'ai-rank-suite' ), ) ); return new WP_REST_Response( array( 'settings' => $this->settings(), 'effective' => $this->effective_ownership(), 'detected' => Detector::active(), 'primary' => Detector::primary(), 'unknown' => Detector::unknown_writer(), 'surfaces' => $surfaces, ) ); } public function update( WP_REST_Request $request ): WP_REST_Response { $defaults = self::defaults(); $values = array(); foreach ( array( 'mode', 'enforce' ) as $key ) { if ( $request->has_param( $key ) ) { $values[ $key ] = $request->get_param( $key ); } } if ( $request->has_param( 'ownership' ) ) { $allowed = array_merge( array_keys( HeadArbiter::surfaces() ), array( 'title' ) ); $clean = array(); foreach ( (array) $request->get_param( 'ownership' ) as $surface => $owner ) { $surface = sanitize_key( (string) $surface ); $owner = sanitize_key( (string) $owner ); if ( in_array( $surface, $allowed, true ) && in_array( $owner, array( 'airs', 'other', 'both' ), true ) ) { $clean[ $surface ] = $owner; } } $values['ownership'] = $clean; } $options = $this->options ?? \AiRankSuite\Kernel::service( Options::class ); $options->set( 'coexistence', $values, $defaults ); return $this->get_state(); } /** * Fetch a real page and count duplicate tags — proof, not a promise. */ public function scan( WP_REST_Request $request ): WP_REST_Response|WP_Error { $url = (string) ( $request->get_param( 'url' ) ?: home_url( '/' ) ); // Only ever scan this site: a scanner that fetches arbitrary URLs is an // SSRF hole with a settings screen. if ( 0 !== strpos( $url, home_url() ) ) { return new WP_Error( 'airanksuite_coex_offsite', __( 'The scan only runs against pages on this site.', 'ai-rank-suite' ), array( 'status' => 400 ) ); } $response = wp_remote_get( add_query_arg( 'airanksuite_scan', (string) time(), $url ), array( 'timeout' => 20, 'user-agent' => 'AI Rank Suite Coexistence Scan', ) ); if ( $response instanceof WP_Error ) { return $response; } $code = (int) wp_remote_retrieve_response_code( $response ); if ( $code >= 400 ) { return new WP_Error( 'airanksuite_coex_fetch', sprintf( /* translators: %d: HTTP status code. */ __( 'That page returned HTTP %d.', 'ai-rank-suite' ), $code ), array( 'status' => 502 ) ); } $html = (string) wp_remote_retrieve_body( $response ); $head = false !== stripos( $html, '' ) ? substr( $html, 0, (int) stripos( $html, '' ) ) : $html; $census = HeadArbiter::census( $head ); /* * Four surfaces WordPress core never writes: a description, Open * Graph, a Twitter card or JSON-LD found outside our markers can only * have come from something else. `robots` and `canonical` are no use * here — core emits both — so a foreign copy of those proves nothing. */ $foreign = false; foreach ( array( 'description', 'open_graph', 'twitter', 'schema' ) as $tell ) { if ( ( $census[ $tell ]['theirs'] ?? 0 ) > 0 ) { $foreign = true; break; } } Detector::note_unknown_writer( $foreign && empty( Detector::active() ) ); $duplicates = 0; $rows = array(); foreach ( HeadArbiter::surfaces() as $id => $def ) { $c = $census[ $id ] ?? array( 'ours' => 0, 'theirs' => 0, ); $total = $c['ours'] + $c['theirs']; // Open Graph and JSON-LD legitimately appear many times; what // matters is whether two *sources* both wrote them. $clash = $c['ours'] > 0 && $c['theirs'] > 0; if ( $clash ) { ++$duplicates; } $rows[] = array( 'id' => $id, 'label' => $def['label'], 'ours' => $c['ours'], 'theirs' => $c['theirs'], 'total' => $total, 'clash' => $clash, ); } return new WP_REST_Response( array( 'url' => $url, 'checked_at' => time(), 'duplicates' => $duplicates, 'clean' => 0 === $duplicates, 'unknown' => Detector::unknown_writer(), 'rows' => $rows, ) ); } }