| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong |
| 4 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded |
| 5 |
namespace Yoast\WP\SEO\Editors\Framework\Seo\Posts; |
| 6 |
|
| 7 |
use WPSEO_Meta; |
| 8 |
use Yoast\WP\SEO\Editors\Domain\Seo\Keyphrase; |
| 9 |
use Yoast\WP\SEO\Editors\Domain\Seo\Seo_Plugin_Data_Interface; |
| 10 |
use Yoast\WP\SEO\Editors\Framework\Seo\Keyphrase_Interface; |
| 11 |
use Yoast\WP\SEO\Helpers\Meta_Helper; |
| 12 |
|
| 13 |
/** |
| 14 |
* Describes if the keyphrase SEO data. |
| 15 |
*/ |
| 16 |
class Keyphrase_Data_Provider extends Abstract_Post_Seo_Data_Provider implements Keyphrase_Interface { |
| 17 |
|
| 18 |
/** |
| 19 |
* The meta helper. |
| 20 |
* |
| 21 |
* @var Meta_Helper |
| 22 |
*/ |
| 23 |
private $meta_helper; |
| 24 |
|
| 25 |
/** |
| 26 |
* The constructor. |
| 27 |
* |
| 28 |
* @param Meta_Helper $meta_helper The meta helper. |
| 29 |
*/ |
| 30 |
public function __construct( Meta_Helper $meta_helper ) { |
| 31 |
$this->meta_helper = $meta_helper; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Counts the number of given Keyphrase used for other posts other than the given post_id. |
| 36 |
* |
| 37 |
* @return array<string> The keyphrase and the associated posts that use it. |
| 38 |
*/ |
| 39 |
public function get_focus_keyphrase_usage(): array { |
| 40 |
$keyphrase = $this->meta_helper->get_value( 'focuskw', $this->post->ID ); |
| 41 |
$usage = [ $keyphrase => $this->get_keyphrase_usage_for_current_post( $keyphrase ) ]; |
| 42 |
|
| 43 |
/** |
| 44 |
* Allows enhancing the array of posts' that share their focus Keyphrase with the post's related Keyphrase. |
| 45 |
* |
| 46 |
* @param array<string> $usage The array of posts' ids that share their focus Keyphrase with the post. |
| 47 |
* @param int $post_id The id of the post we're finding the usage of related Keyphrase for. |
| 48 |
*/ |
| 49 |
return \apply_filters( 'wpseo_posts_for_related_keywords', $usage, $this->post->ID ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Retrieves the post types for the given post IDs. |
| 54 |
* |
| 55 |
* @param array<string|array<string>> $post_ids_per_keyphrase An associative array with keyphrase as keys and an array of post ids where those keyphrases are used. |
| 56 |
* |
| 57 |
* @return array<string|array<string>> The post types for the given post IDs. |
| 58 |
*/ |
| 59 |
public function get_post_types_for_all_ids( array $post_ids_per_keyphrase ): array { |
| 60 |
$post_type_per_keyphrase_result = []; |
| 61 |
foreach ( $post_ids_per_keyphrase as $keyphrase => $post_ids ) { |
| 62 |
$post_type_per_keyphrase_result[ $keyphrase ] = WPSEO_Meta::post_types_for_ids( $post_ids ); |
| 63 |
} |
| 64 |
|
| 65 |
return $post_type_per_keyphrase_result; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Gets the keyphrase usage for the current post and the specified keyphrase. |
| 70 |
* |
| 71 |
* @param string $keyphrase The keyphrase to check the usage of. |
| 72 |
* |
| 73 |
* @return array<string> The post IDs which use the passed keyphrase. |
| 74 |
*/ |
| 75 |
private function get_keyphrase_usage_for_current_post( string $keyphrase ): array { |
| 76 |
return WPSEO_Meta::keyword_usage( $keyphrase, $this->post->ID ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Method to return the keyphrase domain object with SEO data. |
| 81 |
* |
| 82 |
* @return Seo_Plugin_Data_Interface The specific seo data. |
| 83 |
*/ |
| 84 |
public function get_data(): Seo_Plugin_Data_Interface { |
| 85 |
$keyphrase_usage = $this->get_focus_keyphrase_usage(); |
| 86 |
|
| 87 |
return new Keyphrase( $keyphrase_usage, $this->get_post_types_for_all_ids( $keyphrase_usage ) ); |
| 88 |
} |
| 89 |
} |
| 90 |
|