image-optimization
Last commit date
assets
4 months ago
classes
4 months ago
includes
4 months ago
modules
4 months ago
vendor
4 months ago
image-optimization.php
4 months ago
index.php
2 years ago
plugin.php
2 years ago
readme.txt
4 months ago
image-optimization.php
220 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: Image Optimizer - Compress, Resize and Optimize Images |
| 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.1 |
| 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 | |
| 14 | use ImageOptimization\Modules\Core\Module as CoreModule; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; // Exit if accessed directly. |
| 18 | } |
| 19 | |
| 20 | define( 'IMAGE_OPTIMIZATION_VERSION', '1.7.1' ); |
| 21 | define( 'IMAGE_OPTIMIZATION_FILE', __FILE__ ); |
| 22 | define( 'IMAGE_OPTIMIZATION_PATH', plugin_dir_path( IMAGE_OPTIMIZATION_FILE ) ); |
| 23 | define( 'IMAGE_OPTIMIZATION_URL', plugins_url( '/', IMAGE_OPTIMIZATION_FILE ) ); |
| 24 | define( 'IMAGE_OPTIMIZATION_ASSETS_PATH', IMAGE_OPTIMIZATION_PATH . 'assets/' ); |
| 25 | define( 'IMAGE_OPTIMIZATION_ASSETS_URL', IMAGE_OPTIMIZATION_URL . 'assets/' ); |
| 26 | define( 'IMAGE_OPTIMIZATION_PLUGIN_FILE', plugin_basename( IMAGE_OPTIMIZATION_FILE ) ); |
| 27 | |
| 28 | if ( ! defined( 'IMAGE_OPTIMIZATION_MINIMUM_LOG_LEVEL' ) ) { |
| 29 | define( 'IMAGE_OPTIMIZATION_MINIMUM_LOG_LEVEL', 3 ); |
| 30 | } |
| 31 | |
| 32 | register_deactivation_hook( |
| 33 | __FILE__, |
| 34 | [ CoreModule::class, 'on_deactivation' ] |
| 35 | ); |
| 36 | |
| 37 | /** |
| 38 | * ImageOptimization Class |
| 39 | */ |
| 40 | final class ImageOptimization { |
| 41 | private $requirements_errors = []; |
| 42 | const REQUIRED_EXTENSIONS = [ |
| 43 | 'exif', |
| 44 | 'fileinfo', |
| 45 | 'gd', |
| 46 | ]; |
| 47 | |
| 48 | /** |
| 49 | * Constructor |
| 50 | * |
| 51 | * @access public |
| 52 | */ |
| 53 | public function __construct() { |
| 54 | |
| 55 | require_once IMAGE_OPTIMIZATION_PATH . 'vendor/autoload.php'; |
| 56 | |
| 57 | // Init Plugin |
| 58 | add_action( 'plugins_loaded', [ $this, 'init' ], -11 ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Checks if all requirements met to safely start the plugin and renders admin notices |
| 63 | * if something is not right. |
| 64 | * |
| 65 | * @access public |
| 66 | * @return bool |
| 67 | */ |
| 68 | public function plugin_can_start(): bool { |
| 69 | $can_start = true; |
| 70 | |
| 71 | if ( ! version_compare( PHP_VERSION, '7.4', '>=' ) ) { |
| 72 | $this->requirements_errors[] = sprintf( |
| 73 | '%1$s <a href="https://go.elementor.com/wp-dash-update-php/" target="_blank">%2$s</a>', |
| 74 | sprintf( |
| 75 | /* translators: %s: PHP version. */ |
| 76 | esc_html__( 'Update to PHP version %s and get back to optimizing!', 'image-optimization' ), |
| 77 | '7.4', |
| 78 | ), |
| 79 | esc_html__( 'Show me how', 'image-optimization' ) |
| 80 | ); |
| 81 | |
| 82 | $can_start = false; |
| 83 | } |
| 84 | |
| 85 | if ( count( $this->get_missing_extensions_list() ) ) { |
| 86 | $missed_extensions = $this->get_missing_extensions_list(); |
| 87 | |
| 88 | $this->requirements_errors[] = sprintf( |
| 89 | esc_html__( 'The following required PHP extensions are missing: %s', 'image-optimization' ), |
| 90 | implode( ', ', $missed_extensions ) |
| 91 | ); |
| 92 | |
| 93 | $can_start = false; |
| 94 | } |
| 95 | |
| 96 | if ( ! $this->is_db_json_supported() ) { |
| 97 | $this->requirements_errors[] = sprintf( |
| 98 | /* translators: 1: MySQL minimum version. 2: MariaDB minimum version. */ |
| 99 | esc_html__( |
| 100 | 'The database server version is outdated. Update to MySQL version %1$s or MariaDB version %2$s.', |
| 101 | 'image-optimization' |
| 102 | ), |
| 103 | '5.7', |
| 104 | '10.2' |
| 105 | ); |
| 106 | |
| 107 | $can_start = false; |
| 108 | } |
| 109 | |
| 110 | $upload_dir = wp_upload_dir(); |
| 111 | |
| 112 | if ( ! wp_is_writable( $upload_dir['basedir'] ) ) { |
| 113 | $this->requirements_errors[] = esc_html__( |
| 114 | '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.', |
| 115 | 'image-optimization', |
| 116 | ); |
| 117 | |
| 118 | $can_start = false; |
| 119 | } |
| 120 | |
| 121 | return $can_start; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Retrieve an array of missing extensions mentioned in self::REQUIRED_EXTENSIONS. |
| 126 | * |
| 127 | * @access private |
| 128 | * @return array Missing extensions. |
| 129 | */ |
| 130 | private function get_missing_extensions_list(): array { |
| 131 | $output = []; |
| 132 | |
| 133 | foreach ( self::REQUIRED_EXTENSIONS as $extension ) { |
| 134 | if ( ! extension_loaded( $extension ) ) { |
| 135 | $output[] = $extension; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return $output; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * The check is strictly based on the action scheduler requirements that needs JSON partial matching to work |
| 144 | * with action queries. |
| 145 | * |
| 146 | * @return bool |
| 147 | */ |
| 148 | public function is_db_json_supported(): bool { |
| 149 | global $wpdb; |
| 150 | |
| 151 | $db_server_info = is_callable( array( $wpdb, 'db_server_info' ) ) ? $wpdb->db_server_info() : $wpdb->db_version(); |
| 152 | |
| 153 | if ( false !== strpos( $db_server_info, 'MariaDB' ) ) { |
| 154 | $supports_json = version_compare( |
| 155 | PHP_VERSION_ID >= 80016 ? $wpdb->db_version() : preg_replace( '/[^0-9.].*/', '', str_replace( '5.5.5-', '', $db_server_info ) ), |
| 156 | '10.2', |
| 157 | '>=' |
| 158 | ); |
| 159 | } else { |
| 160 | $supports_json = version_compare( $wpdb->db_version(), '5.7', '>=' ); |
| 161 | } |
| 162 | |
| 163 | return (bool) $supports_json; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Renders an admin notice if the setup did not meet requirements. |
| 168 | * |
| 169 | * Fired by `admin_notices` action hook. |
| 170 | * |
| 171 | * @access public |
| 172 | * @return void |
| 173 | */ |
| 174 | public function add_requirements_error() { |
| 175 | $message = sprintf( |
| 176 | '<h3>%s</h3>', |
| 177 | esc_html__( 'Image Optimizer isn’t running because:', 'image-optimization' ) |
| 178 | ); |
| 179 | |
| 180 | $message .= '<ul>'; |
| 181 | |
| 182 | foreach ( $this->requirements_errors as $error ) { |
| 183 | $message .= sprintf( |
| 184 | '%s%s%s', |
| 185 | '<li>', |
| 186 | $error, |
| 187 | '</li>' |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | $message .= '</ul>'; |
| 192 | |
| 193 | $html_message = sprintf( '<div class="error">%s</div>', wpautop( $message ) ); |
| 194 | |
| 195 | echo wp_kses_post( $html_message ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Initialize the plugin. |
| 200 | * |
| 201 | * Validates that PHP version is sufficient. Checks for basic plugin requirements, |
| 202 | * if one check fail don't continue, if all check have passed include the plugin class. |
| 203 | * |
| 204 | * Fired by `plugins_loaded` action hook. |
| 205 | * |
| 206 | * @access public |
| 207 | */ |
| 208 | public function init() { |
| 209 | if ( ! $this->plugin_can_start() ) { |
| 210 | add_action( 'admin_notices', [ $this, 'add_requirements_error' ] ); |
| 211 | } else { |
| 212 | // Once we get here, We have passed all validation checks, so we can safely include our plugin |
| 213 | require_once 'plugin.php'; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Instantiate ImageOptimization. |
| 219 | new ImageOptimization(); |
| 220 |