| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if ( ! class_exists( 'MywpAdminToolbar' ) ) : |
| 8 |
|
| 9 |
final class MywpAdminToolbar { |
| 10 |
|
| 11 |
private static $raw_toolbar_menus = array(); |
| 12 |
|
| 13 |
private static $toolbar_left_menus = array(); |
| 14 |
|
| 15 |
private static $toolbar_right_menus = array(); |
| 16 |
|
| 17 |
private static $default_toolbar; |
| 18 |
|
| 19 |
public static function init() { |
| 20 |
|
| 21 |
add_action( 'mywp_wp_loaded' , array( __CLASS__ , 'mywp_wp_loaded' ) ); |
| 22 |
|
| 23 |
} |
| 24 |
|
| 25 |
public static function mywp_wp_loaded() { |
| 26 |
|
| 27 |
if( ! is_admin() ) { |
| 28 |
|
| 29 |
return false; |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
if( is_network_admin() ) { |
| 34 |
|
| 35 |
return false; |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
add_action( 'wp_before_admin_bar_render' , array( __CLASS__ , 'set_default_toolbar' ) , 999 ); |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
public static function set_default_toolbar() { |
| 44 |
|
| 45 |
global $wp_admin_bar; |
| 46 |
|
| 47 |
$toolbar_menus = $wp_admin_bar->get_nodes(); |
| 48 |
|
| 49 |
if( empty( $toolbar_menus ) ) { |
| 50 |
|
| 51 |
return false; |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
self::$raw_toolbar_menus = $toolbar_menus; |
| 56 |
|
| 57 |
self::set_parent_toolbar_menus( 'top-secondary' , 'right' ); |
| 58 |
self::set_parent_toolbar_menus( '' , 'left' ); |
| 59 |
|
| 60 |
if( isset( self::$toolbar_left_menus['top-secondary'] ) ) { |
| 61 |
|
| 62 |
unset( self::$toolbar_left_menus['top-secondary'] ); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
if( ! empty( self::$toolbar_right_menus ) ) { |
| 67 |
|
| 68 |
foreach( self::$toolbar_right_menus as $menu_id => $menu ) { |
| 69 |
|
| 70 |
if( $menu->parent === 'top-secondary' ) { |
| 71 |
|
| 72 |
self::$toolbar_right_menus[ $menu_id ]->parent = ''; |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
$current_left_menus = apply_filters( 'mywp_admin_toolbar_set_default_toolbar_left_menus' , self::$toolbar_left_menus ); |
| 81 |
$current_right_menus = apply_filters( 'mywp_admin_toolbar_set_default_toolbar_right_menus' , self::$toolbar_right_menus ); |
| 82 |
|
| 83 |
self::$default_toolbar = array( 'left' => $current_left_menus , 'right' => $current_right_menus ); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
private static function set_parent_toolbar_menus( $find_parent_id = false , $menu_location = false ) { |
| 88 |
|
| 89 |
if( $find_parent_id === false or empty( $menu_location ) ) { |
| 90 |
|
| 91 |
return false; |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
reset( self::$raw_toolbar_menus ); |
| 96 |
|
| 97 |
$find_parent_toolbar_menus = array(); |
| 98 |
|
| 99 |
foreach( self::$raw_toolbar_menus as $key => $menu ) { |
| 100 |
|
| 101 |
if( (string) $find_parent_id === (string) $menu->parent ) { |
| 102 |
|
| 103 |
$find_parent_toolbar_menus[ $key ] = $menu; |
| 104 |
|
| 105 |
unset( self::$raw_toolbar_menus[ $key ] ); |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
if( ! empty( $find_parent_toolbar_menus ) ) { |
| 112 |
|
| 113 |
foreach( $find_parent_toolbar_menus as $key => $menu ) { |
| 114 |
|
| 115 |
if( $menu_location === 'left' ) { |
| 116 |
|
| 117 |
self::$toolbar_left_menus[ $key ] = $menu; |
| 118 |
|
| 119 |
} elseif( $menu_location === 'right' ) { |
| 120 |
|
| 121 |
self::$toolbar_right_menus[ $key ] = $menu; |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
self::set_parent_toolbar_menus( $menu->id , $menu_location ); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
public static function get_default_toolbar() { |
| 134 |
|
| 135 |
return self::$default_toolbar; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
private static function find_default_menu( $find_id = false , $find_parent_id = false ) { |
| 140 |
|
| 141 |
if( empty( $find_id ) ) { |
| 142 |
|
| 143 |
return false; |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
$default_toolbar = self::get_default_toolbar(); |
| 148 |
|
| 149 |
if( empty( $default_toolbar['left'] ) && empty( $default_toolbar['right'] ) ) { |
| 150 |
|
| 151 |
return false; |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
$find_id = strip_tags( do_shortcode( $find_id ) ); |
| 156 |
$find_parent_id = strip_tags( $find_parent_id ); |
| 157 |
|
| 158 |
$found_current_default = false; |
| 159 |
$found_parent_default = array(); |
| 160 |
$found_childs_default = array(); |
| 161 |
|
| 162 |
foreach( $default_toolbar as $menu_location => $menus ) { |
| 163 |
|
| 164 |
foreach( $menus as $menu_key => $menu ) { |
| 165 |
|
| 166 |
if( (string) $menu->id === (string) $find_id && (string) $menu->parent === (string) $find_parent_id ) { |
| 167 |
|
| 168 |
$found_current_default = $menu; |
| 169 |
break; |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
if( ! empty( $found_current_default ) ) { |
| 178 |
|
| 179 |
foreach( $default_toolbar as $menu_location => $menus ) { |
| 180 |
|
| 181 |
foreach( $menus as $menu_key => $menu ) { |
| 182 |
|
| 183 |
if( (string) $menu->parent === (string) $found_current_default->id ) { |
| 184 |
|
| 185 |
$found_childs_default[] = $menu; |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
if( ! empty( $found_current_default->parent) ) { |
| 194 |
|
| 195 |
foreach( $default_toolbar as $menu_location => $menus ) { |
| 196 |
|
| 197 |
foreach( $menus as $menu_key => $menu ) { |
| 198 |
|
| 199 |
if( (string) $menu->id === (string) $found_current_default->parent ) { |
| 200 |
|
| 201 |
$found_parent_default[] = $menu; |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
return array( 'current' => $found_current_default , 'parent' => $found_parent_default , 'childs' => $found_childs_default ); |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
public static function default_item_convert( $item = false ) { |
| 217 |
|
| 218 |
$called_text = sprintf( '%s::%s( %s )' , __CLASS__ , __FUNCTION__ , '$item' ); |
| 219 |
|
| 220 |
if( empty( $item ) or ! is_object( $item ) ) { |
| 221 |
|
| 222 |
MywpHelper::error_not_found_message( '$item' , $called_text ); |
| 223 |
|
| 224 |
return false; |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
if( empty( $item->item_type ) ) { |
| 229 |
|
| 230 |
return false; |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
if( $item->item_type !== 'default') { |
| 235 |
|
| 236 |
return false; |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
if( empty( $item->item_default_id ) && empty( $item->item_default_parent_id ) ) { |
| 241 |
|
| 242 |
return false; |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
$default_menu = self::find_default_menu( $item->item_default_id , $item->item_default_parent_id ); |
| 247 |
|
| 248 |
if( empty( $default_menu['current'] ) ) { |
| 249 |
|
| 250 |
return false; |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
$item->item_default_title = $default_menu['current']->title; |
| 255 |
$item->item_meta = $default_menu['current']->meta; |
| 256 |
$item->item_link_url = $default_menu['current']->href; |
| 257 |
|
| 258 |
return apply_filters( 'mywp_admin_toolbar_default_item_convert' , $item ); |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
MywpAdminToolbar::init(); |
| 265 |
|
| 266 |
endif; |
| 267 |
|