PluginProbe ʕ •ᴥ•ʔ
Modern Image Formats / 2.5.0
Modern Image Formats v2.5.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 1 year ago hooks.php 1 year ago image-edit.php 1 year ago load.php 1 year ago picture-element.php 1 year ago readme.txt 1 year ago rest-api.php 1 year ago settings.php 1 year ago uninstall.php 1 year ago
rest-api.php
57 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 ( empty( $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 ( ! empty( $full_src ) && ! empty( $data['media_details']['sources'] ) && ! empty( $data['media_details']['sizes']['full'] ) ) {
45 $full_url_basename = wp_basename( $full_src[0] );
46 foreach ( $data['media_details']['sources'] as $mime => &$mime_details ) {
47 $mime_details['source_url'] = str_replace( $full_url_basename, $mime_details['file'], $full_src[0] );
48 }
49
50 $data['media_details']['sizes']['full']['sources'] = $data['media_details']['sources'];
51 unset( $data['media_details']['sources'] );
52 }
53
54 return new WP_REST_Response( $data );
55 }
56 add_filter( 'rest_prepare_attachment', 'webp_uploads_update_rest_attachment', 10, 2 );
57