Advanced Sidebar Menu - Version 5.0.0

Version Description

  • Greatly improved performance
  • Improved code structure
Download this release

Release Info

Developer Mat Lipe
Plugin Icon 128x128 Advanced Sidebar Menu
Version 5.0.0
Comparing to
See all releases

Code changes from version 4.7.6 to 5.0.0

advanced-sidebar-menu.php CHANGED
@@ -4,11 +4,11 @@ Plugin Name: Advanced Sidebar Menu
4
  Plugin URI: http://matlipe.com/advanced-sidebar-menu/
5
  Description: Creates dynamic menu based on child/parent relationship.
6
  Author: Mat Lipe
7
- Version: 4.7.6
8
  Author URI: http://matlipe.com
9
  */
10
 
11
- define( 'ADVANCED_SIDEBAR_BASIC_VERSION', '4.7.6' );
12
 
13
 
14
  #-- Define Constants
@@ -21,7 +21,10 @@ define( 'ADVANCED_SIDEBAR_LEGACY_DIR', ADVANCED_SIDEBAR_DIR . 'legacy/' );
21
  #-- Bring in the Widgets
22
  require( ADVANCED_SIDEBAR_WIDGETS_DIR.'init.php' );
23
  #-- Bring in the functions
24
- require( ADVANCED_SIDEBAR_DIR.'lib/advancedSidebarMenu.php' );
 
 
 
25
  $asm = new advancedSidebarMenu();
26
 
27
  #-- Translate
4
  Plugin URI: http://matlipe.com/advanced-sidebar-menu/
5
  Description: Creates dynamic menu based on child/parent relationship.
6
  Author: Mat Lipe
7
+ Version: 5.0.0
8
  Author URI: http://matlipe.com
9
  */
10
 
11
+ define( 'ADVANCED_SIDEBAR_BASIC_VERSION', '5.0.0' );
12
 
13
 
14
  #-- Define Constants
21
  #-- Bring in the Widgets
22
  require( ADVANCED_SIDEBAR_WIDGETS_DIR.'init.php' );
23
  #-- Bring in the functions
24
+ require( ADVANCED_SIDEBAR_DIR.'classes/Advanced_Sidebar_Menu_Deprecated.php' );
25
+ require( ADVANCED_SIDEBAR_DIR.'classes/advancedSidebarMenu.php' );
26
+ require( ADVANCED_SIDEBAR_DIR.'classes/Advanced_Sidebar_Menu_Page_Walker.php' );
27
+ require( ADVANCED_SIDEBAR_DIR.'classes/Advanced_Sidebar_Menu_List_Pages.php' );
28
  $asm = new advancedSidebarMenu();
29
 
30
  #-- Translate
