PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.6.8
Image Optimization – Compress Images and Convert to WebP or AVIF v1.6.8
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.8, at image-optimization.php

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