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 / ContentController.php

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

151 lines 4.7 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 AgeGate\Common\Settings;
7 use AgeGate\Admin\Settings\Content;
8 use AgeGate\Common\Content as ContentData;
9 use AgeGate\Common\Admin\AbstractController;
10 use AgeGate\Common\Immutable\Constants as Immutable;
11
12 class ContentController extends AbstractController
13 {
14 use Content;
15 public const PERMISSION = Immutable::CONTENT;
16 public const OPTION = Immutable::OPTION_CONTENT;
17
18 // protected $template = 'content';
19
20 public function register(): void
21 {
22 $this->view->addData([
23 'noOptions' => __('No terms', 'age-gate')
24 ]);
25 $this->menu(__('Content', 'age-gate'), self::PERMISSION);
26 }
27
28 protected function required(): bool
29 {
30 return current_user_can(self::PERMISSION);
31 }
32
33 protected function data(): array
34 {
35 $data = get_option(self::OPTION, []) ?: [];
36
37 unset($data['terms']);
38 return $data;
39 }
40
41 protected function fields(): array
42 {
43 return $this->getContentFields();
44 }
45
46 /**
47 * Special prep for data storage
48 *
49 * @return void
50 */
51 protected function store()
52 {
53 $option = get_option(self::OPTION, []);
54
55
56 $_POST['ag_settings']['terms'] = collect($option['terms'] ?? [])
57 ->map(fn($value) => $value ? 1 : false)
58 ->undot()
59 ->toArray();
60
61 parent::store();
62 }
63
64 public function enqueue(): void
65 {
66 global $hook_suffix;
67
68 if ('age-gate_page_age-gate-content' === $hook_suffix) {
69 wp_enqueue_script('age-gate-admin-content', AGE_GATE_URL . 'dist/admin-content.js', ['age-gate-admin'], AGE_GATE_VERSION, true);
70
71 wp_localize_script( 'age-gate-admin-content', 'ag_content_params', [
72 'save_error' => esc_html__('Error: an error occured while saving. Please try again', 'age-gate'),
73 'load_error' => esc_html__('Error: an error occured while loading. Refreshing the page recommended', 'age-gate'),
74 'nonce' => wp_create_nonce('age_gate_store_terms'),
75 ]);
76 }
77 }
78
79 protected function optionStored($data): void
80 {
81 $data = Arr::undot($data);
82
83 foreach ($data['user'] ?? [] as $id => $options) {
84 $content = (new ContentData($id, 'user'));
85 $settings = Settings::getInstance();
86 // mutli ages?
87 if ($settings->multiAge && current_user_can(Immutable::SET_CUSTOM_AGE)) {
88 $default = $settings->{$content->getLanguage()}['defaultAge'] ?? $settings->defaultAge;
89
90 if ($options['age'] ?? false) {
91 $age = (int) $options['age'];
92
93 if ($age === $default) {
94 // remove the meta as we don't need it
95 delete_user_meta($id, Immutable::META_AGE);
96 } else {
97 // add new meta key
98 update_user_meta($id, Immutable::META_AGE, $age);
99 }
100 }
101 }
102
103 // bypass ?
104 if ($settings->type === 'all' && current_user_can(Immutable::SET_CONTENT)) {
105 if ($options['bypass'] ?? false) {
106 // add new meta key
107 update_user_meta($id, Immutable::META_BYPASS, 1);
108 } else {
109 // remove the meta as we don't need it
110 delete_user_meta($id, Immutable::META_BYPASS);
111 }
112 }
113
114 // restrict
115 if ($settings->type === 'selected' && current_user_can(Immutable::SET_CONTENT)) {
116 if ($options['restrict'] ?? false) {
117 // add new meta key
118 update_user_meta($id, Immutable::META_RESTRICT, 1);
119 } else {
120 // remove the meta as we don't need it
121 delete_user_meta($id, Immutable::META_RESTRICT);
122 }
123 }
124 }
125 }
126
127 protected function rules() : array
128 {
129 $rules = [];
130
131 foreach ($this->getContentFields() as $set) {
132 foreach ($set['fields'] as $name => $field) {
133 if ($field['type'] === 'group') {
134 foreach ($field['fields'] as $n => $f) {
135
136 if ($f['type'] !== 'hidden') {
137 $rules[$name . '.' . $n] = $f['type'] === 'number' ? 'numeric' : 'boolean';
138 }
139 }
140 } else {
141 if ($field['type'] !== 'hidden') {
142 $rules[$name] = $field['type'] === 'checkbox' ? 'boolean' : 'numeric';
143 }
144 }
145 }
146 }
147
148 return $rules;
149 }
150 }
151