PluginProbe
Squeeze – Image Optimization & Compression, WEBP Conversion / 1.7.13
Squeeze – Image Optimization & Compression, WEBP Conversion v1.7.13
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.13, at squeeze.php

409 lines 15.4 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.13
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.13';
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 // Defer compat module loading until after all plugins have initialised.
96 // If load_compat() runs in __construct() the WP Offload Media class may not
97 // exist yet (plugins are loaded in file-system order), so is_active() returns
98 // false and none of the sync / URL-override hooks get registered.
99 add_action( 'plugins_loaded', array($this, 'load_compat'), 20 );
100 add_action( 'init', [$this, 'prepare_localize_args'] );
101 add_action( 'plugins_loaded', array($this, 'load_textdomain') );
102 add_action( 'admin_enqueue_scripts', array($this, 'load_assets') );
103 add_action( 'wp_enqueue_scripts', array($this, 'load_voxel_frontend_assets'), 25 );
104 add_filter(
105 'plugin_action_links',
106 array($this, 'plugin_action_links'),
107 10,
108 2
109 );
110 add_action( 'enqueue_block_editor_assets', array($this, 'load_editor_assets') );
111 add_action( 'squeeze_freemius_loaded', array($this, 'load_freemius') );
112 register_activation_hook( __FILE__, array($this, 'activation_actions') );
113 add_action( 'admin_init', array($this, 'maybe_redirect_to_bulk_page') );
114 $this->register_uninstall_cleanup();
115 }
116
117 /**
118 * Register a single uninstall cleanup entry point.
119 * Freemius owns register_uninstall_hook when present, so premium uses fs_after_uninstall_squeeze.
120 * Free builds without Freemius use the native WordPress uninstall hook.
121 */
122 private function register_uninstall_cleanup() {
123 $callback = array(__NAMESPACE__ . '\\SqueezeHelpers', 'uninstall_cleanup');
124 if ( file_exists( self::$PLUGIN_DIR . 'freemius/start.php' ) ) {
125 add_action( 'fs_after_uninstall_squeeze', $callback );
126 return;
127 }
128 register_uninstall_hook( __FILE__, $callback );
129 }
130
131 /**
132 * Load plugin textdomain
133 */
134 public function load_textdomain() {
135 load_plugin_textdomain( 'squeeze', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
136 }
137
138 public function load_freemius() {
139 }
140
141 public function opt_in_freemius() {
142 }
143
144 public function freemius_admin_notices() {
145 }
146
147 public function prepare_localize_args() {
148 $options = get_option( 'squeeze_options' );
149 $default_options = self::$SqueezeHelpers->get_default_value( null, true );
150 // get all default values
151 $js_options = array();
152 foreach ( $default_options as $key => $value ) {
153 if ( isset( $options[$key] ) ) {
154 if ( is_numeric( $options[$key] ) ) {
155 $js_options[$key] = floatval( $options[$key] );
156 } elseif ( $options[$key] === "on" ) {
157 $js_options[$key] = true;
158 } elseif ( $key === "compress_thumbs" ) {
159 $js_options[$key] = $options[$key];
160 } else {
161 $js_options[$key] = $options[$key];
162 //echo $key . ': ' . $js_options[$key] . ',' . $options[$key] . '<br>';
163 }
164 } else {
165 $js_options[$key] = $value;
166 }
167 }
168 self::$JS_OPTIONS = $js_options;
169 self::$LOCALIZE_ARGS = array(
170 'isPremium' => false,
171 'pluginUrl' => self::$PLUGIN_URL,
172 'upgradeUrl' => self::$UPGRADE_URL,
173 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
174 'nonce' => wp_create_nonce( 'squeeze-nonce' ),
175 'restNonce' => wp_create_nonce( 'wp_rest' ),
176 'options' => wp_json_encode( self::$JS_OPTIONS ),
177 'templateBase' => self::$PLUGIN_URL . 'assets/templates/',
178 'templates' => array(
179 'logWrapper' => self::$PLUGIN_URL . 'assets/templates/log-wrapper.html',
180 'logStep' => self::$PLUGIN_URL . 'assets/templates/log-step.html',
181 'logDetailsButton' => self::$PLUGIN_URL . 'assets/templates/log-details-button.html',
182 'directoryItem' => self::$PLUGIN_URL . 'assets/templates/directory-item.html',
183 'directoryItemEmpty' => self::$PLUGIN_URL . 'assets/templates/directory-item-empty.html',
184 'pathListItem' => self::$PLUGIN_URL . 'assets/templates/path-list-item.html',
185 'previewSqueeze' => self::$PLUGIN_URL . 'assets/templates/preview-squeeze.html',
186 ),
187 'settingsPageUrl' => admin_url( 'options-general.php?page=squeeze' ),
188 );
189 }
190
191 public function load_settings() {
192 require_once self::$PLUGIN_DIR . 'inc/settings.php';
193 }
194
195 public function load_handlers() {
196 require_once self::$PLUGIN_DIR . 'inc/handlers.php';
197 }
198
199 public function load_helpers() {
200 require_once self::$PLUGIN_DIR . 'inc/helpers.php';
201 }
202
203 public function load_review_notice() {
204 require_once self::$PLUGIN_DIR . 'inc/review-notice.php';
205 }
206
207 /**
208 * Load third-party plugin compatibility modules.
209 * Each module is self-contained and checks whether the target plugin is active
210 * before registering any hooks.
211 */
212 public function load_compat() {
213 // WP Offload Media (amazon-s3-and-cloudfront) — push WebP files to external storage.
214 require_once self::$PLUGIN_DIR . 'inc/compat/offload-media.php';
215 new SqueezeOffloadMedia();
216 }
217
218 /**
219 * Script dependencies: admin needs media/plupload; frontend (Voxel) only needs i18n + domReady.
220 *
221 * @return string[]
222 */
223 protected function get_squeeze_script_dependencies() {
224 if ( is_admin() ) {
225 return array(
226 'jquery',
227 'jquery-ui-core',
228 'wp-mediaelement',
229 'wp-i18n',
230 'wp-dom-ready'
231 );
232 }
233 return array('jquery', 'wp-i18n', 'wp-dom-ready');
234 }
235
236 /**
237 * Whether the active theme (or parent) is Voxel.
238 */
239 protected function is_voxel_theme_active() {
240 $theme = wp_get_theme();
241 $slug = $theme->get_template();
242 $active = is_string( $slug ) && strtolower( $slug ) === 'voxel';
243 /**
244 * @since 1.7.8
245 * @param bool $active Whether the active parent theme is Voxel.
246 * @param string $slug Parent theme directory slug (stylesheet template).
247 */
248 return (bool) apply_filters( 'squeeze_is_voxel_theme', $active, ( is_string( $slug ) ? $slug : '' ) );
249 }
250
251 /**
252 * Voxel create-post / file fields upload via multipart FormData to admin-ajax.php, not wp.Uploader.
253 * Load Squeeze on the frontend so client-side compression can run for logged-in users who can upload.
254 */
255 public function load_voxel_frontend_assets() {
256 static $voxel_squeeze_enqueued = false;
257 if ( $voxel_squeeze_enqueued ) {
258 return;
259 }
260 if ( !$this->is_voxel_theme_active() ) {
261 return;
262 }
263 if ( !is_user_logged_in() || !current_user_can( 'upload_files' ) ) {
264 return;
265 }
266 wp_enqueue_script(
267 'squeeze-script',
268 self::$PLUGIN_URL . 'assets/js/script.bundle.js',
269 $this->get_squeeze_script_dependencies(),
270 self::VERSION,
271 true
272 );
273 wp_localize_script( 'squeeze-script', 'squeezeOptions', self::$LOCALIZE_ARGS );
274 wp_set_script_translations( 'squeeze-script', 'squeeze', self::$PLUGIN_DIR . 'languages' );
275 $voxel_squeeze_enqueued = true;
276 }
277
278 /**
279 * Enqueue assets
280 */
281 public function load_assets() {
282 global $pagenow;
283 if ( !wp_script_is( 'media-editor', 'enqueued' ) ) {
284 wp_enqueue_media();
285 }
286 // Enqueue script for backend.
287 wp_enqueue_script(
288 'squeeze-script',
289 self::$PLUGIN_URL . 'assets/js/script.bundle.js',
290 $this->get_squeeze_script_dependencies(),
291 self::VERSION,
292 true
293 );
294 // WP Localized globals. Use dynamic PHP stuff in JavaScript via `squeeze` object.
295 wp_localize_script( 'squeeze-script', 'squeezeOptions', self::$LOCALIZE_ARGS );
296 if ( $pagenow === 'upload.php' && isset( $_GET['page'] ) && $_GET['page'] === 'squeeze-bulk' ) {
297 wp_localize_script( 'squeeze-script', 'squeezeBulk', [
298 'allImages' => implode( ",", self::$SqueezeHelpers->get_total_images() ),
299 'unCompressedImages' => implode( ",", self::$SqueezeHelpers->get_uncompressed_images() ),
300 ] );
301 }
302 // Check if we are on the options page for the plugin
303 if ( $pagenow === 'upload.php' || $pagenow === 'options-general.php' && isset( $_GET['page'] ) && $_GET['page'] === 'squeeze' ) {
304 wp_enqueue_script(
305 'squeeze-settings-script',
306 self::$PLUGIN_URL . 'assets/js/admin.bundle.js',
307 array('jquery'),
308 self::VERSION,
309 true
310 );
311 }
312 wp_set_script_translations( 'squeeze-script', 'squeeze', self::$PLUGIN_DIR . 'languages' );
313 // Enqueue styles for backend.
314 wp_enqueue_style(
315 'squeeze-style',
316 self::$PLUGIN_URL . 'assets/css/admin.css',
317 array(),
318 self::VERSION
319 );
320 wp_localize_script( 'squeeze-editor-script', 'squeezeOptions', array(
321 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
322 'nonce' => wp_create_nonce( 'squeeze-nonce' ),
323 'options' => wp_json_encode( self::$JS_OPTIONS ),
324 ) );
325 }
326
327 public function load_elementor_assets() {
328 }
329
330 public function load_beaver_assets() {
331 }
332
333 public function load_editor_assets() {
334 // Enqueue scripts for the editor.
335 wp_enqueue_script(
336 'squeeze-editor-script',
337 self::$PLUGIN_URL . 'assets/js/editor.bundle.js',
338 array(
339 'wp-blocks',
340 'wp-hooks',
341 'wp-compose',
342 'wp-element',
343 'wp-data',
344 'wp-block-editor',
345 'wp-api-fetch'
346 ),
347 self::VERSION,
348 true
349 );
350 }
351
352 /**
353 * Add settings link on plugin page
354 *
355 * @param array $links
356 * @return array
357 */
358 public function plugin_action_links( $actions, $plugin_file ) {
359 static $plugin;
360 if ( !current_user_can( 'manage_options' ) ) {
361 return $actions;
362 }
363 if ( !isset( $plugin ) ) {
364 $plugin = plugin_basename( __FILE__ );
365 }
366 if ( $plugin === $plugin_file ) {
367 $settings_link = array(
368 'settings' => '<a href="' . admin_url( 'options-general.php?page=squeeze' ) . '">' . __( 'Settings', 'squeeze' ) . '</a>',
369 );
370 $bulk_link = array(
371 'bulk' => '<a href="' . admin_url( 'upload.php?page=squeeze-bulk' ) . '">' . __( 'Bulk / Directory Squeeze', 'squeeze' ) . '</a>',
372 );
373 $docs_link = array(
374 'docs' => '<a target="_blank" href="' . esc_url( self::$DOCS_URL ) . '">' . __( 'Documentation', 'squeeze' ) . '</a>',
375 );
376 $actions['upgrade'] = '<a href="' . esc_url( self::$UPGRADE_URL ) . '">' . esc_html__( 'Go Premium', 'squeeze' ) . '</a>';
377 $actions = array_merge( $docs_link, $actions );
378 $actions = array_merge( $bulk_link, $actions );
379 $actions = array_merge( $settings_link, $actions );
380 }
381 return $actions;
382 }
383
384 public function activation_actions() {
385 // Only for single-site activation
386 if ( !is_network_admin() && !isset( $_GET['activate-multi'] ) ) {
387 add_option( 'squeeze_do_activation_redirect', true );
388 }
389 }
390
391 public function maybe_redirect_to_bulk_page() {
392 if ( get_option( 'squeeze_do_activation_redirect', false ) ) {
393 // Delete the flag so it happens only once
394 delete_option( 'squeeze_do_activation_redirect' );
395 // Skip redirection during bulk or network activation
396 // When you activate several plugins at once (for example, from the Plugins screen using the bulk “Activate” action),
397 // WordPress adds ?activate-multi=true to the URL.
398 if ( isset( $_GET['activate-multi'] ) ) {
399 return;
400 }
401 // Redirect to your custom bulk optimization page
402 wp_safe_redirect( admin_url( 'upload.php?page=squeeze-bulk' ) );
403 exit;
404 }
405 }
406
407 }
408
409 new SqueezeInit();