classes/Advanced_Sidebar_Menu_Deprecated.php ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Advanced_Sidebar_Menu_Deprecated
5
+ *
6
+ * For backward compatibility on template overrides only
7
+ *
8
+ * @see
9
+ */
10
+ class Advanced_Sidebar_Menu_Deprecated {
11
+
12
+
13
+ /**
14
+ * @deprecated 5.0.0
15
+ */
16
+ function page_children( $pID ) {
17
+ global $wpdb, $table_prefix;
18
+
19
+ return $wpdb->get_results( "SELECT ID FROM " . $table_prefix . "posts WHERE post_parent = " . $pID . " AND post_status='publish' ORDER By " . $this->order_by );
20
+
21
+ }
22
+
23
+
24
+ /**
25
+ * @deprecated 5.0.0
26
+ */
27
+ function page_ancestor( $pID ) {
28
+ global $post;
29
+
30
+ if( $pID->ID == $post->ID or $pID->ID == $post->post_parent or @in_array( $pID->ID, $post->ancestors ) ){
31
+ $return = true;
32
+ } else {
33
+ $return = false;
34
+ }
35
+
36
+ return apply_filters( 'advanced_sidebar_menu_page_ancestor', $return, $pID, $this );
37
+ }
38
+
39
+
40
+
41
+ /**
42
+ * @deprecated 5.0.0
43
+ */
44
+ function grandChildLegacyMode( $pID ) {
45
+ #-- if the link that was just listed is the current page we are on
46
+ if( !$this->page_ancestor( $pID ) ){
47
+ return;
48
+ }
49
+
50
+ //Get the children of this page
51
+ $grandkids = $this->page_children( $pID->ID );
52
+ if( $grandkids ){
53
+ #-- Create a new menu with all the children under it
54
+ $content .= '<ul class="grandchild-sidebar-menu">';
55
+ $content .= wp_list_pages( "post_type=" . $this->post_type . "&sort_column=$this->order_by&title_li=&echo=0&exclude=" . $this->instance[ 'exclude' ] . "&child_of=" . $pID->ID );
56
+
57
+ $content .= '</ul>';
58
+ }
59
+
60
+ return $content;
61
+ }
62
+
63
+
64
+ /**
65
+ * @deprecated 5.0.0
66
+ */
67
+ function displayGrandChildMenu( $page ) {
68
+ static $count = 0;
69
+ $count++;
70
+
71
+ //If the page sent is not a child of the current page
72
+ if( !$this->page_ancestor( $page ) ){
73
+ return;
74
+ }
75
+
76
+ //if there are no children of the current page bail
77
+ if( !$children = $this->page_children( $page->ID ) ){
78
+ return;
79
+ }
80
+
81
+
82
+ $content = sprintf( '<ul class="grandchild-sidebar-menu level-%s children">', $count );
83
+ foreach( $children as $child ){
84
+
85
+ //If this page should be excluded bail
86
+ if( in_array( $child->ID, $this->exclude ) ){
87
+ continue;
88
+ }
89
+
90
+ $args = array(
91
+ 'post_type' => $this->post_type,
92
+ 'sort_column' => $this->order_by,
93
+ 'title_li' => '',
94
+ 'echo' => 0,
95
+ 'depth' => 1,
96
+ 'include' => $child->ID
97
+ );
98
+
99
+ $content .= $this->openListItem( wp_list_pages( $args ) );
100
+
101
+ //If this newly outputed child is a direct parent of the current page
102
+ if( $this->page_ancestor( $child ) ){
103
+ $content .= $this->displayGrandChildMenu( $child );
104
+ }
105
+
106
+ $content .= '</li>';
107
+
108
+ }
109
+ $content .= '</ul>';
110
+
111
+ return $content;
112
+
113
+ }
114
+ }
classes/Advanced_Sidebar_Menu_List_Pages.php ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ private $current_page = NULL;
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
+ private $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
+ private $top_parent_id = 0;
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
+ /**
81
+ * Constructor
82
+ *
83
+ * Used in the view
84
+ *
85
+ * @param array $child_pages - array if page_ids
86
+ * @param array $args - see $this->fill_class_vars
87
+ */
88
+ public function __construct( $parent_id, $args ){
89
+
90
+ $this->top_parent_id = $parent_id;
91
+ $this->fill_class_vars( $args );
92
+
93
+ }
94
+
95
+
96
+ /**
97
+ * __toString
98
+ *
99
+ * Magic method to allow using a simple echo to get output
100
+ *
101
+ * @return string
102
+ */
103
+ public function __toString(){
104
+ return $this->output;
105
+ }
106
+
107
+
108
+
109
+ /**
110
+ * fill_class_vars
111
+ *
112
+ * Do any adjustments to class vars here
113
+ *
114
+ * @param $args
115
+ *
116
+ * @return void
117
+ */
118
+ private function fill_class_vars( $args ){
119
+ global $wp_query;
120
+
121
+ $defaults = array(
122
+ 'depth' => 1,
123
+ 'exclude' => '',
124
+ 'echo' => 0,
125
+ 'sort_column' => 'menu_order, post_title',
126
+ 'walker' => new Advanced_Sidebar_Menu_Page_Walker(),
127
+ 'hierarchical' => 0,
128
+ 'link_before' => '',
129
+ 'link_after' => '',
130
+ 'title_li' => ''
131
+ );
132
+
133
+ $args = wp_parse_args( $args, $defaults );
134
+
135
+ // sanitize, mostly to keep spaces out
136
+ if( is_string( $args[ 'exclude' ] ) ){
137
+ $args[ 'exclude' ] = explode( ',', $args[ 'exclude' ] );
138
+ }
139
+ $args[ 'exclude' ] = preg_replace( '/[^0-9,]/', '', implode( ',', apply_filters( 'wp_list_pages_excludes', $args[ 'exclude' ] ) ) );
140
+
141
+ $this->args = $args;
142
+
143
+ if ( is_page() || is_singular() ) {
144
+ $this->current_page = get_queried_object();
145
+ $this->current_page_id = $this->current_page->ID;
146
+ }
147
+
148
+ }
149
+
150
+
151
+ /**
152
+ * list_pages
153
+ *
154
+ * List the pages very similar to wp_list_pages
155
+ *
156
+ * @return string|void
157
+ */
158
+ public function list_pages() {
159
+
160
+ $pages = $this->get_child_pages( $this->top_parent_id );
161
+
162
+ foreach( $pages as $page ){
163
+
164
+ $this->output .= walk_page_tree( array( $page ), 1, $this->current_page_id, $this->args );
165
+
166
+ $this->output .= $this->list_grandchild_pages( $page->ID );
167
+
168
+ $this->output .= "</li>\n";
169
+
170
+ }
171
+
172
+ $this->output = apply_filters( 'wp_list_pages', $this->output, $this->args );
173
+
174
+ if( $this->args[ 'echo' ] ){
175
+ echo $this->output;
176
+ } else {
177
+ return $this->output;
178
+ }
179
+ }
180
+
181
+
182
+ /**
183
+ * list_grandchild_pages
184
+ *
185
+ * List as many levels as exist within the grandchild-sidebar-menu ul
186
+ *
187
+ * @param int $parent_page_id
188
+ *
189
+ * @return string
190
+ */
191
+ private function list_grandchild_pages( $parent_page_id ){
192
+ $content = '';
193
+
194
+ if( !$this->current_page_ancestor( $parent_page_id ) ){
195
+ return '';
196
+ }
197
+
198
+ if( !$pages = $this->get_child_pages( $parent_page_id ) ){
199
+ return '';
200
+ }
201
+
202
+ foreach( $pages as $page ){
203
+ $content .= walk_page_tree( array( $page ), 1, $this->current_page_id, $this->args );
204
+
205
+ $content .= $this->list_grandchild_pages( $page->ID );
206
+
207
+ $content .= "</li>\n";
208
+
209
+ }
210
+
211
+ if( '' == $content ){
212
+ return $content;
213
+ }
214
+
215
+ $this->level++;
216
+
217
+ return sprintf( '<ul class="grandchild-sidebar-menu level-%s children">', $this->level ) . $content . "</ul>\n";
218
+ }
219
+
220
+
221
+ /**
222
+ * page_children
223
+ *
224
+ * Retrieve the child pages of specific page_id
225
+ *
226
+ * @param $parent_page_id
227
+ *
228
+ * @return array
229
+ */
230
+ public function get_child_pages( $parent_page_id ) {
231
+ $args = $this->args;
232
+ $args[ 'parent' ] = $parent_page_id;
233
+
234
+ return get_pages( $args );
235
+
236
+ }
237
+
238
+
239
+ /**
240
+ * current_page_ancestor
241
+ *
242
+ * Is the current page and ancestor of the specified page?
243
+ *
244
+ * @param $page_id
245
+ *
246
+ * @return bool
247
+ */
248
+ private function current_page_ancestor( $page_id ) {
249
+ $return = false;
250
+
251
+ if( !empty( $this->current_page_id ) ){
252
+ if( $page_id == $this->current_page_id ){
253
+ $return = true;
254
+ } elseif( $this->current_page->post_parent == $page_id ) {
255
+ $return = true;
256
+ } else {
257
+ if( !empty( $this->current_page->ancestors ) ){
258
+ if( in_array( $page_id, $this->current_page->ancestors ) ){
259
+ $return = true;
260
+ }
261
+ }
262
+ }
263
+ }
264
+
265
+ $return = apply_filters(
266
+ 'advanced_sidebar_menu_page_ancestor',
267
+ $return,
268
+ $this->current_page_id,
269
+ $this
270
+ );
271
+
272
+ return $return;
273
+ }
274
+
275
+
276
+ }
classes/Advanced_Sidebar_Menu_Page_Walker.php ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+
4
+ class Advanced_Sidebar_Menu_Page_Walker extends Walker_Page{
5
+
6
+ function end_el( &$output, $page, $depth = 0, $args = array() ){
7
+ /** Do Nothing */
8
+ }
9
+
10
+ }
classes/advancedSidebarMenu.php ADDED
@@ -0,0 +1,317 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * advancedSidebarMenu
4
+ *
5
+ * These Functions are Specific to the Advanced Sidebar Menu
6
+ *
7
+ * @author Mat Lipe <mat@matlipe.com>
8
+ *
9
+ * @package Advanced Sidebar Menu
10
+ */
11
+ class advancedSidebarMenu extends Advanced_Sidebar_Menu_Deprecated {
12
+
13
+ var $instance; //The widget instance
14
+ var $top_id; //Either the top cat or page
15
+ var $exclude;
16
+ var $ancestors; //For the category ancestors
17
+ var $count = 1; //Count for grandchild levels
18
+ var $order_by;
19
+ var $taxonomy; //For filters to override the taxonomy
20
+ var $current_term; //Current category or taxonomy
21
+
22
+
23
+ /**
24
+ * Check is a page has children by id
25
+ *
26
+ * @since 8.29.13
27
+ *
28
+ * @param int $postId
29
+ */
30
+ function hasChildren( $postId ) {
31
+ if( $this->post_type == 'page' ){
32
+ $children = get_pages( "child_of=$postId" );
33
+ } else {
34
+ $children = get_posts( array(
35
+ 'post_type' => $this->post_type,
36
+ 'post_parent' => $postId
37
+ ) );
38
+ }
39
+ if( count( $children ) != 0 ){
40
+ return true;
41
+ } else {
42
+ return false;
43
+ }
44
+
45
+ }
46
+
47
+
48
+ /**
49
+ * Checks if a widgets checkbox is checked.
50
+ * * this one is special and does a double check
51
+ *
52
+ * @since 4.1.3
53
+ *
54
+ * @param string $name - name of checkbox
55
+ */
56
+ function checked( $name ) {
57
+ if( isset( $this->instance[ $name ] ) && $this->instance[ $name ] == 'checked' ){
58
+ return true;
59
+ }
60
+
61
+ return false;
62
+
63
+ }
64
+
65
+
66
+ /**
67
+ * Used by a uasort to sort taxonomy arrays by term order
68
+ *
69
+ * @since 4.3.0
70
+ */
71
+ function sortTerms( $a, $b ) {
72
+ if( !isset( $a->{$this->order_by} ) || !isset( $b->{$this->order_by} ) ){
73
+ return 0;
74
+ }
75
+
76
+ return $a->{$this->order_by} > $b->{$this->order_by};
77
+
78
+ }
79
+
80
+
81
+ /**
82
+ * Retrieves the Highest level Category Id
83
+ *
84
+ * @since 6.6.13
85
+ *
86
+ * @param int $catId - id of cat looking for top parent of
87
+ *
88
+ * @return int
89
+ */
90
+ function getTopCat( $catId ) {
91
+ $cat_ancestors = array();
92
+ $cat_ancestors[ ] = $catId;
93
+
94
+ do {
95
+ $catId = get_term( $catId, $this->taxonomy );
96
+ $catId = $catId->parent;
97
+ $cat_ancestors[ ] = $catId;
98
+ } while( $catId );
99
+
100
+ //Reverse the array to start at the last
101
+ $this->ancestors = array_reverse( $cat_ancestors );
102
+
103
+ //forget the [0] because the parent of top parent is always 0
104
+ return $this->ancestors[ 1 ];
105
+
106
+ }
107
+
108
+
109
+ /**
110
+ * Removes the closing </li> tag from a list item to allow for child menus inside of it
111
+ *
112
+ * @param string $item - an <li></li> item
113
+ *
114
+ * @return string|bool
115
+ * @since 4.7.13
116
+ */
117
+ function openListItem( $item = false ) {
118
+ if( !$item ){
119
+ return false;
120
+ }
121
+
122
+ return substr( trim( $item ), 0, -5 );
123
+ }
124
+
125
+
126
+
127
+
128
+
129
+ /**
130
+ * Adds the class for any menu item with children
131
+ *
132
+ * @param array $css the currrent css classes
133
+ * @param obj $page the page being checked
134
+ *
135
+ *
136
+ * @since 8.29.13
137
+ *
138
+ * @return array
139
+ */
140
+ function hasChildrenClass( $css, $page ) {
141
+ if( $this->hasChildren( $page->ID ) ){
142
+ $css[ ] = 'has_children';
143
+ }
144
+
145
+ return $css;
146
+
147
+ }
148
+
149
+
150
+ /**
151
+ * Adds the class for current page item etc to the page list when using a custom post type
152
+ *
153
+ * @param array $css the currrent css classes
154
+ * @param obj $this_menu_item the page being checked
155
+ *
156
+ * @return array
157
+ * @since 10.10.12
158
+ */
159
+ function custom_post_type_css( $css, $this_menu_item ) {
160
+ global $post;
161
+ if( isset( $post->ancestors ) && in_array( $this_menu_item->ID, (array)$post->ancestors ) ){
162
+ $css[ ] = 'current_page_ancestor';
163
+ }
164
+ if( $this_menu_item->ID == $post->ID ){
165
+ $css[ ] = 'current_page_item';
166
+
167
+ } elseif( $this_menu_item->ID == $post->post_parent ) {
168
+ $css[ ] = 'current_page_parent';
169
+ }
170
+
171
+ return $css;
172
+ }
173
+
174
+
175
+ /**
176
+ *
177
+ * IF this is a top level category
178
+ *
179
+ * @param obj $cat the cat object
180
+ *
181
+ * @since 6.13.13
182
+ */
183
+ function first_level_category( $cat ) {
184
+
185
+ if( !in_array( $cat->term_id, $this->exclude ) && $cat->parent == $this->top_id ){
186
+ $return = true;
187
+ } else {
188
+ $return = false;
189
+ }
190
+
191
+ return apply_filters( 'advanced_sidebar_menu_first_level_category', $return, $cat, $this );
192
+
193
+ }
194
+
195
+
196
+ /**
197
+ * If the cat is a second level cat
198
+ *
199
+ * @param obj $cat the cat
200
+ *
201
+ * @since 6.13.13
202
+ */
203
+ function second_level_cat( $cat ) {
204
+
205
+ //if this is the currrent cat or a parent of the current cat
206
+ if( $cat->term_id == $this->current_term || in_array( $cat->term_id, $this->ancestors ) ){
207
+
208
+ $all_children = array();
209
+ $all_children = get_terms( $this->taxonomy, array( 'child_of' => $cat->term_id, 'fields' => 'ids' ) );
210
+ if( !empty( $all_children ) ){
211
+ $return = true;
212
+ } else {
213
+ $return = false;
214
+ }
215
+
216
+ } else {
217
+ $return = false;
218
+ }
219
+
220
+ return apply_filters( 'advanced_sidebar_menu_second_level_category', $return, $cat, $this );
221
+
222
+
223
+ }
224
+
225
+
226
+ /**
227
+ * Determines if all the children should be included
228
+ *
229
+ * @since 6.26.13
230
+ * @return bool
231
+ */
232
+ function display_all() {
233
+
234
+ return $this->checked( 'display_all' );
235
+ }
236
+
237
+
238
+ /**
239
+ * Determines if the parent page or cat should be included
240
+ *
241
+ * @since 6.26.13
242
+ * @return bool
243
+ */
244
+ function include_parent() {
245
+
246
+ if( !$this->checked( 'include_parent' ) ){
247
+ return false;
248
+ }
249
+
250
+ if( ( !in_array( $this->top_id, $this->exclude ) ) ){
251
+ return true;
252
+ }
253
+
254
+ return false;
255
+ }
256
+
257
+
258
+ /**
259
+ * Echos the title of the widget to the page
260
+ *
261
+ * @since 6.26.13
262
+ */
263
+ function title() {
264
+ if( isset( $this->instance[ 'title' ] ) && ( $this->instance[ 'title' ] != '' ) ){
265
+ $title = apply_filters( 'widget_title', $this->instance[ 'title' ], $this->args, $this->instance );
266
+ $title = apply_filters( 'advanced_sidebar_menu_widget_title', $title, $this->args, $this->instance, $this );
267
+
268
+ if( $this->checked( 'legacy_mode' ) ){
269
+ echo '<h4 class="widgettitle">' . $title . '</h4>';
270
+ } else {
271
+ echo $this->args[ 'before_title' ] . $title . $this->args[ 'after_title' ];
272
+ }
273
+ }
274
+
275
+ }
276
+
277
+
278
+ /**
279
+ *
280
+ * Checks is this id is excluded or not
281
+ *
282
+ * @param int $id the id to check
283
+ *
284
+ * @return bool
285
+ */
286
+ function exclude( $id ) {
287
+ if( !in_array( $id, $this->exclude ) ){
288
+ return true;
289
+ } else {
290
+ return false;
291
+ }
292
+ }
293
+
294
+
295
+ /**
296
+ * Allows for Overwritting files in the child theme
297
+ *
298
+ * @since 4.23.13
299
+ *
300
+ * @param string $file the name of the file to overwrite
301
+ */
302
+
303
+ static function file_hyercy( $file, $legacy = false ) {
304
+ if( $theme_file = locate_template( array( 'advanced-sidebar-menu/' . $file ) ) ){
305
+ $file = $theme_file;
306
+ } elseif( $legacy ) {
307
+ $file = ADVANCED_SIDEBAR_LEGACY_DIR . $file;
308
+ } else {
309
+ $file = ADVANCED_SIDEBAR_VIEWS_DIR . $file;
310
+ }
311
+
312
+ return apply_filters( 'advanced_sidebar_menu_view_file', $file, $legacy );
313
+
314
+ }
315
+
316
+ } //End class
317
+
lib/advancedSidebarMenu.php DELETED
@@ -1,401 +0,0 @@
1
- <?php
2
-
3
-
4
- /**
5
- * These Functions are Specific to the Advanced Sidebar Menu
6
- * @author Mat Lipe
7
- * @since 8.29.13
8
- *
9
- * @package Advanced Sidebar Menu
10
- */
11
- class advancedSidebarMenu{
12
- var $instance; //The widget instance
13
- var $top_id; //Either the top cat or page
14
- var $exclude;
15
- var $ancestors; //For the category ancestors
16
- var $count = 1; //Count for grandchild levels
17
- var $order_by;
18
- var $taxonomy; //For filters to override the taxonomy
19
- var $current_term; //Current category or taxonomy
20
-
21
-
22
- /**
23
- * Check is a page has children by id
24
- *
25
- * @since 8.29.13
26
- *
27
- * @param int $postId
28
- */
29
- function hasChildren($postId){
30
- if( $this->post_type == 'page' ){
31
- $children = get_pages("child_of=$postId");
32
- } else {
33
- $children = get_posts(array(
34
- 'post_type' => $this->post_type,
35
- 'post_parent' => $postId
36
- ) );
37
- }
38
- if( count( $children ) != 0 ) {
39
- return true;
40
- } else {
41
- return false;
42
- }
43
-
44
- }
45
-
46
-
47
- /**
48
- * Checks if a widgets checkbox is checked.
49
- * * this one is special and does a double check
50
- *
51
- * @since 4.1.3
52
- *
53
- * @param string $name - name of checkbox
54
- */
55
- function checked($name){
56
- if( isset( $this->instance[$name] ) && $this->instance[$name] == 'checked' ) return true;
57
-
58
- return false;
59
-
60
- }
61
-
62
-
63
- /**
64
- * Used by a uasort to sort taxonomy arrays by term order
65
- *
66
- * @since 4.3.0
67
- */
68
- function sortTerms( $a, $b ){
69
- if( !isset($a->{$this->order_by}) || !isset($b->{$this->order_by}) ) return 0;
70
-
71
- return $a->{$this->order_by} > $b->{$this->order_by};
72
-
73
- }
74
-
75
-
76
-
77
- /**
78
- * Retrieves the Highest level Category Id
79
- *
80
- * @since 6.6.13
81
- * @param int $catId - id of cat looking for top parent of
82
- *
83
- * @return int
84
- */
85
- function getTopCat( $catId ){
86
- $cat_ancestors = array();
87
- $cat_ancestors[] = $catId;
88
-
89
- do {
90
- $catId = get_term($catId, $this->taxonomy);
91
- $catId = $catId->parent;
92
- $cat_ancestors[] = $catId;
93
- }
94
- while ($catId);
95
-
96
- //Reverse the array to start at the last
97
- $this->ancestors = array_reverse( $cat_ancestors );
98
-
99
- //forget the [0] because the parent of top parent is always 0
100
- return $this->ancestors[1];
101
-
102
- }
103
-
104
-
105
-
106
-
107
- /**
108
- * Removes the closing </li> tag from a list item to allow for child menus inside of it
109
- *
110
- * @param string $item - an <li></li> item
111
- * @return string|bool
112
- * @since 4.7.13
113
- */
114
- function openListItem($item = false){
115
- if( !$item ) return false;
116
-
117
- return substr(trim($item), 0, -5);
118
- }
119
-
120
- /**
121
- * The Old way of doing thing which displayed all 3rd levels and below when on a second level page
122
- * This is only here for people afraid of change who liked the old way of doing things
123
- *
124
- * @uses used in views -> page_list.php when legacy mode is checked in widget
125
- * @since 4.7.13
126
- */
127
- function grandChildLegacyMode($pID ){
128
- #-- if the link that was just listed is the current page we are on
129
- if( !$this->page_ancestor( $pID ) ) return;
130
-
131
- //Get the children of this page
132
- $grandkids = $this->page_children($pID->ID );
133
- if( $grandkids ){
134
- #-- Create a new menu with all the children under it
135
- $content .= '<ul class="grandchild-sidebar-menu">';
136
- $content .= wp_list_pages("post_type=".$this->post_type."&sort_column=$this->order_by&title_li=&echo=0&exclude=".$this->instance['exclude']."&child_of=".$pID->ID);
137
-
138
- $content .= '</ul>';
139
- }
140
-
141
- return $content;
142
- }
143
-
144
-
145
- /**
146
- * Displays all the levels of the Grandchild Menus
147
- *
148
- * Will run until there are no children left for the current page's hyercy
149
- * Only displays the pages if we are on a child or grandchild page of the Id sent
150
- * which at the time of creation comes from the child level pages
151
- *
152
- *
153
- * @uses called by the widget view page_list.php
154
- * @since 4.0
155
- *
156
- * @since 8.1.13
157
- */
158
- function displayGrandChildMenu($page){
159
- static $count = 0;
160
- $count++;
161
-
162
- //If the page sent is not a child of the current page
163
- if( !$this->page_ancestor($page) ) return;
164
-
165
- //if there are no children of the current page bail
166
- if( !$children = $this->page_children($page->ID) ) return;
167
-
168
-
169
- $content = sprintf('<ul class="grandchild-sidebar-menu level-%s children">',$count );
170
- foreach( $children as $child ){
171
-
172
- //If this page should be excluded bail
173
- if( in_array($child->ID, $this->exclude) ) continue;
174
-
175
- $args = array(
176
- 'post_type' => $this->post_type,
177
- 'sort_column' => $this->order_by,
178
- 'title_li' => '',
179
- 'echo' => 0,
180
- 'depth' => 1,
181
- 'include' => $child->ID
182
- );
183
-
184
- $content .= $this->openListItem(wp_list_pages($args));
185
-
186
- //If this newly outputed child is a direct parent of the current page
187
- if( $this->page_ancestor($child) ){
188
- $content .= $this->displayGrandChildMenu($child);
189
- }
190
-
191
- $content .= '</li>';
192
-
193
- }
194
- $content .= '</ul>';
195
-
196
- return $content;
197
-
198
- }
199
-
200
-
201
- /**
202
- * Adds the class for any menu item with children
203
- *
204
- * @param array $css the currrent css classes
205
- * @param obj $page the page being checked
206
- *
207
- *
208
- * @since 8.29.13
209
- *
210
- * @return array
211
- */
212
- function hasChildrenClass($css, $page){
213
- if( $this->hasChildren($page->ID) ){
214
- $css[] = 'has_children';
215
- }
216
-
217
- return $css;
218
-
219
- }
220
-
221
- /**
222
- * Adds the class for current page item etc to the page list when using a custom post type
223
- * @param array $css the currrent css classes
224
- * @param obj $this_menu_item the page being checked
225
- * @return array
226
- * @since 10.10.12
227
- */
228
- function custom_post_type_css($css, $this_menu_item){
229
- global $post;
230
- if ( isset($post->ancestors) && in_array($this_menu_item->ID, (array)$post->ancestors) ){
231
- $css[] = 'current_page_ancestor';
232
- }
233
- if ( $this_menu_item->ID == $post->ID ){
234
- $css[] = 'current_page_item';
235
-
236
- } elseif ($this_menu_item->ID == $post->post_parent ){
237
- $css[] = 'current_page_parent';
238
- }
239
- return $css;
240
- }
241
-
242
-
243
- /**
244
- *
245
- * IF this is a top level category
246
- * @param obj $cat the cat object
247
- *
248
- * @since 6.13.13
249
- */
250
- function first_level_category( $cat ){
251
-
252
- if( !in_array($cat->term_id, $this->exclude) && $cat->parent == $this->top_id){
253
- $return = true;
254
- } else {
255
- $return = false;
256
- }
257
-
258
- return apply_filters('advanced_sidebar_menu_first_level_category', $return, $cat, $this);
259
-
260
- }
261
-
262
- /**
263
- * If the cat is a second level cat
264
- * @param obj $cat the cat
265
- * @since 6.13.13
266
- */
267
- function second_level_cat( $cat ){
268
-
269
- //if this is the currrent cat or a parent of the current cat
270
- if( $cat->term_id == $this->current_term || in_array( $cat->term_id, $this->ancestors )){
271
-
272
- $all_children = array();
273
- $all_children = get_terms( $this->taxonomy, array( 'child_of' => $cat->term_id, 'fields' => 'ids' ) );
274
- if( !empty( $all_children ) ){
275
- $return = true;
276
- } else {
277
- $return = false;
278
- }
279
-
280
- } else {
281
- $return = false;
282
- }
283
-
284
- return apply_filters('advanced_sidebar_menu_second_level_category', $return, $cat, $this);
285
-
286
-
287
- }
288
-
289
- /**
290
- * Determines if all the children should be included
291
- * @since 6.26.13
292
- * @return bool
293
- */
294
- function display_all(){
295
-
296
- return $this->checked('display_all');
297
- }
298
-
299
- /**
300
- *
301
- * Returns and array of all the children of a page
302
- * @param int $pID the id of the page
303
- * @since 4.8.13
304
- */
305
- function page_children( $pID ){
306
- global $wpdb, $table_prefix;
307
- return $wpdb->get_results( "SELECT ID FROM ".$table_prefix."posts WHERE post_parent = ".$pID." AND post_status='publish' ORDER By ".$this->order_by );
308
-
309
- }
310
-
311
- /**
312
- *
313
- * Determines if this is an ancestor or the current post
314
- * @param obj $pID the post object
315
- * @since 6.13.13
316
- */
317
- function page_ancestor( $pID ){
318
- global $post;
319
-
320
- if($pID->ID == $post->ID or $pID->ID == $post->post_parent or @in_array($pID->ID, $post->ancestors) ){
321
- $return = true;
322
- } else {
323
- $return = false;
324
- }
325
-
326
- return apply_filters('advanced_sidebar_menu_page_ancestor', $return, $pID, $this);
327
- }
328
-
329
-
330
- /**
331
- * Determines if the parent page or cat should be included
332
- * @since 6.26.13
333
- * @return bool
334
- */
335
- function include_parent(){
336
-
337
- if( !$this->checked('include_parent') ) return false;
338
-
339
- if( (!in_array($this->top_id, $this->exclude)) ){
340
- return true;
341
- }
342
-
343
- return false;
344
- }
345
-
346
-
347
- /**
348
- * Echos the title of the widget to the page
349
- * @since 6.26.13
350
- */
351
- function title(){
352
- if( isset( $this->instance['title']) && ($this->instance['title'] != '') ){
353
- $title = apply_filters('widget_title', $this->instance['title'], $this->args, $this->instance );
354
- $title = apply_filters('advanced_sidebar_menu_widget_title', $title, $this->args, $this->instance, $this );
355
-
356
- if( $this->checked('legacy_mode') ){
357
- echo '<h4 class="widgettitle">' . $title . '</h4>';
358
- } else {
359
- echo $this->args['before_title'] . $title . $this->args['after_title'];
360
- }
361
- }
362
-
363
- }
364
-
365
-
366
- /**
367
- *
368
- * Checks is this id is excluded or not
369
- * @param int $id the id to check
370
- * @return bool
371
- */
372
- function exclude( $id ){
373
- if( !in_array( $id, $this->exclude ) ){
374
- return true;
375
- } else {
376
- return false;
377
- }
378
- }
379
-
380
-
381
-
382
- /**
383
- * Allows for Overwritting files in the child theme
384
- * @since 4.23.13
385
- * @param string $file the name of the file to overwrite
386
- */
387
-
388
- static function file_hyercy( $file, $legacy = false ){
389
- if ( $theme_file = locate_template(array('advanced-sidebar-menu/'.$file)) ) {
390
- $file = $theme_file;
391
- } elseif( $legacy ){
392
- $file = ADVANCED_SIDEBAR_LEGACY_DIR . $file;
393
- } else {
394
- $file = ADVANCED_SIDEBAR_VIEWS_DIR . $file;
395
- }
396
- return apply_filters( 'advanced_sidebar_menu_view_file', $file, $legacy );
397
-
398
- }
399
-
400
- } //End class
401
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
readme.txt CHANGED
@@ -1,11 +1,11 @@
1
- === Advanced Sidebar Menu ===
2
 
