is_module_active( 'jsonld' ) ) { return; } $bundle = AEO_Connect_Cache::get_bundle(); if ( null === $bundle || empty( $bundle['modules']['jsonld'] ) || ! is_array( $bundle['modules']['jsonld'] ) ) { return; } $current_path = $this->current_path(); $seo_plugin_active = defined( 'WPSEO_VERSION' ) || defined( 'RANK_MATH_VERSION' ); $output = ''; foreach ( $bundle['modules']['jsonld'] as $item ) { if ( ! is_array( $item ) || empty( $item['page'] ) || empty( $item['schema'] ) || ! is_array( $item['schema'] ) ) { continue; } if ( ! $this->path_matches( (string) $item['page'], $current_path ) ) { continue; } $type = isset( $item['schema']['@type'] ) ? $item['schema']['@type'] : ''; if ( $seo_plugin_active && is_string( $type ) && in_array( $type, self::SEO_PLUGIN_TYPES, true ) ) { continue; // Avoid duplicating Yoast / Rank Math graphs. } $json = wp_json_encode( $item['schema'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); if ( false === $json ) { continue; } // Hardening: neutralize a potential breakout inside string // values. `<\/` decodes to the exact same JSON data as `' . $json . '' . "\n"; } if ( '' !== $output ) { echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- JSON safely encoded via wp_json_encode with normalize_path( $path ); } /** * Whether a bundle page path matches the current request. * * '/' matches the front page (is_front_page()); any other path requires * an exact (trailing-slash-insensitive) match. * * @param string $page Page path from the bundle item. * @param string $current_path Normalized current path. * @return bool */ private function path_matches( $page, $current_path ) { $page = trim( (string) $page ); if ( '' === $page ) { return false; } if ( '/' === $page ) { return is_front_page() || '/' === $current_path; } return $this->normalize_path( $page ) === $current_path; } /** * Normalize a path: leading slash, no trailing slash, '/' for the root. * * @param string $path Raw path. * @return string */ private function normalize_path( $path ) { $path = (string) $path; if ( '' === $path ) { return '/'; } if ( '/' !== $path[0] ) { $path = '/' . $path; } $path = untrailingslashit( $path ); return '' === $path ? '/' : $path; } /* --------------------------------------------------------------------- * meta / Open Graph (only-if-absent) * ------------------------------------------------------------------ */ /** * Map of bundle `meta` field → ['attr', 'name'] for the tag. * * @var array */ const META_TAG_MAP = array( 'description' => array( 'name', 'description' ), 'og_title' => array( 'property', 'og:title' ), 'og_description' => array( 'property', 'og:description' ), 'og_url' => array( 'property', 'og:url' ), 'og_type' => array( 'property', 'og:type' ), 'twitter_card' => array( 'name', 'twitter:card' ), 'twitter_title' => array( 'name', 'twitter:title' ), 'twitter_description' => array( 'name', 'twitter:description' ), ); /** * Print the bundle's meta / Open Graph tags on wp_head (priority 6). * * Anti-conflict: if Yoast (WPSEO_VERSION) or Rank Math (RANK_MATH_VERSION) * is active, skip entirely — those plugins already emit description and * Open Graph. The plugin NEVER emits a (WordPress owns it). This * module targets sites WITHOUT a dedicated SEO plugin (the common case for * the under-optimized sites we connect). * * @return void */ public function inject_meta() { if ( ! $this->is_module_active( 'meta' ) ) { return; } if ( defined( 'WPSEO_VERSION' ) || defined( 'RANK_MATH_VERSION' ) ) { return; // Let the SEO plugin own description / Open Graph. } $bundle = AEO_Connect_Cache::get_bundle(); if ( null === $bundle || empty( $bundle['modules']['meta'] ) || ! is_array( $bundle['modules']['meta'] ) ) { return; } $meta = $bundle['modules']['meta']; $output = ''; foreach ( self::META_TAG_MAP as $field => $spec ) { if ( empty( $meta[ $field ] ) || ! is_string( $meta[ $field ] ) ) { continue; } $attr = $spec[0]; $name = $spec[1]; $value = $meta[ $field ]; $output .= '<meta ' . $attr . '="' . esc_attr( $name ) . '" content="' . esc_attr( $value ) . '" class="aeo-tool-connect" />' . "\n"; } if ( '' !== $output ) { echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- each attribute escaped via esc_attr above. } } /* --------------------------------------------------------------------- * robots.txt (append-only) * ------------------------------------------------------------------ */ /** * Append the bundle's AI-bot directives AFTER the existing robots.txt * output. Never replaces anything. * * @param string $output Current robots.txt output. * @param bool $is_public Whether the site is public. * @return string */ public function append_robots_directives( $output, $is_public ) { unset( $is_public ); if ( ! $this->is_module_active( 'robots' ) ) { return $output; } $bundle = AEO_Connect_Cache::get_bundle(); if ( null === $bundle || ! isset( $bundle['modules']['robots_txt']['content'] ) ) { return $output; } $content = $bundle['modules']['robots_txt']['content']; if ( ! is_string( $content ) || '' === trim( $content ) ) { return $output; } $output = rtrim( (string) $output ); $output .= "\n\n# --- AEO Tool Connect (AI bots) ---\n" . trim( $content ) . "\n"; return $output; } /* --------------------------------------------------------------------- * Virtual /llms.txt * ------------------------------------------------------------------ */ /** * Register the rewrite rule for the virtual /llms.txt — only when no * physical llms.txt file exists at the WordPress root. * * Static so the activation hook can call it before flushing rules. * * @return void */ public static function register_llms_rewrite() { if ( file_exists( ABSPATH . 'llms.txt' ) ) { return; } add_rewrite_rule( '^llms\.txt$', 'index.php?' . self::LLMS_QUERY_VAR . '=1', 'top' ); } /** * Register the llms.txt query var. * * @param string[] $vars Public query vars. * @return string[] */ public function register_query_var( $vars ) { $vars[] = self::LLMS_QUERY_VAR; return $vars; } /** * Serve the virtual llms.txt as text/plain. * * @return void */ public function maybe_serve_llms_txt() { if ( '1' !== (string) get_query_var( self::LLMS_QUERY_VAR ) ) { return; } if ( file_exists( ABSPATH . 'llms.txt' ) ) { return; // A physical file appeared since activation: never shadow it. } $content = ''; if ( $this->is_module_active( 'llms' ) ) { $bundle = AEO_Connect_Cache::get_bundle(); if ( null !== $bundle && isset( $bundle['modules']['llms_txt']['content'] ) && is_string( $bundle['modules']['llms_txt']['content'] ) ) { $content = $bundle['modules']['llms_txt']['content']; } } if ( '' === trim( $content ) ) { global $wp_query; if ( $wp_query instanceof WP_Query ) { $wp_query->set_404(); } status_header( 404 ); return; } status_header( 200 ); header( 'Content-Type: text/plain; charset=utf-8' ); header( 'X-Content-Type-Options: nosniff' ); header( 'Cache-Control: max-age=3600' ); echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- plain-text body served with an explicit text/plain content type; content sanitized on intake. exit; } }