wp/wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    87 			);
    87 			);
    88 		}
    88 		}
    89 
    89 
    90 		$collections_page = array_slice( $collections_all, ( $page - 1 ) * $per_page, $per_page );
    90 		$collections_page = array_slice( $collections_all, ( $page - 1 ) * $per_page, $per_page );
    91 
    91 
       
    92 		$is_head_request = $request->is_method( 'HEAD' );
       
    93 
    92 		$items = array();
    94 		$items = array();
    93 		foreach ( $collections_page as $collection ) {
    95 		foreach ( $collections_page as $collection ) {
    94 			$item = $this->prepare_item_for_response( $collection, $request );
    96 			$item = $this->prepare_item_for_response( $collection, $request );
    95 
    97 
    96 			// If there's an error loading a collection, skip it and continue loading valid collections.
    98 			// If there's an error loading a collection, skip it and continue loading valid collections.
    97 			if ( is_wp_error( $item ) ) {
    99 			if ( is_wp_error( $item ) ) {
    98 				continue;
   100 				continue;
    99 			}
   101 			}
       
   102 
       
   103 			/*
       
   104 			 * Skip preparing the response body for HEAD requests.
       
   105 			 * Cannot exit earlier due to backward compatibility reasons,
       
   106 			 * as validation occurs in the prepare_item_for_response method.
       
   107 			 */
       
   108 			if ( $is_head_request ) {
       
   109 				continue;
       
   110 			}
       
   111 
   100 			$item    = $this->prepare_response_for_collection( $item );
   112 			$item    = $this->prepare_response_for_collection( $item );
   101 			$items[] = $item;
   113 			$items[] = $item;
   102 		}
   114 		}
   103 
   115 
   104 		$response = rest_ensure_response( $items );
   116 		$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $items );
   105 
   117 
   106 		$response->header( 'X-WP-Total', (int) $total_items );
   118 		$response->header( 'X-WP-Total', (int) $total_items );
   107 		$response->header( 'X-WP-TotalPages', $max_pages );
   119 		$response->header( 'X-WP-TotalPages', $max_pages );
   108 
   120 
   109 		$request_params = $request->get_query_params();
   121 		$request_params = $request->get_query_params();
   173 			if ( is_wp_error( $collection_data ) ) {
   185 			if ( is_wp_error( $collection_data ) ) {
   174 				$collection_data->add_data( array( 'status' => 500 ) );
   186 				$collection_data->add_data( array( 'status' => 500 ) );
   175 				return $collection_data;
   187 				return $collection_data;
   176 			}
   188 			}
   177 
   189 
       
   190 			/**
       
   191 			 * Don't prepare the response body for HEAD requests.
       
   192 			 * Can't exit at the beginning of the method due to the potential need to return a WP_Error object.
       
   193 			 */
       
   194 			if ( $request->is_method( 'HEAD' ) ) {
       
   195 				/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */
       
   196 				return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request );
       
   197 			}
       
   198 
   178 			foreach ( $data_fields as $field ) {
   199 			foreach ( $data_fields as $field ) {
   179 				if ( rest_is_field_included( $field, $fields ) ) {
   200 				if ( rest_is_field_included( $field, $fields ) ) {
   180 					$data[ $field ] = $collection_data[ $field ];
   201 					$data[ $field ] = $collection_data[ $field ];
   181 				}
   202 				}
   182 			}
   203 			}
       
   204 		}
       
   205 
       
   206 		/**
       
   207 		 * Don't prepare the response body for HEAD requests.
       
   208 		 * Can't exit at the beginning of the method due to the potential need to return a WP_Error object.
       
   209 		 */
       
   210 		if ( $request->is_method( 'HEAD' ) ) {
       
   211 			/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */
       
   212 			return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request );
   183 		}
   213 		}
   184 
   214 
   185 		$response = rest_ensure_response( $data );
   215 		$response = rest_ensure_response( $data );
   186 
   216 
   187 		if ( rest_is_field_included( '_links', $fields ) ) {
   217 		if ( rest_is_field_included( '_links', $fields ) ) {