WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/02/2018 01:24:30 AM (3 years ago)
Author:
pento
Message:

REST API: Filter responses based on the _fields parameter, before data is processed.

Historically, the REST API would generate the entire response object, including running expensive filters, then it would apply the _fields parameter, discarding the fields that weren't specificed.

This change causes _fields to be applied earlier, so that only requested fields are processed.

Props danielbachhuber.
See #43874.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r42449 r43087  
    285285    public function prepare_item_for_response( $post, $request ) {
    286286        $response = parent::prepare_item_for_response( $post, $request );
     287        $fields   = $this->get_fields_for_response( $request );
    287288        $data     = $response->get_data();
    288289
    289         $data['description'] = array(
    290             'raw'      => $post->post_content,
     290        if ( in_array( 'description', $fields, true ) ) {
     291            $data['description'] = array(
     292                'raw'      => $post->post_content,
     293                /** This filter is documented in wp-includes/post-template.php */
     294                'rendered' => apply_filters( 'the_content', $post->post_content ),
     295            );
     296        }
     297
     298        if ( in_array( 'caption', $fields, true ) ) {
    291299            /** This filter is documented in wp-includes/post-template.php */
    292             'rendered' => apply_filters( 'the_content', $post->post_content ),
    293         );
    294 
    295         /** This filter is documented in wp-includes/post-template.php */
    296         $caption         = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
    297         $data['caption'] = array(
    298             'raw'      => $post->post_excerpt,
    299             'rendered' => $caption,
    300         );
    301 
    302         $data['alt_text']      = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
    303         $data['media_type']    = wp_attachment_is_image( $post->ID ) ? 'image' : 'file';
    304         $data['mime_type']     = $post->post_mime_type;
    305         $data['media_details'] = wp_get_attachment_metadata( $post->ID );
    306         $data['post']          = ! empty( $post->post_parent ) ? (int) $post->post_parent : null;
    307         $data['source_url']    = wp_get_attachment_url( $post->ID );
    308 
    309         // Ensure empty details is an empty object.
    310         if ( empty( $data['media_details'] ) ) {
    311             $data['media_details'] = new stdClass;
    312         } elseif ( ! empty( $data['media_details']['sizes'] ) ) {
    313 
    314             foreach ( $data['media_details']['sizes'] as $size => &$size_data ) {
    315 
    316                 if ( isset( $size_data['mime-type'] ) ) {
    317                     $size_data['mime_type'] = $size_data['mime-type'];
    318                     unset( $size_data['mime-type'] );
     300            $caption         = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
     301            $data['caption'] = array(
     302                'raw'      => $post->post_excerpt,
     303                'rendered' => $caption,
     304            );
     305        }
     306
     307        if ( in_array( 'alt_text', $fields, true ) ) {
     308            $data['alt_text'] = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
     309        }
     310
     311        if ( in_array( 'media_type', $fields, true ) ) {
     312            $data['media_type'] = wp_attachment_is_image( $post->ID ) ? 'image' : 'file';
     313        }
     314
     315        if ( in_array( 'mime_type', $fields, true ) ) {
     316            $data['mime_type'] = $post->post_mime_type;
     317        }
     318
     319        if ( in_array( 'media_details', $fields, true ) ) {
     320            $data['media_details'] = wp_get_attachment_metadata( $post->ID );
     321
     322            // Ensure empty details is an empty object.
     323            if ( empty( $data['media_details'] ) ) {
     324                $data['media_details'] = new stdClass;
     325            } elseif ( ! empty( $data['media_details']['sizes'] ) ) {
     326
     327                foreach ( $data['media_details']['sizes'] as $size => &$size_data ) {
     328
     329                    if ( isset( $size_data['mime-type'] ) ) {
     330                        $size_data['mime_type'] = $size_data['mime-type'];
     331                        unset( $size_data['mime-type'] );
     332                    }
     333
     334                    // Use the same method image_downsize() does.
     335                    $image_src = wp_get_attachment_image_src( $post->ID, $size );
     336                    if ( ! $image_src ) {
     337                        continue;
     338                    }
     339
     340                    $size_data['source_url'] = $image_src[0];
    319341                }
    320342
    321                 // Use the same method image_downsize() does.
    322                 $image_src = wp_get_attachment_image_src( $post->ID, $size );
    323                 if ( ! $image_src ) {
    324                     continue;
     343                $full_src = wp_get_attachment_image_src( $post->ID, 'full' );
     344
     345                if ( ! empty( $full_src ) ) {
     346                    $data['media_details']['sizes']['full'] = array(
     347                        'file'       => wp_basename( $full_src[0] ),
     348                        'width'      => $full_src[1],
     349                        'height'     => $full_src[2],
     350                        'mime_type'  => $post->post_mime_type,
     351                        'source_url' => $full_src[0],
     352                    );
    325353                }
    326 
    327                 $size_data['source_url'] = $image_src[0];
    328             }
    329 
    330             $full_src = wp_get_attachment_image_src( $post->ID, 'full' );
    331 
    332             if ( ! empty( $full_src ) ) {
    333                 $data['media_details']['sizes']['full'] = array(
    334                     'file'       => wp_basename( $full_src[0] ),
    335                     'width'      => $full_src[1],
    336                     'height'     => $full_src[2],
    337                     'mime_type'  => $post->post_mime_type,
    338                     'source_url' => $full_src[0],
    339                 );
    340             }
    341         } else {
    342             $data['media_details']['sizes'] = new stdClass;
     354            } else {
     355                $data['media_details']['sizes'] = new stdClass;
     356            }
     357        }
     358
     359        if ( in_array( 'post', $fields, true ) ) {
     360            $data['post'] = ! empty( $post->post_parent ) ? (int) $post->post_parent : null;
     361        }
     362
     363        if ( in_array( 'source_url', $fields, true ) ) {
     364            $data['source_url'] = wp_get_attachment_url( $post->ID );
    343365        }
    344366
Note: See TracChangeset for help on using the changeset viewer.