| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if ( ! class_exists( 'MywpFrontendToolbar' ) ) : |
| 8 |
|
| 9 |
final class MywpFrontendToolbar { |
| 10 |
|
| 11 |
private static $default_toolbar; |
| 12 |
|
| 13 |
public static function init() {} |
| 14 |
|
| 15 |
public static function get_default_toolbar() { |
| 16 |
|
| 17 |
if( ! empty( self::$default_toolbar ) ) { |
| 18 |
|
| 19 |
return self::$default_toolbar; |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
$mywp_model = new MywpModel( 'frontend_regist_toolbar' , 'class' ); |
| 24 |
|
| 25 |
$option = $mywp_model->get_option(); |
| 26 |
|
| 27 |
if( empty( $option['home'] ) ) { |
| 28 |
|
| 29 |
return false; |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
self::$default_toolbar = $option['home']; |
| 34 |
|
| 35 |
return self::$default_toolbar; |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
private static function find_default_menu( $find_id = false , $find_parent_id = false ) { |
| 40 |
|
| 41 |
if( empty( $find_id ) ) { |
| 42 |
|
| 43 |
return false; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
$default_toolbar = self::get_default_toolbar(); |
| 48 |
|
| 49 |
if( empty( $default_toolbar['left'] ) && empty( $default_toolbar['right'] ) ) { |
| 50 |
|
| 51 |
return false; |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
$find_id = strip_tags( do_shortcode( $find_id ) ); |
| 56 |
$find_parent_id = strip_tags( $find_parent_id ); |
| 57 |
|
| 58 |
$found_current_default = false; |
| 59 |
$found_parent_default = array(); |
| 60 |
$found_childs_default = array(); |
| 61 |
|
| 62 |
foreach( $default_toolbar as $menu_location => $menus ) { |
| 63 |
|
| 64 |
foreach( $menus as $menu_key => $menu ) { |
| 65 |
|
| 66 |
if( (string) $menu->id === (string) $find_id && (string) $menu->parent === (string) $find_parent_id ) { |
| 67 |
|
| 68 |
$found_current_default = $menu; |
| 69 |
break; |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
if( ! empty( $found_current_default ) ) { |
| 78 |
|
| 79 |
foreach( $default_toolbar as $menu_location => $menus ) { |
| 80 |
|
| 81 |
foreach( $menus as $menu_key => $menu ) { |
| 82 |
|
| 83 |
if( (string) $menu->parent === (string) $found_current_default->id ) { |
| 84 |
|
| 85 |
$found_childs_default[] = $menu; |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
if( ! empty( $found_current_default->parent) ) { |
| 94 |
|
| 95 |
foreach( $default_toolbar as $menu_location => $menus ) { |
| 96 |
|
| 97 |
foreach( $menus as $menu_key => $menu ) { |
| 98 |
|
| 99 |
if( (string) $menu->id === (string) $found_current_default->parent ) { |
| 100 |
|
| 101 |
$found_parent_default[] = $menu; |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
return array( 'current' => $found_current_default , 'parent' => $found_parent_default , 'childs' => $found_childs_default ); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
public static function default_item_convert( $item = false ) { |
| 117 |
|
| 118 |
$called_text = sprintf( '%s::%s( %s )' , __CLASS__ , __FUNCTION__ , '$item' ); |
| 119 |
|
| 120 |
if( empty( $item ) or ! is_object( $item ) ) { |
| 121 |
|
| 122 |
MywpHelper::error_not_found_message( '$item' , $called_text ); |
| 123 |
return false; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
if( empty( $item->item_type ) ) { |
| 128 |
|
| 129 |
return false; |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
if( $item->item_type !== 'default') { |
| 134 |
|
| 135 |
return false; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
if( empty( $item->item_default_id ) && empty( $item->item_default_parent_id ) ) { |
| 140 |
|
| 141 |
return false; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
$default_menu = self::find_default_menu( $item->item_default_id , $item->item_default_parent_id ); |
| 146 |
|
| 147 |
if( empty( $default_menu['current'] ) ) { |
| 148 |
|
| 149 |
return false; |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
$item->item_default_title = $default_menu['current']->title; |
| 154 |
$item->item_meta = $default_menu['current']->meta; |
| 155 |
|
| 156 |
if( ! in_array( $item->item_default_id , array( 'site-name' , 'edit' , 'logout' ) ) ) { |
| 157 |
|
| 158 |
$item->item_link_url = $default_menu['current']->href; |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
return apply_filters( 'mywp_frontend_toolbar_default_item_convert' , $item ); |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
MywpFrontendToolbar::init(); |
| 169 |
|
| 170 |
endif; |
| 171 |
|