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