PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.6.6
Image Optimization – Compress Images and Convert to WebP or AVIF v1.6.6
1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 All 32 releases
image-optimization / image-optimization.php

image-optimization.php in Image Optimization – Compress Images and Convert to WebP or AVIF 1.6.6, at image-optimization.php

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