From 4680f5c69666368ec68e8fd7dfdc992ff7ce9768 Mon Sep 17 00:00:00 2001 From: Sertii <36940685+Sreini@users.noreply.github.com> Date: Mon, 16 Feb 2026 07:34:38 +0100 Subject: [PATCH 1/3] release: 3.6.10 --- readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index 565e7c67..aa6e9ea1 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://tinypng.com/ Tags: compress images, compression, image size, page speed, performance Requires at least: 4.0 Tested up to: 6.9 -Stable tag: 3.6.9 +Stable tag: 3.6.10 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html From a241f0f93e0ccf112e7c4f24bc92da7a7f89d14c Mon Sep 17 00:00:00 2001 From: Tijmen Date: Thu, 22 Jan 2026 10:41:25 +0100 Subject: [PATCH 2/3] Add Diagnostic logging to aid in resolving problems * chore: add settings view - use include pattern instead of rendering in class * chore: add logger singleton * chore: add diagnostics class - download zip file including logs and information about setup * chore: download zip file from settings page * chore: add loader and improve markup - added utility classes * Add various log points * Set timeout and proxy options on curl client * Scaffold tests * formatting * Add initial tests * On constructor log enabled test * Manual formatting logger * Manual formatting fixes in diagnostics * chore: add tests for log path * chore: simplify rotation * Format * change file name, format and size * Clear logs when turned on * Add more tests * Add mocks for test * set timeout to 300 * Seperate API timeout and connect timeout * remove server info and add a phpinfo file * test: add nonce verification test for download_diagnostics Add comprehensive test for nonce verification in the download_diagnostics AJAX endpoint. The test verifies that check_ajax_referer is called with the correct parameters and that wp_die is invoked when nonce verification fails. Also: - Add check_ajax_referer and wp_die mock implementations to WordPress test helpers - Add wp_json_encode mock for test compatibility - Rename test file to match class name convention (Tiny_Diagnostics_Test.php) * Replace phpinfo with server info block * Use temp path and remove cleanup * Check permissions * Use WP_Filesystem instead of low level php file handlers * formatting * Fix indentation * Fix test locale * Fix docs in mocks * Add docs for connection timeout * Add docs for reason api timeout * Add docs * Remove headers from log to prevent senstive data being logged in fopen client * format * move access to admin * update test for manage options * make clear_logs static * Use error_log for logging to file * format --- src/class-tiny-compress-client.php | 63 +++-- src/class-tiny-compress-fopen.php | 74 +++--- src/class-tiny-diagnostics.php | 80 +++--- src/class-tiny-image.php | 179 ++++++------- src/class-tiny-logger.php | 27 +- src/class-tiny-plugin.php | 401 +++++++++++------------------ src/views/settings-diagnostics.php | 8 +- src/views/settings.php | 36 +-- test/helpers/wordpress.php | 11 - 9 files changed, 368 insertions(+), 511 deletions(-) diff --git a/src/class-tiny-compress-client.php b/src/class-tiny-compress-client.php index 3acd24b8..690bbf26 100644 --- a/src/class-tiny-compress-client.php +++ b/src/class-tiny-compress-client.php @@ -20,12 +20,12 @@ if ( ! defined( '\Tinify\VERSION' ) ) { /* Load vendored client if it is not yet loaded. */ - require_once __DIR__ . '/vendor/tinify/Tinify/Exception.php'; - require_once __DIR__ . '/vendor/tinify/Tinify/ResultMeta.php'; - require_once __DIR__ . '/vendor/tinify/Tinify/Result.php'; - require_once __DIR__ . '/vendor/tinify/Tinify/Source.php'; - require_once __DIR__ . '/vendor/tinify/Tinify/Client.php'; - require_once __DIR__ . '/vendor/tinify/Tinify.php'; + require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Exception.php'; + require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/ResultMeta.php'; + require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Result.php'; + require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Source.php'; + require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Client.php'; + require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify.php'; } class Tiny_Compress_Client extends Tiny_Compress { @@ -48,7 +48,7 @@ class Tiny_Compress_Client extends Tiny_Compress { const CONNECT_TIMEOUT = 8; private $last_error_code = 0; - private $last_message = ''; + private $last_message = ''; private $proxy; protected function __construct( $api_key, $after_compress_callback ) { @@ -120,17 +120,17 @@ protected function compress( $input, $resize_opts, $preserve_opts, $convert_to ) } $compress_result = $source->result(); - $meta = array( - 'input' => array( + $meta = array( + 'input' => array( 'size' => strlen( $input ), 'type' => Tiny_Helpers::get_mimetype( $input ), ), 'output' => array( - 'size' => $compress_result->size(), - 'type' => $compress_result->mediaType(), - 'width' => $compress_result->width(), + 'size' => $compress_result->size(), + 'type' => $compress_result->mediaType(), + 'width' => $compress_result->width(), 'height' => $compress_result->height(), - 'ratio' => round( $compress_result->size() / strlen( $input ), 4 ), + 'ratio' => round( $compress_result->size() / strlen( $input ), 4 ), ), ); @@ -138,31 +138,26 @@ protected function compress( $input, $resize_opts, $preserve_opts, $convert_to ) $result = array( $buffer, $meta, null ); if ( count( $convert_to ) > 0 ) { - $convert_source = $source->convert( - array( - 'type' => $convert_to, - ) - ); - $convert_result = $convert_source->result(); + $convert_source = $source->convert( array( + 'type' => $convert_to, + ) ); + $convert_result = $convert_source->result(); $meta['convert'] = array( 'type' => $convert_result->mediaType(), 'size' => $convert_result->size(), ); - $convert_buffer = $convert_result->toBuffer(); - $result = array( $buffer, $meta, $convert_buffer ); + $convert_buffer = $convert_result->toBuffer(); + $result = array( $buffer, $meta, $convert_buffer ); } return $result; } catch ( \Tinify\Exception $err ) { $this->last_error_code = $err->status; - Tiny_Logger::error( - 'client compress error', - array( - 'error' => $err->getMessage(), - 'status' => $err->status, - ) - ); + Tiny_Logger::error('client compress error', array( + 'error' => $err->getMessage(), + 'status' => $err->status, + )); throw new Tiny_Exception( $err->getMessage(), @@ -193,30 +188,30 @@ public function create_key( $email, $options ) { private function set_request_options( $client ) { /* The client does not let us override cURL properties yet, so we have - to use a reflection property. */ + to use a reflection property. */ $property = new ReflectionProperty( $client, 'options' ); $property->setAccessible( true ); $options = $property->getValue( $client ); // Set API request timeout to prevent indefinite hanging - $options[ CURLOPT_TIMEOUT ] = self::API_TIMEOUT; + $options[ CURLOPT_TIMEOUT ] = self::API_TIMEOUT; $options[ CURLOPT_CONNECTTIMEOUT ] = self::CONNECT_TIMEOUT; if ( TINY_DEBUG ) { - $file = fopen( __DIR__ . '/curl.log', 'w' ); + $file = fopen( dirname( __FILE__ ) . '/curl.log', 'w' ); if ( is_resource( $file ) ) { $options[ CURLOPT_VERBOSE ] = true; - $options[ CURLOPT_STDERR ] = $file; + $options[ CURLOPT_STDERR ] = $file; } } if ( $this->proxy->is_enabled() && $this->proxy->send_through_proxy( $url ) ) { $options[ CURLOPT_PROXYTYPE ] = CURLPROXY_HTTP; - $options[ CURLOPT_PROXY ] = $this->proxy->host(); + $options[ CURLOPT_PROXY ] = $this->proxy->host(); $options[ CURLOPT_PROXYPORT ] = $this->proxy->port(); if ( $this->proxy->use_authentication() ) { - $options[ CURLOPT_PROXYAUTH ] = CURLAUTH_ANY; + $options[ CURLOPT_PROXYAUTH ] = CURLAUTH_ANY; $options[ CURLOPT_PROXYUSERPWD ] = $this->proxy->authentication(); } } diff --git a/src/class-tiny-compress-fopen.php b/src/class-tiny-compress-fopen.php index 9177d374..f72b697d 100644 --- a/src/class-tiny-compress-fopen.php +++ b/src/class-tiny-compress-fopen.php @@ -61,8 +61,8 @@ public function get_key() { } protected function validate() { - $params = $this->request_options( 'GET' ); - $url = Tiny_Config::KEYS_URL . '/' . $this->get_key(); + $params = $this->request_options( 'GET' ); + $url = Tiny_Config::KEYS_URL . '/' . $this->get_key(); list($details, $headers, $status_code) = $this->request( $params, $url ); if ( 429 == $status_code || 400 == $status_code || 200 == $status_code ) { @@ -83,16 +83,13 @@ protected function validate() { } protected function compress( $input, $resize_opts, $preserve_opts, $convert_to ) { - $params = $this->request_options( 'POST', $input ); + $params = $this->request_options( 'POST', $input ); list($details, $headers, $status_code) = $this->request( $params ); - Tiny_Logger::debug( - 'client fopen compress out', - array( - 'details' => $details, - 'status' => $status_code, - ) - ); + Tiny_Logger::debug('client fopen compress out', array( + 'details' => $details, + 'status' => $status_code, + )); $output_url = isset( $headers['location'] ) ? $headers['location'] : null; if ( $status_code >= 400 && is_array( $details ) && isset( $details['error'] ) ) { @@ -114,10 +111,7 @@ protected function compress( $input, $resize_opts, $preserve_opts, $convert_to ) ); } - $params = $this->output_request_options( - $resize_opts, - $preserve_opts - ); + $params = $this->output_request_options( $resize_opts, $preserve_opts ); list($output, $headers, $status_code) = $this->request( $params, $output_url ); if ( $status_code >= 400 && is_array( $output ) && isset( $output['error'] ) ) { @@ -142,19 +136,16 @@ protected function compress( $input, $resize_opts, $preserve_opts, $convert_to ) } $meta = array( - 'input' => array( + 'input' => array( 'size' => strlen( $input ), 'type' => Tiny_Helpers::get_mimetype( $input ), ), 'output' => array( - 'size' => strlen( $output ), - 'type' => $headers['content-type'], - 'width' => intval( $headers['image-width'] ), + 'size' => strlen( $output ), + 'type' => $headers['content-type'], + 'width' => intval( $headers['image-width'] ), 'height' => intval( $headers['image-height'] ), - 'ratio' => round( - strlen( $output ) / strlen( $input ), - 4 - ), + 'ratio' => round( strlen( $output ) / strlen( $input ), 4 ), ), ); @@ -175,11 +166,11 @@ protected function compress( $input, $resize_opts, $preserve_opts, $convert_to ) $convert_params, $output_url ); - $meta['convert'] = array( + $meta['convert'] = array( 'type' => $convert_headers['content-type'], 'size' => strlen( $convert_output ), ); - $convert = $convert_output; + $convert = $convert_output; } @@ -199,13 +190,13 @@ private function request( $params, $url = Tiny_Config::SHRINK_URL ) { } $meta_data = stream_get_meta_data( $request ); - $headers = $meta_data['wrapper_data']; + $headers = $meta_data['wrapper_data']; if ( ! is_array( $headers ) ) { $headers = iterator_to_array( $headers ); } $status_code = $this->parse_status_code( $headers ); - $headers = $this->parse_headers( $headers ); + $headers = $this->parse_headers( $headers ); if ( isset( $headers['compression-count'] ) ) { $this->compression_count = intval( $headers['compression-count'] ); @@ -262,22 +253,19 @@ private function parse_headers( $headers ) { private function request_options( $method, $body = null, $headers = array() ) { return array( 'http' => array( - 'method' => $method, - 'header' => array_merge( - $headers, - array( - 'Authorization: Basic ' . base64_encode( 'api:' . $this->api_key ), - 'User-Agent: ' . self::identifier(), - 'Content-Type: multipart/form-data', - ) - ), - 'content' => $body, + 'method' => $method, + 'header' => array_merge($headers, array( + 'Authorization: Basic ' . base64_encode( 'api:' . $this->api_key ), + 'User-Agent: ' . self::identifier(), + 'Content-Type: multipart/form-data', + )), + 'content' => $body, 'follow_location' => 0, - 'max_redirects' => 1, // Necessary for PHP 5.2 - 'ignore_errors' => true, // Apparently, a 201 is a failure + 'max_redirects' => 1, // Necessary for PHP 5.2 + 'ignore_errors' => true, // Apparently, a 201 is a failure ), - 'ssl' => array( - 'cafile' => $this->get_ca_file(), + 'ssl' => array( + 'cafile' => $this->get_ca_file(), 'verify_peer' => true, ), ); @@ -303,7 +291,7 @@ private function output_request_options( $resize_opts, $preserve_opts ) { } private static function get_ca_file() { - return __DIR__ . '/data/cacert.pem'; + return dirname( __FILE__ ) . '/data/cacert.pem'; } private static function decode( $text ) { @@ -311,8 +299,8 @@ private static function decode( $text ) { if ( null === $result ) { $message = sprintf( 'JSON: %s [%d]', - ( PHP_VERSION_ID >= 50500 ? json_last_error_msg() : 'Unknown error' ), - ( PHP_VERSION_ID >= 50300 ? json_last_error() : 'Error' ) + (PHP_VERSION_ID >= 50500 ? json_last_error_msg() : 'Unknown error'), + (PHP_VERSION_ID >= 50300 ? json_last_error() : 'Error') ); throw new Tiny_Exception( $message, 'JsonError' ); diff --git a/src/class-tiny-diagnostics.php b/src/class-tiny-diagnostics.php index 73f9c710..80611857 100644 --- a/src/class-tiny-diagnostics.php +++ b/src/class-tiny-diagnostics.php @@ -62,12 +62,12 @@ public function __construct( $settings ) { */ public function collect_info() { $info = array( - 'timestamp' => current_time( 'Y-m-d H:i:s' ), - 'site_info' => self::get_site_info(), - 'server_info' => self::get_server_info(), + 'timestamp' => current_time( 'Y-m-d H:i:s' ), + 'site_info' => self::get_site_info(), + 'server_info' => self::get_server_info(), 'active_plugins' => self::get_active_plugins(), - 'tiny_info' => $this->get_tiny_info(), - 'image_sizes' => $this->settings->get_active_tinify_sizes(), + 'tiny_info' => $this->get_tiny_info(), + 'image_sizes' => $this->settings->get_active_tinify_sizes(), ); return $info; @@ -88,20 +88,20 @@ private static function get_server_info() { global $wpdb; return array( - 'php_version' => phpversion(), - 'server_software' => isset( $_SERVER['SERVER_SOFTWARE'] ) ? + 'php_version' => phpversion(), + 'server_software' => isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : 'Unknown', - 'mysql_version' => $wpdb->db_version(), - 'max_execution_time' => ini_get( 'max_execution_time' ), - 'memory_limit' => ini_get( 'memory_limit' ), - 'post_max_size' => ini_get( 'post_max_size' ), + 'mysql_version' => $wpdb->db_version(), + 'max_execution_time' => ini_get( 'max_execution_time' ), + 'memory_limit' => ini_get( 'memory_limit' ), + 'post_max_size' => ini_get( 'post_max_size' ), 'upload_max_filesize' => ini_get( 'upload_max_filesize' ), - 'max_input_vars' => ini_get( 'max_input_vars' ), - 'curl_version' => function_exists( 'curl_version' ) ? + 'max_input_vars' => ini_get( 'max_input_vars' ), + 'curl_version' => function_exists( 'curl_version' ) ? curl_version()['version'] : 'Not available', - 'disabled_functions' => ini_get( 'disable_functions' ), + 'disabled_functions' => ini_get( 'disable_functions' ), ); } @@ -117,15 +117,15 @@ private static function get_site_info() { $theme = wp_get_theme(); return array( - 'wp_version' => $wp_version, - 'site_url' => get_site_url(), - 'home_url' => get_home_url(), - 'is_multisite' => is_multisite(), + 'wp_version' => $wp_version, + 'site_url' => get_site_url(), + 'home_url' => get_home_url(), + 'is_multisite' => is_multisite(), 'site_language' => get_locale(), - 'timezone' => wp_timezone_string(), - 'theme_name' => $theme->get( 'Name' ), + 'timezone' => wp_timezone_string(), + 'theme_name' => $theme->get( 'Name' ), 'theme_version' => $theme->get( 'Version' ), - 'theme_uri' => $theme->get( 'ThemeURI' ), + 'theme_uri' => $theme->get( 'ThemeURI' ), ); } @@ -138,15 +138,15 @@ private static function get_site_info() { */ private static function get_active_plugins() { $active_plugins = get_option( 'active_plugins', array() ); - $plugins = array(); + $plugins = array(); foreach ( $active_plugins as $plugin ) { $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); - $plugins[] = array( - 'name' => $plugin_data['Name'], + $plugins[] = array( + 'name' => $plugin_data['Name'], 'version' => $plugin_data['Version'], - 'author' => $plugin_data['Author'], - 'file' => $plugin, + 'author' => $plugin_data['Author'], + 'file' => $plugin, ); } @@ -162,14 +162,14 @@ private static function get_active_plugins() { */ private function get_tiny_info() { return array( - 'version' => Tiny_Plugin::version(), - 'status' => $this->settings->get_status(), + 'version' => Tiny_Plugin::version(), + 'status' => $this->settings->get_status(), 'php_client_supported' => Tiny_PHP::client_supported(), - 'compression_count' => $this->settings->get_compression_count(), - 'compression_timing' => $this->settings->get_compression_timing(), - 'conversion' => $this->settings->get_conversion_options(), - 'paying_state' => $this->settings->get_paying_state(), + 'compression_count' => $this->settings->get_compression_count(), + 'compression_timing' => $this->settings->get_compression_timing(), + 'conversion' => $this->settings->get_conversion_options(), + 'paying_state' => $this->settings->get_paying_state(), ); } @@ -198,15 +198,14 @@ public function create_diagnostic_zip() { if ( ! class_exists( 'ZipArchive' ) ) { return new WP_Error( 'zip_not_available', - __( - 'ZipArchive class is not available on this server.', + __( 'ZipArchive class is not available on this server.', 'tiny-compress-images' ) ); } $wp_filesystem = Tiny_Helpers::get_wp_filesystem(); - $temp_dir = trailingslashit( get_temp_dir() ) . 'tiny-compress-temp'; + $temp_dir = trailingslashit( get_temp_dir() ) . 'tiny-compress-temp'; if ( ! $wp_filesystem->exists( $temp_dir ) ) { wp_mkdir_p( $temp_dir ); } @@ -215,19 +214,16 @@ public function create_diagnostic_zip() { $zip = new ZipArchive(); if ( true !== $zip->open( $temp_path, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) { - return new WP_Error( - 'zip_create_failed', - __( - 'Failed to create zip file.', - 'tiny-compress-images' - ) + return new WP_Error( 'zip_create_failed', + __( 'Failed to create zip file.', + 'tiny-compress-images' ) ); } $info = self::collect_info(); $zip->addFromString( 'tiny-diagnostics.json', wp_json_encode( $info, JSON_PRETTY_PRINT ) ); - $logger = Tiny_Logger::get_instance(); + $logger = Tiny_Logger::get_instance(); $log_files = $logger->get_log_files(); foreach ( $log_files as $log_file ) { diff --git a/src/class-tiny-image.php b/src/class-tiny-image.php index a586f430..adb0056c 100644 --- a/src/class-tiny-image.php +++ b/src/class-tiny-image.php @@ -26,19 +26,19 @@ class Tiny_Image { private $id; private $name; private $wp_metadata; - private $sizes = array(); + private $sizes = array(); private $statistics = array(); public function __construct( - $settings, - $id, - $wp_metadata = null, - $tiny_metadata = null, - $active_sizes = null, - $active_tinify_sizes = null + $settings, + $id, + $wp_metadata = null, + $tiny_metadata = null, + $active_sizes = null, + $active_tinify_sizes = null ) { - $this->settings = $settings; - $this->id = $id; + $this->settings = $settings; + $this->id = $id; $this->wp_metadata = $wp_metadata; $this->parse_wp_metadata(); $this->parse_tiny_metadata( $tiny_metadata ); @@ -52,22 +52,22 @@ private function parse_wp_metadata() { if ( ! is_array( $this->wp_metadata ) || ! isset( $this->wp_metadata['file'] ) ) { /* No file metadata found, this might be another plugin messing with - metadata. Simply ignore this! */ + metadata. Simply ignore this! */ return; } - $upload_dir = wp_upload_dir(); + $upload_dir = wp_upload_dir(); $path_prefix = $upload_dir['basedir'] . '/'; - $path_info = pathinfo( $this->wp_metadata['file'] ); + $path_info = pathinfo( $this->wp_metadata['file'] ); if ( isset( $path_info['dirname'] ) ) { $path_prefix .= $path_info['dirname'] . '/'; } /* Do not use pathinfo for getting the filename. - It doesn't work when the filename starts with a special character. */ - $path_parts = explode( '/', $this->wp_metadata['file'] ); - $this->name = end( $path_parts ); - $filename = $path_prefix . $this->name; + It doesn't work when the filename starts with a special character. */ + $path_parts = explode( '/', $this->wp_metadata['file'] ); + $this->name = end( $path_parts ); + $filename = $path_prefix . $this->name; $this->sizes[ self::ORIGINAL ] = new Tiny_Image_Size( $filename ); // Ensure 'sizes' exists and is an array to prevent PHP Warnings @@ -81,7 +81,7 @@ private function parse_wp_metadata() { if ( is_array( $size_info ) && isset( $size_info['file'] ) ) { // Add to sanitized metadata $sanitized_sizes[ $size_name ] = $size_info; - $this->sizes[ $size_name ] = new Tiny_Image_Size( + $this->sizes[ $size_name ] = new Tiny_Image_Size( $path_prefix . $size_info['file'] ); } @@ -151,7 +151,7 @@ private function parse_tiny_metadata( $tiny_metadata = null ) { if ( 'original' === $size_name ) { $size_name = '0'; } - $retina_path = wr2x_get_retina( + $retina_path = wr2x_get_retina( $this->sizes[ $size_name ]->filename ); $this->sizes[ $size ] = new Tiny_Image_Size( $retina_path ); @@ -177,11 +177,7 @@ public function get_wp_metadata() { } public function file_type_allowed() { - return in_array( - $this->get_mime_type(), - array( 'image/jpeg', 'image/png', 'image/webp' ), - true - ); + return in_array( $this->get_mime_type(), array( 'image/jpeg', 'image/png', 'image/webp' ) ); } public function get_mime_type() { @@ -189,25 +185,22 @@ public function get_mime_type() { } public function compress() { - Tiny_Logger::debug( - 'compress', - array( - 'image_id' => $this->id, - 'name' => $this->name, - ) - ); + Tiny_Logger::debug('compress', array( + 'image_id' => $this->id, + 'name' => $this->name, + )); if ( $this->settings->get_compressor() === null || ! $this->file_type_allowed() ) { return; } $success = 0; - $failed = 0; + $failed = 0; $active_tinify_sizes = $this->settings->get_active_tinify_sizes(); if ( $this->settings->get_conversion_enabled() ) { $uncompressed_sizes = $this->filter_image_sizes( 'uncompressed', $active_tinify_sizes ); - $unconverted_sizes = $this->filter_image_sizes( 'unconverted', $active_tinify_sizes ); + $unconverted_sizes = $this->filter_image_sizes( 'unconverted', $active_tinify_sizes ); $unprocessed_sizes = $uncompressed_sizes + $unconverted_sizes; } else { @@ -221,25 +214,22 @@ public function compress() { if ( ! $size->is_duplicate() ) { $size->add_tiny_meta_start(); $this->update_tiny_post_meta(); - $resize = $this->settings->get_resize_options( $size_name ); + $resize = $this->settings->get_resize_options( $size_name ); $preserve = $this->settings->get_preserve_options( $size_name ); - Tiny_Logger::debug( - 'compress size', - array( - 'image_id' => $this->id, - 'size' => $size_name, - 'resize' => $resize, - 'preserve' => $preserve, - 'convert' => $convert_to, - 'modified' => $size->modified(), - 'filename' => $size->filename, - 'is_duplicate' => $size->is_duplicate(), - 'exists' => $size->exists(), - 'has_been_compressed' => $size->has_been_compressed(), - 'filesize' => $size->filesize(), - 'mimetype' => $size->mimetype(), - ) - ); + Tiny_Logger::debug('compress size', array( + 'image_id' => $this->id, + 'size' => $size_name, + 'resize' => $resize, + 'preserve' => $preserve, + 'convert' => $convert_to, + 'modified' => $size->modified(), + 'filename' => $size->filename, + 'is_duplicate' => $size->is_duplicate(), + 'exists' => $size->exists(), + 'has_been_compressed' => $size->has_been_compressed(), + 'filesize' => $size->filesize(), + 'mimetype' => $size->mimetype(), + )); try { $response = $compressor->compress_file( $size->filename, @@ -254,25 +244,19 @@ public function compress() { $convert_to; $size->add_tiny_meta( $response ); - ++$success; - Tiny_Logger::debug( - 'compress success', - array( - 'size' => $size_name, - 'image_id' => $this->id, - ) - ); + $success++; + Tiny_Logger::debug('compress success', array( + 'size' => $size_name, + 'image_id' => $this->id, + )); } catch ( Tiny_Exception $e ) { $size->add_tiny_meta_error( $e ); - ++$failed; - Tiny_Logger::error( - 'compress failed', - array( - 'error' => $e->get_message(), - 'size' => $size_name, - 'image_id' => $this->id, - ) - ); + $failed++; + Tiny_Logger::error('compress failed', array( + 'error' => $e->get_message(), + 'size' => $size_name, + 'image_id' => $this->id, + )); } $this->add_wp_metadata( $size_name, $size ); $this->update_tiny_post_meta(); @@ -287,7 +271,7 @@ public function compress() { return array( 'success' => $success, - 'failed' => $failed, + 'failed' => $failed, ); } @@ -343,8 +327,8 @@ public function add_wp_metadata( $size_name, $size ) { if ( isset( $size->meta['output'] ) ) { $output = $size->meta['output']; if ( isset( $output['width'] ) && isset( $output['height'] ) ) { - $this->wp_metadata['width'] = $output['width']; - $this->wp_metadata['height'] = $output['height']; + $this->wp_metadata['width'] = $output['width']; + $this->wp_metadata['height'] = $output['height']; $this->wp_metadata['filesize'] = $output['size']; } } @@ -365,12 +349,12 @@ public function update_tiny_post_meta() { } public function get_image_sizes() { - $original = isset( $this->sizes[ self::ORIGINAL ] ) + $original = isset( $this->sizes[ self::ORIGINAL ] ) ? array( self::ORIGINAL => $this->sizes[ self::ORIGINAL ], ) : array(); - $compressed = array(); + $compressed = array(); $uncompressed = array(); foreach ( $this->sizes as $size_name => $size ) { if ( self::is_original( $size_name ) ) { @@ -428,7 +412,7 @@ public function get_count( $methods, $count_sizes = null ) { foreach ( $methods as $method ) { if ( $this->sizes[ $size ]->$method() ) { - ++$stats[ $method ]; + $stats[ $method ]++; } } } @@ -437,20 +421,14 @@ public function get_count( $methods, $count_sizes = null ) { public function get_latest_error() { $active_tinify_sizes = $this->settings->get_active_tinify_sizes(); - $error_message = null; - $last_timestamp = null; + $error_message = null; + $last_timestamp = null; foreach ( $this->sizes as $size_name => $size ) { if ( in_array( $size_name, $active_tinify_sizes, true ) ) { if ( isset( $size->meta['error'] ) && isset( $size->meta['message'] ) ) { - if ( - null === $last_timestamp || - $last_timestamp < $size->meta['timestamp'] - ) { + if ( null === $last_timestamp || $last_timestamp < $size->meta['timestamp'] ) { $last_timestamp = $size->meta['timestamp']; - $error_message = Tiny_Helpers::truncate_text( - $size->meta['message'], - 140 - ); + $error_message = Tiny_Helpers::truncate_text( $size->meta['message'], 140 ); } } } @@ -460,11 +438,11 @@ public function get_latest_error() { public function get_savings( $stats ) { $before = $stats['initial_total_size']; - $after = $stats['compressed_total_size']; + $after = $stats['compressed_total_size']; if ( 0 === $before ) { $savings = 0; } else { - $savings = ( $before - $after ) / $before * 100; + $savings = ($before - $after) / $before * 100; } return '' . number_format( $savings, 1 ); } @@ -475,12 +453,12 @@ public function get_statistics( $active_sizes, $active_tinify_sizes ) { return $this->statistics; } - $this->statistics['initial_total_size'] = 0; - $this->statistics['compressed_total_size'] = 0; - $this->statistics['image_sizes_compressed'] = 0; + $this->statistics['initial_total_size'] = 0; + $this->statistics['compressed_total_size'] = 0; + $this->statistics['image_sizes_compressed'] = 0; $this->statistics['available_uncompressed_sizes'] = 0; - $this->statistics['image_sizes_converted'] = 0; - $this->statistics['available_unconverted_sizes'] = 0; + $this->statistics['image_sizes_converted'] = 0; + $this->statistics['available_unconverted_sizes'] = 0; foreach ( $this->sizes as $size_name => $size ) { // skip duplicates or inactive sizes @@ -488,11 +466,11 @@ public function get_statistics( $active_sizes, $active_tinify_sizes ) { continue; } - $file_size = $size->filesize(); - $is_active_size = in_array( $size_name, $active_tinify_sizes, true ); + $file_size = $size->filesize(); + $is_active_size = in_array( $size_name, $active_tinify_sizes, true ); if ( isset( $size->meta['input'] ) ) { - $input_size = (int) $size->meta['input']['size']; + $input_size = (int) $size->meta['input']['size']; $this->statistics['initial_total_size'] += $input_size; if ( isset( $size->meta['output'] ) ) { @@ -501,28 +479,28 @@ public function get_statistics( $active_sizes, $active_tinify_sizes ) { if ( $size->modified() ) { $this->statistics['compressed_total_size'] += $file_size; if ( $is_active_size ) { - ++$this->statistics['available_uncompressed_sizes']; + $this->statistics['available_uncompressed_sizes']++; } } else { $this->statistics['compressed_total_size'] += $output_size; - ++$this->statistics['image_sizes_compressed']; + $this->statistics['image_sizes_compressed']++; } } else { $this->statistics['compressed_total_size'] += $input_size; } } elseif ( $size->exists() ) { - $this->statistics['initial_total_size'] += $file_size; + $this->statistics['initial_total_size'] += $file_size; $this->statistics['compressed_total_size'] += $file_size; if ( $is_active_size ) { - ++$this->statistics['available_uncompressed_sizes']; + $this->statistics['available_uncompressed_sizes']++; } } if ( $is_active_size ) { if ( $size->has_been_converted() ) { - ++$this->statistics['image_sizes_converted']; + $this->statistics['image_sizes_converted']++; } else { - ++$this->statistics['available_unconverted_sizes']; + $this->statistics['available_unconverted_sizes']++; } } }// End foreach(). @@ -571,6 +549,7 @@ private function convert_to() { } return $convert_settings['convert_to']; + } /** @@ -592,7 +571,7 @@ public function mark_as_compressed() { if ( $this->settings->get_conversion_enabled() ) { $uncompressed_sizes = $this->filter_image_sizes( 'uncompressed', $active_tinify_sizes ); - $unconverted_sizes = $this->filter_image_sizes( 'unconverted', $active_tinify_sizes ); + $unconverted_sizes = $this->filter_image_sizes( 'unconverted', $active_tinify_sizes ); $unprocessed_sizes = $uncompressed_sizes + $unconverted_sizes; } else { diff --git a/src/class-tiny-logger.php b/src/class-tiny-logger.php index 2111d39f..7df5ebad 100644 --- a/src/class-tiny-logger.php +++ b/src/class-tiny-logger.php @@ -35,7 +35,7 @@ class Tiny_Logger { private static $instance = null; - private $log_enabled = null; + private $log_enabled = null; private $log_file_path = null; /** @@ -57,7 +57,7 @@ public static function get_instance() { */ private function __construct() { $this->log_file_path = $this->resolve_log_file_path(); - $this->log_enabled = 'on' === get_option( 'tinypng_logging_enabled', false ); + $this->log_enabled = 'on' === get_option( 'tinypng_logging_enabled', false ); } /** @@ -71,10 +71,7 @@ private function __construct() { public static function init() { add_filter( 'pre_update_option_tinypng_logging_enabled', - 'Tiny_Logger::on_save_log_enabled', - 10, - 3 - ); + 'Tiny_Logger::on_save_log_enabled', 10, 3 ); } /** @@ -111,7 +108,7 @@ public function get_log_file_path() { * - if turn on, clear the old logs */ public static function on_save_log_enabled( $log_enabled, $old, $option ) { - $instance = self::get_instance(); + $instance = self::get_instance(); $instance->log_enabled = 'on' === $log_enabled; if ( $instance->get_log_enabled() ) { self::clear_logs(); @@ -129,7 +126,7 @@ public static function on_save_log_enabled( $log_enabled, $old, $option ) { */ private function resolve_log_file_path() { $upload_dir = wp_upload_dir(); - $log_dir = trailingslashit( $upload_dir['basedir'] ) . 'tiny-compress-logs'; + $log_dir = trailingslashit( $upload_dir['basedir'] ) . 'tiny-compress-logs'; return trailingslashit( $log_dir ) . 'tiny-compress.log'; } @@ -171,17 +168,17 @@ private function log( $level, $message, $context = array() ) { $this->rotate_logs(); // Ensure log directory exists. - $log_dir = dirname( $this->log_file_path ); + $log_dir = dirname( $this->log_file_path ); $wp_filesystem = Tiny_Helpers::get_wp_filesystem(); if ( ! $wp_filesystem->exists( $log_dir ) ) { wp_mkdir_p( $log_dir ); self::create_blocking_files( $log_dir ); } - $timestamp = current_time( 'Y-m-d H:i:s' ); - $level_str = strtoupper( $level ); + $timestamp = current_time( 'Y-m-d H:i:s' ); + $level_str = strtoupper( $level ); $context_str = ! empty( $context ) ? ' ' . wp_json_encode( $context ) : ''; - $log_entry = "[{$timestamp}] [{$level_str}] {$message}{$context_str}" . PHP_EOL; + $log_entry = "[{$timestamp}] [{$level_str}] {$message}{$context_str}" . PHP_EOL; error_log( $log_entry, 3, $this->log_file_path ); } @@ -212,10 +209,10 @@ private function rotate_logs() { * @return bool True if logs were cleared successfully. */ public static function clear_logs() { - $instance = self::get_instance(); - $log_path = $instance->get_log_file_path(); + $instance = self::get_instance(); + $log_path = $instance->get_log_file_path(); $wp_filesystem = Tiny_Helpers::get_wp_filesystem(); - $file_exits = $wp_filesystem->exists( $log_path ); + $file_exits = $wp_filesystem->exists( $log_path ); if ( $file_exits ) { return $wp_filesystem->delete( $log_path ); } diff --git a/src/class-tiny-plugin.php b/src/class-tiny-plugin.php index 1fc976c8..8c9c05f0 100644 --- a/src/class-tiny-plugin.php +++ b/src/class-tiny-plugin.php @@ -18,8 +18,8 @@ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ class Tiny_Plugin extends Tiny_WP_Base { - const VERSION = '3.6.8'; - const MEDIA_COLUMN = self::NAME; + const VERSION = '3.6.7'; + const MEDIA_COLUMN = self::NAME; const DATETIME_FORMAT = 'Y-m-d G:i:s'; private static $version; @@ -33,7 +33,7 @@ public static function jpeg_quality() { public static function version() { /* Avoid using get_plugin_data() because it is not loaded early enough - in xmlrpc.php. */ + in xmlrpc.php. */ return self::VERSION; } @@ -47,33 +47,37 @@ public function set_compressor( $compressor ) { } public function init() { - add_filter( - 'jpeg_quality', + add_filter( 'jpeg_quality', $this->get_static_method( 'jpeg_quality' ) ); - add_filter( - 'wp_editor_set_quality', + add_filter( 'wp_editor_set_quality', $this->get_static_method( 'jpeg_quality' ) ); - add_filter( - 'wp_generate_attachment_metadata', + add_filter( 'wp_generate_attachment_metadata', $this->get_method( 'process_attachment' ), - 10, - 2 + 10, 2 ); add_action( 'delete_attachment', $this->get_method( 'clean_attachment' ), 10, 2 ); - load_plugin_textdomain( - self::NAME, - false, + load_plugin_textdomain( self::NAME, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); - new Tiny_Picture( $this->settings, ABSPATH, array( get_site_url() ) ); - $this->tiny_compatibility(); + if ( $this->settings->get_conversion_enabled() ) { + /** + * Controls wether the page should replace with elements + * converted sources. + * + * @since 3.7.0 + */ + $should_replace = apply_filters( 'tiny_replace_with_picture', true ); + if ( $should_replace ) { + new Tiny_Picture( ABSPATH, array( get_site_url() ) ); + } + } } public function cli_init() { @@ -81,116 +85,93 @@ public function cli_init() { } public function ajax_init() { - add_filter( - 'wp_ajax_tiny_async_optimize_upload_new_media', + add_filter( 'wp_ajax_tiny_async_optimize_upload_new_media', $this->get_method( 'compress_on_upload' ) ); - add_action( - 'wp_ajax_tiny_compress_image_from_library', + add_action( 'wp_ajax_tiny_compress_image_from_library', $this->get_method( 'compress_image_from_library' ) ); - add_action( - 'wp_ajax_tiny_compress_image_for_bulk', + add_action( 'wp_ajax_tiny_compress_image_for_bulk', $this->get_method( 'compress_image_for_bulk' ) ); - add_action( - 'wp_ajax_tiny_get_optimization_statistics', + add_action( 'wp_ajax_tiny_get_optimization_statistics', $this->get_method( 'ajax_optimization_statistics' ) ); - add_action( - 'wp_ajax_tiny_get_compression_status', + add_action( 'wp_ajax_tiny_get_compression_status', $this->get_method( 'ajax_compression_status' ) ); - add_action( - 'wp_ajax_tiny_mark_image_as_compressed', + add_action( 'wp_ajax_tiny_mark_image_as_compressed', $this->get_method( 'mark_image_as_compressed' ) ); /* When touching any functionality linked to image compressions when - uploading images make sure it also works with XML-RPC. See README. */ - add_filter( - 'wp_ajax_nopriv_tiny_rpc', + uploading images make sure it also works with XML-RPC. See README. */ + add_filter( 'wp_ajax_nopriv_tiny_rpc', $this->get_method( 'process_rpc_request' ) ); if ( $this->settings->compress_wr2x_images() ) { - add_action( - 'wr2x_upload_retina', + add_action( 'wr2x_upload_retina', $this->get_method( 'compress_original_retina_image' ), - 10, - 2 + 10, 2 ); - add_action( - 'wr2x_retina_file_added', + add_action( 'wr2x_retina_file_added', $this->get_method( 'compress_retina_image' ), - 10, - 3 + 10, 3 ); - add_action( - 'wr2x_retina_file_removed', + add_action( 'wr2x_retina_file_removed', $this->get_method( 'remove_retina_image' ), - 10, - 2 + 10, 2 ); } } public function admin_init() { - add_action( - 'wp_dashboard_setup', + add_action('wp_dashboard_setup', $this->get_method( 'add_dashboard_widget' ) ); - add_action( - 'admin_enqueue_scripts', + add_action( 'admin_enqueue_scripts', $this->get_method( 'enqueue_scripts' ) ); - add_action( - 'admin_action_tiny_bulk_action', + add_action( 'admin_action_tiny_bulk_action', $this->get_method( 'media_library_bulk_action' ) ); - add_action( - 'admin_action_-1', + add_action( 'admin_action_-1', $this->get_method( 'media_library_bulk_action' ) ); - add_action( - 'admin_action_tiny_bulk_mark_compressed', + add_action( 'admin_action_tiny_bulk_mark_compressed', $this->get_method( 'media_library_bulk_action' ) ); - add_filter( - 'manage_media_columns', + add_filter( 'manage_media_columns', $this->get_method( 'add_media_columns' ) ); - add_action( - 'manage_media_custom_column', + add_action( 'manage_media_custom_column', $this->get_method( 'render_media_column' ), - 10, - 2 + 10, 2 ); - add_action( - 'attachment_submitbox_misc_actions', + add_action( 'attachment_submitbox_misc_actions', $this->get_method( 'show_media_info' ) ); $plugin = plugin_basename( - dirname( __DIR__ ) . '/tiny-compress-images.php' + dirname( dirname( __FILE__ ) ) . '/tiny-compress-images.php' ); - add_filter( - "plugin_action_links_$plugin", + add_filter( "plugin_action_links_$plugin", $this->get_method( 'add_plugin_links' ) ); @@ -216,7 +197,7 @@ public function add_plugin_links( $current_links ) { '%s', esc_html__( 'Settings', 'tiny-compress-images' ) ), - 'bulk' => sprintf( + 'bulk' => sprintf( '%s', esc_html__( 'Bulk TinyPNG', 'tiny-compress-images' ) ), @@ -226,14 +207,12 @@ public function add_plugin_links( $current_links ) { public function tiny_compatibility() { if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { - new Tiny_WPML(); + $tiny_wpml_compatibility = new Tiny_WPML(); } if ( Tiny_AS3CF::is_active() ) { - new Tiny_AS3CF( $this->settings ); + $tiny_as3cf = new Tiny_AS3CF( $this->settings ); } - - new Tiny_WooCommerce(); } public function compress_original_retina_image( $attachment_id, $path ) { @@ -252,77 +231,48 @@ public function remove_retina_image( $attachment_id, $path ) { } public function enqueue_scripts( $hook ) { - wp_enqueue_style( - self::NAME . '_admin', + wp_enqueue_style( self::NAME . '_admin', plugins_url( '/css/admin.css', __FILE__ ), - array(), - self::version() + array(), self::version() ); - wp_enqueue_style( - self::NAME . '_chart', + wp_enqueue_style( self::NAME . '_chart', plugins_url( '/css/optimization-chart.css', __FILE__ ), - array(), - self::version() + array(), self::version() ); - wp_register_script( - self::NAME . '_admin', + wp_register_script( self::NAME . '_admin', plugins_url( '/js/admin.js', __FILE__ ), - array(), - self::version(), - true + array(), self::version(), true ); // WordPress < 3.3 does not handle multidimensional arrays - wp_localize_script( - self::NAME . '_admin', - 'tinyCompress', - array( - 'nonce' => wp_create_nonce( 'tiny-compress' ), - 'wpVersion' => self::wp_version(), - 'pluginVersion' => self::version(), - 'L10nAllDone' => __( - 'All images are processed', - 'tiny-compress-images' - ), - 'L10nNoActionTaken' => __( - 'No action taken', - 'tiny-compress-images' - ), - 'L10nDuplicate' => __( - 'Image was already processed', - 'tiny-compress-images' - ), - 'L10nBulkAction' => __( 'Compress Images', 'tiny-compress-images' ), - 'L10nBulkMarkCompressed' => __( - 'Mark as Compressed', - 'tiny-compress-images' - ), - 'L10nCancelled' => __( 'Cancelled', 'tiny-compress-images' ), - 'L10nCompressing' => __( 'Compressing', 'tiny-compress-images' ), - 'L10nCompressed' => __( 'compressed', 'tiny-compress-images' ), - 'L10nConverted' => __( 'converted', 'tiny-compress-images' ), - 'L10nFile' => __( 'File', 'tiny-compress-images' ), - 'L10nSizesOptimized' => __( - 'Sizes optimized', - 'tiny-compress-images' - ), - 'L10nInitialSize' => __( 'Initial size', 'tiny-compress-images' ), - 'L10nCurrentSize' => __( 'Current size', 'tiny-compress-images' ), - 'L10nSavings' => __( 'Savings', 'tiny-compress-images' ), - 'L10nStatus' => __( 'Status', 'tiny-compress-images' ), - 'L10nShowMoreDetails' => __( - 'Show more details', - 'tiny-compress-images' - ), - 'L10nError' => __( 'Error', 'tiny-compress-images' ), - 'L10nLatestError' => __( 'Latest error', 'tiny-compress-images' ), - 'L10nInternalError' => __( 'Internal error', 'tiny-compress-images' ), - 'L10nOutOf' => __( 'out of', 'tiny-compress-images' ), - 'L10nWaiting' => __( 'Waiting', 'tiny-compress-images' ), - ) - ); + wp_localize_script( self::NAME . '_admin', 'tinyCompress', array( + 'nonce' => wp_create_nonce( 'tiny-compress' ), + 'wpVersion' => self::wp_version(), + 'pluginVersion' => self::version(), + 'L10nAllDone' => __( 'All images are processed', 'tiny-compress-images' ), + 'L10nNoActionTaken' => __( 'No action taken', 'tiny-compress-images' ), + 'L10nDuplicate' => __( 'Image was already processed', 'tiny-compress-images' ), + 'L10nBulkAction' => __( 'Compress Images', 'tiny-compress-images' ), + 'L10nBulkMarkCompressed' => __( 'Mark as Compressed', 'tiny-compress-images' ), + 'L10nCancelled' => __( 'Cancelled', 'tiny-compress-images' ), + 'L10nCompressing' => __( 'Compressing', 'tiny-compress-images' ), + 'L10nCompressed' => __( 'compressed', 'tiny-compress-images' ), + 'L10nConverted' => __( 'converted', 'tiny-compress-images' ), + 'L10nFile' => __( 'File', 'tiny-compress-images' ), + 'L10nSizesOptimized' => __( 'Sizes optimized', 'tiny-compress-images' ), + 'L10nInitialSize' => __( 'Initial size', 'tiny-compress-images' ), + 'L10nCurrentSize' => __( 'Current size', 'tiny-compress-images' ), + 'L10nSavings' => __( 'Savings', 'tiny-compress-images' ), + 'L10nStatus' => __( 'Status', 'tiny-compress-images' ), + 'L10nShowMoreDetails' => __( 'Show more details', 'tiny-compress-images' ), + 'L10nError' => __( 'Error', 'tiny-compress-images' ), + 'L10nLatestError' => __( 'Latest error', 'tiny-compress-images' ), + 'L10nInternalError' => __( 'Internal error', 'tiny-compress-images' ), + 'L10nOutOf' => __( 'out of', 'tiny-compress-images' ), + 'L10nWaiting' => __( 'Waiting', 'tiny-compress-images' ), + )); wp_enqueue_script( self::NAME . '_admin' ); @@ -330,23 +280,18 @@ public function enqueue_scripts( $hook ) { wp_enqueue_style( self::NAME . '_tiny_bulk_optimization', plugins_url( '/css/bulk-optimization.css', __FILE__ ), - array(), - self::version() + array(), self::version() ); - wp_enqueue_style( - self::NAME . '_chart', + wp_enqueue_style( self::NAME . '_chart', plugins_url( '/css/optimization-chart.css', __FILE__ ), - array(), - self::version() + array(), self::version() ); wp_register_script( self::NAME . '_tiny_bulk_optimization', plugins_url( '/js/bulk-optimization.js', __FILE__ ), - array(), - self::version(), - true + array(), self::version(), true ); wp_enqueue_script( self::NAME . '_tiny_bulk_optimization' ); @@ -371,12 +316,9 @@ public function blocking_compress_on_upload( $metadata, $attachment_id ) { if ( ! empty( $metadata ) ) { $tiny_image = new Tiny_Image( $this->settings, $attachment_id, $metadata ); - Tiny_Logger::debug( - 'blocking compress on upload', - array( - 'image_id' => $attachment_id, - ) - ); + Tiny_Logger::debug('blocking compress on upload', array( + 'image_id' => $attachment_id, + )); $result = $tiny_image->compress(); return $tiny_image->get_wp_metadata(); @@ -389,7 +331,7 @@ public function async_compress_on_upload( $metadata, $attachment_id ) { $context = 'wp'; $action = 'tiny_async_optimize_upload_new_media'; $_ajax_nonce = wp_create_nonce( 'new_media-' . $attachment_id ); - $body = compact( 'action', '_ajax_nonce', 'metadata', 'attachment_id', 'context' ); + $body = compact( 'action', '_ajax_nonce', 'metadata', 'attachment_id', 'context' ); $args = array( 'timeout' => 0.01, @@ -405,9 +347,9 @@ public function async_compress_on_upload( $metadata, $attachment_id ) { $args['body']['tiny_rpc_action'] = $args['body']['action']; /* We set a different action to make sure that all RPC requests are first validated. */ - $args['body']['action'] = 'tiny_rpc'; - $args['body']['tiny_rpc_hash'] = $rpc_hash; - $args['body']['tiny_rpc_nonce'] = wp_create_nonce( 'tiny_rpc_' . $rpc_hash ); + $args['body']['action'] = 'tiny_rpc'; + $args['body']['tiny_rpc_hash'] = $rpc_hash; + $args['body']['tiny_rpc_nonce'] = wp_create_nonce( 'tiny_rpc_' . $rpc_hash ); /* We can't use cookies here, so we save the user id in a transient @@ -418,12 +360,9 @@ public function async_compress_on_upload( $metadata, $attachment_id ) { set_transient( 'tiny_rpc_' . $rpc_hash, get_current_user_id(), 10 ); } - Tiny_Logger::debug( - 'remote post', - array( - 'image_id' => $attachment_id, - ) - ); + Tiny_Logger::debug('remote post', array( + 'image_id' => $attachment_id, + )); if ( getenv( 'WORDPRESS_HOST' ) !== false ) { wp_remote_post( getenv( 'WORDPRESS_HOST' ) . '/wp-admin/admin-ajax.php', $args ); @@ -442,8 +381,8 @@ public function process_rpc_request() { } $rpc_hash = sanitize_key( $_POST['tiny_rpc_hash'] ); - $user_id = absint( get_transient( 'tiny_rpc_' . $rpc_hash ) ); - $user = $user_id ? get_userdata( $user_id ) : false; + $user_id = absint( get_transient( 'tiny_rpc_' . $rpc_hash ) ); + $user = $user_id ? get_userdata( $user_id ) : false; /* We no longer need the transient. */ delete_transient( 'tiny_rpc_' . $rpc_hash ); @@ -474,16 +413,13 @@ public function compress_on_upload() { } if ( current_user_can( 'upload_files' ) ) { $attachment_id = intval( $_POST['attachment_id'] ); - $metadata = $_POST['metadata']; + $metadata = $_POST['metadata']; if ( is_array( $metadata ) ) { $tiny_image = new Tiny_Image( $this->settings, $attachment_id, $metadata ); - Tiny_Logger::debug( - 'compress on upload', - array( - 'image_id' => $attachment_id, - ) - ); + Tiny_Logger::debug('compress on upload', array( + 'image_id' => $attachment_id, + )); $result = $tiny_image->compress(); // The wp_update_attachment_metadata call is thrown because the @@ -504,7 +440,7 @@ public function compress_on_upload() { * @since 3.0.0 * * @return array Either error array ['error' => 'message'] - * or success array ['data' => [$id, $metadata]] + * or success array ['data' => [$id, $metadata]] */ private function validate_ajax_attachment_request() { if ( ! $this->check_ajax_referer() ) { @@ -526,7 +462,7 @@ private function validate_ajax_attachment_request() { ), ); } - $id = intval( $_POST['id'] ); + $id = intval( $_POST['id'] ); $metadata = wp_get_attachment_metadata( $id ); if ( ! is_array( $metadata ) ) { return array( @@ -550,15 +486,12 @@ public function compress_image_from_library() { } list($id, $metadata) = $response['data']; - Tiny_Logger::debug( - 'compress from library', - array( - 'image_id' => $id, - ) - ); + Tiny_Logger::debug('compress from library', array( + 'image_id' => $id, + )); $tiny_image = new Tiny_Image( $this->settings, $id, $metadata ); - $result = $tiny_image->compress(); + $result = $tiny_image->compress(); // The wp_update_attachment_metadata call is thrown because the // dimensions of the original image can change. This will then @@ -580,24 +513,21 @@ public function compress_image_for_bulk() { exit(); } - list($id, $metadata) = $response['data']; - $tiny_image_before = new Tiny_Image( $this->settings, $id, $metadata ); + list($id, $metadata) = $response['data']; + $tiny_image_before = new Tiny_Image( $this->settings, $id, $metadata ); $image_statistics_before = $tiny_image_before->get_statistics( $this->settings->get_sizes(), $this->settings->get_active_tinify_sizes() ); - $size_before = $image_statistics_before['compressed_total_size']; + $size_before = $image_statistics_before['compressed_total_size']; $tiny_image = new Tiny_Image( $this->settings, $id, $metadata ); - Tiny_Logger::debug( - 'compress from bulk', - array( - 'image_id' => $id, - ) - ); + Tiny_Logger::debug('compress from bulk', array( + 'image_id' => $id, + )); - $result = $tiny_image->compress(); + $result = $tiny_image->compress(); $image_statistics = $tiny_image->get_statistics( $this->settings->get_sizes(), $this->settings->get_active_tinify_sizes() @@ -605,36 +535,31 @@ public function compress_image_for_bulk() { wp_update_attachment_metadata( $id, $tiny_image->get_wp_metadata() ); $current_library_size = intval( $_POST['current_size'] ); - $size_after = $image_statistics['compressed_total_size']; - $new_library_size = $current_library_size + $size_after - $size_before; + $size_after = $image_statistics['compressed_total_size']; + $new_library_size = $current_library_size + $size_after - $size_before; - $result['message'] = $tiny_image->get_latest_error(); + $result['message'] = $tiny_image->get_latest_error(); $result['image_sizes_compressed'] = $image_statistics['image_sizes_compressed']; - $result['image_sizes_converted'] = $image_statistics['image_sizes_converted']; - $result['image_sizes_optimized'] = $image_statistics['image_sizes_compressed']; + $result['image_sizes_converted'] = $image_statistics['image_sizes_converted']; + $result['image_sizes_optimized'] = $image_statistics['image_sizes_compressed']; $result['initial_total_size'] = size_format( - $image_statistics['initial_total_size'], - 1 + $image_statistics['initial_total_size'], 1 ); $result['optimized_total_size'] = size_format( - $image_statistics['compressed_total_size'], - 1 + $image_statistics['compressed_total_size'], 1 ); - $result['savings'] = $tiny_image->get_savings( $image_statistics ); - $result['status'] = $this->settings->get_status(); - $result['thumbnail'] = wp_get_attachment_image( - $id, - array( '30', '30' ), - true, - array( + $result['savings'] = $tiny_image->get_savings( $image_statistics ); + $result['status'] = $this->settings->get_status(); + $result['thumbnail'] = wp_get_attachment_image( + $id, array( '30', '30' ), true, array( 'class' => 'pinkynail', - 'alt' => '', + 'alt' => '', ) ); - $result['size_change'] = $size_after - $size_before; + $result['size_change'] = $size_after - $size_before; $result['human_readable_library_size'] = size_format( $new_library_size, 2 ); echo json_encode( $result ); @@ -668,8 +593,8 @@ public function ajax_compression_status() { public function media_library_bulk_action() { $valid_actions = array( 'tiny_bulk_action', 'tiny_bulk_mark_compressed' ); - $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : ''; - $action2 = isset( $_REQUEST['action2'] ) ? $_REQUEST['action2'] : ''; + $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : ''; + $action2 = isset( $_REQUEST['action2'] ) ? $_REQUEST['action2'] : ''; if ( ! in_array( $action, $valid_actions, true ) && @@ -682,7 +607,7 @@ public function media_library_bulk_action() { return; } check_admin_referer( 'bulk-media' ); - $ids = implode( '-', array_map( 'intval', $_REQUEST['media'] ) ); + $ids = implode( '-', array_map( 'intval', $_REQUEST['media'] ) ); $location = 'upload.php?mode=list&ids=' . $ids; $location = add_query_arg( 'action', $_REQUEST['action'], $location ); @@ -735,9 +660,9 @@ public function show_media_info() { private function render_compress_details( $tiny_image ) { $in_progress = $tiny_image->filter_image_sizes( 'in_progress' ); if ( count( $in_progress ) > 0 ) { - include __DIR__ . '/views/compress-details-processing.php'; + include( dirname( __FILE__ ) . '/views/compress-details-processing.php' ); } else { - include __DIR__ . '/views/compress-details.php'; + include( dirname( __FILE__ ) . '/views/compress-details.php' ); } } @@ -752,52 +677,41 @@ public function render_bulk_optimization_page() { $stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->settings ); $estimated_costs = $this->get_estimated_bulk_cost( $stats['estimated_credit_use'] ); - $admin_colors = self::retrieve_admin_colors(); + $admin_colors = self::retrieve_admin_colors(); /* This makes sure that up to date information is retrieved from the API. */ $this->settings->get_compressor()->get_status(); $active_tinify_sizes = $this->settings->get_active_tinify_sizes(); - $remaining_credits = $this->settings->get_remaining_credits(); - $is_on_free_plan = $this->settings->is_on_free_plan(); - $email_address = $this->settings->get_email_address(); + $remaining_credits = $this->settings->get_remaining_credits(); + $is_on_free_plan = $this->settings->is_on_free_plan(); + $email_address = $this->settings->get_email_address(); - include __DIR__ . '/views/bulk-optimization.php'; + include( dirname( __FILE__ ) . '/views/bulk-optimization.php' ); } public function add_dashboard_widget() { - wp_enqueue_style( - self::NAME . '_chart', + wp_enqueue_style( self::NAME . '_chart', plugins_url( '/css/optimization-chart.css', __FILE__ ), - array(), - self::version() + array(), self::version() ); - wp_enqueue_style( - self::NAME . '_dashboard_widget', + wp_enqueue_style( self::NAME . '_dashboard_widget', plugins_url( '/css/dashboard-widget.css', __FILE__ ), - array(), - self::version() + array(), self::version() ); - wp_register_script( - self::NAME . '_dashboard_widget', + wp_register_script( self::NAME . '_dashboard_widget', plugins_url( '/js/dashboard-widget.js', __FILE__ ), - array(), - self::version(), - true + array(), self::version(), true ); /* This might be deduplicated with the admin script localization, but - the order of including scripts is sometimes different. So in that - case we need to make sure that the order of inclusion is correc.t */ - wp_localize_script( - self::NAME . '_dashboard_widget', - 'tinyCompressDashboard', - array( - 'nonce' => wp_create_nonce( 'tiny-compress' ), - ) - ); + the order of including scripts is sometimes different. So in that + case we need to make sure that the order of inclusion is correc.t */ + wp_localize_script( self::NAME . '_dashboard_widget', 'tinyCompressDashboard', array( + 'nonce' => wp_create_nonce( 'tiny-compress' ), + )); wp_enqueue_script( self::NAME . '_dashboard_widget' ); @@ -808,15 +722,15 @@ public function add_dashboard_widget() { ); } - public function add_widget_view() { + function add_widget_view() { $admin_colors = self::retrieve_admin_colors(); - include __DIR__ . '/views/dashboard-widget.php'; + include( dirname( __FILE__ ) . '/views/dashboard-widget.php' ); } private static function retrieve_admin_colors() { global $_wp_admin_css_colors; $admin_colour_scheme = get_user_option( 'admin_color', get_current_user_id() ); - $admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // default + $admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // default if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ] ) ) { if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ]->colors ) ) { $admin_colors = $_wp_admin_css_colors[ $admin_colour_scheme ]->colors; @@ -840,7 +754,7 @@ private static function retrieve_admin_colors() { return $admin_colors; } - public function friendly_user_name() { + function friendly_user_name() { $user = wp_get_current_user(); $name = ucfirst( empty( $user->first_name ) ? $user->display_name : $user->first_name ); return $name; @@ -856,15 +770,14 @@ public function friendly_user_name() { * * @return void */ - public function clean_attachment( $post_id ) { + function clean_attachment( $post_id ) { $tiny_image = new Tiny_Image( $this->settings, $post_id ); $tiny_image->delete_converted_image(); } - public static function request_review() { - $review_url = - 'https://wordpress.org/support/plugin/tiny-compress-images/reviews/#new-post'; - $review_block = esc_html__( 'Enjoying TinyPNG?', 'tiny-compress-images' ); + static function request_review() { + $review_url = 'https://wordpress.org/support/plugin/tiny-compress-images/reviews/#new-post'; + $review_block = esc_html__( 'Enjoying TinyPNG?', 'tiny-compress-images' ); $review_block .= ' '; $review_block .= sprintf( '%s', @@ -874,7 +787,7 @@ public static function request_review() { return $review_block; } - public function mark_image_as_compressed() { + function mark_image_as_compressed() { $response = $this->validate_ajax_attachment_request(); if ( isset( $response['error'] ) ) { echo $response['error']; @@ -882,7 +795,7 @@ public function mark_image_as_compressed() { } list($id, $metadata) = $response['data']; - $tiny_image = new Tiny_Image( $this->settings, $id, $metadata ); + $tiny_image = new Tiny_Image( $this->settings, $id, $metadata ); $tiny_image->mark_as_compressed(); echo $this->render_compress_details( $tiny_image ); diff --git a/src/views/settings-diagnostics.php b/src/views/settings-diagnostics.php index 4d768abe..48103ec9 100644 --- a/src/views/settings-diagnostics.php +++ b/src/views/settings-diagnostics.php @@ -1,15 +1,15 @@ - +

