AccessDeniedRedirect.php
1 year ago
AdminToolbar.php
1 year ago
ApiRoute.php
1 year ago
BackendMenu.php
1 year ago
BackwardCompatibility.php
1 year ago
Capability.php
1 year ago
Configs.php
5 months ago
Content.php
1 year ago
Identity.php
1 year ago
Jwt.php
5 months ago
LoginRedirect.php
1 year ago
LogoutRedirect.php
1 year ago
Metabox.php
1 year ago
Mu.php
1 year ago
NotFoundRedirect.php
1 year ago
Policies.php
1 year ago
Roles.php
1 year ago
SecureLogin.php
1 year ago
SecurityAudit.php
1 year ago
ServiceTrait.php
1 year ago
Settings.php
1 year ago
Urls.php
1 year ago
Users.php
1 year ago
Widgets.php
1 year ago
Mu.php
209 lines
| 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 | * RESTful API service that must be on at all time |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @version 7.0.0 |
| 15 | */ |
| 16 | class AAM_Restful_Mu |
| 17 | { |
| 18 | |
| 19 | use AAM_Restful_ServiceTrait; |
| 20 | |
| 21 | /** |
| 22 | * Necessary permissions to access endpoint |
| 23 | * |
| 24 | * @version 7.0.0 |
| 25 | */ |
| 26 | const PERMISSIONS = [ |
| 27 | 'aam_manager', |
| 28 | 'aam_manage_admin_toolbar' |
| 29 | ]; |
| 30 | |
| 31 | /** |
| 32 | * Construct |
| 33 | * |
| 34 | * @return void |
| 35 | * @access protected |
| 36 | * |
| 37 | * @version 7.0.0 |
| 38 | */ |
| 39 | protected function __construct() |
| 40 | { |
| 41 | // Covering the bug in WordPress core that does not set correct user local |
| 42 | add_filter('rest_pre_dispatch', function($r, $_, $request) { |
| 43 | global $wp_locale_switcher; |
| 44 | |
| 45 | if (strpos($request->get_route(), '/aam/') !== false) { |
| 46 | $user_id = get_current_user_id(); |
| 47 | |
| 48 | if (function_exists('switch_to_user_locale')) { |
| 49 | switch_to_user_locale($user_id); |
| 50 | } elseif (is_a($wp_locale_switcher, 'WP_Locale_Switcher')) { |
| 51 | $wp_locale_switcher->switch_to_locale( |
| 52 | get_user_locale($user_id), $user_id |
| 53 | ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return $r; |
| 58 | }, 10, 3); |
| 59 | |
| 60 | // Register API endpoint |
| 61 | add_action('rest_api_init', function() { |
| 62 | $permission_cb = function() { |
| 63 | return current_user_can('aam_manager') |
| 64 | && AAM::api()->misc->is_admin(); |
| 65 | }; |
| 66 | |
| 67 | // Reset AAM |
| 68 | $this->_register_route('/core/reset', [ |
| 69 | 'methods' => WP_REST_Server::DELETABLE, |
| 70 | 'callback' => [ $this, 'reset' ] |
| 71 | ], $permission_cb, false); |
| 72 | |
| 73 | // Export AAM settings, configurations & roles |
| 74 | $this->_register_route('/core/export', [ |
| 75 | 'methods' => WP_REST_Server::READABLE, |
| 76 | 'callback' => [ $this, 'export' ] |
| 77 | ], $permission_cb, false); |
| 78 | |
| 79 | // Import AAM settings, configurations & roles |
| 80 | $this->_register_route('/core/import', [ |
| 81 | 'methods' => WP_REST_Server::EDITABLE, |
| 82 | 'callback' => [ $this, 'import' ] |
| 83 | ], $permission_cb, false); |
| 84 | }); |
| 85 | |
| 86 | // Get currently managed "Access Level" |
| 87 | add_filter( |
| 88 | 'aam_rest_get_access_level_filter', |
| 89 | [ $this, 'get_access_level' ], |
| 90 | 10, |
| 91 | 2 |
| 92 | ); |
| 93 | |
| 94 | // Handle common REST errors |
| 95 | add_filter( |
| 96 | 'aam_rest_get_error_response_filter', |
| 97 | [ $this, 'get_error_response' ], |
| 98 | 10, |
| 99 | 4 |
| 100 | ); |
| 101 | |
| 102 | // Register a common AAM, access level aware RESTful API endpoint |
| 103 | add_action( |
| 104 | 'aam_rest_register_route', |
| 105 | function($route, $args, $auth, $access_level_aware = true, $ns = null) { |
| 106 | return $this->_register_route( |
| 107 | $route, |
| 108 | $args, |
| 109 | $auth, |
| 110 | $access_level_aware, |
| 111 | $ns |
| 112 | ); |
| 113 | }, 10, 5 |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Reset all AAM settings |
| 119 | * |
| 120 | * @return void |
| 121 | * @access public |
| 122 | * |
| 123 | * @version 7.0.0 |
| 124 | */ |
| 125 | public function reset() |
| 126 | { |
| 127 | return rest_ensure_response([ |
| 128 | 'success' => AAM_Service_Core::get_instance()->reset() |
| 129 | ]); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Export all AAM settings |
| 134 | * |
| 135 | * @return void |
| 136 | * @access public |
| 137 | * |
| 138 | * @version 7.0.0 |
| 139 | */ |
| 140 | public function export() |
| 141 | { |
| 142 | return rest_ensure_response(AAM_Service_Core::get_instance()->export()); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Export all AAM settings |
| 147 | * |
| 148 | * @param WP_REST_Request $request |
| 149 | * |
| 150 | * @return void |
| 151 | * @access public |
| 152 | * |
| 153 | * @version 7.0.0 |
| 154 | */ |
| 155 | public function import($request) |
| 156 | { |
| 157 | return rest_ensure_response([ |
| 158 | 'success' => AAM_Service_Core::get_instance()->import( |
| 159 | $request->get_param('dataset') |
| 160 | ) |
| 161 | ]); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Get current access level |
| 166 | * |
| 167 | * @param null|object $access_level |
| 168 | * @param WP_REST_Request $request |
| 169 | * |
| 170 | * @return null|object |
| 171 | * @access public |
| 172 | * |
| 173 | * @version 7.0.0 |
| 174 | */ |
| 175 | public function get_access_level($access_level, $request) |
| 176 | { |
| 177 | if (is_null($access_level)) { |
| 178 | $access_level = $this->_determine_access_level($request); |
| 179 | } |
| 180 | |
| 181 | return $access_level; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Get RESTful error response |
| 186 | * |
| 187 | * @param mixed $response |
| 188 | * @param Exception $exception |
| 189 | * @param string $rest_code |
| 190 | * @param int $http_status |
| 191 | * |
| 192 | * @return WP_REST_Response |
| 193 | * @access public |
| 194 | * |
| 195 | * @version 7.0.0 |
| 196 | */ |
| 197 | public function get_error_response( |
| 198 | $response, $exception, $rest_code, $http_status |
| 199 | ) { |
| 200 | if (is_null($response)) { |
| 201 | $response = $this->_prepare_error_response( |
| 202 | $exception, $rest_code, $http_status |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | return $response; |
| 207 | } |
| 208 | |
| 209 | } |