PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.9
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 / llms-txt / user-interface / available-posts-route.php

available-posts-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.9, at src/llms-txt/user-interface/available-posts-route.php

150 lines 3.8 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\Llms_Txt\User_Interface;
5
6 use Exception;
7 use WP_Post_Type;
8 use WP_REST_Request;
9 use WP_REST_Response;
10 use Yoast\WP\SEO\Conditionals\No_Conditionals;
11 use Yoast\WP\SEO\Helpers\Capability_Helper;
12 use Yoast\WP\SEO\Llms_Txt\Application\Available_Posts\Available_Posts_Repository;
13 use Yoast\WP\SEO\Llms_Txt\Domain\Available_Posts\Data_Provider\Parameters;
14 use Yoast\WP\SEO\Llms_Txt\Domain\Available_Posts\Invalid_Post_Type_Exception;
15 use Yoast\WP\SEO\Main;
16 use Yoast\WP\SEO\Routes\Route_Interface;
17
18 /**
19 * Available posts route.
20 */
21 class Available_Posts_Route implements Route_Interface {
22
23 use No_Conditionals;
24
25 /**
26 * The namespace of the route.
27 *
28 * @var string
29 */
30 public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
31
32 /**
33 * The prefix of the route.
34 *
35 * @var string
36 */
37 public const ROUTE_NAME = '/available_posts';
38
39 /**
40 * Holds the available posts repository.
41 *
42 * @var Available_Posts_Repository
43 */
44 private $available_posts_repository;
45
46 /**
47 * Holds the capability helper instance.
48 *
49 * @var Capability_Helper
50 */
51 private $capability_helper;
52
53 /**
54 * The constructor.
55 *
56 * @param Available_Posts_Repository $available_posts_repository The data provider for the available posts.
57 * @param Capability_Helper $capability_helper The capability helper.
58 */
59 public function __construct(
60 Available_Posts_Repository $available_posts_repository,
61 Capability_Helper $capability_helper
62 ) {
63 $this->available_posts_repository = $available_posts_repository;
64 $this->capability_helper = $capability_helper;
65 }
66
67 /**
68 * Registers routes for scores.
69 *
70 * @return void
71 */
72 public function register_routes() {
73 \register_rest_route(
74 self::ROUTE_NAMESPACE,
75 self::ROUTE_NAME,
76 [
77 [
78 'methods' => 'GET',
79 'callback' => [ $this, 'get_available_posts' ],
80 'permission_callback' => [ $this, 'permission_manage_options' ],
81 'args' => [
82 'search' => [
83 'type' => 'string',
84 'sanitize_callback' => 'sanitize_text_field',
85 'default' => '',
86 ],
87 'postType' => [
88 'type' => 'string',
89 'sanitize_callback' => 'sanitize_text_field',
90 'default' => 'page',
91 ],
92 ],
93 ],
94 ],
95 );
96 }
97
98 /**
99 * Gets the available posts.
100 *
101 * @param WP_REST_Request $request The request object.
102 *
103 * @return WP_REST_Response The success or failure response.
104 */
105 public function get_available_posts( WP_REST_Request $request ): WP_REST_Response {
106 try {
107 $request_parameters = new Parameters( $request->get_param( 'postType' ), $request->get_param( 'search' ) );
108 $this->validate_request_parameters( $request_parameters );
109
110 $available_posts_container = $this->available_posts_repository->get_posts( $request_parameters );
111 } catch ( Exception $exception ) {
112 return new WP_REST_Response(
113 [
114 'error' => $exception->getMessage(),
115 ],
116 $exception->getCode(),
117 );
118 }
119
120 return new WP_REST_Response(
121 $available_posts_container->to_array(),
122 200,
123 );
124 }
125
126 /**
127 * Validates the request's parameters.
128 *
129 * @param Parameters $request_parameters The request parameters.
130 *
131 * @return void.
132 *
133 * @throws Invalid_Post_Type_Exception When the given post type is invalid.
134 */
135 public function validate_request_parameters( Parameters $request_parameters ): void {
136 if ( ! \is_a( \get_post_type_object( $request_parameters->get_post_type() ), WP_Post_Type::class ) ) {
137 throw new Invalid_Post_Type_Exception();
138 }
139 }
140
141 /**
142 * Permission callback.
143 *
144 * @return bool True when user has the 'wpseo_manage_options' capability.
145 */
146 public function permission_manage_options() {
147 return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
148 }
149 }
150