-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsubpages-extended.php
226 lines (181 loc) · 8.18 KB
/
subpages-extended.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
<?php
if ( ! defined( 'ABSPATH' ) ) {
die( 'Invalid request.' );
}
class shailan_SubpagesWidget extends WP_Widget {
/** constructor */
public function __construct(){
$widget_ops = array(
'classname' => 'shailan-subpages-widget',
'description' => __( 'Subpages list', 'subpages-extended' )
);
parent::__construct(
'shailan-subpages-widget',
__( 'Subpages Extended', 'subpages-extended' ),
$widget_ops
);
$this->alt_option_name = 'widget_shailan_subpages';
$this->help_page = 'https://wpassist.me/docs/subpages-extended-help/';
if ( is_active_widget(false, false, $this->id_base) )
add_action( 'wp_head', array(&$this, 'styles') );
$this->sort_options = array(
'Post Title' => 'post_title',
'Menu Order' => 'menu_order, post_title',
'Date' => 'post_date',
'Last Modified' => 'post_modified',
'Page ID' => 'ID',
'Page Author' => 'post_author',
'Page Slug' => 'post_name'
);
$this->widget_defaults = array(
'title' => '',
'exclude' => '',
'depth' => -1,
'use_parent_title' => false,
'exceptme' => false,
'childof' => '',
'sort_by' => 'menu_order, post_title',
'link_on_title' => false,
'rel' => ''
);
}
/** @see WP_Widget::widget */
function widget($args, $instance) {
global $post;
extract( $args );
$widget_options = wp_parse_args( $instance, $this->widget_defaults );
extract( $widget_options, EXTR_SKIP );
$use_parent_title = (bool) $use_parent_title;
/* $use_menu_labels = (bool) $use_menu_labels; */
$link_on_title = (bool) $link_on_title;
$is_visible = false;
if( '-1' == $childof ) {
$childof = $post->ID;
} elseif( '*parent*' == $childof ) {
$childof = $post->post_parent;
if($childof == 0){ $childof = $post->ID; } /* Top pages display sub pages only */
} elseif( '*full-branch*' == $childof ) {
if(!$post->post_parent){
$childof = $post->ID;
} else {
$parent = $post->post_parent;
$p = get_post($parent);
while($p->post_parent) {
$p = get_post($p->post_parent);
}
$childof = $p->ID;
}
} else {
$is_visible = true;
}
if( is_page() || $is_visible ){
$parent = $childof;
// Setup page walker
$walker = new Shailan_Walker_Page;
$title_filter = 'walker_page_title';
$walker->set_rel($rel);
// Use parent title
if( $use_parent_title ){ $title = get_the_title($parent); }
// Link parent title
if( $use_parent_title && $link_on_title ){
$title = '<a href="' . get_permalink($parent) . '" title="' . esc_attr( wp_strip_all_tags( apply_filters( 'the_title', $title, $parent) ) ) . '">' . apply_filters( $title_filter, $title, $parent ) . '</a>';
} else {
$title = apply_filters( $title_filter, $title, $parent );
}
if( !$use_parent_title ){ $title = apply_filters('widget_title', $title); }
$children=wp_list_pages( 'echo=0&child_of=' . $parent . '&title_li=' );
$subpage_args = array(
'depth' => $depth,
'show_date' => 0,
'date_format' => get_option('date_format'),
'child_of' => $parent,
'exclude' => $exclude,
'include' => '',
'title_li' => '',
'echo' => 1,
'authors' => '',
'sort_column' => $sort_by,
'link_before' => '',
'link_after' => '',
'walker' => $walker );
if ($children) {
?>
<?php echo $before_widget; ?>
<?php if ( $title )
echo $before_title . $title . $after_title;
?>
<div id="shailan-subpages-<?php echo $this->number; ?>">
<ul class="subpages">
<?php wp_list_pages($subpage_args); ?>
</ul>
</div>
<?php echo $after_widget; ?>
<?php
} else {
echo "\n\t<!-- SUBPAGES : This page doesn't have any subpages. -->";
};
}
}
function update($new_instance, $old_instance) {
return $new_instance;
}
function form($instance) {
$widget_options = wp_parse_args( $instance, $this->widget_defaults );
extract( $widget_options, EXTR_SKIP );
$title = esc_attr($title);
$use_parent_title = (bool) $use_parent_title;
$link_on_title = (bool) $link_on_title;
?>
<style>a.help-link {
background: #eee;
border-radius: 50%;
display: inline-block;
height: 16px;
width: 16px;
padding: 0;
color: #0085ba;
text-align: center;
line-height: 15px;
text-decoration: none;
border: 1px solid;
}</style>
<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('use_parent_title'); ?>" name="<?php echo $this->get_field_name('use_parent_title'); ?>"<?php checked( $use_parent_title ); ?> /> <label for="<?php echo $this->get_field_id('use_parent_title'); ?>"><?php _e( 'Use page title as widget title' , 'subpages-extended' ); ?></label>
<a class="help-link" target="_blank" rel="noopener" href="<?php echo $this->help_page; ?>#title">?</a>
</p>
<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('link_on_title'); ?>" name="<?php echo $this->get_field_name('link_on_title'); ?>"<?php checked( $link_on_title ); ?> /> <label for="<?php echo $this->get_field_id('link_on_title'); ?>"><?php _e( 'Use link on widget title' , 'subpages-extended' ); ?></label>
<a class="help-link" target="_blank" rel="noopener" href="<?php echo $this->help_page; ?>#title-link">?</a>
</p>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title :'); ?> <a class="help-link" target="_blank" rel="noopener" href="<?php echo $this->help_page; ?>#title">?</a> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('childof'); ?>"><?php _e('Parent (Subpages of):'); ?> <a class="help-link" target="_blank" rel="noopener" href="<?php echo $this->help_page; ?>#parent">?</a> <?php
$args = array(
'selected' => $childof,
'show_option_no_change' => '*Current page*',
'show_option_none' => '*Parent of current page*',
'option_none_value' => '*parent*',
'name' => $this->get_field_name('childof'),
'id' => $this->get_field_id('childof')
); shailan_subpages_dropdown_pages($args); ?></label></p>
<p><label for="<?php echo $this->get_field_id('rel'); ?>"><?php _e('Rel :'); ?> <a class="help-link" target="_blank" rel="noopener" href="<?php echo $this->help_page; ?>#rel">?</a> <input class="widefat" id="<?php echo $this->get_field_id('rel'); ?>" name="<?php echo $this->get_field_name('rel'); ?>" type="text" value="<?php echo $rel; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e('Exclude:'); ?> <a class="help-link" target="_blank" rel="noopener" href="<?php echo $this->help_page; ?>#exclude">?</a> <input class="widefat" id="<?php echo $this->get_field_id('exclude'); ?>" name="<?php echo $this->get_field_name('exclude'); ?>" type="text" value="<?php echo $exclude; ?>" /></label><br />
<small>Page IDs, separated by commas.</small></p>
<p><label for="<?php echo $this->get_field_id('sort_by'); ?>"><?php _e('Sort by :'); ?> <a class="help-link" target="_blank" rel="noopener" href="<?php echo $this->help_page; ?>#sort-by">?</a> <select name="<?php echo $this->get_field_name('sort_by'); ?>" id="<?php echo $this->get_field_id('sort_by'); ?>" ><?php
foreach ($this->sort_options as $value=>$key) {
$option = '<option value="'. $key .'" '. ( $key == $sort_by ? ' selected="selected"' : '' ) .'>';
$option .= $value;
$option .= '</option>\n';
echo $option;
}
?>
</select></label></p>
<p><label for="<?php echo $this->get_field_id('depth'); ?>"><?php _e('Depth:'); ?> <a class="help-link" target="_blank" rel="noopener" href="<?php echo $this->help_page; ?>#depth">?</a> <input class="widefat" id="<?php echo $this->get_field_id('depth'); ?>" name="<?php echo $this->get_field_name('depth'); ?>" type="text" value="<?php echo $depth; ?>" /></label><br />
<small>Depth of menu.</small></p>
<?php
}
function styles($instance){
// additional styles will be printed here.
}
} // class shailan_SubpagesWidget
// register widget
add_action('widgets_init', function(){
register_widget("shailan_SubpagesWidget");
});