143 ) |
143 ) |
144 ); |
144 ); |
145 } |
145 } |
146 |
146 |
147 /** |
147 /** |
|
148 * Checks if the terms for a post can be read. |
|
149 * |
|
150 * @since 6.0.3 |
|
151 * |
|
152 * @param WP_Post $post Post object. |
|
153 * @param WP_REST_Request $request Full details about the request. |
|
154 * @return bool Whether the terms for the post can be read. |
|
155 */ |
|
156 public function check_read_terms_permission_for_post( $post, $request ) { |
|
157 // If the requested post isn't associated with this taxonomy, deny access. |
|
158 if ( ! is_object_in_taxonomy( $post->post_type, $this->taxonomy ) ) { |
|
159 return false; |
|
160 } |
|
161 |
|
162 // Grant access if the post is publicly viewable. |
|
163 if ( is_post_publicly_viewable( $post ) ) { |
|
164 return true; |
|
165 } |
|
166 |
|
167 // Otherwise grant access if the post is readable by the logged in user. |
|
168 if ( current_user_can( 'read_post', $post->ID ) ) { |
|
169 return true; |
|
170 } |
|
171 |
|
172 // Otherwise, deny access. |
|
173 return false; |
|
174 } |
|
175 |
|
176 /** |
148 * Checks if a request has access to read terms in the specified taxonomy. |
177 * Checks if a request has access to read terms in the specified taxonomy. |
149 * |
178 * |
150 * @since 4.7.0 |
179 * @since 4.7.0 |
151 * |
180 * |
152 * @param WP_REST_Request $request Full details about the request. |
181 * @param WP_REST_Request $request Full details about the request. |
153 * @return true|WP_Error True if the request has read access, otherwise false or WP_Error object. |
182 * @return bool|WP_Error True if the request has read access, otherwise false or WP_Error object. |
154 */ |
183 */ |
155 public function get_items_permissions_check( $request ) { |
184 public function get_items_permissions_check( $request ) { |
156 $tax_obj = get_taxonomy( $this->taxonomy ); |
185 $tax_obj = get_taxonomy( $this->taxonomy ); |
157 |
186 |
158 if ( ! $tax_obj || ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) { |
187 if ( ! $tax_obj || ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) { |
163 return new WP_Error( |
192 return new WP_Error( |
164 'rest_forbidden_context', |
193 'rest_forbidden_context', |
165 __( 'Sorry, you are not allowed to edit terms in this taxonomy.' ), |
194 __( 'Sorry, you are not allowed to edit terms in this taxonomy.' ), |
166 array( 'status' => rest_authorization_required_code() ) |
195 array( 'status' => rest_authorization_required_code() ) |
167 ); |
196 ); |
|
197 } |
|
198 |
|
199 if ( ! empty( $request['post'] ) ) { |
|
200 $post = get_post( $request['post'] ); |
|
201 |
|
202 if ( ! $post ) { |
|
203 return new WP_Error( |
|
204 'rest_post_invalid_id', |
|
205 __( 'Invalid post ID.' ), |
|
206 array( |
|
207 'status' => 400, |
|
208 ) |
|
209 ); |
|
210 } |
|
211 |
|
212 if ( ! $this->check_read_terms_permission_for_post( $post, $request ) ) { |
|
213 return new WP_Error( |
|
214 'rest_forbidden_context', |
|
215 __( 'Sorry, you are not allowed to view terms for this post.' ), |
|
216 array( |
|
217 'status' => rest_authorization_required_code(), |
|
218 ) |
|
219 ); |
|
220 } |
168 } |
221 } |
169 |
222 |
170 return true; |
223 return true; |
171 } |
224 } |
172 |
225 |
293 |
346 |
294 $response = rest_ensure_response( $response ); |
347 $response = rest_ensure_response( $response ); |
295 |
348 |
296 // Store pagination values for headers. |
349 // Store pagination values for headers. |
297 $per_page = (int) $prepared_args['number']; |
350 $per_page = (int) $prepared_args['number']; |
298 $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); |
351 $page = (int) ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); |
299 |
352 |
300 $response->header( 'X-WP-Total', (int) $total_terms ); |
353 $response->header( 'X-WP-Total', (int) $total_terms ); |
301 |
354 |
302 $max_pages = ceil( $total_terms / $per_page ); |
355 $max_pages = (int) ceil( $total_terms / $per_page ); |
303 |
356 |
304 $response->header( 'X-WP-TotalPages', (int) $max_pages ); |
357 $response->header( 'X-WP-TotalPages', $max_pages ); |
305 |
358 |
306 $base = add_query_arg( urlencode_deep( $request->get_query_params() ), rest_url( $this->namespace . '/' . $this->rest_base ) ); |
359 $request_params = $request->get_query_params(); |
|
360 $collection_url = rest_url( rest_get_route_for_taxonomy_items( $this->taxonomy ) ); |
|
361 $base = add_query_arg( urlencode_deep( $request_params ), $collection_url ); |
|
362 |
307 if ( $page > 1 ) { |
363 if ( $page > 1 ) { |
308 $prev_page = $page - 1; |
364 $prev_page = $page - 1; |
309 |
365 |
310 if ( $prev_page > $max_pages ) { |
366 if ( $prev_page > $max_pages ) { |
311 $prev_page = $max_pages; |
367 $prev_page = $max_pages; |
359 * Checks if a request has access to read or edit the specified term. |
415 * Checks if a request has access to read or edit the specified term. |
360 * |
416 * |
361 * @since 4.7.0 |
417 * @since 4.7.0 |
362 * |
418 * |
363 * @param WP_REST_Request $request Full details about the request. |
419 * @param WP_REST_Request $request Full details about the request. |
364 * @return true|WP_Error True if the request has read access for the item, otherwise false or WP_Error object. |
420 * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object. |
365 */ |
421 */ |
366 public function get_item_permissions_check( $request ) { |
422 public function get_item_permissions_check( $request ) { |
367 $term = $this->get_term( $request['id'] ); |
423 $term = $this->get_term( $request['id'] ); |
368 |
424 |
369 if ( is_wp_error( $term ) ) { |
425 if ( is_wp_error( $term ) ) { |
749 * |
805 * |
750 * @param WP_REST_Request $request Request object. |
806 * @param WP_REST_Request $request Request object. |
751 * @return object Term object. |
807 * @return object Term object. |
752 */ |
808 */ |
753 public function prepare_item_for_database( $request ) { |
809 public function prepare_item_for_database( $request ) { |
754 $prepared_term = new stdClass; |
810 $prepared_term = new stdClass(); |
755 |
811 |
756 $schema = $this->get_item_schema(); |
812 $schema = $this->get_item_schema(); |
757 if ( isset( $request['name'] ) && ! empty( $schema['properties']['name'] ) ) { |
813 if ( isset( $request['name'] ) && ! empty( $schema['properties']['name'] ) ) { |
758 $prepared_term->name = $request['name']; |
814 $prepared_term->name = $request['name']; |
759 } |
815 } |
857 $data = $this->add_additional_fields_to_object( $data, $request ); |
913 $data = $this->add_additional_fields_to_object( $data, $request ); |
858 $data = $this->filter_response_by_context( $data, $context ); |
914 $data = $this->filter_response_by_context( $data, $context ); |
859 |
915 |
860 $response = rest_ensure_response( $data ); |
916 $response = rest_ensure_response( $data ); |
861 |
917 |
862 $response->add_links( $this->prepare_links( $item ) ); |
918 if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { |
|
919 $response->add_links( $this->prepare_links( $item ) ); |
|
920 } |
863 |
921 |
864 /** |
922 /** |
865 * Filters the term data for a REST API response. |
923 * Filters the term data for a REST API response. |
866 * |
924 * |
867 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. |
925 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. |
889 * |
947 * |
890 * @param WP_Term $term Term object. |
948 * @param WP_Term $term Term object. |
891 * @return array Links for the given term. |
949 * @return array Links for the given term. |
892 */ |
950 */ |
893 protected function prepare_links( $term ) { |
951 protected function prepare_links( $term ) { |
894 $base = $this->namespace . '/' . $this->rest_base; |
|
895 $links = array( |
952 $links = array( |
896 'self' => array( |
953 'self' => array( |
897 'href' => rest_url( trailingslashit( $base ) . $term->term_id ), |
954 'href' => rest_url( rest_get_route_for_term( $term ) ), |
898 ), |
955 ), |
899 'collection' => array( |
956 'collection' => array( |
900 'href' => rest_url( $base ), |
957 'href' => rest_url( rest_get_route_for_taxonomy_items( $this->taxonomy ) ), |
901 ), |
958 ), |
902 'about' => array( |
959 'about' => array( |
903 'href' => rest_url( sprintf( 'wp/v2/taxonomies/%s', $this->taxonomy ) ), |
960 'href' => rest_url( sprintf( 'wp/v2/taxonomies/%s', $this->taxonomy ) ), |
904 ), |
961 ), |
905 ); |
962 ); |
907 if ( $term->parent ) { |
964 if ( $term->parent ) { |
908 $parent_term = get_term( (int) $term->parent, $term->taxonomy ); |
965 $parent_term = get_term( (int) $term->parent, $term->taxonomy ); |
909 |
966 |
910 if ( $parent_term ) { |
967 if ( $parent_term ) { |
911 $links['up'] = array( |
968 $links['up'] = array( |
912 'href' => rest_url( trailingslashit( $base ) . $parent_term->term_id ), |
969 'href' => rest_url( rest_get_route_for_term( $parent_term ) ), |
913 'embeddable' => true, |
970 'embeddable' => true, |
914 ); |
971 ); |
915 } |
972 } |
916 } |
973 } |
917 |
974 |