| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong |
| 4 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded |
| 5 |
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Indexables; |
| 6 |
|
| 7 |
use Yoast\WP\SEO\Dashboard\Application\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Repository; |
| 8 |
use Yoast\WP\SEO\Dashboard\Domain\Data_Provider\Data_Container; |
| 9 |
use Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\No_SEO_Score_Group; |
| 10 |
use Yoast\WP\SEO\Dashboard\Domain\Search_Rankings\Top_Page_Data; |
| 11 |
use Yoast\WP\SEO\Models\Indexable; |
| 12 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 13 |
|
| 14 |
/** |
| 15 |
* The indexable collector that gets SEO scores from the indexables of top pages. |
| 16 |
*/ |
| 17 |
class Top_Page_Indexable_Collector { |
| 18 |
|
| 19 |
/** |
| 20 |
* The indexable repository. |
| 21 |
* |
| 22 |
* @var Indexable_Repository |
| 23 |
*/ |
| 24 |
private $indexable_repository; |
| 25 |
|
| 26 |
/** |
| 27 |
* The SEO score groups repository. |
| 28 |
* |
| 29 |
* @var SEO_Score_Groups_Repository |
| 30 |
*/ |
| 31 |
private $seo_score_groups_repository; |
| 32 |
|
| 33 |
/** |
| 34 |
* The constructor. |
| 35 |
* |
| 36 |
* @param Indexable_Repository $indexable_repository The indexable repository. |
| 37 |
* @param SEO_Score_Groups_Repository $seo_score_groups_repository The SEO score groups repository. |
| 38 |
*/ |
| 39 |
public function __construct( |
| 40 |
Indexable_Repository $indexable_repository, |
| 41 |
SEO_Score_Groups_Repository $seo_score_groups_repository |
| 42 |
) { |
| 43 |
$this->indexable_repository = $indexable_repository; |
| 44 |
$this->seo_score_groups_repository = $seo_score_groups_repository; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Gets full data for top pages. |
| 49 |
* |
| 50 |
* @param Data_Container $top_pages The top pages. |
| 51 |
* |
| 52 |
* @return Data_Container Data about SEO scores of top pages. |
| 53 |
*/ |
| 54 |
public function get_data( Data_Container $top_pages ): Data_Container { |
| 55 |
$top_page_data_container = new Data_Container(); |
| 56 |
|
| 57 |
foreach ( $top_pages->get_data() as $top_page ) { |
| 58 |
$url = $top_page->get_subject(); |
| 59 |
|
| 60 |
$indexable = $this->get_top_page_indexable( $url ); |
| 61 |
|
| 62 |
if ( $indexable instanceof Indexable ) { |
| 63 |
$seo_score_group = $this->seo_score_groups_repository->get_score_group( $indexable->primary_focus_keyword_score ); |
| 64 |
$edit_link = $this->get_top_page_edit_link( $indexable ); |
| 65 |
|
| 66 |
$top_page_data_container->add_data( new Top_Page_Data( $top_page, $seo_score_group, $edit_link ) ); |
| 67 |
|
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
$seo_score_group = new No_SEO_Score_Group(); |
| 72 |
$top_page_data_container->add_data( new Top_Page_Data( $top_page, $seo_score_group ) ); |
| 73 |
} |
| 74 |
|
| 75 |
return $top_page_data_container; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Gets indexable for a top page URL. |
| 80 |
* |
| 81 |
* @param string $url The URL of the top page. |
| 82 |
* |
| 83 |
* @return bool|Indexable The indexable of the top page URL or false if there is none. |
| 84 |
*/ |
| 85 |
protected function get_top_page_indexable( string $url ) { |
| 86 |
// First check if the URL is the static homepage. |
| 87 |
if ( \trailingslashit( $url ) === \trailingslashit( \get_home_url() ) && \get_option( 'show_on_front' ) === 'page' ) { |
| 88 |
return $this->indexable_repository->find_by_id_and_type( \get_option( 'page_on_front' ), 'post', false ); |
| 89 |
} |
| 90 |
|
| 91 |
return $this->indexable_repository->find_by_permalink( $url ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Gets edit links from a top page's indexable. |
| 96 |
* |
| 97 |
* @param Indexable $indexable The top page's indexable. |
| 98 |
* |
| 99 |
* @return string|null The edit link for the top page. |
| 100 |
*/ |
| 101 |
protected function get_top_page_edit_link( Indexable $indexable ): ?string { |
| 102 |
if ( $indexable->object_type === 'post' && \current_user_can( 'edit_post', $indexable->object_id ) ) { |
| 103 |
return \get_edit_post_link( $indexable->object_id, '&' ); |
| 104 |
} |
| 105 |
|
| 106 |
if ( $indexable->object_type === 'term' && \current_user_can( 'edit_term', $indexable->object_id ) ) { |
| 107 |
return \get_edit_term_link( $indexable->object_id ); |
| 108 |
} |
| 109 |
|
| 110 |
return null; |
| 111 |
} |
| 112 |
} |
| 113 |
|