-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.php
591 lines (531 loc) · 17.8 KB
/
app.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
<?php
/**
* @package Restrict User Access
* @author Joachim Jensen <joachim@dev.institute>
* @license GPLv3
* @copyright 2024 by Joachim Jensen
*/
defined('ABSPATH') || exit;
/**
* @deprecated
*/
final class RUA_App
{
/**
* Plugin version
*/
const PLUGIN_VERSION = '2.7';
/**
* Prefix for metadata
* Same as wp-content-aware-engine
*/
const META_PREFIX = '_ca_';
/**
* Post Type for restriction
*/
const TYPE_RESTRICT = \RestrictUserAccess\Level\PostType::NAME;
/**
* Post type statuses
*/
const STATUS_ACTIVE = 'publish';
const STATUS_INACTIVE = 'draft';
const STATUS_SCHEDULED = 'future';
/**
* Capability to manage restrictions
* @deprecated use capability in post type object
*/
const CAPABILITY = 'manage_options';
const BASE_SCREEN = 'wprua';
const ICON_SVG = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCIgdmlld0JveD0iMCAwIDIwIDIwIj48ZyBmaWxsPSIjYTBhNWFhIj48cGF0aCBkPSJNMTAuMDEyIDE0LjYyNUw1Ljc4IDEyLjI3Yy0xLjkwNi42NjQtMy42MDUgMS43Ni00Ljk4IDMuMTc4IDIuMTA1IDIuNzcgNS40MzYgNC41NiA5LjE4NSA0LjU2IDMuNzY2IDAgNy4xMTItMS44MDIgOS4yMTUtNC41OTMtMS4zOC0xLjQwNC0zLjA3LTIuNDk2LTQuOTctMy4xNTRsLTQuMjE4IDIuMzY3em0tLjAwNS0xNC42M0M3LjQxMi0uMDA1IDUuMzEgMS45MSA1LjMxIDQuMjhoOS4zOTNjMC0yLjM3LTIuMS00LjI4Ni00LjY5Ni00LjI4NnptNi4xMjYgMTAuNzFjLjE1OC0uMDMyLjY0LS4yMzIuNjMtLjMzMy0uMDI1LS4yNC0uNjg2LTUuNTg0LS42ODYtNS41ODRzLS40MjItLjI3LS42ODYtLjI5M2MuMDI0LjIxLjY5IDUuNzYuNzQ1IDYuMjF6bS0xMi4yNTMgMGMtLjE1OC0uMDMyLS42NC0uMjMyLS42My0uMzMzLjAyNS0uMjQuNjg2LTUuNTg0LjY4Ni01LjU4NHMuNDItLjI3LjY4Ni0uMjkzYy0uMDIuMjEtLjY5IDUuNzYtLjc0MiA2LjIxeiIvPjxwYXRoIGQ9Ik0xMCAxMy45NjdoLjAyM2wuOTc1LS41NXYtNC4yMWMuNzgtLjM3NyAxLjMxNC0xLjE3MyAxLjMxNC0yLjA5NyAwLTEuMjg1LTEuMDM1LTIuMzIzLTIuMzItMi4zMjNTNy42NyA1LjgyNSA3LjY3IDcuMTFjMCAuOTIzLjUzNSAxLjcyIDEuMzE1IDIuMDkzVjEzLjRsMS4wMTYuNTY3em0tMS43NjQtLjk4NXYtLjAzNWMwLTMuNjEtMS4zNS02LjU4My0zLjA4My02Ljk2bC0uMDMuMy0uNTIgNC42NyAzLjYzMyAyLjAyNXptMy41Ni0uMDM1YzAgLjAxNCAwIC4wMTguMDAzLjAyM2wzLjYxLTIuMDI1LS41My00LjY4LS4wMjgtLjI3M2MtMS43MjMuNC0zLjA1NyAzLjM2Mi0zLjA1NyA2Ljk1NXoiLz48L2c+PC9zdmc+';
/**
* @var array
*/
private $levels = [];
/**
* @var array<int, int>
*/
private $level_extends_map = [];
/**
* @var array<int, int[]>
*/
private $level_extended_by_map = [];
/**
* @var WP_DB_Updater
*/
private $db_updater;
/**
* @var RUA_App
*/
private static $_instance;
/**
* @var RUA_Level_Manager
*/
public $level_manager;
public function __construct()
{
$this->level_manager = new RUA_Level_Manager();
$this->db_updater = new WP_DB_Updater('rua_plugin_version', self::PLUGIN_VERSION);
new RUA_Nav_Menu();
if (is_admin()) {
$rua_fs = rua_fs();
new RUA_Level_Overview();
new RUA_Level_Edit();
new RUA_Settings_Page();
new RUA_Admin_Screen_Account($rua_fs);
new RUA_Admin_Screen_Addons($rua_fs);
add_action(
'admin_enqueue_scripts',
[$this,'load_admin_scripts'],
999
);
add_action(
'show_user_profile',
[$this,'add_field_access_level']
);
add_action(
'edit_user_profile',
[$this,'add_field_access_level']
);
add_action(
'personal_options_update',
[$this,'save_user_profile']
);
add_action(
'edit_user_profile_update',
[$this,'save_user_profile']
);
add_action(
'delete_user',
[$this, 'sync_user_deletion'],
1,
3
);
add_filter(
'manage_users_columns',
[$this,'add_user_column_headers']
);
add_filter(
'manage_users_custom_column',
[$this,'add_user_columns'],
10,
3
);
add_filter(
'cas/metadata/populate',
[$this,'add_levels_to_visibility']
);
$file = plugin_basename(plugin_dir_path(__FILE__)) . '/restrict-user-access.php';
add_filter(
'plugin_action_links_' . $file,
[$this,'plugin_action_links'],
10,
4
);
} else {
new RUA_Admin_Bar();
}
add_action('wpca/loaded', [$this, 'ensure_wpca_loaded']);
add_shortcode(
'login-form',
[$this,'shortcode_login_form']
);
add_filter(
'cas/user_visibility',
[$this,'sidebars_check_levels']
);
}
public function ensure_wpca_loaded()
{
//hook early, other plugins might add dynamic caps later
//fixes problem with WooCommerce Orders
//todo: verify if this is still an issue, now that we run in wpca/loaded
add_filter(
'user_has_cap',
[$this,'user_level_has_cap'],
9,
4
);
}
/**
* Override user caps with level caps.
*
* @param array $allcaps
* @param string $cap
* @param array $args {
* @type string [0] Requested capability
* @type int [1] User ID
* @type WP_User [2] Associated object ID (User object)
* }
* @param WP_User $user
*
* @return array
*/
public function user_level_has_cap($allcaps, $cap, $args, $user)
{
return rua_get_user($user)->get_caps($allcaps);
}
/**
* Instantiates and returns class singleton
*
* @since 0.1
* @return RUA_App
*/
public static function instance()
{
if (!self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* @since 1.1
* @return WP_DB_Updater
*/
public function get_updater()
{
return $this->db_updater;
}
/**
* Add Levels to sidebar visibility metadata list
*
* @since 0.12
* @param WPCACollection $metadata
*/
public function add_levels_to_visibility($metadata)
{
$visibility = $metadata->get('visibility');
$list = $visibility->get_input_list();
if (isset($list['rua-levels'])) {
return $metadata;
}
$levels = $this->get_levels();
if ($levels) {
$options = [];
foreach ($levels as $level) {
$options[$level->ID] = $level->post_title;
}
if (!defined('CAS_App::PLUGIN_VERSION')
|| version_compare(CAS_App::PLUGIN_VERSION, '3.8', '<')) {
$list = $list + $options;
} else {
$list['rua-levels'] = [
'label' => __('Access Levels', 'restrict-user-access'),
'options' => $options
];
}
$visibility->set_input_list($list);
}
return $metadata;
}
/**
* Check if user level has access to sidebar
*
* @since 0.12
* @param array $visibility
* @return array
*/
public function sidebars_check_levels($visibility)
{
$user = rua_get_user();
if (!$user->has_global_access()) {
return array_merge($visibility, $user->get_level_ids());
}
return array_merge($visibility, array_keys($this->get_levels()));
}
/**
* Get login form in shotcode
*
* @version 0.9
* @param array $atts
* @param string $content
* @return string
*/
public function shortcode_login_form($atts, $content = null)
{
if (is_user_logged_in()) {
return $content;
}
$a = shortcode_atts([
'remember' => true,
'redirect' => '',
'form_id' => 'loginform',
'id_username' => 'user_login',
'id_password' => 'user_pass',
'id_remember' => 'rememberme',
'id_submit' => 'wp-submit',
'label_username' => __('Username'),
'label_password' => __('Password'),
'label_remember' => __('Remember Me'),
'label_log_in' => __('Log In'),
'value_username' => '',
'value_remember' => false
], $atts);
$a['echo'] = false;
if (!$a['redirect']) {
$parts = parse_url(home_url());
$root = "{$parts['scheme']}://{$parts['host']}";
if (isset($parts['port']) && $parts['port']) {
$root .= ':' . $parts['port'];
}
if (isset($_GET['redirect_to'])) {
$a['redirect'] = $root . urldecode($_GET['redirect_to']);
} else {
$a['redirect'] = $root . add_query_arg(null, null);
}
}
return wp_login_form($a);
}
/**
* Add Access Level to user profile
*
* @since 0.3
* @param WP_User $user
*/
public function add_field_access_level($user)
{
$post_type = get_post_type_object(self::TYPE_RESTRICT);
if (!current_user_can($post_type->cap->edit_posts) || is_network_admin()) {
return;
}
$rua_user = rua_get_user($user);
$user_levels = [];
$link = '<a target="_blank" href="https://dev.institute/docs/restrict-user-access/getting-started/add-level-members/">' . __('Visitor Traits', 'restrict-user-access') . '</a>';
foreach ($rua_user->level_memberships() as $membership) {
$user_levels[] = $membership->get_level_id();
} ?>
<h3><?php _e('Access Control', 'restrict-user-access'); ?>
</h3>
<table class="form-table">
<tr>
<th><label for="_ca_level"><?php _e('Level Memberships', 'restrict-user-access'); ?></label>
</th>
<td>
<div style="width:25em;"><select style="width:100%;" class="js-rua-levels" multiple="multiple"
name="_ca_level[]"
data-value="<?php echo esc_html(implode(',', $user_levels)); ?>"></select>
</div>
<p class="description"><?php printf(__('Access Levels provided by %s will not be listed here.', 'restrict-user-access'), $link); ?>
</p>
</td>
</tr>
</table>
<?php
}
/**
* Save additional data for
* user profile
*
* @since 0.3
* @param int $user_id
* @return void
*/
public function save_user_profile($user_id)
{
$post_type = get_post_type_object(self::TYPE_RESTRICT);
if (!current_user_can($post_type->cap->edit_posts) || is_network_admin()) {
return;
}
$user = rua_get_user($user_id);
$new_levels = isset($_POST[self::META_PREFIX . 'level']) ? (array) $_POST[self::META_PREFIX . 'level'] : [];
$user_levels = [];
foreach ($user->level_memberships() as $membership) {
$user_levels[$membership->get_level_id()] = 1;
}
wp_defer_comment_counting(true);
foreach ($new_levels as $level) {
if (isset($user_levels[$level])) {
unset($user_levels[$level]);
} else {
$user->add_level($level);
}
}
foreach ($user_levels as $level => $value) {
$user->remove_level($level);
}
wp_defer_comment_counting(false);
}
/**
* Add column headers on
* User overview
*
* @since 0.3
* @param array $column
*/
public function add_user_column_headers($columns)
{
$new_columns = [];
foreach ($columns as $key => $title) {
$new_columns[$key] = $title;
if ($key == 'role') {
$new_columns['level'] = __('Access Levels', 'restrict-user-access');
}
}
return $new_columns;
}
/**
* Add columns on user overview
*
* @since 0.3
* @param string $output
* @param string $column_name
* @param int $user_id
*/
public function add_user_columns($output, $column_name, $user_id)
{
switch ($column_name) {
case 'level':
$level_links = [];
foreach (rua_get_user($user_id)->level_memberships() as $membership) {
$level_links[] = sprintf(
'<a href="%s">%s%s</a>',
get_edit_post_link($membership->get_level_id()),
$membership->level()->get_title(),
!$membership->is_active() ? ' (' . $membership->get_status() . ') ' : ''
);
}
sort($level_links);
$output = implode(', ', $level_links);
break;
default:
}
return $output;
}
/**
* @param int $level_id
*
* @return int[]
*/
public function get_level_extends($level_id)
{
$levels = [];
while (isset($this->level_extends_map[$level_id])) {
$level_id = $this->level_extends_map[$level_id];
$levels[] = $level_id;
}
return $levels;
}
/**
* @param int $level_id
*
* @return int[]
*/
public function get_level_extended_by($level_id)
{
$levels = [];
if (isset($this->level_extended_by_map[$level_id])) {
foreach ($this->level_extended_by_map[$level_id] as $level) {
$levels[] = $level;
$levels = array_merge($levels, $this->get_level_extended_by($level));
}
}
return $levels;
}
/**
* Get all levels
*
* @since 0.3
* @return array
*/
public function get_levels()
{
if (!$this->levels) {
$levels = get_posts([
'numberposts' => -1,
'post_type' => self::TYPE_RESTRICT,
'post_status' => [
self::STATUS_ACTIVE,
self::STATUS_INACTIVE,
self::STATUS_SCHEDULED
],
'update_post_meta_cache' => true
]);
foreach ($levels as $level) {
$this->levels[$level->ID] = $level;
if ($level->post_parent) {
$this->level_extends_map[$level->ID] = $level->post_parent;
if (!isset($this->level_extended_by_map[$level->post_parent])) {
$this->level_extended_by_map[$level->post_parent] = [];
}
$this->level_extended_by_map[$level->post_parent][] = $level->ID;
}
}
}
return $this->levels;
}
public function sync_user_deletion($id, $reassign, $user)
{
global $wpdb;
$entities = $wpdb->get_results( $wpdb->prepare( "SELECT comment_post_ID, comment_ID FROM $wpdb->comments
WHERE comment_type = %s AND user_id = %d",
RUA_User_Level::ENTITY_TYPE,
$id
));
wp_defer_comment_counting(true);
foreach($entities as $entity) {
wp_delete_comment($entity->comment_ID, true);
wp_update_comment_count($entity->comment_post_ID);
}
wp_defer_comment_counting(false);
}
/**
* Add actions to plugin in Plugins screen
*
* @version 1.0
* @param array $actions
* @param string $plugin_file
* @param [type] $plugin_data
* @param [type] $context
* @return array
*/
public function plugin_action_links($actions, $plugin_file, $plugin_data, $context)
{
$new_actions = [];
$new_actions['docs'] = '<a href="https://dev.institute/docs/restrict-user-access/?utm_source=plugin&utm_medium=referral&utm_content=plugin-list&utm_campaign=rua" target="_blank">' . __('Documentation & FAQ', 'restrict-user-access') . '</a>';
return array_merge($new_actions, $actions);
}
/**
* Load scripts and styles for administration
*
* @since 0.1
* @param string $hook
* @return void
*/
public function load_admin_scripts($hook)
{
$current_screen = get_current_screen();
if ($current_screen->id == 'nav-menus' || $current_screen->id == 'user-edit' || $current_screen->id == 'profile') {
//todo: enqueue automatically in wpcacore
if (wp_script_is('select2', 'registered')) {
wp_deregister_script('select2');
}
wp_register_script(
'select2',
plugins_url('/lib/wp-content-aware-engine/assets/js/select2.min.js', __FILE__),
['jquery'],
'4.0.3',
false
);
wp_enqueue_style(self::META_PREFIX . 'condition-groups');
$levels = [];
foreach ($this->get_levels() as $level) {
$levels[] = [
'id' => $level->ID,
'text' => $level->post_title
];
}
wp_enqueue_script('rua/admin/suggest-levels', plugins_url('/assets/js/suggest-levels.min.js', __FILE__), ['select2','jquery'], self::PLUGIN_VERSION);
wp_localize_script('rua/admin/suggest-levels', 'RUA', [
'search' => __('Search for Levels', 'restrict-user-access'),
'levels' => $levels
]);
}
}
/**
* @return RUA_Collection|\RestrictUserAccess\Membership\Automator\AbstractAutomator[]
*/
public function get_level_automators()
{
return rua()->get(\RestrictUserAccess\Membership\Automator\AutomatorService::class)
->get_level_automators();
}
}