wordpress-seo
/
src
/
schema-aggregator
/
infrastructure
/
schema_map
/
schema-map-indexable-repository.php
schema-map-indexable-repository.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/schema-aggregator/infrastructure/schema_map/schema-map-indexable-repository.php
| 1 | <?php |
| 2 | // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 | namespace Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Map; |
| 4 | |
| 5 | use Exception; |
| 6 | use Yoast\WP\Lib\Model; |
| 7 | use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 8 | use Yoast\WP\SEO\Schema_Aggregator\Domain\Indexable_Count; |
| 9 | use Yoast\WP\SEO\Schema_Aggregator\Domain\Indexable_Count_Collection; |
| 10 | |
| 11 | /** |
| 12 | * Class Schema_Map_Indexable_Repository |
| 13 | * |
| 14 | * Maps indexable repository queries for schema map needs. |
| 15 | */ |
| 16 | class Schema_Map_Indexable_Repository implements Schema_Map_Repository_Interface { |
| 17 | |
| 18 | /** |
| 19 | * The indexable repository. |
| 20 | * |
| 21 | * @var Indexable_Repository |
| 22 | */ |
| 23 | private $indexable_repository; |
| 24 | |
| 25 | /** |
| 26 | * Schema_Map_Indexable_Repository constructor. |
| 27 | * |
| 28 | * @param Indexable_Repository $indexable_repository The indexable repository. |
| 29 | */ |
| 30 | public function __construct( Indexable_Repository $indexable_repository ) { |
| 31 | $this->indexable_repository = $indexable_repository; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Gets the indexable count per post type. |
| 36 | * |
| 37 | * @param array<string> $post_types The post types to get the indexable count for. |
| 38 | * |
| 39 | * @return Indexable_Count_Collection The indexable count per post type. |
| 40 | */ |
| 41 | public function get_indexable_count_per_post_type( array $post_types ): Indexable_Count_Collection { |
| 42 | $post_type_counts = new Indexable_Count_Collection(); |
| 43 | $indexable_raw_value = $this->indexable_repository->query() |
| 44 | ->select_expr( 'object_sub_type,count(object_sub_type) as count' ) |
| 45 | ->where_in( 'object_sub_type', $post_types ) |
| 46 | ->where_in( 'object_type', [ 'post', 'page' ] ) |
| 47 | ->where( 'post_status', 'publish' ) |
| 48 | ->where_raw( '( is_public IS NULL OR is_public = 1 )' ) |
| 49 | ->group_by( [ 'object_type', 'object_sub_type' ] ) |
| 50 | ->find_array(); |
| 51 | |
| 52 | if ( empty( $indexable_raw_value ) ) { |
| 53 | return $post_type_counts; |
| 54 | } |
| 55 | |
| 56 | foreach ( $indexable_raw_value as $indexable ) { |
| 57 | $post_type_counts->add_indexable_count( new Indexable_Count( $indexable['object_sub_type'], (int) $indexable['count'] ) ); |
| 58 | } |
| 59 | |
| 60 | return $post_type_counts; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets the indexable count for a single post type. |
| 65 | * |
| 66 | * @param string $post_type The post type to get the indexable count for. |
| 67 | * |
| 68 | * @return Indexable_Count The indexable count for the post type. |
| 69 | */ |
| 70 | public function get_indexable_count_for_post_type( string $post_type ): Indexable_Count { |
| 71 | $indexable_raw_value = $this->indexable_repository->query() |
| 72 | ->select_expr( 'object_sub_type,count(object_sub_type) as count' ) |
| 73 | ->where( 'object_sub_type', $post_type ) |
| 74 | ->where( 'post_status', 'publish' ) |
| 75 | ->where_in( 'object_type', [ 'post', 'page' ] ) |
| 76 | ->where_raw( '( is_public IS NULL OR is_public = 1 )' ) |
| 77 | ->find_one(); |
| 78 | |
| 79 | if ( empty( $indexable_raw_value ) ) { |
| 80 | return new Indexable_Count( $post_type, 0 ); |
| 81 | } |
| 82 | |
| 83 | return new Indexable_Count( $indexable_raw_value->object_sub_type, (int) $indexable_raw_value->count ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get lastmod timestamp for a post type and page range |
| 88 | * |
| 89 | * Returns the latest post_modified_gmt timestamp for posts in the given range. |
| 90 | * Used for schemamap index to enable selective updates. |
| 91 | * |
| 92 | * @param string $post_type Post type slug. |
| 93 | * @param int $page Page number (1-indexed). |
| 94 | * @param int $per_page Items per page. |
| 95 | * @return string ISO 8601 timestamp (e.g., "2025-10-21T14:23:17Z"). |
| 96 | */ |
| 97 | public function get_lastmod_for_post_type( string $post_type, int $page, int $per_page ): string { |
| 98 | global $wpdb; |
| 99 | $fallback = \gmdate( 'Y-m-d\TH:i:s\Z' ); |
| 100 | |
| 101 | try { |
| 102 | $offset = ( ( $page - 1 ) * $per_page ); |
| 103 | |
| 104 | $indexable_table = Model::get_table_name( 'Indexable' ); |
| 105 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Reason: There is no unescaped user input. |
| 106 | // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching |
| 107 | // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 108 | |
| 109 | $lastmod = $wpdb->get_var( |
| 110 | $wpdb->prepare( |
| 111 | " |
| 112 | SELECT MAX(object_last_modified) |
| 113 | FROM ( |
| 114 | |
| 115 | SELECT indexable_table.object_last_modified |
| 116 | FROM {$indexable_table} indexable_table |
| 117 | WHERE object_sub_type = %s |
| 118 | AND post_status = 'publish' |
| 119 | AND ( is_public IS NULL OR is_public = 1 ) |
| 120 | ORDER BY ID |
| 121 | LIMIT %d OFFSET %d |
| 122 | )AS posts_range |
| 123 | ", |
| 124 | $post_type, |
| 125 | $per_page, |
| 126 | $offset, |
| 127 | ), |
| 128 | ); |
| 129 | // Convert to ISO 8601 format or use current time if no posts. |
| 130 | if ( $lastmod && ! empty( $lastmod ) ) { |
| 131 | return \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( $lastmod ) ); |
| 132 | } |
| 133 | |
| 134 | return $fallback; |
| 135 | } catch ( Exception $e ) { |
| 136 | return $fallback; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 |