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