| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Abilities\Application; |
| 5 |
|
| 6 |
use WP_Error; |
| 7 |
use Yoast\WP\SEO\Abilities\Infrastructure\Post_Access_Checker; |
| 8 |
use Yoast\WP\SEO\Abilities\Infrastructure\Post_Identifier_Resolver; |
| 9 |
use Yoast\WP\SEO\Abilities\Infrastructure\Post_SEO_Field_Map; |
| 10 |
|
| 11 |
/** |
| 12 |
* Application service that reads the SEO data of one or more posts. |
| 13 |
* |
| 14 |
* Doubles as a discovery tool: a title keyword search returns full SEO data for |
| 15 |
* every matching post. A request must carry an identifier; an empty request is an error. |
| 16 |
* Only posts the current user may edit are exposed. |
| 17 |
*/ |
| 18 |
class Post_SEO_Data_Collector { |
| 19 |
|
| 20 |
/** |
| 21 |
* The post identifier resolver. |
| 22 |
* |
| 23 |
* @var Post_Identifier_Resolver |
| 24 |
*/ |
| 25 |
private $resolver; |
| 26 |
|
| 27 |
/** |
| 28 |
* The post SEO field map. |
| 29 |
* |
| 30 |
* @var Post_SEO_Field_Map |
| 31 |
*/ |
| 32 |
private $field_map; |
| 33 |
|
| 34 |
/** |
| 35 |
* The post access checker. |
| 36 |
* |
| 37 |
* @var Post_Access_Checker |
| 38 |
*/ |
| 39 |
private $post_access_checker; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor. |
| 43 |
* |
| 44 |
* @param Post_Identifier_Resolver $resolver The post identifier resolver. |
| 45 |
* @param Post_SEO_Field_Map $field_map The post SEO field map. |
| 46 |
* @param Post_Access_Checker $post_access_checker The post access checker. |
| 47 |
*/ |
| 48 |
public function __construct( |
| 49 |
Post_Identifier_Resolver $resolver, |
| 50 |
Post_SEO_Field_Map $field_map, |
| 51 |
Post_Access_Checker $post_access_checker |
| 52 |
) { |
| 53 |
$this->resolver = $resolver; |
| 54 |
$this->field_map = $field_map; |
| 55 |
$this->post_access_checker = $post_access_checker; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Retrieves the SEO data for the posts matching the input. |
| 60 |
* |
| 61 |
* @param array<string, int|string|bool|null> $input The input containing an optional 'post_id', 'permalink', or 'title'. |
| 62 |
* |
| 63 |
* @return array<int, array<string, int|string|bool|null>>|WP_Error The SEO data for each matching post, or an error. |
| 64 |
*/ |
| 65 |
public function get_post_seo_data( array $input ) { |
| 66 |
$indexables = $this->resolver->resolve_many( $input ); |
| 67 |
|
| 68 |
if ( $indexables instanceof WP_Error ) { |
| 69 |
return $indexables; |
| 70 |
} |
| 71 |
|
| 72 |
$editable = $this->post_access_checker->filter_editable( $indexables ); |
| 73 |
|
| 74 |
// Every match was a post the user may not edit; an already empty match list |
| 75 |
// (a title-search page past the last result) stays a valid empty result. |
| 76 |
if ( $editable === [] && $indexables !== [] ) { |
| 77 |
return $this->post_access_checker->forbidden_error(); |
| 78 |
} |
| 79 |
|
| 80 |
return $this->field_map->indexables_to_arrays( $editable ); |
| 81 |
} |
| 82 |
} |
| 83 |
|