3
  Contributors: Mat Lipe
4
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=paypal%40lipeimagination%2einfo&lc=US&item_name=Advanced%20Sidebar%20Menu&no_note=0&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest
5
  Tags: menus, sidebar menu, heirchy, category menu, pages menu
6
  Requires at least: 3.8.0
7
- Tested up to: 3.9.1
8
- Stable tag: 4.7.6
9
 
10
  Creates a widget for both page and categories that will display the current page/category and all child pages or categories.
11
 
@@ -72,11 +72,11 @@ e.g.
72
  == Frequently Asked Questions ==
73
 
74
  Developer docs may be found here:
75
- <a href="http://matlipe.com/advanced-sidebar-menu/developer-docs/">http://matlipe.com/advanced-sidebar-menu/developer-docs/</a>
76
-
77
- = What text domain do I use for translation and where is the .pot file? =
78
-
79
- The .pot file may be found in the plugins' languages folder. Use the 'advanced-sidebar-menu' text domain.
80
 
81
 
82
  = Version 4.0+ is not displaying the same as previous version of the plugin. How do I bring this back? =
@@ -128,6 +128,10 @@ I do offer preminum services for buiding custom add-ons for additional functiona
128
 
129
 
130
  == Changelog ==
 
 
 
 
131
  = 4.7.0 =
