| @@ -1,8 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Get Admin Bar items and change them. | |
| 4 | - * | |
| 5 | 3 | * @package Adminimize |
| 6 | 4 | * @subpackage Admin Bar Items |
| 7 | 5 | * @author Frank Bültge |
| 8 | 6 | */ |
| @@ -11,22 +9,20 @@ | ||
| 11 | 9 | echo "Hi there! I'm just a part of plugin, not much I can do when called directly."; |
| 12 | 10 | exit; |
| 13 | 11 | } |
| 14 | 12 | |
| 15 | -// Get all Admin Bar items, different between front- and backend. | |
| 16 | -add_action( 'wp_before_admin_bar_render', '_mw_adminimize_get_admin_bar_nodes', 99999 ); | |
| 17 | -// Render the Admin bar new, different between front- and backend. | |
| 18 | -add_action( 'wp_before_admin_bar_render', '_mw_adminimize_change_admin_bar', 99999 ); | |
| 19 | - | |
| 13 | +//add_action( 'wp_before_admin_bar_render', '_mw_adminimize_get_admin_bar_nodes', 9999 ); | |
| 14 | +add_action( 'admin_bar_menu', '_mw_adminimize_get_admin_bar_nodes', 99999 ); | |
| 20 | 15 | /** |
| 21 | 16 | * Get all admin bar items in back end and write in a options of Adminimize settings array |
| 22 | 17 | * |
| 23 | - * @since 1.8.1 01/10/2013 | |
| 18 | + * @since 1.8.1 01/10/2013 | |
| 19 | + * | |
| 20 | + * @param $wp_admin_bar | |
| 24 | 21 | */ |
| 25 | -function _mw_adminimize_get_admin_bar_nodes() { | |
| 22 | +function _mw_adminimize_get_admin_bar_nodes( $wp_admin_bar ) { | |
| 26 | 23 | |
| 27 | - // Only Administrator get all items. | |
| 28 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 24 | + if ( ! is_admin() ) { | |
| 29 | 25 | return; |
| 30 | 26 | } |
| 31 | 27 | |
| 32 | 28 | if ( _mw_adminimize_exclude_settings_page() ) { |
| @@ -32,99 +28,180 @@ | ||
| 32 | 28 | if ( _mw_adminimize_exclude_settings_page() ) { |
| 33 | 29 | return; |
| 34 | 30 | } |
| 35 | 31 | |
| 36 | - /** | |
| 37 | - * Link to Admin Bar class to get the methods. | |
| 38 | - * | |
| 39 | - * @var $wp_admin_bar \WP_Admin_Bar | |
| 40 | - */ | |
| 41 | - global $wp_admin_bar; | |
| 42 | - | |
| 43 | 32 | // @see: http://codex.wordpress.org/Function_Reference/get_nodes |
| 44 | 33 | $all_toolbar_nodes = $wp_admin_bar->get_nodes(); |
| 45 | 34 | |
| 46 | 35 | if ( $all_toolbar_nodes ) { |
| 36 | + // get all options | |
| 37 | + $adminimizeoptions = _mw_adminimize_get_option_value(); | |
| 47 | 38 | |
| 48 | - $settings = 'mw_adminimize_admin_bar_frontend_nodes'; | |
| 49 | - // Set string on settings for Admin Area. | |
| 50 | - if ( is_admin() ) { | |
| 51 | - $settings = 'mw_adminimize_admin_bar_nodes'; | |
| 52 | - } | |
| 39 | + // add admin bar array | |
| 40 | + $adminimizeoptions[ 'mw_adminimize_admin_bar_nodes' ] = $all_toolbar_nodes; | |
| 53 | 41 | |
| 54 | - // get all options. | |
| 55 | - $adminimizeoptions = (array) _mw_adminimize_get_option_value(); | |
| 42 | + // update options | |
| 43 | + _mw_adminimize_update_option( $adminimizeoptions ); | |
| 44 | + } | |
| 45 | +} | |
| 56 | 46 | |
| 57 | - // add admin bar array. | |
| 58 | - $adminimizeoptions[ $settings ] = $all_toolbar_nodes; | |
| 47 | +//add_action( 'wp_before_admin_bar_render', '_mw_adminimize_get_admin_bar_frontend_nodes', 9999 ); | |
| 48 | +add_action( 'admin_bar_menu', '_mw_adminimize_get_admin_bar_frontend_nodes', 99999 ); | |
| 49 | +/** | |
| 50 | + * Get admin bar items from frontend view. | |
| 51 | + * | |
| 52 | + * @since 2015-07-03 | |
| 53 | + * | |
| 54 | + * @param $wp_admin_bar | |
| 55 | + * | |
| 56 | + * @return null | |
| 57 | + */ | |
| 58 | +function _mw_adminimize_get_admin_bar_frontend_nodes( $wp_admin_bar ) { | |
| 59 | 59 | |
| 60 | + if ( ! is_user_logged_in() ) { | |
| 61 | + return; | |
| 62 | + } | |
| 63 | + | |
| 64 | + if ( is_admin() ) { | |
| 65 | + return; | |
| 66 | + } | |
| 67 | + | |
| 68 | + // Only the right capability allow to get all items and update settings. | |
| 69 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 70 | + return; | |
| 71 | + } | |
| 72 | + // @see: http://codex.wordpress.org/Function_Reference/get_nodes | |
| 73 | + $all_toolbar_nodes = $wp_admin_bar->get_nodes(); | |
| 74 | + | |
| 75 | + if ( $all_toolbar_nodes ) { | |
| 76 | + // get all options | |
| 77 | + $adminimizeoptions = _mw_adminimize_get_option_value(); | |
| 78 | + | |
| 79 | + // add admin bar array | |
| 80 | + $adminimizeoptions[ 'mw_adminimize_admin_bar_frontend_nodes' ] = $all_toolbar_nodes; | |
| 81 | + | |
| 82 | + // update options | |
| 60 | 83 | _mw_adminimize_update_option( $adminimizeoptions ); |
| 61 | 84 | } |
| 62 | 85 | } |
| 63 | 86 | |
| 87 | +add_action( 'admin_bar_menu', '_mw_adminimize_change_admin_bar', 99999 ); | |
| 64 | 88 | /** |
| 65 | - * Remove items in Admin Bar for current role of current active user in front end area | |
| 89 | + * Remove items in Admin Bar for current role of current active user in back end area | |
| 66 | 90 | * Exclude Super Admin, if active |
| 67 | 91 | * Exclude Settings page of Adminimize |
| 68 | 92 | * |
| 69 | 93 | * @since 1.8.1 01/10/2013 |
| 94 | + * | |
| 95 | + * @param $wp_admin_bar | |
| 96 | + * | |
| 97 | + * @return null | |
| 70 | 98 | */ |
| 71 | -function _mw_adminimize_change_admin_bar() { | |
| 99 | +function _mw_adminimize_change_admin_bar( $wp_admin_bar ) { | |
| 72 | 100 | |
| 73 | - // Only for users, there logged in. | |
| 74 | - if ( ! is_user_logged_in() ) { | |
| 101 | + // works only for back end admin bar | |
| 102 | + if ( ! is_admin() ) { | |
| 75 | 103 | return; |
| 76 | 104 | } |
| 77 | 105 | |
| 78 | - // Exclude super admin. | |
| 79 | - if ( _mw_adminimize_exclude_super_admin() ) { | |
| 106 | + if ( _mw_adminimize_exclude_settings_page() ) { | |
| 80 | 107 | return; |
| 81 | 108 | } |
| 82 | 109 | |
| 83 | - // Exclude the new settings of the Admin Bar on settings page of Adminimize. | |
| 84 | - if ( _mw_adminimize_exclude_settings_page() ) { | |
| 110 | + // Exclude super admin | |
| 111 | + if ( _mw_adminimize_exclude_super_admin() ) { | |
| 85 | 112 | return; |
| 86 | 113 | } |
| 87 | 114 | |
| 88 | - // If the admin bar is not active, filtering is not necessary. | |
| 89 | - if ( ! is_admin_bar_showing() ) { | |
| 90 | - return; | |
| 115 | + $user_roles = _mw_adminimize_get_all_user_roles(); | |
| 116 | + $disabled_admin_bar_option_ = array(); | |
| 117 | + | |
| 118 | + foreach ( $user_roles as $role ) { | |
| 119 | + | |
| 120 | + $disabled_admin_bar_option_[ $role ] = _mw_adminimize_get_option_value( | |
| 121 | + 'mw_adminimize_disabled_admin_bar_' . $role . '_items' | |
| 122 | + ); | |
| 91 | 123 | } |
| 92 | 124 | |
| 93 | - /** | |
| 94 | - * Link to Admin Bar class to get the methods. | |
| 95 | - * | |
| 96 | - * @var $wp_admin_bar \WP_Admin_Bar | |
| 97 | - */ | |
| 98 | - global $wp_admin_bar; | |
| 125 | + foreach ( $user_roles as $role ) { | |
| 99 | 126 | |
| 100 | - // Get current user data. | |
| 101 | - $user = wp_get_current_user(); | |
| 102 | - if ( ! $user->roles ) { | |
| 103 | - return; | |
| 127 | + if ( ! isset( $disabled_admin_bar_option_[ $role ][ '0' ] ) ) { | |
| 128 | + $disabled_admin_bar_option_[ $role ][ '0' ] = ''; | |
| 129 | + } | |
| 104 | 130 | } |
| 105 | 131 | |
| 106 | - $disabled_admin_bar_option_ = array(); | |
| 132 | + foreach ( $user_roles as $role ) { | |
| 133 | + $user = wp_get_current_user(); | |
| 107 | 134 | |
| 108 | - $role_prefix = is_admin() ? 'mw_adminimize_disabled_admin_bar_' : 'mw_adminimize_disabled_admin_bar_frontend_'; | |
| 135 | + if ( is_array( $user->roles ) | |
| 136 | + && in_array( $role, $user->roles ) | |
| 137 | + && _mw_adminimize_current_user_has_role( $role ) | |
| 138 | + && is_array( $disabled_admin_bar_option_[ $role ] ) | |
| 139 | + ) { | |
| 109 | 140 | |
| 110 | - foreach ( $user->roles as $role ) { | |
| 111 | - $disabled_admin_bar_option_[] = _mw_adminimize_get_option_value( $role_prefix . $role . '_items' ); | |
| 141 | + foreach ( $disabled_admin_bar_option_[ $role ] as $admin_bar_item ) { | |
| 142 | + $wp_admin_bar->remove_node( $admin_bar_item ); | |
| 143 | + } | |
| 144 | + | |
| 145 | + } // end if user roles | |
| 112 | 146 | } |
| 113 | 147 | |
| 114 | - // Merge multidimensional array in to one, flat. | |
| 115 | - $disabled_admin_bar_option_ = _mw_adminimize_array_flatten( $disabled_admin_bar_option_ ); | |
| 148 | +} | |
| 116 | 149 | |
| 117 | - // Support Multiple Roles for users. | |
| 118 | - if ( _mw_adminimize_get_option_value( 'mw_adminimize_multiple_roles' ) && 1 < count( $user->roles ) ) { | |
| 119 | - $disabled_admin_bar_option_ = _mw_adminimize_get_duplicate( $disabled_admin_bar_option_ ); | |
| 150 | +add_action( 'admin_bar_menu', '_mw_adminimize_change_admin_bar_frontend', 99999 ); | |
| 151 | +/** | |
| 152 | + * Remove items in Admin Bar for current role of current active user in front end area | |
| 153 | + * Exclude Super Admin, if active | |
| 154 | + * Exclude Settings page of Adminimize | |
| 155 | + * | |
| 156 | + * @since 1.8.1 01/10/2013 | |
| 157 | + * | |
| 158 | + * @param $wp_admin_bar | |
| 159 | + * | |
| 160 | + * @return null | |
| 161 | + */ | |
| 162 | +function _mw_adminimize_change_admin_bar_frontend( $wp_admin_bar ) { | |
| 163 | + | |
| 164 | + // works only for back end admin bar | |
| 165 | + if ( is_admin() ) { | |
| 166 | + return; | |
| 120 | 167 | } |
| 121 | 168 | |
| 122 | - // No settings for this role, exit. | |
| 123 | - if ( ! $disabled_admin_bar_option_ ) { | |
| 169 | + // Exclude super admin | |
| 170 | + if ( _mw_adminimize_exclude_super_admin() ) { | |
| 124 | 171 | return; |
| 125 | 172 | } |
| 126 | 173 | |
| 127 | - foreach ( (array) $disabled_admin_bar_option_ as $admin_bar_item ) { | |
| 128 | - $wp_admin_bar->remove_node( $admin_bar_item ); | |
| 174 | + $user_roles = _mw_adminimize_get_all_user_roles(); | |
| 175 | + $disabled_admin_bar_frontend_option_ = ''; | |
| 176 | + | |
| 177 | + foreach ( $user_roles as $role ) { | |
| 178 | + | |
| 179 | + $disabled_admin_bar_frontend_option_[ $role ] = _mw_adminimize_get_option_value( | |
| 180 | + 'mw_adminimize_disabled_admin_bar_frontend_' . $role . '_items' | |
| 181 | + ); | |
| 129 | 182 | } |
| 130 | -} | |
| 183 | + | |
| 184 | + foreach ( $user_roles as $role ) { | |
| 185 | + | |
| 186 | + if ( ! isset( $disabled_admin_bar_frontend_option_[ $role ][ '0' ] ) ) { | |
| 187 | + $disabled_admin_bar_frontend_option_[ $role ][ '0' ] = ''; | |
| 188 | + } | |
| 189 | + } | |
| 190 | + | |
| 191 | + foreach ( $user_roles as $role ) { | |
| 192 | + $user = wp_get_current_user(); | |
| 193 | + | |
| 194 | + if ( is_array( $user->roles ) | |
| 195 | + && in_array( $role, $user->roles ) | |
| 196 | + && _mw_adminimize_current_user_has_role( $role ) | |
| 197 | + && is_array( $disabled_admin_bar_frontend_option_[ $role ] ) | |
| 198 | + ) { | |
| 199 | + | |
| 200 | + foreach ( $disabled_admin_bar_frontend_option_[ $role ] as $admin_bar_item ) { | |
| 201 | + $wp_admin_bar->remove_node( $admin_bar_item ); | |
| 202 | + } | |
| 203 | + | |
| 204 | + } // end if user roles | |
| 205 | + } | |
| 206 | + | |
| 207 | +} | |