compatibility
7 years ago
config
7 years ago
css
7 years ago
data
9 years ago
images
9 years ago
js
7 years ago
vendor
7 years ago
views
7 years ago
class-tiny-bulk-optimization.php
7 years ago
class-tiny-compress-client.php
7 years ago
class-tiny-compress-fopen.php
7 years ago
class-tiny-compress.php
7 years ago
class-tiny-exception.php
7 years ago
class-tiny-image-size.php
7 years ago
class-tiny-image.php
7 years ago
class-tiny-notices.php
7 years ago
class-tiny-php.php
7 years ago
class-tiny-plugin.php
7 years ago
class-tiny-settings.php
7 years ago
class-tiny-wp-base.php
7 years ago
class-tiny-plugin.php
696 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Tiny Compress Images - WordPress plugin. |
| 4 | * Copyright (C) 2015-2018 Tinify B.V. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | class Tiny_Plugin extends Tiny_WP_Base { |
| 21 | const VERSION = '3.1.0'; |
| 22 | const MEDIA_COLUMN = self::NAME; |
| 23 | const DATETIME_FORMAT = 'Y-m-d G:i:s'; |
| 24 | |
| 25 | private static $version; |
| 26 | |
| 27 | private $settings; |
| 28 | private $twig; |
| 29 | |
| 30 | public static function jpeg_quality() { |
| 31 | return 85; |
| 32 | } |
| 33 | |
| 34 | public static function version() { |
| 35 | /* Avoid using get_plugin_data() because it is not loaded early enough |
| 36 | in xmlrpc.php. */ |
| 37 | return self::VERSION; |
| 38 | } |
| 39 | |
| 40 | public function __construct() { |
| 41 | parent::__construct(); |
| 42 | |
| 43 | $this->settings = new Tiny_Settings(); |
| 44 | } |
| 45 | |
| 46 | public function set_compressor( $compressor ) { |
| 47 | $this->settings->set_compressor( $compressor ); |
| 48 | } |
| 49 | |
| 50 | public function init() { |
| 51 | add_filter( 'jpeg_quality', |
| 52 | $this->get_static_method( 'jpeg_quality' ) |
| 53 | ); |
| 54 | |
| 55 | add_filter( 'wp_editor_set_quality', |
| 56 | $this->get_static_method( 'jpeg_quality' ) |
| 57 | ); |
| 58 | |
| 59 | add_filter( 'wp_generate_attachment_metadata', |
| 60 | $this->get_method( 'process_attachment' ), |
| 61 | 10, 2 |
| 62 | ); |
| 63 | |
| 64 | /* When touching any functionality linked to image compressions when |
| 65 | uploading images make sure it also works with XML-RPC. See NOTES. */ |
| 66 | add_filter( 'wp_ajax_nopriv_tiny_rpc', |
| 67 | $this->get_method( 'process_rpc_request' ) |
| 68 | ); |
| 69 | |
| 70 | load_plugin_textdomain( self::NAME, false, |
| 71 | dirname( plugin_basename( __FILE__ ) ) . '/languages' |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | public function admin_init() { |
| 76 | add_action('wp_dashboard_setup', |
| 77 | $this->get_method( 'add_dashboard_widget' ) |
| 78 | ); |
| 79 | |
| 80 | add_action( 'admin_enqueue_scripts', |
| 81 | $this->get_method( 'enqueue_scripts' ) |
| 82 | ); |
| 83 | |
| 84 | add_action( 'admin_action_tiny_bulk_action', |
| 85 | $this->get_method( 'media_library_bulk_action' ) |
| 86 | ); |
| 87 | |
| 88 | add_action( 'admin_action_-1', |
| 89 | $this->get_method( 'media_library_bulk_action' ) |
| 90 | ); |
| 91 | |
| 92 | add_filter( 'manage_media_columns', |
| 93 | $this->get_method( 'add_media_columns' ) |
| 94 | ); |
| 95 | |
| 96 | add_action( 'manage_media_custom_column', |
| 97 | $this->get_method( 'render_media_column' ), |
| 98 | 10, 2 |
| 99 | ); |
| 100 | |
| 101 | add_action( 'attachment_submitbox_misc_actions', |
| 102 | $this->get_method( 'show_media_info' ) |
| 103 | ); |
| 104 | |
| 105 | add_filter( 'wp_ajax_tiny_async_optimize_upload_new_media', |
| 106 | $this->get_method( 'compress_on_upload' ) |
| 107 | ); |
| 108 | |
| 109 | add_action( 'wp_ajax_tiny_compress_image_from_library', |
| 110 | $this->get_method( 'compress_image_from_library' ) |
| 111 | ); |
| 112 | |
| 113 | add_action( 'wp_ajax_tiny_compress_image_for_bulk', |
| 114 | $this->get_method( 'compress_image_for_bulk' ) |
| 115 | ); |
| 116 | |
| 117 | add_action( 'wp_ajax_tiny_get_optimization_statistics', |
| 118 | $this->get_method( 'ajax_optimization_statistics' ) |
| 119 | ); |
| 120 | |
| 121 | add_action( 'wp_ajax_tiny_get_compression_status', |
| 122 | $this->get_method( 'ajax_compression_status' ) |
| 123 | ); |
| 124 | |
| 125 | $plugin = plugin_basename( |
| 126 | dirname( dirname( __FILE__ ) ) . '/tiny-compress-images.php' |
| 127 | ); |
| 128 | |
| 129 | add_filter( "plugin_action_links_$plugin", |
| 130 | $this->get_method( 'add_plugin_links' ) |
| 131 | ); |
| 132 | |
| 133 | add_action( 'wr2x_retina_file_added', |
| 134 | $this->get_method( 'compress_retina_image' ), |
| 135 | 10, 3 |
| 136 | ); |
| 137 | |
| 138 | add_action( 'wr2x_retina_file_removed', |
| 139 | $this->get_method( 'remove_retina_image' ), |
| 140 | 10, 2 |
| 141 | ); |
| 142 | |
| 143 | $this->tiny_compatibility(); |
| 144 | |
| 145 | add_thickbox(); |
| 146 | } |
| 147 | |
| 148 | public function admin_menu() { |
| 149 | add_media_page( |
| 150 | __( 'Bulk Optimization', 'tiny-compress-images' ), |
| 151 | esc_html__( 'Bulk Optimization', 'tiny-compress-images' ), |
| 152 | 'upload_files', |
| 153 | 'tiny-bulk-optimization', |
| 154 | $this->get_method( 'render_bulk_optimization_page' ) |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | public function add_plugin_links( $current_links ) { |
| 159 | $additional = array( |
| 160 | 'settings' => sprintf( |
| 161 | '<a href="options-general.php?page=tinify">%s</a>', |
| 162 | esc_html__( 'Settings', 'tiny-compress-images' ) |
| 163 | ), |
| 164 | 'bulk' => sprintf( |
| 165 | '<a href="upload.php?page=tiny-bulk-optimization">%s</a>', |
| 166 | esc_html__( 'Bulk Optimization', 'tiny-compress-images' ) |
| 167 | ), |
| 168 | ); |
| 169 | return array_merge( $additional, $current_links ); |
| 170 | } |
| 171 | |
| 172 | public function tiny_compatibility() { |
| 173 | if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { |
| 174 | $tiny_wpml_compatibility = new Tiny_WPML(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | public function compress_retina_image( $attachment_id, $path, $size_name ) { |
| 179 | if ( $this->settings->compress_wr2x_images() ) { |
| 180 | $tiny_image = new Tiny_Image( $this->settings, $attachment_id ); |
| 181 | $tiny_image->compress_retina( $size_name . '_wr2x', $path ); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | public function remove_retina_image( $attachment_id, $path ) { |
| 186 | $tiny_image = new Tiny_Image( $this->settings, $attachment_id ); |
| 187 | $tiny_image->remove_retina_metadata(); |
| 188 | } |
| 189 | |
| 190 | public function enqueue_scripts( $hook ) { |
| 191 | wp_enqueue_style( self::NAME . '_admin', |
| 192 | plugins_url( '/css/admin.css', __FILE__ ), |
| 193 | array(), self::version() |
| 194 | ); |
| 195 | |
| 196 | wp_enqueue_style( self::NAME . '_chart', |
| 197 | plugins_url( '/css/chart.css', __FILE__ ), |
| 198 | array(), self::version() |
| 199 | ); |
| 200 | |
| 201 | wp_register_script( self::NAME . '_admin', |
| 202 | plugins_url( '/js/admin.js', __FILE__ ), |
| 203 | array(), self::version(), true |
| 204 | ); |
| 205 | |
| 206 | // WordPress < 3.3 does not handle multidimensional arrays |
| 207 | wp_localize_script( self::NAME . '_admin', 'tinyCompress', array( |
| 208 | 'nonce' => wp_create_nonce( 'tiny-compress' ), |
| 209 | 'wpVersion' => self::wp_version(), |
| 210 | 'pluginVersion' => self::version(), |
| 211 | 'L10nAllDone' => __( 'All images are processed', 'tiny-compress-images' ), |
| 212 | 'L10nNoActionTaken' => __( 'No action taken', 'tiny-compress-images' ), |
| 213 | 'L10nBulkAction' => __( 'Compress Images', 'tiny-compress-images' ), |
| 214 | 'L10nCancelled' => __( 'Cancelled', 'tiny-compress-images' ), |
| 215 | 'L10nCompressing' => __( 'Compressing', 'tiny-compress-images' ), |
| 216 | 'L10nCompressed' => __( 'compressed', 'tiny-compress-images' ), |
| 217 | 'L10nFile' => __( 'File', 'tiny-compress-images' ), |
| 218 | 'L10nSizesOptimized' => __( 'Sizes optimized', 'tiny-compress-images' ), |
| 219 | 'L10nInitialSize' => __( 'Initial size', 'tiny-compress-images' ), |
| 220 | 'L10nCurrentSize' => __( 'Current size', 'tiny-compress-images' ), |
| 221 | 'L10nSavings' => __( 'Savings', 'tiny-compress-images' ), |
| 222 | 'L10nStatus' => __( 'Status', 'tiny-compress-images' ), |
| 223 | 'L10nShowMoreDetails' => __( 'Show more details', 'tiny-compress-images' ), |
| 224 | 'L10nError' => __( 'Error', 'tiny-compress-images' ), |
| 225 | 'L10nLatestError' => __( 'Latest error', 'tiny-compress-images' ), |
| 226 | 'L10nInternalError' => __( 'Internal error', 'tiny-compress-images' ), |
| 227 | 'L10nOutOf' => __( 'out of', 'tiny-compress-images' ), |
| 228 | 'L10nWaiting' => __( 'Waiting', 'tiny-compress-images' ), |
| 229 | )); |
| 230 | |
| 231 | wp_enqueue_script( self::NAME . '_admin' ); |
| 232 | |
| 233 | if ( 'media_page_tiny-bulk-optimization' == $hook ) { |
| 234 | wp_enqueue_style( |
| 235 | self::NAME . '_tiny_bulk_optimization', |
| 236 | plugins_url( '/css/bulk-optimization.css', __FILE__ ), |
| 237 | array(), self::version() |
| 238 | ); |
| 239 | |
| 240 | wp_enqueue_style( self::NAME . '_chart', |
| 241 | plugins_url( '/css/chart.css', __FILE__ ), |
| 242 | array(), self::version() |
| 243 | ); |
| 244 | |
| 245 | wp_register_script( |
| 246 | self::NAME . '_tiny_bulk_optimization', |
| 247 | plugins_url( '/js/bulk-optimization.js', __FILE__ ), |
| 248 | array(), self::version(), true |
| 249 | ); |
| 250 | |
| 251 | wp_enqueue_script( self::NAME . '_tiny_bulk_optimization' ); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | public function process_attachment( $metadata, $attachment_id ) { |
| 256 | if ( $this->settings->auto_compress_enabled() ) { |
| 257 | if ( |
| 258 | $this->settings->background_compress_enabled() && |
| 259 | ! $this->settings->remove_local_files_setting_enabled() |
| 260 | ) { |
| 261 | $this->async_compress_on_upload( $metadata, $attachment_id ); |
| 262 | } else { |
| 263 | return $this->blocking_compress_on_upload( $metadata, $attachment_id ); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return $metadata; |
| 268 | } |
| 269 | |
| 270 | public function blocking_compress_on_upload( $metadata, $attachment_id ) { |
| 271 | if ( ! empty( $metadata ) ) { |
| 272 | $tiny_image = new Tiny_Image( $this->settings, $attachment_id, $metadata ); |
| 273 | $result = $tiny_image->compress( $this->settings ); |
| 274 | return $tiny_image->get_wp_metadata(); |
| 275 | } else { |
| 276 | return $metadata; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | public function async_compress_on_upload( $metadata, $attachment_id ) { |
| 281 | $context = 'wp'; |
| 282 | $action = 'tiny_async_optimize_upload_new_media'; |
| 283 | $_ajax_nonce = wp_create_nonce( 'new_media-' . $attachment_id ); |
| 284 | $body = compact( 'action', '_ajax_nonce', 'metadata', 'attachment_id', 'context' ); |
| 285 | |
| 286 | $args = array( |
| 287 | 'timeout' => 0.01, |
| 288 | 'blocking' => false, |
| 289 | 'body' => $body, |
| 290 | 'cookies' => isset( $_COOKIE ) && is_array( $_COOKIE ) ? $_COOKIE : array(), |
| 291 | 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
| 292 | ); |
| 293 | |
| 294 | if ( defined( 'XMLRPC_REQUEST' ) && get_current_user_id() ) { |
| 295 | /* We generate a hash to be used for the transient we use to store the current user. */ |
| 296 | $rpc_hash = md5( maybe_serialize( $body ) ); |
| 297 | |
| 298 | $args['body']['tiny_rpc_action'] = $args['body']['action']; |
| 299 | /* We set a different action to make sure that all RPC requests are first validated. */ |
| 300 | $args['body']['action'] = 'tiny_rpc'; |
| 301 | $args['body']['tiny_rpc_hash'] = $rpc_hash; |
| 302 | $args['body']['tiny_rpc_nonce'] = wp_create_nonce( 'tiny_rpc_' . $rpc_hash ); |
| 303 | |
| 304 | /* |
| 305 | We can't use cookies here, so we save the user id in a transient |
| 306 | so that we can retrieve it again when processing the RPC request. |
| 307 | We should be able to use a relatively short timeout, as the request |
| 308 | should be processed directly afterwards. |
| 309 | */ |
| 310 | set_transient( 'tiny_rpc_' . $rpc_hash, get_current_user_id(), 10 ); |
| 311 | } |
| 312 | |
| 313 | if ( getenv( 'WORDPRESS_HOST' ) !== false ) { |
| 314 | wp_remote_post( getenv( 'WORDPRESS_HOST' ) . '/wp-admin/admin-ajax.php', $args ); |
| 315 | } else { |
| 316 | wp_remote_post( admin_url( 'admin-ajax.php' ), $args ); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | public function process_rpc_request() { |
| 321 | if ( |
| 322 | empty( $_POST['tiny_rpc_action'] ) || |
| 323 | empty( $_POST['tiny_rpc_hash'] ) || |
| 324 | 32 !== strlen( $_POST['tiny_rpc_hash'] ) |
| 325 | ) { |
| 326 | exit(); |
| 327 | } |
| 328 | |
| 329 | $rpc_hash = sanitize_key( $_POST['tiny_rpc_hash'] ); |
| 330 | $user_id = absint( get_transient( 'tiny_rpc_' . $rpc_hash ) ); |
| 331 | $user = $user_id ? get_userdata( $user_id ) : false; |
| 332 | |
| 333 | /* We no longer need the transient. */ |
| 334 | delete_transient( 'tiny_rpc_' . $rpc_hash ); |
| 335 | |
| 336 | if ( ! $user || ! $user->exists() ) { |
| 337 | exit(); |
| 338 | } |
| 339 | wp_set_current_user( $user_id ); |
| 340 | |
| 341 | if ( ! check_ajax_referer( 'tiny_rpc_' . $rpc_hash, 'tiny_rpc_nonce', false ) ) { |
| 342 | exit(); |
| 343 | } |
| 344 | |
| 345 | /* Now that everything is checked, perform the actual action. */ |
| 346 | $action = $_POST['tiny_rpc_action']; |
| 347 | unset( |
| 348 | $_POST['action'], |
| 349 | $_POST['tiny_rpc_action'], |
| 350 | $_POST['tiny_rpc_id'], |
| 351 | $_POST['tiny_rpc_nonce'] |
| 352 | ); |
| 353 | do_action( 'wp_ajax_' . $action ); |
| 354 | } |
| 355 | |
| 356 | public function compress_on_upload() { |
| 357 | $attachment_id = intval( $_POST['attachment_id'] ); |
| 358 | $metadata = $_POST['metadata']; |
| 359 | if ( is_array( $metadata ) ) { |
| 360 | $tiny_image = new Tiny_Image( $this->settings, $attachment_id, $metadata ); |
| 361 | $result = $tiny_image->compress( $this->settings ); |
| 362 | // The wp_update_attachment_metadata call is thrown because the |
| 363 | // dimensions of the original image can change. This will then |
| 364 | // trigger other plugins and can result in unexpected behaviour and |
| 365 | // further changes to the image. This may require another approach. |
| 366 | wp_update_attachment_metadata( $attachment_id, $tiny_image->get_wp_metadata() ); |
| 367 | } |
| 368 | exit(); |
| 369 | } |
| 370 | |
| 371 | public function compress_image_from_library() { |
| 372 | if ( ! $this->check_ajax_referer() ) { |
| 373 | exit(); |
| 374 | } |
| 375 | if ( ! current_user_can( 'upload_files' ) ) { |
| 376 | $message = esc_html__( |
| 377 | "You don't have permission to upload files.", |
| 378 | 'tiny-compress-images' |
| 379 | ); |
| 380 | echo $message; |
| 381 | exit(); |
| 382 | } |
| 383 | if ( empty( $_POST['id'] ) ) { |
| 384 | $message = esc_html__( |
| 385 | 'Not a valid media file.', |
| 386 | 'tiny-compress-images' |
| 387 | ); |
| 388 | echo $message; |
| 389 | exit(); |
| 390 | } |
| 391 | $id = intval( $_POST['id'] ); |
| 392 | $metadata = wp_get_attachment_metadata( $id ); |
| 393 | if ( ! is_array( $metadata ) ) { |
| 394 | $message = esc_html__( |
| 395 | 'Could not find metadata of media file.', |
| 396 | 'tiny-compress-images' |
| 397 | ); |
| 398 | echo $message; |
| 399 | exit; |
| 400 | } |
| 401 | |
| 402 | $tiny_image = new Tiny_Image( $this->settings, $id, $metadata ); |
| 403 | $result = $tiny_image->compress( $this->settings ); |
| 404 | |
| 405 | // The wp_update_attachment_metadata call is thrown because the |
| 406 | // dimensions of the original image can change. This will then |
| 407 | // trigger other plugins and can result in unexpected behaviour and |
| 408 | // further changes to the image. This may require another approach. |
| 409 | wp_update_attachment_metadata( $id, $tiny_image->get_wp_metadata() ); |
| 410 | |
| 411 | echo $this->render_compress_details( $tiny_image ); |
| 412 | |
| 413 | exit(); |
| 414 | } |
| 415 | |
| 416 | public function compress_image_for_bulk() { |
| 417 | if ( ! $this->check_ajax_referer() ) { |
| 418 | exit(); |
| 419 | } |
| 420 | if ( ! current_user_can( 'upload_files' ) ) { |
| 421 | $message = esc_html__( |
| 422 | "You don't have permission to upload files.", |
| 423 | 'tiny-compress-images' |
| 424 | ); |
| 425 | echo json_encode( array( |
| 426 | 'error' => $message, |
| 427 | ) ); |
| 428 | exit(); |
| 429 | } |
| 430 | if ( empty( $_POST['id'] ) ) { |
| 431 | $message = esc_html__( |
| 432 | 'Not a valid media file.', |
| 433 | 'tiny-compress-images' |
| 434 | ); |
| 435 | echo json_encode( array( |
| 436 | 'error' => $message, |
| 437 | ) ); |
| 438 | exit(); |
| 439 | } |
| 440 | $id = intval( $_POST['id'] ); |
| 441 | $metadata = wp_get_attachment_metadata( $id ); |
| 442 | if ( ! is_array( $metadata ) ) { |
| 443 | $message = esc_html__( |
| 444 | 'Could not find metadata of media file.', |
| 445 | 'tiny-compress-images' |
| 446 | ); |
| 447 | echo json_encode( array( |
| 448 | 'error' => $message, |
| 449 | ) ); |
| 450 | exit; |
| 451 | } |
| 452 | |
| 453 | $tiny_image_before = new Tiny_Image( $this->settings, $id, $metadata ); |
| 454 | $image_statistics_before = $tiny_image_before->get_statistics( |
| 455 | $this->settings->get_sizes(), |
| 456 | $this->settings->get_active_tinify_sizes() |
| 457 | ); |
| 458 | $size_before = $image_statistics_before['optimized_total_size']; |
| 459 | |
| 460 | $tiny_image = new Tiny_Image( $this->settings, $id, $metadata ); |
| 461 | $result = $tiny_image->compress( $this->settings ); |
| 462 | $image_statistics = $tiny_image->get_statistics( |
| 463 | $this->settings->get_sizes(), |
| 464 | $this->settings->get_active_tinify_sizes() |
| 465 | ); |
| 466 | wp_update_attachment_metadata( $id, $tiny_image->get_wp_metadata() ); |
| 467 | |
| 468 | $current_library_size = intval( $_POST['current_size'] ); |
| 469 | $size_after = $image_statistics['optimized_total_size']; |
| 470 | $new_library_size = $current_library_size + $size_after - $size_before; |
| 471 | |
| 472 | $result['message'] = $tiny_image->get_latest_error(); |
| 473 | $result['image_sizes_optimized'] = $image_statistics['image_sizes_optimized']; |
| 474 | |
| 475 | $result['initial_total_size'] = size_format( |
| 476 | $image_statistics['initial_total_size'], 1 |
| 477 | ); |
| 478 | |
| 479 | $result['optimized_total_size'] = size_format( |
| 480 | $image_statistics['optimized_total_size'], 1 |
| 481 | ); |
| 482 | |
| 483 | $result['savings'] = $tiny_image->get_savings( $image_statistics ); |
| 484 | $result['status'] = $this->settings->get_status(); |
| 485 | $result['thumbnail'] = wp_get_attachment_image( |
| 486 | $id, array( '30', '30' ), true, array( |
| 487 | 'class' => 'pinkynail', |
| 488 | 'alt' => '', |
| 489 | ) |
| 490 | ); |
| 491 | $result['size_change'] = $size_after - $size_before; |
| 492 | $result['human_readable_library_size'] = size_format( $new_library_size, 2 ); |
| 493 | |
| 494 | echo json_encode( $result ); |
| 495 | |
| 496 | exit(); |
| 497 | } |
| 498 | |
| 499 | public function ajax_optimization_statistics() { |
| 500 | if ( ! $this->check_ajax_referer() ) { |
| 501 | exit(); |
| 502 | } |
| 503 | $stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->settings ); |
| 504 | echo json_encode( $stats ); |
| 505 | exit(); |
| 506 | } |
| 507 | |
| 508 | public function ajax_compression_status() { |
| 509 | if ( ! $this->check_ajax_referer() ) { |
| 510 | exit(); |
| 511 | } |
| 512 | |
| 513 | if ( empty( $_POST['id'] ) ) { |
| 514 | $message = esc_html__( |
| 515 | 'Not a valid media file.', |
| 516 | 'tiny-compress-images' |
| 517 | ); |
| 518 | echo $message; |
| 519 | exit(); |
| 520 | } |
| 521 | $id = intval( $_POST['id'] ); |
| 522 | $metadata = wp_get_attachment_metadata( $id ); |
| 523 | if ( ! is_array( $metadata ) ) { |
| 524 | $message = esc_html__( |
| 525 | 'Could not find metadata of media file.', |
| 526 | 'tiny-compress-images' |
| 527 | ); |
| 528 | echo $message; |
| 529 | exit; |
| 530 | } |
| 531 | |
| 532 | $tiny_image = new Tiny_Image( $this->settings, $id, $metadata ); |
| 533 | |
| 534 | echo $this->render_compress_details( $tiny_image ); |
| 535 | |
| 536 | exit(); |
| 537 | } |
| 538 | |
| 539 | public function media_library_bulk_action() { |
| 540 | if ( empty( $_REQUEST['action'] ) || ( |
| 541 | 'tiny_bulk_action' != $_REQUEST['action'] && |
| 542 | 'tiny_bulk_action' != $_REQUEST['action2'] ) ) { |
| 543 | return; |
| 544 | } |
| 545 | if ( empty( $_REQUEST['media'] ) || ( ! $_REQUEST['media'] ) ) { |
| 546 | $_REQUEST['action'] = ''; |
| 547 | return; |
| 548 | } |
| 549 | check_admin_referer( 'bulk-media' ); |
| 550 | $ids = implode( '-', array_map( 'intval', $_REQUEST['media'] ) ); |
| 551 | $location = 'upload.php?mode=list&ids=' . $ids; |
| 552 | |
| 553 | if ( ! empty( $_REQUEST['paged'] ) ) { |
| 554 | $location = add_query_arg( 'paged', absint( $_REQUEST['paged'] ), $location ); |
| 555 | } |
| 556 | if ( ! empty( $_REQUEST['s'] ) ) { |
| 557 | $location = add_query_arg( 's', $_REQUEST['s'], $location ); |
| 558 | } |
| 559 | if ( ! empty( $_REQUEST['m'] ) ) { |
| 560 | $location = add_query_arg( 'm', $_REQUEST['m'], $location ); |
| 561 | } |
| 562 | |
| 563 | wp_redirect( admin_url( $location ) ); |
| 564 | exit(); |
| 565 | } |
| 566 | |
| 567 | public function add_media_columns( $columns ) { |
| 568 | $columns[ self::MEDIA_COLUMN ] = esc_html__( 'Compression', 'tiny-compress-images' ); |
| 569 | return $columns; |
| 570 | } |
| 571 | |
| 572 | public function render_media_column( $column, $id ) { |
| 573 | if ( self::MEDIA_COLUMN === $column ) { |
| 574 | $tiny_image = new Tiny_Image( $this->settings, $id ); |
| 575 | if ( $tiny_image->file_type_allowed() ) { |
| 576 | echo '<div class="tiny-ajax-container">'; |
| 577 | $this->render_compress_details( $tiny_image ); |
| 578 | echo '</div>'; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | public function show_media_info() { |
| 584 | global $post; |
| 585 | $tiny_image = new Tiny_Image( $this->settings, $post->ID ); |
| 586 | if ( $tiny_image->file_type_allowed() ) { |
| 587 | echo '<div class="misc-pub-section tiny-compress-images">'; |
| 588 | echo '<h4>'; |
| 589 | esc_html_e( 'JPEG and PNG optimization', 'tiny-compress-images' ); |
| 590 | echo '</h4>'; |
| 591 | echo '<div class="tiny-ajax-container">'; |
| 592 | $this->render_compress_details( $tiny_image ); |
| 593 | echo '</div>'; |
| 594 | echo '</div>'; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | private function render_compress_details( $tiny_image ) { |
| 599 | $in_progress = $tiny_image->filter_image_sizes( 'in_progress' ); |
| 600 | if ( count( $in_progress ) > 0 ) { |
| 601 | include( dirname( __FILE__ ) . '/views/compress-details-processing.php' ); |
| 602 | } else { |
| 603 | include( dirname( __FILE__ ) . '/views/compress-details.php' ); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | public function render_bulk_optimization_page() { |
| 608 | $stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->settings ); |
| 609 | $estimated_costs = Tiny_Compress::estimate_cost( |
| 610 | $stats['available-unoptimised-sizes'], |
| 611 | $this->settings->get_compression_count() |
| 612 | ); |
| 613 | $admin_colors = self::retrieve_admin_colors(); |
| 614 | |
| 615 | /* This makes sure that up to date information is retrieved from the API. */ |
| 616 | $this->settings->get_compressor()->get_status(); |
| 617 | |
| 618 | $active_tinify_sizes = $this->settings->get_active_tinify_sizes(); |
| 619 | $remaining_credits = $this->settings->get_remaining_credits(); |
| 620 | $is_on_free_plan = $this->settings->is_on_free_plan(); |
| 621 | $email_address = $this->settings->get_email_address(); |
| 622 | |
| 623 | include( dirname( __FILE__ ) . '/views/bulk-optimization.php' ); |
| 624 | } |
| 625 | |
| 626 | public function add_dashboard_widget() { |
| 627 | wp_enqueue_style( self::NAME . '_chart', |
| 628 | plugins_url( '/css/chart.css', __FILE__ ), |
| 629 | array(), self::version() |
| 630 | ); |
| 631 | |
| 632 | wp_enqueue_style( self::NAME . '_dashboard_widget', |
| 633 | plugins_url( '/css/dashboard-widget.css', __FILE__ ), |
| 634 | array(), self::version() |
| 635 | ); |
| 636 | |
| 637 | wp_register_script( self::NAME . '_dashboard_widget', |
| 638 | plugins_url( '/js/dashboard-widget.js', __FILE__ ), |
| 639 | array(), self::version(), true |
| 640 | ); |
| 641 | |
| 642 | /* This might be deduplicated with the admin script localization, but |
| 643 | the order of including scripts is sometimes different. So in that |
| 644 | case we need to make sure that the order of inclusion is correc.t */ |
| 645 | wp_localize_script( self::NAME . '_dashboard_widget', 'tinyCompressDashboard', array( |
| 646 | 'nonce' => wp_create_nonce( 'tiny-compress' ), |
| 647 | )); |
| 648 | |
| 649 | wp_enqueue_script( self::NAME . '_dashboard_widget' ); |
| 650 | |
| 651 | wp_add_dashboard_widget( |
| 652 | $this->get_prefixed_name( 'dashboard_widget' ), |
| 653 | esc_html__( 'Compress JPEG & PNG images', 'tiny-compress-images' ), |
| 654 | $this->get_method( 'add_widget_view' ) |
| 655 | ); |
| 656 | } |
| 657 | |
| 658 | function add_widget_view() { |
| 659 | $admin_colors = self::retrieve_admin_colors(); |
| 660 | include( dirname( __FILE__ ) . '/views/dashboard-widget.php' ); |
| 661 | } |
| 662 | |
| 663 | private static function retrieve_admin_colors() { |
| 664 | global $_wp_admin_css_colors; |
| 665 | $admin_colour_scheme = get_user_option( 'admin_color', get_current_user_id() ); |
| 666 | $admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // default |
| 667 | if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ] ) ) { |
| 668 | if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ]->colors ) ) { |
| 669 | $admin_colors = $_wp_admin_css_colors[ $admin_colour_scheme ]->colors; |
| 670 | } |
| 671 | } |
| 672 | if ( '#e5e5e5' == $admin_colors[0] && '#999' == $admin_colors[1] ) { |
| 673 | $admin_colors[0] = '#bbb'; |
| 674 | } |
| 675 | if ( '#5589aa' == $admin_colors[0] && '#cfdfe9' == $admin_colors[1] ) { |
| 676 | $admin_colors[1] = '#85aec5'; |
| 677 | } |
| 678 | if ( '#7c7976' == $admin_colors[0] && '#c6c6c6' == $admin_colors[1] ) { |
| 679 | $admin_colors[1] = '#adaba9'; |
| 680 | $admin_colors[2] = '#adaba9'; |
| 681 | } |
| 682 | if ( self::wp_version() > 3.7 ) { |
| 683 | if ( 'fresh' == $admin_colour_scheme ) { |
| 684 | $admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // better |
| 685 | } |
| 686 | } |
| 687 | return $admin_colors; |
| 688 | } |
| 689 | |
| 690 | function friendly_user_name() { |
| 691 | $user = wp_get_current_user(); |
| 692 | $name = ucfirst( empty( $user->first_name ) ? $user->display_name : $user->first_name ); |
| 693 | return $name; |
| 694 | } |
| 695 | } |
| 696 |