132
  * Added Internationalization (I18n) support
133
 
@@ -190,6 +194,9 @@ I do offer preminum services for buiding custom add-ons for additional functiona
190
 
191
  == Upgrade Notice ==
192
 
 
 
 
193
  = 3.3.0 =
194
  If you customized the output previously you may want to redo it on this version to take advantage of the new structure.
195
  IF you are are using the page_list.php view you will most likely get an error message to remove a couple lines.
1
+ === Advanced Sidebar Menu ===
2
 
3
  Contributors: Mat Lipe
4
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=paypal%40lipeimagination%2einfo&lc=US&item_name=Advanced%20Sidebar%20Menu&no_note=0&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest
5
  Tags: menus, sidebar menu, heirchy, category menu, pages menu
6
  Requires at least: 3.8.0
7
+ Tested up to: 3.9.2
8
+ Stable tag: 5.0.0
9
 
10
  Creates a widget for both page and categories that will display the current page/category and all child pages or categories.
11
 
72
  == Frequently Asked Questions ==
73
 
74
  Developer docs may be found here:
75
+ <a href="http://matlipe.com/advanced-sidebar-menu/developer-docs/">http://matlipe.com/advanced-sidebar-menu/developer-docs/</a>
76
+
77
+ = What text domain do I use for translation and where is the .pot file? =
78
+
79
+ The .pot file may be found in the plugins' languages folder. Use the 'advanced-sidebar-menu' text domain.
80
 
