PluginProbe ʕ •ᴥ•ʔ
Modern Image Formats / 2.1.0
Modern Image Formats v2.1.0
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
deprecated.php 2 years ago helper.php 1 year ago hooks.php 1 year ago image-edit.php 2 years ago load.php 1 year ago picture-element.php 1 year ago readme.txt 1 year ago rest-api.php 2 years ago settings.php 1 year ago uninstall.php 2 years ago
rest-api.php
55 lines
1 <?php
2 /**
3 * REST API integration for the plugin.
4 *
5 * @package webp-uploads
6 *
7 * @since 1.0.0
8 */
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 /**
15 * Updates the response for an attachment to include sources for additional mime types available the image.
16 *
17 * @since 1.0.0
18 *
19 * @param WP_REST_Response $response The original response object.
20 * @param WP_Post $post The post object.
21 * @return WP_REST_Response A new response object for the attachment with additional sources.
22 */
23 function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Post $post ): WP_REST_Response {
24 $data = $response->get_data();
25 if ( ! isset( $data['media_details'] ) || ! is_array( $data['media_details'] ) || ! isset( $data['media_details']['sizes'] ) || ! is_array( $data['media_details']['sizes'] ) ) {
26 return $response;
27 }
28
29 foreach ( $data['media_details']['sizes'] as $size => &$details ) {
30
31 if ( empty( $details['sources'] ) || ! is_array( $details['sources'] ) ) {
32 continue;
33 }
34
35 $image_url_basename = wp_basename( $details['source_url'] );
36 foreach ( $details['sources'] as $mime => &$mime_details ) {
37 $mime_details['source_url'] = str_replace( $image_url_basename, $mime_details['file'], $details['source_url'] );
38 }
39 }
40
41 $full_src = wp_get_attachment_image_src( $post->ID, 'full' );
42 if ( ! empty( $full_src ) && ! empty( $data['media_details']['sources'] ) && ! empty( $data['media_details']['sizes']['full'] ) ) {
43 $full_url_basename = wp_basename( $full_src[0] );
44 foreach ( $data['media_details']['sources'] as $mime => &$mime_details ) {
45 $mime_details['source_url'] = str_replace( $full_url_basename, $mime_details['file'], $full_src[0] );
46 }
47
48 $data['media_details']['sizes']['full']['sources'] = $data['media_details']['sources'];
49 unset( $data['media_details']['sources'] );
50 }
51
52 return new WP_REST_Response( $data );
53 }
54 add_filter( 'rest_prepare_attachment', 'webp_uploads_update_rest_attachment', 10, 2 );
55