array( 'Article', 'NewsArticle', 'BlogPosting', 'TechArticle', 'ScholarlyArticle' ), 'NewsArticle' => array( 'Article', 'NewsArticle', 'BlogPosting' ), 'BlogPosting' => array( 'Article', 'NewsArticle', 'BlogPosting' ), 'Product' => array( 'Product' ), 'Recipe' => array( 'Recipe' ), 'Event' => array( 'Event' ), 'LocalBusiness' => array( 'LocalBusiness', 'Organization' ), 'Organization' => array( 'Organization' ), 'Person' => array( 'Person' ), 'FAQPage' => array( 'FAQPage' ), 'HowTo' => array( 'HowTo' ), 'Review' => array( 'Review' ), 'Course' => array( 'Course' ), 'VideoObject' => array( 'VideoObject' ), 'ImageObject' => array( 'ImageObject' ), 'BreadcrumbList' => array( 'BreadcrumbList' ), 'Service' => array( 'Service' ), ); /** * Types that Rank Math should keep unless Schema AI explicitly provides them. * * @var array */ private $protected_types = array( 'WebSite', 'WebPage', 'BreadcrumbList', 'SearchAction', 'CollectionPage', 'ProfilePage' ); /** * Suppress all Rank Math JSON-LD output when Schema AI is active. * * @param array $data Associative array of schema pieces. * @param object $jsonld The Rank Math JsonLD object instance. * @return array Empty array — Schema AI owns all schema output. */ public function filter_rank_math_schema( $data, $jsonld ) { return array(); } /** * Check if Schema AI has any schema for a given post. * * Queries the schema_ai_schemas custom table for rows matching the * given post ID with a non-invalid validation status. * * @param int $post_id The WordPress post ID. * @return bool True if at least one schema exists. */ public function has_schema_ai_schema( $post_id ) { global $wpdb; $table = esc_sql( $wpdb->prefix . 'schema_ai_schemas' ); // Verify the table exists before querying. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for custom table; no WP API alternative exists. $table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ); if ( ! $table_exists ) { return false; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for custom table; no WP API alternative exists. $count = $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is sanitized via esc_sql() and not user input. "SELECT COUNT(*) FROM {$table} WHERE post_id = %d AND validation_status != %s", $post_id, 'invalid' ) ); return absint( $count ) > 0; } /** * Get all Schema AI schema types for a given post. * * @param int $post_id The WordPress post ID. * @return array Array of schema_type strings. */ private function get_schema_ai_types( $post_id ) { global $wpdb; $table = esc_sql( $wpdb->prefix . 'schema_ai_schemas' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for custom table; no WP API alternative exists. $table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ); if ( ! $table_exists ) { return array(); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for custom table; no WP API alternative exists. $results = $wpdb->get_col( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is sanitized via esc_sql() and not user input. "SELECT schema_type FROM {$table} WHERE post_id = %d AND validation_status != %s", $post_id, 'invalid' ) ); return ! empty( $results ) ? $results : array(); } /** * Find keys in the Rank Math data array that conflict with Schema AI types. * * Iterates over the Rank Math schema pieces, extracts each piece's @type, * and checks whether it conflicts with any Schema AI type (including * alias types). Protected types are only flagged as conflicting if * Schema AI explicitly provides the same type. * * @param array $data Rank Math schema data (keyed array of schema pieces). * @param array $schema_ai_types Array of Schema AI schema type strings. * @return array Array of keys to remove from the Rank Math data. */ public function get_conflicting_keys( $data, $schema_ai_types ) { $conflicting = array(); foreach ( $data as $key => $piece ) { if ( ! is_array( $piece ) ) { continue; } $rank_math_type = isset( $piece['@type'] ) ? $piece['@type'] : ''; // Handle array @type values. if ( is_array( $rank_math_type ) ) { foreach ( $rank_math_type as $single_type ) { if ( $this->type_conflicts( $single_type, $schema_ai_types ) ) { $conflicting[] = $key; break; } } continue; } // Single string @type. if ( $this->type_conflicts( $rank_math_type, $schema_ai_types ) ) { $conflicting[] = $key; } } return $conflicting; } /** * Check if a single Rank Math type conflicts with Schema AI types. * * Protected types are only removed if Schema AI explicitly has the * same type. Other types are checked against the alias map. * * @param string $rank_math_type The @type from a Rank Math schema piece. * @param array $schema_ai_types Array of Schema AI schema types. * @return bool True if there is a conflict. */ private function type_conflicts( $rank_math_type, $schema_ai_types ) { if ( empty( $rank_math_type ) ) { return false; } // Protected types: only remove on exact match. if ( in_array( $rank_math_type, $this->protected_types, true ) ) { return in_array( $rank_math_type, $schema_ai_types, true ); } // Check alias map. foreach ( $schema_ai_types as $ai_type ) { if ( isset( $this->type_aliases[ $ai_type ] ) ) { if ( in_array( $rank_math_type, $this->type_aliases[ $ai_type ], true ) ) { return true; } } // Direct match. if ( $rank_math_type === $ai_type ) { return true; } } return false; } /** * Check if Rank Math is active. * * @return bool True if the RankMath class is available. */ public function is_rank_math_active() { return class_exists( 'RankMath' ); } }