PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.8.2
Kubio AI Page Builder v2.8.2
2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / build / block-library / blocks / menu / menu-items / index.php
kubio / build / block-library / blocks / menu / menu-items Last commit date
block.json 3 years ago index.php 1 year ago
index.php
170 lines
1 <?php
2
3 namespace Kubio\Blocks;
4
5 use Kubio\Core\Blocks\BlockBase;
6 use Kubio\Core\Registry;
7 use Kubio\Core\Utils;
8
9 class MenuItemsBlock extends BlockBase {
10 const BLOCK_NAME = 'kubio/menu-items';
11 const PARENT_MENU_ARROW = '<svg class="kubio-menu-item-icon" role="img" viewBox="0 0 320 512">' .
12 ' <path d="M143 352.3L7 216.3c-9.4-9.4-9.4-24.6 0-33.9l22.6-22.6c9.4-9.4 24.6-9.4 33.9 0l96.4 96.4 96.4-96.4c9.4-9.4 24.6-9.4 33.9 0l22.6 22.6c9.4 9.4 9.4 24.6 0 33.9l-136 136c-9.2 9.4-24.4 9.4-33.8 0z"></path>' .
13 '</svg>';
14
15
16 private static $instances = array();
17
18
19 public function __construct( $block, $autoload = true ) {
20 parent::__construct( $block, $autoload );
21 }
22
23 public function addParentMenuItemsIcon( $args, $item, $depth ) {
24 if ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
25 $args->link_before = '<span>';
26 $args->link_after = '</span>' . MenuItemsBlock::PARENT_MENU_ARROW;
27 } else {
28 $args->link_before = '';
29 $args->link_after = '';
30 }
31
32 return $args;
33 }
34
35 public function fixCSSClasses( $classes, $item, $args, $depth ) {
36 $next_classes = array_diff( $classes, array( 'current-menu-item', 'current_page_item' ) );
37 $url = $item->url;
38
39 if ( preg_replace( '/#(.*)/', '', $url ) !== $url ) {
40 $classes = $next_classes;
41 }
42
43 return $classes;
44 }
45
46 public function mapPropsToElements() {
47 return array(
48 'outer' => array(
49 'innerHTML' => $this->renderMenu(),
50 ),
51 );
52 }
53
54
55 public function menuItemsAttrs( $atts, $item, $args, $depth ) {
56 $style = isset( $atts['style'] ) ? $atts['style'] : '';
57
58 $depth_value = min( array( $depth, 4 ) );
59
60 $style .= ";--kubio-menu-item-depth:{$depth_value}";
61
62 $atts['style'] = $style;
63
64 return $atts;
65 }
66
67 public function renderMenu() {
68
69 $is_first_level_only = $this->isForcedOnlyFirstLevel();
70
71 add_filter( 'kubio/nav_menu_link_attributes', array( $this, 'menuItemsAttrs' ), 2, 4 );
72
73 $location = $this->getAttribute( 'location', '' );
74 $id = $this->getAttribute( 'id', '' );
75
76 $menu = empty( $location ) ? $id : $location;
77 // Try to use a cached output for Menu Items based on this key.
78 $instance_key = $menu . '-' . ( $is_first_level_only ? 'first_level' : 'full' );
79
80 if ( isset( static::$instances[ $instance_key ] ) ) {
81 return static::$instances[ $instance_key ];
82 }
83
84 if ( ! $this->isForcedOnlyFirstLevel() ) {
85 add_filter( 'kubio/nav_menu_item_args', array( $this, 'addParentMenuItemsIcon' ), 2, 3 );
86 add_filter( 'kubio/nav_menu_css_class', array( $this, 'fixCSSClasses' ), 2, 4 );
87 }
88
89 if ( ! class_exists( 'Kubio_Nav_Menu_Walker' ) ) {
90 require_once KUBIO_ROOT_DIR . '/lib/menu/class-kubio-menu-wallker.php';
91 }
92
93 $args = array(
94 'container' => false,
95 'depth' => $is_first_level_only ? 1 : 0,
96 'echo' => false,
97 'fallback_cb' => array( $this, 'fallback' ),
98 'menu_class' => 'menu kubio-has-gap-fallback',
99 'walker' => new \Kubio_Nav_Menu_Walker(),
100 );
101
102 if ( $location ) {
103 $args['theme_location'] = $location;
104 }
105
106 if ( $menu ) {
107 $args['menu'] = $menu;
108 }
109
110 // add a dummy location when the many has nothing assigned to it so we can trigger the fallback
111 if ( empty( $location ) && empty( $id ) ) {
112 $args['theme_location'] = uniqid( 'kubio-dummy-location-' );
113 }
114
115 // because has_filter returns a number when the filter is added we strictly check for false
116 $has_wc_page_endpoint_title = false !== has_filter( 'the_title', 'wc_page_endpoint_title' );
117
118 if ( $has_wc_page_endpoint_title ) {
119 remove_filter( 'the_title', 'wc_page_endpoint_title' );
120 }
121
122 $menu_content = wp_nav_menu( $args );
123 if ( ! $this->isForcedOnlyFirstLevel() ) {
124 remove_filter( 'kubio/nav_menu_item_args', array( $this, 'addParentMenuItemsIcon' ), 2 );
125 remove_filter( 'kubio/nav_menu_css_class', array( $this, 'fixCSSClasses' ), 2 );
126 }
127
128 remove_filter( 'kubio/nav_menu_link_attributes', array( $this, 'menuItemsAttrs' ), 2, 4 );
129 remove_filter( 'the_title', 'wc_page_endpoint_title' );
130
131 if ( $has_wc_page_endpoint_title ) {
132 add_filter( 'the_title', 'wc_page_endpoint_title' );
133 }
134
135 static::$instances[ $instance_key ] = $menu_content;
136
137 return $menu_content;
138 }
139
140 public function fallback() {
141 if ( is_user_logged_in() ) {
142 return Utils::getFrontendPlaceHolder(
143 sprintf(
144 '%s<br/><div class="kubio-frontent-placeholder--small">%s</div>',
145 __( 'This block has no menu assigned.', 'kubio' ),
146 __( 'Edit this page to select a menu or delete this block.', 'kubio' )
147 )
148 );
149 } else {
150 return '';
151 }
152 }
153
154 private function isForcedOnlyFirstLevel() {
155 $menu_block = Registry::getInstance()->getLastBlockOfName( DropDownMenuBlock::BLOCK_NAME );
156
157 if ( ! $menu_block ) {
158 $menu_block = Registry::getInstance()->getLastBlockOfName( AccordionMenuBlock::BLOCK_NAME );
159 }
160
161 return $menu_block->getAttribute( 'hideSubmenu', false );
162 }
163 }
164
165
166 Registry::registerBlock(
167 __DIR__,
168 MenuItemsBlock::class
169 );
170