PluginProbe
Age Gate / trunk
Age Gate vtrunk
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 1.4.9 All 112 releases
age-gate / src / Update / Activate.php

Activate.php in Age Gate trunk, at src/Update/Activate.php

196 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AgeGate\Update;
4
5 use WP_User;
6 use Asylum\Utility\Arr;
7 use Asylum\Utility\Notice;
8 use AgeGate\Admin\Settings\Tools;
9 use AgeGate\Admin\Settings\Access;
10 use AgeGate\Admin\Settings\Content;
11 use AgeGate\Admin\Settings\Message;
12 use AgeGate\Admin\Settings\Advanced;
13 use AgeGate\Update\Migration\Migrate;
14 use AgeGate\Admin\Settings\Appearance;
15 use AgeGate\Admin\Settings\Restriction;
16 use AgeGate\Common\Immutable\Constants;
17
18 class Activate
19 {
20 use Restriction;
21 use Message;
22 use Access;
23 use Appearance;
24 use Content;
25 use Tools;
26 use Advanced;
27
28 private static $instance = null;
29
30 private $migrationMap = [
31
32 ];
33
34 private function __construct()
35 {
36 }
37
38 public static function activate()
39 {
40 if (self::$instance === null) {
41 self::$instance = new Activate();
42 }
43
44 // default settings
45 $defaultSettings = self::$instance->getDefaultSettings();
46
47 // user settings
48 $userSettings = self::$instance->getUserSettings();
49
50 $merge = array_merge(Arr::dot($defaultSettings), Arr::dot($userSettings));
51
52 $filtered = Arr::undot((array_filter($merge, fn ($item) => $item !== null)));
53
54
55 if ($fromVersion = get_option('wp_age_gate_version', false)) {
56 $filtered = (new Migrate())->mapSimpleSettings($filtered);
57 update_option('age_gate_updated_from', $fromVersion);
58 delete_option('wp_age_gate_version');
59 }
60
61 self::$instance->setCapabilities(get_option('age_gate_version', false), $fromVersion);
62
63 if ($filtered['advanced']['css'] ?? false) {
64 update_option('age_gate_legacy_css', esc_textarea($filtered['advanced']['css']));
65
66 $cssId = get_theme_mod('custom_css_post_id') ?: false;
67
68 if ($cssId && $post = get_post($cssId)) {
69 $content = $post->post_content ?? '';
70 $content = $content . "\r\n" . wp_kses($filtered['advanced']['css'], []);
71 update_option('age_gate_theme_css', $post->content);
72
73 wp_update_post([
74 'ID' => $cssId,
75 'post_content' => $content,
76 ]);
77 } else {
78 $id = wp_insert_post([
79 'post_type' => 'custom_css',
80 'post_title' => get_option('stylesheet'),
81 'post_status' => 'publish',
82 'post_content' => wp_kses($filtered['advanced']['css'], []),
83 ]);
84
85 set_theme_mod('custom_css_post_id', $id);
86 }
87
88 unset($filtered['advanced']['css']);
89 }
90
91 // always bin
92 unset($filtered['advanced']['css_file']);
93
94 foreach ($filtered as $key => $options) {
95 // dump(Constants::AGE_GATE_OPTIONS[$key]);
96 if (Constants::AGE_GATE_OPTIONS[$key] ?? false) {
97 update_option(Constants::AGE_GATE_OPTIONS[$key], $options);
98 }
99 }
100
101 if (wp_next_scheduled('age_gate/cron_options')) {
102 wp_clear_scheduled_hook('age_gate/cron_options');
103 }
104
105 update_option('age_gate_version', AGE_GATE_VERSION);
106 }
107
108 private function getDefaultSettings()
109 {
110 $defaults = [];
111
112 foreach (Constants::AGE_GATE_OPTIONS as $set => $option) {
113 $method = 'get' . ucfirst($set) . 'Fields';
114 $dot = Arr::dot(self::$instance->$method());
115
116
117 $values = Arr::where($dot, function ($value, $key) {
118 return substr($key, -8) === '.default';
119 });
120
121 foreach ($values as $key => $value) {
122 $exp = '/(?:[0-9]).fields.([a-z_.]+).default/';
123 $k = preg_replace($exp, '$1', $key);
124
125 $k = str_replace('.fields', '', $k);
126 $defaults[$set][trim($k, '.')] = $values[$key];
127 }
128 }
129
130 return $defaults;
131 }
132
133 private function getUserSettings()
134 {
135 $user = [];
136
137 foreach (Constants::AGE_GATE_OPTIONS as $set => $option) {
138 $user[$set] = get_option($option, null);
139 }
140
141 return $user;
142 }
143
144 private function setCapabilities($version, $old)
145 {
146 if (!$version && !$old) {
147 // editor / author
148
149 $editor = get_role('editor');
150
151 if ($editor) {
152 $editor->add_cap(Constants::MESSAGES);
153 $editor->add_cap(Constants::RESTRICTIONS);
154 $editor->add_cap(Constants::SET_CONTENT);
155 $editor->add_cap(Constants::SET_CUSTOM_AGE);
156 $editor->add_cap(Constants::APPEARANCE);
157 }
158
159
160 $author = get_role('author');
161
162 if ($author) {
163 $author->add_cap(Constants::SET_CONTENT);
164 $author->add_cap(Constants::SET_CUSTOM_AGE);
165 }
166 }
167
168 // administrator
169 $admin = get_role('administrator');
170
171 if (!$admin) {
172 // there's no admin role, let's find the first that can activate plugins
173 // and use that instead, and match against current user
174 $options = [];
175
176 foreach (wp_roles()->roles as $key => $role) {
177 if (array_key_exists('manage_options', $role['capabilities'] ?? []) || array_key_exists('activate_plugins', $role['capabilities'])) {
178 $options[] = $key;
179 }
180 }
181
182
183 $admin = $options ? get_role($options[0]) : wp_get_current_user();
184 }
185
186 if ($admin instanceof WP_User) {
187 Notice::add(__('Age Gate could not find a suitable administrator role. Admin permissions had been added to you only. Please visit the Age Gate access settings to set permissions for other users and roles.', 'age-gate'), 'warning');
188 }
189
190 foreach (Constants::AGE_GATE_PERMISSION_ARRAY as $cap) {
191 $admin->add_cap($cap);
192 }
193
194 }
195 }
196