| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if ( ! class_exists( 'MywpAdminSidebar' ) ) : |
| 8 |
|
| 9 |
final class MywpAdminSidebar { |
| 10 |
|
| 11 |
private static $default_sidebar; |
| 12 |
|
| 13 |
public static function init() { |
| 14 |
|
| 15 |
add_action( 'mywp_wp_loaded' , array( __CLASS__ , 'mywp_wp_loaded' ) ); |
| 16 |
|
| 17 |
} |
| 18 |
|
| 19 |
public static function mywp_wp_loaded() { |
| 20 |
|
| 21 |
if( ! is_admin() ) { |
| 22 |
|
| 23 |
return false; |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
if( is_network_admin() ) { |
| 28 |
|
| 29 |
return false; |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
add_action( 'admin_head' , array( __CLASS__ , 'set_default_sidebar' ) , 999 ); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
public static function set_default_sidebar() { |
| 38 |
|
| 39 |
global $menu; |
| 40 |
global $submenu; |
| 41 |
|
| 42 |
if( empty( $menu ) or empty( $submenu ) ) { |
| 43 |
|
| 44 |
return false; |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
$current_menus = $menu; |
| 49 |
$current_submenus = $submenu; |
| 50 |
|
| 51 |
$remove_classes = array( 'menu-top-first' , 'menu-top-last' , 'menu-top' ); |
| 52 |
|
| 53 |
foreach( $current_menus as $key => $current_menu ) { |
| 54 |
|
| 55 |
$current_menu[2] = urldecode( html_entity_decode( $current_menu[2] ) ); |
| 56 |
$current_menus[ $key ][2] = $current_menu[2]; |
| 57 |
|
| 58 |
$current_menus[ $key ][2] = self::get_remove_return_url( $current_menu[2] ); |
| 59 |
|
| 60 |
if( empty( $current_menu[4] ) ) { |
| 61 |
|
| 62 |
continue; |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
$changed_class = false; |
| 67 |
|
| 68 |
foreach( $remove_classes as $remove_class ) { |
| 69 |
|
| 70 |
if( strpos( $current_menu[4] , "{$remove_class}" ) !== false ) { |
| 71 |
|
| 72 |
$current_menu[4] = str_replace( "{$remove_class}" , '' , $current_menu[4] ); |
| 73 |
|
| 74 |
$changed_class = true; |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
if( $changed_class ) { |
| 81 |
|
| 82 |
$current_menus[ $key ][4] = preg_replace( '/\s(?=\s)/' , '' , $current_menu[4] ); |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
$current_menus[ $key ][4] = ltrim( $current_menus[ $key ][4] ); |
| 87 |
$current_menus[ $key ][4] = rtrim( $current_menus[ $key ][4] ); |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
foreach( $current_submenus as $parent_id => $current_submenus_arr ) { |
| 92 |
|
| 93 |
foreach( $current_submenus_arr as $key => $current_submenu ) { |
| 94 |
|
| 95 |
$current_submenu[2] = urldecode( html_entity_decode( $current_submenu[2] ) ); |
| 96 |
|
| 97 |
$current_submenus[ $parent_id ][ $key ][2] = $current_submenu[2]; |
| 98 |
|
| 99 |
$current_submenus[ $parent_id ][ $key ][2] = self::get_remove_return_url( $current_submenu[2] ); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
$current_menus = apply_filters( 'mywp_admin_sidebar_set_default_sidebar_menus' , $current_menus ); |
| 106 |
$current_submenus = apply_filters( 'mywp_admin_sidebar_set_default_sidebar_submenu' , $current_submenus ); |
| 107 |
|
| 108 |
self::$default_sidebar = array( 'menu' => $current_menus , 'submenu' => $current_submenus ); |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
public static function get_default_sidebar() { |
| 113 |
|
| 114 |
return self::$default_sidebar; |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
private static function get_remove_return_url( $url = false ) { |
| 119 |
|
| 120 |
if( empty( $url ) ) { |
| 121 |
|
| 122 |
return $url; |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
if( strpos( $url , 'customize.php' ) === false or strpos( $url , 'return=' ) === false ) { |
| 127 |
|
| 128 |
return $url; |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
$return_url = sprintf( '?return=%s' , wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 133 |
|
| 134 |
if( strpos( $url , $return_url . '&' ) !== false ) { |
| 135 |
|
| 136 |
$url = str_replace( $return_url . '&' , '?' , $url ); |
| 137 |
|
| 138 |
} elseif( strpos( $url , $return_url ) !== false ) { |
| 139 |
|
| 140 |
$url = str_replace( $return_url , '' , $url ); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
return $url; |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
private static function find_default_menu( $find_id = false , $find_parent_id = false ) { |
| 149 |
|
| 150 |
if( empty( $find_id ) ) { |
| 151 |
|
| 152 |
return false; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
$default_sidebar = self::get_default_sidebar(); |
| 157 |
|
| 158 |
if( empty( $default_sidebar['menu'] ) ) { |
| 159 |
|
| 160 |
return false; |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
$find_id = strip_tags( do_shortcode( $find_id ) ); |
| 165 |
$find_parent_id = strip_tags( $find_parent_id ); |
| 166 |
|
| 167 |
$found_current_default = false; |
| 168 |
$found_parent_default = false; |
| 169 |
$found_childs_default = false; |
| 170 |
|
| 171 |
if( ! empty( $find_parent_id ) ) { |
| 172 |
|
| 173 |
if( empty( $default_sidebar['submenu'][ $find_parent_id ] ) ) { |
| 174 |
|
| 175 |
return false; |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
foreach( $default_sidebar['menu'] as $key => $menu ) { |
| 180 |
|
| 181 |
if( ! isset( $menu[2] ) ) { |
| 182 |
|
| 183 |
continue; |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
if( $menu[2] !== $find_parent_id ) { |
| 188 |
|
| 189 |
continue; |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
$found_parent_default = $menu; |
| 194 |
|
| 195 |
break; |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
if( empty( $found_parent_default ) ) { |
| 200 |
|
| 201 |
return false; |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
foreach( $default_sidebar['submenu'][ $find_parent_id ] as $key => $submenu ) { |
| 206 |
|
| 207 |
if( ! isset( $submenu[2] ) ) { |
| 208 |
|
| 209 |
continue; |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
if( $submenu[2] !== $find_id ) { |
| 214 |
|
| 215 |
continue; |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
$found_current_default = $submenu; |
| 220 |
|
| 221 |
break; |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
} else { |
| 226 |
|
| 227 |
foreach( $default_sidebar['menu'] as $key => $menu ) { |
| 228 |
|
| 229 |
if( ! isset( $menu[2] ) ) { |
| 230 |
|
| 231 |
continue; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
if( $menu[2] !== $find_id ) { |
| 236 |
|
| 237 |
continue; |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
$found_current_default = $menu; |
| 242 |
|
| 243 |
break; |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
if( ! empty( $found_current_default ) && ! empty( $default_sidebar['submenu'][ $find_id ] ) ) { |
| 248 |
|
| 249 |
$found_childs_default = $default_sidebar['submenu'][ $find_id ]; |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
return array( 'current' => $found_current_default , 'parent' => $found_parent_default , 'childs' => $found_childs_default ); |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
private static function is_third_party_menu( $current_menu_id = false , $parent_menu_id = false ) { |
| 260 |
|
| 261 |
if( empty( $current_menu_id ) ) { |
| 262 |
|
| 263 |
return false; |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
$current_menu_id = strip_tags( $current_menu_id ); |
| 268 |
|
| 269 |
if( in_array( $current_menu_id , array( 'index.php' ) ) ) { |
| 270 |
|
| 271 |
return false; |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
if( empty( $parent_menu_id ) ) { |
| 276 |
|
| 277 |
$parent_menu_id = 'admin.php'; |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
$parent_menu_id = strip_tags( $parent_menu_id ); |
| 282 |
|
| 283 |
$menu_hook = get_plugin_page_hook( $current_menu_id , $parent_menu_id ); |
| 284 |
|
| 285 |
$menu_file = $current_menu_id; |
| 286 |
|
| 287 |
$pos = strpos( $menu_file , '?' ); |
| 288 |
|
| 289 |
if ( false !== $pos ) { |
| 290 |
|
| 291 |
$menu_file = substr( $menu_file, 0, $pos ); |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
if( ! empty( $menu_hook ) ) { |
| 296 |
|
| 297 |
return true; |
| 298 |
|
| 299 |
} elseif( file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) { |
| 300 |
|
| 301 |
return true; |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
return false; |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
public static function default_item_convert( $item = false ) { |
| 310 |
|
| 311 |
$called_text = sprintf( '%s::%s( %s )' , __CLASS__ , __FUNCTION__ , '$item' ); |
| 312 |
|
| 313 |
if( empty( $item ) or ! is_object( $item ) ) { |
| 314 |
|
| 315 |
MywpHelper::error_not_found_message( '$item' , $called_text ); |
| 316 |
|
| 317 |
return false; |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
if( empty( $item->item_type ) ) { |
| 322 |
|
| 323 |
return false; |
| 324 |
|
| 325 |
} |
| 326 |
|
| 327 |
if( $item->item_type !== 'default') { |
| 328 |
|
| 329 |
return false; |
| 330 |
|
| 331 |
} |
| 332 |
|
| 333 |
if( empty( $item->item_default_type ) ) { |
| 334 |
|
| 335 |
return false; |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
if( empty( $item->item_default_id ) && empty( $item->item_default_parent_id ) ) { |
| 340 |
|
| 341 |
return false; |
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
$default_menu = self::find_default_menu( $item->item_default_id , $item->item_default_parent_id ); |
| 346 |
|
| 347 |
if( empty( $default_menu['current'] ) ) { |
| 348 |
|
| 349 |
return false; |
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
$item->item_default_title = $default_menu['current'][0]; |
| 354 |
$item->item_capability = $default_menu['current'][1]; |
| 355 |
|
| 356 |
if( empty( $default_menu['parent'] ) ) { |
| 357 |
|
| 358 |
if( ! empty( $default_menu['childs'] ) ) { |
| 359 |
|
| 360 |
$child_menus = array_values( $default_menu['childs'] ); |
| 361 |
|
| 362 |
$menu_slug = $child_menus[0][2]; |
| 363 |
|
| 364 |
$is_third_party_menu = self::is_third_party_menu( $menu_slug , $default_menu['current'][2] ); |
| 365 |
|
| 366 |
} else { |
| 367 |
|
| 368 |
$menu_slug = $default_menu['current'][2]; |
| 369 |
|
| 370 |
$is_third_party_menu = self::is_third_party_menu( $menu_slug ); |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
if( $is_third_party_menu ) { |
| 375 |
|
| 376 |
$item->item_link_url = add_query_arg( array( 'page' => $menu_slug ) , admin_url( 'admin.php' ) ); |
| 377 |
|
| 378 |
} elseif( strpos( $menu_slug , 'http' ) !== false ) { |
| 379 |
|
| 380 |
$item->item_link_url = $menu_slug; |
| 381 |
|
| 382 |
} else { |
| 383 |
|
| 384 |
$item->item_link_url = admin_url( $menu_slug ); |
| 385 |
|
| 386 |
} |
| 387 |
|
| 388 |
} else { |
| 389 |
|
| 390 |
$menu_slug = $default_menu['current'][2]; |
| 391 |
|
| 392 |
$is_third_party_menu = self::is_third_party_menu( $menu_slug , $default_menu['parent'][2] ); |
| 393 |
|
| 394 |
if( $is_third_party_menu ) { |
| 395 |
|
| 396 |
$menu_file = $default_menu['parent'][2]; |
| 397 |
|
| 398 |
$pos = strpos( $menu_file , '?' ); |
| 399 |
|
| 400 |
if ( false !== $pos ) { |
| 401 |
|
| 402 |
$menu_file = substr( $menu_file, 0, $pos ); |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
$found_menu = self::find_default_menu( $default_menu['parent'][2] ); |
| 407 |
|
| 408 |
if( ! empty( $found_menu['childs'] ) ) { |
| 409 |
|
| 410 |
$child_menus = array_values( $found_menu['childs'] ); |
| 411 |
|
| 412 |
$admin_is_parent = self::is_third_party_menu( $child_menus[0][2] , $found_menu['current'][2] ); |
| 413 |
|
| 414 |
} else { |
| 415 |
|
| 416 |
$admin_is_parent = self::is_third_party_menu( $found_menu['current'][2] ); |
| 417 |
|
| 418 |
} |
| 419 |
|
| 420 |
if ( |
| 421 |
( |
| 422 |
! $admin_is_parent && |
| 423 |
file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && |
| 424 |
! is_dir( WP_PLUGIN_DIR . "/{$default_menu['parent'][2]}" ) |
| 425 |
) |
| 426 |
|| |
| 427 |
file_exists( $menu_file ) |
| 428 |
) { |
| 429 |
|
| 430 |
$item->item_link_url = add_query_arg( array( 'page' => $menu_slug ) , admin_url( $default_menu['parent'][2] ) ); |
| 431 |
|
| 432 |
} else { |
| 433 |
|
| 434 |
$item->item_link_url = add_query_arg( array( 'page' => $menu_slug ) , admin_url( 'admin.php' ) ); |
| 435 |
|
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
} elseif( strpos( $menu_slug , 'http' ) !== false ) { |
| 440 |
|
| 441 |
$item->item_link_url = $menu_slug; |
| 442 |
|
| 443 |
} else { |
| 444 |
|
| 445 |
$item->item_link_url = admin_url( $menu_slug ); |
| 446 |
|
| 447 |
} |
| 448 |
|
| 449 |
} |
| 450 |
|
| 451 |
if( ! empty( $default_menu['current'][4] ) ) { |
| 452 |
|
| 453 |
$item->item_li_class = $item->item_link_class = $default_menu['current'][4]; |
| 454 |
|
| 455 |
if( strpos( $default_menu['current'][4] , 'wp-menu-separator' ) !== false ) { |
| 456 |
|
| 457 |
$item->item_type = 'custom'; |
| 458 |
$item->item_custom_html = '<div class="separator"></div>'; |
| 459 |
$item->item_li_class = ''; |
| 460 |
$item->item_li_id = ''; |
| 461 |
$item->item_link_url = ''; |
| 462 |
|
| 463 |
return $item; |
| 464 |
|
| 465 |
} |
| 466 |
|
| 467 |
} |
| 468 |
|
| 469 |
$customizer_url = admin_url( 'customize.php' ); |
| 470 |
|
| 471 |
if( strpos( $item->item_link_url , $customizer_url ) !== false && strpos( $item->item_link_url , 'return' ) === false ) { |
| 472 |
|
| 473 |
$param_str = false; |
| 474 |
|
| 475 |
if( strpos( $item->item_link_url , $customizer_url . '?' ) !== false ) { |
| 476 |
|
| 477 |
$param_str = str_replace( $customizer_url . '?' , '&' , $item->item_link_url ); |
| 478 |
|
| 479 |
} |
| 480 |
|
| 481 |
$url = urldecode( add_query_arg( array( 'return' => wp_unslash( $_SERVER['REQUEST_URI'] ) ) , $customizer_url ) ); |
| 482 |
|
| 483 |
$item->item_link_url = $url . $param_str; |
| 484 |
|
| 485 |
} |
| 486 |
|
| 487 |
if( ! empty( $default_menu['current'][5] ) ) { |
| 488 |
|
| 489 |
$item->item_li_id = preg_replace( '|[^a-zA-Z0-9_:.]|' , '-' , $default_menu['current'][5] ); |
| 490 |
|
| 491 |
} |
| 492 |
|
| 493 |
if( ! empty( $default_menu['current'][6] ) ) { |
| 494 |
|
| 495 |
if( strpos( $default_menu['current'][6] , 'data:image/svg+xml;base64,' ) ) { |
| 496 |
|
| 497 |
$item->item_icon_class = ' svg'; |
| 498 |
$item->item_icon_style = sprintf( ' background-image: url( "%s" );' , esc_attr( $default_menu['current'][6] ) ); |
| 499 |
|
| 500 |
} elseif( strpos( $default_menu['current'][6] , 'dashicons-' ) !== false ) { |
| 501 |
|
| 502 |
//$item->item_icon_class = ' dashicons-before ' . $default_menu['current'][6]; |
| 503 |
|
| 504 |
} elseif( $default_menu['current'][6] === 'none' or $default_menu['current'][6] === 'div' ) { |
| 505 |
|
| 506 |
$item->item_icon_class = ' dashicons-before '; |
| 507 |
|
| 508 |
} else { |
| 509 |
|
| 510 |
$item->item_icon_img = $default_menu['current'][6]; |
| 511 |
|
| 512 |
} |
| 513 |
|
| 514 |
} |
| 515 |
|
| 516 |
return apply_filters( 'mywp_admin_sidebar_default_item_convert' , $item ); |
| 517 |
|
| 518 |
} |
| 519 |
|
| 520 |
} |
| 521 |
|
| 522 |
MywpAdminSidebar::init(); |
| 523 |
|
| 524 |
endif; |
| 525 |
|