Changeset 43648 for branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php
- Timestamp:
- 09/18/2018 03:54:20 AM (2 years ago)
- Location:
- branches/4.9
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.9
- Property svn:mergeinfo changed
/trunk merged: 43584-43586,43647
- Property svn:mergeinfo changed
-
branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php
r43445 r43648 198 198 } 199 199 200 $revisions = wp_get_post_revisions( $request['parent'] ); 200 // Ensure a search string is set in case the orderby is set to 'relevance'. 201 if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) { 202 return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.' ), array( 'status' => 400 ) ); 203 } 204 205 // Ensure an include parameter is set in case the orderby is set to 'include'. 206 if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) { 207 return new WP_Error( 'rest_orderby_include_missing_include', __( 'You need to define an include parameter to order by include.' ), array( 'status' => 400 ) ); 208 } 209 210 if ( wp_revisions_enabled( $parent ) ) { 211 $registered = $this->get_collection_params(); 212 $args = array( 213 'post_parent' => $parent->ID, 214 'post_type' => 'revision', 215 'post_status' => 'inherit', 216 'posts_per_page' => -1, 217 'orderby' => 'date ID', 218 'order' => 'DESC', 219 'suppress_filters' => true, 220 ); 221 222 $parameter_mappings = array( 223 'exclude' => 'post__not_in', 224 'include' => 'post__in', 225 'offset' => 'offset', 226 'order' => 'order', 227 'orderby' => 'orderby', 228 'page' => 'paged', 229 'per_page' => 'posts_per_page', 230 'search' => 's', 231 ); 232 233 foreach ( $parameter_mappings as $api_param => $wp_param ) { 234 if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { 235 $args[ $wp_param ] = $request[ $api_param ]; 236 } 237 } 238 239 // For backward-compatibility, 'date' needs to resolve to 'date ID'. 240 if ( isset( $args['orderby'] ) && 'date' === $args['orderby'] ) { 241 $args['orderby'] = 'date ID'; 242 } 243 244 /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ 245 $args = apply_filters( 'rest_revision_query', $args, $request ); 246 $query_args = $this->prepare_items_query( $args, $request ); 247 248 $revisions_query = new WP_Query(); 249 $revisions = $revisions_query->query( $query_args ); 250 $offset = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0; 251 $page = (int) $query_args['paged']; 252 $total_revisions = $revisions_query->found_posts; 253 254 if ( $total_revisions < 1 ) { 255 // Out-of-bounds, run the query again without LIMIT for total count. 256 unset( $query_args['paged'], $query_args['offset'] ); 257 258 $count_query = new WP_Query(); 259 $count_query->query( $query_args ); 260 261 $total_revisions = $count_query->found_posts; 262 } 263 264 if ( $revisions_query->query_vars['posts_per_page'] > 0 ) { 265 $max_pages = ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] ); 266 } else { 267 $max_pages = $total_revisions > 0 ? 1 : 0; 268 } 269 270 if ( $total_revisions > 0 ) { 271 if ( $offset >= $total_revisions ) { 272 return new WP_Error( 'rest_revision_invalid_offset_number', __( 'The offset number requested is larger than or equal to the number of available revisions.' ), array( 'status' => 400 ) ); 273 } elseif ( ! $offset && $page > $max_pages ) { 274 return new WP_Error( 'rest_revision_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), array( 'status' => 400 ) ); 275 } 276 } 277 } else { 278 $revisions = array(); 279 $total_revisions = 0; 280 $max_pages = 0; 281 $page = (int) $request['page']; 282 } 201 283 202 284 $response = array(); … … 205 287 $response[] = $this->prepare_response_for_collection( $data ); 206 288 } 207 return rest_ensure_response( $response ); 289 290 $response = rest_ensure_response( $response ); 291 292 $response->header( 'X-WP-Total', (int) $total_revisions ); 293 $response->header( 'X-WP-TotalPages', (int) $max_pages ); 294 295 $request_params = $request->get_query_params(); 296 $base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) ) ); 297 298 if ( $page > 1 ) { 299 $prev_page = $page - 1; 300 301 if ( $prev_page > $max_pages ) { 302 $prev_page = $max_pages; 303 } 304 305 $prev_link = add_query_arg( 'page', $prev_page, $base ); 306 $response->link_header( 'prev', $prev_link ); 307 } 308 if ( $max_pages > $page ) { 309 $next_page = $page + 1; 310 $next_link = add_query_arg( 'page', $next_page, $base ); 311 312 $response->link_header( 'next', $next_link ); 313 } 314 315 return $response; 208 316 } 209 317 … … 316 424 $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) ); 317 425 return $response; 426 } 427 428 /** 429 * Determines the allowed query_vars for a get_items() response and prepares 430 * them for WP_Query. 431 * 432 * @since 5.0.0 433 * 434 * @param array $prepared_args Optional. Prepared WP_Query arguments. Default empty array. 435 * @param WP_REST_Request $request Optional. Full details about the request. 436 * @return array Items query arguments. 437 */ 438 protected function prepare_items_query( $prepared_args = array(), $request = null ) { 439 $query_args = array(); 440 441 foreach ( $prepared_args as $key => $value ) { 442 /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */ 443 $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 444 } 445 446 // Map to proper WP_Query orderby param. 447 if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) { 448 $orderby_mappings = array( 449 'id' => 'ID', 450 'include' => 'post__in', 451 'slug' => 'post_name', 452 'include_slugs' => 'post_name__in', 453 ); 454 455 if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) { 456 $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ]; 457 } 458 } 459 460 return $query_args; 318 461 } 319 462 … … 538 681 */ 539 682 public function get_collection_params() { 540 return array( 541 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 683 $query_params = parent::get_collection_params(); 684 685 $query_params['context']['default'] = 'view'; 686 687 unset( $query_params['per_page']['default'] ); 688 689 $query_params['exclude'] = array( 690 'description' => __( 'Ensure result set excludes specific IDs.' ), 691 'type' => 'array', 692 'items' => array( 693 'type' => 'integer', 694 ), 695 'default' => array(), 542 696 ); 697 698 $query_params['include'] = array( 699 'description' => __( 'Limit result set to specific IDs.' ), 700 'type' => 'array', 701 'items' => array( 702 'type' => 'integer', 703 ), 704 'default' => array(), 705 ); 706 707 $query_params['offset'] = array( 708 'description' => __( 'Offset the result set by a specific number of items.' ), 709 'type' => 'integer', 710 ); 711 712 $query_params['order'] = array( 713 'description' => __( 'Order sort attribute ascending or descending.' ), 714 'type' => 'string', 715 'default' => 'desc', 716 'enum' => array( 'asc', 'desc' ), 717 ); 718 719 $query_params['orderby'] = array( 720 'description' => __( 'Sort collection by object attribute.' ), 721 'type' => 'string', 722 'default' => 'date', 723 'enum' => array( 724 'date', 725 'id', 726 'include', 727 'relevance', 728 'slug', 729 'include_slugs', 730 'title', 731 ), 732 ); 733 734 return $query_params; 543 735 } 544 736
Note: See TracChangeset
for help on using the changeset viewer.