81
 
82
  = Version 4.0+ is not displaying the same as previous version of the plugin. How do I bring this back? =
128
 
129
 
130
  == Changelog ==
131
+ = 5.0.0 =
132
+ * Greatly improved performance
133
+ * Improved code structure
134
+
135
  = 4.7.0 =
136
  * Added Internationalization (I18n) support
137
 
194
 
195
  == Upgrade Notice ==
196
 
197
+ = 5.0.0 =
198
+ If you used a custom page_list.php template previously you may want to redo it on this version to take advantage of the new structure.
199
+
200
  = 3.3.0 =
201
  If you customized the output previously you may want to redo it on this version to take advantage of the new structure.
202
  IF you are are using the page_list.php view you will most likely get an error message to remove a couple lines.
views/page_list.php CHANGED
@@ -1,16 +1,14 @@
1
- <?php
2
  /**
3
- * The Ouput of the Advanced Sidebar Page Widget
 
4
  * @author Mat Lipe
5
- *
6
- * @since 4.5.0
7
- *
8
- * @since 9.24.13
9
  *
 
10
  *
11
- * @uses to edit, create a file named page_list.php and put in a folder in the your theme called 'advanced-sidebar-menu
12
- * @uses copy the contents of the file into that file and edit at will
13
- * @uses Do not edit this file in its original location or it will break on upgrade
14
  */
