| 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://bogdan.kyiv.ua |
| 7 |
* Author: Bogdan Bendziukov |
| 8 |
* Version: 1.6 |
| 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 Squeeze; |
| 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.6'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Allowed image formats |
| 32 |
*/ |
| 33 |
const ALLOWED_IMAGE_FORMATS = array( |
| 34 |
'jpg' => 'JPG', |
| 35 |
'jpeg' => 'JPEG', |
| 36 |
'png' => 'PNG', |
| 37 |
'webp' => 'WebP', |
| 38 |
'avif' => 'AVIF', |
| 39 |
); |
| 40 |
|
| 41 |
public static $SqueezeHelpers; |
| 42 |
|
| 43 |
public static $SqueezeSettings; |
| 44 |
|
| 45 |
public static $SqueezeHandlers; |
| 46 |
|
| 47 |
public static $SqueezePremium; |
| 48 |
|
| 49 |
/** |
| 50 |
* Media per page |
| 51 |
*/ |
| 52 |
public static $MEDIA_PER_PAGE; |
| 53 |
|
| 54 |
/** |
| 55 |
* Plugin directory |
| 56 |
*/ |
| 57 |
public static $PLUGIN_DIR; |
| 58 |
|
| 59 |
/** |
| 60 |
* Plugin URL |
| 61 |
*/ |
| 62 |
public static $PLUGIN_URL; |
| 63 |
|
| 64 |
/** |
| 65 |
* Initialize the plugin |
| 66 |
*/ |
| 67 |
public function __construct() { |
| 68 |
self::$PLUGIN_DIR = plugin_dir_path( __FILE__ ); |
| 69 |
self::$PLUGIN_URL = plugin_dir_url( __FILE__ ); |
| 70 |
self::$MEDIA_PER_PAGE = apply_filters( 'squeeze_media_per_page', 50 ); |
| 71 |
|
| 72 |
$this->load_helpers(); |
| 73 |
$this->load_handlers(); |
| 74 |
$this->load_settings(); |
| 75 |
self::$SqueezeHelpers = new SqueezeHelpers(); |
| 76 |
self::$SqueezeSettings = new SqueezeSettings(); |
| 77 |
self::$SqueezeHandlers = new SqueezeHandlers(); |
| 78 |
add_action( 'plugins_loaded', array($this, 'load_textdomain') ); |
| 79 |
add_action( 'admin_print_footer_scripts', array($this, 'load_assets') ); |
| 80 |
add_filter( |
| 81 |
'plugin_action_links', |
| 82 |
array($this, 'plugin_action_links'), |
| 83 |
10, |
| 84 |
2 |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Load plugin textdomain |
| 90 |
*/ |
| 91 |
public function load_textdomain() { |
| 92 |
load_plugin_textdomain( 'squeeze', false, self::$PLUGIN_DIR . '/languages/' ); |
| 93 |
} |
| 94 |
|
| 95 |
public function load_settings() { |
| 96 |
require_once self::$PLUGIN_DIR . 'inc/settings.php'; |
| 97 |
} |
| 98 |
|
| 99 |
public function load_handlers() { |
| 100 |
require_once self::$PLUGIN_DIR . 'inc/handlers.php'; |
| 101 |
} |
| 102 |
|
| 103 |
public function load_helpers() { |
| 104 |
require_once self::$PLUGIN_DIR . 'inc/helpers.php'; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Enqueue assets |
| 109 |
*/ |
| 110 |
public function load_assets() { |
| 111 |
global $pagenow; |
| 112 |
$options = get_option( 'squeeze_options' ); |
| 113 |
$default_options = self::$SqueezeHelpers->get_default_value( null, true ); |
| 114 |
// get all default values |
| 115 |
$js_options = array(); |
| 116 |
foreach ( $default_options as $key => $value ) { |
| 117 |
if ( isset( $options[$key] ) ) { |
| 118 |
if ( is_numeric( $options[$key] ) ) { |
| 119 |
$js_options[$key] = intval( $options[$key] ); |
| 120 |
} elseif ( $options[$key] === "on" ) { |
| 121 |
$js_options[$key] = true; |
| 122 |
} elseif ( $key === "compress_thumbs" ) { |
| 123 |
$js_options[$key] = $options[$key]; |
| 124 |
} |
| 125 |
} else { |
| 126 |
$js_options[$key] = $value; |
| 127 |
} |
| 128 |
} |
| 129 |
if ( !wp_script_is( 'media-editor', 'enqueued' ) ) { |
| 130 |
wp_enqueue_media(); |
| 131 |
} |
| 132 |
// Enqueue script for backend. |
| 133 |
wp_enqueue_script( |
| 134 |
'squeeze-script', |
| 135 |
self::$PLUGIN_URL . 'assets/js/script.bundle.js', |
| 136 |
array('jquery', 'wp-mediaelement'), |
| 137 |
self::VERSION, |
| 138 |
true |
| 139 |
); |
| 140 |
// WP Localized globals. Use dynamic PHP stuff in JavaScript via `squeeze` object. |
| 141 |
wp_localize_script( |
| 142 |
'squeeze-script', |
| 143 |
'squeezeOptions', |
| 144 |
// Array containing dynamic data for a JS Global. |
| 145 |
[ |
| 146 |
'pluginUrl' => self::$PLUGIN_URL, |
| 147 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 148 |
'nonce' => wp_create_nonce( 'squeeze-nonce' ), |
| 149 |
'options' => wp_json_encode( $js_options ), |
| 150 |
'templateBase' => self::$PLUGIN_URL . 'assets/templates/', |
| 151 |
'templates' => array( |
| 152 |
'logWrapper' => self::$PLUGIN_URL . 'assets/templates/log-wrapper.html', |
| 153 |
'logStep' => self::$PLUGIN_URL . 'assets/templates/log-step.html', |
| 154 |
'logDetailsButton' => self::$PLUGIN_URL . 'assets/templates/log-details-button.html', |
| 155 |
'directoryItem' => self::$PLUGIN_URL . 'assets/templates/directory-item.html', |
| 156 |
'directoryItemEmpty' => self::$PLUGIN_URL . 'assets/templates/directory-item-empty.html', |
| 157 |
'pathListItem' => self::$PLUGIN_URL . 'assets/templates/path-list-item.html', |
| 158 |
), |
| 159 |
] |
| 160 |
); |
| 161 |
if ( $pagenow === 'upload.php' && isset( $_GET['page'] ) && $_GET['page'] === 'squeeze-bulk' ) { |
| 162 |
wp_localize_script( 'squeeze-script', 'squeezeBulk', [ |
| 163 |
'allImages' => implode( ",", self::$SqueezeHelpers->get_total_images() ), |
| 164 |
'unCompressedImages' => implode( ",", self::$SqueezeHelpers->get_uncompressed_images() ), |
| 165 |
] ); |
| 166 |
} |
| 167 |
// Check if we are on the options page for the plugin |
| 168 |
if ( $pagenow === 'upload.php' || $pagenow === 'options-general.php' && isset( $_GET['page'] ) && $_GET['page'] === 'squeeze' ) { |
| 169 |
wp_enqueue_script( |
| 170 |
'squeeze-settings-script', |
| 171 |
self::$PLUGIN_URL . 'assets/js/admin.js', |
| 172 |
array('jquery'), |
| 173 |
self::VERSION, |
| 174 |
true |
| 175 |
); |
| 176 |
} |
| 177 |
wp_set_script_translations( 'squeeze-script', 'squeeze', self::$PLUGIN_DIR . 'languages' ); |
| 178 |
// Enqueue styles for backend. |
| 179 |
wp_enqueue_style( |
| 180 |
'squeeze-style', |
| 181 |
self::$PLUGIN_URL . 'assets/css/admin.css', |
| 182 |
array(), |
| 183 |
self::VERSION |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Add settings link on plugin page |
| 189 |
* |
| 190 |
* @param array $links |
| 191 |
* @return array |
| 192 |
*/ |
| 193 |
public function plugin_action_links( $actions, $plugin_file ) { |
| 194 |
static $plugin; |
| 195 |
if ( !isset( $plugin ) ) { |
| 196 |
$plugin = plugin_basename( __FILE__ ); |
| 197 |
} |
| 198 |
if ( $plugin === $plugin_file ) { |
| 199 |
$settings_link = array( |
| 200 |
'settings' => '<a href="' . admin_url( 'options-general.php?page=squeeze' ) . '">' . __( 'Settings', 'squeeze' ) . '</a>', |
| 201 |
); |
| 202 |
$bulk_link = array( |
| 203 |
'bulk' => '<a href="' . admin_url( 'upload.php?page=squeeze-bulk' ) . '">' . __( 'Bulk Squeeze', 'squeeze' ) . '</a>', |
| 204 |
); |
| 205 |
$actions = array_merge( $bulk_link, $actions ); |
| 206 |
$actions = array_merge( $settings_link, $actions ); |
| 207 |
} |
| 208 |
return $actions; |
| 209 |
} |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
new SqueezeInit(); |
| 214 |
|