Toolbar.php
| 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 | * Toolbar service |
| 12 | * |
| 13 | * @since 6.9.0 https://github.com/aamplugin/advanced-access-manager/issues/223 |
| 14 | * @since 6.4.0 https://github.com/aamplugin/advanced-access-manager/issues/76 |
| 15 | * @since 6.0.0 Initial implementation of the class |
| 16 | * |
| 17 | * @package AAM |
| 18 | * @version 6.9.0 |
| 19 | */ |
| 20 | class AAM_Service_Toolbar |
| 21 | { |
| 22 | use AAM_Core_Contract_RequestTrait, |
| 23 | AAM_Core_Contract_ServiceTrait; |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * DB option name for cache |
| 28 | * |
| 29 | * @version 6.0.0 |
| 30 | */ |
| 31 | const DB_OPTION = 'aam_toolbar_cache'; |
| 32 | |
| 33 | /** |
| 34 | * AAM configuration setting that is associated with the service |
| 35 | * |
| 36 | * @version 6.0.0 |
| 37 | */ |
| 38 | const FEATURE_FLAG = 'core.service.toolbar.enabled'; |
| 39 | |
| 40 | /** |
| 41 | * Constructor |
| 42 | * |
| 43 | * @return void |
| 44 | * |
| 45 | * @access protected |
| 46 | * @version 6.0.0 |
| 47 | */ |
| 48 | protected function __construct() |
| 49 | { |
| 50 | if (is_admin()) { |
| 51 | // Hook that initialize the AAM UI part of the service |
| 52 | if (AAM_Core_Config::get(self::FEATURE_FLAG, true)) { |
| 53 | add_action('aam_init_ui_action', function () { |
| 54 | AAM_Backend_Feature_Main_Toolbar::register(); |
| 55 | }); |
| 56 | |
| 57 | //admin toolbar |
| 58 | if (AAM::isAAM()) { |
| 59 | add_action('wp_after_admin_bar_render', array($this, 'cacheAdminBar')); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Hook that returns the detailed information about the nature of the |
| 64 | // service. This is used to display information about service on the |
| 65 | // Settings->Services tab |
| 66 | add_filter('aam_service_list_filter', function ($services) { |
| 67 | $services[] = array( |
| 68 | 'title' => __('Admin Toolbar', AAM_KEY), |
| 69 | 'description' => __('Manage access to the top admin toolbar items for any role or individual user. The service only removes restricted items but does not actually protect from direct access via link.', AAM_KEY), |
| 70 | 'setting' => self::FEATURE_FLAG |
| 71 | ); |
| 72 | |
| 73 | return $services; |
| 74 | }, 10); |
| 75 | } |
| 76 | |
| 77 | if (AAM_Core_Config::get(self::FEATURE_FLAG, true)) { |
| 78 | $this->initializeHooks(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Cache admin tool bar |
| 84 | * |
| 85 | * This is done so the complete list of admin toolbar items can be displayed on |
| 86 | * the AAM UI page |
| 87 | * |
| 88 | * @return void |
| 89 | * |
| 90 | * @since 6.9.0 https://github.com/aamplugin/advanced-access-manager/issues/223 |
| 91 | * @since 6.0.0 Initial implementation of the method |
| 92 | * |
| 93 | * @access public |
| 94 | * @global object $wp_admin_bar |
| 95 | * @version 6.9.0 |
| 96 | */ |
| 97 | public function cacheAdminBar() |
| 98 | { |
| 99 | global $wp_admin_bar; |
| 100 | |
| 101 | $reflection = new ReflectionClass(get_class($wp_admin_bar)); |
| 102 | $cache = array(); |
| 103 | |
| 104 | if ($reflection->hasProperty('nodes')) { |
| 105 | $prop = $reflection->getProperty('nodes'); |
| 106 | $prop->setAccessible(true); |
| 107 | |
| 108 | $nodes = $prop->getValue($wp_admin_bar); |
| 109 | |
| 110 | if (isset($nodes['root'])) { |
| 111 | foreach ($nodes['root']->children as $node) { |
| 112 | $cache = array_merge($cache, $node->children); |
| 113 | } |
| 114 | |
| 115 | // do some cleanup |
| 116 | foreach ($cache as $i => $node) { |
| 117 | if ($node->id === 'menu-toggle') { |
| 118 | unset($cache[$i]); |
| 119 | } |
| 120 | } |
| 121 | AAM_Core_API::updateOption(self::DB_OPTION, $cache); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return $cache; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get cached admin toolbar |
| 130 | * |
| 131 | * @return array |
| 132 | * |
| 133 | * @access public |
| 134 | * @version 6.0.0 |
| 135 | */ |
| 136 | public function getToolbarCache() |
| 137 | { |
| 138 | return AAM_Core_API::getOption(self::DB_OPTION); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Initialize Admin Toolbar hooks |
| 143 | * |
| 144 | * @return void |
| 145 | * |
| 146 | * @since 6.4.0 Fixed https://github.com/aamplugin/advanced-access-manager/issues/76 |
| 147 | * @since 6.0.0 Initial implementation of the method |
| 148 | * |
| 149 | * @access protected |
| 150 | * @version 6.4.0 |
| 151 | */ |
| 152 | protected function initializeHooks() |
| 153 | { |
| 154 | if ($this->getFromQuery('init') !== 'toolbar' && !AAM::isAAM()) { |
| 155 | add_action( |
| 156 | 'wp_before_admin_bar_render', |
| 157 | function () { |
| 158 | global $wp_admin_bar; |
| 159 | |
| 160 | $toolbar = AAM::getUser()->getObject('toolbar'); |
| 161 | $nodes = $wp_admin_bar->get_nodes(); |
| 162 | |
| 163 | foreach ((is_array($nodes) ? $nodes : array()) as $id => $node) { |
| 164 | if ($toolbar->isHidden($id, true)) { |
| 165 | if (!empty($node->parent)) { // update parent node with # link |
| 166 | $parent = $wp_admin_bar->get_node($node->parent); |
| 167 | if ($parent && ($parent->href === $node->href)) { |
| 168 | $wp_admin_bar->add_node(array( |
| 169 | 'id' => $parent->id, |
| 170 | 'href' => '#' |
| 171 | )); |
| 172 | } |
| 173 | } |
| 174 | $wp_admin_bar->remove_node($id); |
| 175 | } |
| 176 | } |
| 177 | }, |
| 178 | PHP_INT_MAX |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | // Policy generation hook |
| 183 | add_filter( |
| 184 | 'aam_generated_policy_filter', array($this, 'generatePolicy'), 10, 4 |
| 185 | ); |
| 186 | |
| 187 | add_action('aam_clear_settings_action', function() { |
| 188 | AAM_Core_API::deleteOption(self::DB_OPTION); |
| 189 | }); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Generate Toolbar policy statements |
| 194 | * |
| 195 | * @param array $policy |
| 196 | * @param string $resource_type |
| 197 | * @param array $options |
| 198 | * @param AAM_Core_Policy_Generator $generator |
| 199 | * |
| 200 | * @return array |
| 201 | * |
| 202 | * @access public |
| 203 | * @version 6.4.0 |
| 204 | */ |
| 205 | public function generatePolicy($policy, $resource_type, $options, $generator) |
| 206 | { |
| 207 | if ($resource_type === AAM_Core_Object_Toolbar::OBJECT_TYPE) { |
| 208 | if (!empty($options)) { |
| 209 | $policy['Statement'] = array_merge( |
| 210 | $policy['Statement'], |
| 211 | $generator->generateBasicStatements($options, 'Toolbar') |
| 212 | ); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return $policy; |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | |
| 221 | if (defined('AAM_KEY')) { |
| 222 | AAM_Service_Toolbar::bootstrap(); |
| 223 | } |