webp-uploads
Last commit date
deprecated.php
14 hours ago
helper.php
14 hours ago
hooks.php
14 hours ago
image-edit.php
14 hours ago
load.php
14 hours ago
picture-element.php
14 hours ago
readme.txt
14 hours ago
rest-api.php
14 hours ago
settings.php
14 hours ago
uninstall.php
14 hours ago
rest-api.php
68 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 ( ! isset( $data['media_details'] ) || ! is_array( $data['media_details'] ) || ! isset( $data['media_details']['sizes'] ) || ! is_array( $data['media_details']['sizes'] ) ) { |
| 30 | return $response; |
| 31 | } |
| 32 | |
| 33 | foreach ( $data['media_details']['sizes'] as $size => &$details ) { |
| 34 | |
| 35 | if ( ! isset( $details['sources'] ) || ! is_array( $details['sources'] ) ) { |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | $image_url_basename = wp_basename( $details['source_url'] ); |
| 40 | foreach ( $details['sources'] as $mime => &$mime_details ) { |
| 41 | $mime_details['source_url'] = str_replace( $image_url_basename, $mime_details['file'], $details['source_url'] ); |
| 42 | } |
| 43 | unset( $mime_details ); |
| 44 | } |
| 45 | unset( $details ); |
| 46 | |
| 47 | $full_src = wp_get_attachment_image_src( $post->ID, 'full' ); |
| 48 | if ( |
| 49 | isset( $full_src[0] ) && |
| 50 | isset( $data['media_details']['sources'] ) && |
| 51 | is_array( $data['media_details']['sources'] ) && |
| 52 | isset( $data['media_details']['sizes']['full'] ) && |
| 53 | is_array( $data['media_details']['sizes']['full'] ) |
| 54 | ) { |
| 55 | $full_url_basename = wp_basename( $full_src[0] ); |
| 56 | foreach ( $data['media_details']['sources'] as $mime => &$mime_details ) { |
| 57 | $mime_details['source_url'] = str_replace( $full_url_basename, $mime_details['file'], $full_src[0] ); |
| 58 | } |
| 59 | unset( $mime_details ); |
| 60 | |
| 61 | $data['media_details']['sizes']['full']['sources'] = $data['media_details']['sources']; |
| 62 | unset( $data['media_details']['sources'] ); |
| 63 | } |
| 64 | |
| 65 | return new WP_REST_Response( $data ); |
| 66 | } |
| 67 | add_filter( 'rest_prepare_attachment', 'webp_uploads_update_rest_attachment', 10, 2 ); |
| 68 |