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 |