15
 
16
  $asm->title();
@@ -18,7 +16,7 @@ $asm->title();
18
  #-- list the parent page if chosen
19
  if( $asm->include_parent() ){
20
  $content .= '<ul class="parent-sidebar-menu" >';
21
- $content .= $asm->openListItem(wp_list_pages("post_type=".$post_type."&sort_column=$order_by&title_li=&echo=0&depth=1&include=".$top_parent) );
22
  }
23
 
24
 
@@ -28,36 +26,25 @@ if( $child_pages ){
28
 
29
  #-- If they want all the pages displayed always
30
  if( $asm->display_all() ){
 
31
 
32
- $content .= wp_list_pages("post_type=".$post_type."&sort_column=$order_by&title_li=&echo=0&child_of=".$top_parent."&depth=".$instance['levels']."&exclude=".implode(',',$asm->exclude) );
33
  } else {
34
 
35
- #-- Display children of current page's parent only
36
- foreach($child_pages as $pID){
37
-
38
- #-- If the page is not in the excluded ones
39
- if( $asm->exclude( $pID->ID) ){
40
- #--echo the current page from the $result
41
- $content .= $asm->openListItem(wp_list_pages("post_type=".$post_type."&sort_column=$order_by&title_li=&echo=0&depth=1&include=".$pID->ID));
42
- } else {
43
- continue;
44
- }
45
-
46
- $content .= $asm->displayGrandChildMenu($pID);
47
-
48
-
49
- $content .= '</li>';
50
- }
51
  }
52
 
53
  #-- Close the First Level menu
54
  $content .= '</ul><!-- End child-sidebar-menu -->';
55
 
56
  }
