| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The file that defines the core plugin class |
| 5 |
* |
| 6 |
* A class definition that includes attributes and functions used across both the |
| 7 |
* public-facing side of the site and the admin area. |
| 8 |
* |
| 9 |
* @link http://webdeclic.com |
| 10 |
* @since 1.0.0 |
| 11 |
* |
| 12 |
* @package Quickwebp |
| 13 |
* @subpackage Quickwebp/includes |
| 14 |
*/ |
| 15 |
|
| 16 |
/** |
| 17 |
* The core plugin class. |
| 18 |
* |
| 19 |
* This is used to define internationalization, admin-specific hooks, and |
| 20 |
* public-facing site hooks. |
| 21 |
* |
| 22 |
* Also maintains the unique identifier of this plugin as well as the current |
| 23 |
* version of the plugin. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
* @package Quickwebp |
| 27 |
* @subpackage Quickwebp/includes |
| 28 |
* @author Webdeclic <contact@webdeclic.com> |
| 29 |
*/ |
| 30 |
class Quickwebp { |
| 31 |
|
| 32 |
/** |
| 33 |
* The loader that's responsible for maintaining and registering all hooks that power |
| 34 |
* the plugin. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* @access protected |
| 38 |
* @var Quickwebp_Loader $loader Maintains and registers all hooks for the plugin. |
| 39 |
*/ |
| 40 |
protected $loader; |
| 41 |
|
| 42 |
/** |
| 43 |
* The unique identifier of this plugin. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
* @access protected |
| 47 |
* @var string $plugin_name The string used to uniquely identify this plugin. |
| 48 |
*/ |
| 49 |
protected $plugin_name; |
| 50 |
|
| 51 |
/** |
| 52 |
* The current version of the plugin. |
| 53 |
* |
| 54 |
* @since 1.0.0 |
| 55 |
* @access protected |
| 56 |
* @var string $version The current version of the plugin. |
| 57 |
*/ |
| 58 |
protected $version; |
| 59 |
|
| 60 |
/** |
| 61 |
* Define the core functionality of the plugin. |
| 62 |
* |
| 63 |
* Set the plugin name and the plugin version that can be used throughout the plugin. |
| 64 |
* Load the dependencies, define the locale, and set the hooks for the admin area and |
| 65 |
* the public-facing side of the site. |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
*/ |
| 69 |
public function __construct() { |
| 70 |
if ( defined( 'QUICKWEBP_VERSION' ) ) { |
| 71 |
$this->version = QUICKWEBP_VERSION; |
| 72 |
} else { |
| 73 |
$this->version = '1.0.0'; |
| 74 |
} |
| 75 |
$this->plugin_name = 'quickwebp'; |
| 76 |
|
| 77 |
$this->load_dependencies(); |
| 78 |
$this->set_locale(); |
| 79 |
$this->define_admin_hooks(); |
| 80 |
$this->define_public_hooks(); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Load the required dependencies for this plugin. |
| 86 |
* |
| 87 |
* Include the following files that make up the plugin: |
| 88 |
* |
| 89 |
* - Quickwebp_Loader. Orchestrates the hooks of the plugin. |
| 90 |
* - Quickwebp_i18n. Defines internationalization functionality. |
| 91 |
* - Quickwebp_Admin. Defines all hooks for the admin area. |
| 92 |
* - Quickwebp_Public. Defines all hooks for the public side of the site. |
| 93 |
* |
| 94 |
* Create an instance of the loader which will be used to register the hooks |
| 95 |
* with WordPress. |
| 96 |
* |
| 97 |
* @since 1.0.0 |
| 98 |
* @access private |
| 99 |
*/ |
| 100 |
private function load_dependencies() { |
| 101 |
|
| 102 |
/** |
| 103 |
* The class responsible for loading composer dependencies. |
| 104 |
*/ |
| 105 |
require_once QUICKWEBP_PLUGIN_PATH . 'includes/vendor/autoload.php'; |
| 106 |
|
| 107 |
/** |
| 108 |
* The class responsible for orchestrating the actions and filters of the |
| 109 |
* core plugin. |
| 110 |
*/ |
| 111 |
require_once QUICKWEBP_PLUGIN_PATH . 'includes/class-quickwebp-loader.php'; |
| 112 |
|
| 113 |
/** |
| 114 |
* This file is loaded only on local environement for test or debug. |
| 115 |
*/ |
| 116 |
if( $_SERVER['REMOTE_ADDR'] == '127.0.0.1' || $_SERVER['REMOTE_ADDR'] == '::1' ){ |
| 117 |
require_once QUICKWEBP_PLUGIN_PATH. 'includes/dev-toolkits.php'; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* The global functions for this plugin |
| 122 |
*/ |
| 123 |
require_once QUICKWEBP_PLUGIN_PATH . 'includes/global-functions.php'; |
| 124 |
|
| 125 |
/** |
| 126 |
* The class responsible for defining internationalization functionality |
| 127 |
* of the plugin. |
| 128 |
*/ |
| 129 |
require_once QUICKWEBP_PLUGIN_PATH . 'includes/class-quickwebp-i18n.php'; |
| 130 |
|
| 131 |
/** |
| 132 |
* The class responsible of settings. |
| 133 |
*/ |
| 134 |
require_once QUICKWEBP_PLUGIN_PATH . 'admin/class-settings.php'; |
| 135 |
|
| 136 |
/** |
| 137 |
* The core functionality for image optimizing |
| 138 |
*/ |
| 139 |
require_once QUICKWEBP_PLUGIN_PATH . 'admin/class-image-optimizer.php'; |
| 140 |
|
| 141 |
/** |
| 142 |
* The core functionality for wp media extending |
| 143 |
*/ |
| 144 |
require_once QUICKWEBP_PLUGIN_PATH . 'admin/class-wp-media-extends.php'; |
| 145 |
|
| 146 |
/** |
| 147 |
* The class responsible of cron job. |
| 148 |
*/ |
| 149 |
require_once QUICKWEBP_PLUGIN_PATH . 'admin/class-cron-job.php'; |
| 150 |
|
| 151 |
/** |
| 152 |
* The handle the front-end display functionality of the plugin. |
| 153 |
*/ |
| 154 |
require_once QUICKWEBP_PLUGIN_PATH . 'public/class-display-webp.php'; |
| 155 |
|
| 156 |
$this->loader = new Quickwebp_Loader(); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Define the locale for this plugin for internationalization. |
| 162 |
* |
| 163 |
* Uses the Quickwebp_i18n class in order to set the domain and to register the hook |
| 164 |
* with WordPress. |
| 165 |
* |
| 166 |
* @since 1.0.0 |
| 167 |
* @access private |
| 168 |
*/ |
| 169 |
private function set_locale() { |
| 170 |
|
| 171 |
$plugin_i18n = new Quickwebp_i18n(); |
| 172 |
|
| 173 |
$this->loader->add_action( 'init', $plugin_i18n, 'load_plugin_textdomain' ); |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Register all of the hooks related to the admin area functionality |
| 179 |
* of the plugin. |
| 180 |
* |
| 181 |
* @since 1.0.0 |
| 182 |
* @access private |
| 183 |
*/ |
| 184 |
private function define_admin_hooks() { |
| 185 |
|
| 186 |
$quickwebp_settings = new Quickwebp_Settings( $this->get_plugin_name(), $this->get_version() ); |
| 187 |
$this->loader->add_action( 'admin_enqueue_scripts', $quickwebp_settings, 'enqueue_scripts_styles' ); |
| 188 |
$this->loader->add_action( 'admin_menu', $quickwebp_settings, 'add_settings_menu' ); |
| 189 |
$this->loader->add_filter( 'plugin_action_links', $quickwebp_settings, 'add_settings_link', 10, 2 ); |
| 190 |
$this->loader->add_action( 'admin_init', $quickwebp_settings, 'show_notice_if_library_not_exist' ); |
| 191 |
$this->loader->add_filter( 'sanitize_option_quickwebp_settings_conversion_display_webp_mode', $quickwebp_settings, 'add_rewrite_rules', 5 ); |
| 192 |
|
| 193 |
$quickwebp_image_optimizer = new Quickwebp_Image_Optimizer( $this->get_plugin_name(), $this->get_version() ); |
| 194 |
$this->loader->add_filter( 'wp_handle_upload_prefilter', $quickwebp_image_optimizer, 'image_optimizition' ); |
| 195 |
$this->loader->add_filter( 'wp_generate_attachment_metadata', $quickwebp_image_optimizer, 'add_data_to_attachment', 10, 3 ); |
| 196 |
$this->loader->add_filter( 'big_image_size_threshold', $quickwebp_image_optimizer, 'change_wp_max_size', PHP_INT_MAX, 4 ); |
| 197 |
$this->loader->add_filter( 'wp_editor_set_quality', $quickwebp_image_optimizer, 'change_wp_quality', PHP_INT_MAX, 2 ); |
| 198 |
$this->loader->add_action( 'wp_ajax_image_optimizition_ajax', $quickwebp_image_optimizer, 'image_optimizition_ajax' ); |
| 199 |
$this->loader->add_action( 'wp_ajax_single_optimizition_ajax', $quickwebp_image_optimizer, 'single_optimizition_ajax' ); |
| 200 |
$this->loader->add_action( 'wp_ajax_undo_single_optimizition_ajax', $quickwebp_image_optimizer, 'undo_single_optimizition_ajax' ); |
| 201 |
$this->loader->add_action( 'delete_attachment', $quickwebp_image_optimizer, 'before_delete_attachment', 10, 2 ); |
| 202 |
|
| 203 |
$quickwebp_wp_media_extends = new Quickwebp_Wp_Media_Extends( $this->get_plugin_name(), $this->get_version() ); |
| 204 |
$this->loader->add_action( 'wp_enqueue_media', $quickwebp_wp_media_extends, 'enqueue_scripts' ); |
| 205 |
$this->loader->add_filter( 'attachment_fields_to_edit', $quickwebp_wp_media_extends, 'add_attachment_fields_to_edit', PHP_INT_MAX, 2 ); |
| 206 |
$this->loader->add_filter( 'manage_media_columns', $quickwebp_wp_media_extends, 'add_media_columns'); |
| 207 |
$this->loader->add_action( 'manage_media_custom_column', $quickwebp_wp_media_extends, 'add_media_custom_column', 10, 2 ); |
| 208 |
$this->loader->add_action( 'attachment_submitbox_misc_actions', $quickwebp_wp_media_extends, 'add_attachment_submitbox_misc_actions', PHP_INT_MAX ); |
| 209 |
|
| 210 |
$quickwebp_cron_job = new Quickwebp_Cron_Job( $this->get_plugin_name(), $this->get_version() ); |
| 211 |
$this->loader->add_filter( 'cron_schedules', $quickwebp_cron_job, 'crons_registrations' ); |
| 212 |
$this->loader->add_action( 'quickwebp_bulk_optimization_hook', $quickwebp_cron_job, 'excute_bulk_optimization' ); |
| 213 |
$this->loader->add_action( 'wp_ajax_start_bulk_optimization', $quickwebp_cron_job, 'start_bulk_optimization' ); |
| 214 |
$this->loader->add_action( 'wp_ajax_stop_bulk_optimization', $quickwebp_cron_job, 'stop_bulk_optimization' ); |
| 215 |
$this->loader->add_action( 'wp_ajax_check_bulk_optimization_progress', $quickwebp_cron_job, 'check_bulk_optimization_progress' ); |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Register all of the hooks related to the public-facing functionality |
| 221 |
* of the plugin. |
| 222 |
* |
| 223 |
* @since 1.0.0 |
| 224 |
* @access private |
| 225 |
*/ |
| 226 |
private function define_public_hooks() { |
| 227 |
|
| 228 |
$quickwebp_display_webp = new Quickwebp_Display_Webp( $this->get_plugin_name(), $this->get_version() ); |
| 229 |
$this->loader->add_action( 'template_redirect', $quickwebp_display_webp, 'start_content_process', -1000 ); |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Run the loader to execute all of the hooks with WordPress. |
| 235 |
* |
| 236 |
* @since 1.0.0 |
| 237 |
*/ |
| 238 |
public function run() { |
| 239 |
$this->loader->run(); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* The name of the plugin used to uniquely identify it within the context of |
| 244 |
* WordPress and to define internationalization functionality. |
| 245 |
* |
| 246 |
* @since 1.0.0 |
| 247 |
* @return string The name of the plugin. |
| 248 |
*/ |
| 249 |
public function get_plugin_name() { |
| 250 |
return $this->plugin_name; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* The reference to the class that orchestrates the hooks with the plugin. |
| 255 |
* |
| 256 |
* @since 1.0.0 |
| 257 |
* @return Quickwebp_Loader Orchestrates the hooks of the plugin. |
| 258 |
*/ |
| 259 |
public function get_loader() { |
| 260 |
return $this->loader; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Retrieve the version number of the plugin. |
| 265 |
* |
| 266 |
* @since 1.0.0 |
| 267 |
* @return string The version number of the plugin. |
| 268 |
*/ |
| 269 |
public function get_version() { |
| 270 |
return $this->version; |
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|