PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / trunk
Advanced Access Manager – Access Governance for WordPress vtrunk
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 6.9.27 6.9.28 6.9.29 6.9.3 6.9.30 6.9.31 6.9.32 6.9.33 6.9.34 6.9.35 6.9.36 6.9.37 6.9.38 6.9.39 6.9.4 6.9.41 6.9.42 6.9.43 6.9.44 6.9.45 6.9.46 6.9.47 6.9.48 6.9.49 6.9.5 6.9.51 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.0-alpha.6 7.0.0-alpha.7 7.0.0-beta.1 7.0.0-rc1 7.0.0-rc2 7.0.0-rc3 7.0.1 7.0.10 7.0.11 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 7.1.0 7.1.1 trunk 3.0 4.0 4.0.1 4.1 4.2 4.3 4.4 4.4.1 4.5 4.6 4.6.1 4.6.2 4.7 4.7.1 4.7.2 4.7.5 4.7.6 4.8 4.8.1 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.5.1 4.9.5.2 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1 5.1.1 5.10 5.11 5.2 5.2.1 5.2.5 5.2.6 5.2.7 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.4 5.4.1 5.4.2 5.4.3 5.4.3.1 5.4.3.2 5.5 5.5.1 5.5.2 5.6 5.6.1 5.6.1.1 5.7 5.7.1 5.7.2 5.7.3 5.8 5.8.1 5.8.2 5.8.3 5.9 5.9.1 5.9.1.1 5.9.2 5.9.2.1 5.9.3 5.9.4 5.9.5 5.9.6 5.9.6.1 5.9.6.2 5.9.6.3 5.9.7 5.9.7.1 5.9.7.2 5.9.7.3 5.9.8 5.9.8.1 5.9.9 5.9.9.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.2.0 6.2.1 6.2.2 6.3.0 6.3.1 6.3.2 6.3.3 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.6.0 6.6.1 6.6.2 6.6.3 6.6.4 6.7.0 6.7.1 6.7.2 6.7.3 6.7.4 6.7.5 6.7.6 6.7.7 6.7.8 6.7.9 6.8.0 6.8.1 6.8.2 6.8.3
advanced-access-manager / application / Service / Widgets.php
advanced-access-manager / application / Service Last commit date
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 }