PluginProbe
Squeeze – Image Optimization & Compression, WEBP Conversion / 1.7.9
Squeeze – Image Optimization & Compression, WEBP Conversion v1.7.9
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
← All changes | squeeze.php +370 -92 1.01.7.9 View file →
@@ -1,92 +1,370 @@
1 -<?php
2 -/**
3 - * Plugin Name: Squeeze
4 - * Description: Compress images by squoosh.app
5 - * Author URI: https://bogdan.kyiv.ua
6 - * Author: Bogdan Bendziukov
7 - * Version: 1.0
8 - *
9 - * Text Domain: squeeze
10 - * Domain Path: /languages
11 - *
12 - * License: GNU GPL v3
13 - * License URI: https://www.gnu.org/licenses/gpl-3.0.html
14 - *
15 - * Network: false
16 - *
17 - */
18 -
19 -// Exit if accessed directly.
20 -if (!defined('ABSPATH')) {
21 - exit;
22 -}
23 -
24 -define('SQUEEZE_PLUGIN_DIR', plugin_dir_path(__FILE__));
25 -define('SQUEEZE_PLUGIN_URL', plugin_dir_url(__FILE__));
26 -
27 -add_action('plugins_loaded', 'squeeze_load_textdomain');
28 -/**
29 - * Load plugin textdomain
30 - */
31 -function squeeze_load_textdomain() {
32 - load_plugin_textdomain('squeeze', false, dirname(plugin_basename(__FILE__)) . '/languages/');
33 -}
34 -
35 -include_once(SQUEEZE_PLUGIN_DIR . 'src/settings.php');
36 -include_once(SQUEEZE_PLUGIN_DIR . 'src/handlers.php');
37 -
38 -
39 -add_action('admin_print_footer_scripts', 'squeeze_block_assets');
40 -/**
41 - * Enqueue assets
42 - */
43 -function squeeze_block_assets() {
44 - global $pagenow;
45 -
46 - $options = get_option( 'squeeze_options' );
47 - $default_options = squeeze_get_default_value( null, true); // get all default values
48 - $js_options = array();
49 -
50 - foreach ($default_options as $key => $value) {
51 - if (isset($options[$key])) {
52 - if (is_numeric($options[$key])) {
53 - $js_options[$key] = intval($options[$key]);
54 - } elseif ($options[$key] === "on") {
55 - $js_options[$key] = true;
56 - }
57 - } else {
58 - $js_options[$key] = $value;
59 - }
60 - }
61 -
62 - // Enqueue script for backend.
63 -
64 - wp_enqueue_script(
65 - 'squeeze-script',
66 - // Handle.
67 - plugins_url('/assets/js/script.bundle.js', __FILE__),
68 - array('jquery', 'wp-mediaelement'),
69 - // Dependencies, defined above.
70 - null,
71 - true // Enqueue the script in the footer.
72 - );
73 -
74 -
75 - // WP Localized globals. Use dynamic PHP stuff in JavaScript via `squeeze` object.
76 - wp_localize_script(
77 - 'squeeze-script',
78 - 'squeeze',
79 - // Array containing dynamic data for a JS Global.
80 - [
81 - 'pluginUrl' => SQUEEZE_PLUGIN_URL,
82 - 'ajaxUrl' => admin_url('admin-ajax.php'),
83 - 'nonce' => wp_create_nonce('squeeze-nonce'),
84 - 'options' => json_encode($js_options),
85 - ]
86 - );
87 -
88 - wp_set_script_translations('squeeze-script', 'squeeze', plugin_dir_path(__DIR__) . 'languages');
89 -
90 -}
91 -
92 -
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.9
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.9';
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 + add_action( 'init', [$this, 'prepare_localize_args'] );
92 + add_action( 'plugins_loaded', array($this, 'load_textdomain') );
93 + add_action( 'admin_enqueue_scripts', array($this, 'load_assets') );
94 + add_action( 'wp_enqueue_scripts', array($this, 'load_voxel_frontend_assets'), 25 );
95 + add_filter(
96 + 'plugin_action_links',
97 + array($this, 'plugin_action_links'),
98 + 10,
99 + 2
100 + );
101 + add_action( 'enqueue_block_editor_assets', array($this, 'load_editor_assets') );
102 + add_action( 'squeeze_freemius_loaded', array($this, 'load_freemius') );
103 + register_activation_hook( __FILE__, array($this, 'activation_actions') );
104 + add_action( 'admin_init', array($this, 'maybe_redirect_to_bulk_page') );
105 + }
106 +
107 + /**
108 + * Load plugin textdomain
109 + */
110 + public function load_textdomain() {
111 + load_plugin_textdomain( 'squeeze', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
112 + }
113 +
114 + public function load_freemius() {
115 + }
116 +
117 + public function opt_in_freemius() {
118 + }
119 +
120 + public function freemius_admin_notices() {
121 + }
122 +
123 + public function prepare_localize_args() {
124 + $options = get_option( 'squeeze_options' );
125 + $default_options = self::$SqueezeHelpers->get_default_value( null, true );
126 + // get all default values
127 + $js_options = array();
128 + foreach ( $default_options as $key => $value ) {
129 + if ( isset( $options[$key] ) ) {
130 + if ( is_numeric( $options[$key] ) ) {
131 + $js_options[$key] = floatval( $options[$key] );
132 + } elseif ( $options[$key] === "on" ) {
133 + $js_options[$key] = true;
134 + } elseif ( $key === "compress_thumbs" ) {
135 + $js_options[$key] = $options[$key];
136 + } else {
137 + $js_options[$key] = $options[$key];
138 + //echo $key . ': ' . $js_options[$key] . ',' . $options[$key] . '<br>';
139 + }
140 + } else {
141 + $js_options[$key] = $value;
142 + }
143 + }
144 + self::$JS_OPTIONS = $js_options;
145 + self::$LOCALIZE_ARGS = array(
146 + 'isPremium' => false,
147 + 'pluginUrl' => self::$PLUGIN_URL,
148 + 'upgradeUrl' => self::$UPGRADE_URL,
149 + 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
150 + 'nonce' => wp_create_nonce( 'squeeze-nonce' ),
151 + 'restNonce' => wp_create_nonce( 'wp_rest' ),
152 + 'options' => wp_json_encode( self::$JS_OPTIONS ),
153 + 'templateBase' => self::$PLUGIN_URL . 'assets/templates/',
154 + 'templates' => array(
155 + 'logWrapper' => self::$PLUGIN_URL . 'assets/templates/log-wrapper.html',
156 + 'logStep' => self::$PLUGIN_URL . 'assets/templates/log-step.html',
157 + 'logDetailsButton' => self::$PLUGIN_URL . 'assets/templates/log-details-button.html',
158 + 'directoryItem' => self::$PLUGIN_URL . 'assets/templates/directory-item.html',
159 + 'directoryItemEmpty' => self::$PLUGIN_URL . 'assets/templates/directory-item-empty.html',
160 + 'pathListItem' => self::$PLUGIN_URL . 'assets/templates/path-list-item.html',
161 + 'previewSqueeze' => self::$PLUGIN_URL . 'assets/templates/preview-squeeze.html',
162 + ),
163 + 'settingsPageUrl' => admin_url( 'options-general.php?page=squeeze' ),
164 + );
165 + }
166 +
167 + public function load_settings() {
168 + require_once self::$PLUGIN_DIR . 'inc/settings.php';
169 + }
170 +
171 + public function load_handlers() {
172 + require_once self::$PLUGIN_DIR . 'inc/handlers.php';
173 + }
174 +
175 + public function load_helpers() {
176 + require_once self::$PLUGIN_DIR . 'inc/helpers.php';
177 + }
178 +
179 + /**
180 + * Script dependencies: admin needs media/plupload; frontend (Voxel) only needs i18n + domReady.
181 + *
182 + * @return string[]
183 + */
184 + protected function get_squeeze_script_dependencies() {
185 + if ( is_admin() ) {
186 + return array(
187 + 'jquery',
188 + 'jquery-ui-core',
189 + 'wp-mediaelement',
190 + 'wp-i18n',
191 + 'wp-dom-ready'
192 + );
193 + }
194 + return array('jquery', 'wp-i18n', 'wp-dom-ready');
195 + }
196 +
197 + /**
198 + * Whether the active theme (or parent) is Voxel.
199 + */
200 + protected function is_voxel_theme_active() {
201 + $theme = wp_get_theme();
202 + $slug = $theme->get_template();
203 + $active = is_string( $slug ) && strtolower( $slug ) === 'voxel';
204 + /**
205 + * @since 1.7.8
206 + * @param bool $active Whether the active parent theme is Voxel.
207 + * @param string $slug Parent theme directory slug (stylesheet template).
208 + */
209 + return (bool) apply_filters( 'squeeze_is_voxel_theme', $active, ( is_string( $slug ) ? $slug : '' ) );
210 + }
211 +
212 + /**
213 + * Voxel create-post / file fields upload via multipart FormData to admin-ajax.php, not wp.Uploader.
214 + * Load Squeeze on the frontend so client-side compression can run for logged-in users who can upload.
215 + */
216 + public function load_voxel_frontend_assets() {
217 + static $voxel_squeeze_enqueued = false;
218 + if ( $voxel_squeeze_enqueued ) {
219 + return;
220 + }
221 + if ( !$this->is_voxel_theme_active() ) {
222 + return;
223 + }
224 + if ( !is_user_logged_in() || !current_user_can( 'upload_files' ) ) {
225 + return;
226 + }
227 + wp_enqueue_script(
228 + 'squeeze-script',
229 + self::$PLUGIN_URL . 'assets/js/script.bundle.js',
230 + $this->get_squeeze_script_dependencies(),
231 + self::VERSION,
232 + true
233 + );
234 + wp_localize_script( 'squeeze-script', 'squeezeOptions', self::$LOCALIZE_ARGS );
235 + wp_set_script_translations( 'squeeze-script', 'squeeze', self::$PLUGIN_DIR . 'languages' );
236 + $voxel_squeeze_enqueued = true;
237 + }
238 +
239 + /**
240 + * Enqueue assets
241 + */
242 + public function load_assets() {
243 + global $pagenow;
244 + if ( !wp_script_is( 'media-editor', 'enqueued' ) ) {
245 + wp_enqueue_media();
246 + }
247 + // Enqueue script for backend.
248 + wp_enqueue_script(
249 + 'squeeze-script',
250 + self::$PLUGIN_URL . 'assets/js/script.bundle.js',
251 + $this->get_squeeze_script_dependencies(),
252 + self::VERSION,
253 + true
254 + );
255 + // WP Localized globals. Use dynamic PHP stuff in JavaScript via `squeeze` object.
256 + wp_localize_script( 'squeeze-script', 'squeezeOptions', self::$LOCALIZE_ARGS );
257 + if ( $pagenow === 'upload.php' && isset( $_GET['page'] ) && $_GET['page'] === 'squeeze-bulk' ) {
258 + wp_localize_script( 'squeeze-script', 'squeezeBulk', [
259 + 'allImages' => implode( ",", self::$SqueezeHelpers->get_total_images() ),
260 + 'unCompressedImages' => implode( ",", self::$SqueezeHelpers->get_uncompressed_images() ),
261 + ] );
262 + }
263 + // Check if we are on the options page for the plugin
264 + if ( $pagenow === 'upload.php' || $pagenow === 'options-general.php' && isset( $_GET['page'] ) && $_GET['page'] === 'squeeze' ) {
265 + wp_enqueue_script(
266 + 'squeeze-settings-script',
267 + self::$PLUGIN_URL . 'assets/js/admin.bundle.js',
268 + array('jquery'),
269 + self::VERSION,
270 + true
271 + );
272 + }
273 + wp_set_script_translations( 'squeeze-script', 'squeeze', self::$PLUGIN_DIR . 'languages' );
274 + // Enqueue styles for backend.
275 + wp_enqueue_style(
276 + 'squeeze-style',
277 + self::$PLUGIN_URL . 'assets/css/admin.css',
278 + array(),
279 + self::VERSION
280 + );
281 + wp_localize_script( 'squeeze-editor-script', 'squeezeOptions', array(
282 + 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
283 + 'nonce' => wp_create_nonce( 'squeeze-nonce' ),
284 + 'options' => wp_json_encode( self::$JS_OPTIONS ),
285 + ) );
286 + }
287 +
288 + public function load_elementor_assets() {
289 + }
290 +
291 + public function load_beaver_assets() {
292 + }
293 +
294 + public function load_editor_assets() {
295 + // Enqueue scripts for the editor.
296 + wp_enqueue_script(
297 + 'squeeze-editor-script',
298 + self::$PLUGIN_URL . 'assets/js/editor.bundle.js',
299 + array(
300 + 'wp-blocks',
301 + 'wp-hooks',
302 + 'wp-compose',
303 + 'wp-element',
304 + 'wp-data',
305 + 'wp-block-editor',
306 + 'wp-api-fetch'
307 + ),
308 + self::VERSION,
309 + true
310 + );
311 + }
312 +
313 + /**
314 + * Add settings link on plugin page
315 + *
316 + * @param array $links
317 + * @return array
318 + */
319 + public function plugin_action_links( $actions, $plugin_file ) {
320 + static $plugin;
321 + if ( !current_user_can( 'manage_options' ) ) {
322 + return $actions;
323 + }
324 + if ( !isset( $plugin ) ) {
325 + $plugin = plugin_basename( __FILE__ );
326 + }
327 + if ( $plugin === $plugin_file ) {
328 + $settings_link = array(
329 + 'settings' => '<a href="' . admin_url( 'options-general.php?page=squeeze' ) . '">' . __( 'Settings', 'squeeze' ) . '</a>',
330 + );
331 + $bulk_link = array(
332 + 'bulk' => '<a href="' . admin_url( 'upload.php?page=squeeze-bulk' ) . '">' . __( 'Bulk Squeeze', 'squeeze' ) . '</a>',
333 + );
334 + $docs_link = array(
335 + 'docs' => '<a target="_blank" href="https://pluginarium.com/squeeze/squeeze-documentation/">' . __( 'Documentation', 'squeeze' ) . '</a>',
336 + );
337 + $actions['upgrade'] = '<a href="' . esc_url( self::$UPGRADE_URL ) . '">' . esc_html__( 'Go Premium', 'squeeze' ) . '</a>';
338 + $actions = array_merge( $docs_link, $actions );
339 + $actions = array_merge( $bulk_link, $actions );
340 + $actions = array_merge( $settings_link, $actions );
341 + }
342 + return $actions;
343 + }
344 +
345 + public function activation_actions() {
346 + // Only for single-site activation
347 + if ( !is_network_admin() && !isset( $_GET['activate-multi'] ) ) {
348 + add_option( 'squeeze_do_activation_redirect', true );
349 + }
350 + }
351 +
352 + public function maybe_redirect_to_bulk_page() {
353 + if ( get_option( 'squeeze_do_activation_redirect', false ) ) {
354 + // Delete the flag so it happens only once
355 + delete_option( 'squeeze_do_activation_redirect' );
356 + // Skip redirection during bulk or network activation
357 + // When you activate several plugins at once (for example, from the Plugins screen using the bulk “Activate” action),
358 + // WordPress adds ?activate-multi=true to the URL.
359 + if ( isset( $_GET['activate-multi'] ) ) {
360 + return;
361 + }
362 + // Redirect to your custom bulk optimization page
363 + wp_safe_redirect( admin_url( 'upload.php?page=squeeze-bulk' ) );
364 + exit;
365 + }
366 + }
367 +
368 +}
369 +
370 +new SqueezeInit();