PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.4
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 / routes / meta-search-route.php

meta-search-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.4, at src/routes/meta-search-route.php

99 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Routes;
4
5 use WP_REST_Request;
6 use WP_REST_Response;
7 use Yoast\WP\SEO\Conditionals\No_Conditionals;
8 use Yoast\WP\SEO\Main;
9
10 /**
11 * Meta_Search_Route class
12 */
13 class Meta_Search_Route implements Route_Interface {
14
15 use No_Conditionals;
16
17 /**
18 * Represents meta search route.
19 *
20 * @var string
21 */
22 public const META_SEARCH_ROUTE = '/meta/search';
23
24 /**
25 * Registers routes with WordPress.
26 *
27 * @return void
28 */
29 public function register_routes() {
30 $route = [
31 [
32 'methods' => 'GET',
33 'callback' => [ $this, 'search_meta' ],
34 'permission_callback' => [ $this, 'permission_check' ],
35 ],
36 ];
37
38 \register_rest_route( Main::API_V1_NAMESPACE, self::META_SEARCH_ROUTE, $route );
39 }
40
41 /**
42 * Performs the permission check.
43 *
44 * @param WP_REST_Request $request The request.
45 *
46 * @return bool
47 */
48 public function permission_check( $request ) {
49 if ( ! isset( $request['post_id'] ) ) {
50 return false;
51 }
52
53 $post_type = \get_post_type( $request['post_id'] );
54 $post_type_object = \get_post_type_object( $post_type );
55
56 return \current_user_can( $post_type_object->cap->edit_post, $request['post_id'] );
57 }
58
59 /**
60 * Searches meta fields of a given post.
61 *
62 * @param WP_REST_Request $request The REST request.
63 *
64 * @return WP_REST_Response
65 */
66 public function search_meta( $request ) {
67 $post_id = $request['post_id'];
68 $query = $request['query'];
69 $meta = \get_post_custom( $post_id );
70 $matches = [];
71
72 foreach ( $meta as $key => $values ) {
73 if ( \substr( $key, 0, \strlen( $query ) ) !== $query ) {
74 continue;
75 }
76
77 if ( empty( $query ) && \substr( $key, 0, 1 ) === '_' ) {
78 continue;
79 }
80
81 // Skip custom field values that are serialized.
82 if ( \is_serialized( $values[0] ) ) {
83 continue;
84 }
85
86 $matches[] = [
87 'key' => $key,
88 'value' => $values[0],
89 ];
90
91 if ( \count( $matches ) >= 25 ) {
92 break;
93 }
94 }
95
96 return \rest_ensure_response( [ 'meta' => $matches ] );
97 }
98 }
99