PluginProbe
Squeeze – Image Optimization & Compression, WEBP Conversion / 1.7.15
Squeeze – Image Optimization & Compression, WEBP Conversion v1.7.15
1.7.15 1.7.14 1.7.13 1.7.12 1.7.11 1.7.10 trunk 1.0 1.1 1.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5 1.5.1 1.5.2 1.6 All 42 releases
squeeze / squeeze.php

squeeze.php in Squeeze – Image Optimization & Compression, WEBP Conversion 1.7.15, at squeeze.php

427 lines 16.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Name: Squeeze – Image Optimization & Compression, WebP Conversion
5 * Description: Compress unlimited images directly into your browser. Convert images to WebP format. No limits on file size or number of images. No third-party services or API keys required.
6 * Author URI: https://pluginarium.com
7 * Author: Bogdan Bendziukov
8 * Version: 1.7.15
9 *
10 * Text Domain: squeeze
11 * Domain Path: /languages
12 *
13 * License: GPLv3
14 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
15 *
16 *
17 */
18 namespace SqueezeFree;
19
20 // Exit if accessed directly.
21 if ( !defined( 'ABSPATH' ) ) {
22 exit;
23 }
24 class SqueezeInit {
25 /**
26 * Plugin version
27 */
28 const VERSION = '1.7.15';
29
30 const CHECKOUT_URL = 'https://checkout.freemius.com/plugin/17217/plan/28703/?utm_source=wordpress_plugin&utm_medium=admin&utm_campaign=squeeze_upgrade&utm_content=settings_upgrade_tab';
31
32 /**
33 * Allowed image formats
34 */
35 const ALLOWED_IMAGE_FORMATS = array(
36 'jpg' => 'JPG/JPEG',
37 'png' => 'PNG',
38 'webp' => 'WebP',
39 'avif' => 'AVIF',
40 );
41
42 // Array containing dynamic data for a JS Global.
43 public static $LOCALIZE_ARGS;
44
45 public static $JS_OPTIONS = array();
46
47 public static $SqueezeHelpers;
48
49 public static $SqueezeSettings;
50
51 public static $SqueezeHandlers;
52
53 public static $SqueezePremium;
54
55 public static $SqueezeReviewNotice;
56
57 public static $UPGRADE_URL;
58
59 public static $SETTINGS_URL;
60
61 /**
62 * Media per page
63 */
64 public static $MEDIA_PER_PAGE;
65
66 /**
67 * Plugin directory
68 */
69 public static $PLUGIN_DIR;
70
71 /**
72 * Plugin URL
73 */
74 public static $PLUGIN_URL;
75
76 public static $DOCS_URL = 'https://pluginarium.com/squeeze/squeeze-documentation/?utm_source=wordpress_plugin&utm_medium=admin&utm_campaign=squeeze_docs&utm_content=docs_tab';
77
78 /**
79 * Initialize the plugin
80 */
81 public function __construct() {
82 self::$PLUGIN_DIR = plugin_dir_path( __FILE__ );
83 self::$PLUGIN_URL = plugin_dir_url( __FILE__ );
84 self::$MEDIA_PER_PAGE = apply_filters( 'squeeze_media_per_page', 50 );
85 self::$UPGRADE_URL = admin_url( 'options-general.php?page=squeeze#squeeze_upgrade' );
86 self::$SETTINGS_URL = admin_url( 'options-general.php?page=squeeze' );
87 $this->load_helpers();
88 $this->load_handlers();
89 $this->load_settings();
90 $this->load_review_notice();
91 self::$SqueezeHelpers = new SqueezeHelpers();
92 self::$SqueezeSettings = new SqueezeSettings();
93 self::$SqueezeHandlers = new SqueezeHandlers();
94 self::$SqueezeReviewNotice = new SqueezeReviewNotice();
95 $this->maybe_disable_client_side_media_processing();
96 // Defer compat module loading until after all plugins have initialised.
97 // If load_compat() runs in __construct() the WP Offload Media class may not
98 // exist yet (plugins are loaded in file-system order), so is_active() returns
99 // false and none of the sync / URL-override hooks get registered.
100 add_action( 'plugins_loaded', array($this, 'load_compat'), 20 );
101 add_action( 'init', [$this, 'prepare_localize_args'] );
102 add_action( 'plugins_loaded', array($this, 'load_textdomain') );
103 add_action( 'admin_enqueue_scripts', array($this, 'load_assets') );
104 add_action( 'wp_enqueue_scripts', array($this, 'load_voxel_frontend_assets'), 25 );
105 add_filter(
106 'plugin_action_links',
107 array($this, 'plugin_action_links'),
108 10,
109 2
110 );
111 add_action( 'enqueue_block_editor_assets', array($this, 'load_editor_assets') );
112 add_action( 'squeeze_freemius_loaded', array($this, 'load_freemius') );
113 register_activation_hook( __FILE__, array($this, 'activation_actions') );
114 add_action( 'admin_init', array($this, 'maybe_redirect_to_bulk_page') );
115 $this->register_uninstall_cleanup();
116 }
117
118 /**
119 * Register a single uninstall cleanup entry point.
120 * Freemius owns register_uninstall_hook when present, so premium uses fs_after_uninstall_squeeze.
121 * Free builds without Freemius use the native WordPress uninstall hook.
122 */
123 private function register_uninstall_cleanup() {
124 $callback = array(__NAMESPACE__ . '\\SqueezeHelpers', 'uninstall_cleanup');
125 if ( file_exists( self::$PLUGIN_DIR . 'freemius/start.php' ) ) {
126 add_action( 'fs_after_uninstall_squeeze', $callback );
127 return;
128 }
129 register_uninstall_hook( __FILE__, $callback );
130 }
131
132 /**
133 * Turn off WordPress 7.1+ client-side media processing while Squeeze on-upload is enabled.
134 *
135 * Core CSMP encodes in the browser then sideloads thumbs and finalize — that double-encodes
136 * Squeeze uploads and races thumbnail overwrite. Users who want HEIC conversion / HDR thumbs
137 * from core can disable Squeeze on upload; bulk and manual squeeze still work.
138 */
139 public function maybe_disable_client_side_media_processing() {
140 if ( !function_exists( 'wp_is_client_side_media_processing_enabled' ) ) {
141 return;
142 }
143 if ( !self::$SqueezeHelpers->get_option( 'auto_compress' ) ) {
144 return;
145 }
146 add_filter( 'wp_client_side_media_processing_enabled', '__return_false' );
147 }
148
149 /**
150 * Load plugin textdomain
151 */
152 public function load_textdomain() {
153 load_plugin_textdomain( 'squeeze', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
154 }
155
156 public function load_freemius() {
157 }
158
159 public function opt_in_freemius() {
160 }
161
162 public function freemius_admin_notices() {
163 }
164
165 public function prepare_localize_args() {
166 $options = get_option( 'squeeze_options' );
167 $default_options = self::$SqueezeHelpers->get_default_value( null, true );
168 // get all default values
169 $js_options = array();
170 foreach ( $default_options as $key => $value ) {
171 if ( isset( $options[$key] ) ) {
172 if ( is_numeric( $options[$key] ) ) {
173 $js_options[$key] = floatval( $options[$key] );
174 } elseif ( $options[$key] === "on" ) {
175 $js_options[$key] = true;
176 } elseif ( $key === "compress_thumbs" ) {
177 $js_options[$key] = $options[$key];
178 } else {
179 $js_options[$key] = $options[$key];
180 //echo $key . ': ' . $js_options[$key] . ',' . $options[$key] . '<br>';
181 }
182 } else {
183 $js_options[$key] = $value;
184 }
185 }
186 self::$JS_OPTIONS = $js_options;
187 self::$LOCALIZE_ARGS = array(
188 'isPremium' => false,
189 'pluginUrl' => self::$PLUGIN_URL,
190 'upgradeUrl' => self::$UPGRADE_URL,
191 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
192 'nonce' => wp_create_nonce( 'squeeze-nonce' ),
193 'restNonce' => wp_create_nonce( 'wp_rest' ),
194 'options' => wp_json_encode( self::$JS_OPTIONS ),
195 'templateBase' => self::$PLUGIN_URL . 'assets/templates/',
196 'templates' => array(
197 'logWrapper' => self::$PLUGIN_URL . 'assets/templates/log-wrapper.html',
198 'logStep' => self::$PLUGIN_URL . 'assets/templates/log-step.html',
199 'logDetailsButton' => self::$PLUGIN_URL . 'assets/templates/log-details-button.html',
200 'directoryItem' => self::$PLUGIN_URL . 'assets/templates/directory-item.html',
201 'directoryItemEmpty' => self::$PLUGIN_URL . 'assets/templates/directory-item-empty.html',
202 'pathListItem' => self::$PLUGIN_URL . 'assets/templates/path-list-item.html',
203 'previewSqueeze' => self::$PLUGIN_URL . 'assets/templates/preview-squeeze.html',
204 ),
205 'settingsPageUrl' => admin_url( 'options-general.php?page=squeeze' ),
206 );
207 }
208
209 public function load_settings() {
210 require_once self::$PLUGIN_DIR . 'inc/settings.php';
211 }
212
213 public function load_handlers() {
214 require_once self::$PLUGIN_DIR . 'inc/handlers.php';
215 }
216
217 public function load_helpers() {
218 require_once self::$PLUGIN_DIR . 'inc/helpers.php';
219 }
220
221 public function load_review_notice() {
222 require_once self::$PLUGIN_DIR . 'inc/review-notice.php';
223 }
224
225 /**
226 * Load third-party plugin compatibility modules.
227 * Each module is self-contained and checks whether the target plugin is active
228 * before registering any hooks.
229 */
230 public function load_compat() {
231 // WP Offload Media (amazon-s3-and-cloudfront) — push WebP files to external storage.
232 require_once self::$PLUGIN_DIR . 'inc/compat/offload-media.php';
233 new SqueezeOffloadMedia();
234 }
235
236 /**
237 * Script dependencies: admin needs media/plupload; frontend (Voxel) only needs i18n + domReady.
238 *
239 * @return string[]
240 */
241 protected function get_squeeze_script_dependencies() {
242 if ( is_admin() ) {
243 return array(
244 'jquery',
245 'jquery-ui-core',
246 'wp-mediaelement',
247 'wp-i18n',
248 'wp-dom-ready'
249 );
250 }
251 return array('jquery', 'wp-i18n', 'wp-dom-ready');
252 }
253
254 /**
255 * Whether the active theme (or parent) is Voxel.
256 */
257 protected function is_voxel_theme_active() {
258 $theme = wp_get_theme();
259 $slug = $theme->get_template();
260 $active = is_string( $slug ) && strtolower( $slug ) === 'voxel';
261 /**
262 * @since 1.7.8
263 * @param bool $active Whether the active parent theme is Voxel.
264 * @param string $slug Parent theme directory slug (stylesheet template).
265 */
266 return (bool) apply_filters( 'squeeze_is_voxel_theme', $active, ( is_string( $slug ) ? $slug : '' ) );
267 }
268
269 /**
270 * Voxel create-post / file fields upload via multipart FormData to admin-ajax.php, not wp.Uploader.
271 * Load Squeeze on the frontend so client-side compression can run for logged-in users who can upload.
272 */
273 public function load_voxel_frontend_assets() {
274 static $voxel_squeeze_enqueued = false;
275 if ( $voxel_squeeze_enqueued ) {
276 return;
277 }
278 if ( !$this->is_voxel_theme_active() ) {
279 return;
280 }
281 if ( !is_user_logged_in() || !current_user_can( 'upload_files' ) ) {
282 return;
283 }
284 wp_enqueue_script(
285 'squeeze-script',
286 self::$PLUGIN_URL . 'assets/js/script.bundle.js',
287 $this->get_squeeze_script_dependencies(),
288 self::VERSION,
289 true
290 );
291 wp_localize_script( 'squeeze-script', 'squeezeOptions', self::$LOCALIZE_ARGS );
292 wp_set_script_translations( 'squeeze-script', 'squeeze', self::$PLUGIN_DIR . 'languages' );
293 $voxel_squeeze_enqueued = true;
294 }
295
296 /**
297 * Enqueue assets
298 */
299 public function load_assets() {
300 global $pagenow;
301 if ( !wp_script_is( 'media-editor', 'enqueued' ) ) {
302 wp_enqueue_media();
303 }
304 // Enqueue script for backend.
305 wp_enqueue_script(
306 'squeeze-script',
307 self::$PLUGIN_URL . 'assets/js/script.bundle.js',
308 $this->get_squeeze_script_dependencies(),
309 self::VERSION,
310 true
311 );
312 // WP Localized globals. Use dynamic PHP stuff in JavaScript via `squeeze` object.
313 wp_localize_script( 'squeeze-script', 'squeezeOptions', self::$LOCALIZE_ARGS );
314 if ( $pagenow === 'upload.php' && isset( $_GET['page'] ) && $_GET['page'] === 'squeeze-bulk' ) {
315 wp_localize_script( 'squeeze-script', 'squeezeBulk', [
316 'allImages' => implode( ",", self::$SqueezeHelpers->get_total_images() ),
317 'unCompressedImages' => implode( ",", self::$SqueezeHelpers->get_uncompressed_images() ),
318 ] );
319 }
320 // Check if we are on the options page for the plugin
321 if ( $pagenow === 'upload.php' || $pagenow === 'options-general.php' && isset( $_GET['page'] ) && $_GET['page'] === 'squeeze' ) {
322 wp_enqueue_script(
323 'squeeze-settings-script',
324 self::$PLUGIN_URL . 'assets/js/admin.bundle.js',
325 array('jquery'),
326 self::VERSION,
327 true
328 );
329 }
330 wp_set_script_translations( 'squeeze-script', 'squeeze', self::$PLUGIN_DIR . 'languages' );
331 // Enqueue styles for backend.
332 wp_enqueue_style(
333 'squeeze-style',
334 self::$PLUGIN_URL . 'assets/css/admin.css',
335 array(),
336 self::VERSION
337 );
338 wp_localize_script( 'squeeze-editor-script', 'squeezeOptions', array(
339 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
340 'nonce' => wp_create_nonce( 'squeeze-nonce' ),
341 'options' => wp_json_encode( self::$JS_OPTIONS ),
342 ) );
343 }
344
345 public function load_elementor_assets() {
346 }
347
348 public function load_beaver_assets() {
349 }
350
351 public function load_editor_assets() {
352 // Enqueue scripts for the editor.
353 wp_enqueue_script(
354 'squeeze-editor-script',
355 self::$PLUGIN_URL . 'assets/js/editor.bundle.js',
356 array(
357 'wp-blocks',
358 'wp-hooks',
359 'wp-compose',
360 'wp-element',
361 'wp-data',
362 'wp-block-editor',
363 'wp-api-fetch'
364 ),
365 self::VERSION,
366 true
367 );
368 }
369
370 /**
371 * Add settings link on plugin page
372 *
373 * @param array $links
374 * @return array
375 */
376 public function plugin_action_links( $actions, $plugin_file ) {
377 static $plugin;
378 if ( !current_user_can( 'manage_options' ) ) {
379 return $actions;
380 }
381 if ( !isset( $plugin ) ) {
382 $plugin = plugin_basename( __FILE__ );
383 }
384 if ( $plugin === $plugin_file ) {
385 $settings_link = array(
386 'settings' => '<a href="' . admin_url( 'options-general.php?page=squeeze' ) . '">' . __( 'Settings', 'squeeze' ) . '</a>',
387 );
388 $bulk_link = array(
389 'bulk' => '<a href="' . admin_url( 'upload.php?page=squeeze-bulk' ) . '">' . __( 'Bulk / Directory Squeeze', 'squeeze' ) . '</a>',
390 );
391 $docs_link = array(
392 'docs' => '<a target="_blank" href="' . esc_url( self::$DOCS_URL ) . '">' . __( 'Documentation', 'squeeze' ) . '</a>',
393 );
394 $actions['upgrade'] = '<a href="' . esc_url( self::$UPGRADE_URL ) . '">' . esc_html__( 'Go Premium', 'squeeze' ) . '</a>';
395 $actions = array_merge( $docs_link, $actions );
396 $actions = array_merge( $bulk_link, $actions );
397 $actions = array_merge( $settings_link, $actions );
398 }
399 return $actions;
400 }
401
402 public function activation_actions() {
403 // Only for single-site activation
404 if ( !is_network_admin() && !isset( $_GET['activate-multi'] ) ) {
405 add_option( 'squeeze_do_activation_redirect', true );
406 }
407 }
408
409 public function maybe_redirect_to_bulk_page() {
410 if ( get_option( 'squeeze_do_activation_redirect', false ) ) {
411 // Delete the flag so it happens only once
412 delete_option( 'squeeze_do_activation_redirect' );
413 // Skip redirection during bulk or network activation
414 // When you activate several plugins at once (for example, from the Plugins screen using the bulk “Activate” action),
415 // WordPress adds ?activate-multi=true to the URL.
416 if ( isset( $_GET['activate-multi'] ) ) {
417 return;
418 }
419 // Redirect to your custom bulk optimization page
420 wp_safe_redirect( admin_url( 'upload.php?page=squeeze-bulk' ) );
421 exit;
422 }
423 }
424
425 }
426
427 new SqueezeInit();