wp/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
   260 
   260 
   261 		if ( isset( $registered['page'] ) && empty( $request['offset'] ) ) {
   261 		if ( isset( $registered['page'] ) && empty( $request['offset'] ) ) {
   262 			$prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['page'] ) - 1 );
   262 			$prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['page'] ) - 1 );
   263 		}
   263 		}
   264 
   264 
       
   265 		$is_head_request = $request->is_method( 'HEAD' );
       
   266 		if ( $is_head_request ) {
       
   267 			// Force the 'fields' argument. For HEAD requests, only post IDs are required to calculate pagination.
       
   268 			$prepared_args['fields'] = 'ids';
       
   269 			// Disable priming comment meta for HEAD requests to improve performance.
       
   270 			$prepared_args['update_comment_meta_cache'] = false;
       
   271 		}
       
   272 
   265 		/**
   273 		/**
   266 		 * Filters WP_Comment_Query arguments when querying comments via the REST API.
   274 		 * Filters WP_Comment_Query arguments when querying comments via the REST API.
   267 		 *
   275 		 *
   268 		 * @since 4.7.0
   276 		 * @since 4.7.0
   269 		 *
   277 		 *
   275 		$prepared_args = apply_filters( 'rest_comment_query', $prepared_args, $request );
   283 		$prepared_args = apply_filters( 'rest_comment_query', $prepared_args, $request );
   276 
   284 
   277 		$query        = new WP_Comment_Query();
   285 		$query        = new WP_Comment_Query();
   278 		$query_result = $query->query( $prepared_args );
   286 		$query_result = $query->query( $prepared_args );
   279 
   287 
   280 		$comments = array();
   288 		if ( ! $is_head_request ) {
   281 
   289 			$comments = array();
   282 		foreach ( $query_result as $comment ) {
   290 
   283 			if ( ! $this->check_read_permission( $comment, $request ) ) {
   291 			foreach ( $query_result as $comment ) {
   284 				continue;
   292 				if ( ! $this->check_read_permission( $comment, $request ) ) {
   285 			}
   293 					continue;
   286 
   294 				}
   287 			$data       = $this->prepare_item_for_response( $comment, $request );
   295 
   288 			$comments[] = $this->prepare_response_for_collection( $data );
   296 				$data       = $this->prepare_item_for_response( $comment, $request );
       
   297 				$comments[] = $this->prepare_response_for_collection( $data );
       
   298 			}
   289 		}
   299 		}
   290 
   300 
   291 		$total_comments = (int) $query->found_comments;
   301 		$total_comments = (int) $query->found_comments;
   292 		$max_pages      = (int) $query->max_num_pages;
   302 		$max_pages      = (int) $query->max_num_pages;
   293 
   303 
   301 
   311 
   302 			$total_comments = $query->query( $prepared_args );
   312 			$total_comments = $query->query( $prepared_args );
   303 			$max_pages      = (int) ceil( $total_comments / $request['per_page'] );
   313 			$max_pages      = (int) ceil( $total_comments / $request['per_page'] );
   304 		}
   314 		}
   305 
   315 
   306 		$response = rest_ensure_response( $comments );
   316 		$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $comments );
   307 		$response->header( 'X-WP-Total', $total_comments );
   317 		$response->header( 'X-WP-Total', $total_comments );
   308 		$response->header( 'X-WP-TotalPages', $max_pages );
   318 		$response->header( 'X-WP-TotalPages', $max_pages );
   309 
   319 
   310 		$base = add_query_arg( urlencode_deep( $request->get_query_params() ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
   320 		$base = add_query_arg( urlencode_deep( $request->get_query_params() ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
   311 
   321 
  1038 	 * @return WP_REST_Response Response object.
  1048 	 * @return WP_REST_Response Response object.
  1039 	 */
  1049 	 */
  1040 	public function prepare_item_for_response( $item, $request ) {
  1050 	public function prepare_item_for_response( $item, $request ) {
  1041 		// Restores the more descriptive, specific name for use within this method.
  1051 		// Restores the more descriptive, specific name for use within this method.
  1042 		$comment = $item;
  1052 		$comment = $item;
       
  1053 
       
  1054 		// Don't prepare the response body for HEAD requests.
       
  1055 		if ( $request->is_method( 'HEAD' ) ) {
       
  1056 			/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php */
       
  1057 			return apply_filters( 'rest_prepare_comment', new WP_REST_Response( array() ), $comment, $request );
       
  1058 		}
  1043 
  1059 
  1044 		$fields = $this->get_fields_for_response( $request );
  1060 		$fields = $this->get_fields_for_response( $request );
  1045 		$data   = array();
  1061 		$data   = array();
  1046 
  1062 
  1047 		if ( in_array( 'id', $fields, true ) ) {
  1063 		if ( in_array( 'id', $fields, true ) ) {
  1245 	/**
  1261 	/**
  1246 	 * Checks comment_approved to set comment status for single comment output.
  1262 	 * Checks comment_approved to set comment status for single comment output.
  1247 	 *
  1263 	 *
  1248 	 * @since 4.7.0
  1264 	 * @since 4.7.0
  1249 	 *
  1265 	 *
  1250 	 * @param string|int $comment_approved comment status.
  1266 	 * @param string $comment_approved Comment status.
  1251 	 * @return string Comment status.
  1267 	 * @return string Comment status.
  1252 	 */
  1268 	 */
  1253 	protected function prepare_status_response( $comment_approved ) {
  1269 	protected function prepare_status_response( $comment_approved ) {
  1254 
  1270 
  1255 		switch ( $comment_approved ) {
  1271 		switch ( $comment_approved ) {