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

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