> */ public static function all(): array { $stored = get_option( self::OPTION ); return is_array( $stored ) ? array_values( $stored ) : array(); } /** * Persist the full template list. * * @param array $templates Raw templates. * @return array> */ public static function save( array $templates ): array { $clean = array(); foreach ( array_slice( $templates, 0, 50 ) as $template ) { if ( ! is_array( $template ) ) { continue; } $type = sanitize_text_field( (string) ( $template['type'] ?? '' ) ); if ( '' === $type || ! SchemaTypes::exists( $type ) || 'auto' === $type ) { continue; } $allowed = array_column( SchemaTypes::fields( $type ), 'key' ); $data = array(); foreach ( (array) ( $template['data'] ?? array() ) as $key => $value ) { if ( in_array( $key, $allowed, true ) ) { // Template values carry %variable% tokens, which the stock // WordPress sanitizers would mangle — see Support\Sanitize. $data[ sanitize_key( $key ) ] = \AiRankSuite\Support\Sanitize::template_multiline( (string) $value ); } } $conditions = (array) ( $template['conditions'] ?? array() ); $clean[] = array( 'id' => sanitize_key( (string) ( $template['id'] ?? uniqid( 'tpl_' ) ) ), 'name' => sanitize_text_field( (string) ( $template['name'] ?? $type ) ), 'type' => $type, 'enabled' => ! empty( $template['enabled'] ), 'data' => $data, 'conditions' => array( 'post_types' => array_values( array_intersect( array_map( 'sanitize_key', (array) ( $conditions['post_types'] ?? array() ) ), get_post_types() ) ), 'terms' => array_values( array_filter( array_map( 'absint', (array) ( $conditions['terms'] ?? array() ) ) ) ), 'post_ids' => array_values( array_filter( array_map( 'absint', (array) ( $conditions['post_ids'] ?? array() ) ) ) ), 'exclude' => array_values( array_filter( array_map( 'absint', (array) ( $conditions['exclude'] ?? array() ) ) ) ), ), ); } update_option( self::OPTION, $clean, false ); return $clean; } /** * Templates whose conditions match a post. * * @return array> */ public static function matching( WP_Post $post ): array { $out = array(); foreach ( self::all() as $template ) { if ( empty( $template['enabled'] ) ) { continue; } if ( self::matches( $template, $post ) ) { $out[] = $template; } } return $out; } /** * @param array $template Template. */ private static function matches( array $template, WP_Post $post ): bool { $conditions = (array) $template['conditions']; if ( in_array( $post->ID, (array) $conditions['exclude'], true ) ) { return false; } // An explicit post ID always wins. if ( ! empty( $conditions['post_ids'] ) && in_array( $post->ID, (array) $conditions['post_ids'], true ) ) { return true; } $has_scope = ! empty( $conditions['post_types'] ) || ! empty( $conditions['terms'] ); if ( ! $has_scope ) { // No scope at all: only explicit IDs may match, otherwise a template // would silently apply to the entire site. return empty( $conditions['post_ids'] ); } if ( ! empty( $conditions['post_types'] ) && ! in_array( $post->post_type, (array) $conditions['post_types'], true ) ) { return false; } if ( ! empty( $conditions['terms'] ) ) { $matched = false; foreach ( get_object_taxonomies( $post ) as $taxonomy ) { $terms = get_the_terms( $post, $taxonomy ); if ( ! is_array( $terms ) ) { continue; } foreach ( $terms as $term ) { if ( in_array( (int) $term->term_id, (array) $conditions['terms'], true ) ) { $matched = true; break 2; } } } if ( ! $matched ) { return false; } } return true; } /** * Extract JSON-LD from a public URL so users can copy a competitor's setup * as a starting point. * * @return array>|\WP_Error */ public static function import_from_url( string $url ) { $url = esc_url_raw( $url ); if ( ! wp_http_validate_url( $url ) ) { return new \WP_Error( 'airanksuite_schema_bad_url', __( 'That URL cannot be fetched.', 'ai-rank-suite' ), array( 'status' => 400 ) ); } $response = wp_safe_remote_get( $url, array( 'timeout' => 20, 'user-agent' => 'AI Rank Suite Schema Importer', ) ); if ( is_wp_error( $response ) ) { return $response; } $code = (int) wp_remote_retrieve_response_code( $response ); if ( $code >= 400 ) { return new \WP_Error( 'airanksuite_schema_fetch_failed', sprintf( /* translators: %d: HTTP status */ __( 'The page responded with HTTP %d.', 'ai-rank-suite' ), $code ), array( 'status' => 502 ) ); } $body = (string) wp_remote_retrieve_body( $response ); if ( ! preg_match_all( '#]*type=["\']application/ld\+json["\'][^>]*>(.*?)#is', $body, $matches ) ) { return new \WP_Error( 'airanksuite_schema_none_found', __( 'No structured data found on that page.', 'ai-rank-suite' ), array( 'status' => 404 ) ); } $found = array(); foreach ( $matches[1] as $json ) { $decoded = json_decode( trim( $json ), true ); if ( ! is_array( $decoded ) ) { continue; } $nodes = isset( $decoded['@graph'] ) && is_array( $decoded['@graph'] ) ? $decoded['@graph'] : array( $decoded ); foreach ( $nodes as $node ) { if ( ! is_array( $node ) || empty( $node['@type'] ) ) { continue; } $type = is_array( $node['@type'] ) ? (string) reset( $node['@type'] ) : (string) $node['@type']; $found[] = array( 'type' => $type, 'supported' => SchemaTypes::exists( $type ), 'node' => $node, ); } } if ( empty( $found ) ) { return new \WP_Error( 'airanksuite_schema_none_found', __( 'No structured data found on that page.', 'ai-rank-suite' ), array( 'status' => 404 ) ); } return array_slice( $found, 0, 20 ); } }