Version Description
Download this release
Release Info
Developer | Mat Lipe |
Plugin | Advanced Sidebar Menu |
Version | 6.4.2 |
Comparing to | |
See all releases |
Code changes from version 6.4.1 to 6.4.2
- advanced-sidebar-menu.php +2 -2
- readme.txt +1 -1
- src/Advanced_Sidebar_Menu_List_Pages.php +392 -392
advanced-sidebar-menu.php
CHANGED
@@ -4,12 +4,12 @@ Plugin Name: Advanced Sidebar Menu
|
|
4 |
Plugin URI: https://matlipe.com/advanced-sidebar-menu/
|
5 |
Description: Creates dynamic menus based on parent/child relationship of your pages or categories.
|
6 |
Author: Mat Lipe
|
7 |
-
Version: 6.4.
|
8 |
Author URI: https://matlipe.com
|
9 |
Text Domain: advanced-sidebar-menu
|
10 |
*/
|
11 |
|
12 |
-
define( 'ADVANCED_SIDEBAR_BASIC_VERSION', '6.4.
|
13 |
define( 'ADVANCED_SIDEBAR_DIR', plugin_dir_path( __FILE__ ) );
|
14 |
|
15 |
if( !function_exists( 'advanced_sidebar_menu_load' ) ){
|
4 |
Plugin URI: https://matlipe.com/advanced-sidebar-menu/
|
5 |
Description: Creates dynamic menus based on parent/child relationship of your pages or categories.
|
6 |
Author: Mat Lipe
|
7 |
+
Version: 6.4.2
|
8 |
Author URI: https://matlipe.com
|
9 |
Text Domain: advanced-sidebar-menu
|
10 |
*/
|
11 |
|
12 |
+
define( 'ADVANCED_SIDEBAR_BASIC_VERSION', '6.4.2' );
|
13 |
define( 'ADVANCED_SIDEBAR_DIR', plugin_dir_path( __FILE__ ) );
|
14 |
|
15 |
if( !function_exists( 'advanced_sidebar_menu_load' ) ){
|
readme.txt
CHANGED
@@ -5,7 +5,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=paypa
|
|
5 |
Tags: menus, sidebar menu, hierarchy, category menu, pages menu
|
6 |
Requires at least: 4.6.0
|
7 |
Tested up to: 4.8.2
|
8 |
-
Stable tag: 6.4.
|
9 |
|
10 |
== Description ==
|
11 |
|
5 |
Tags: menus, sidebar menu, hierarchy, category menu, pages menu
|
6 |
Requires at least: 4.6.0
|
7 |
Tested up to: 4.8.2
|
8 |
+
Stable tag: 6.4.2
|
9 |
|
10 |
== Description ==
|
11 |
|
src/Advanced_Sidebar_Menu_List_Pages.php
CHANGED
@@ -1,393 +1,393 @@
|
|
1 |
-
<?php
|
2 |
-
|
3 |
-
/**
|
4 |
-
* Advanced_Sidebar_Menu_List_Pages
|
5 |
-
*
|
6 |
-
* Parse and build the child and grandchild menus
|
7 |
-
* Create the opening and closing <ul class="child-sidebar-menu">
|
8 |
-
* in the view and this will fill in the guts.
|
9 |
-
*
|
10 |
-
* Send the args ( similar to wp_list_pages ) to the constructor and then
|
11 |
-
* display by calling list_pages()
|
12 |
-
*
|
13 |
-
* @package Advanced Sidebar Menu
|
14 |
-
*
|
15 |
-
* @author Mat Lipe <mat@matlipe.com>
|
16 |
-
*
|
17 |
-
* @since 5.0.0
|
18 |
-
*
|
19 |
-
*/
|
20 |
-
class Advanced_Sidebar_Menu_List_Pages{
|
21 |
-
|
22 |
-
/**
|
23 |
-
* output
|
24 |
-
*
|
25 |
-
* The page list
|
26 |
-
*
|
27 |
-
* @var string
|
28 |
-
*/
|
29 |
-
public $output = '';
|
30 |
-
|
31 |
-
/**
|
32 |
-
* current_page
|
33 |
-
*
|
34 |
-
* Used when walking the list
|
35 |
-
*
|
36 |
-
* @var WP_Post
|
37 |
-
*/
|
38 |
-
protected $current_page;
|
39 |
-
|
40 |
-
/**
|
41 |
-
* current_page_id
|
42 |
-
*
|
43 |
-
* Holds id of current page. Separate from current_page because
|
44 |
-
* current_page could be empty if something custom going on
|
45 |
-
*
|
46 |
-
* @var int
|
47 |
-
*/
|
48 |
-
protected $current_page_id = 0;
|
49 |
-
|
50 |
-
/**
|
51 |
-
* top_parent_id
|
52 |
-
*
|
53 |
-
* Id of current page unless filtered when whatever set during
|
54 |
-
* widgetcreation
|
55 |
-
*
|
56 |
-
* @var int
|
57 |
-
*/
|
58 |
-
public $top_parent_id;
|
59 |
-
|
60 |
-
/**
|
61 |
-
* args
|
62 |
-
*
|
63 |
-
* Passed during construct given to walker and used for queries
|
64 |
-
*
|
65 |
-
* @var array
|
66 |
-
*/
|
67 |
-
private $args = array();
|
68 |
-
|
69 |
-
|
70 |
-
/**
|
71 |
-
* level
|
72 |
-
*
|
73 |
-
* Level of grandchild pages we are on
|
74 |
-
*
|
75 |
-
* @var int
|
76 |
-
*/
|
77 |
-
private $level = 0;
|
78 |
-
|
79 |
-
/**
|
80 |
-
* Used exclusively for caching
|
81 |
-
* Holds the value of the latest parent we
|
82 |
-
* retrieve children for
|
83 |
-
*
|
84 |
-
* @var int
|
85 |
-
*/
|
86 |
-
private $current_children_parent = 0;
|
87 |
-
|
88 |
-
/**
|
89 |
-
* menu
|
90 |
-
*
|
91 |
-
* @var \Advanced_Sidebar_Menu_Menu
|
92 |
-
*/
|
93 |
-
protected $menu;
|
94 |
-
|
95 |
-
|
96 |
-
/**
|
97 |
-
* Constructor
|
98 |
-
*
|
99 |
-
* Used in the view
|
100 |
-
*
|
101 |
-
* @param int $parent_id - $asm->top_id
|
102 |
-
* @param Advanced_Sidebar_Menu_Menu $asm
|
103 |
-
* @param WP_Post $current_page;
|
104 |
-
*/
|
105 |
-
public function __construct( $parent_id,
|
106 |
-
$this->menu = $asm;
|
107 |
-
$this->top_parent_id = $parent_id;
|
108 |
-
$this->current_page = $current_page;
|
109 |
-
if( null !== $current_page ){
|
110 |
-
$this->current_page_id = $current_page->ID;
|
111 |
-
}
|
112 |
-
|
113 |
-
$args = array(
|
114 |
-
'post_type' => $asm->post_type,
|
115 |
-
'orderby' => $asm->order_by,
|
116 |
-
'order' => $asm->order,
|
117 |
-
'exclude' => $asm->exclude,
|
118 |
-
'levels' => $asm->levels,
|
119 |
-
);
|
120 |
-
|
121 |
-
$this->parse_args( $args );
|
122 |
-
$this->hooks();
|
123 |
-
|
124 |
-
}
|
125 |
-
|
126 |
-
|
127 |
-
/**
|
128 |
-
* Hooks should only hook once
|
129 |
-
*
|
130 |
-
* @return void
|
131 |
-
*/
|
132 |
-
protected function hooks() {
|
133 |
-
static $been_hooked;
|
134 |
-
if( null === $been_hooked ){
|
135 |
-
$been_hooked = true;
|
136 |
-
add_filter( 'page_css_class', array( $this, 'add_list_item_classes' ), 2, 2 );
|
137 |
-
}
|
138 |
-
|
139 |
-
}
|
140 |
-
|
141 |
-
|
142 |
-
/**
|
143 |
-
* Add the custom classes to the list items
|
144 |
-
*
|
145 |
-
*
|
146 |
-
* @param array $classes
|
147 |
-
* @param \WP_Post $post
|
148 |
-
*
|
149 |
-
* @return array
|
150 |
-
*/
|
151 |
-
public function add_list_item_classes( $classes,
|
152 |
-
if( $post->ID === $this->top_parent_id ){
|
153 |
-
$children = $this->get_child_pages( $post->ID, true );
|
154 |
-
} else {
|
155 |
-
$children = $this->get_child_pages( $post->ID );
|
156 |
-
}
|
157 |
-
if( !empty( $children ) ){
|
158 |
-
$classes[] = 'has_children';
|
159 |
-
}
|
160 |
-
|
161 |
-
//below is only for custom post types
|
162 |
-
if( $this->current_page->post_type !== 'page' ){
|
163 |
-
if( isset( $post->ancestors ) && in_array( $this->current_page->ID, (array) $post->ancestors, true ) ){
|
164 |
-
$classes[] = 'current_page_ancestor';
|
165 |
-
} elseif( $this->current_page->ID === $post->post_parent ) {
|
166 |
-
$classes[] = 'current_page_parent';
|
167 |
-
}
|
168 |
-
}
|
169 |
-
|
170 |
-
return $classes;
|
171 |
-
}
|
172 |
-
|
173 |
-
|
174 |
-
/**
|
175 |
-
* Return the list of args that have been populated by this class
|
176 |
-
*
|
177 |
-
* @return array
|
178 |
-
*/
|
179 |
-
public function get_args(){
|
180 |
-
return $this->args;
|
181 |
-
}
|
182 |
-
|
183 |
-
|
184 |
-
/**
|
185 |
-
* __toString
|
186 |
-
*
|
187 |
-
* Magic method to allow using a simple echo to get output
|
188 |
-
*
|
189 |
-
* @return string
|
190 |
-
*/
|
191 |
-
public function __toString(){
|
192 |
-
return $this->output;
|
193 |
-
}
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
/**
|
198 |
-
*
|
199 |
-
* Do any adjustments to class args here
|
200 |
-
*
|
201 |
-
* @param array $args
|
202 |
-
*
|
203 |
-
* @return void
|
204 |
-
*/
|
205 |
-
private function parse_args( $args ){
|
206 |
-
$defaults = array(
|
207 |
-
'exclude' => '',
|
208 |
-
'echo' => 0,
|
209 |
-
'order' => 'ASC',
|
210 |
-
'orderby' => 'menu_order, post_title',
|
211 |
-
'walker' => new Advanced_Sidebar_Menu_Page_Walker(),
|
212 |
-
'link_before' => '',
|
213 |
-
'link_after' => '',
|
214 |
-
'title_li' => '',
|
215 |
-
'levels' => 100,
|
216 |
-
'item_spacing' => 'preserve',
|
217 |
-
'nopaging' => true,
|
218 |
-
);
|
219 |
-
|
220 |
-
$args = wp_parse_args( $args, $defaults );
|
221 |
-
|
222 |
-
// sanitize, mostly to keep spaces out
|
223 |
-
if( is_string( $args[ 'exclude' ] ) ){
|
224 |
-
$args[ 'exclude' ] = explode( ',', $args[ 'exclude' ] );
|
225 |
-
}
|
226 |
-
$args[ 'exclude' ] = preg_replace( '/[^0-9,]/', '', implode( ',', apply_filters( 'wp_list_pages_excludes', $args[ 'exclude' ] ) ) );
|
227 |
-
|
228 |
-
$this->args = apply_filters( 'advanced_sidebar_menu_list_pages_args', $args, $this );
|
229 |
-
|
230 |
-
}
|
231 |
-
|
232 |
-
|
233 |
-
/**
|
234 |
-
* list_pages
|
235 |
-
*
|
236 |
-
* List the pages very similar to wp_list_pages
|
237 |
-
*
|
238 |
-
* @return string
|
239 |
-
*/
|
240 |
-
public function list_pages() {
|
241 |
-
|
242 |
-
$pages = $this->get_child_pages( $this->top_parent_id, true );
|
243 |
-
|
244 |
-
foreach( $pages as $page ){
|
245 |
-
|
246 |
-
$this->output .= walk_page_tree( array( $page ), 1, $this->current_page_id, $this->args );
|
247 |
-
|
248 |
-
$this->output .= $this->list_grandchild_pages( $page->ID );
|
249 |
-
|
250 |
-
$this->output .= '</li>' . "\n";
|
251 |
-
|
252 |
-
}
|
253 |
-
|
254 |
-
$this->output = apply_filters( 'wp_list_pages', $this->output, $this->args );
|
255 |
-
|
256 |
-
if( !$this->args[ 'echo' ] ){
|
257 |
-
return $this->output;
|
258 |
-
}
|
259 |
-
|
260 |
-
echo $this->output;
|
261 |
-
}
|
262 |
-
|
263 |
-
|
264 |
-
/**
|
265 |
-
* list_grandchild_pages
|
266 |
-
*
|
267 |
-
* List as many levels as exist within the grandchild-sidebar-menu ul
|
268 |
-
*
|
269 |
-
* @param int $parent_page_id
|
270 |
-
*
|
271 |
-
* @return string
|
272 |
-
*/
|
273 |
-
private function list_grandchild_pages( $parent_page_id ){
|
274 |
-
if( !$this->current_page_ancestor( $parent_page_id ) ){
|
275 |
-
return '';
|
276 |
-
}
|
277 |
-
|
278 |
-
if( !$pages = $this->get_child_pages( $parent_page_id ) ){
|
279 |
-
return '';
|
280 |
-
}
|
281 |
-
|
282 |
-
if( $this->level === (int)$this->args[ 'levels' ] ){
|
283 |
-
return '';
|
284 |
-
}
|
285 |
-
|
286 |
-
$this->level++;
|
287 |
-
|
288 |
-
$content = sprintf( '<ul class="grandchild-sidebar-menu level-%s children">', $this->level );
|
289 |
-
$inside = '';
|
290 |
-
|
291 |
-
foreach( $pages as $page ){
|
292 |
-
$inside .= walk_page_tree( array( $page ), 1, $this->current_page_id, $this->args );
|
293 |
-
$inside .= $this->list_grandchild_pages( $page->ID );
|
294 |
-
$inside .= "</li>\n";
|
295 |
-
|
296 |
-
}
|
297 |
-
|
298 |
-
if( '' === $inside ){
|
299 |
-
return '';
|
300 |
-
}
|
301 |
-
|
302 |
-
|
303 |
-
return $content . $inside . "</ul>\n";
|
304 |
-
}
|
305 |
-
|
306 |
-
|
307 |
-
/**
|
308 |
-
* page_children
|
309 |
-
*
|
310 |
-
* Retrieve the child pages of specific page_id
|
311 |
-
*
|
312 |
-
* @param int $parent_page_id
|
313 |
-
* @param bool $is_first_level
|
314 |
-
*
|
315 |
-
* @return array
|
316 |
-
*/
|
317 |
-
public function get_child_pages( $parent_page_id, $is_first_level = false ) {
|
318 |
-
$this->current_children_parent = $parent_page_id;
|
319 |
-
|
320 |
-
$cache = Advanced_Sidebar_Menu_Cache::get_instance();
|
321 |
-
$child_pages = $cache->get_child_pages( $this );
|
322 |
-
if( $child_pages === false ){
|
323 |
-
$args = $this->args;
|
324 |
-
$args[ 'post_parent' ] = $parent_page_id;
|
325 |
-
$args[ 'fields' ] = 'ids';
|
326 |
-
|
327 |
-
$child_pages = get_posts( $args );
|
328 |
-
$cache->add_child_pages( $this, $child_pages );
|
329 |
-
}
|
330 |
-
|
331 |
-
$child_pages = array_map( 'get_post', (array)$child_pages );
|
332 |
-
|
333 |
-
//we only filter the first level with this filter for backward pro compatibility
|
334 |
-
if( $is_first_level ){
|
335 |
-
$child_pages = apply_filters( 'advanced_sidebar_menu_child_pages', $child_pages, $this->current_page, $this->menu->instance, $this->menu->args, $this->menu );
|
336 |
-
}
|
337 |
-
|
338 |
-
return $child_pages;
|
339 |
-
|
340 |
-
}
|
341 |
-
|
342 |
-
|
343 |
-
/**
|
344 |
-
* current_page_ancestor
|
345 |
-
*
|
346 |
-
* Is the current page and ancestor of the specified page?
|
347 |
-
*
|
348 |
-
* @param $page_id
|
349 |
-
*
|
350 |
-
* @return bool
|
351 |
-
*/
|
352 |
-
private function current_page_ancestor( $page_id ) {
|
353 |
-
$return = false;
|
354 |
-
if( !empty( $this->current_page_id ) ){
|
355 |
-
if( (int)$page_id === $this->current_page_id ){
|
356 |
-
$return = true;
|
357 |
-
} elseif( $this->current_page->post_parent === (int)$page_id ) {
|
358 |
-
$return = true;
|
359 |
-
} elseif( !empty( $this->current_page->ancestors ) && in_array( (int)$page_id, $this->current_page->ancestors, true ) ) {
|
360 |
-
$return = true;
|
361 |
-
}
|
362 |
-
}
|
363 |
-
|
364 |
-
$return = apply_filters(
|
365 |
-
'advanced_sidebar_menu_page_ancestor',
|
366 |
-
$return,
|
367 |
-
$this->current_page_id,
|
368 |
-
$this
|
369 |
-
);
|
370 |
-
|
371 |
-
return $return;
|
372 |
-
}
|
373 |
-
|
374 |
-
|
375 |
-
/**
|
376 |
-
*
|
377 |
-
* @param
|
378 |
-
* @param
|
379 |
-
*
|
380 |
-
* @static
|
381 |
-
*
|
382 |
-
* @return
|
383 |
-
*/
|
384 |
-
public static function factory( Advanced_Sidebar_Menu_Menu $menu, $current_page = null ){
|
385 |
-
if( null === $current_page ){
|
386 |
-
if ( is_page() || is_singular() ) {
|
387 |
-
$current_page = get_queried_object();
|
388 |
-
}
|
389 |
-
}
|
390 |
-
return new self( $menu->top_id, $menu, $current_page );
|
391 |
-
}
|
392 |
-
|
393 |
}
|
1 |
+
<?php
|
2 |
+
|
3 |
+
/**
|
4 |
+
* Advanced_Sidebar_Menu_List_Pages
|
5 |
+
*
|
6 |
+
* Parse and build the child and grandchild menus
|
7 |
+
* Create the opening and closing <ul class="child-sidebar-menu">
|
8 |
+
* in the view and this will fill in the guts.
|
9 |
+
*
|
10 |
+
* Send the args ( similar to wp_list_pages ) to the constructor and then
|
11 |
+
* display by calling list_pages()
|
12 |
+
*
|
13 |
+
* @package Advanced Sidebar Menu
|
14 |
+
*
|
15 |
+
* @author Mat Lipe <mat@matlipe.com>
|
16 |
+
*
|
17 |
+
* @since 5.0.0
|
18 |
+
*
|
19 |
+
*/
|
20 |
+
class Advanced_Sidebar_Menu_List_Pages{
|
21 |
+
|
22 |
+
/**
|
23 |
+
* output
|
24 |
+
*
|
25 |
+
* The page list
|
26 |
+
*
|
27 |
+
* @var string
|
28 |
+
*/
|
29 |
+
public $output = '';
|
30 |
+
|
31 |
+
/**
|
32 |
+
* current_page
|
33 |
+
*
|
34 |
+
* Used when walking the list
|
35 |
+
*
|
36 |
+
* @var WP_Post
|
37 |
+
*/
|
38 |
+
protected $current_page;
|
39 |
+
|
40 |
+
/**
|
41 |
+
* current_page_id
|
42 |
+
*
|
43 |
+
* Holds id of current page. Separate from current_page because
|
44 |
+
* current_page could be empty if something custom going on
|
45 |
+
*
|
46 |
+
* @var int
|
47 |
+
*/
|
48 |
+
protected $current_page_id = 0;
|
49 |
+
|
50 |
+
/**
|
51 |
+
* top_parent_id
|
52 |
+
*
|
53 |
+
* Id of current page unless filtered when whatever set during
|
54 |
+
* widgetcreation
|
55 |
+
*
|
56 |
+
* @var int
|
57 |
+
*/
|
58 |
+
public $top_parent_id;
|
59 |
+
|
60 |
+
/**
|
61 |
+
* args
|
62 |
+
*
|
63 |
+
* Passed during construct given to walker and used for queries
|
64 |
+
*
|
65 |
+
* @var array
|
66 |
+
*/
|
67 |
+
private $args = array();
|
68 |
+
|
69 |
+
|
70 |
+
/**
|
71 |
+
* level
|
72 |
+
*
|
73 |
+
* Level of grandchild pages we are on
|
74 |
+
*
|
75 |
+
* @var int
|
76 |
+
*/
|
77 |
+
private $level = 0;
|
78 |
+
|
79 |
+
/**
|
80 |
+
* Used exclusively for caching
|
81 |
+
* Holds the value of the latest parent we
|
82 |
+
* retrieve children for
|
83 |
+
*
|
84 |
+
* @var int
|
85 |
+
*/
|
86 |
+
private $current_children_parent = 0;
|
87 |
+
|
88 |
+
/**
|
89 |
+
* menu
|
90 |
+
*
|
91 |
+
* @var \Advanced_Sidebar_Menu_Menu
|
92 |
+
*/
|
93 |
+
protected $menu;
|
94 |
+
|
95 |
+
|
96 |
+
/**
|
97 |
+
* Constructor
|
98 |
+
*
|
99 |
+
* Used in the view
|
100 |
+
*
|
101 |
+
* @param int $parent_id - $asm->top_id
|
102 |
+
* @param Advanced_Sidebar_Menu_Menu $asm
|
103 |
+
* @param WP_Post $current_page;
|
104 |
+
*/
|
105 |
+
public function __construct( $parent_id, Advanced_Sidebar_Menu_Menu $asm, $current_page ){
|
106 |
+
$this->menu = $asm;
|
107 |
+
$this->top_parent_id = $parent_id;
|
108 |
+
$this->current_page = $current_page;
|
109 |
+
if( null !== $current_page ){
|
110 |
+
$this->current_page_id = $current_page->ID;
|
111 |
+
}
|
112 |
+
|
113 |
+
$args = array(
|
114 |
+
'post_type' => $asm->post_type,
|
115 |
+
'orderby' => $asm->order_by,
|
116 |
+
'order' => $asm->order,
|
117 |
+
'exclude' => $asm->exclude,
|
118 |
+
'levels' => $asm->levels,
|
119 |
+
);
|
120 |
+
|
121 |
+
$this->parse_args( $args );
|
122 |
+
$this->hooks();
|
123 |
+
|
124 |
+
}
|
125 |
+
|
126 |
+
|
127 |
+
/**
|
128 |
+
* Hooks should only hook once
|
129 |
+
*
|
130 |
+
* @return void
|
131 |
+
*/
|
132 |
+
protected function hooks() {
|
133 |
+
static $been_hooked;
|
134 |
+
if( null === $been_hooked ){
|
135 |
+
$been_hooked = true;
|
136 |
+
add_filter( 'page_css_class', array( $this, 'add_list_item_classes' ), 2, 2 );
|
137 |
+
}
|
138 |
+
|
139 |
+
}
|
140 |
+
|
141 |
+
|
142 |
+
/**
|
143 |
+
* Add the custom classes to the list items
|
144 |
+
*
|
145 |
+
*
|
146 |
+
* @param array $classes
|
147 |
+
* @param \WP_Post $post
|
148 |
+
*
|
149 |
+
* @return array
|
150 |
+
*/
|
151 |
+
public function add_list_item_classes( $classes, WP_Post $post ) {
|
152 |
+
if( $post->ID === $this->top_parent_id ){
|
153 |
+
$children = $this->get_child_pages( $post->ID, true );
|
154 |
+
} else {
|
155 |
+
$children = $this->get_child_pages( $post->ID );
|
156 |
+
}
|
157 |
+
if( !empty( $children ) ){
|
158 |
+
$classes[] = 'has_children';
|
159 |
+
}
|
160 |
+
|
161 |
+
//below is only for custom post types
|
162 |
+
if( $this->current_page->post_type !== 'page' ){
|
163 |
+
if( isset( $post->ancestors ) && in_array( $this->current_page->ID, (array) $post->ancestors, true ) ){
|
164 |
+
$classes[] = 'current_page_ancestor';
|
165 |
+
} elseif( $this->current_page->ID === $post->post_parent ) {
|
166 |
+
$classes[] = 'current_page_parent';
|
167 |
+
}
|
168 |
+
}
|
169 |
+
|
170 |
+
return $classes;
|
171 |
+
}
|
172 |
+
|
173 |
+
|
174 |
+
/**
|
175 |
+
* Return the list of args that have been populated by this class
|
176 |
+
*
|
177 |
+
* @return array
|
178 |
+
*/
|
179 |
+
public function get_args(){
|
180 |
+
return $this->args;
|
181 |
+
}
|
182 |
+
|
183 |
+
|
184 |
+
/**
|
185 |
+
* __toString
|
186 |
+
*
|
187 |
+
* Magic method to allow using a simple echo to get output
|
188 |
+
*
|
189 |
+
* @return string
|
190 |
+
*/
|
191 |
+
public function __toString(){
|
192 |
+
return $this->output;
|
193 |
+
}
|
194 |
+
|
195 |
+
|
196 |
+
|
197 |
+
/**
|
198 |
+
*
|
199 |
+
* Do any adjustments to class args here
|
200 |
+
*
|
201 |
+
* @param array $args
|
202 |
+
*
|
203 |
+
* @return void
|
204 |
+
*/
|
205 |
+
private function parse_args( $args ){
|
206 |
+
$defaults = array(
|
207 |
+
'exclude' => '',
|
208 |
+
'echo' => 0,
|
209 |
+
'order' => 'ASC',
|
210 |
+
'orderby' => 'menu_order, post_title',
|
211 |
+
'walker' => new Advanced_Sidebar_Menu_Page_Walker(),
|
212 |
+
'link_before' => '',
|
213 |
+
'link_after' => '',
|
214 |
+
'title_li' => '',
|
215 |
+
'levels' => 100,
|
216 |
+
'item_spacing' => 'preserve',
|
217 |
+
'nopaging' => true,
|
218 |
+
);
|
219 |
+
|
220 |
+
$args = wp_parse_args( $args, $defaults );
|
221 |
+
|
222 |
+
// sanitize, mostly to keep spaces out
|
223 |
+
if( is_string( $args[ 'exclude' ] ) ){
|
224 |
+
$args[ 'exclude' ] = explode( ',', $args[ 'exclude' ] );
|
225 |
+
}
|
226 |
+
$args[ 'exclude' ] = preg_replace( '/[^0-9,]/', '', implode( ',', apply_filters( 'wp_list_pages_excludes', $args[ 'exclude' ] ) ) );
|
227 |
+
|
228 |
+
$this->args = apply_filters( 'advanced_sidebar_menu_list_pages_args', $args, $this );
|
229 |
+
|
230 |
+
}
|
231 |
+
|
232 |
+
|
233 |
+
/**
|
234 |
+
* list_pages
|
235 |
+
*
|
236 |
+
* List the pages very similar to wp_list_pages
|
237 |
+
*
|
238 |
+
* @return string
|
239 |
+
*/
|
240 |
+
public function list_pages() {
|
241 |
+
|
242 |
+
$pages = $this->get_child_pages( $this->top_parent_id, true );
|
243 |
+
|
244 |
+
foreach( $pages as $page ){
|
245 |
+
|
246 |
+
$this->output .= walk_page_tree( array( $page ), 1, $this->current_page_id, $this->args );
|
247 |
+
|
248 |
+
$this->output .= $this->list_grandchild_pages( $page->ID );
|
249 |
+
|
250 |
+
$this->output .= '</li>' . "\n";
|
251 |
+
|
252 |
+
}
|
253 |
+
|
254 |
+
$this->output = apply_filters( 'wp_list_pages', $this->output, $this->args );
|
255 |
+
|
256 |
+
if( !$this->args[ 'echo' ] ){
|
257 |
+
return $this->output;
|
258 |
+
}
|
259 |
+
|
260 |
+
echo $this->output;
|
261 |
+
}
|
262 |
+
|
263 |
+
|
264 |
+
/**
|
265 |
+
* list_grandchild_pages
|
266 |
+
*
|
267 |
+
* List as many levels as exist within the grandchild-sidebar-menu ul
|
268 |
+
*
|
269 |
+
* @param int $parent_page_id
|
270 |
+
*
|
271 |
+
* @return string
|
272 |
+
*/
|
273 |
+
private function list_grandchild_pages( $parent_page_id ){
|
274 |
+
if( !$this->current_page_ancestor( $parent_page_id ) ){
|
275 |
+
return '';
|
276 |
+
}
|
277 |
+
|
278 |
+
if( !$pages = $this->get_child_pages( $parent_page_id ) ){
|
279 |
+
return '';
|
280 |
+
}
|
281 |
+
|
282 |
+
if( $this->level === (int)$this->args[ 'levels' ] ){
|
283 |
+
return '';
|
284 |
+
}
|
285 |
+
|
286 |
+
$this->level++;
|
287 |
+
|
288 |
+
$content = sprintf( '<ul class="grandchild-sidebar-menu level-%s children">', $this->level );
|
289 |
+
$inside = '';
|
290 |
+
|
291 |
+
foreach( $pages as $page ){
|
292 |
+
$inside .= walk_page_tree( array( $page ), 1, $this->current_page_id, $this->args );
|
293 |
+
$inside .= $this->list_grandchild_pages( $page->ID );
|
294 |
+
$inside .= "</li>\n";
|
295 |
+
|
296 |
+
}
|
297 |
+
|
298 |
+
if( '' === $inside ){
|
299 |
+
return '';
|
300 |
+
}
|
301 |
+
|
302 |
+
|
303 |
+
return $content . $inside . "</ul>\n";
|
304 |
+
}
|
305 |
+
|
306 |
+
|
307 |
+
/**
|
308 |
+
* page_children
|
309 |
+
*
|
310 |
+
* Retrieve the child pages of specific page_id
|
311 |
+
*
|
312 |
+
* @param int $parent_page_id
|
313 |
+
* @param bool $is_first_level
|
314 |
+
*
|
315 |
+
* @return array
|
316 |
+
*/
|
317 |
+
public function get_child_pages( $parent_page_id, $is_first_level = false ) {
|
318 |
+
$this->current_children_parent = $parent_page_id;
|
319 |
+
|
320 |
+
$cache = Advanced_Sidebar_Menu_Cache::get_instance();
|
321 |
+
$child_pages = $cache->get_child_pages( $this );
|
322 |
+
if( $child_pages === false ){
|
323 |
+
$args = $this->args;
|
324 |
+
$args[ 'post_parent' ] = $parent_page_id;
|
325 |
+
$args[ 'fields' ] = 'ids';
|
326 |
+
|
327 |
+
$child_pages = get_posts( $args );
|
328 |
+
$cache->add_child_pages( $this, $child_pages );
|
329 |
+
}
|
330 |
+
|
331 |
+
$child_pages = array_map( 'get_post', (array)$child_pages );
|
332 |
+
|
333 |
+
//we only filter the first level with this filter for backward pro compatibility
|
334 |
+
if( $is_first_level ){
|
335 |
+
$child_pages = apply_filters( 'advanced_sidebar_menu_child_pages', $child_pages, $this->current_page, $this->menu->instance, $this->menu->args, $this->menu );
|
336 |
+
}
|
337 |
+
|
338 |
+
return $child_pages;
|
339 |
+
|
340 |
+
}
|
341 |
+
|
342 |
+
|
343 |
+
/**
|
344 |
+
* current_page_ancestor
|
345 |
+
*
|
346 |
+
* Is the current page and ancestor of the specified page?
|
347 |
+
*
|
348 |
+
* @param $page_id
|
349 |
+
*
|
350 |
+
* @return bool
|
351 |
+
*/
|
352 |
+
private function current_page_ancestor( $page_id ) {
|
353 |
+
$return = false;
|
354 |
+
if( !empty( $this->current_page_id ) ){
|
355 |
+
if( (int)$page_id === $this->current_page_id ){
|
356 |
+
$return = true;
|
357 |
+
} elseif( $this->current_page->post_parent === (int)$page_id ) {
|
358 |
+
$return = true;
|
359 |
+
} elseif( !empty( $this->current_page->ancestors ) && in_array( (int)$page_id, $this->current_page->ancestors, true ) ) {
|
360 |
+
$return = true;
|
361 |
+
}
|
362 |
+
}
|
363 |
+
|
364 |
+
$return = apply_filters(
|
365 |
+
'advanced_sidebar_menu_page_ancestor',
|
366 |
+
$return,
|
367 |
+
$this->current_page_id,
|
368 |
+
$this
|
369 |
+
);
|
370 |
+
|
371 |
+
return $return;
|
372 |
+
}
|
373 |
+
|
374 |
+
|
375 |
+
/**
|
376 |
+
*
|
377 |
+
* @param Advanced_Sidebar_Menu_Menu $menu
|
378 |
+
* @param WP_Post|null $current_page;
|
379 |
+
*
|
380 |
+
* @static
|
381 |
+
*
|
382 |
+
* @return Advanced_Sidebar_Menu_List_Pages
|
383 |
+
*/
|
384 |
+
public static function factory( Advanced_Sidebar_Menu_Menu $menu, $current_page = null ){
|
385 |
+
if( null === $current_page ){
|
386 |
+
if ( is_page() || is_singular() ) {
|
387 |
+
$current_page = get_queried_object();
|
388 |
+
}
|
389 |
+
}
|
390 |
+
return new self( $menu->top_id, $menu, $current_page );
|
391 |
+
}
|
392 |
+
|
393 |
}
|