PluginProbe ʕ •ᴥ•ʔ
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN / 3.14.1
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN v3.14.1
4.1.0 4.0.3 4.0.2 2.8.1 2.9.1 3.0.0 3.0.1 3.0.2 3.1.1 3.10.1 3.10.2 3.10.3 3.11.1 3.12.3 3.12.4 3.12.5 3.12.6 3.13.0 3.13.1 3.13.2 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.2 3.16.4 3.16.5 3.16.6 3.17.0 3.17.1 3.18.0 3.18.1 3.2.0.1 3.2.1 3.2.2.1 3.2.4 3.20.0 3.21.1 3.22.1 3.22.3 3.23.0 3.23.1 3.23.2 3.23.3 3.23.4 3.24.0 3.24.0-beta.2 3.3.0 3.3.1 3.3.2 3.4.1 3.4.2 3.6.1 3.6.3 3.7.0 3.7.1 3.7.2 3.7.3 3.8.2 3.8.3 3.8.4 3.8.5 3.8.7 3.8.8 3.9.0 3.9.1 3.9.11 3.9.2 3.9.4 3.9.5 3.9.8 3.9.9 trunk 1.0.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.2 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.5.1 1.6.5.2 1.6.5.3 1.6.5.4 1.7 1.7.1 1.7.1.1 2.0 2.0.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.6.2 2.0.6.3 2.0.6.5 2.0.7 2.0.7.1 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.2 2.3 2.3.1 2.4 2.4.2 2.4.3 2.4.4 2.4.5 2.5.2 2.5.3 2.6.1 2.6.2 2.6.3 2.7 2.7.1 2.7.4 2.7.4.1 2.7.5 2.7.6 2.7.8 2.7.8.1 2.7.9.1 2.8.0 2.8.0.1
wp-smushit / core / class-rest.php
wp-smushit / core Last commit date
api 2 years ago backups 2 years ago external 2 years ago integrations 2 years ago media 2 years ago media-library 2 years ago modules 2 years ago photon 2 years ago png2jpg 2 years ago resize 2 years ago s3 2 years ago smush 2 years ago stats 2 years ago webp 2 years ago class-animated-status-controller.php 2 years ago class-array-utils.php 2 years ago class-attachment-id-list.php 2 years ago class-backup-size.php 2 years ago class-cli.php 2 years ago class-configs.php 2 years ago class-controller.php 2 years ago class-core.php 2 years ago class-deprecated-hooks.php 2 years ago class-error-handler.php 2 years ago class-file-system.php 2 years ago class-helper.php 2 years ago class-installer.php 2 years ago class-modules.php 2 years ago class-optimization-controller.php 2 years ago class-plugin-settings-watcher.php 2 years ago class-rest.php 2 years ago class-server-utils.php 2 years ago class-settings.php 2 years ago class-smush-file.php 2 years ago class-stats.php 2 years ago class-upload-dir.php 2 years ago
class-rest.php
118 lines
1 <?php
2 /**
3 * Smush integration with Rest API: Rest class
4 *
5 * @package Smush\Core
6 * @since 2.8.0
7 *
8 * @author Anton Vanyukov <anton@incsub.com>
9 *
10 * @copyright (c) 2018, Incsub (http://incsub.com)
11 */
12
13 namespace Smush\Core;
14
15 use WP_Smush;
16
17 if ( ! defined( 'WPINC' ) ) {
18 die;
19 }
20
21 /**
22 * Singleton class Rest for extending the WordPress REST API interface.
23 *
24 * @since 2.8.0
25 */
26 class Rest {
27
28 /**
29 * Rest constructor.
30 */
31 public function __construct() {
32 // Register smush meta fields and callbacks for the image object in the
33 // wp-json/wp/v2/media REST API endpoint.
34 add_action( 'rest_api_init', array( $this, 'register_smush_meta' ) );
35
36 // Custom route for handling configs.
37 add_action( 'rest_api_init', array( $this, 'register_configs_route' ) );
38 }
39
40 /**
41 * Callback for rest_api_init action.
42 *
43 * @since 2.8.0
44 */
45 public function register_smush_meta() {
46 register_rest_field(
47 'attachment',
48 'smush',
49 array(
50 'get_callback' => array( $this, 'register_image_stats' ),
51 'schema' => array(
52 'description' => __( 'Smush data.', 'wp-smushit' ),
53 'type' => 'string',
54 ),
55 )
56 );
57 }
58
59 /**
60 * Add image stats to the wp-json/wp/v2/media REST API endpoint.
61 *
62 * Will add the stats from wp-smpro-smush-data image meta key to the media REST API endpoint.
63 * If image is Smushed, the stats from the meta can be queried, if the not - the status of Smushing
64 * will be displayed as a string in the API.
65 *
66 * @since 2.8.0
67 *
68 * @link https://developer.wordpress.org/rest-api/reference/media/
69 *
70 * @param array $image Image array.
71 *
72 * @return array|string
73 */
74 public function register_image_stats( $image ) {
75 if ( get_transient( 'smush-in-progress-' . $image['id'] ) ) {
76 return __( 'Smushing in progress', 'wp-smushit' );
77 }
78
79 $wp_smush_data = get_post_meta( $image['id'], Modules\Smush::$smushed_meta_key, true );
80
81 if ( empty( $wp_smush_data ) ) {
82 return __( 'Not processed', 'wp-smushit' );
83 }
84
85 $wp_resize_savings = get_post_meta( $image['id'], 'wp-smush-resize_savings', true );
86 $conversion_savings = get_post_meta( $image['id'], 'wp-smush-pngjpg_savings', true );
87
88 $combined_stats = WP_Smush::get_instance()->core()->combined_stats( $wp_smush_data, $wp_resize_savings );
89
90 return WP_Smush::get_instance()->core()->combine_conversion_stats( $combined_stats, $conversion_savings );
91 }
92
93 /**
94 * Registers the custom route for handling configs.
95 *
96 * @since 3.8.6
97 */
98 public function register_configs_route() {
99 $configs_handler = new Configs();
100 register_rest_route(
101 'wp-smush/v1',
102 '/preset_configs/',
103 array(
104 array(
105 'methods' => 'GET',
106 'callback' => array( $configs_handler, 'get_callback' ),
107 'permission_callback' => array( $configs_handler, 'permission_callback' ),
108 ),
109 array(
110 'methods' => 'POST',
111 'callback' => array( $configs_handler, 'post_callback' ),
112 'permission_callback' => array( $configs_handler, 'permission_callback' ),
113 ),
114 )
115 );
116 }
117 }
118