| 1 |
<?php |
| 2 |
/** |
| 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.6 |
| 7 |
* Author: Elementor.com |
| 8 |
* Author URI: https://go.elementor.com/author-uri-io/ |
| 9 |
* Text Domain: image-optimization |
| 10 |
* License: GPL-3 |
| 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. |
| 22 |
*/ |
| 23 |
|
| 24 |
use ImageOptimization\Modules\Core\Module as CoreModule; |
| 25 |
|
| 26 |
if ( ! defined( 'ABSPATH' ) ) { |
| 27 |
exit; // Exit if accessed directly. |
| 28 |
} |
| 29 |
|
| 30 |
define( 'IMAGE_OPTIMIZATION_VERSION', '1.7.6' ); |
| 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 ) ); |
| 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 |
|
| 47 |
/** |
| 48 |
* ImageOptimization Class |
| 49 |
*/ |
| 50 |
final class ImageOptimization { |
| 51 |
private $requirements_errors = []; |
| 52 |
const REQUIRED_EXTENSIONS = [ |
| 53 |
'exif', |
| 54 |
'fileinfo', |
| 55 |
'gd', |
| 56 |
]; |
| 57 |
|
| 58 |
/** |
| 59 |
* Constructor |
| 60 |
* |
| 61 |
* @access public |
| 62 |
*/ |
| 63 |
public function __construct() { |
| 64 |
require_once IMAGE_OPTIMIZATION_PATH . 'vendor/autoload.php'; |
| 65 |
|
| 66 |
// Init Plugin |
| 67 |
add_action( 'plugins_loaded', [ $this, 'init' ], -11 ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Checks if all requirements met to safely start the plugin and renders admin notices |
| 72 |
* if something is not right. |
| 73 |
* |
| 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. |
| 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 |
* |
| 180 |
* @access public |
| 181 |
* @return void |
| 182 |
*/ |
| 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 ); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Initialize the plugin. |
| 209 |
* |
| 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. |
| 212 |
* |
| 213 |
* Fired by `plugins_loaded` action hook. |
| 214 |
* |
| 215 |
* @access public |
| 216 |
*/ |
| 217 |
public function init() { |
| 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 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
// Instantiate ImageOptimization. |
| 228 |
new ImageOptimization(); |
| 229 |
|