forked from billerickson/be-mega-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbe-mega-menu.php
219 lines (182 loc) · 5.65 KB
/
be-mega-menu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* Plugin Name: BE Mega Menu
* Plugin URI: https://github.com/billerickson/be-mega-menu
* Description: Use a visual editor for managing mega menu dropdowns
* Author: Bill Erickson
* Author URI: http://www.billerickson.net
* Version: 1.1.0
*
* BE Mega Menu is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* BE Mega Menu is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BE Mega Menu. If not, see <http://www.gnu.org/licenses/>.
*
* @package BE_Mega_Menu
* @author Bill Erickson
* @since 1.0.0
* @license GPL-2.0+
* @copyright Copyright (c) 2015
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Main class
*
* @since 1.1.0
* @package BE_Mega_Menu
*/
final class BE_Mega_Menu {
/**
* Menu Location
*
* @since 1.1.0
*/
public $menu_location = 'header';
/**
* Plugin Constructor.
*
* @since 1.1.0
* @return BE_Mega_Menu
*/
function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
/**
* Initialize
*
* @since 1.1.0
*/
function init() {
// Set new location
$this->menu_location = apply_filters( 'be_mega_menu_location', $this->menu_location );
add_action( 'init', array( $this, 'register_cpt' ), 20 );
add_filter( 'wp_nav_menu_args', array( $this, 'limit_menu_depth' ) );
add_filter( 'nav_menu_css_class', array( $this, 'menu_item_classes' ), 10, 4 );
add_filter( 'walker_nav_menu_start_el', array( $this, 'display_mega_menus' ), 10, 4 );
}
/**
* Register Mega Menu post type
*
* @since 1.0.0
*/
function register_cpt() {
$labels = array(
'name' => 'Mega Menus',
'singular_name' => 'Mega Menu',
'add_new' => 'Add New',
'add_new_item' => 'Add New Mega Menu',
'edit_item' => 'Edit Mega Menu',
'new_item' => 'New Mega Menu',
'view_item' => 'View Mega Menu',
'search_items' => 'Search Mega Menus',
'not_found' => 'No Mega Menus found',
'not_found_in_trash' => 'No Mega Menus found in Trash',
'parent_item_colon' => 'Parent Mega Menu:',
'menu_name' => 'Mega Menus',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array( 'title', 'editor', 'revisions' ),
'public' => false,
'show_ui' => true,
'show_in_menu' => 'themes.php',
'show_in_nav_menus' => false,
'publicly_queryable' => true,
'exclude_from_search' => true,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => array( 'slug' => 'megamenu', 'with_front' => false ),
'menu_icon' => 'dashicons-editor-table', // https://developer.wordpress.org/resource/dashicons/
);
register_post_type( 'megamenu', apply_filters( 'be_mega_menu_post_type_args', $args ) );
}
/**
* Limit Menu Depth
*
* @since 1.0.0
* @param array $args
* @return array
*/
function limit_menu_depth( $args ) {
if( $this->menu_location == $args['theme_location'] )
$args['depth'] = 1;
return $args;
}
/**
* Menu Item Classes
*
* @since 1.1.0
* @param array $classes
* @param object $item
* @param object $args
* @param int $depth
* @return array
*/
function menu_item_classes( $classes, $item, $args, $depth ) {
if( $this->menu_location != $args->theme_location )
return $classes;
if( in_array( 'menu-item-has-children', $classes ) )
$classes = array_diff( $classes, array( 'menu-item-has-children' ) );
if( $this->mega_menu( $item ) )
$classes[] = 'menu-item-has-children';
return $classes;
}
/**
* Display Mega Menus
*
* @since 1.0.0
* @param string $item_output
* @param object $item
* @param int $depth
* @param object $args
* @return string
*/
function display_mega_menus( $item_output, $item, $depth, $args ) {
if( ! ( $this->menu_location == $args->theme_location && 0 == $depth ) )
return $item_output;
$submenu_object = $this->mega_menu( $item );
if( !empty( $submenu_object ) && ! is_wp_error( $submenu_object ) ) {
$opening_markup = apply_filters( 'be_mega_menu_opening_markup', '<div class="mega-menu"><div class="wrap">' );
$closing_markup = apply_filters( 'be_mega_menu_closing_markup', '</div></div>' );
$submenu = $opening_markup . apply_filters( 'be_mega_menu_content', $submenu_object->post_content, $submenu_object ) . $closing_markup;
$item_output = str_replace( '</a>', '</a>' . $submenu, $item_output );
}
return $item_output;
}
/**
* Mega Menu
*
* @since 1.1.0
* @param object $item
* @return object $submenu_object
*/
function mega_menu( $item ) {
$submenu_object = false;
foreach( $item->classes as $class ) {
if( strpos( $class, 'megamenu-' ) !== false )
$submenu_object = get_post( intval( str_replace( 'megamenu-', '', $class ) ) );
}
if( ! $submenu_object )
$submenu_object = get_page_by_title( $item->title, false, 'megamenu' );
// WPML Support
if( function_exists( 'icl_object_id' ) && $submenu_object ) {
$translation = icl_object_id( $submenu_object->ID, 'megamenu', false );
if( $translation ) {
$submenu_object = get_post( intval( $translation ) );
}
}
return $submenu_object;
}
}
new BE_Mega_Menu;