-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
class-compat.php
638 lines (568 loc) · 18.1 KB
/
class-compat.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
<?php
/**
* View Admin As - Class Compat
*
* @author Jory Hogeveen <info@keraweb.nl>
* @package View_Admin_As
*/
if ( ! defined( 'VIEW_ADMIN_AS_DIR' ) ) {
die();
}
/**
* Compatibility class.
*
* @author Jory Hogeveen <info@keraweb.nl>
* @package View_Admin_As
* @since 1.6.0
* @version 1.8.7
* @uses \VAA_View_Admin_As_Base Extends class
*/
final class VAA_View_Admin_As_Compat extends VAA_View_Admin_As_Base
{
/**
* The single instance of the class.
*
* @since 1.6.0
* @static
* @var \VAA_View_Admin_As_Compat
*/
private static $_instance = null;
/**
* Populate the instance.
*
* @since 1.6.0
* @since 1.6.1 `$vaa` param.
* @access protected
* @param \VAA_View_Admin_As $vaa The main VAA object.
*/
protected function __construct( $vaa ) {
self::$_instance = $this;
parent::__construct( $vaa );
}
/**
* Fix compatibility issues.
*
* @since 0.1.0
* @since 1.6.0 Moved `third_party_compatibility()` from `VAA_View_Admin_As`.
* @access public
* @return void
*/
public function init() {
$this->add_action( 'vaa_view_admin_as_init', array( $this, 'init_after' ) );
/**
* Add our caps to the members plugin.
* Hook `members_get_capabilities` also used by:
* - User Role Editor (URE) >> Own filter: `ure_full_capabilites`
* - WPFront User Role Editor
* - Capability Manager Enhanced >> Own filter: `capsman_get_capabilities`
* - Pods
* - Yoast SEO 5.8+
*
* @since 1.6.0
*/
$this->add_filter( 'members_get_capabilities', array( $this, 'get_vaa_capabilities' ) );
$this->add_action( 'members_register_cap_groups', array( $this, 'action_members_register_cap_group' ) );
/**
* Add our caps to the User Role Editor plugin (URE).
* @since 1.6.4
*/
$this->add_filter( 'ure_capabilities_groups_tree', array( $this, 'filter_ure_capabilities_groups_tree' ) );
$this->add_filter( 'ure_custom_capability_groups', array( $this, 'filter_ure_custom_capability_groups' ), 10, 2 );
/**
* Get all capabilities.
* @since 1.5.0
*/
$this->add_filter( 'view_admin_as_get_capabilities', array( $this, 'get_capabilities' ), 10, 2 );
/**
* WP Admin UI Customize 1.5.11+.
* @since 1.7.4
*/
// wauc_admin_bar_default_load
$this->add_filter( 'wauc_admin_bar_menu_add_nodes', array( $this, 'filter_wauc_admin_bar_menu_add_nodes' ), 10, 2 );
$this->add_filter( 'wauc_admin_bar_filter_load', array( $this, 'filter_wauc_admin_bar_filter_load' ) );
$this->add_filter( 'wauc_admin_bar_menu_widget_no_submenu', array( $this, 'filter_wauc_admin_bar_menu_widget_no_submenu' ) );
$this->add_filter( 'wauc_admin_bar_menu_widget_title_readonly_vaa', '__return_true' );
$this->add_filter( 'wauc_admin_bar_menu_widget_disable_target_vaa', '__return_true' );
}
/**
* Fix compatibility issues on load.
* Called from 'vaa_view_admin_as_init' hook (after loading all data).
*
* @since 1.6.1
* @access public
* @return void
*/
public function init_after() {
// Cache plugins.
if ( VAA_API::is_view_active() || defined( 'VAA_DOING_AJAX' ) ) {
// WP Rocket & WP Super Cache
if ( ! defined( 'DONOTCACHEPAGE' ) ) {
define( 'DONOTCACHEPAGE', true );
}
// WP Rocket.
// @link https://github.com/wp-media/wp-rocket-helpers/blob/master/cache/wp-rocket-no-cache-for-admins/wp-rocket-no-cache-for-admins.php#L71-L83
if ( ! defined( 'DONOTROCKETOPTIMIZE' ) ) {
define( 'DONOTROCKETOPTIMIZE', true );
}
// LiteSpeed.
// @link https://docs.litespeedtech.com/lscache/lscwp/api/#set-current-page-as-non-cacheable
do_action( 'litespeed_control_set_nocache', 'View Admin As' );
}
if ( VAA_API::is_user_modified() ) {
// Only apply the filter if the current user is modified.
$this->add_filter( 'pods_is_admin', array( $this, 'filter_pods_caps_check' ), 99, 2 );
}
}
/**
* Get's current capabilities and merges with capabilities from other plugins.
*
* @since 1.6.0
* @access public
* @see \VAA_View_Admin_As_Compat::init()
*
* @param array $caps The capabilities.
* @param bool[] $args Pass arguments to get only certain capabilities.
* @return array Both key and values are the capabilities.
*/
public function get_capabilities( $caps = array(), $args = array() ) {
$defaults = array(
'vaa' => true, // Get VAA related capabilities?
'wp' => true, // Get WP core related capabilities?
'plugins' => true, // Get capabilities from plugin hooks and filters?
);
$args = wp_parse_args( $args, $defaults );
if ( $args['vaa'] ) {
$caps = $this->get_vaa_capabilities( $caps );
}
if ( $args['wp'] ) {
$caps = $this->get_wordpress_capabilities( $caps );
}
if ( $args['plugins'] ) {
$caps = $this->get_plugin_capabilities( $caps );
}
// @since 1.8.0 Make sure both keys and values are capabilities.
$all_caps = array();
foreach ( $caps as $cap_key => $cap ) {
if ( is_string( $cap ) && ! is_numeric( $cap ) ) {
$all_caps[ $cap ] = $cap;
} elseif ( is_string( $cap_key ) && ! is_numeric( $cap_key ) ) {
$all_caps[ $cap_key ] = $cap_key;
}
}
return $all_caps;
}
/**
* Add our capabilities to an existing list of capabilities.
*
* @since 1.6.0
* @since 1.7.3 Renamed from `add_capabilities()`.
* @access public
* @see \VAA_View_Admin_As_Compat::init()
*
* @param array $caps The capabilities.
* @return array
*/
public function get_vaa_capabilities( $caps = array() ) {
// Allow VAA modules to add their capabilities.
foreach ( (array) apply_filters( 'view_admin_as_add_capabilities', array( 'view_admin_as' ) ) as $cap ) {
$caps[ $cap ] = $cap;
}
return $caps;
}
/**
* Get all capabilities from WP core or WP objects.
*
* @since 1.7.1
* @param array $caps The capabilities.
* @return array
*/
public function get_wordpress_capabilities( $caps = array() ) {
// @since 1.7.1 Store available capabilities existing in roles.
foreach ( $this->store->get_roles() as $role ) {
if ( is_array( $role->capabilities ) ) {
$role_caps = array_keys( $role->capabilities );
$caps = array_merge( array_combine( $role_caps, $role_caps ), $caps );
}
}
/**
* @since 1.7.1 Add post type and taxonomy caps.
* @since 1.7.3 Prevent duplicate array keys.
*/
$wp_objects = array_merge(
array_values( (array) get_post_types( array(), 'objects' ) ),
array_values( (array) get_taxonomies( array(), 'objects' ) )
);
foreach ( $wp_objects as $obj ) {
if ( ! empty( $obj->cap ) ) {
// WP stores the object caps as general_cap_name => actual_cap.
$caps = array_merge( array_combine( (array) $obj->cap, (array) $obj->cap ), $caps );
}
}
/**
* Other WordPress capabilities.
* @since 1.7.4 WordPress 4.9 capabilities.
* @since 1.8.3 WordPress 4.9.6 privacy capabilities.
*/
if ( VAA_API::validate_wp_version( '4.9' ) ) {
$caps['activate_plugin'] = 'activate_plugin';
$caps['deactivate_plugin'] = 'deactivate_plugin';
$caps['deactivate_plugins'] = 'deactivate_plugins';
$caps['install_languages'] = 'install_languages';
$caps['update_languages'] = 'update_languages';
}
if ( VAA_API::validate_wp_version( '4.9.6' ) ) {
$caps['erase_others_personal_data'] = 'erase_others_personal_data';
$caps['export_others_personal_data'] = 'export_others_personal_data';
$caps['manage_privacy_options'] = 'manage_privacy_options';
}
/**
* Network capabilities.
* @since 1.5.3
* @since 1.7.2 Added new WP 4.8 caps.
* https://make.wordpress.org/core/2017/05/22/multisite-focused-changes-in-4-8/
* @see https://codex.wordpress.org/Roles_and_Capabilities
*/
if ( is_multisite() ) {
$network_caps = array(
'manage_network',
'manage_sites',
'manage_network_users',
'manage_network_plugins',
'manage_network_themes',
'manage_network_options',
);
if ( VAA_API::validate_wp_version( '4.8' ) ) {
$network_caps[] = 'upgrade_network';
$network_caps[] = 'setup_network';
}
$caps = array_merge( array_combine( $network_caps, $network_caps ), $caps );
}
return $caps;
}
/**
* Get all capabilities from other plugins.
*
* Disable some PHPMD checks for this method.
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @todo Refactor to enable above checks?
*
* @since 1.7.1
* @param array $caps The capabilities.
* @return array
*/
public function get_plugin_capabilities( $caps = array() ) {
// WooCommerce caps are not accessible but are assigned to roles on install.
// get_wordpress_capabilities() will find them.
// @since 1.7.1 Gravity Forms.
if ( VAA_API::exists_callable( array( 'GFCommon', 'all_caps' ) ) ) {
$caps = array_merge( (array) GFCommon::all_caps(), $caps );
}
// @since 1.7.1 User Role Editor.
if ( VAA_API::exists_callable( array( 'URE_Own_Capabilities', 'get_caps' ) ) ) {
$caps = array_merge( (array) URE_Own_Capabilities::get_caps(), $caps );
}
// @since 1.8.6 Parse hooked caps.
foreach ( apply_filters( 'ure_full_capabilites', array() ) as $cap ) {
if ( is_array( $cap ) ) {
if ( empty( $cap['inner'] ) ) {
continue;
}
$cap = $cap['inner'];
}
$caps[ $cap ] = $cap;
}
// @since 1.7.1 WPFront User Role Editor.
if ( class_exists( 'WPFront_User_Role_Editor' ) && ! empty( WPFront_User_Role_Editor::$ROLE_CAPS ) ) {
$caps = array_merge( (array) WPFront_User_Role_Editor::$ROLE_CAPS, $caps );
}
// @since 1.7.1 User Roles and Capabilities.
if ( VAA_API::exists_callable( array( 'Solvease_Roles_Capabilities_User_Caps', 'solvease_roles_capabilities_caps' ) ) ) {
$caps = array_merge( (array) Solvease_Roles_Capabilities_User_Caps::solvease_roles_capabilities_caps(), $caps );
}
// @since 1.7.1 bbPress.
if ( function_exists( 'bbp_get_caps_for_role' ) ) {
if ( function_exists( 'bbp_get_keymaster_role' ) ) {
$bbp_keymaster_role = bbp_get_keymaster_role();
} else {
$bbp_keymaster_role = apply_filters( 'bbp_get_keymaster_role', 'bbp_keymaster' );
}
$caps = array_merge( (array) bbp_get_caps_for_role( $bbp_keymaster_role ), $caps );
}
// @since 1.7.1 BuddyPress.
if ( class_exists( 'BuddyPress' ) ) {
$caps = array_merge(
array(
'bp_moderate',
'bp_xprofile_change_field_visibility',
// @todo Check usage of capabilities below.
/*
'throttle',
'keep_gate',
'moderate_comments',
'edit_cover_image',
'edit_avatar',
'edit_favorites',
'edit_favorites_of',
'add_tag_to',
'edit_tag_by_on',
'change_user_password',
'moderate',
'browse_deleted',
'view_by_ip',
'write_posts',
'write_topic',
'write_topics',
'move_topic',
'stick_topic',
'close_topic',
'edit_topic',
'delete_topic',
'delete_forum',
'manage_forums',
'manage_tags',
*/
),
// @see bp_get_community_caps() >> bp-core-caps.php.
apply_filters( 'bp_get_community_caps', array() ),
$caps
);
} // End if().
// @since 1.7.4 Yoast SEO 5.5+ Load integration on front end.
if ( ! is_admin() ) {
/**
* @since 1.8.0 Yoast SEO 8.3+ - Check for API function.
* @link https://github.com/Yoast/wordpress-seo/pull/9365
*/
if ( function_exists( 'wpseo_get_capabilities' ) ) {
$caps = array_merge( (array) wpseo_get_capabilities(), $caps );
} elseif ( defined( 'WPSEO_VERSION' ) ) {
$caps = array_merge(
array(
// Yoast SEO caps.
'wpseo_bulk_edit',
'wpseo_edit_advanced_metadata',
'wpseo_manage_options',
),
$caps
);
}
}
// @since 1.8.6 Google Site Kit.
if ( defined( 'GOOGLESITEKIT_VERSION' ) ) {
// @todo https://github.com/JoryHogeveen/view-admin-as/issues/110
$caps = array_merge(
array(
'googlesitekit_authenticate',
'googlesitekit_setup',
'googlesitekit_view_posts_insights',
'googlesitekit_view_dashboard',
'googlesitekit_view_module_details',
'googlesitekit_manage_options',
'googlesitekit_publish_posts',
),
$caps
);
}
// Members.
if ( function_exists( 'members_get_plugin_capabilities' ) ) {
$caps = array_merge( (array) members_get_plugin_capabilities(), $caps );
}
// Get caps from multiple plugins through the Members filter.
$caps = array_merge( (array) apply_filters( 'members_get_capabilities', $caps ), $caps );
// Pods.
$caps = array_merge( (array) apply_filters( 'pods_roles_get_capabilities', $caps ), $caps );
return $caps;
}
/**
* Fix compatibility issues Pods Framework 2.0+.
*
* @since 1.0.1
* @since 1.6.0 Moved from `VAA_View_Admin_As`.
* @since 1.6.2 Check for all provided capabilities.
* @access public
* @see \VAA_View_Admin_As_Compat::init()
*
* @param bool $bool Boolean provided by the pods_is_admin hook (not used).
* @param array $caps String or Array provided by the pods_is_admin hook.
* @return bool
*/
public function filter_pods_caps_check( $bool, $caps ) {
foreach ( (array) $caps as $capability ) {
if ( $this->vaa->view()->current_view_can( $capability ) ) {
return true;
}
}
return false;
}
/**
* Add our capabilities to our own group in the Members plugin.
*
* @see members_register_cap_group()
*
* @since 1.6.0
* @access public
* @see \VAA_View_Admin_As_Compat::init()
*/
public function action_members_register_cap_group() {
if ( ! function_exists( 'members_register_cap_group' ) ) {
return;
}
// Register the vaa group.
members_register_cap_group(
'view_admin_as',
array(
'label' => esc_html__( 'View Admin As', VIEW_ADMIN_AS_DOMAIN ),
'caps' => $this->get_vaa_capabilities(),
'icon' => 'dashicons-visibility',
'diff_added' => true,
)
);
}
/**
* Add our our own capability group in the URE plugin.
*
* @since 1.6.4
* @access public
* @see \VAA_View_Admin_As_Compat::init()
* @see \URE_Capabilities_Groups_Manager::get_groups_tree()
* @param array $groups Current groups
* @return array
*/
public function filter_ure_capabilities_groups_tree( $groups ) {
$groups = (array) $groups;
$groups['view_admin_as'] = array(
'caption' => esc_html__( 'View Admin As', VIEW_ADMIN_AS_DOMAIN ),
'parent' => 'custom',
'level' => 3,
);
return $groups;
}
/**
* Add our capabilities to our own group in the URE plugin.
*
* @since 1.6.4
* @access public
* @see \VAA_View_Admin_As_Compat::init()
* @see \URE_Capabilities_Groups_Manager::get_cap_groups()
* @param array $groups Current capability groups.
* @param string $cap_id Capability identifier.
* @return array
*/
public function filter_ure_custom_capability_groups( $groups, $cap_id ) {
if ( in_array( $cap_id, $this->get_vaa_capabilities(), true ) ) {
$groups = (array) $groups;
$groups[] = 'view_admin_as';
}
return $groups;
}
/**
* Force user setting location when WAUC is active.
*
* Disable some PHPMD checks for this method.
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @todo Refactor to enable above checks?
*
* @since 1.7.4
* @access public
* @see \WP_Admin_UI_Customize::admin_bar_menu()
* @param array $wauc_nodes
* @param array $all_nodes
* @return array
*/
public function filter_wauc_admin_bar_menu_add_nodes( $wauc_nodes, $all_nodes ) {
$admin_menu_location = $this->store->get_userSettings( 'admin_menu_location' );
$vaa_root = VAA_View_Admin_As_Admin_Bar::$root;
$check = array(
'depth' => 'main',
);
if ( 'my-account' === $admin_menu_location ) {
$check['depth'] = 'sub';
$check['location'] = 'right';
$check['parent'] = $admin_menu_location;
}
// Compat when this node is added twice.
foreach ( $wauc_nodes as $location => $levels ) {
foreach ( $levels as $depth => $nodes ) {
foreach ( $nodes as $id => $node ) {
if ( isset( $node['id'] ) && $vaa_root === $node['id'] ) {
$current = array(
'depth' => $depth,
'location' => $location,
'parent' => $node['parent'],
);
foreach ( $check as $key => $val ) {
if ( $val !== $current[ $key ] ) {
unset( $wauc_nodes[ $location ][ $depth ][ $id ] );
break;
}
}
}
}
}
}
// Make sure all VAA nodes will be added.
$wauc_nodes['vaa'] = array( 'sub' => array() );
foreach ( $all_nodes as $node ) {
$id = ( ! empty( $node->id ) ) ? $node->id : null;
if ( $vaa_root !== $id && 0 === strpos( $id, $vaa_root ) ) {
$wauc_nodes['vaa']['sub'][] = (array) $node;
}
}
return $wauc_nodes;
}
/**
* Remove our nodes from the WAUC admin bar editor.
* @since 1.7.4
* @access public
* @see \WP_Admin_UI_Customize::admin_bar_filter_load()
* @param array $all_nodes
* @return array
*/
public function filter_wauc_admin_bar_filter_load( $all_nodes ) {
if ( empty( $all_nodes['right'] ) ) {
return $all_nodes;
}
$slug = VAA_View_Admin_As_Admin_Bar::$root;
foreach ( (array) $all_nodes['right'] as $location => $nodes ) {
if ( 0 !== strpos( $location, 'sub' ) ) {
continue;
}
foreach ( $nodes as $key => $node ) {
// Check if node ID starts with `vaa-` and node parent starts with `vaa`.
if ( 0 === strpos( $node->id, $slug . '-' ) && 0 === strpos( $node->parent, $slug ) ) {
unset( $all_nodes['right'][ $location ][ $key ] );
}
}
}
return $all_nodes;
}
/**
* Remove submenu options for WAUC.
* @since 1.7.4
* @see \WP_Admin_UI_Customize::admin_bar_menu_widget()
* @param array $no_submenu
* @return array
*/
public function filter_wauc_admin_bar_menu_widget_no_submenu( $no_submenu ) {
$no_submenu[] = VAA_View_Admin_As_Admin_Bar::$root;
return $no_submenu;
}
/**
* Main Instance.
*
* Ensures only one instance of this class is loaded or can be loaded.
*
* @since 1.6.0
* @access public
* @static
* @param \VAA_View_Admin_As $caller The referrer class.
* @return \VAA_View_Admin_As_Compat $this
*/
public static function get_instance( $caller = null ) {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self( $caller );
}
return self::$_instance;
}
} // End class VAA_View_Admin_As_Compat.