wordpress-seo
/
src
/
task-list
/
infrastructure
/
indexables
/
recent-content-indexable-collector.php
recent-content-indexable-collector.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.2, at src/task-list/infrastructure/indexables/recent-content-indexable-collector.php
| 1 | <?php |
| 2 | // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong |
| 3 | // phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded |
| 4 | namespace Yoast\WP\SEO\Task_List\Infrastructure\Indexables; |
| 5 | |
| 6 | use Yoast\WP\SEO\Dashboard\Application\Score_Groups\Readability_Score_Groups\Readability_Score_Groups_Repository; |
| 7 | use Yoast\WP\SEO\Dashboard\Application\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Repository; |
| 8 | use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 9 | use Yoast\WP\SEO\Task_List\Domain\Data\Content_Item_Score_Data; |
| 10 | use Yoast\WP\SEO\Task_List\Domain\Data\Meta_Description_Content_Item_Data; |
| 11 | |
| 12 | /** |
| 13 | * Collector that retrieves recent content items with their scores. |
| 14 | */ |
| 15 | class Recent_Content_Indexable_Collector { |
| 16 | |
| 17 | /** |
| 18 | * The indexable repository. |
| 19 | * |
| 20 | * @var Indexable_Repository |
| 21 | */ |
| 22 | private $indexable_repository; |
| 23 | |
| 24 | /** |
| 25 | * The SEO score groups repository. |
| 26 | * |
| 27 | * @var SEO_Score_Groups_Repository |
| 28 | */ |
| 29 | private $seo_score_groups_repository; |
| 30 | |
| 31 | /** |
| 32 | * The readability score groups repository. |
| 33 | * |
| 34 | * @var Readability_Score_Groups_Repository |
| 35 | */ |
| 36 | private $readability_score_groups_repository; |
| 37 | |
| 38 | /** |
| 39 | * The constructor. |
| 40 | * |
| 41 | * @param Indexable_Repository $indexable_repository The indexable repository. |
| 42 | * @param SEO_Score_Groups_Repository $seo_score_groups_repository The SEO score groups repository. |
| 43 | * @param Readability_Score_Groups_Repository $readability_score_groups_repository The readability score groups repository. |
| 44 | */ |
| 45 | public function __construct( |
| 46 | Indexable_Repository $indexable_repository, |
| 47 | SEO_Score_Groups_Repository $seo_score_groups_repository, |
| 48 | Readability_Score_Groups_Repository $readability_score_groups_repository |
| 49 | ) { |
| 50 | $this->indexable_repository = $indexable_repository; |
| 51 | $this->seo_score_groups_repository = $seo_score_groups_repository; |
| 52 | $this->readability_score_groups_repository = $readability_score_groups_repository; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Gets recent content items with SEO scores for the given post type. |
| 57 | * |
| 58 | * @param string $post_type The post type to query. |
| 59 | * @param string $date_limit The date limit (content modified after this date). |
| 60 | * @param int|null $limit Optional. Maximum number of items to retrieve. |
| 61 | * |
| 62 | * @return Content_Item_Score_Data[] Array of content item score data value objects. |
| 63 | */ |
| 64 | public function get_recent_content_with_seo_scores( string $post_type, string $date_limit, ?int $limit = null ): array { |
| 65 | $raw_results = $this->indexable_repository->get_recent_posts_with_keywords_for_post_type( |
| 66 | $post_type, |
| 67 | $limit, |
| 68 | $date_limit, |
| 69 | ); |
| 70 | |
| 71 | return $this->map_to_seo_score_data( $raw_results, $post_type ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Gets recent content items with readability scores for the given post type. |
| 76 | * |
| 77 | * @param string $post_type The post type to query. |
| 78 | * @param string $date_limit The date limit (content modified after this date). |
| 79 | * @param int|null $limit Optional. Maximum number of items to retrieve. |
| 80 | * |
| 81 | * @return Content_Item_Score_Data[] Array of content item score data value objects. |
| 82 | */ |
| 83 | public function get_recent_content_with_readability_scores( string $post_type, string $date_limit, ?int $limit = null ): array { |
| 84 | $raw_results = $this->indexable_repository->get_recent_posts_with_readability_scores_for_post_type( |
| 85 | $post_type, |
| 86 | $limit, |
| 87 | $date_limit, |
| 88 | ); |
| 89 | |
| 90 | return $this->map_to_readability_score_data( $raw_results, $post_type ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Gets recent content items for the meta descriptions task for the given post type. |
| 95 | * |
| 96 | * @param string $post_type The post type to query. |
| 97 | * @param string $date_limit The date limit (content modified after this date). |
| 98 | * @param int|null $limit Optional. Maximum number of items to retrieve. |
| 99 | * |
| 100 | * @return Meta_Description_Content_Item_Data[] Array of content item data value objects. |
| 101 | */ |
| 102 | public function get_recent_content_for_meta_descriptions( string $post_type, string $date_limit, ?int $limit = null ): array { |
| 103 | $raw_results = $this->indexable_repository->get_recent_posts_for_post_type( |
| 104 | $post_type, |
| 105 | $limit, |
| 106 | $date_limit, |
| 107 | ); |
| 108 | |
| 109 | if ( ! \is_array( $raw_results ) ) { |
| 110 | return []; |
| 111 | } |
| 112 | |
| 113 | $content_items = []; |
| 114 | |
| 115 | foreach ( $raw_results as $result ) { |
| 116 | $content_items[] = new Meta_Description_Content_Item_Data( |
| 117 | (int) $result['object_id'], |
| 118 | $result['breadcrumb_title'], |
| 119 | (string) $result['description'] !== '', |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | return $content_items; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Maps raw database results to Content_Item_Score_Data value objects for SEO scores. |
| 128 | * |
| 129 | * @param array<array<string, string>> $raw_results The raw results from the repository. |
| 130 | * @param string $post_type The post type. |
| 131 | * |
| 132 | * @return Content_Item_Score_Data[] Array of content item score data value objects. |
| 133 | */ |
| 134 | private function map_to_seo_score_data( array $raw_results, string $post_type ): array { |
| 135 | $content_items = []; |
| 136 | |
| 137 | foreach ( $raw_results as $result ) { |
| 138 | $seo_score_group = $this->seo_score_groups_repository->get_score_group( (int) $result['primary_focus_keyword_score'] ); |
| 139 | |
| 140 | $content_items[] = new Content_Item_Score_Data( |
| 141 | (int) $result['object_id'], |
| 142 | $result['breadcrumb_title'], |
| 143 | $seo_score_group->get_name(), |
| 144 | $post_type, |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | return $content_items; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Maps raw database results to Content_Item_Score_Data value objects for readability scores. |
| 153 | * |
| 154 | * @param array<array<string, string>> $raw_results The raw results from the repository. |
| 155 | * @param string $post_type The post type. |
| 156 | * |
| 157 | * @return Content_Item_Score_Data[] Array of content item score data value objects. |
| 158 | */ |
| 159 | private function map_to_readability_score_data( array $raw_results, string $post_type ): array { |
| 160 | $content_items = []; |
| 161 | |
| 162 | foreach ( $raw_results as $result ) { |
| 163 | // @TODO: Instead of this inline quick fix, let's properly handle the case where readability_score is 0 in the repository method, and return 'bad' directly from there (SEO scores having a different logic might make this a bit harder). |
| 164 | // Also read as: The refactoring of https://github.com/Yoast/wordpress-seo/pull/22947 was not quite right. |
| 165 | if ( (int) $result['readability_score'] === 0 ) { |
| 166 | $score_name = 'bad'; |
| 167 | } |
| 168 | else { |
| 169 | $readability_score_group = $this->readability_score_groups_repository->get_score_group( (int) $result['readability_score'] ); |
| 170 | $score_name = $readability_score_group->get_name(); |
| 171 | } |
| 172 | |
| 173 | $content_items[] = new Content_Item_Score_Data( |
| 174 | (int) $result['object_id'], |
| 175 | $result['breadcrumb_title'], |
| 176 | $score_name, |
| 177 | $post_type, |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | return $content_items; |
| 182 | } |
| 183 | } |
| 184 |