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

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