| @@ -1,33 +1,60 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Plugin Name: Image Optimizer by Elementor – Compress, Resize and Optimize Images | |
| 4 | - * Description: Automatically compress and enhance your images, boosting your website speed, appearance, and SEO. Get Image Optimizer and optimize your images in seconds. | |
| 5 | - * Plugin URI: https://elementor.com/ | |
| 6 | - * Version: 1.0.0 | |
| 3 | + * Plugin Name: Image Optimization - Optimize Images and Convert to WebP or AVIF | |
| 4 | + * Description: Automatically resize, optimize, and convert images to WebP and AVIF. Compress images in bulk or on upload to boost your WordPress site performance. | |
| 5 | + * Plugin URI: https://go.elementor.com/wp-repo-description-tab-io-product-page/ | |
| 6 | + * Version: 1.7.7 | |
| 7 | 7 | * Author: Elementor.com |
| 8 | - * Author URI: https://elementor.com/ | |
| 9 | - * Text Domain: image-optimizer | |
| 8 | + * Author URI: https://go.elementor.com/author-uri-io/ | |
| 9 | + * Text Domain: image-optimization | |
| 10 | 10 | * License: GPL-3 |
| 11 | 11 | * License URI: https://www.gnu.org/licenses/gpl-3.0.en.html |
| 12 | + * | |
| 13 | + * Image Optimization is free software: you can redistribute it and/or modify | |
| 14 | + * it under the terms of the GNU General Public License as published by | |
| 15 | + * the Free Software Foundation, either version 3 of the License, or | |
| 16 | + * any later version. | |
| 17 | + * | |
| 18 | + * Image Optimization is distributed in the hope that it will be useful, | |
| 19 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 20 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 21 | + * GNU General Public License for more details. | |
| 12 | 22 | */ |
| 13 | 23 | |
| 24 | +use ImageOptimization\Modules\Core\Module as CoreModule; | |
| 25 | + | |
| 14 | 26 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | 27 | exit; // Exit if accessed directly. |
| 16 | 28 | } |
| 17 | 29 | |
| 18 | -define( 'IMAGE_OPTIMIZER_VERSION', '1.0.0' ); | |
| 19 | -define( 'IMAGE_OPTIMIZER_PATH', plugin_dir_path( __FILE__ ) ); | |
| 20 | -define( 'IMAGE_OPTIMIZER_URL', plugins_url( '/', __FILE__ ) ); | |
| 21 | -define( 'IMAGE_OPTIMIZER_ASSETS_PATH', IMAGE_OPTIMIZER_PATH . 'assets/' ); | |
| 22 | -define( 'IMAGE_OPTIMIZER_ASSETS_URL', IMAGE_OPTIMIZER_URL . 'assets/' ); | |
| 23 | -define( 'IMAGE_OPTIMIZER_PLUGIN_FILE', basename( __FILE__ ) ); | |
| 30 | +define( 'IMAGE_OPTIMIZATION_VERSION', '1.7.7' ); | |
| 31 | +define( 'IMAGE_OPTIMIZATION_FILE', __FILE__ ); | |
| 32 | +define( 'IMAGE_OPTIMIZATION_PATH', plugin_dir_path( IMAGE_OPTIMIZATION_FILE ) ); | |
| 33 | +define( 'IMAGE_OPTIMIZATION_URL', plugins_url( '/', IMAGE_OPTIMIZATION_FILE ) ); | |
| 34 | +define( 'IMAGE_OPTIMIZATION_ASSETS_PATH', IMAGE_OPTIMIZATION_PATH . 'assets/' ); | |
| 35 | +define( 'IMAGE_OPTIMIZATION_ASSETS_URL', IMAGE_OPTIMIZATION_URL . 'assets/' ); | |
| 36 | +define( 'IMAGE_OPTIMIZATION_PLUGIN_FILE', plugin_basename( IMAGE_OPTIMIZATION_FILE ) ); | |
| 24 | 37 | |
| 38 | +if ( ! defined( 'IMAGE_OPTIMIZATION_MINIMUM_LOG_LEVEL' ) ) { | |
| 39 | + define( 'IMAGE_OPTIMIZATION_MINIMUM_LOG_LEVEL', 3 ); | |
| 40 | +} | |
| 41 | + | |
| 42 | +register_deactivation_hook( | |
| 43 | + __FILE__, | |
| 44 | + [ CoreModule::class, 'on_deactivation' ] | |
| 45 | +); | |
| 46 | + | |
| 25 | 47 | /** |
| 26 | - * ImageOptimizer Class | |
| 27 | - * | |
| 48 | + * ImageOptimization Class | |
| 28 | 49 | */ |
| 29 | -final class ImageOptimizer { | |
| 50 | +final class ImageOptimization { | |
| 51 | + private $requirements_errors = []; | |
| 52 | + const REQUIRED_EXTENSIONS = [ | |
| 53 | + 'exif', | |
| 54 | + 'fileinfo', | |
| 55 | + 'gd', | |
| 56 | + ]; | |
| 30 | 57 | |
| 31 | 58 | /** |
| 32 | 59 | * Constructor |
| 33 | 60 | * |
| @@ -33,10 +60,9 @@ | ||
| 33 | 60 | * |
| 34 | 61 | * @access public |
| 35 | 62 | */ |
| 36 | 63 | public function __construct() { |
| 37 | - // Load translation | |
| 38 | - add_action( 'init', [ $this, 'i18n' ] ); | |
| 64 | + require_once IMAGE_OPTIMIZATION_PATH . 'vendor/autoload.php'; | |
| 39 | 65 | |
| 40 | 66 | // Init Plugin |
| 41 | 67 | add_action( 'plugins_loaded', [ $this, 'init' ], -11 ); |
| 42 | 68 | } |
| @@ -41,35 +67,162 @@ | ||
| 41 | 67 | add_action( 'plugins_loaded', [ $this, 'init' ], -11 ); |
| 42 | 68 | } |
| 43 | 69 | |
| 44 | 70 | /** |
| 45 | - * Load Textdomain | |
| 71 | + * Checks if all requirements met to safely start the plugin and renders admin notices | |
| 72 | + * if something is not right. | |
| 46 | 73 | * |
| 47 | - * Load plugin localization files. | |
| 48 | - * Fired by `init` action hook. | |
| 74 | + * @access public | |
| 75 | + * @return bool | |
| 76 | + */ | |
| 77 | + public function plugin_can_start(): bool { | |
| 78 | + $can_start = true; | |
| 79 | + | |
| 80 | + if ( ! version_compare( PHP_VERSION, '7.4', '>=' ) ) { | |
| 81 | + $this->requirements_errors[] = sprintf( | |
| 82 | + '%1$s <a href="https://go.elementor.com/wp-dash-update-php/" target="_blank">%2$s</a>', | |
| 83 | + sprintf( | |
| 84 | + /* translators: %s: PHP version. */ | |
| 85 | + esc_html__( 'Update to PHP version %s and get back to optimizing!', 'image-optimization' ), | |
| 86 | + '7.4', | |
| 87 | + ), | |
| 88 | + esc_html__( 'Show me how', 'image-optimization' ) | |
| 89 | + ); | |
| 90 | + | |
| 91 | + $can_start = false; | |
| 92 | + } | |
| 93 | + | |
| 94 | + if ( count( $this->get_missing_extensions_list() ) ) { | |
| 95 | + $missed_extensions = $this->get_missing_extensions_list(); | |
| 96 | + | |
| 97 | + $this->requirements_errors[] = sprintf( | |
| 98 | + esc_html__( 'The following required PHP extensions are missing: %s', 'image-optimization' ), | |
| 99 | + implode( ', ', $missed_extensions ) | |
| 100 | + ); | |
| 101 | + | |
| 102 | + $can_start = false; | |
| 103 | + } | |
| 104 | + | |
| 105 | + if ( ! $this->is_db_json_supported() ) { | |
| 106 | + $this->requirements_errors[] = sprintf( | |
| 107 | + /* translators: 1: MySQL minimum version. 2: MariaDB minimum version. */ | |
| 108 | + esc_html__( | |
| 109 | + 'The database server version is outdated. Update to MySQL version %1$s or MariaDB version %2$s.', | |
| 110 | + 'image-optimization' | |
| 111 | + ), | |
| 112 | + '5.7', | |
| 113 | + '10.2' | |
| 114 | + ); | |
| 115 | + | |
| 116 | + $can_start = false; | |
| 117 | + } | |
| 118 | + | |
| 119 | + $upload_dir = wp_upload_dir(); | |
| 120 | + | |
| 121 | + if ( ! wp_is_writable( $upload_dir['basedir'] ) ) { | |
| 122 | + $this->requirements_errors[] = esc_html__( | |
| 123 | + 'Your site doesn’t have the necessary read/write permissions for your file system to use this plugin. Please contact your hosting provider to resolve this matter.', | |
| 124 | + 'image-optimization', | |
| 125 | + ); | |
| 126 | + | |
| 127 | + $can_start = false; | |
| 128 | + } | |
| 129 | + | |
| 130 | + return $can_start; | |
| 131 | + } | |
| 132 | + | |
| 133 | + /** | |
| 134 | + * Retrieve an array of missing extensions mentioned in self::REQUIRED_EXTENSIONS. | |
| 49 | 135 | * |
| 136 | + * @access private | |
| 137 | + * @return array Missing extensions. | |
| 138 | + */ | |
| 139 | + private function get_missing_extensions_list(): array { | |
| 140 | + $output = []; | |
| 141 | + | |
| 142 | + foreach ( self::REQUIRED_EXTENSIONS as $extension ) { | |
| 143 | + if ( ! extension_loaded( $extension ) ) { | |
| 144 | + $output[] = $extension; | |
| 145 | + } | |
| 146 | + } | |
| 147 | + | |
| 148 | + return $output; | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * The check is strictly based on the action scheduler requirements that needs JSON partial matching to work | |
| 153 | + * with action queries. | |
| 154 | + * | |
| 155 | + * @return bool | |
| 156 | + */ | |
| 157 | + public function is_db_json_supported(): bool { | |
| 158 | + global $wpdb; | |
| 159 | + | |
| 160 | + $db_server_info = is_callable( array( $wpdb, 'db_server_info' ) ) ? $wpdb->db_server_info() : $wpdb->db_version(); | |
| 161 | + | |
| 162 | + if ( false !== strpos( $db_server_info, 'MariaDB' ) ) { | |
| 163 | + $supports_json = version_compare( | |
| 164 | + PHP_VERSION_ID >= 80016 ? $wpdb->db_version() : preg_replace( '/[^0-9.].*/', '', str_replace( '5.5.5-', '', $db_server_info ) ), | |
| 165 | + '10.2', | |
| 166 | + '>=' | |
| 167 | + ); | |
| 168 | + } else { | |
| 169 | + $supports_json = version_compare( $wpdb->db_version(), '5.7', '>=' ); | |
| 170 | + } | |
| 171 | + | |
| 172 | + return (bool) $supports_json; | |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 176 | + * Renders an admin notice if the setup did not meet requirements. | |
| 177 | + * | |
| 178 | + * Fired by `admin_notices` action hook. | |
| 179 | + * | |
| 50 | 180 | * @access public |
| 181 | + * @return void | |
| 51 | 182 | */ |
| 52 | - public function i18n() { | |
| 53 | - load_plugin_textdomain( 'image-optimizer' ); | |
| 183 | + public function add_requirements_error() { | |
| 184 | + $message = sprintf( | |
| 185 | + '<h3>%s</h3>', | |
| 186 | + esc_html__( 'Image Optimization isn’t running because:', 'image-optimization' ) | |
| 187 | + ); | |
| 188 | + | |
| 189 | + $message .= '<ul>'; | |
| 190 | + | |
| 191 | + foreach ( $this->requirements_errors as $error ) { | |
| 192 | + $message .= sprintf( | |
| 193 | + '%s%s%s', | |
| 194 | + '<li>', | |
| 195 | + $error, | |
| 196 | + '</li>' | |
| 197 | + ); | |
| 198 | + } | |
| 199 | + | |
| 200 | + $message .= '</ul>'; | |
| 201 | + | |
| 202 | + $html_message = sprintf( '<div class="error">%s</div>', wpautop( $message ) ); | |
| 203 | + | |
| 204 | + echo wp_kses_post( $html_message ); | |
| 54 | 205 | } |
| 55 | 206 | |
| 56 | 207 | /** |
| 57 | - * Initialize the plugin | |
| 208 | + * Initialize the plugin. | |
| 58 | 209 | * |
| 59 | - * Validates that Elementor is already loaded. | |
| 60 | - * Checks for basic plugin requirements, if one check fail don't continue, | |
| 61 | - * if all check have passed include the plugin class. | |
| 210 | + * Validates that PHP version is sufficient. Checks for basic plugin requirements, | |
| 211 | + * if one check fail don't continue, if all check have passed include the plugin class. | |
| 62 | 212 | * |
| 63 | 213 | * Fired by `plugins_loaded` action hook. |
| 64 | 214 | * |
| 65 | - * @since 1.2.0 | |
| 66 | 215 | * @access public |
| 67 | 216 | */ |
| 68 | 217 | public function init() { |
| 69 | - // Once we get here, We have passed all validation checks so we can safely include our plugin | |
| 70 | - require_once 'plugin.php'; | |
| 218 | + if ( ! $this->plugin_can_start() ) { | |
| 219 | + add_action( 'admin_notices', [ $this, 'add_requirements_error' ] ); | |
| 220 | + } else { | |
| 221 | + // Once we get here, We have passed all validation checks, so we can safely include our plugin | |
| 222 | + require_once 'plugin.php'; | |
| 223 | + } | |
| 71 | 224 | } |
| 72 | 225 | } |
| 73 | 226 | |
| 74 | -// Instantiate ImageOptimizer.. | |
| 75 | -new ImageOptimizer(); | |
| 227 | +// Instantiate ImageOptimization. | |
| 228 | +new ImageOptimization(); | |