PluginProbe
Advanced Access Manager – Access Governance for WordPress / 6.9.0
Advanced Access Manager – Access Governance for WordPress v6.9.0
7.1.4 7.1.2 7.1.3 6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 All 210 releases
advanced-access-manager / application / Core / API.php
API.php
393 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ======================================================================
5 * LICENSE: This file is subject to the terms and conditions defined in *
6 * file 'license.txt', which is part of this source code package. *
7 * ======================================================================
8 */
9
10 /**
11 * AAM core API
12 *
13 * @since 6.7.0 https://github.com/aamplugin/advanced-access-manager/issues/151
14 * @since 6.6.4 https://github.com/aamplugin/advanced-access-manager/issues/142
15 * @since 6.3.1 Fixed bug with setting clearing
16 * @since 6.3.0 Optimized for Multisite setup
17 * @since 6.2.2 Minor refactoring to the clearSettings method
18 * @since 6.0.5 Fixed bug with getOption method where incorrect type could be
19 * returned
20 * @since 6.0.0 Initial implementation of the class
21 *
22 * @package AAM
23 * @version 6.7.0
24 */
25 final class AAM_Core_API
26 {
27
28 /**
29 * Get option from the database
30 *
31 * @param string $option
32 * @param mixed $default
33 * @param int $blog_id
34 *
35 * @return mixed
36 *
37 * @since 6.3.0 Optimized for Multisite setup
38 * @since 6.0.5 Fixed the bug where option may not be returned as proper type
39 * @since 6.0.0 Initial implementation of the method
40 *
41 * @access public
42 * @version 6.3.0
43 */
44 public static function getOption($option, $default = array(), $blog_id = null)
45 {
46 if (is_multisite()) {
47 $result = get_blog_option(
48 ($blog_id ? $blog_id : get_current_blog_id()), $option, $default
49 );
50 } else {
51 $result = get_option($option, $default);
52 }
53
54 return $result;
55 }
56
57 /**
58 * Update option in the DB
59 *
60 * @param string $option
61 * @param mixed $data
62 * @param int $blog_id
63 *
64 * @return bool
65 *
66 * @since 6.7.0 https://github.com/aamplugin/advanced-access-manager/issues/151
67 * @since 6.3.0 Optimized for Multisite setup
68 * @since 6.0.0 Initial implementation of the method
69 *
70 * @access public
71 * @version 6.7.0
72 */
73 public static function updateOption($option, $data, $blog_id = null)
74 {
75 $old_value = self::getOption($option, null, $blog_id);
76
77 if (maybe_serialize($old_value) !== maybe_serialize($data)) {
78 if (is_multisite()) {
79 $result = update_blog_option(
80 ($blog_id ? $blog_id : get_current_blog_id()), $option, $data
81 );
82 } else {
83 $result = update_option($option, $data);
84 }
85 } else {
86 $result = true;
87 }
88
89 return $result;
90 }
91
92 /**
93 * Delete option from the DB
94 *
95 * @param string $option
96 * @param int $blog_id
97 *
98 * @return bool
99 *
100 * @since 6.3.0 Optimized for Multisite setup
101 * @since 6.0.0 Initial implementation of the method
102 *
103 * @access public
104 * @version 6.3.0
105 */
106 public static function deleteOption($option, $blog_id = null)
107 {
108 if (is_multisite()) {
109 $result = delete_blog_option(
110 ($blog_id ? $blog_id : get_current_blog_id()), $option
111 );
112 } else {
113 $result = delete_option($option);
114 }
115
116 return $result;
117 }
118
119 /**
120 * Get role list
121 *
122 * @global WP_Roles $wp_roles
123 *
124 * @return WP_Roles
125 *
126 * @access public
127 * @version 6.0.0
128 */
129 public static function getRoles()
130 {
131 global $wp_roles;
132
133 if (function_exists('wp_roles')) {
134 $roles = wp_roles();
135 } elseif (isset($wp_roles)) {
136 $roles = $wp_roles;
137 } else {
138 $roles = new WP_Roles();
139 }
140
141 return $roles;
142 }
143
144 /**
145 * Get main site id
146 *
147 * Compatibility method for sites that are under WP 4.9.0
148 *
149 * @return int
150 *
151 * @access public
152 * @version 6.4.2
153 */
154 public static function getMainSiteId()
155 {
156 if (function_exists('get_main_site_id')) {
157 $id = get_main_site_id();
158 } elseif (is_multisite()) {
159 $network = get_network();
160 $id = ($network ? $network->site_id : 0);
161 } else {
162 $id = get_current_blog_id();
163 }
164
165 return $id;
166 }
167
168 /**
169 * Return max user level
170 *
171 * @param array $caps
172 * @param int $default
173 *
174 * @return int
175 *
176 * @since 6.6.4 https://github.com/aamplugin/advanced-access-manager/issues/142
177 * @since 6.0.0 Initial implementation of the method
178 *
179 * @access public
180 * @version 6.6.4
181 */
182 public static function maxLevel($caps, $default = 0)
183 {
184 $max = $default;
185
186 if (is_array($caps)) { // WP Error Fix bug report
187 foreach ($caps as $cap => $granted) {
188 if (!empty($granted) && (strpos($cap, 'level_') === 0)) {
189 $level = intval(substr($cap, 6));
190 $max = ($max < $level ? $level : $max);
191 }
192 }
193 }
194
195 return intval($max);
196 }
197
198 /**
199 * Get list of all capabilities
200 *
201 * Prepare and return list of all registered in the system capabilities
202 *
203 * @return array
204 *
205 * @access public
206 * @version 6.0.0
207 */
208 public static function getAllCapabilities()
209 {
210 static $caps = array();
211
212 if (empty($caps)) {
213 foreach (self::getRoles()->role_objects as $role) {
214 if (is_array($role->capabilities)) {
215 $caps = array_merge($caps, $role->capabilities);
216 }
217 }
218 }
219
220 return $caps;
221 }
222
223 /**
224 * Check if capability exists
225 *
226 * @param string $cap
227 *
228 * @return boolean
229 *
230 * @access public
231 * @version 6.0.0
232 */
233 public static function capExists($cap)
234 {
235 // Get list of all capabilities registered on the role levels
236 $caps = self::getAllCapabilities();
237
238 // Get list of all capabilities that are assigned on the user level if user
239 // is authenticated
240 if (is_user_logged_in()) {
241 $user = wp_get_current_user();
242 $caps = array_merge($user->caps, $user->allcaps, $caps);
243 }
244
245 return (is_string($cap) && array_key_exists($cap, $caps));
246 }
247
248 /**
249 * Check if AAM capability is allowed
250 *
251 * @param string $cap
252 *
253 * @return boolean
254 *
255 * @access public
256 * @version 6.0.0
257 */
258 public static function isAAMCapabilityAllowed($cap)
259 {
260 return !self::capExists($cap) || current_user_can($cap);
261 }
262
263 /**
264 * Clear all AAM settings
265 *
266 * @return void
267 *
268 * @since 6.3.1 Fixed bug https://github.com/aamplugin/advanced-access-manager/issues/48
269 * @since 6.2.2 Refactored the way we iterate over the deleting list of options
270 * @since 6.0.0 Initial implementation of the method
271 *
272 * @access public
273 * @version 6.3.1
274 */
275 public static function clearSettings()
276 {
277 global $wpdb;
278
279 $options = array(
280 AAM_Core_AccessSettings::DB_OPTION,
281 AAM_Core_Config::DB_OPTION,
282 AAM_Core_ConfigPress::DB_OPTION,
283 AAM_Service_AdminMenu::CACHE_DB_OPTION,
284 AAM_Service_Toolbar::DB_OPTION
285 );
286
287 foreach($options as $option) {
288 self::deleteOption($option);
289 }
290
291 // Delete all legacy options
292 $query = "DELETE FROM {$wpdb->options} WHERE (`option_name` LIKE %s) AND ";
293 $query .= "(`option_name` NOT IN ('aam_addons', 'aam_migrations'))";
294 $wpdb->query($wpdb->prepare($query, 'aam%'));
295
296 $wpdb->query($wpdb->prepare(
297 "DELETE FROM {$wpdb->postmeta} WHERE `meta_key` LIKE %s", 'aam-%'
298 ));
299
300 $wpdb->query($wpdb->prepare(
301 "DELETE FROM {$wpdb->usermeta} WHERE `meta_key` LIKE %s", 'aam%'
302 ));
303
304 // Trigger the action to inform other services to clean-up the options
305 do_action('aam_clear_settings_action', $options);
306 }
307
308 /**
309 * Get current post
310 *
311 * @return AAM_Core_Object_Post|null
312 *
313 * @access public
314 * @global WP_Query $wp_query
315 * @global WP_Post $post
316 * @version 6.0.0
317 */
318 public static function getCurrentPost()
319 {
320 global $wp_query, $post;
321
322 $res = $post;
323
324 if (get_the_ID()) {
325 $res = get_post(get_the_ID());
326 } elseif (!empty($wp_query->queried_object)) {
327 $res = $wp_query->queried_object;
328 } elseif (!empty($wp_query->post)) {
329 $res = $wp_query->post;
330 } elseif (!empty($wp_query->query_vars['p'])) {
331 $res = get_post($wp_query->query_vars['p']);
332 } elseif (!empty($wp_query->query_vars['page_id'])) {
333 $res = get_post($wp_query->query_vars['page_id']);
334 } elseif (!empty($wp_query->query['name'])) {
335 //Important! Cover the scenario of NOT LIST but ALLOW READ
336 if (!empty($wp_query->posts)) {
337 foreach ($wp_query->posts as $p) {
338 if ($p->post_name === $wp_query->query['name']) {
339 $res = $p;
340 break;
341 }
342 }
343 } elseif (!empty($wp_query->query['post_type'])) {
344 $res = get_page_by_path(
345 $wp_query->query['name'],
346 OBJECT,
347 $wp_query->query['post_type']
348 );
349 }
350 }
351
352 if (is_a($res, 'WP_Post')) {
353 $result = AAM::getUser()->getObject(
354 AAM_Core_Object_Post::OBJECT_TYPE, $res->ID
355 );
356 } else {
357 $result = null;
358 }
359
360 return $result;
361 }
362
363 /**
364 * Get WP core password hasher
365 *
366 * @return PasswordHash
367 *
368 * @access public
369 * @version 6.0.0
370 */
371 public static function prepareHasher()
372 {
373 require_once ABSPATH . WPINC . '/class-phpass.php';
374
375 return new PasswordHash(8, true);
376 }
377
378 /**
379 * Get AAM API endpoint
380 *
381 * @return string
382 *
383 * @access public
384 * @version 6.0.0
385 */
386 public static function getAPIEndpoint()
387 {
388 $endpoint = getenv('AAM_ENDPOINT');
389
390 return ($endpoint ? $endpoint : 'https://api.aamplugin.com/v2');
391 }
392
393 }