equal
deleted
inserted
replaced
9 |
9 |
10 /** |
10 /** |
11 * Core class representing a search handler for posts in the REST API. |
11 * Core class representing a search handler for posts in the REST API. |
12 * |
12 * |
13 * @since 5.0.0 |
13 * @since 5.0.0 |
|
14 * |
|
15 * @see WP_REST_Search_Handler |
14 */ |
16 */ |
15 class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler { |
17 class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler { |
16 |
18 |
17 /** |
19 /** |
18 * Constructor. |
20 * Constructor. |
144 public function prepare_item_links( $id ) { |
146 public function prepare_item_links( $id ) { |
145 $post = get_post( $id ); |
147 $post = get_post( $id ); |
146 |
148 |
147 $links = array(); |
149 $links = array(); |
148 |
150 |
149 $item_route = $this->detect_rest_item_route( $post ); |
151 $item_route = rest_get_route_for_post( $post ); |
150 if ( ! empty( $item_route ) ) { |
152 if ( ! empty( $item_route ) ) { |
151 $links['self'] = array( |
153 $links['self'] = array( |
152 'href' => rest_url( $item_route ), |
154 'href' => rest_url( $item_route ), |
153 'embeddable' => true, |
155 'embeddable' => true, |
154 ); |
156 ); |
178 |
180 |
179 /** |
181 /** |
180 * Attempts to detect the route to access a single item. |
182 * Attempts to detect the route to access a single item. |
181 * |
183 * |
182 * @since 5.0.0 |
184 * @since 5.0.0 |
|
185 * @deprecated 5.5.0 Use rest_get_route_for_post() |
|
186 * @see rest_get_route_for_post() |
183 * |
187 * |
184 * @param WP_Post $post Post object. |
188 * @param WP_Post $post Post object. |
185 * @return string REST route relative to the REST base URI, or empty string if unknown. |
189 * @return string REST route relative to the REST base URI, or empty string if unknown. |
186 */ |
190 */ |
187 protected function detect_rest_item_route( $post ) { |
191 protected function detect_rest_item_route( $post ) { |
188 $post_type = get_post_type_object( $post->post_type ); |
192 _deprecated_function( __METHOD__, '5.5.0', 'rest_get_route_for_post()' ); |
189 if ( ! $post_type ) { |
|
190 return ''; |
|
191 } |
|
192 |
193 |
193 // It's currently impossible to detect the REST URL from a custom controller. |
194 return rest_get_route_for_post( $post ); |
194 if ( ! empty( $post_type->rest_controller_class ) && 'WP_REST_Posts_Controller' !== $post_type->rest_controller_class ) { |
|
195 return ''; |
|
196 } |
|
197 |
|
198 $namespace = 'wp/v2'; |
|
199 $rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name; |
|
200 |
|
201 return sprintf( '%s/%s/%d', $namespace, $rest_base, $post->ID ); |
|
202 } |
195 } |
203 |
196 |
204 } |
197 } |