57
- if( $asm->include_parent() ) {
58
  $content .= '</li></ul><!-- .parent-sidebar-menu -->';
59
- }
60
-
61
-
62
-
63
-
1
+ <?php
2
  /**
3
+ * The Output of the Advanced Sidebar Page Widget
4
+ *
5
  * @author Mat Lipe
 
 
 
 
6
  *
7
+ * @since 5.0.0
8
  *
9
+ * @uses to edit, create a file named page_list.php and put in a folder in the your theme called 'advanced-sidebar-menu
10
+ * @uses copy the contents of the file into that file and edit at will
11
+ * @uses Do not edit this file in its original location or it will break on upgrade
12
  */
13
 
14
  $asm->title();
16
  #-- list the parent page if chosen
17
  if( $asm->include_parent() ){
18
  $content .= '<ul class="parent-sidebar-menu" >';
19
+ $content .= $asm->openListItem( wp_list_pages( "post_type=" . $post_type . "&sort_column=$order_by&title_li=&echo=0&depth=1&include=" . $top_parent ) );
20
  }
21
 
22
 
26
 
27
  #-- If they want all the pages displayed always
28
  if( $asm->display_all() ){
29
+ $content .= wp_list_pages( "post_type=" . $post_type . "&sort_column=$order_by&title_li=&echo=0&child_of=" . $top_parent . "&depth=" . $instance[ 'levels' ] . "&exclude=" . implode( ',', $asm->exclude ) );
30
 
 
31
  } else {
32
 
33
+ $args = array(
34
+ 'post_type' => $post_type,
35
+ 'sort_column' => $order_by,
36
+ 'exclude' => $asm->exclude
37
+ );
38
+
39
+ $menu = new Advanced_Sidebar_Menu_List_Pages( $top_parent, $args );
40
+ $content .= $menu->list_pages();
41
+
 
 
 
 
 
 
 
42
  }
43
 
44
  #-- Close the First Level menu
45
  $content .= '</ul><!-- End child-sidebar-menu -->';
46
 
47
  }
48
+ if( $asm->include_parent() ){
49
  $content .= '</li></ul><!-- .parent-sidebar-menu -->';
50
+ }