-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathasset-queue-manager.php
560 lines (452 loc) · 15.5 KB
/
asset-queue-manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/**
* Plugin Name: Asset Queue Manager
* Plugin URI: https://github.com/zao-web/asset-queue-manager
* Description: A tool for front-end experts to take control of all scripts and styles enqueued on their site.
* Version: 2.0.0
* Author: Nate Wright
* Author URI: https://zao.is
* License: GNU General Public License v2.0 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Text Domain: asset-queue-manager
* Domain Path: /languages/
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
defined( 'ABSPATH' ) || die( '(╯°□°)╯︵ ┻━┻' );
class Asset_Queue_Manager {
/**
* Sets queried object's query vars.
*
* @since 2.0.0
*/
public $object_query_vars = array();
/**
* The single instance of this class
*/
private static $instance;
/**
* Path to the plugin directory
*/
public static $plugin_dir;
/**
* URL to the plugin
*/
public static $plugin_url;
/**
* Array of assets to be managed
*/
public $assets;
/**
* Create or retrieve the single instance of the class
*
* @since 0.1
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new Asset_Queue_Manager;
self::$plugin_dir = untrailingslashit( plugin_dir_path( __FILE__ ) );
self::$plugin_url = untrailingslashit( plugin_dir_url( __FILE__ ) );
self::$instance->init();
}
return self::$instance;
}
/**
* Initialize the plugin
*/
public function init() {
add_action( 'wp', array( $this, 'set_object_query_vars' ), 9999 );
// Textdomain
add_action( 'init', array( $this, 'load_textdomain' ) );
// Handle queue management requests via Ajax
add_action( 'wp_ajax_nopriv_aqm-modify-asset' , array( $this , 'ajax_nopriv_default' ) );
add_action( 'wp_ajax_aqm-modify-asset' , array( $this, 'ajax_modify_asset' ) );
// Process an emergency restore request
add_action( 'init', array( $this, 'restore_queue' ) );
// Add the rest of the hooks which are only needed when the
// admin bar is showing
add_action( 'admin_bar_init', array( $this, 'admin_bar_init' ) );
// Deregister assets
add_action( 'wp_head' , array( $this, 'deregister_assets' ), 7 );
add_action( 'wp_footer', array( $this, 'deregister_assets' ) );
}
public function set_object_query_vars( $wp ) {
$q = $GLOBALS['wp_query'];
$vars = [
'post_type' => $q->get( 'post_type' ),
'is_front_page' => is_front_page()
];
foreach ( $q as $key => $val ) {
if ( $val && false !== strpos( $key, 'is_' ) ) {
$vars[ $key ] = $val;
}
}
$object = $q->get_queried_object();
if ( $object && isset( $object->post_type ) ) {
$vars['post_type'] = $object->post_type;
}
if ( $object && isset( $object->ID ) ) {
$vars['id'] = $object->ID;
}
$this->object_query_vars = $vars;
}
/**
* Add the hooks to display the asset panel in the admin bar
* @since 0.0.1
*/
public function admin_bar_init() {
if ( ! is_super_admin() || !is_admin_bar_showing() || $this->is_wp_login() ) {
return;
}
// Add links to the plugin listing on the installed plugins page
add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2);
// Don't bother showing the panel in the admin area
if ( is_admin() ) {
return;
}
// Enqueue assets for the control panel
add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ) );
// Store all assets enqueued in the head
add_action( 'wp_head', array( $this, 'store_head_assets' ), 1000 );
// Store any new assets enqueued in the footer
add_action( 'wp_footer', array( $this, 'store_footer_assets' ), 1000 );
// Add the Assets item to the admin bar
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ) );
// Print the assets panel in the footer
add_action( 'wp_footer', array( $this, 'print_assets_panel' ), 1000 );
}
/**
* Load the plugin textdomain for localistion
* @since 0.0.1
*/
public function load_textdomain() {
load_plugin_textdomain( 'asset-queue-manager', false, plugin_basename( dirname( __FILE__ ) ) . '/languages/' );
}
/**
* Check if we're on the login page, because the admin bar isn't
* shown there. Thanks to debug-bar for the heads-up.
* https://wordpress.org/plugins/debug-bar/
*
* @since 0.0.1
*/
public function is_wp_login() {
return 'wp-login.php' == basename( $_SERVER['SCRIPT_NAME'] );
}
/**
* Enqueue the front-end CSS and Javascript for the control panel
* @since 0.0.1
*/
public function register_assets() {
wp_enqueue_style( 'asset-queue-manager', self::$plugin_url . '/assets/css/aqm.css' );
// Load unminified scripts in debug mode
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
wp_enqueue_script( 'asset-queue-manager', self::$plugin_url . '/assets/js/aqm.js', array( 'jquery' ), '', true );
} else {
wp_enqueue_script( 'asset-queue-manager', self::$plugin_url . '/assets/js/aqm.min.js', array( 'jquery' ), '', true );
}
// Add translateable strings, nonce, and URLs for ajax requests
wp_localize_script(
'asset-queue-manager',
'aqm',
array(
'nonce' => wp_create_nonce( 'asset-queue-manager' ),
'siteurl' => get_bloginfo( 'url' ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'strings' => array(
'head_scripts' => __( 'Head Scripts', 'asset-queue-manager' ),
'footer_scripts' => __( 'Footer Scripts', 'asset-queue-manager' ),
'head_styles' => __( 'Head Styles', 'asset-queue-manager' ),
'footer_styles' => __( 'Footer Styles', 'asset-queue-manager' ),
'dequeued_scripts' => __( 'Dequeued Scripts', 'asset-queue-manager' ),
'dequeued_styles' => __( 'Dequeued Styles', 'asset-queue-manager' ),
'no_src' => __( 'This asset handle calls its dependent assets but loads no source files itself.', 'asset-queue-manager' ),
'requeued' => __( 'This asset is no longer being dequeued. Reload the page to view where it is enqueued.', 'asset-queue-manager' ),
'deps' => __( 'Dependencies:', 'asset-queue-manager' ),
'dequeue' => __( 'Dequeue Asset', 'asset-queue-manager' ),
'enqueue' => __( 'Stop Dequeuing', 'asset-queue-manager' ),
'view' => __( 'View Asset', 'asset-queue-manager' ),
'sending' => __( 'Sending Request', 'asset-queue-manager' ),
'unknown_error' => __( 'There was an unknown error with this request. Sorry.', 'asset-queue-manager' )
),
)
);
}
/**
* Store assets found in the list of enqueued assets
* @since 0.0.1
*/
public function store_asset_list( $enqueued_slugs, $asset_data, $location, $type ) {
foreach( $enqueued_slugs as $slug ) {
$this->store_asset( $slug, $asset_data[ $slug ], $location, $type );
}
}
/**
* Store a single asset's data
* @since 0.0.1
*/
public function store_asset( $slug, $data, $location, $type ) {
if ( !isset( $this->assets[ $location ] ) ) {
$this->assets[ $location ] = array();
}
if ( !isset( $this->assets[ $location ][ $type ] ) ) {
$this->assets[ $location ][ $type ] = array();
}
if ( $this->is_asset_stored( $slug, $location, $type ) ) {
return;
}
$this->assets[ $location ][ $type ][ $slug ] = $data;
}
/**
* Check if an asset has already been added to our list
* @since 0.0.1
*/
public function is_asset_stored( $slug, $location, $type ) {
// Only check in the footer
if ( $location !== 'footer' ) {
return false;
}
if ( isset( $this->assets[ 'head' ] ) && isset( $this->assets[ 'head' ][ $type ] ) && isset( $this->assets[ 'head' ][ $type ][ $slug ] ) ) {
return true;
}
return false;
}
/**
* Store assets enqueued in the head
* @since 0.0.1
*/
public function store_head_assets() {
global $wp_scripts;
$this->store_asset_list( $wp_scripts->done, $wp_scripts->registered, 'head', 'scripts' );
global $wp_styles;
$this->store_asset_list( $wp_styles->done, $wp_styles->registered, 'head', 'styles' );
}
/**
* Store assets enqueued in the footer
* @since 0.0.1
*/
public function store_footer_assets() {
global $wp_scripts;
$this->store_asset_list( $wp_scripts->done, $wp_scripts->registered, 'footer', 'scripts' );
global $wp_styles;
$this->store_asset_list( $wp_styles->done, $wp_styles->registered, 'footer', 'styles' );
}
/**
* Retrieve assets dequeued by this plugin
* @since 0.0.1
*/
public function get_dequeued_assets() {
if ( !isset( $this->assets['dequeued'] ) ) {
$this->assets['dequeued'] = get_option( 'aqm-dequeued' );
}
return $this->assets['dequeued'];
}
/**
* Deregister all dequeued assets. This should be called before
* wp_head and wp_footer.
* @since 0.0.1
*/
public function deregister_assets() {
$this->get_dequeued_assets();
if ( !empty( $this->assets['dequeued']['scripts'] ) ) {
foreach( $this->assets['dequeued']['scripts'] as $handle => $asset ) {
wp_deregister_script( $handle );
}
}
if ( !empty( $this->assets['dequeued']['styles'] ) ) {
foreach( $this->assets['dequeued']['styles'] as $handle => $asset ) {
wp_deregister_style( $handle );
}
}
}
/**
* Add an Assets item to the admin bar menu
* @since 0.0.1
*/
public function admin_bar_menu( $wp_admin_bar ) {
if ( is_admin() ) {
return;
}
$recovery_message = sprintf( __( 'The Asset Queue Manager panel did not load. This can happen if jQuery is not being loaded on the page. If you have encountered this error after dequeuing an asset by mistake, you can %srestore all assets%s dequeued by Asset Queue Manager. This message is only shown to administrators.', 'asset-queue-manager' ), '<a href="' . admin_url() . '?aqm=restore">', '</a>' );
$wp_admin_bar->add_node(
array(
'id' => 'asset-queue-manager',
'parent' => 'top-secondary',
'title' => __( 'Assets', 'asset-queue-manager' ),
'meta' => array(
'html' => '<div class="inactive"><p>' . $recovery_message . '</p></div>'
)
)
);
}
/**
* Print the assets panel and pass the assets array to the script
* for loading. We can't use wp_localize_script() because this has
* to come after the last enqueue opportunity.
* @since 0.0.1
*/
public function print_assets_panel() {
// Add dequeued assets to the $assets array
$this->get_dequeued_assets();
$data = array(
'assets' => $this->assets,
'notices' => $this->get_notices(),
'query_vars' => $this->object_query_vars
);
?>
<div id="aqm-panel" class="inactive"></div>
<script type='text/javascript'>
/* <![CDATA[ */
var aqmData = <?php echo json_encode( $data ); ?>
/* ]]> */
</script>
<?php
}
/**
* Define the notices and warnings to display for special assets
* @since 0.0.1
*/
public function get_notices() {
$notices = array(
'core' => array(
'msg' => __( 'This asset is part of WordPress core. Dequeuing this asset could cause serious problems, including breaking the admin bar.', 'asset-queue-manager' ),
'handles' => array(
'jquery',
'jquery-core',
'jquery-migrate',
),
),
'adminbar' => array(
'msg' => __( 'This asset is commonly loaded with the admin bar for logged in users. It may not be loaded when logged-out users visit this page. Dequeuing this asset could break the admin bar, including this asset manager.', 'asset-queue-manager' ),
'handles' => array(
'open-sans',
'dashicons',
'admin-bar',
),
),
'self' => array(
'msg' => __( 'This asset is loaded by Asset Queue Manager. It will only be loaded for admin users and dequeuing it will prevent you from managing other assets.', 'asset-queue-manager' ),
'handles' => array(
'asset-queue-manager',
)
),
);
return apply_filters( 'aqm_notices', $notices );
}
/**
* Handle all ajax requests from logged out users
* @since 0.0.1
*/
public function ajax_nopriv_default() {
wp_send_json_error(
array(
'error' => 'loggedout',
'msg' => __( 'You have been logged out. Please login again to perform this request.', 'asset-queue-manager' ),
)
);
}
/**
* Handle ajax request to dequeue or re-enqueue an asset
* @since 0.0.1
*/
public function ajax_modify_asset() {
if ( ! check_ajax_referer( 'asset-queue-manager', 'nonce' ) || !is_super_admin() ) {
$this->ajax_nopriv_default();
}
if ( empty( $_POST['handle'] ) || empty( $_POST['type'] ) || empty( $_POST['asset_data'] ) ) {
wp_send_json_error(
array(
'error' => 'noasset',
'msg' => __( 'There was an error with this dequeue request. No asset information was passed.', 'asset-queue-manager' ),
'post' => $_POST
)
);
}
if ( $_POST['type'] !== 'scripts' && $_POST['type'] !== 'styles' ) {
wp_send_json_error(
array(
'error' => 'badtype',
'msg' => __( 'There was an error with this dequeue request. The asset type was not recognized.', 'asset-queue-manager' ),
'post' => $_POST
)
);
}
$handle = sanitize_key( $_POST['handle'] );
$type = sanitize_key( $_POST['type'] );
$this->get_dequeued_assets();
// Initialize the array if nothing's been dequeued yet
if ( empty( $this->assets['dequeued'][ $type ] ) ) {
$this->assets['dequeued'][ $type ] = array();
}
// Handle dequeue request
if ( $_POST['dequeue'] === 'true' ) {
if ( in_array( $handle, $this->assets['dequeued'][ $type ] ) ) {
wp_send_json_error(
array(
'error' => 'alreadydequeued',
'msg' => __( 'This asset has already been dequeued. If the asset is still being loaded, the author may not have properly enqueued the asset using the wp_enqueue_* functions.', 'asset-queue-manager' ),
)
);
}
$this->assets['dequeued'][ $type ][ $handle ] = $_POST['asset_data'];
update_option( 'aqm-dequeued', $this->assets['dequeued'] );
wp_send_json_success(
array(
'type' => $type,
'handle' => $handle,
'option' => $this->assets['dequeued'],
'dequeue' => true
)
);
// Handle enqueue request
} else {
unset( $this->assets['dequeued'][ $type ][ $handle ] );
update_option( 'aqm-dequeued', $this->assets['dequeued'] );
wp_send_json_success(
array(
'type' => $type,
'handle' => $handle,
'option' => $this->assets['dequeued'],
'dequeue' => false
)
);
}
}
/**
* Delete dequeue option so that no assets are being blocked
*
* This is an emergency restore function in case people get
* themselves into a bit of a bind. Don't want them to have to get
* into the database to do this.
*
* @since 0.0.1
*/
public function restore_queue() {
if ( empty( $_REQUEST['aqm'] ) || $_REQUEST['aqm'] !== 'restore' || !is_super_admin() ) {
return;
}
delete_option( 'aqm-dequeued' );
}
/**
* Add links to the plugin listing on the installed plugins page
* @since 0.0.1
*/
public function plugin_action_links( $links, $plugin ) {
if ( $plugin == plugin_basename( __FILE__ ) ) {
$links['restore'] = '<a href="' . admin_url() . '?aqm=restore" title="' . __( 'Restore any assets dequeued by this plugin.', 'asset-queue-manager' ) . '">' . __( 'Restore Dequeued Assets', 'asset-queue-manager' ) . '</a>';
}
return $links;
}
}
function aqm_init() {
return Asset_Queue_Manager::get_instance();
}
add_action( 'plugins_loaded', 'aqm_init' );