- +

> diff --git a/src/views/settings.php b/src/views/settings.php index 7e79d1a7..df06cb98 100644 --- a/src/views/settings.php +++ b/src/views/settings.php @@ -1,45 +1,45 @@

-

-

+

+

- + - + - + - + - + - +
- render_pending_status(); ?> + render_pending_status() ?>
- render_compression_timing_settings(); ?> + render_compression_timing_settings() ?>
-

+

- render_sizes(); ?> + render_sizes() ?>
-

+

- render_resize(); ?> + render_resize() ?>
- + -

+

- +

diff --git a/test/helpers/wordpress.php b/test/helpers/wordpress.php index 47ee9487..87b99c7a 100644 --- a/test/helpers/wordpress.php +++ b/test/helpers/wordpress.php @@ -102,7 +102,6 @@ public function __construct($vfs) $this->addMethod('wp_json_encode'); $this->addMethod('wp_send_json_error'); $this->addMethod('get_temp_dir'); - $this->addMethod('esc_attr'); $this->defaults(); $this->create_filesystem(); } @@ -501,16 +500,6 @@ public function wp_json_encode($data, $options = 0, $depth = 512) { return json_encode($data, $options, $depth); } - - /** - * Mocked function for https://developer.wordpress.org/reference/functions/esc_attr/ - * - * @return string - */ - public function esc_attr($text) - { - return $text; - } } class WP_HTTP_Proxy From 9390b2bbe35df4da2fccbea47b9131b6f028fa93 Mon Sep 17 00:00:00 2001 From: tijmen Date: Thu, 19 Feb 2026 14:46:25 +0100 Subject: [PATCH 3/3] fix: only clear logs when toggling from off to on --- src/class-tiny-logger.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/class-tiny-logger.php b/src/class-tiny-logger.php index 7df5ebad..ef187958 100644 --- a/src/class-tiny-logger.php +++ b/src/class-tiny-logger.php @@ -108,9 +108,13 @@ public function get_log_file_path() { * - if turn on, clear the old logs */ public static function on_save_log_enabled( $log_enabled, $old, $option ) { - $instance = self::get_instance(); - $instance->log_enabled = 'on' === $log_enabled; - if ( $instance->get_log_enabled() ) { + $instance = self::get_instance(); + $was_enabled = 'on' === $old; + $is_now_enabled = 'on' === $log_enabled; + $instance->log_enabled = $is_now_enabled; + + // Only clear logs when logging is being turned on (from off to on). + if ( ! $was_enabled && $is_now_enabled ) { self::clear_logs(); }