-
Notifications
You must be signed in to change notification settings - Fork 7
/
submenu.php
279 lines (232 loc) · 6.71 KB
/
submenu.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/*
Plugin Name: JC Submenu
Plugin URI: http://jamescollings.co.uk/blog/jc-submenu-dynamic-wordpress-menu-plugin/
Description: Wordpress Submenu Plugin, automatically populate your navigation menus with custom post_types, taxonomies, or child pages. An easy to use plugin created to be a lightweight menu extension.
Version: 0.8.4
Author: James Collings
Author URI: http://www.jamescollings.co.uk
*/
/**
* JC Submenu Class
*
* Core plugin file, load all required classes
*
* @author James Collings <james@jclabs.co.uk>
* @version 0.8.4
*/
class JCSubmenu{
var $version = '0.8.4';
var $version_check = 70;
var $plugin_dir = false;
var $plugin_url = false;
var $prefix = 'jc-submenu';
var $edit_walker = false;
var $public_walker = true;
/**
* Setup plugin
* @return void
*/
function __construct(){
$this->plugin_dir = plugin_dir_path( __FILE__ );
$this->plugin_url = plugins_url( '/', __FILE__ );
$this->load_modules();
// add plugin hooks
add_action('jcs/menu_section', array($this, 'output_menu_section'), 10, 2);
add_action('jcs/split_menu', array($this, 'output_split_menu'), 10, 2);
// add plugin shortcodes
add_shortcode( 'jcs_split_menu', array($this, 'split_menu_shortcode') );
add_shortcode( 'jcs_menu_section', array($this, 'menu_section_shortcode') );
// init menu attachment
add_action('init', array($this, 'init'));
}
/**
* Set which type of attachment is used
* @return void
*/
public function init(){
$this->public_walker = apply_filters('jcs/enable_public_walker', true );
if(!$this->public_walker){
add_filter( 'wp_nav_menu_objects', array( $this, 'populate_menu_items' ));
}else{
add_filter( 'wp_nav_menu_args', array( $this, 'attach_menu_walker' ));
}
}
/**
* Attach custom nav walker
*
* Hook into theme menu, attach custom walker
*
* @param array $args
* @return array
*/
function attach_menu_walker($args){
if(empty($args['walker'])){
$args['walker'] = new JC_Submenu_Nav_Walker();
}
return $args;
}
/**
* Add menu items without using a custom walker
*
* @param array $menu_items
* @return array new menu items
*/
function populate_menu_items($menu_items = array()){
$walker = new JC_Submenu_Nav_Walker();
$menu_items = $walker->attach_elements($menu_items);
$menu_items = $walker->_process_menu($menu_items);
return $menu_items;
}
/**
* Load Required Modules
* @return void
*/
function load_modules(){
include 'walkers/AdminMenuWalker.php';
include 'walkers/SubmenuWalker.php';
include 'walkers/DropdownWalker.php';
include 'widgets/SectionMenuWidget.php';
include 'widgets/SplitMenuWidget.php';
include 'SubmenuModel.php';
SubmenuModel::init($this);
include 'SubmenuAdmin.php';
new SubmenuAdmin($this);
}
/**
* Slit Menu Section Shortcode
*
* Display a dynamic split menu section via wordpress shortcode tags
*
* @param array $atts
* @return string
*/
function split_menu_shortcode($atts){
extract(shortcode_atts( array(
'hierarchy' => 1,
'start' => 0,
'depth' => 5,
'show_parent' => 0,
'menu' => false,
'trigger_depth' => 0
), $atts ));
if(!$menu)
return false;
ob_start();
do_action('jcs/split_menu', $menu, array(
'hierarchy' => $hierarchy,
'start' => $start,
'depth' => $depth,
'show_parent' => $show_parent,
'trigger_depth' => $trigger_depth
));
$output = ob_get_contents();
ob_end_clean();
return $output;
}
/**
* Menu Section Shortcode
*
* Display section of menu via wordpress shortcode tags
*
* @param array $atts
* @return string
*/
function menu_section_shortcode($atts){
extract(shortcode_atts( array(
'hierarchy' => 1,
'start' => 0,
'depth' => 5,
'show_parent' => 0,
'menu' => false
), $atts ));
if(!$menu)
return false;
ob_start();
do_action('jcs/menu_section', $menu, array(
'hierarchy' => $hierarchy,
'start' => $start,
'depth' => $depth,
'show_parent' => $show_parent,
));
$output = ob_get_contents();
ob_end_clean();
return $output;
}
/**
* Output menu section
*
* Display a section of the selected menu in your theme
*
* @param string $menu
* @param array $args
* @return void
*/
function output_menu_section($menu, $args = array()){
$debug = isset($args['debug']) ? $args['debug'] : false;
$hierarchical = isset($args['hierarchy']) ? $args['hierarchy'] : 1;
$start = isset($args['start']) ? $args['start'] : 0;
$depth = isset($args['depth']) ? $args['depth'] : 5;
$show_parent = isset($args['show_parent']) ? $args['show_parent'] : 0;
$options = array('menu' => $menu, 'walker' => new JC_Submenu_Nav_Walker(array(
'debug' => $debug,
'section_menu' => true,
'menu_item' => $start,
'menu_depth' => $depth,
'show_parent' => $show_parent
))
);
if(isset($args['menu_class']))
$options['menu_class'] = $args['menu_class'];
if(isset($args['menu_id']))
$options['menu_id'] = $args['menu_id'];
if(isset($args['container']))
$options['container'] = $args['container'];
if(isset($args['container_id']))
$options['container_id'] = $args['container_id'];
if(isset($args['container_class']))
$options['container_class'] = $args['container_class'];
wp_nav_menu($options);
}
/**
* Output Split Menu
*
* Display a dynamic section of the selected menu in your theme relative to your current page
*
* @param string $menu
* @param array $args
* @return void
*/
function output_split_menu($menu, $args = array()){
$hierarchical = isset($args['hierarchy']) ? $args['hierarchy'] : 1;
$menu_start = isset($args['start']) ? $args['start'] : 1;
$menu_depth = isset($args['depth']) ? $args['depth'] : 5;
$show_parent = isset($args['show_parent']) ? $args['show_parent'] : 0;
$trigger_depth = isset($args['trigger_depth']) ? $args['trigger_depth'] : 0;
$parent_label = isset($args['parent_label']) ? $args['parent_label'] : false;
$options = array(
'menu' => $menu, 'walker' => new JC_Submenu_Nav_Walker(array(
'hierarchical' => $hierarchical,
'menu_start' => $menu_start,
'menu_depth' => $menu_depth,
'show_parent' => $show_parent,
'trigger_depth' => $trigger_depth,
'parent_label' => $parent_label,
'split_menu' => true
))
);
if(isset($args['menu_class']))
$options['menu_class'] = $args['menu_class'];
if(isset($args['menu_id']))
$options['menu_id'] = $args['menu_id'];
if(isset($args['container']))
$options['container'] = $args['container'];
if(isset($args['container_id']))
$options['container_id'] = $args['container_id'];
if(isset($args['container_class']))
$options['container_class'] = $args['container_class'];
wp_nav_menu($options);
}
}
$GLOBALS['jcsubmenu'] = new JCSubmenu();
?>