config
9 years ago
css
9 years ago
data
9 years ago
images
9 years ago
js
9 years ago
vendor
9 years ago
views
9 years ago
class-tiny-compress-client.php
9 years ago
class-tiny-compress-fopen.php
9 years ago
class-tiny-compress.php
9 years ago
class-tiny-exception.php
9 years ago
class-tiny-image-size.php
9 years ago
class-tiny-image.php
9 years ago
class-tiny-notices.php
9 years ago
class-tiny-php.php
9 years ago
class-tiny-plugin.php
9 years ago
class-tiny-settings.php
9 years ago
class-tiny-wp-base.php
9 years ago
class-tiny-plugin.php
452 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Tiny Compress Images - WordPress plugin. |
| 4 | * Copyright (C) 2015-2016 Voormedia 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 | |
| 21 | class Tiny_Plugin extends Tiny_WP_Base { |
| 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 | if ( is_null( self::$version ) ) { |
| 36 | $plugin_data = get_plugin_data( dirname( __FILE__ ) . '/../tiny-compress-images.php' ); |
| 37 | self::$version = $plugin_data['Version']; |
| 38 | } |
| 39 | return self::$version; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | public function __construct() { |
| 44 | parent::__construct(); |
| 45 | |
| 46 | $this->settings = new Tiny_Settings(); |
| 47 | } |
| 48 | |
| 49 | public function set_compressor( $compressor ) { |
| 50 | $this->settings->set_compressor( $compressor ); |
| 51 | } |
| 52 | |
| 53 | public function init() { |
| 54 | add_filter( 'jpeg_quality', |
| 55 | $this->get_static_method( 'jpeg_quality' ) |
| 56 | ); |
| 57 | |
| 58 | add_filter( 'wp_editor_set_quality', |
| 59 | $this->get_static_method( 'jpeg_quality' ) |
| 60 | ); |
| 61 | |
| 62 | add_filter( 'wp_generate_attachment_metadata', |
| 63 | $this->get_method( 'compress_on_upload' ), |
| 64 | 10, 2 |
| 65 | ); |
| 66 | |
| 67 | load_plugin_textdomain( self::NAME, false, |
| 68 | dirname( plugin_basename( __FILE__ ) ) . '/languages' |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | public function admin_init() { |
| 73 | add_action( 'admin_enqueue_scripts', |
| 74 | $this->get_method( 'enqueue_scripts' ) |
| 75 | ); |
| 76 | |
| 77 | add_action( 'admin_action_tiny_bulk_action', |
| 78 | $this->get_method( 'media_library_bulk_action' ) |
| 79 | ); |
| 80 | |
| 81 | add_action( 'admin_action_-1', |
| 82 | $this->get_method( 'media_library_bulk_action' ) |
| 83 | ); |
| 84 | |
| 85 | add_filter( 'manage_media_columns', |
| 86 | $this->get_method( 'add_media_columns' ) |
| 87 | ); |
| 88 | |
| 89 | add_action( 'manage_media_custom_column', |
| 90 | $this->get_method( 'render_media_column' ), |
| 91 | 10, 2 |
| 92 | ); |
| 93 | |
| 94 | add_action( 'attachment_submitbox_misc_actions', |
| 95 | $this->get_method( 'show_media_info' ) |
| 96 | ); |
| 97 | |
| 98 | add_action( 'wp_ajax_tiny_compress_image_from_library', |
| 99 | $this->get_method( 'compress_image_from_library' ) |
| 100 | ); |
| 101 | |
| 102 | add_action( 'wp_ajax_tiny_compress_image_for_bulk', |
| 103 | $this->get_method( 'compress_image_for_bulk' ) |
| 104 | ); |
| 105 | |
| 106 | add_action( 'wp_ajax_tiny_get_optimization_statistics', |
| 107 | $this->get_method( 'ajax_optimization_statistics' ) |
| 108 | ); |
| 109 | |
| 110 | $plugin = plugin_basename( |
| 111 | dirname( dirname( __FILE__ ) ) . '/tiny-compress-images.php' |
| 112 | ); |
| 113 | |
| 114 | add_filter( "plugin_action_links_$plugin", |
| 115 | $this->get_method( 'add_plugin_links' ) |
| 116 | ); |
| 117 | |
| 118 | add_thickbox(); |
| 119 | } |
| 120 | |
| 121 | public function admin_menu() { |
| 122 | add_media_page( |
| 123 | __( 'Bulk Optimization', 'tiny-compress-images' ), |
| 124 | esc_html__( 'Bulk Optimization', 'tiny-compress-images' ), |
| 125 | 'upload_files', |
| 126 | 'tiny-bulk-optimization', |
| 127 | $this->get_method( 'render_bulk_optimization_page' ) |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | public function add_plugin_links( $current_links ) { |
| 132 | $additional = array( |
| 133 | 'settings' => sprintf( |
| 134 | '<a href="options-media.php#%s">%s</a>', |
| 135 | self::NAME, |
| 136 | esc_html__( 'Settings', 'tiny-compress-images' ) |
| 137 | ), |
| 138 | 'bulk' => sprintf( |
| 139 | '<a href="upload.php?page=tiny-bulk-optimization">%s</a>', |
| 140 | esc_html__( 'Bulk Optimization', 'tiny-compress-images' ) |
| 141 | ), |
| 142 | ); |
| 143 | return array_merge( $additional, $current_links ); |
| 144 | } |
| 145 | |
| 146 | public function enqueue_scripts( $hook ) { |
| 147 | wp_enqueue_style( self::NAME .'_admin', plugins_url( '/css/admin.css', __FILE__ ), |
| 148 | array(), self::version() ); |
| 149 | wp_register_script( self::NAME .'_admin', plugins_url( '/js/admin.js', __FILE__ ), |
| 150 | array(), self::version(), true ); |
| 151 | |
| 152 | // WordPress < 3.3 does not handle multidimensional arrays |
| 153 | wp_localize_script( self::NAME .'_admin', 'tinyCompress', array( |
| 154 | 'nonce' => wp_create_nonce( 'tiny-compress' ), |
| 155 | 'wpVersion' => self::wp_version(), |
| 156 | 'pluginVersion' => self::version(), |
| 157 | 'L10nAllDone' => __( 'All images are processed', 'tiny-compress-images' ), |
| 158 | 'L10nNoActionTaken' => __( 'No action taken', 'tiny-compress-images' ), |
| 159 | 'L10nBulkAction' => __( 'Compress Images', 'tiny-compress-images' ), |
| 160 | 'L10nCancelled' => __( 'Cancelled', 'tiny-compress-images' ), |
| 161 | 'L10nCompressing' => __( 'Compressing', 'tiny-compress-images' ), |
| 162 | 'L10nCompressed' => __( 'compressed', 'tiny-compress-images' ), |
| 163 | 'L10nFile' => __( 'File', 'tiny-compress-images' ), |
| 164 | 'L10nSizesOptimized' => __( 'Sizes optimized', 'tiny-compress-images' ), |
| 165 | 'L10nInitialSize' => __( 'Initial size', 'tiny-compress-images' ), |
| 166 | 'L10nCurrentSize' => __( 'Current size', 'tiny-compress-images' ), |
| 167 | 'L10nSavings' => __( 'Savings', 'tiny-compress-images' ), |
| 168 | 'L10nStatus' => __( 'Status', 'tiny-compress-images' ), |
| 169 | 'L10nShowMoreDetails' => __( 'Show more details', 'tiny-compress-images' ), |
| 170 | 'L10nError' => __( 'Error', 'tiny-compress-images' ), |
| 171 | 'L10nLatestError' => __( 'Latest error', 'tiny-compress-images' ), |
| 172 | 'L10nInternalError' => __( 'Internal error', 'tiny-compress-images' ), |
| 173 | 'L10nOutOf' => __( 'out of', 'tiny-compress-images' ), |
| 174 | 'L10nWaiting' => __( 'Waiting', 'tiny-compress-images' ), |
| 175 | )); |
| 176 | |
| 177 | wp_enqueue_script( self::NAME .'_admin' ); |
| 178 | |
| 179 | if ( 'media_page_tiny-bulk-optimization' == $hook ) { |
| 180 | wp_enqueue_style( |
| 181 | self::NAME . '_tiny_bulk_optimization', |
| 182 | plugins_url( '/css/bulk-optimization.css', __FILE__ ), |
| 183 | array(), self::version() |
| 184 | ); |
| 185 | |
| 186 | wp_register_script( |
| 187 | self::NAME . '_tiny_bulk_optimization', |
| 188 | plugins_url( '/js/bulk-optimization.js', __FILE__ ), |
| 189 | array(), self::version(), true |
| 190 | ); |
| 191 | |
| 192 | wp_enqueue_script( self::NAME .'_tiny_bulk_optimization' ); |
| 193 | } |
| 194 | |
| 195 | } |
| 196 | |
| 197 | public function compress_on_upload( $metadata, $attachment_id ) { |
| 198 | if ( ! empty( $metadata ) ) { |
| 199 | $tiny_image = new Tiny_Image( $attachment_id, $metadata ); |
| 200 | $result = $tiny_image->compress( $this->settings ); |
| 201 | return $tiny_image->get_wp_metadata(); |
| 202 | } else { |
| 203 | return $metadata; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | public function compress_image_from_library() { |
| 208 | if ( ! $this->check_ajax_referer() ) { |
| 209 | exit(); |
| 210 | } |
| 211 | if ( ! current_user_can( 'upload_files' ) ) { |
| 212 | $message = esc_html__( |
| 213 | "You don't have permission to upload files.", |
| 214 | 'tiny-compress-images' |
| 215 | ); |
| 216 | echo $message; |
| 217 | exit(); |
| 218 | } |
| 219 | if ( empty( $_POST['id'] ) ) { |
| 220 | $message = esc_html__( |
| 221 | 'Not a valid media file.', |
| 222 | 'tiny-compress-images' |
| 223 | ); |
| 224 | echo $message; |
| 225 | exit(); |
| 226 | } |
| 227 | $id = intval( $_POST['id'] ); |
| 228 | $metadata = wp_get_attachment_metadata( $id ); |
| 229 | if ( ! is_array( $metadata ) ) { |
| 230 | $message = esc_html__( |
| 231 | 'Could not find metadata of media file.', |
| 232 | 'tiny-compress-images' |
| 233 | ); |
| 234 | echo $message; |
| 235 | exit; |
| 236 | } |
| 237 | |
| 238 | $tiny_image = new Tiny_Image( $id, $metadata ); |
| 239 | $result = $tiny_image->compress( $this->settings ); |
| 240 | |
| 241 | // The wp_update_attachment_metadata call is thrown because the |
| 242 | // dimensions of the original image can change. This will then |
| 243 | // trigger other plugins and can result in unexpected behaviour and |
| 244 | // further changes to the image. This may require another approach. |
| 245 | wp_update_attachment_metadata( $id, $tiny_image->get_wp_metadata() ); |
| 246 | |
| 247 | echo $this->render_compress_details( $tiny_image ); |
| 248 | |
| 249 | exit(); |
| 250 | } |
| 251 | |
| 252 | public function compress_image_for_bulk() { |
| 253 | if ( ! $this->check_ajax_referer() ) { |
| 254 | exit(); |
| 255 | } |
| 256 | if ( ! current_user_can( 'upload_files' ) ) { |
| 257 | $message = esc_html__( |
| 258 | "You don't have permission to upload files.", |
| 259 | 'tiny-compress-images' |
| 260 | ); |
| 261 | echo json_encode( array( 'error' => $message ) ); |
| 262 | exit(); |
| 263 | } |
| 264 | if ( empty( $_POST['id'] ) ) { |
| 265 | $message = esc_html__( |
| 266 | 'Not a valid media file.', |
| 267 | 'tiny-compress-images' |
| 268 | ); |
| 269 | echo json_encode( array( 'error' => $message ) ); |
| 270 | exit(); |
| 271 | } |
| 272 | $id = intval( $_POST['id'] ); |
| 273 | $metadata = wp_get_attachment_metadata( $id ); |
| 274 | if ( ! is_array( $metadata ) ) { |
| 275 | $message = esc_html__( |
| 276 | 'Could not find metadata of media file.', |
| 277 | 'tiny-compress-images' |
| 278 | ); |
| 279 | echo json_encode( array( 'error' => $message ) ); |
| 280 | exit; |
| 281 | } |
| 282 | |
| 283 | $tiny_image_before = new Tiny_Image( $id, $metadata ); |
| 284 | $image_statistics_before = $tiny_image_before->get_statistics(); |
| 285 | $size_before = $image_statistics_before['optimized_total_size']; |
| 286 | |
| 287 | $tiny_image = new Tiny_Image( $id, $metadata ); |
| 288 | $result = $tiny_image->compress( $this->settings ); |
| 289 | $image_statistics = $tiny_image->get_statistics(); |
| 290 | wp_update_attachment_metadata( $id, $tiny_image->get_wp_metadata() ); |
| 291 | |
| 292 | $current_library_size = intval( $_POST['current_size'] ); |
| 293 | $size_after = $image_statistics['optimized_total_size']; |
| 294 | $new_library_size = $current_library_size + $size_after - $size_before; |
| 295 | |
| 296 | $result['message'] = $tiny_image->get_latest_error(); |
| 297 | $result['image_sizes_optimized'] = $image_statistics['image_sizes_optimized']; |
| 298 | |
| 299 | $result['initial_total_size'] = size_format( |
| 300 | $image_statistics['initial_total_size'], 1 |
| 301 | ); |
| 302 | |
| 303 | $result['optimized_total_size'] = size_format( |
| 304 | $image_statistics['optimized_total_size'], 1 |
| 305 | ); |
| 306 | |
| 307 | $result['savings'] = $tiny_image->get_savings( $image_statistics ); |
| 308 | $result['status'] = $this->settings->get_status(); |
| 309 | $result['thumbnail'] = wp_get_attachment_image( |
| 310 | $id, array( '30', '30' ), true, array( |
| 311 | 'class' => 'pinkynail', |
| 312 | 'alt' => '', |
| 313 | ) |
| 314 | ); |
| 315 | $result['size_change'] = $size_after - $size_before; |
| 316 | $result['human_readable_library_size'] = size_format( $new_library_size, 2 ); |
| 317 | |
| 318 | echo json_encode( $result ); |
| 319 | |
| 320 | exit(); |
| 321 | } |
| 322 | |
| 323 | public function ajax_optimization_statistics() { |
| 324 | if ( ! $this->check_ajax_referer() ) { |
| 325 | exit(); |
| 326 | } |
| 327 | $stats = Tiny_Image::get_optimization_statistics(); |
| 328 | echo json_encode( $stats ); |
| 329 | exit(); |
| 330 | } |
| 331 | |
| 332 | public function media_library_bulk_action() { |
| 333 | |
| 334 | if ( empty( $_REQUEST['action'] ) || ( |
| 335 | 'tiny_bulk_action' != $_REQUEST['action'] && |
| 336 | 'tiny_bulk_action' != $_REQUEST['action2'] ) ) { |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | if ( empty( $_REQUEST['media'] ) || ( ! $_REQUEST['media'] ) ) { |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | check_admin_referer( 'bulk-media' ); |
| 345 | $ids = implode( '-', array_map( 'intval', $_REQUEST['media'] ) ); |
| 346 | wp_redirect(add_query_arg( |
| 347 | '_wpnonce', |
| 348 | wp_create_nonce( 'tiny-bulk-optimization' ), |
| 349 | admin_url( "upload.php?page=tiny-bulk-optimization&ids=$ids" ) |
| 350 | )); |
| 351 | exit(); |
| 352 | } |
| 353 | |
| 354 | public function add_media_columns( $columns ) { |
| 355 | $columns[ self::MEDIA_COLUMN ] = esc_html__( 'Compression', 'tiny-compress-images' ); |
| 356 | return $columns; |
| 357 | } |
| 358 | |
| 359 | public function render_media_column( $column, $id ) { |
| 360 | if ( self::MEDIA_COLUMN === $column ) { |
| 361 | $tiny_image = new Tiny_Image( $id ); |
| 362 | if ( $tiny_image->file_type_allowed() ) { |
| 363 | echo '<div class="tiny-ajax-container">'; |
| 364 | $this->render_compress_details( $tiny_image ); |
| 365 | echo '</div>'; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | public function show_media_info() { |
| 371 | global $post; |
| 372 | $tiny_image = new Tiny_Image( $post->ID ); |
| 373 | if ( $tiny_image->file_type_allowed() ) { |
| 374 | echo '<div class="misc-pub-section tiny-compress-images">'; |
| 375 | echo '<h4>'; |
| 376 | esc_html_e( 'JPEG and PNG optimization', 'tiny-compress-images' ); |
| 377 | echo '</h4>'; |
| 378 | echo '<div class="tiny-ajax-container">'; |
| 379 | $this->render_compress_details( $tiny_image ); |
| 380 | echo '</div>'; |
| 381 | echo '</div>'; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | private function render_compress_details( $tiny_image ) { |
| 386 | $in_progress = $tiny_image->filter_image_sizes( 'in_progress' ); |
| 387 | if ( count( $in_progress ) > 0 ) { |
| 388 | include( dirname( __FILE__ ) . '/views/compress-details-processing.php' ); |
| 389 | } else { |
| 390 | include( dirname( __FILE__ ) . '/views/compress-details.php' ); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | public function render_bulk_optimization_page() { |
| 395 | $stats = Tiny_Image::get_optimization_statistics(); |
| 396 | $estimated_costs = Tiny_Compress::estimate_cost( |
| 397 | $stats['available-unoptimised-sizes'], |
| 398 | $this->settings->get_compression_count() |
| 399 | ); |
| 400 | $admin_colors = self::retrieve_admin_colors(); |
| 401 | |
| 402 | $active_tinify_sizes = $this->settings->get_active_tinify_sizes(); |
| 403 | |
| 404 | $auto_start_bulk = isset( $_REQUEST['ids'] ); |
| 405 | |
| 406 | include( dirname( __FILE__ ) . '/views/bulk-optimization.php' ); |
| 407 | } |
| 408 | |
| 409 | private static function retrieve_admin_colors() { |
| 410 | global $_wp_admin_css_colors; |
| 411 | $admin_colour_scheme = get_user_option( 'admin_color', get_current_user_id() ); |
| 412 | $admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // default |
| 413 | if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ] ) ) { |
| 414 | if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ]->colors ) ) { |
| 415 | $admin_colors = $_wp_admin_css_colors[ $admin_colour_scheme ]->colors; |
| 416 | } |
| 417 | } |
| 418 | if ( '#e5e5e5' == $admin_colors[0] && '#999' == $admin_colors[1] ) { |
| 419 | $admin_colors[0] = '#bbb'; |
| 420 | } |
| 421 | if ( '#5589aa' == $admin_colors[0] && '#cfdfe9' == $admin_colors[1] ) { |
| 422 | $admin_colors[1] = '#85aec5'; |
| 423 | } |
| 424 | if ( '#7c7976' == $admin_colors[0] && '#c6c6c6' == $admin_colors[1] ) { |
| 425 | $admin_colors[1] = '#adaba9'; |
| 426 | $admin_colors[2] = '#adaba9'; |
| 427 | } |
| 428 | if ( self::wp_version() > 3.7 ) { |
| 429 | if ( 'fresh' == $admin_colour_scheme ) { |
| 430 | $admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // better |
| 431 | } |
| 432 | } |
| 433 | return $admin_colors; |
| 434 | } |
| 435 | |
| 436 | private function get_ids_to_compress() { |
| 437 | if ( empty( $_REQUEST['ids'] ) ) { |
| 438 | return array(); |
| 439 | } |
| 440 | |
| 441 | $ids = implode( ',', array_map( 'intval', explode( '-', $_REQUEST['ids'] ) ) ); |
| 442 | $condition = "AND ID IN($ids)"; |
| 443 | |
| 444 | global $wpdb; |
| 445 | return $wpdb->get_results( |
| 446 | "SELECT ID, post_title FROM $wpdb->posts |
| 447 | WHERE post_type = 'attachment' $condition |
| 448 | AND (post_mime_type = 'image/jpeg' OR post_mime_type = 'image/png') |
| 449 | ORDER BY ID DESC", ARRAY_A); |
| 450 | } |
| 451 | } |
| 452 |