PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / schema-aggregator / application / meta / response-meta-provider.php

response-meta-provider.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/schema-aggregator/application/meta/response-meta-provider.php

105 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3 namespace Yoast\WP\SEO\Schema_Aggregator\Application\Meta;
4
5 use Yoast\WP\SEO\Schema_Aggregator\Application\Schema_Map\Schema_Map_Builder;
6 use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Map\Schema_Map_Indexable_Repository;
7
8 /**
9 * Provides metadata for API responses.
10 *
11 * @codeCoverageIgnore -- Currently not used.
12 */
13 class Response_Meta_Provider {
14
15 /**
16 * The schema map indexable repository.
17 *
18 * @var Schema_Map_Indexable_Repository
19 */
20 private $schema_map_repository;
21
22 /**
23 * The schema map builder.
24 *
25 * @var Schema_Map_Builder
26 */
27 private $schema_map_builder;
28
29 /**
30 * Response_Meta_Provider constructor.
31 *
32 * @param Schema_Map_Indexable_Repository $schema_map_repository The schema map indexable repository.
33 * @param Schema_Map_Builder $schema_map_builder The schema map builder.
34 */
35 public function __construct( Schema_Map_Indexable_Repository $schema_map_repository, Schema_Map_Builder $schema_map_builder ) {
36 $this->schema_map_repository = $schema_map_repository;
37 $this->schema_map_builder = $schema_map_builder;
38 }
39
40 /**
41 * Build metadata structure for API response
42 *
43 * @param string $post_type The post type being queried.
44 * @param int $page The page number (1-based).
45 * @param int $page_size The number of items per page.
46 *
47 * @return array<string,array<string>> Metadata structure.
48 */
49 public function get_metadata( string $post_type, int $page, int $page_size ): array {
50 $metadata = [
51 'generator' => [
52 'name' => 'Yoast NLWeb Integration',
53 'version' => \WPSEO_VERSION,
54 'vendor' => 'Yoast',
55 'url' => 'https://yoast.com',
56 ],
57 'dependencies' => [
58 'wordpress' => \function_exists( 'get_bloginfo' ) ? \get_bloginfo( 'version' ) : 'unknown',
59 'yoast_seo' => \WPSEO_VERSION,
60 ],
61 'generated_at' => \gmdate( 'Y-m-d\TH:i:s\Z' ),
62 ];
63
64 if ( \defined( 'WPSEO_WOO_VERSION' ) ) {
65 $metadata['dependencies']['yoast_seo_woocommerce'] = \WPSEO_WOO_VERSION;
66 }
67
68 return $this->maybe_add_pagination_metadata( $metadata, $post_type, $page, $page_size );
69 }
70
71 /**
72 * Add pagination metadata to the response if applicable.
73 *
74 * @param array<string,array<string>> $metadata The metadata array to add pagination info to.
75 * @param string $post_type The post type being queried.
76 * @param int $page The current page number (1-based).
77 * @param int $page_size The number of items per page.
78 *
79 * @return array<string,array<string>> The updated metadata array.
80 */
81 private function maybe_add_pagination_metadata(
82 array $metadata,
83 string $post_type,
84 int $page,
85 int $page_size
86 ): array {
87
88 $indexable_count = $this->schema_map_repository->get_indexable_count_for_post_type( $post_type );
89 $total_items = $indexable_count->get_count();
90
91 if ( $total_items === 0 ) {
92 return $metadata;
93 }
94
95 $total_pages = (int) \ceil( $total_items / $page_size );
96
97 if ( $page < $total_pages ) {
98 $next_page_url = $this->schema_map_builder->get_rest_route( $post_type, ( $page + 1 ) );
99 $metadata['next'] = $next_page_url;
100
101 }
102 return $metadata;
103 }
104 }
105