| 1 |
<?php |
| 2 |
|
| 3 |
////////////////////////////////////////////////////////////// |
| 4 |
//=========================================================== |
| 5 |
// nav_walker.php |
| 6 |
//=========================================================== |
| 7 |
// PAGELAYER |
| 8 |
// Inspired by the DESIRE to be the BEST OF ALL |
| 9 |
// ---------------------------------------------------------- |
| 10 |
// Started by: Pulkit Gupta |
| 11 |
// Date: 23rd Jan 2017 |
| 12 |
// Time: 23:00 hrs |
| 13 |
// Site: http://pagelayer.com/wordpress (PAGELAYER) |
| 14 |
// ---------------------------------------------------------- |
| 15 |
// Please Read the Terms of use at http://pagelayer.com/tos |
| 16 |
// ---------------------------------------------------------- |
| 17 |
//=========================================================== |
| 18 |
// (c)Pagelayer Team |
| 19 |
//=========================================================== |
| 20 |
////////////////////////////////////////////////////////////// |
| 21 |
|
| 22 |
// Are we being accessed directly ? |
| 23 |
if(!defined('PAGELAYER_VERSION')) { |
| 24 |
exit('Hacking Attempt !'); |
| 25 |
} |
| 26 |
|
| 27 |
if ( ! class_exists( 'Pagelayer_Walker_Nav_Menu' ) ) { |
| 28 |
|
| 29 |
class Pagelayer_Walker_Nav_Menu extends Walker_Nav_Menu{ |
| 30 |
|
| 31 |
|
| 32 |
// Starts the list before the elements are added. |
| 33 |
public function start_lvl( &$output, $depth = 0, $args = null ) { |
| 34 |
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { |
| 35 |
$t = ''; |
| 36 |
$n = ''; |
| 37 |
} else { |
| 38 |
$t = "\t"; |
| 39 |
$n = "\n"; |
| 40 |
} |
| 41 |
$indent = str_repeat( $t, $depth ); |
| 42 |
|
| 43 |
// Default class. |
| 44 |
$classes = array( 'sub-menu' ); |
| 45 |
|
| 46 |
// Filters the CSS class(es) applied to a menu list element. |
| 47 |
$class_names = implode( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) ); |
| 48 |
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
| 49 |
|
| 50 |
$output .= "{$n}{$indent}<ul$class_names>{$n}"; |
| 51 |
} |
| 52 |
|
| 53 |
// Ends the list of after the elements are added. |
| 54 |
public function end_lvl( &$output, $depth = 0, $args = null ) { |
| 55 |
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { |
| 56 |
$t = ''; |
| 57 |
$n = ''; |
| 58 |
} else { |
| 59 |
$t = "\t"; |
| 60 |
$n = "\n"; |
| 61 |
} |
| 62 |
$indent = str_repeat( $t, $depth ); |
| 63 |
$output .= "$indent</ul>{$n}"; |
| 64 |
} |
| 65 |
|
| 66 |
// Starts the element output. |
| 67 |
public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { |
| 68 |
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { |
| 69 |
$t = ''; |
| 70 |
$n = ''; |
| 71 |
} else { |
| 72 |
$t = "\t"; |
| 73 |
$n = "\n"; |
| 74 |
} |
| 75 |
$indent = ( $depth ) ? str_repeat( $t, $depth ) : ''; |
| 76 |
|
| 77 |
// Menu setting keys |
| 78 |
$keys = array('_pagelayer_content'); |
| 79 |
|
| 80 |
foreach($keys as $key){ |
| 81 |
$menu_item_setting = get_post_meta( $item->ID, $key, true ); |
| 82 |
|
| 83 |
if(!empty($menu_item_setting)){ |
| 84 |
$item->$key = $menu_item_setting; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
$classes = empty( $item->classes ) ? array() : (array) $item->classes; |
| 89 |
$classes[] = 'menu-item-' . $item->ID; |
| 90 |
|
| 91 |
// Get custom setting data |
| 92 |
$item_content = $this->get_item_data($item, '_pagelayer_content'); |
| 93 |
|
| 94 |
$settings = array(); |
| 95 |
$pagelayer_has_content = 0; |
| 96 |
|
| 97 |
if(!empty($item_content) && has_blocks($item_content)){ |
| 98 |
|
| 99 |
$blocks = parse_blocks($item_content); |
| 100 |
|
| 101 |
$attrs = array(); |
| 102 |
|
| 103 |
foreach($blocks as $index => $block){ |
| 104 |
if($block['blockName'] != 'pagelayer/pl_nav_menu_item'){ |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
// Overrig the menu ID to apply css and others |
| 109 |
foreach($item as $kk => $vv){ |
| 110 |
if($kk == '_pagelayer_content'){ |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
$block['attrs'][$kk] = $vv; |
| 115 |
} |
| 116 |
|
| 117 |
$settings = $block['attrs']; |
| 118 |
|
| 119 |
// Add settings to $item |
| 120 |
foreach($settings as $skey => $sval){ |
| 121 |
|
| 122 |
if(isset($item->$skey)){ |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
$item->$skey = $sval; |
| 127 |
} |
| 128 |
|
| 129 |
if(!empty($block['innerBlocks'])){ |
| 130 |
$pagelayer_has_content = $this->has_block_content($block['innerBlocks']); |
| 131 |
} |
| 132 |
|
| 133 |
$blocks[$index] = $block; |
| 134 |
} |
| 135 |
|
| 136 |
$item_content = serialize_blocks($blocks); |
| 137 |
} |
| 138 |
|
| 139 |
$pagelayer_has_content = pagelayer_is_live() ? 1 : $pagelayer_has_content; |
| 140 |
|
| 141 |
// Get menu type |
| 142 |
$menu_type = $this->get_item_data($item, 'menu_type'); |
| 143 |
|
| 144 |
if(!empty($menu_type) && $menu_type == 'mega' && $depth == 0 && !empty($pagelayer_has_content)){ |
| 145 |
$classes[] = 'pagelayer-mega-menu-item'; |
| 146 |
} |
| 147 |
|
| 148 |
if(!empty($menu_type) && $menu_type == 'column' && $depth == 0){ |
| 149 |
$classes[] = 'pagelayer-mega-column-item'; |
| 150 |
} |
| 151 |
|
| 152 |
$menu_icon_class = $this->get_item_data($item, 'icon_position'); |
| 153 |
if(!empty($menu_icon_class)){ |
| 154 |
$classes[] = 'pagelayer-nav-menu-icon-'.$menu_icon_class; |
| 155 |
} |
| 156 |
|
| 157 |
// Filters the arguments for a single nav menu item. |
| 158 |
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth ); |
| 159 |
|
| 160 |
// Filters the CSS classes applied to a menu item's list item element. |
| 161 |
$class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) ); |
| 162 |
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
| 163 |
|
| 164 |
// Filters the ID applied to a menu item's list item element. |
| 165 |
$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth ); |
| 166 |
$id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; |
| 167 |
|
| 168 |
$output .= $indent . '<li' . $id . $class_names . '>'; |
| 169 |
|
| 170 |
$atts = array(); |
| 171 |
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; |
| 172 |
$atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
| 173 |
if ( '_blank' === $item->target && empty( $item->xfn ) ) { |
| 174 |
$atts['rel'] = 'noopener'; |
| 175 |
} else { |
| 176 |
$atts['rel'] = $item->xfn; |
| 177 |
} |
| 178 |
$atts['href'] = ! empty( $item->url ) ? $item->url : ''; |
| 179 |
$atts['aria-current'] = $item->current ? 'page' : ''; |
| 180 |
|
| 181 |
// Filters the HTML attributes applied to a menu item's anchor element. |
| 182 |
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth ); |
| 183 |
|
| 184 |
$attributes = ''; |
| 185 |
foreach ( $atts as $attr => $value ) { |
| 186 |
if ( is_scalar( $value ) && '' !== $value && false !== $value ) { |
| 187 |
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
| 188 |
|
| 189 |
// Get disable links |
| 190 |
$disable_link = $this->get_item_data($item, 'disable_link'); |
| 191 |
if ( 'href' === $attr && !empty($disable_link) ) { |
| 192 |
$value = 'javascript:void(0)'; |
| 193 |
} |
| 194 |
$attributes .= ' ' . $attr . '="' . $value . '"'; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** This filter is documented in wp-includes/post-template.php */ |
| 199 |
$title = $this->get_item_data($item, 'title'); |
| 200 |
$title = apply_filters( 'the_title', $title, $item->ID ); |
| 201 |
|
| 202 |
// Filters a menu item's title. |
| 203 |
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth ); |
| 204 |
|
| 205 |
$item_output = $args->before; |
| 206 |
$item_output .= '<a' . $attributes . '>'; |
| 207 |
|
| 208 |
// Add menu icon |
| 209 |
$menu_icon = $this->get_item_data($item, 'menu_icon'); |
| 210 |
if (!empty($menu_icon)) { |
| 211 |
$item_output .= '<i class="pagelayer-menu-icon '.$menu_icon.'"></i>'; |
| 212 |
} |
| 213 |
|
| 214 |
$item_output .= '<span class="pagelayer-nav-menu-title">' . $args->link_before . $title . $args->link_after .'</span>'; |
| 215 |
|
| 216 |
// Add highlight lable |
| 217 |
$highlight_label = $this->get_item_data($item, 'highlight_label'); |
| 218 |
if (!empty($highlight_label)) { |
| 219 |
$item_output .= '<span class="pagelayer-menu-highlight">'.$highlight_label.'</span>'; |
| 220 |
} |
| 221 |
|
| 222 |
$item_output .= '</a>'; |
| 223 |
$item_output .= $args->after; |
| 224 |
|
| 225 |
$mega_class = 'pagelayer-mega-editor-'.$item->ID; |
| 226 |
|
| 227 |
$item_output .= '<div class="pagelayer-mega-menu '.$mega_class.'">'; |
| 228 |
|
| 229 |
// Add mega menu |
| 230 |
if(!empty($item_content)){ |
| 231 |
$item_content = pagelayer_the_content($item_content, true); |
| 232 |
|
| 233 |
// Change the pagelayer ID |
| 234 |
$item_output .= pagelayer_change_id($item_content); |
| 235 |
} |
| 236 |
|
| 237 |
$item_output .= '</div>'; |
| 238 |
|
| 239 |
// Filters a menu item's starting output. |
| 240 |
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
| 241 |
} |
| 242 |
|
| 243 |
// Ends the element output, if needed. |
| 244 |
public function end_el( &$output, $item, $depth = 0, $args = null ) { |
| 245 |
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { |
| 246 |
$t = ''; |
| 247 |
$n = ''; |
| 248 |
} else { |
| 249 |
$t = "\t"; |
| 250 |
$n = "\n"; |
| 251 |
} |
| 252 |
$output .= "</li>{$n}"; |
| 253 |
} |
| 254 |
|
| 255 |
// Get the post value |
| 256 |
public function get_item_data( $item, $name ) { |
| 257 |
|
| 258 |
if(pagelayer_is_live() && isset($_REQUEST['pagelayer_nav_items']) && isset($_REQUEST['pagelayer_nav_items'][$item->ID][$name]) ){ |
| 259 |
$data = $_REQUEST['pagelayer_nav_items'][$item->ID][$name]; |
| 260 |
|
| 261 |
// If Title is empty |
| 262 |
if(empty($data) && $name == 'title'){ |
| 263 |
$_item = clone $item; |
| 264 |
$_item->post_title = ''; |
| 265 |
$_item = wp_setup_nav_menu_item($_item); |
| 266 |
return $_item->title; |
| 267 |
} |
| 268 |
|
| 269 |
if(!empty($data) && $name == '_pagelayer_content'){ |
| 270 |
$data = base64_decode($data); |
| 271 |
} |
| 272 |
|
| 273 |
return stripslashes_deep($data); |
| 274 |
} |
| 275 |
|
| 276 |
return @$item->$name; |
| 277 |
} |
| 278 |
|
| 279 |
// Check the block has inner block |
| 280 |
public function has_block_content( $blocks ) { |
| 281 |
|
| 282 |
$tags = array('pagelayer/pl_row', 'pagelayer/pl_inner_row', 'pagelayer/pl_col', 'pagelayer/pl_inner_col'); |
| 283 |
$has_content = 0; |
| 284 |
|
| 285 |
foreach($blocks as $block){ |
| 286 |
if(!in_array( $block['blockName'], $tags) || !empty($has_content)){ |
| 287 |
$has_content = 1; |
| 288 |
break; |
| 289 |
} |
| 290 |
|
| 291 |
if(empty($block['innerBlocks']) ){ |
| 292 |
continue; |
| 293 |
} |
| 294 |
|
| 295 |
$has_content = $this->has_block_content($block['innerBlocks']); |
| 296 |
} |
| 297 |
|
| 298 |
return $has_content; |
| 299 |
} |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
} |