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