| @@ -1,14 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 2 | +use Imagify\Notices\Notices; | |
| 3 | +use Imagify\Traits\InstanceGetterTrait; | |
| 3 | 4 | |
| 4 | 5 | /** |
| 5 | 6 | * Class that handles stylesheets and JavaScripts. |
| 6 | 7 | * |
| 7 | - * @since 1.6.10 | |
| 8 | - * @author Grégory Viguier | |
| 8 | + * @since 1.6.10 | |
| 9 | 9 | */ |
| 10 | 10 | class Imagify_Assets extends Imagify_Assets_Deprecated { |
| 11 | + use InstanceGetterTrait; | |
| 11 | 12 | |
| 12 | 13 | /** |
| 13 | 14 | * Class version. |
| 14 | 15 | * |
| @@ -34,9 +35,9 @@ | ||
| 34 | 35 | * An array containing our registered styles. |
| 35 | 36 | * |
| 36 | 37 | * @var array |
| 37 | 38 | */ |
| 38 | - protected $styles = array(); | |
| 39 | + protected $styles = []; | |
| 39 | 40 | |
| 40 | 41 | /** |
| 41 | 42 | * An array containing our registered scripts. |
| 42 | 43 | * |
| @@ -41,9 +42,9 @@ | ||
| 41 | 42 | * An array containing our registered scripts. |
| 42 | 43 | * |
| 43 | 44 | * @var array |
| 44 | 45 | */ |
| 45 | - protected $scripts = array(); | |
| 46 | + protected $scripts = []; | |
| 46 | 47 | |
| 47 | 48 | /** |
| 48 | 49 | * Current handle. |
| 49 | 50 | * |
| @@ -62,9 +63,9 @@ | ||
| 62 | 63 | * Array of scripts that should be localized when they are enqueued. |
| 63 | 64 | * |
| 64 | 65 | * @var array |
| 65 | 66 | */ |
| 66 | - protected $deferred_localizations = array(); | |
| 67 | + protected $deferred_localizations = []; | |
| 67 | 68 | |
| 68 | 69 | /** |
| 69 | 70 | * A "random" script version to use when debug is on. |
| 70 | 71 | * |
| @@ -72,15 +73,8 @@ | ||
| 72 | 73 | */ |
| 73 | 74 | protected static $version; |
| 74 | 75 | |
| 75 | 76 | /** |
| 76 | - * The single instance of the class. | |
| 77 | - * | |
| 78 | - * @var object | |
| 79 | - */ | |
| 80 | - protected static $_instance; | |
| 81 | - | |
| 82 | - /** | |
| 83 | 77 | * The constructor. |
| 84 | 78 | * |
| 85 | 79 | * @return void |
| 86 | 80 | */ |
| @@ -95,44 +89,26 @@ | ||
| 95 | 89 | /** PUBLIC METHODS ========================================================================== */ |
| 96 | 90 | /** ----------------------------------------------------------------------------------------- */ |
| 97 | 91 | |
| 98 | 92 | /** |
| 99 | - * Get the main Instance. | |
| 100 | - * | |
| 101 | - * @since 1.6.10 | |
| 102 | - * @author Grégory Viguier | |
| 103 | - * | |
| 104 | - * @return object Main instance. | |
| 105 | - */ | |
| 106 | - public static function get_instance() { | |
| 107 | - if ( ! isset( self::$_instance ) ) { | |
| 108 | - self::$_instance = new self(); | |
| 109 | - } | |
| 110 | - | |
| 111 | - return self::$_instance; | |
| 112 | - } | |
| 113 | - | |
| 114 | - /** | |
| 115 | 93 | * Launch the hooks. |
| 116 | 94 | * |
| 117 | - * @since 1.6.10 | |
| 118 | - * @author Grégory Viguier | |
| 95 | + * @since 1.6.10 | |
| 119 | 96 | */ |
| 120 | 97 | public function init() { |
| 121 | 98 | if ( ! is_admin() ) { |
| 122 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles_and_scripts_frontend' ) ); | |
| 99 | + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles_and_scripts_frontend' ] ); | |
| 123 | 100 | return; |
| 124 | 101 | } |
| 125 | 102 | |
| 126 | - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles_and_scripts' ), IMAGIFY_INT_MAX ); | |
| 127 | - add_action( 'wp_enqueue_media', array( $this, 'enqueue_media_modal' ) ); | |
| 103 | + add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles_and_scripts' ], IMAGIFY_INT_MAX ); | |
| 104 | + add_action( 'wp_enqueue_media', [ $this, 'enqueue_media_modal' ] ); | |
| 128 | 105 | } |
| 129 | 106 | |
| 130 | 107 | /** |
| 131 | 108 | * Enqueue stylesheets and scripts for the frontend. |
| 132 | 109 | * |
| 133 | - * @since 1.6.10 | |
| 134 | - * @author Grégory Viguier | |
| 110 | + * @since 1.6.10 | |
| 135 | 111 | */ |
| 136 | 112 | public function enqueue_styles_and_scripts_frontend() { |
| 137 | 113 | if ( ! $this->is_admin_bar_item_showing() ) { |
| 138 | 114 | return; |
| @@ -138,9 +114,9 @@ | ||
| 138 | 114 | return; |
| 139 | 115 | } |
| 140 | 116 | |
| 141 | 117 | $this->register_style( 'admin-bar' ); |
| 142 | - $this->register_script( 'admin-bar', 'admin-bar', array( 'jquery' ) ); | |
| 118 | + $this->register_script( 'admin-bar', 'admin-bar', [ 'jquery' ] ); | |
| 143 | 119 | |
| 144 | 120 | $this->enqueue_assets( 'admin-bar' )->localize( 'imagifyAdminBar' ); |
| 145 | 121 | } |
| 146 | 122 | |
| @@ -146,10 +122,9 @@ | ||
| 146 | 122 | |
| 147 | 123 | /** |
| 148 | 124 | * Register stylesheets and scripts for the administration area. |
| 149 | 125 | * |
| 150 | - * @since 1.6.10 | |
| 151 | - * @author Grégory Viguier | |
| 126 | + * @since 1.6.10 | |
| 152 | 127 | */ |
| 153 | 128 | public function register_styles_and_scripts() { |
| 154 | 129 | static $done = false; |
| 155 | 130 | |
| @@ -160,75 +135,77 @@ | ||
| 160 | 135 | |
| 161 | 136 | /** |
| 162 | 137 | * 3rd Party Styles. |
| 163 | 138 | */ |
| 164 | - $this->register_style( 'sweetalert-core', 'sweetalert2', array(), '4.6.6' ); | |
| 139 | + $this->register_style( 'sweetalert-core', 'sweetalert2', [], '4.6.6' ); | |
| 165 | 140 | |
| 166 | 141 | /** |
| 167 | 142 | * Imagify Styles. |
| 168 | 143 | */ |
| 169 | - $this->register_style( 'sweetalert', 'sweetalert-custom', array( 'sweetalert-core' ) ); | |
| 144 | + $this->register_style( 'sweetalert', 'sweetalert-custom', [ 'sweetalert-core' ] ); | |
| 170 | 145 | |
| 171 | 146 | $this->register_style( 'admin-bar' ); |
| 172 | 147 | |
| 173 | 148 | $this->register_style( 'admin' ); |
| 174 | 149 | |
| 175 | - $this->register_style( 'notices', 'notices', array( 'admin' ) ); // Needs SweetAlert on some cases. | |
| 150 | + $this->register_style( 'notices', 'notices', [ 'admin' ] ); // Needs SweetAlert on some cases. | |
| 176 | 151 | |
| 177 | - $this->register_style( 'twentytwenty', 'twentytwenty', array( 'admin' ) ); | |
| 152 | + $this->register_style( 'twentytwenty', 'twentytwenty', [ 'admin' ] ); | |
| 178 | 153 | |
| 179 | - $this->register_style( 'pricing-modal', 'pricing-modal', array( 'admin' ) ); | |
| 154 | + $this->register_style( 'pricing-modal', 'pricing-modal', [ 'admin' ] ); | |
| 180 | 155 | |
| 181 | - $this->register_style( 'bulk', 'bulk', array( 'sweetalert', 'admin' ) ); | |
| 156 | + $this->register_style( 'bulk', 'bulk', [ 'sweetalert', 'admin' ] ); | |
| 182 | 157 | |
| 183 | - $this->register_style( 'options', 'options', array( 'sweetalert', 'admin' ) ); | |
| 158 | + $this->register_style( 'options', 'options', [ 'sweetalert', 'admin' ] ); | |
| 184 | 159 | |
| 185 | - $this->register_style( 'files-list', 'files-list', array( 'admin' ) ); | |
| 160 | + $this->register_style( 'files-list', 'files-list', [ 'admin' ] ); | |
| 186 | 161 | |
| 187 | 162 | /** |
| 188 | 163 | * 3rd Party Scripts. |
| 189 | 164 | */ |
| 190 | - $this->register_script( 'promise-polyfill', 'es6-promise.auto', array(), '4.1.1' ); | |
| 165 | + $this->register_script( 'promise-polyfill', 'es6-promise.auto', [], '4.1.1' ); | |
| 191 | 166 | |
| 192 | - $this->register_script( 'sweetalert', 'sweetalert2', array( 'promise-polyfill' ), '4.6.6' )->localize( 'imagifySwal' ); | |
| 167 | + $this->register_script( 'sweetalert', 'sweetalert2', [ 'promise-polyfill' ], '4.6.6' )->localize( 'imagifySwal' ); | |
| 193 | 168 | |
| 194 | - $this->register_script( 'chart', 'chart', array(), '2.7.1.0' ); | |
| 169 | + $this->register_bud_script( 'runtime', 'runtime' ); | |
| 170 | + $this->register_bud_script( 'chart', 'chart', [ 'runtime' ], '4.4.0' ); | |
| 195 | 171 | |
| 196 | - $this->register_script( 'event-move', 'jquery.event.move', array( 'jquery' ), '2.0.1' ); | |
| 172 | + $this->register_script( 'event-move', 'jquery.event.move', [ 'jquery' ], '2.0.1' ); | |
| 197 | 173 | |
| 198 | 174 | /** |
| 199 | 175 | * Imagify Scripts. |
| 200 | 176 | */ |
| 201 | - $this->register_script( 'admin-bar', 'admin-bar', array( 'jquery' ) )->defer_localization( 'imagifyAdminBar' ); | |
| 177 | + $this->register_script( 'admin-bar', 'admin-bar', [ 'jquery' ] )->defer_localization( 'imagifyAdminBar' ); | |
| 202 | 178 | |
| 203 | - $this->register_script( 'admin', 'admin', array( 'jquery' ) ); | |
| 179 | + $this->register_script( 'helpers', 'helpers' ); | |
| 204 | 180 | |
| 205 | - $this->register_script( 'notices', 'notices', array( 'jquery', 'admin' ) )->defer_localization( 'imagifyNotices' ); // Needs SweetAlert on some cases. | |
| 181 | + $this->register_script( 'admin', 'admin', [ 'jquery', 'helpers' ] ); | |
| 206 | 182 | |
| 207 | - $this->register_script( 'twentytwenty', 'jquery.twentytwenty', array( 'jquery', 'event-move', 'chart', 'admin' ) )->defer_localization( 'imagifyTTT' ); | |
| 183 | + $this->register_script( 'notices', 'notices', [ 'jquery', 'admin' ] )->defer_localization( 'imagifyNotices' ); // Needs SweetAlert on some cases. | |
| 208 | 184 | |
| 209 | - $this->register_script( 'beat', 'beat', array( 'jquery' ) )->localize( 'imagifybeatSettings' ); | |
| 185 | + $this->register_script( 'twentytwenty', 'jquery.twentytwenty', [ 'jquery', 'event-move', 'chart', 'admin' ] )->defer_localization( 'imagifyTTT' ); | |
| 210 | 186 | |
| 211 | - $this->register_script( 'media-modal', 'media-modal', array( 'jquery', 'beat', 'underscore', 'chart', 'admin' ) )->localize( 'imagifyModal' ); | |
| 187 | + $this->register_script( 'beat', 'beat', [ 'jquery' ] )->localize( 'imagifybeatSettings' ); | |
| 212 | 188 | |
| 213 | - $this->register_script( 'pricing-modal', 'pricing-modal', array( 'jquery', 'admin' ) )->defer_localization( 'imagifyPricingModal' ); | |
| 189 | + $this->register_script( 'media-modal', 'media-modal', [ 'jquery', 'beat', 'underscore', 'chart', 'admin' ] )->localize( 'imagifyModal' ); | |
| 214 | 190 | |
| 215 | - $this->register_script( 'library', 'library', array( 'jquery', 'media-modal' ) )->defer_localization( 'imagifyLibrary' ); | |
| 191 | + $this->register_script( 'pricing-modal', 'pricing-modal', [ 'jquery', 'admin' ] )->defer_localization( 'imagifyPricingModal' ); | |
| 216 | 192 | |
| 193 | + $this->register_script( 'library', 'library', [ 'jquery', 'media-modal' ] )->defer_localization( 'imagifyLibrary' ); | |
| 194 | + | |
| 217 | 195 | $this->register_script( 'async', 'imagify-gulp' ); |
| 218 | 196 | |
| 219 | - $this->register_script( 'bulk', 'bulk', array( 'jquery', 'beat', 'underscore', 'chart', 'sweetalert', 'async', 'admin' ) )->defer_localization( 'imagifyBulk' ); | |
| 197 | + $this->register_bud_script( 'bulk', 'bulk', [ 'jquery', 'beat', 'underscore', 'chart', 'sweetalert', 'async', 'admin' ] )->defer_localization( 'imagifyBulk' ); | |
| 220 | 198 | |
| 221 | - $this->register_script( 'options', 'options', array( 'jquery', 'beat', 'sweetalert', 'underscore', 'admin' ) )->defer_localization( 'imagifyOptions' ); | |
| 199 | + $this->register_script( 'options', 'options', [ 'jquery', 'beat', 'sweetalert', 'underscore', 'admin' ] )->defer_localization( 'imagifyOptions' ); | |
| 222 | 200 | |
| 223 | - $this->register_script( 'files-list', 'files-list', array( 'jquery', 'beat', 'underscore', 'chart', 'admin' ) )->defer_localization( 'imagifyFiles' ); | |
| 201 | + $this->register_script( 'files-list', 'files-list', [ 'jquery', 'beat', 'underscore', 'chart', 'admin' ] )->defer_localization( 'imagifyFiles' ); | |
| 224 | 202 | } |
| 225 | 203 | |
| 226 | 204 | /** |
| 227 | 205 | * Enqueue stylesheets and scripts for the administration area. |
| 228 | 206 | * |
| 229 | - * @since 1.6.10 | |
| 230 | - * @author Grégory Viguier | |
| 207 | + * @since 1.6.10 | |
| 231 | 208 | */ |
| 232 | 209 | public function enqueue_styles_and_scripts() { |
| 233 | 210 | static $done = false; |
| 234 | 211 | |
| @@ -251,9 +228,9 @@ | ||
| 251 | 228 | |
| 252 | 229 | /** |
| 253 | 230 | * Notices. |
| 254 | 231 | */ |
| 255 | - $notices = Imagify_Notices::get_instance(); | |
| 232 | + $notices = Notices::get_instance(); | |
| 256 | 233 | |
| 257 | 234 | if ( $notices->has_notices() ) { |
| 258 | 235 | if ( $notices->display_welcome_steps() || $notices->display_wrong_api_key() ) { |
| 259 | 236 | // This is where we display things about the API key. |
| @@ -280,9 +257,9 @@ | ||
| 280 | 257 | /** |
| 281 | 258 | * Loaded in the bulk optimization page. |
| 282 | 259 | */ |
| 283 | 260 | if ( imagify_is_screen( 'bulk' ) ) { |
| 284 | - $this->enqueue_assets( array( 'pricing-modal', 'bulk' ) ); | |
| 261 | + $this->enqueue_assets( 'bulk' ); | |
| 285 | 262 | } |
| 286 | 263 | |
| 287 | 264 | /* |
| 288 | 265 | * Loaded in the settings page. |
| @@ -287,9 +264,9 @@ | ||
| 287 | 264 | /* |
| 288 | 265 | * Loaded in the settings page. |
| 289 | 266 | */ |
| 290 | 267 | if ( imagify_is_screen( 'imagify-settings' ) ) { |
| 291 | - $this->enqueue_assets( array( 'sweetalert', 'notices', 'twentytwenty', 'pricing-modal', 'options' ) ); | |
| 268 | + $this->enqueue_assets( [ 'sweetalert', 'notices', 'twentytwenty', 'options' ] ); | |
| 292 | 269 | } |
| 293 | 270 | |
| 294 | 271 | /* |
| 295 | 272 | * Loaded in the files list page. |
| @@ -294,16 +271,17 @@ | ||
| 294 | 271 | /* |
| 295 | 272 | * Loaded in the files list page. |
| 296 | 273 | */ |
| 297 | 274 | if ( imagify_is_screen( 'files-list' ) ) { |
| 298 | - $this->enqueue_assets( array( 'files-list', 'twentytwenty' ) ); | |
| 275 | + $this->enqueue_assets( [ 'files-list', 'twentytwenty' ] ); | |
| 299 | 276 | } |
| 300 | 277 | |
| 278 | + $this->enqueue_assets( 'pricing-modal' ); | |
| 279 | + | |
| 301 | 280 | /** |
| 302 | 281 | * Triggered after Imagify CSS and JS have been enqueued. |
| 303 | 282 | * |
| 304 | 283 | * @since 1.6.10 |
| 305 | - * @author Grégory Viguier | |
| 306 | 284 | */ |
| 307 | 285 | do_action( 'imagify_assets_enqueued' ); |
| 308 | 286 | } |
| 309 | 287 | |
| @@ -309,10 +287,9 @@ | ||
| 309 | 287 | |
| 310 | 288 | /** |
| 311 | 289 | * Enqueue stylesheets and scripts for the media modal. |
| 312 | 290 | * |
| 313 | - * @since 1.6.10 | |
| 314 | - * @author Grégory Viguier | |
| 291 | + * @since 1.6.10 | |
| 315 | 292 | */ |
| 316 | 293 | public function enqueue_media_modal() { |
| 317 | 294 | static $done = false; |
| 318 | 295 | |
| @@ -334,9 +311,8 @@ | ||
| 334 | 311 | /** |
| 335 | 312 | * Triggered after Imagify CSS and JS have been enqueued for the media modal. |
| 336 | 313 | * |
| 337 | 314 | * @since 1.6.10 |
| 338 | - * @author Grégory Viguier | |
| 339 | 315 | */ |
| 340 | 316 | do_action( 'imagify_media_modal_assets_enqueued' ); |
| 341 | 317 | } |
| 342 | 318 | |
| @@ -348,10 +324,9 @@ | ||
| 348 | 324 | |
| 349 | 325 | /** |
| 350 | 326 | * Register a style. |
| 351 | 327 | * |
| 352 | - * @since 1.6.10 | |
| 353 | - * @author Grégory Viguier | |
| 328 | + * @since 1.6.10 | |
| 354 | 329 | * |
| 355 | 330 | * @param string $handle Name of the stylesheet. Should be unique. |
| 356 | 331 | * @param string|null $file_name The file name, without the extension. If null, $handle is used. |
| 357 | 332 | * @param array $dependencies An array of registered stylesheet handles this stylesheet depends on. |
| @@ -357,18 +332,18 @@ | ||
| 357 | 332 | * @param array $dependencies An array of registered stylesheet handles this stylesheet depends on. |
| 358 | 333 | * @param string|null $version String specifying stylesheet version number. If set to null, the plugin version is used. If SCRIPT_DEBUG is true, a random string is used. |
| 359 | 334 | * @return object This class instance. |
| 360 | 335 | */ |
| 361 | - public function register_style( $handle, $file_name = null, $dependencies = array(), $version = null ) { | |
| 336 | + public function register_style( $handle, $file_name = null, $dependencies = [], $version = null ) { | |
| 362 | 337 | // If we register it, it's one of our styles. |
| 363 | 338 | $this->styles[ $handle ] = 1; |
| 364 | 339 | $this->current_handle = $handle; |
| 365 | 340 | $this->current_handle_type = 'css'; |
| 366 | 341 | |
| 367 | - $file_name = $file_name ? $file_name : $handle; | |
| 368 | - $version = $version ? $version : IMAGIFY_VERSION; | |
| 342 | + $file_name = $file_name ? $file_name : $handle; | |
| 343 | + $version = $version ? $version : IMAGIFY_VERSION; | |
| 369 | 344 | $version = $this->is_debug() ? self::$version : $version; |
| 370 | - $extension = $this->is_debug() ? '.css' : '.min.css'; | |
| 345 | + $extension = $this->is_debug() ? '.css' : '.min.css'; | |
| 371 | 346 | $handle = self::CSS_PREFIX . $handle; |
| 372 | 347 | $dependencies = $this->prefix_dependencies( $dependencies, 'css' ); |
| 373 | 348 | |
| 374 | 349 | wp_register_style( |
| @@ -383,10 +358,9 @@ | ||
| 383 | 358 | |
| 384 | 359 | /** |
| 385 | 360 | * Enqueue a style. |
| 386 | 361 | * |
| 387 | - * @since 1.6.10 | |
| 388 | - * @author Grégory Viguier | |
| 362 | + * @since 1.6.10 | |
| 389 | 363 | * |
| 390 | 364 | * @param string|array $handles Name of the stylesheet. Should be unique. Can be an array to enqueue several stylesheets. |
| 391 | 365 | * @return object This class instance. |
| 392 | 366 | */ |
| @@ -410,10 +384,9 @@ | ||
| 410 | 384 | |
| 411 | 385 | /** |
| 412 | 386 | * Dequeue a style. |
| 413 | 387 | * |
| 414 | - * @since 1.6.10 | |
| 415 | - * @author Grégory Viguier | |
| 388 | + * @since 1.6.10 | |
| 416 | 389 | * |
| 417 | 390 | * @param string|array $handles Name of the stylesheet. Should be unique. Can be an array to dequeue several stylesheets. |
| 418 | 391 | * @return object This class instance. |
| 419 | 392 | */ |
| @@ -437,10 +410,9 @@ | ||
| 437 | 410 | |
| 438 | 411 | /** |
| 439 | 412 | * Register a script. |
| 440 | 413 | * |
| 441 | - * @since 1.6.10 | |
| 442 | - * @author Grégory Viguier | |
| 414 | + * @since 1.6.10 | |
| 443 | 415 | * |
| 444 | 416 | * @param string $handle Name of the script. Should be unique. |
| 445 | 417 | * @param string|null $file_name The file name, without the extension. If null, $handle is used. |
| 446 | 418 | * @param array $dependencies An array of registered script handles this script depends on. |
| @@ -446,19 +418,19 @@ | ||
| 446 | 418 | * @param array $dependencies An array of registered script handles this script depends on. |
| 447 | 419 | * @param string|null $version String specifying script version number. If set to null, the plugin version is used. If SCRIPT_DEBUG is true, a random string is used. |
| 448 | 420 | * @return object This class instance. |
| 449 | 421 | */ |
| 450 | - public function register_script( $handle, $file_name = null, $dependencies = array(), $version = null ) { | |
| 422 | + public function register_script( $handle, $file_name = null, $dependencies = [], $version = null ) { | |
| 451 | 423 | // If we register it, it's one of our scripts. |
| 452 | - $this->scripts[ $handle ] = 1; | |
| 424 | + $this->scripts[ $handle ] = 1; | |
| 453 | 425 | // Set the current handler and handler type. |
| 454 | 426 | $this->current_handle = $handle; |
| 455 | 427 | $this->current_handle_type = 'js'; |
| 456 | 428 | |
| 457 | - $file_name = $file_name ? $file_name : $handle; | |
| 458 | - $version = $version ? $version : IMAGIFY_VERSION; | |
| 429 | + $file_name = $file_name ? $file_name : $handle; | |
| 430 | + $version = $version ? $version : IMAGIFY_VERSION; | |
| 459 | 431 | $version = $this->is_debug() ? self::$version : $version; |
| 460 | - $extension = $this->is_debug() ? '.js' : '.min.js'; | |
| 432 | + $extension = $this->is_debug() ? '.js' : '.min.js'; | |
| 461 | 433 | $handle = self::JS_PREFIX . $handle; |
| 462 | 434 | $dependencies = $this->prefix_dependencies( $dependencies ); |
| 463 | 435 | |
| 464 | 436 | wp_register_script( |
| @@ -472,12 +444,47 @@ | ||
| 472 | 444 | return $this; |
| 473 | 445 | } |
| 474 | 446 | |
| 475 | 447 | /** |
| 448 | + * Register a script. | |
| 449 | + * | |
| 450 | + * @since 1.6.10 | |
| 451 | + * | |
| 452 | + * @param string $handle Name of the script. Should be unique. | |
| 453 | + * @param string|null $file_name The file name, without the extension. If null, $handle is used. | |
| 454 | + * @param array $dependencies An array of registered script handles this script depends on. | |
| 455 | + * @param string|null $version String specifying script version number. If set to null, the plugin version is used. If SCRIPT_DEBUG is true, a random string is used. | |
| 456 | + * @return object This class instance. | |
| 457 | + */ | |
| 458 | + public function register_bud_script( $handle, $file_name = null, $dependencies = [], $version = null ) { | |
| 459 | + // If we register it, it's one of our scripts. | |
| 460 | + $this->scripts[ $handle ] = 1; | |
| 461 | + // Set the current handler and handler type. | |
| 462 | + $this->current_handle = $handle; | |
| 463 | + $this->current_handle_type = 'js'; | |
| 464 | + | |
| 465 | + $file_name = $file_name ? $file_name : $handle; | |
| 466 | + $version = $version ? $version : IMAGIFY_VERSION; | |
| 467 | + $version = $this->is_debug() ? self::$version : $version; | |
| 468 | + $extension = '.js'; | |
| 469 | + $handle = self::JS_PREFIX . $handle; | |
| 470 | + $dependencies = $this->prefix_dependencies( $dependencies ); | |
| 471 | + | |
| 472 | + wp_register_script( | |
| 473 | + $handle, | |
| 474 | + IMAGIFY_URL . 'assets/admin/js/' . $file_name . $extension, | |
| 475 | + $dependencies, | |
| 476 | + $version, | |
| 477 | + true | |
| 478 | + ); | |
| 479 | + | |
| 480 | + return $this; | |
| 481 | + } | |
| 482 | + | |
| 483 | + /** | |
| 476 | 484 | * Enqueue a script. |
| 477 | 485 | * |
| 478 | - * @since 1.6.10 | |
| 479 | - * @author Grégory Viguier | |
| 486 | + * @since 1.6.10 | |
| 480 | 487 | * |
| 481 | 488 | * @param string|array $handles Name of the script. Should be unique. Can be an array to enqueue several scripts. |
| 482 | 489 | * @return object This class instance. |
| 483 | 490 | */ |
| @@ -501,9 +508,9 @@ | ||
| 501 | 508 | wp_enqueue_script( $handle ); |
| 502 | 509 | |
| 503 | 510 | // Deferred localization. |
| 504 | 511 | if ( ! empty( $this->deferred_localizations[ $this->current_handle ] ) ) { |
| 505 | - array_map( array( $this, 'localize' ), $this->deferred_localizations[ $this->current_handle ] ); | |
| 512 | + array_map( [ $this, 'localize' ], $this->deferred_localizations[ $this->current_handle ] ); | |
| 506 | 513 | unset( $this->deferred_localizations[ $this->current_handle ] ); |
| 507 | 514 | } |
| 508 | 515 | } |
| 509 | 516 | |
| @@ -512,10 +519,9 @@ | ||
| 512 | 519 | |
| 513 | 520 | /** |
| 514 | 521 | * Dequeue a script. |
| 515 | 522 | * |
| 516 | - * @since 1.6.10 | |
| 517 | - * @author Grégory Viguier | |
| 523 | + * @since 1.6.10 | |
| 518 | 524 | * |
| 519 | 525 | * @param string|array $handles Name of the script. Should be unique. Can be an array to dequeue several scripts. |
| 520 | 526 | * @return object This class instance. |
| 521 | 527 | */ |
| @@ -544,10 +550,9 @@ | ||
| 544 | 550 | |
| 545 | 551 | /** |
| 546 | 552 | * Localize a script. |
| 547 | 553 | * |
| 548 | - * @since 1.6.10 | |
| 549 | - * @author Grégory Viguier | |
| 554 | + * @since 1.6.10 | |
| 550 | 555 | * |
| 551 | 556 | * @param string $handle Name of the script. Should be unique. |
| 552 | 557 | * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: '/[a-zA-Z0-9_]+/'. |
| 553 | 558 | * @param string|array|null $l10n The data itself. The data can be either a single or multi-dimensional array. If null, $handle is used. |
| @@ -581,10 +586,9 @@ | ||
| 581 | 586 | |
| 582 | 587 | /** |
| 583 | 588 | * Enqueue a style and a script that have the same handle. |
| 584 | 589 | * |
| 585 | - * @since 1.6.10 | |
| 586 | - * @author Grégory Viguier | |
| 590 | + * @since 1.6.10 | |
| 587 | 591 | * |
| 588 | 592 | * @param string|array $handles Name of the script. Should be unique. Can be an array to enqueue several scripts. |
| 589 | 593 | * @return object This class instance. |
| 590 | 594 | */ |
| @@ -600,10 +604,9 @@ | ||
| 600 | 604 | |
| 601 | 605 | /** |
| 602 | 606 | * Dequeue a style and a script that have the same handle. |
| 603 | 607 | * |
| 604 | - * @since 1.6.10 | |
| 605 | - * @author Grégory Viguier | |
| 608 | + * @since 1.6.10 | |
| 606 | 609 | * |
| 607 | 610 | * @param string|array $handles Name of the script. Should be unique. Can be an array to dequeue several scripts. |
| 608 | 611 | * @return object This class instance. |
| 609 | 612 | */ |
| @@ -620,10 +623,9 @@ | ||
| 620 | 623 | |
| 621 | 624 | /** |
| 622 | 625 | * Enqueue the current script or style. |
| 623 | 626 | * |
| 624 | - * @since 1.6.10 | |
| 625 | - * @author Grégory Viguier | |
| 627 | + * @since 1.6.10 | |
| 626 | 628 | * |
| 627 | 629 | * @return object This class instance. |
| 628 | 630 | */ |
| 629 | 631 | public function enqueue() { |
| @@ -638,10 +640,9 @@ | ||
| 638 | 640 | |
| 639 | 641 | /** |
| 640 | 642 | * Localize the current script. |
| 641 | 643 | * |
| 642 | - * @since 1.6.10 | |
| 643 | - * @author Grégory Viguier | |
| 644 | + * @since 1.6.10 | |
| 644 | 645 | * |
| 645 | 646 | * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: '/[a-zA-Z0-9_]+/'. |
| 646 | 647 | * @param string|array|null $l10n The data itself. The data can be either a single or multi-dimensional array. If null, $handle is used. |
| 647 | 648 | * @return object This class instance. |
| @@ -654,10 +655,9 @@ | ||
| 654 | 655 | * Localize the current script when it is enqueued with `$this->enqueue()` or `$this->enqueue_script()`. This should be used right after `$this->register_script()`. |
| 655 | 656 | * Be careful, it won't work if the script is enqueued because it's a dependency. |
| 656 | 657 | * This is handy to not forget to localize the script later. It also prevents to localize the script right away, and maybe execute all localizations while the script is not enqueued (so we localize for nothing). |
| 657 | 658 | * |
| 658 | - * @since 1.6.10 | |
| 659 | - * @author Grégory Viguier | |
| 659 | + * @since 1.6.10 | |
| 660 | 660 | * |
| 661 | 661 | * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: '/[a-zA-Z0-9_]+/'. |
| 662 | 662 | * @return object This class instance. |
| 663 | 663 | */ |
| @@ -662,9 +662,9 @@ | ||
| 662 | 662 | * @return object This class instance. |
| 663 | 663 | */ |
| 664 | 664 | public function defer_localization( $object_name ) { |
| 665 | 665 | if ( ! isset( $this->deferred_localizations[ $this->current_handle ] ) ) { |
| 666 | - $this->deferred_localizations[ $this->current_handle ] = array(); | |
| 666 | + $this->deferred_localizations[ $this->current_handle ] = []; | |
| 667 | 667 | } |
| 668 | 668 | |
| 669 | 669 | $this->deferred_localizations[ $this->current_handle ][ $object_name ] = $object_name; |
| 670 | 670 | |
| @@ -673,10 +673,9 @@ | ||
| 673 | 673 | |
| 674 | 674 | /** |
| 675 | 675 | * Remove a deferred localization. |
| 676 | 676 | * |
| 677 | - * @since 1.6.10 | |
| 678 | - * @author Grégory Viguier | |
| 677 | + * @since 1.6.10 | |
| 679 | 678 | * |
| 680 | 679 | * @param string $handle Name of the script. Should be unique. |
| 681 | 680 | * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: '/[a-zA-Z0-9_]+/'. |
| 682 | 681 | * @return object This class instance. |
| @@ -697,16 +696,15 @@ | ||
| 697 | 696 | |
| 698 | 697 | /** |
| 699 | 698 | * Get all translations we can use with wp_localize_script(). |
| 700 | 699 | * |
| 701 | - * @since 1.6.10 | |
| 702 | - * @author Grégory Viguier | |
| 700 | + * @since 1.6.10 | |
| 703 | 701 | * |
| 704 | 702 | * @param string $context The translation context. |
| 705 | 703 | * @param array $more_data More data to merge. |
| 706 | 704 | * @return array $translations The translations. |
| 707 | 705 | */ |
| 708 | - public function get_localization_data( $context, $more_data = array() ) { | |
| 706 | + public function get_localization_data( $context, $more_data = [] ) { | |
| 709 | 707 | $data = get_imagify_localize_script_translations( $context ); |
| 710 | 708 | |
| 711 | 709 | if ( $more_data ) { |
| 712 | 710 | return array_merge( $data, $more_data ); |
| @@ -722,10 +720,9 @@ | ||
| 722 | 720 | |
| 723 | 721 | /** |
| 724 | 722 | * Prefix the dependencies if they are ours. |
| 725 | 723 | * |
| 726 | - * @since 1.6.10 | |
| 727 | - * @author Grégory Viguier | |
| 724 | + * @since 1.6.10 | |
| 728 | 725 | * |
| 729 | 726 | * @param array $dependencies An array of registered script handles this script depends on. |
| 730 | 727 | * @param string $type Type of dependency: css or js. |
| 731 | 728 | * @return array |
| @@ -731,9 +728,9 @@ | ||
| 731 | 728 | * @return array |
| 732 | 729 | */ |
| 733 | 730 | protected function prefix_dependencies( $dependencies, $type = 'js' ) { |
| 734 | 731 | if ( ! $dependencies ) { |
| 735 | - return array(); | |
| 732 | + return []; | |
| 736 | 733 | } |
| 737 | 734 | |
| 738 | 735 | if ( 'js' === $type ) { |
| 739 | 736 | $prefix = self::JS_PREFIX; |
| @@ -742,9 +739,9 @@ | ||
| 742 | 739 | $prefix = self::CSS_PREFIX; |
| 743 | 740 | $scripts = $this->styles; |
| 744 | 741 | } |
| 745 | 742 | |
| 746 | - $depts = array(); | |
| 743 | + $depts = []; | |
| 747 | 744 | |
| 748 | 745 | foreach ( $dependencies as $dept ) { |
| 749 | 746 | if ( ! empty( $scripts[ $dept ] ) ) { |
| 750 | 747 | $depts[] = $prefix . $dept; |
| @@ -758,22 +755,20 @@ | ||
| 758 | 755 | |
| 759 | 756 | /** |
| 760 | 757 | * Tell if debug is on. |
| 761 | 758 | * |
| 762 | - * @since 1.6.10 | |
| 763 | - * @author Grégory Viguier | |
| 759 | + * @since 1.6.10 | |
| 764 | 760 | * |
| 765 | 761 | * @return bool |
| 766 | 762 | */ |
| 767 | 763 | protected function is_debug() { |
| 768 | - return defined( 'IMAGIFY_DEBUG' ) && IMAGIFY_DEBUG || defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; | |
| 764 | + return ( defined( 'IMAGIFY_DEBUG' ) && IMAGIFY_DEBUG ) || ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ); | |
| 769 | 765 | } |
| 770 | 766 | |
| 771 | 767 | /** |
| 772 | 768 | * Tell if the admin bar item is displaying. |
| 773 | 769 | * |
| 774 | - * @since 1.6.10 | |
| 775 | - * @author Grégory Viguier | |
| 770 | + * @since 1.6.10 | |
| 776 | 771 | * |
| 777 | 772 | * @return bool |
| 778 | 773 | */ |
| 779 | 774 | protected function is_admin_bar_item_showing() { |