| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if( ! class_exists( 'MywpControllerAbstractModule' ) ) { |
| 8 |
return false; |
| 9 |
} |
| 10 |
|
| 11 |
if ( ! class_exists( 'MywpControllerModuleAdminSidebar' ) ) : |
| 12 |
|
| 13 |
final class MywpControllerModuleAdminSidebar extends MywpControllerAbstractModule { |
| 14 |
|
| 15 |
static protected $id = 'admin_sidebar'; |
| 16 |
|
| 17 |
static private $sidebar = false; |
| 18 |
|
| 19 |
static private $sidebar_items = false; |
| 20 |
|
| 21 |
static private $sidebar_items_added_classes = false; |
| 22 |
|
| 23 |
static private $child_items = array(); |
| 24 |
|
| 25 |
static private $parent_items = array(); |
| 26 |
|
| 27 |
static private $found_parent_item_ids = array(); |
| 28 |
|
| 29 |
static private $current_url = false; |
| 30 |
|
| 31 |
static private $find_parent_id = array(); |
| 32 |
|
| 33 |
public static function mywp_controller_initial_data( $initial_data ) { |
| 34 |
|
| 35 |
$initial_data['custom_menu_ui'] = ''; |
| 36 |
|
| 37 |
return $initial_data; |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
public static function mywp_controller_default_data( $default_data ) { |
| 42 |
|
| 43 |
$default_data['custom_menu_ui'] = false; |
| 44 |
|
| 45 |
return $default_data; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
public static function mywp_wp_loaded() { |
| 50 |
|
| 51 |
if( ! is_admin() ) { |
| 52 |
|
| 53 |
return false; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
if( is_network_admin() ) { |
| 58 |
|
| 59 |
return false; |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
if( ! self::is_do_controller() ) { |
| 64 |
|
| 65 |
return false; |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
add_action( 'admin_enqueue_scripts' , array( __CLASS__ , 'admin_enqueue_scripts' ) ); |
| 70 |
add_action( 'admin_head' , array( __CLASS__ , 'hidden_default_menus' ) , 1000 ); |
| 71 |
add_action( 'adminmenu' , array( __CLASS__ , 'render_menus' ) ); |
| 72 |
add_filter( 'mywp_controller_admin_sidebar_get_sidebar_item_added_classes_found_current_item_ids' , array( __CLASS__ , 'mywp_controller_admin_sidebar_get_sidebar_item_added_classes_found_current_item_ids' ) , 9 , 5 ); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
private static function get_sidebar() { |
| 77 |
|
| 78 |
if( ! empty( self::$sidebar ) ) { |
| 79 |
|
| 80 |
return self::$sidebar; |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
$args = array( 'post_status' => array( 'publish' ) , 'post_type' => 'mywp_admin_sidebar' , 'order' => 'ASC' , 'orderby' => 'menu_order' , 'posts_per_page' => -1 ); |
| 85 |
|
| 86 |
$args = apply_filters( 'mywp_controller_admin_sidebar_get_sidebar_args' , $args ); |
| 87 |
|
| 88 |
$posts = MywpPostType::get_posts( $args ); |
| 89 |
|
| 90 |
$sidebar = apply_filters( 'mywp_controller_admin_sidebar_get_sidebar' , $posts ); |
| 91 |
|
| 92 |
self::$sidebar = $sidebar; |
| 93 |
|
| 94 |
return $sidebar; |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
private static function get_sidebar_items() { |
| 99 |
|
| 100 |
if( ! empty( self::$sidebar_items ) ) { |
| 101 |
|
| 102 |
return self::$sidebar_items; |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
$sidebar = self::get_sidebar(); |
| 107 |
|
| 108 |
if( empty( $sidebar ) ) { |
| 109 |
|
| 110 |
return false; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
$sidebar_items = array(); |
| 115 |
|
| 116 |
foreach( $sidebar as $key => $sidebar_item ) { |
| 117 |
|
| 118 |
if( $sidebar_item->item_type == 'default') { |
| 119 |
|
| 120 |
$sidebar_item = MywpAdminSidebar::default_item_convert( $sidebar_item ); |
| 121 |
|
| 122 |
if( ! empty( $sidebar_item ) ) { |
| 123 |
|
| 124 |
$sidebar_items[] = $sidebar_item; |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
} else { |
| 129 |
|
| 130 |
$sidebar_items[] = $sidebar_item; |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
$sidebar_items = apply_filters( 'mywp_controller_admin_sidebar_get_sidebar_item' , $sidebar_items ); |
| 137 |
|
| 138 |
if( empty( $sidebar_items ) ) { |
| 139 |
|
| 140 |
return false; |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
ksort( $sidebar_items ); |
| 145 |
|
| 146 |
self::$sidebar_items = $sidebar_items; |
| 147 |
|
| 148 |
return $sidebar_items; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
private static function get_sidebar_items_added_classes(){ |
| 153 |
|
| 154 |
if( ! empty( self::$sidebar_items_added_classes ) ) { |
| 155 |
|
| 156 |
return self::$sidebar_items_added_classes; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
$sidebar_items = self::get_sidebar_items(); |
| 161 |
|
| 162 |
if( empty( $sidebar_items ) ) { |
| 163 |
|
| 164 |
return false; |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
foreach( $sidebar_items as $key => $sidebar_item ) { |
| 169 |
|
| 170 |
if( ! is_object( $sidebar_item ) ) { |
| 171 |
|
| 172 |
unset( $sidebar_items[ $key ] ); |
| 173 |
|
| 174 |
continue; |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
if( empty( $sidebar_item->item_li_class ) ) { |
| 179 |
|
| 180 |
$sidebar_items[ $key ]->item_li_class = ''; |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
if( empty( $sidebar_item->item_link_class ) ) { |
| 185 |
|
| 186 |
$sidebar_items[ $key ]->item_link_class = ''; |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
if( empty( $sidebar_item->item_link_url ) ) { |
| 191 |
|
| 192 |
$sidebar_items[ $key ]->item_link_url = ''; |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
if( empty( $sidebar_item->item_link_url_parse ) ) { |
| 197 |
|
| 198 |
$sidebar_items[ $key ]->item_link_url_parse = array(); |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
if( empty( $sidebar_item->item_link_url_parse_query ) ) { |
| 203 |
|
| 204 |
$sidebar_items[ $key ]->item_link_url_parse_query = array(); |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
$sidebar_item->item_link_url = do_shortcode( $sidebar_item->item_link_url ); |
| 209 |
|
| 210 |
$sidebar_items[ $key ]->item_link_url = $sidebar_item->item_link_url; |
| 211 |
|
| 212 |
if( ! empty( $sidebar_item->item_link_url ) ) { |
| 213 |
|
| 214 |
$item_link_url_parse = parse_url( $sidebar_item->item_link_url ); |
| 215 |
|
| 216 |
if( isset( $item_link_url_parse['fragment'] ) ) { |
| 217 |
|
| 218 |
unset( $item_link_url_parse['fragment'] ); |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
if( empty( $item_link_url_parse['query'] ) ) { |
| 223 |
|
| 224 |
$item_link_url_parse['query'] = ''; |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
$sidebar_items[ $key ]->item_link_url_parse = $item_link_url_parse; |
| 229 |
|
| 230 |
if( ! empty( $item_link_url_parse['query'] ) ) { |
| 231 |
|
| 232 |
wp_parse_str( $item_link_url_parse['query'] , $item_link_url_parse_query ); |
| 233 |
|
| 234 |
ksort( $item_link_url_parse_query ); |
| 235 |
|
| 236 |
$sidebar_items[ $key ]->item_link_url_parse_query = $item_link_url_parse_query; |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
foreach( $sidebar_items as $key => $sidebar_item ) { |
| 245 |
|
| 246 |
self::$child_items[ $sidebar_item->item_parent ][] = $sidebar_item; |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
$tmp = $sidebar_items; |
| 251 |
|
| 252 |
foreach( $sidebar_items as $key => $sidebar_item ) { |
| 253 |
|
| 254 |
if( empty( $sidebar_item->item_parent ) ) { |
| 255 |
|
| 256 |
continue; |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
foreach( $tmp as $tmp_key => $tmp_sidebar_item ) { |
| 261 |
|
| 262 |
if( $tmp_sidebar_item->ID == $sidebar_item->item_parent ) { |
| 263 |
|
| 264 |
self::$parent_items[ $sidebar_item->ID ][] = $tmp_sidebar_item; |
| 265 |
|
| 266 |
break; |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
unset( $tmp ); |
| 275 |
|
| 276 |
$first = true; |
| 277 |
|
| 278 |
foreach( $sidebar_items as $key => $sidebar_item ) { |
| 279 |
|
| 280 |
if( $first ) { |
| 281 |
|
| 282 |
$sidebar_items[ $key ]->item_li_class .= ' wp-first-item'; |
| 283 |
$sidebar_items[ $key ]->item_link_class .= ' wp-first-item'; |
| 284 |
$first = false; |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
if( $sidebar_item->item_type === 'separator' ) { |
| 289 |
|
| 290 |
continue; |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
if( empty( $sidebar_item->item_parent ) ) { |
| 295 |
|
| 296 |
$sidebar_items[ $key ]->item_li_class .= ' menu-top'; |
| 297 |
$sidebar_items[ $key ]->item_link_class .= ' menu-top'; |
| 298 |
|
| 299 |
} |
| 300 |
|
| 301 |
if( ! empty( self::$child_items[ $sidebar_item->ID ] ) ) { |
| 302 |
|
| 303 |
$sidebar_items[ $key ]->item_li_class .= ' wp-has-submenu'; |
| 304 |
$sidebar_items[ $key ]->item_link_class .= ' wp-has-submenu'; |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
$current_url = self::get_current_url(); |
| 311 |
|
| 312 |
$current_url_parse = parse_url( $current_url ); |
| 313 |
$current_url_query = array(); |
| 314 |
|
| 315 |
if( isset( $current_url_parse['fragment'] ) ) { |
| 316 |
|
| 317 |
unset( $current_url_parse['fragment'] ); |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
if( ! empty( $current_url_parse['query'] ) ) { |
| 322 |
|
| 323 |
wp_parse_str( $current_url_parse['query'] , $current_url_query ); |
| 324 |
|
| 325 |
ksort( $current_url_query ); |
| 326 |
|
| 327 |
} else { |
| 328 |
|
| 329 |
$current_url_parse['query'] = ''; |
| 330 |
|
| 331 |
} |
| 332 |
|
| 333 |
$found_current_item_ids = array(); |
| 334 |
|
| 335 |
foreach( $sidebar_items as $key => $sidebar_item ) { |
| 336 |
|
| 337 |
if( empty( $sidebar_item->item_link_url_parse['host'] ) or empty( $sidebar_item->item_link_url_parse['path'] ) ) { |
| 338 |
|
| 339 |
continue; |
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
if( |
| 344 |
$current_url_parse['scheme'] === $sidebar_item->item_link_url_parse['scheme'] && |
| 345 |
$current_url_parse['host'] === $sidebar_item->item_link_url_parse['host'] && |
| 346 |
$current_url_parse['path'] === $sidebar_item->item_link_url_parse['path'] && |
| 347 |
$current_url_query === $sidebar_item->item_link_url_parse_query |
| 348 |
) { |
| 349 |
|
| 350 |
$found_current_item_ids[] = $sidebar_item->ID; |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
if( empty( $found_current_item_ids ) ) { |
| 357 |
|
| 358 |
$identification_query = array(); |
| 359 |
|
| 360 |
if( ! empty( $current_url_query['page'] ) ) { |
| 361 |
|
| 362 |
$identification_query['page'] = $current_url_query['page']; |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
if( ! empty( $current_url_query['post_type'] ) ) { |
| 367 |
|
| 368 |
if( $current_url_query['post_type'] != 'post' ) { |
| 369 |
|
| 370 |
$identification_query['post_type'] = $current_url_query['post_type']; |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
} |
| 375 |
|
| 376 |
if( ! empty( $current_url_query['taxonomy'] ) ) { |
| 377 |
|
| 378 |
$identification_query['taxonomy'] = $current_url_query['taxonomy']; |
| 379 |
|
| 380 |
} |
| 381 |
|
| 382 |
foreach( $sidebar_items as $key => $sidebar_item ) { |
| 383 |
|
| 384 |
if( empty( $sidebar_item->item_link_url_parse['host'] ) or empty( $sidebar_item->item_link_url_parse['path'] ) ) { |
| 385 |
|
| 386 |
continue; |
| 387 |
|
| 388 |
} |
| 389 |
|
| 390 |
if( |
| 391 |
$current_url_parse['scheme'] === $sidebar_item->item_link_url_parse['scheme'] && |
| 392 |
$current_url_parse['host'] === $sidebar_item->item_link_url_parse['host'] && |
| 393 |
$current_url_parse['path'] === $sidebar_item->item_link_url_parse['path'] && |
| 394 |
http_build_query( $identification_query ) === $sidebar_item->item_link_url_parse['query'] |
| 395 |
) { |
| 396 |
|
| 397 |
$found_current_item_ids[] = $sidebar_item->ID; |
| 398 |
|
| 399 |
} |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
} |
| 404 |
|
| 405 |
$found_current_item_ids = apply_filters( 'mywp_controller_admin_sidebar_get_sidebar_item_added_classes_found_current_item_ids' , $found_current_item_ids , $sidebar_items , $current_url , $current_url_parse , $current_url_query ); |
| 406 |
|
| 407 |
if( ! empty( $found_current_item_ids ) ) { |
| 408 |
|
| 409 |
$found_current_item_ids = array_map( 'strip_tags' , $found_current_item_ids ); |
| 410 |
|
| 411 |
} |
| 412 |
|
| 413 |
if( ! empty( $found_current_item_ids ) ) { |
| 414 |
|
| 415 |
foreach( $sidebar_items as $key => $sidebar_item ) { |
| 416 |
|
| 417 |
if( in_array( $sidebar_item->ID , $found_current_item_ids ) ) { |
| 418 |
|
| 419 |
$sidebar_items[ $key ]->item_li_class .= ' current'; |
| 420 |
$sidebar_items[ $key ]->item_link_class .= ' current'; |
| 421 |
|
| 422 |
} |
| 423 |
|
| 424 |
} |
| 425 |
|
| 426 |
foreach( $found_current_item_ids as $found_current_item_id ) { |
| 427 |
|
| 428 |
$find_item_parents_ids = self::get_find_item_to_parent_ids( $found_current_item_id ); |
| 429 |
|
| 430 |
if( ! empty( $find_item_parents_ids ) ) { |
| 431 |
|
| 432 |
foreach( $sidebar_items as $key => $sidebar_item ) { |
| 433 |
|
| 434 |
if( ! in_array( $sidebar_item->ID , $find_item_parents_ids ) ) { |
| 435 |
|
| 436 |
continue; |
| 437 |
|
| 438 |
} |
| 439 |
|
| 440 |
$sidebar_items[ $key ]->item_li_class .= ' wp-has-current-submenu wp-menu-open'; |
| 441 |
$sidebar_items[ $key ]->item_link_class .= ' wp-has-current-submenu wp-menu-open'; |
| 442 |
|
| 443 |
} |
| 444 |
|
| 445 |
} |
| 446 |
|
| 447 |
} |
| 448 |
|
| 449 |
} |
| 450 |
|
| 451 |
$sidebar_items = apply_filters( 'mywp_controller_admin_sidebar_get_sidebar_item_added_classes' , $sidebar_items ); |
| 452 |
|
| 453 |
self::$sidebar_items_added_classes = $sidebar_items; |
| 454 |
|
| 455 |
return self::$sidebar_items_added_classes; |
| 456 |
|
| 457 |
} |
| 458 |
|
| 459 |
public static function mywp_controller_admin_sidebar_get_sidebar_item_added_classes_found_current_item_ids( $found_current_item_ids , $sidebar_items , $current_url , $current_url_parse , $current_url_query ) { |
| 460 |
|
| 461 |
if( empty( $sidebar_items ) ) { |
| 462 |
|
| 463 |
return $sidebar_items; |
| 464 |
|
| 465 |
} |
| 466 |
|
| 467 |
if( ! empty( $found_current_item_ids ) ) { |
| 468 |
|
| 469 |
return $found_current_item_ids; |
| 470 |
|
| 471 |
} |
| 472 |
|
| 473 |
foreach( $sidebar_items as $key => $sidebar_item ) { |
| 474 |
|
| 475 |
if( empty( $sidebar_item->item_link_url_parse['host'] ) or empty( $sidebar_item->item_link_url_parse['path'] ) ) { |
| 476 |
|
| 477 |
continue; |
| 478 |
|
| 479 |
} |
| 480 |
|
| 481 |
if( |
| 482 |
$current_url_parse['scheme'] !== $sidebar_item->item_link_url_parse['scheme'] or |
| 483 |
$current_url_parse['host'] !== $sidebar_item->item_link_url_parse['host'] |
| 484 |
) { |
| 485 |
|
| 486 |
continue; |
| 487 |
|
| 488 |
} |
| 489 |
|
| 490 |
if( strpos( $current_url_parse['path'] , 'post.php' ) !== false && ! empty( $current_url_query['post'] ) ) { |
| 491 |
|
| 492 |
if( strpos( $sidebar_item->item_link_url_parse['path'] , 'edit.php' ) === false ) { |
| 493 |
|
| 494 |
continue; |
| 495 |
|
| 496 |
} |
| 497 |
|
| 498 |
$post_type = get_post_type( intval( $current_url_query['post'] ) ); |
| 499 |
|
| 500 |
if( empty( $post_type ) ) { |
| 501 |
|
| 502 |
continue; |
| 503 |
|
| 504 |
} |
| 505 |
|
| 506 |
if( $post_type == 'post' && empty( $sidebar_item->item_link_url_parse_query['post_type'] ) ) { |
| 507 |
|
| 508 |
$found_current_item_ids[] = $sidebar_item->ID; |
| 509 |
|
| 510 |
} elseif( ! empty( $sidebar_item->item_link_url_parse_query['post_type'] ) && $sidebar_item->item_link_url_parse_query === array( 'post_type' => $post_type ) ) { |
| 511 |
|
| 512 |
$found_current_item_ids[] = $sidebar_item->ID; |
| 513 |
|
| 514 |
} |
| 515 |
|
| 516 |
} elseif( strpos( $current_url_parse['path'] , 'term.php' ) !== false && $current_url_query['taxonomy'] ) { |
| 517 |
|
| 518 |
if( strpos( $sidebar_item->item_link_url_parse['path'] , 'edit-tags.php' ) === false ) { |
| 519 |
|
| 520 |
continue; |
| 521 |
|
| 522 |
} |
| 523 |
|
| 524 |
if( $current_url_query['taxonomy'] == $sidebar_item->item_link_url_parse_query['taxonomy'] ) { |
| 525 |
|
| 526 |
$found_current_item_ids[] = $sidebar_item->ID; |
| 527 |
|
| 528 |
} |
| 529 |
|
| 530 |
} elseif( strpos( $current_url_parse['path'] , 'comment.php' ) !== false ) { |
| 531 |
|
| 532 |
if( strpos( $sidebar_item->item_link_url_parse['path'] , 'edit-comments.php' ) === false ) { |
| 533 |
|
| 534 |
continue; |
| 535 |
|
| 536 |
} |
| 537 |
|
| 538 |
if( $sidebar_item->item_link_url_parse_query === array() ) { |
| 539 |
|
| 540 |
$found_current_item_ids[] = $sidebar_item->ID; |
| 541 |
|
| 542 |
} |
| 543 |
|
| 544 |
} elseif( strpos( $current_url_parse['path'] , 'theme-install.php' ) !== false ) { |
| 545 |
|
| 546 |
if( strpos( $sidebar_item->item_link_url_parse['path'] , 'themes.php' ) === false ) { |
| 547 |
|
| 548 |
continue; |
| 549 |
|
| 550 |
} |
| 551 |
|
| 552 |
if( $sidebar_item->item_link_url_parse_query === array() ) { |
| 553 |
|
| 554 |
$found_current_item_ids[] = $sidebar_item->ID; |
| 555 |
|
| 556 |
} |
| 557 |
|
| 558 |
} elseif( strpos( $current_url_parse['path'] , 'user-edit.php' ) !== false ) { |
| 559 |
|
| 560 |
if( strpos( $sidebar_item->item_link_url_parse['path'] , 'users.php' ) === false ) { |
| 561 |
|
| 562 |
continue; |
| 563 |
|
| 564 |
} |
| 565 |
|
| 566 |
if( $sidebar_item->item_link_url_parse_query === array() ) { |
| 567 |
|
| 568 |
$found_current_item_ids[] = $sidebar_item->ID; |
| 569 |
|
| 570 |
} |
| 571 |
|
| 572 |
} |
| 573 |
|
| 574 |
} |
| 575 |
|
| 576 |
return $found_current_item_ids; |
| 577 |
|
| 578 |
} |
| 579 |
|
| 580 |
private static function get_find_item_to_parent_ids( $find_id = false ) { |
| 581 |
|
| 582 |
if( empty( $find_id ) ) { |
| 583 |
|
| 584 |
return false; |
| 585 |
|
| 586 |
} |
| 587 |
|
| 588 |
$find_id = strip_tags( $find_id ); |
| 589 |
|
| 590 |
self::set_find_item_parent_ids( $find_id ); |
| 591 |
|
| 592 |
return self::$found_parent_item_ids; |
| 593 |
|
| 594 |
} |
| 595 |
|
| 596 |
private static function set_find_item_parent_ids( $find_id ) { |
| 597 |
|
| 598 |
if( empty( $find_id ) ) { |
| 599 |
|
| 600 |
return false; |
| 601 |
|
| 602 |
} |
| 603 |
|
| 604 |
if( empty( self::$parent_items[ $find_id ] ) ) { |
| 605 |
|
| 606 |
return false; |
| 607 |
|
| 608 |
} |
| 609 |
|
| 610 |
$parent_items = self::$parent_items[ $find_id ]; |
| 611 |
|
| 612 |
foreach( $parent_items as $parent_item ) { |
| 613 |
|
| 614 |
self::$found_parent_item_ids[] = $parent_item->ID; |
| 615 |
|
| 616 |
if( ! empty( $parent_item->item_parent ) ) { |
| 617 |
|
| 618 |
self::set_find_item_parent_ids( $parent_item->ID ); |
| 619 |
|
| 620 |
} |
| 621 |
|
| 622 |
} |
| 623 |
|
| 624 |
} |
| 625 |
|
| 626 |
private static function get_find_menu_items_to_parent_id( $parent_id = 0 ) { |
| 627 |
|
| 628 |
$sidebar_items_added_classes = self::get_sidebar_items_added_classes(); |
| 629 |
|
| 630 |
if( empty( $sidebar_items_added_classes ) ) { |
| 631 |
|
| 632 |
return false; |
| 633 |
|
| 634 |
} |
| 635 |
|
| 636 |
if( ! empty( $parent_id ) ) { |
| 637 |
|
| 638 |
if( is_numeric( $parent_id ) ) { |
| 639 |
|
| 640 |
$parent_id = intval( $parent_id ); |
| 641 |
|
| 642 |
} else { |
| 643 |
|
| 644 |
$parent_id = strip_tags( $parent_id ); |
| 645 |
|
| 646 |
} |
| 647 |
|
| 648 |
} |
| 649 |
|
| 650 |
if( ! empty( self::$find_parent_id[ $parent_id ] ) ) { |
| 651 |
|
| 652 |
return self::$find_parent_id[ $parent_id ]; |
| 653 |
|
| 654 |
} |
| 655 |
|
| 656 |
$find_items = array(); |
| 657 |
|
| 658 |
foreach( $sidebar_items_added_classes as $item ) { |
| 659 |
|
| 660 |
$item_parent = $item->item_parent; |
| 661 |
|
| 662 |
if( ! empty( $item_parent ) ) { |
| 663 |
|
| 664 |
if( is_numeric( $item_parent ) ) { |
| 665 |
|
| 666 |
$item_parent = intval( $item_parent ); |
| 667 |
|
| 668 |
} else { |
| 669 |
|
| 670 |
$item_parent = strip_tags( $item_parent ); |
| 671 |
|
| 672 |
} |
| 673 |
|
| 674 |
} |
| 675 |
|
| 676 |
if( $item_parent !== $parent_id ) { |
| 677 |
|
| 678 |
continue; |
| 679 |
|
| 680 |
} |
| 681 |
|
| 682 |
$find_items[] = $item; |
| 683 |
|
| 684 |
} |
| 685 |
|
| 686 |
if( empty( $find_items ) ) { |
| 687 |
|
| 688 |
return false; |
| 689 |
|
| 690 |
} |
| 691 |
|
| 692 |
self::$find_parent_id[ $parent_id ] = $find_items; |
| 693 |
|
| 694 |
return $find_items; |
| 695 |
|
| 696 |
} |
| 697 |
|
| 698 |
private static function get_current_url() { |
| 699 |
|
| 700 |
if( ! empty( self::$current_url ) ) { |
| 701 |
|
| 702 |
return self::$current_url; |
| 703 |
|
| 704 |
} |
| 705 |
|
| 706 |
$current_url = urldecode( do_shortcode( '[mywp_url current="1"]' ) ); |
| 707 |
|
| 708 |
self::$current_url = $current_url; |
| 709 |
|
| 710 |
return $current_url; |
| 711 |
|
| 712 |
} |
| 713 |
|
| 714 |
public static function admin_enqueue_scripts() { |
| 715 |
|
| 716 |
$sidebar = self::get_sidebar(); |
| 717 |
|
| 718 |
if( empty( $sidebar ) ) { |
| 719 |
|
| 720 |
return false; |
| 721 |
|
| 722 |
} |
| 723 |
|
| 724 |
wp_register_style( 'mywp_admin_sidebar' , MywpApi::get_plugin_url( 'css' ) . 'admin-sidebar.css' , array() , MYWP_VERSION ); |
| 725 |
wp_register_script( 'mywp_admin_sidebar' , MywpApi::get_plugin_url( 'js' ) . 'admin-sidebar.js' , array( 'jquery' ) , MYWP_VERSION ); |
| 726 |
|
| 727 |
wp_enqueue_style( 'mywp_admin_sidebar' ); |
| 728 |
wp_enqueue_script( 'mywp_admin_sidebar' ); |
| 729 |
|
| 730 |
$setting_data = self::get_setting_data(); |
| 731 |
|
| 732 |
if( ! empty( $setting_data['custom_menu_ui'] ) ) { |
| 733 |
|
| 734 |
wp_register_style( 'mywp_admin_sidebar_custom_ui' , MywpApi::get_plugin_url( 'css' ) . 'admin-sidebar-custom-ui.css' , array( 'mywp_admin_sidebar' ) , MYWP_VERSION ); |
| 735 |
wp_register_script( 'mywp_admin_sidebar_custom_ui' , MywpApi::get_plugin_url( 'js' ) . 'admin-sidebar-custom-ui.js' , array( 'jquery' , 'mywp_admin_sidebar' ) , MYWP_VERSION ); |
| 736 |
|
| 737 |
wp_enqueue_style( 'mywp_admin_sidebar_custom_ui' ); |
| 738 |
wp_enqueue_script( 'mywp_admin_sidebar_custom_ui' ); |
| 739 |
|
| 740 |
} |
| 741 |
|
| 742 |
} |
| 743 |
|
| 744 |
public static function hidden_default_menus() { |
| 745 |
|
| 746 |
global $menu; |
| 747 |
global $submenu; |
| 748 |
|
| 749 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 750 |
|
| 751 |
return false; |
| 752 |
|
| 753 |
} |
| 754 |
|
| 755 |
$sidebar = self::get_sidebar(); |
| 756 |
|
| 757 |
if( empty( $sidebar ) ) { |
| 758 |
|
| 759 |
return false; |
| 760 |
|
| 761 |
} |
| 762 |
|
| 763 |
$menu = array(); |
| 764 |
$submenu = array(); |
| 765 |
|
| 766 |
self::after_do_function( __FUNCTION__ ); |
| 767 |
|
| 768 |
} |
| 769 |
|
| 770 |
public static function render_menus() { |
| 771 |
|
| 772 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 773 |
|
| 774 |
return false; |
| 775 |
|
| 776 |
} |
| 777 |
|
| 778 |
$parent_items = self::get_find_menu_items_to_parent_id(); |
| 779 |
|
| 780 |
if( empty( $parent_items ) ) { |
| 781 |
|
| 782 |
return false; |
| 783 |
|
| 784 |
} |
| 785 |
|
| 786 |
foreach( $parent_items as $item ) { |
| 787 |
|
| 788 |
self::print_sidebar_item( $item ); |
| 789 |
|
| 790 |
} |
| 791 |
|
| 792 |
echo '<li id="sidebar-collapse"><span class="collapse-button-icon dashicons-before"></span></li>'; |
| 793 |
|
| 794 |
self::after_do_function( __FUNCTION__ ); |
| 795 |
|
| 796 |
} |
| 797 |
|
| 798 |
private static function print_sidebar_item( $item ) { |
| 799 |
|
| 800 |
if( empty( $item ) or empty( $item->item_type ) or empty( $item->ID ) ) { |
| 801 |
|
| 802 |
return false; |
| 803 |
|
| 804 |
} |
| 805 |
|
| 806 |
$item = apply_filters( 'mywp_controller_admin_sidebar_print_sidebar_item' , $item ); |
| 807 |
|
| 808 |
if( ! empty( $item->item_capability ) ) { |
| 809 |
|
| 810 |
if( ! current_user_can( $item->item_capability ) ) { |
| 811 |
|
| 812 |
return false; |
| 813 |
|
| 814 |
} |
| 815 |
|
| 816 |
} |
| 817 |
|
| 818 |
$item_id = $item->ID; |
| 819 |
|
| 820 |
if( is_numeric( $item_id ) ) { |
| 821 |
|
| 822 |
$item_id = intval( $item_id ); |
| 823 |
|
| 824 |
} else { |
| 825 |
|
| 826 |
$item_id = strip_tags( $item_id ); |
| 827 |
|
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
$item_type = $item->item_type; |
| 832 |
$item_parent = $item->item_parent; |
| 833 |
|
| 834 |
$item->item_link_title = do_shortcode( $item->item_link_title ); |
| 835 |
|
| 836 |
$li_class = ''; |
| 837 |
|
| 838 |
if( ! empty( $item->item_li_class ) ) { |
| 839 |
|
| 840 |
$li_class = $item->item_li_class; |
| 841 |
|
| 842 |
} |
| 843 |
|
| 844 |
$li_id = ''; |
| 845 |
|
| 846 |
if( ! empty( $item->item_li_id ) ) { |
| 847 |
|
| 848 |
$li_id = $item->item_li_id; |
| 849 |
|
| 850 |
} |
| 851 |
|
| 852 |
printf( '<li class="mywp-sidebar-item item-%d item-type-%s %s" id="%s">' , esc_attr( $item_id ) , esc_attr( $item_type ) , esc_attr( $li_class ) , esc_attr( $li_id ) ); |
| 853 |
|
| 854 |
if( $item_type == 'custom' ) { |
| 855 |
|
| 856 |
echo do_shortcode( $item->item_custom_html ); |
| 857 |
|
| 858 |
} elseif( $item_type == 'separator' ) { |
| 859 |
|
| 860 |
echo '<div class="separator"></div>'; |
| 861 |
|
| 862 |
} elseif( in_array( $item_type , array( 'default' , 'link' ) ) ) { |
| 863 |
|
| 864 |
$link_class = ''; |
| 865 |
|
| 866 |
if( ! empty( $item->item_link_class ) ) { |
| 867 |
|
| 868 |
$link_class = $item->item_link_class; |
| 869 |
|
| 870 |
} |
| 871 |
|
| 872 |
$link_id = ''; |
| 873 |
|
| 874 |
if( ! empty( $item->item_link_id ) ) { |
| 875 |
|
| 876 |
$link_id = $item->item_link_id; |
| 877 |
|
| 878 |
} |
| 879 |
|
| 880 |
$link_attr = ''; |
| 881 |
|
| 882 |
if( ! empty( $item->item_link_attr ) ) { |
| 883 |
|
| 884 |
$link_attr = $item->item_link_attr; |
| 885 |
|
| 886 |
} |
| 887 |
|
| 888 |
printf( '<a href="%s" class="mywp-sidebar-item-link %s" id="%s" %s>' , esc_url( $item->item_link_url ) , esc_attr( $link_class ) , esc_attr( $link_id ) , esc_attr( $link_attr ) ); |
| 889 |
|
| 890 |
$icon_class = ''; |
| 891 |
|
| 892 |
if( ! empty( $item->item_icon_class ) ) { |
| 893 |
|
| 894 |
$icon_class = $item->item_icon_class; |
| 895 |
|
| 896 |
} |
| 897 |
|
| 898 |
$icon_id = ''; |
| 899 |
|
| 900 |
if( ! empty( $item->item_icon_id ) ) { |
| 901 |
|
| 902 |
$icon_id = $item->item_icon_id; |
| 903 |
|
| 904 |
} |
| 905 |
|
| 906 |
$icon_title = ''; |
| 907 |
|
| 908 |
if( ! empty( $item->item_icon_title ) ) { |
| 909 |
|
| 910 |
$icon_title = $item->item_icon_title; |
| 911 |
|
| 912 |
} |
| 913 |
|
| 914 |
$icon_style = ''; |
| 915 |
|
| 916 |
if( ! empty( $item->item_icon_style ) ) { |
| 917 |
|
| 918 |
$icon_style = $item->item_icon_style; |
| 919 |
|
| 920 |
} |
| 921 |
|
| 922 |
$icon_img = ''; |
| 923 |
|
| 924 |
if( ! empty( $item->item_icon_img ) ) { |
| 925 |
|
| 926 |
$icon_img = $item->item_icon_img; |
| 927 |
|
| 928 |
} |
| 929 |
|
| 930 |
if( ! empty( $item->item_icon_img ) ) { |
| 931 |
|
| 932 |
printf( '<img src="%s" alt="%s"></div>' , esc_attr( $icon_img ) , esc_attr( $icon_title ) ); |
| 933 |
|
| 934 |
} elseif( ! empty( $icon_class ) or ! empty( $icon_style ) or ! empty( $icon_id ) ) { |
| 935 |
|
| 936 |
printf( '<div class="wp-menu-image mywp-sidebar-item-icon %s" id="%s" style="%s">%s</div>' , esc_attr( $icon_class ) , esc_attr( $icon_id ) , $icon_style , $icon_title ); |
| 937 |
|
| 938 |
} else { |
| 939 |
|
| 940 |
echo do_action( 'mywp_controller_admin_sidebar_print_sidebar_item_icon' , $item ); |
| 941 |
|
| 942 |
} |
| 943 |
|
| 944 |
printf( '<div class="wp-menu-name mywp-sidebar-name">%s</div>' , $item->item_link_title ); |
| 945 |
|
| 946 |
echo '</a>'; |
| 947 |
|
| 948 |
$child_items = self::get_find_menu_items_to_parent_id( $item_id ); |
| 949 |
|
| 950 |
if( ! empty( $child_items ) ) { |
| 951 |
|
| 952 |
echo '<ul class="wp-submenu wp-submenu-wrap mywp-sidebar-item-childs">'; |
| 953 |
|
| 954 |
printf( '<li class="wp-submenu-head" aria-hidden="true">%s</li>' , $item->item_link_title ); |
| 955 |
|
| 956 |
foreach( $child_items as $child_item ) { |
| 957 |
|
| 958 |
self::print_sidebar_item( $child_item ); |
| 959 |
|
| 960 |
} |
| 961 |
|
| 962 |
echo '</ul>'; |
| 963 |
|
| 964 |
} |
| 965 |
|
| 966 |
} |
| 967 |
|
| 968 |
echo '</li>'; |
| 969 |
|
| 970 |
} |
| 971 |
|
| 972 |
} |
| 973 |
|
| 974 |
MywpControllerModuleAdminSidebar::init(); |
| 975 |
|
| 976 |
endif; |
| 977 |
|