PluginProbe ʕ •ᴥ•ʔ
Modern Image Formats / 1.0.5
Modern Image Formats v1.0.5
2.7.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 2.0.0 2.0.1 2.0.2 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.5.1 2.6.0 2.6.1
webp-uploads / rest-api.php
webp-uploads Last commit date
.wordpress-org 2 years ago .gitattributes 2 years ago can-load.php 2 years ago fallback.js 3 years ago helper.php 2 years ago hooks.php 2 years ago image-edit.php 2 years ago load.php 2 years ago readme.txt 2 years ago rest-api.php 2 years ago settings.php 2 years ago
rest-api.php
54 lines
1 <?php
2 /**
3 * REST API integration for the module.
4 *
5 * @package webp-uploads
6 * @since 1.0.0
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 /**
14 * Updates the response for an attachment to include sources for additional mime types available the image.
15 *
16 * @since 1.0.0
17 *
18 * @param WP_REST_Response $response The original response object.
19 * @param WP_Post $post The post object.
20 * @return WP_REST_Response A new response object for the attachment with additional sources.
21 */
22 function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Post $post ) {
23 $data = $response->get_data();
24 if ( ! isset( $data['media_details'] ) || ! is_array( $data['media_details'] ) || ! isset( $data['media_details']['sizes'] ) || ! is_array( $data['media_details']['sizes'] ) ) {
25 return $response;
26 }
27
28 foreach ( $data['media_details']['sizes'] as $size => &$details ) {
29
30 if ( empty( $details['sources'] ) || ! is_array( $details['sources'] ) ) {
31 continue;
32 }
33
34 $image_url_basename = wp_basename( $details['source_url'] );
35 foreach ( $details['sources'] as $mime => &$mime_details ) {
36 $mime_details['source_url'] = str_replace( $image_url_basename, $mime_details['file'], $details['source_url'] );
37 }
38 }
39
40 $full_src = wp_get_attachment_image_src( $post->ID, 'full' );
41 if ( ! empty( $full_src ) && ! empty( $data['media_details']['sources'] ) && ! empty( $data['media_details']['sizes']['full'] ) ) {
42 $full_url_basename = wp_basename( $full_src[0] );
43 foreach ( $data['media_details']['sources'] as $mime => &$mime_details ) {
44 $mime_details['source_url'] = str_replace( $full_url_basename, $mime_details['file'], $full_src[0] );
45 }
46
47 $data['media_details']['sizes']['full']['sources'] = $data['media_details']['sources'];
48 unset( $data['media_details']['sources'] );
49 }
50
51 return rest_ensure_response( $data );
52 }
53 add_filter( 'rest_prepare_attachment', 'webp_uploads_update_rest_attachment', 10, 2 );
54