forked from billerickson/BE-Static-Subpages-Widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-static-subpages-widget.php
193 lines (161 loc) · 6.5 KB
/
be-static-subpages-widget.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
<?php
/*
Plugin Name: BE Static Subpages Widget
Plugin URI: http://www.billerickson.net
Description: Select a page, and widget will list that page's subpages
Version: 1.0
Author: Bill Erickson
Author URI: http://www.billerickson.net
License: GPLv2
*/
/**
* Register Widget
*
*/
function be_static_subpages_load_widgets() {
register_widget( 'BE_Static_Subpages_Widget' );
}
add_action( 'widgets_init', 'be_static_subpages_load_widgets' );
/**
* Subpages Widget Class
*
* @author Bill Erickson <bill@billerickson.net>
* @copyright Copyright (c) 2011, Bill Erickson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
class BE_Static_Subpages_Widget extends WP_Widget {
/**
* Constructor
*
* @return void
**/
function __construct() {
load_plugin_textdomain( 'be-static-subpages', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
$widget_ops = array( 'classname' => 'widget_static_subpages', 'description' => __( 'Select a page, and widget will list that page\'s subpages', 'be-static-subpages' ) );
parent::__construct( 'static-subpages-widget', __( 'Static Subpages Widget', 'be-static-subpages' ), $widget_ops );
}
/**
* Outputs the HTML for this widget.
*
* @param array, An array of standard parameters for widgets in this theme
* @param array, An array of settings for this widget instance
* @return void Echoes it's output
**/
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
$page = (int) $instance['page'];
if( empty( $page ) )
return;
// Build a menu listing top level parent's children
$args = array(
'child_of' => $page,
'parent' => $page,
'sort_column' => 'menu_order',
'post_type' => 'page',
);
$depth = 1;
$subpages = get_pages( apply_filters( 'be_static_subpages_widget_args', $args, $depth ) );
// If there are pages, display the widget
if ( empty( $subpages ) )
return;
echo $before_widget;
// Build title
$title = esc_attr( $instance['title'] );
if( 1 == $instance['title_from_parent'] )
$title = get_the_title( $page );
$title = apply_filters( 'be_static_subpages_widget_title', $title );
if( 1 == $instance['title_link'] )
$title = '<a href="' . get_permalink( $page ) . '">' . $title . '</a>';
if( !empty( $title ) )
echo $before_title . $title . $after_title;
// Print the tree
$active = is_singular() && ! $instance['active'] ? get_the_ID() : $instance['active'];
$this->build_subpages( $subpages, $page, $active );
echo $after_widget;
}
/**
* Build the Subpages
*
* @param array $subpages, array of post objects
* @param array $parents, array of parent IDs
* @param bool $deep_subpages, whether to include current page's subpages
* @return string $output
*/
function build_subpages( $subpages, $page, $active ) {
if( empty( $subpages ) )
return;
// Build the page listing
echo '<ul>';
foreach ( $subpages as $subpage ) {
$class = array();
// Set special class for current page
if ( $subpage->ID == $active )
$class[] = 'widget_subpages_current_page';
$class = apply_filters( 'be_static_subpages_widget_class', $class, $subpage );
$class = !empty( $class ) ? ' class="' . implode( ' ', $class ) . '"' : '';
echo '<li' . $class . '><a href="' . get_permalink( $subpage->ID ) . '">' . apply_filters( 'be_subpages_page_title', $subpage->post_title, $subpage ) . '</a></li>';
}
echo '</ul>';
}
/**
* Sanitizes form inputs on save
*
* @param array $new_instance
* @param array $old_instance
* @return array $new_instance
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags (if needed) and update the widget settings. */
$instance['page'] = (int) $new_instance['page'];
$instance['active'] = (int) $new_instance['active'];
$instance['title'] = esc_attr( $new_instance['title'] );
$instance['title_from_parent'] = (int) $new_instance['title_from_parent'];
$instance['title_link'] = (int) $new_instance['title_link'];
return $instance;
}
/**
* Build the widget's form
*
* @param array $instance, An array of settings for this widget instance
* @return null
*/
function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array( 'page' => false, 'active' => false, 'title' => '', 'title_from_parent' => 0, 'title_link' => 0 );
$instance = wp_parse_args( (array) $instance, $defaults );
$dropdown_args = array(
'post_type' => 'page',
'selected' => $instance['page'],
'name' => $this->get_field_name( 'page' ),
'sort_column' => 'menu_order, post_title',
'echo' => 0,
);
$dropdown_args = apply_filters( 'be_static_subpages_dropdown_args', $dropdown_args );
echo '<p><label for="' . $this->get_field_id( 'page' ) . '">Show Subpages of:</label><br />' . wp_dropdown_pages( $dropdown_args ) . '</p>';
$active_args = array(
'post_type' => 'page',
'selected' => $instance['active'],
'name' => $this->get_field_name( 'active' ),
'sort_column' => 'menu_order, post_title',
'echo' => 0,
'show_option_none' => '--None',
);
$active_args = apply_filters( 'be_static_subpages_active_args', $active_args );
echo '<p><label for="' . $this->get_field_id( 'active' ) . '">Show as Active Page:</label><br />' . wp_dropdown_pages( $active_args ) . '</p>';
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'be-subpages' );?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<input class="checkbox" type="checkbox" value="1" <?php checked( $instance['title_from_parent'], 1 ); ?> id="<?php echo $this->get_field_id( 'title_from_parent' ); ?>" name="<?php echo $this->get_field_name( 'title_from_parent' ); ?>" />
<label for="<?php echo $this->get_field_id( 'title_from_parent' ); ?>"><?php _e( 'Use top level page as section title.', 'be-subpages' );?></label>
</p>
<p>
<input class="checkbox" type="checkbox" value="1" <?php checked( $instance['title_link'], 1 ); ?> id="<?php echo $this->get_field_id( 'title_link' ); ?>" name="<?php echo $this->get_field_name( 'title_link' ); ?>" />
<label for="<?php echo $this->get_field_id( 'title_link' ); ?>"><?php _e( 'Link widget title to top level page', 'be-subpages' ); echo '</label>';?>
</p>
<?php
}
}