wordpress-seo
/
src
/
schema-aggregator
/
infrastructure
/
indexable-repository
/
wordpress-query-repository.php
wordpress-query-repository.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/schema-aggregator/infrastructure/indexable-repository/wordpress-query-repository.php
| 1 | <?php |
| 2 | // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 | namespace Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Indexable_Repository; |
| 4 | |
| 5 | use WP_Query; |
| 6 | use Yoast\WP\SEO\Builders\Indexable_Builder; |
| 7 | use Yoast\WP\SEO\Models\Indexable; |
| 8 | use Yoast\WP\SEO\Repositories\Indexable_Repository as Pure_Indexable_Repository; |
| 9 | |
| 10 | /** |
| 11 | * WordPress-based implementation of the Indexable Repository Interface. |
| 12 | */ |
| 13 | class WordPress_Query_Repository implements Indexable_Repository_Interface { |
| 14 | |
| 15 | /** |
| 16 | * The indexable builder instance. |
| 17 | * |
| 18 | * @var Indexable_Builder |
| 19 | */ |
| 20 | private $indexable_builder; |
| 21 | |
| 22 | /** |
| 23 | * The indexables repository. |
| 24 | * |
| 25 | * @var Pure_Indexable_Repository |
| 26 | */ |
| 27 | private $indexable_repository; |
| 28 | |
| 29 | /** |
| 30 | * Constructor. |
| 31 | * |
| 32 | * @param Indexable_Builder $indexable_builder The indexable builder. |
| 33 | * @param Pure_Indexable_Repository $indexable_repository The indexable repository. |
| 34 | */ |
| 35 | public function __construct( Indexable_Builder $indexable_builder, Pure_Indexable_Repository $indexable_repository ) { |
| 36 | $this->indexable_builder = $indexable_builder; |
| 37 | $this->indexable_repository = $indexable_repository; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Builds on-the-fly public indexables in a paginated manner. |
| 42 | * |
| 43 | * @codeCoverageIgnore -- This is covered by dedicated integration tests. |
| 44 | * |
| 45 | * @param int $page The page number. |
| 46 | * @param int $page_size The number of items per page. |
| 47 | * @param string $post_type The post type to filter by. |
| 48 | * |
| 49 | * @return array<Indexable> The array of public indexables. |
| 50 | */ |
| 51 | public function get( int $page, int $page_size, string $post_type ): array { |
| 52 | $query = new WP_Query( |
| 53 | [ |
| 54 | 'post_type' => $post_type, |
| 55 | 'post_status' => 'publish', |
| 56 | 'posts_per_page' => $page_size, |
| 57 | 'paged' => $page, |
| 58 | 'fields' => 'ids', |
| 59 | 'no_found_rows' => false, |
| 60 | ], |
| 61 | ); |
| 62 | |
| 63 | if ( ! $query instanceof WP_Query ) { |
| 64 | return []; |
| 65 | } |
| 66 | |
| 67 | $post_ids = isset( $query->posts ) && \is_array( $query->posts ) ? $query->posts : []; |
| 68 | |
| 69 | $indexables = $this->indexable_repository->find_by_multiple_ids_and_type( $post_ids, 'post' ); |
| 70 | $public_indexables = []; |
| 71 | foreach ( $indexables as $indexable ) { |
| 72 | if ( $indexable !== null && ( $indexable->is_public === true || $indexable->is_public === null ) ) { |
| 73 | if ( empty( $indexable->id ) ) { |
| 74 | |
| 75 | $indexable->id = $indexable->object_id; |
| 76 | } |
| 77 | $public_indexables[] = $indexable; |
| 78 | } |
| 79 | } |
| 80 | return $public_indexables; |
| 81 | } |
| 82 | } |
| 83 |