PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / abilities / application / post-seo-data-collector.php

post-seo-data-collector.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/abilities/application/post-seo-data-collector.php

83 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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