Shortcode
5 months ago
AccessDeniedRedirect.php
1 year ago
AdminToolbar.php
1 year ago
ApiRoute.php
1 year ago
BackendMenu.php
1 year ago
BaseTrait.php
1 year ago
Capability.php
1 year ago
Content.php
5 months ago
Core.php
9 months ago
Hooks.php
1 year ago
Identity.php
3 months ago
Jwt.php
3 months ago
LoginRedirect.php
1 year ago
LogoutRedirect.php
3 months ago
Metaboxes.php
1 year ago
NotFoundRedirect.php
1 year ago
Policies.php
1 year ago
SecureLogin.php
1 year ago
SecurityAudit.php
1 year ago
Shortcodes.php
5 months ago
Urls.php
1 year ago
Welcome.php
1 year ago
Widgets.php
1 year ago
Widgets.php
284 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 | * Widget service |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @version 7.0.0 |
| 15 | */ |
| 16 | class AAM_Service_Widgets |
| 17 | { |
| 18 | |
| 19 | use AAM_Service_BaseTrait; |
| 20 | |
| 21 | /** |
| 22 | * Collection of captured widgets |
| 23 | * |
| 24 | * @version 7.0.0 |
| 25 | */ |
| 26 | const CACHE_OPTION = 'aam_widgets'; |
| 27 | |
| 28 | /** |
| 29 | * Constructor |
| 30 | * |
| 31 | * @return void |
| 32 | * @access protected |
| 33 | * |
| 34 | * @version 7.0.4 |
| 35 | */ |
| 36 | protected function __construct() |
| 37 | { |
| 38 | // Register RESTful API endpoints |
| 39 | AAM_Restful_Widgets::bootstrap(); |
| 40 | |
| 41 | add_action('init', function() { |
| 42 | $this->initialize_hooks(); |
| 43 | }, PHP_INT_MAX); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Prepare the list of widgets |
| 48 | * |
| 49 | * @param AAM_Framework_Service_Widgets $service |
| 50 | * |
| 51 | * @return array |
| 52 | * @access private |
| 53 | * |
| 54 | * @version 7.0.0 |
| 55 | */ |
| 56 | public function get_widget_list($service) |
| 57 | { |
| 58 | $result = []; |
| 59 | |
| 60 | // Getting the menu cache so we can build the list |
| 61 | $cache = AAM::api()->cache->get(self::CACHE_OPTION, []); |
| 62 | |
| 63 | if (!empty($cache) && is_array($cache)) { |
| 64 | foreach($cache as $s_id => $widgets) { |
| 65 | foreach($widgets as $widget) { |
| 66 | array_push($result, $this->_prepare_widget( |
| 67 | $widget, $s_id, $service |
| 68 | )); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return $result; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Initialize Widgets hooks |
| 78 | * |
| 79 | * @return void |
| 80 | * @access protected |
| 81 | * |
| 82 | * @version 7.0.4 |
| 83 | */ |
| 84 | protected function initialize_hooks() |
| 85 | { |
| 86 | if (is_admin()) { |
| 87 | // Hook that initialize the AAM UI part of the service |
| 88 | add_action('aam_initialize_ui_action', function () { |
| 89 | AAM_Backend_Feature_Main_Widget::register(); |
| 90 | }); |
| 91 | |
| 92 | // Manager WordPress metaboxes |
| 93 | add_action('in_admin_header', function () { |
| 94 | $screen = get_current_screen(); |
| 95 | |
| 96 | if (filter_input(INPUT_GET, 'init') === 'widget') { |
| 97 | if ($screen && $screen->id === 'dashboard') { |
| 98 | $this->_initialize_widgets(); |
| 99 | |
| 100 | exit; // No need to load the rest of the site |
| 101 | } |
| 102 | } elseif ($screen && $screen->id === 'dashboard') { |
| 103 | $this->_filter_dashboard_widgets(); |
| 104 | } |
| 105 | }, PHP_INT_MAX); |
| 106 | |
| 107 | // Manage the list of widgets rendered on the "Appearance -> Widgets" |
| 108 | // page |
| 109 | add_action('widgets_admin_page', function() { |
| 110 | $this->_filter_frontend_widgets(); |
| 111 | }, PHP_INT_MAX); |
| 112 | } else { |
| 113 | // Widget filters |
| 114 | add_filter('sidebars_widgets', function($widgets) { |
| 115 | $this->_filter_frontend_widgets(); |
| 116 | |
| 117 | return $widgets; |
| 118 | }, PHP_INT_MAX); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Normalize and prepare the widget model |
| 124 | * |
| 125 | * @param array $widget |
| 126 | * @param string $area |
| 127 | * @param AAM_Framework_Service_Widgets $service |
| 128 | * |
| 129 | * @return array |
| 130 | * @access private |
| 131 | * |
| 132 | * @version 7.0.0 |
| 133 | */ |
| 134 | private function _prepare_widget($widget, $area, $service) |
| 135 | { |
| 136 | return [ |
| 137 | 'slug' => $widget['slug'], |
| 138 | 'area' => $area, |
| 139 | 'title' => base64_decode($widget['title']), |
| 140 | 'is_restricted' => $service->is_denied($widget), |
| 141 | ]; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Collect the list of all registered widgets |
| 146 | * |
| 147 | * @return void |
| 148 | * @access private |
| 149 | * |
| 150 | * @version 7.0.0 |
| 151 | */ |
| 152 | private function _initialize_widgets() |
| 153 | { |
| 154 | global $wp_registered_widgets, $wp_meta_boxes; |
| 155 | |
| 156 | $cache = [ |
| 157 | 'frontend' => [], |
| 158 | 'dashboard' => [] |
| 159 | ]; |
| 160 | |
| 161 | // Collect all registered frontend widgets |
| 162 | foreach ((array)$wp_registered_widgets as $widget) { |
| 163 | // Extracting class name from the registered widget |
| 164 | $slug = $this->_get_widget_slug($widget); |
| 165 | |
| 166 | if (!empty($slug)) { |
| 167 | $cache['frontend'][$slug] = [ |
| 168 | 'title' => base64_encode(wp_strip_all_tags($widget['name'])), |
| 169 | 'area' => 'frontend', |
| 170 | 'slug' => $slug |
| 171 | ]; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Now collect Admin Dashboard Widgets |
| 176 | if (isset($wp_meta_boxes['dashboard'])) { |
| 177 | foreach ((array) $wp_meta_boxes['dashboard'] as $levels) { |
| 178 | foreach ((array) $levels as $boxes) { |
| 179 | foreach ((array) $boxes as $data) { |
| 180 | $slug = $this->_get_widget_slug($data); |
| 181 | |
| 182 | if (!empty($slug)) { |
| 183 | $cache['dashboard'][$slug] = [ |
| 184 | 'slug' => $slug, |
| 185 | 'area' => 'dashboard', |
| 186 | 'title' => base64_encode( |
| 187 | wp_strip_all_tags($data['title']) |
| 188 | ) |
| 189 | ]; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | AAM::api()->cache->set(self::CACHE_OPTION, $cache, 31536000); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Filter the Dashboard -> Home widgets |
| 201 | * |
| 202 | * @return void |
| 203 | * @access private |
| 204 | * |
| 205 | * @version 7.0.2 |
| 206 | */ |
| 207 | private function _filter_dashboard_widgets() |
| 208 | { |
| 209 | global $wp_meta_boxes; |
| 210 | |
| 211 | $service = AAM::api()->widgets(); |
| 212 | |
| 213 | if (isset($wp_meta_boxes['dashboard'])) { |
| 214 | foreach ($wp_meta_boxes['dashboard'] as $priority => $groups) { |
| 215 | foreach($groups as $widgets) { |
| 216 | foreach($widgets as $widget) { |
| 217 | if (!empty($widget) && $service->is_denied($widget)) { |
| 218 | remove_meta_box($widget['id'], 'dashboard', $priority); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Filter frontend widgets |
| 228 | * |
| 229 | * @return void |
| 230 | * @access private |
| 231 | * |
| 232 | * @version 7.0.0 |
| 233 | */ |
| 234 | private function _filter_frontend_widgets() |
| 235 | { |
| 236 | global $wp_registered_widgets; |
| 237 | |
| 238 | $service = AAM::api()->widgets(); |
| 239 | |
| 240 | if (is_array($wp_registered_widgets)) { |
| 241 | foreach ($wp_registered_widgets as $id => $widget) { |
| 242 | if ($service->is_denied($widget)) { |
| 243 | // We do not know if widget was registered with widget instance |
| 244 | // or a class name. This is why we are trying to remove both |
| 245 | // ways |
| 246 | if (is_array($widget['callback']) |
| 247 | && is_object($widget['callback'][0]) |
| 248 | ) { |
| 249 | unregister_widget(spl_object_hash($widget['callback'][0])); |
| 250 | unregister_widget(get_class($widget['callback'][0])); |
| 251 | } |
| 252 | |
| 253 | // Remove it from registered widget global var!! |
| 254 | // INFORM: Why Unregister Widget does not clear global var? |
| 255 | unset($wp_registered_widgets[$id]); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Get widget slug |
| 263 | * |
| 264 | * The callback is used as unique widget identifier |
| 265 | * |
| 266 | * @param array $widget |
| 267 | * |
| 268 | * @return string |
| 269 | * @access private |
| 270 | * |
| 271 | * @version 7.0.0 |
| 272 | */ |
| 273 | private function _get_widget_slug($widget) |
| 274 | { |
| 275 | $slug = null; |
| 276 | |
| 277 | if (!empty($widget['callback'])) { |
| 278 | $slug = AAM::api()->misc->callable_to_slug($widget['callback']); |
| 279 | } |
| 280 | |
| 281 | return $slug; |
| 282 | } |
| 283 | |
| 284 | } |