| 1 |
<?php |
| 2 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 |
namespace Yoast\WP\SEO\Schema_Aggregator\Application\Schema_Map; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Main; |
| 6 |
use Yoast\WP\SEO\Schema_Aggregator\Domain\Indexable_Count_Collection; |
| 7 |
use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Config; |
| 8 |
use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Map\Schema_Map_Repository_Interface; |
| 9 |
|
| 10 |
/** |
| 11 |
* Builds the schema map. |
| 12 |
*/ |
| 13 |
class Schema_Map_Builder { |
| 14 |
|
| 15 |
/** |
| 16 |
* The schema map repository. |
| 17 |
* |
| 18 |
* @var Schema_Map_Repository_Interface |
| 19 |
*/ |
| 20 |
private $schema_map_repository; |
| 21 |
|
| 22 |
/** |
| 23 |
* The configuration. |
| 24 |
* |
| 25 |
* @var Config |
| 26 |
*/ |
| 27 |
private $config; |
| 28 |
|
| 29 |
/** |
| 30 |
* Schema_Map_Builder constructor. |
| 31 |
* |
| 32 |
* @param Config $config The configuration. |
| 33 |
*/ |
| 34 |
public function __construct( Config $config ) { |
| 35 |
$this->config = $config; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Sets the schema map repository. |
| 40 |
* |
| 41 |
* @param Schema_Map_Repository_Interface $schema_map_repository The schema map repository. |
| 42 |
* |
| 43 |
* @return self |
| 44 |
*/ |
| 45 |
public function with_repository( Schema_Map_Repository_Interface $schema_map_repository ): self { |
| 46 |
$this->schema_map_repository = $schema_map_repository; |
| 47 |
return $this; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Builds the schema map based on indexable counts and threshold. |
| 52 |
* |
| 53 |
* @param Indexable_Count_Collection $indexable_counts The indexable counts per post type. |
| 54 |
* |
| 55 |
* @return array<int,array<string>> The schema map. |
| 56 |
*/ |
| 57 |
public function build( Indexable_Count_Collection $indexable_counts ): array { |
| 58 |
$schema_map = []; |
| 59 |
foreach ( $indexable_counts->get_indexable_counts() as $indexable_count ) { |
| 60 |
|
| 61 |
$post_type = $indexable_count->get_post_type(); |
| 62 |
$count = $indexable_count->get_count(); |
| 63 |
|
| 64 |
$threshold = $this->config->get_per_page( $post_type ); |
| 65 |
|
| 66 |
$total_pages = (int) \ceil( $count / $threshold ); |
| 67 |
|
| 68 |
for ( $page = 1; $page <= $total_pages; $page++ ) { |
| 69 |
if ( $page === 1 && $total_pages === 1 ) { |
| 70 |
$url = $this->get_rest_route( $post_type ); |
| 71 |
} |
| 72 |
elseif ( $page === 1 ) { |
| 73 |
$url = $this->get_rest_route( $post_type ); |
| 74 |
} |
| 75 |
else { |
| 76 |
$url = $this->get_rest_route( $post_type, $page ); |
| 77 |
} |
| 78 |
|
| 79 |
$lastmod = $this->schema_map_repository->get_lastmod_for_post_type( $post_type, $page, $threshold ); |
| 80 |
|
| 81 |
$page_count = ( $page === $total_pages ) ? ( $count - ( ( $page - 1 ) * $threshold ) ) : $threshold; |
| 82 |
|
| 83 |
$schema_map[] = [ |
| 84 |
'post_type' => $post_type, |
| 85 |
'url' => $url, |
| 86 |
'lastmod' => $lastmod, |
| 87 |
'count' => $page_count, |
| 88 |
]; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $schema_map; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Gets the REST route for the given post type and page. |
| 97 |
* |
| 98 |
* @param string $post_type The post type. |
| 99 |
* @param int $page The page number (default is 1). |
| 100 |
* |
| 101 |
* @return string The REST route URL. |
| 102 |
*/ |
| 103 |
public function get_rest_route( $post_type, $page = 1 ): string { |
| 104 |
if ( $page === 1 ) { |
| 105 |
return \rest_url( Main::API_V1_NAMESPACE . '/schema-aggregator/get-schema/' . $post_type ); |
| 106 |
} |
| 107 |
else { |
| 108 |
return \rest_url( Main::API_V1_NAMESPACE . '/schema-aggregator/get-schema/' . $post_type . '/' . $page ); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|