PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.2.1
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.2.1
1.7.7 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 All 33 releases
image-optimization / image-optimization.php

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

202 lines 5.1 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 by Elementor – Compress, Resize and Optimize Images
4 * Description: Automatically compress and enhance your images, boosting your website speed, appearance, and SEO. Get Image Optimizer and optimize your images in seconds.
5 * Plugin URI: https://go.elementor.com/wp-repo-description-tab-io-product-page/
6 * Version: 1.2.1
7 * Author: Elementor.com
8 * Author URI: https://go.elementor.com/wp-repo-description-tab-io-author-url/
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.2.1' );
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( 'init', [ $this, 'i18n' ] );
44
45 // Init Plugin
46 add_action( 'plugins_loaded', [ $this, 'init' ], -11 );
47 }
48
49 /**
50 * Load Textdomain
51 *
52 * Load plugin localization files.
53 * Fired by `init` action hook.
54 *
55 * @access public
56 */
57 public function i18n() {
58 load_plugin_textdomain( 'image-optimization' );
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 * @return bool
66 */
67 public function plugin_can_start(): bool {
68 $can_start = true;
69
70 if ( ! version_compare( PHP_VERSION, '7.4', '>=' ) ) {
71 /* translators: 1: PHP version. 2: Link opening tag, 3: Link closing tag. */
72 $this->requirements_errors[] = sprintf(
73 esc_html__( 'PHP is outdated. Update to PHP version %1$s. %2$sShow me how%3$s', 'image-optimization' ),
74 '7.4',
75 '<a href="https://go.elementor.com/wp-dash-update-php/" target="_blank">',
76 '</a>'
77 );
78
79 $can_start = false;
80 }
81
82 if ( count( $this->get_missing_extensions_list() ) ) {
83 $missed_extensions = $this->get_missing_extensions_list();
84
85 $this->requirements_errors[] = sprintf(
86 esc_html__( 'The following required PHP extensions are missing: %s', 'image-optimization' ),
87 implode( ', ', $missed_extensions )
88 );
89
90 $can_start = false;
91 }
92
93 if ( ! $this->is_db_json_supported() ) {
94 $this->requirements_errors[] = sprintf(
95 /* translators: 1: MySQL minimum version. 2: MariaDB minimum version. */
96 esc_html__(
97 'The database server version is outdated. Update to MySQL version %1$s or MariaDB version %2$s',
98 'image-optimization'
99 ),
100 '5.7',
101 '10.2'
102 );
103
104 $can_start = false;
105 }
106
107 $upload_dir = wp_upload_dir();
108
109 if ( ! wp_is_writable( $upload_dir['basedir'] ) ) {
110 $this->requirements_errors[] = esc_html__(
111 '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.',
112 'image-optimization',
113 );
114
115 $can_start = false;
116 }
117
118 return $can_start;
119 }
120
121 /**
122 * Returns an array of non-loaded extensions mentioned in self::REQUIRED_EXTENSIONS.
123 *
124 * @return array Missed extensions.
125 */
126 private function get_missing_extensions_list(): array {
127 $output = [];
128
129 foreach ( self::REQUIRED_EXTENSIONS as $extension ) {
130 if ( ! extension_loaded( $extension ) ) {
131 $output[] = $extension;
132 }
133 }
134
135 return $output;
136 }
137
138 public function is_db_json_supported(): bool {
139 global $wpdb;
140
141 $result = $wpdb->query("
142 SELECT JSON_EXTRACT('[1, 2]', '$[1]');
143 ");
144
145 return false !== $result;
146 }
147
148 /**
149 * Renders an admin notice if the setup did not meet requirements.
150 *
151 * @return void
152 */
153 public function add_requirements_error() {
154 $message = sprintf(
155 /* translators: 1: `<h3>` opening tag, 2: `</h3>` closing tag */
156 esc_html__( '%1$sImage Optimizer isn’t running because:%2$s', 'image-optimization' ),
157 '<h3>',
158 '</h3>'
159 );
160
161 $message .= '<ul>';
162
163 foreach ( $this->requirements_errors as $error ) {
164 $message .= sprintf(
165 '%s%s%s',
166 '<li>',
167 $error,
168 '</li>'
169 );
170 }
171
172 $message .= '</ul>';
173
174 $html_message = sprintf( '<div class="error">%s</div>', wpautop( $message ) );
175
176 echo wp_kses_post( $html_message );
177 }
178
179 /**
180 * Initialize the plugin
181 *
182 * Validates that PHP version is sufficient.
183 * Checks for basic plugin requirements, if one check fail don't continue,
184 * if all check have passed include the plugin class.
185 *
186 * Fired by `plugins_loaded` action hook.
187 *
188 * @access public
189 */
190 public function init() {
191 if ( ! $this->plugin_can_start() ) {
192 add_action( 'admin_notices', [ $this, 'add_requirements_error' ] );
193 } else {
194 // Once we get here, We have passed all validation checks, so we can safely include our plugin
195 require_once 'plugin.php';
196 }
197 }
198 }
199
200 // Instantiate ImageOptimization..
201 new ImageOptimization();
202