PluginProbe
Age Gate / 3.7.3
Age Gate v3.7.3
3.7.3 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 All 113 releases
age-gate / src / Admin / Controller / AccessController.php

AccessController.php in Age Gate 3.7.3, at src/Admin/Controller/AccessController.php

109 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AgeGate\Admin\Controller;
4
5 use Asylum\Utility\Arr;
6 use Asylum\Utility\Notice;
7 use Asylum\Validation\Validator;
8 use AgeGate\Common\Admin\AbstractController;
9 use AgeGate\Common\Immutable\Constants as Immutable;
10
11 class AccessController extends AbstractController
12 {
13 // const PERMISSION = Constants::RESTRICTIONS;
14 public const PERMISSION = Immutable::ACCESS;
15 public const OPTION = Immutable::OPTION_ACCESS;
16
17 protected $template = 'access';
18
19 public function register(): void
20 {
21 $this->menu(__('Access', 'age-gate'), self::PERMISSION);
22 }
23
24 protected function required(): bool
25 {
26 return current_user_can(self::PERMISSION);
27 }
28
29 protected function data(): array
30 {
31 global $wp_roles;
32
33 return [
34 'roles' => $wp_roles->roles ?? [],
35 'sections' => Immutable::AGE_GATE_PERMISSION_ARRAY,
36 ];
37 }
38
39 public function store()
40 {
41
42 // check nonce & permission
43 if (!wp_verify_nonce(sanitize_key($_POST['_wpnonce'] ?? ''), 'age_gate_access') || !current_user_can(static::PERMISSION)) {
44 wp_die('Disallowed action');
45 }
46
47 global $wp_roles;
48
49 $validation = new Validator;
50 $data = $validation->sanitize($_POST);
51
52 if (Validator::is_valid($data['ag_settings'] ?? [], $this->rules()) !== true) {
53 Notice::add(__('Invalid form data', 'age-gate'));
54 $this->redirect($data['_wp_http_referer'], 0);
55 exit;
56 }
57
58 $set = array_fill_keys(array_keys(Immutable::AGE_GATE_PERMISSION_ARRAY), []);
59
60 $distribution = array_merge($set, $data['ag_settings'] ?? []);
61
62 foreach ($distribution as $cap => $roles) {
63 foreach ($wp_roles->roles as $key => $role) {
64 if ($key === 'administrator') {
65 continue;
66 }
67
68 $role = get_role($key);
69
70 $capability = Immutable::AGE_GATE_PERMISSION_ARRAY[$cap];
71
72 if (array_key_exists($key, $roles)) {
73 if (!$role->has_cap($capability)) {
74 $role->add_cap($capability);
75 }
76 } else {
77 if ($role->has_cap($capability)) {
78 $role->remove_cap($capability);
79 }
80 }
81 }
82 }
83
84 // set all on admin anyway
85 $admin = get_role('administrator');
86
87 foreach (Immutable::AGE_GATE_PERMISSION_ARRAY as $cap) {
88 if (!$admin->has_cap($cap)) {
89 $admin->add_cap($cap);
90 }
91 }
92
93 $this->redirect($data['_wp_http_referer']);
94 }
95
96 protected function fields(): array
97 {
98 return [];
99 }
100
101 protected function rules() : array
102 {
103
104 $validation = new Validator;
105 $data = $validation->sanitize($_POST); // phpcs:ignore WordPress.Security.NonceVerification.Missing
106 return array_fill_keys(array_keys(Arr::dot($data['ag_settings'] ?? [])), 'boolean');
107 }
108 }
109