Make WordPress Core

Changeset 53786


Ignore:
Timestamp:
07/27/2022 03:13:16 PM (2 years ago)
Author:
flixos90
Message:

Media: Add information about additional MIME type sources to attachments REST endpoints.

This changeset is a follow-up to [53751] which ensures the additional sources information stored in attachment metadata are available under media_details for each image size in the REST API responses for attachments.

Props mukesh27, eugenemanuilov, mitogh, flixos90, aaemnnosttv.
See #55443.

Location:
trunk
Files:
2 edited

Legend:

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

    r52068 r53786  
    784784
    785785                    $size_data['source_url'] = $image_src[0];
     786
     787                    if ( empty( $size_data['sources'] ) || ! is_array( $size_data['sources'] ) ) {
     788                        continue;
     789                    }
     790
     791                    $image_url_basename = wp_basename( $image_src[0] );
     792                    foreach ( $size_data['sources'] as $mime => &$mime_details ) {
     793                        $mime_details['source_url'] = str_replace( $image_url_basename, $mime_details['file'], $image_src[0] );
     794                    }
    786795                }
    787796
     
    796805                        'source_url' => $full_src[0],
    797806                    );
     807
     808                    if ( ! empty( $data['media_details']['sources'] ) ) {
     809                        $full_url_basename = wp_basename( $full_src[0] );
     810                        foreach ( $data['media_details']['sources'] as $mime => &$mime_details ) {
     811                            $mime_details['source_url'] = str_replace( $full_url_basename, $mime_details['file'], $full_src[0] );
     812                        }
     813                        $data['media_details']['sizes']['full']['sources'] = $data['media_details']['sources'];
     814                        unset( $data['media_details']['sources'] );
     815                    }
    798816                }
    799817            } else {
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r53498 r53786  
    22632263        );
    22642264    }
     2265
     2266    /**
     2267     * @ticket 55443
     2268     */
     2269    public function test_image_sources_to_rest_response() {
     2270
     2271        $attachment_id = self::factory()->attachment->create_upload_object( $this->test_file );
     2272        $metadata      = wp_get_attachment_metadata( $attachment_id );
     2273        $request       = new WP_REST_Request();
     2274        $request['id'] = $attachment_id;
     2275        $controller    = new WP_REST_Attachments_Controller( 'attachment' );
     2276        $response      = $controller->get_item( $request );
     2277
     2278        $this->assertNotWPError( $response );
     2279
     2280        $data       = $response->get_data();
     2281        $mime_types = array(
     2282            'image/jpeg',
     2283        );
     2284
     2285        if ( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     2286            array_push( $mime_types, 'image/webp' );
     2287        }
     2288
     2289        foreach ( $data['media_details']['sizes'] as $size_name => $properties ) {
     2290            if ( ! isset( $metadata['sizes'][ $size_name ]['sources'] ) ) {
     2291                continue;
     2292            }
     2293
     2294            $this->assertArrayHasKey( 'sources', $properties );
     2295            $this->assertIsArray( $properties['sources'] );
     2296
     2297            foreach ( $mime_types as $mime_type ) {
     2298                $this->assertArrayHasKey( $mime_type, $properties['sources'] );
     2299                $this->assertArrayHasKey( 'filesize', $properties['sources'][ $mime_type ] );
     2300                $this->assertArrayHasKey( 'file', $properties['sources'][ $mime_type ] );
     2301                $this->assertArrayHasKey( 'source_url', $properties['sources'][ $mime_type ] );
     2302                $this->assertNotFalse( filter_var( $properties['sources'][ $mime_type ]['source_url'], FILTER_VALIDATE_URL ) );
     2303            }
     2304        }
     2305        $this->assertArrayNotHasKey( 'sources', $data['media_details'] );
     2306    }
    22652307}
Note: See TracChangeset for help on using the changeset viewer.