PluginProbe
Gutenberg / 4.1.0
Gutenberg v4.1.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / class-wp-rest-post-search-handler.php

class-wp-rest-post-search-handler.php in Gutenberg 4.1.0, at lib/class-wp-rest-post-search-handler.php

191 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API: WP_REST_Post_Search_Handler class
4 *
5 * @package gutenberg
6 * @since 3.3.0
7 */
8
9 /**
10 * Core class representing a search handler for posts in the REST API.
11 *
12 * @since 3.3.0
13 */
14 class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler {
15
16 /**
17 * Constructor.
18 *
19 * @since 3.3.0
20 */
21 public function __construct() {
22 $this->type = 'post';
23
24 // Support all public post types except attachments.
25 $this->subtypes = array_diff(
26 array_values(
27 get_post_types(
28 array(
29 'public' => true,
30 'show_in_rest' => true,
31 ),
32 'names'
33 )
34 ),
35 array( 'attachment' )
36 );
37 }
38
39 /**
40 * Searches the object type content for a given search request.
41 *
42 * @since 3.3.0
43 *
44 * @param WP_REST_Request $request Full REST request.
45 * @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
46 * an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
47 * total count for the matching search results.
48 */
49 public function search_items( WP_REST_Request $request ) {
50
51 // Get the post types to search for the current request.
52 $post_types = $request[ WP_REST_Search_Controller::PROP_SUBTYPE ];
53 if ( in_array( WP_REST_Search_Controller::TYPE_ANY, $post_types, true ) ) {
54 $post_types = $this->subtypes;
55 }
56
57 $query_args = array(
58 'post_type' => $post_types,
59 'post_status' => 'publish',
60 'paged' => (int) $request['page'],
61 'posts_per_page' => (int) $request['per_page'],
62 'ignore_sticky_posts' => true,
63 'fields' => 'ids',
64 );
65
66 if ( ! empty( $request['search'] ) ) {
67 $query_args['s'] = $request['search'];
68 }
69
70 $query = new WP_Query();
71 $found_ids = $query->query( $query_args );
72 $total = $query->found_posts;
73
74 return array(
75 self::RESULT_IDS => $found_ids,
76 self::RESULT_TOTAL => $total,
77 );
78 }
79
80 /**
81 * Prepares the search result for a given ID.
82 *
83 * @since 3.3.0
84 *
85 * @param int $id Item ID.
86 * @param array $fields Fields to include for the item.
87 * @return array Associative array containing all fields for the item.
88 */
89 public function prepare_item( $id, array $fields ) {
90 $post = get_post( $id );
91
92 $data = array();
93
94 if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
95 $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $post->ID;
96 }
97
98 if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
99 if ( post_type_supports( $post->post_type, 'title' ) ) {
100 add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
101 $data[ WP_REST_Search_Controller::PROP_TITLE ] = get_the_title( $post->ID );
102 remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
103 } else {
104 $data[ WP_REST_Search_Controller::PROP_TITLE ] = '';
105 }
106 }
107
108 if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
109 $data[ WP_REST_Search_Controller::PROP_URL ] = get_permalink( $post->ID );
110 }
111
112 if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
113 $data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
114 }
115
116 if ( in_array( WP_REST_Search_Controller::PROP_SUBTYPE, $fields, true ) ) {
117 $data[ WP_REST_Search_Controller::PROP_SUBTYPE ] = $post->post_type;
118 }
119
120 return $data;
121 }
122
123 /**
124 * Prepares links for the search result of a given ID.
125 *
126 * @since 3.3.0
127 *
128 * @param int $id Item ID.
129 * @return array Links for the given item.
130 */
131 public function prepare_item_links( $id ) {
132 $post = get_post( $id );
133
134 $links = array();
135
136 $item_route = $this->detect_rest_item_route( $post );
137 if ( ! empty( $item_route ) ) {
138 $links['self'] = array(
139 'href' => rest_url( $item_route ),
140 'embeddable' => true,
141 );
142 }
143
144 $links['about'] = array(
145 'href' => rest_url( 'wp/v2/types/' . $post->post_type ),
146 );
147
148 return $links;
149 }
150
151 /**
152 * Overwrites the default protected title format.
153 *
154 * By default, WordPress will show password protected posts with a title of
155 * "Protected: %s". As the REST API communicates the protected status of a post
156 * in a machine readable format, we remove the "Protected: " prefix.
157 *
158 * @since 3.3.0
159 *
160 * @return string Protected title format.
161 */
162 public function protected_title_format() {
163 return '%s';
164 }
165
166 /**
167 * Attempts to detect the route to access a single item.
168 *
169 * @since 3.3.0
170 *
171 * @param WP_Post $post Post object.
172 * @return string REST route relative to the REST base URI, or empty string if unknown.
173 */
174 protected function detect_rest_item_route( $post ) {
175 $post_type = get_post_type_object( $post->post_type );
176 if ( ! $post_type ) {
177 return '';
178 }
179
180 // It's currently impossible to detect the REST URL from a custom controller.
181 if ( ! empty( $post_type->rest_controller_class ) && 'WP_REST_Posts_Controller' !== $post_type->rest_controller_class ) {
182 return '';
183 }
184
185 $namespace = 'wp/v2';
186 $rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
187
188 return sprintf( '%s/%s/%d', $namespace, $rest_base, $post->ID );
189 }
190 }
191