-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress-posts.php
More file actions
executable file
·322 lines (263 loc) · 15.6 KB
/
express-posts.php
File metadata and controls
executable file
·322 lines (263 loc) · 15.6 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/*
Plugin Name: Express Posts
Plugin URI: https://wordpress.org/plugins/express-posts/
Description: Express posts provides a widget to display either a subset of posts, the children of a page or its siblings.
Version: 1.3.0
Author: Grant Mangham
Author URI: http://begtodiffer.ca
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Express_Posts extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'express_posts',
'description' => 'Display either a subset of posts, the children of a page or it\'s siblings',
);
parent::__construct( 'express_posts', 'Express Posts', $widget_ops );
add_action( 'admin_print_scripts-widgets.php', array( $this, 'express_posts_scripts' ) );
}
function express_posts_scripts() {
wp_enqueue_script( 'express-posts', plugins_url( '/express-posts.js', __FILE__ ), array( 'jquery' ), '1.0' );
}
function widget( $args, $instance ) {
global $post;
$quantity = ( ( empty( $instance['quantity'] ) || ! $quantity = absint( $instance['quantity'] ) ) ? 5 : $instance['quantity'] );
$defaults = array(
'show_widget_title' => 0,
'show_excerpt' => 0,
'categories' => array(),
);
$instance = wp_parse_args( ( array ) $instance, $defaults );
switch ( $instance['relationship'] ) {
case 'subset':
$query_args = array(
'category__in' => $instance['categories'],
'posts_per_page' => $quantity,
'ignore_sticky_posts' => 1,
'orderby' => ( substr( $instance['ordering'], stripos( $instance['ordering'], ' ' ) ) ),
'order' => ( 'date asc' == $instance['ordering'] ? 'ASC' : 'DESC' ),
);
break;
case 'children':
$ancestors = get_post_ancestors( $post->ID );
if ( 'show' == $instance['children_generations_filter'] ) {
if ( ! in_array( count( $ancestors ) + 1, $instance['children_generations'] ) ) {
break;
}
}
if ( 'hide' == $instance['children_generations_filter'] ) {
if ( in_array( count( $ancestors ) + 1, $instance['children_generations'] ) ) {
break;
}
}
$query_args = array(
'post_parent' => $post->ID,
'posts_per_page' => - 1,
'orderby' => $instance['children_ordering'],
'order' => 'ASC',
'post_type' => 'page',
);
break;
case 'siblings':
$ancestors = get_post_ancestors( $post->ID );
if ( 'show' == $instance['siblings_generations_filter'] ) {
if ( ! in_array( count( $ancestors ) + 1, $instance['siblings_generations'] ) ) {
break;
}
}
if ( 'hide' == $instance['siblings_generations_filter'] ) {
if ( in_array( count( $ancestors ) + 1, $instance['siblings_generations'] ) ) {
break;
}
}
if ( $ancestors ) {
$parent_post_id = $ancestors[0];
$query_args = array(
'post_parent' => $parent_post_id,
'posts_per_page' => - 1,
'orderby' => $instance['siblings_ordering'],
'order' => 'ASC',
'post_type' => 'page',
'post__not_in' => array( $post->ID ),
);
}
break;
}
if ( ! isset( $query_args ) ) {
return;
}
$query_args['no_found_rows'] = true;
$query = new WP_Query( $query_args );
if ( $query->posts ) {
echo str_replace( 'widget-container', 'widget-container express_posts-' . $instance['relationship'], $args['before_widget'] );
if ( $instance['show_widget_title'] ) {
// Does title include a placeholder?
if ( stripos( $instance['title'], '[' ) ) {
if ( 'children' == $instance['relationship'] ) {
// If relationship is children, replace placeholder with current post title
$instance['title'] = str_ireplace( '[title]', $post->post_title, $instance['title'] );
}
if ( 'siblings' == $instance['relationship'] ) {
// If relationship is siblings and current post has a parent, replace placeholder with parent's title
$instance['title'] = str_ireplace( '[title]', get_the_title( $parent_post_id ), $instance['title'] );
}
}
echo $args['before_title'] . $instance['title'] . $args['after_title'];
}
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '" class="title">';
if ( get_the_title() ) {
the_title();
} else {
the_ID();
}
echo '</a>';
if ( 'subset' == $instance['relationship'] ) {
echo( 'none' == $instance['date_format'] ? '' : '<div class="entry-date">' . ( 'default' == $instance['date_format'] ? get_the_time( get_option( 'date_format' ) ) : get_the_time( $instance['custom_date_format'] ) ) . '</div>' );
if ( $instance['show_excerpt'] ) {
the_excerpt();
}
}
echo '</li>';
}
echo '</ul>';
if ( $instance['footer'] ) {
echo '<div class="footer">' . $instance['footer'] . '</div>';
}
echo $args['after_widget'];
}
wp_reset_postdata();
}
function form( $instance ) {
$defaults = array(
'title' => '',
'show_widget_title' => 0,
'show_excerpt' => 0,
'categories' => array(),
'children_generations_filter' => 'none',
'children_generations' => array(),
'siblings_generations_filter' => 'none',
'siblings_generations' => array(),
'date_format' => 'default',
'quantity' => 5,
'ordering' => 'date desc',
'children_ordering' => 'menu_order',
'siblings_ordering' => 'menu_order',
'custom_date_format' => 'F j, Y',
'relationship' => 'subset',
'footer' => '',
);
$instance = wp_parse_args( ( array ) $instance, $defaults );
$title = strip_tags( $instance['title'] );
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"/><br/>
<input type="checkbox" id="<?php echo $this->get_field_id( 'show_widget_title' ); ?>" name="<?php echo $this->get_field_name( 'show_widget_title' ); ?>" <?php checked( $instance['show_widget_title'], 1, true ); ?> value="1"/> <label for="<?php echo $this->get_field_id( 'show_widget_title' ); ?>">Show title</label></p>
<p><input type="radio" name="<?php echo $this->get_field_name( 'relationship' ); ?>" value="subset" id="<?php echo $this->get_field_id( 'relationship' ); ?>-1" class="express-posts-relationship" <?php checked( $instance['relationship'], 'subset' ); ?> /> <label for="<?php echo $this->get_field_id( 'relationship' ); ?>-1">Posts subset</label><br/>
<input type="radio" name="<?php echo $this->get_field_name( 'relationship' ); ?>" value="children" id="<?php echo $this->get_field_id( 'relationship' ); ?>-2" class="express-posts-relationship" <?php checked( $instance['relationship'], 'children' ); ?> /> <label for="<?php echo $this->get_field_id( 'relationship' ); ?>-2">Child pages</label><br/>
<input type="radio" name="<?php echo $this->get_field_name( 'relationship' ); ?>" value="siblings" id="<?php echo $this->get_field_id( 'relationship' ); ?>-3" class="express-posts-relationship" <?php checked( $instance['relationship'], 'siblings' ); ?> /> <label for="<?php echo $this->get_field_id( 'relationship' ); ?>-3">Sibling pages</label></p>
<fieldset class="subset" style="display: <?php echo( 'subset' == $instance['relationship'] ? 'block' : 'none' ); ?>">
<p><label for="<?php echo $this->get_field_id( 'quantity' ); ?>"><?php _e( 'Quantity:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'quantity' ); ?>" name="<?php echo $this->get_field_name( 'quantity' ); ?>" type="text" value="<?php echo esc_attr( $instance['quantity'] ); ?>" size="3"/></p>
<p><input type="checkbox" id="<?php echo $this->get_field_id( 'show_excerpt' ); ?>" name="<?php echo $this->get_field_name( 'show_excerpt' ); ?>" <?php checked( $instance['show_excerpt'], 1, true ); ?> value="1"/> <label for="<?php echo $this->get_field_id( 'show_excerpt' ); ?>">Show excerpt</label></p>
<p><label for="<?php echo $this->get_field_id( 'ordering' ); ?>"><?php _e( 'Order by' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'ordering' ); ?>" name="<?php echo $this->get_field_name( 'ordering' ); ?>">
<option value="date desc"<?php selected( $instance['ordering'], 'date desc' ); ?>>Date (Newest to oldest)</option>
<option value="date asc"<?php selected( $instance['ordering'], 'date asc' ); ?>>Date (Oldest to newest)</option>
<option value="title"<?php selected( $instance['ordering'], 'title' ); ?>>Title</option>
<option value="rand"<?php selected( $instance['ordering'], 'rand' ); ?>>Random</option>
</select></p>
<p><label for="<?php echo $this->get_field_id( 'date_format' ); ?>"><?php _e( 'Date' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'date_format' ); ?>" name="<?php echo $this->get_field_name( 'date_format' ); ?>">
<option value="default"<?php selected( $instance['date_format'], 'default' ); ?>>Show date: default format</option>
<option value="custom"<?php selected( $instance['date_format'], 'custom' ); ?>>Show date: custom format</option>
<option value="none"<?php selected( $instance['date_format'], 'none' ); ?>>Don't show date</option>
</select></p>
<p><label for="<?php echo $this->get_field_id( 'custom_date_format' ); ?>"><?php _e( 'Custom date format:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'custom_date_format' ); ?>" name="<?php echo $this->get_field_name( 'custom_date_format' ); ?>" type="text" value="<?php echo esc_attr( $instance['custom_date_format'] ); ?>" size="3"/></p>
<p><label for="<?php echo $this->get_field_id( 'categories' ); ?>"><?php _e( 'Categories:' ); ?></label><br/>
<?php
foreach (
get_categories( array(
'type' => 'post',
'hide_empty' => 0,
) ) as $category
) {
$option = '<input type="checkbox" id="' . $this->get_field_id( 'categories' ) . '-' . $category->term_id . '" name="' . $this->get_field_name( 'categories' ) . '[' . $category->term_id . ']" ' . checked( in_array( $category->term_id, $instance['categories'] ), true, false );
$option .= ' value="' . $category->term_id . '" /> <label for="' . $this->get_field_id( 'categories' ) . '-' . $category->term_id . '">' . $category->cat_name . '</label><br />';
echo $option;
}
?>
</p>
<p><label for="<?php echo $this->get_field_id( 'footer' ); ?>"><?php _e( 'Footer:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'footer' ); ?>" name="<?php echo $this->get_field_name( 'footer' ); ?>" type="text" value="<?php echo esc_attr( $instance['footer'] ); ?>"/><br/></p>
</fieldset>
<fieldset class="children" style="display: <?php echo( 'children' == $instance['relationship'] ? 'block' : 'none' ); ?>">
<p><label for="<?php echo $this->get_field_id( 'children_ordering' ); ?>"><?php _e( 'Order by' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'children_ordering' ); ?>" name="<?php echo $this->get_field_name( 'children_ordering' ); ?>">
<option value="menu_order"<?php selected( $instance['children_ordering'], 'menu_order' ); ?>>Menu order</option>
<option value="title"<?php selected( $instance['children_ordering'], 'title' ); ?>>Title</option>
</select></p>
<p>You can use [title] in the title field as a placeholder for the current post's title.</p>
<p><select name="<?php echo $this->get_field_name( 'children_generations_filter' ); ?>" id="<?php echo $this->get_field_id( 'children_generations_filter' ); ?>">
<option value="none">Filters</option>
<option value="show"<?php selected( $instance['children_generations_filter'], 'show' ); ?>>Show widget on these generations</option>
<option value="hide"<?php selected( $instance['children_generations_filter'], 'hide' ); ?>>Hide widget on these generations</option>
</select></p>
<p><label for="<?php echo $this->get_field_id( 'children_generations' ); ?>"><?php _e( 'Generations:' ); ?></label><br/>
<?php
for ( $generation = 1; $generation < 11; $generation ++ ) {
$option = '<input type="checkbox" id="' . $this->get_field_id( 'children_generations' ) . '-' . $generation . '" name="' . $this->get_field_name( 'children_generations' ) . '[' . $generation . ']" ' . checked( in_array( $generation, $instance['children_generations'] ), true, false );
$option .= ' value="' . $generation . '" /> <label for="' . $this->get_field_id( 'children_generations' ) . '-' . $generation . '">' . $this->get_ordinal( $generation ) . '</label><br />';
echo $option;
}
?>
</p>
</fieldset>
<fieldset class="siblings" style="display: <?php echo( 'siblings' == $instance['relationship'] ? 'block' : 'none' ); ?>">
<p><label for="<?php echo $this->get_field_id( 'siblings_ordering' ); ?>"><?php _e( 'Order by' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'siblings_ordering' ); ?>" name="<?php echo $this->get_field_name( 'siblings_ordering' ); ?>">
<option value="menu_order"<?php selected( $instance['siblings_ordering'], 'menu_order' ); ?>>Menu order</option>
<option value="title"<?php selected( $instance['siblings_ordering'], 'title' ); ?>>Title</option>
</select></p>
<p>You can use [title] in the title field as a placeholder for the current post's parent's title.</p>
<p><select name="<?php echo $this->get_field_name( 'siblings_generations_filter' ); ?>" id="<?php echo $this->get_field_id( 'siblings_generations_filter' ); ?>">
<option value="none">Filters</option>
<option value="show"<?php selected( $instance['siblings_generations_filter'], 'show' ); ?>>Show widget on these generations</option>
<option value="hide"<?php selected( $instance['siblings_generations_filter'], 'hide' ); ?>>Hide widget on these generations</option>
</select></p>
<p><label for="<?php echo $this->get_field_id( 'siblings_generations' ); ?>"><?php _e( 'Generations:' ); ?></label><br/>
<?php
for ( $generation = 1; $generation < 11; $generation ++ ) {
$option = '<input type="checkbox" id="' . $this->get_field_id( 'siblings_generations' ) . '-' . $generation . '" name="' . $this->get_field_name( 'siblings_generations' ) . '[' . $generation . ']" ' . checked( in_array( $generation, $instance['siblings_generations'] ), true, false );
$option .= ' value="' . $generation . '" /> <label for="' . $this->get_field_id( 'siblings_generations' ) . '-' . $generation . '">' . $this->get_ordinal( $generation ) . '</label><br />';
echo $option;
}
?>
</p>
</fieldset>
<?php
}
function get_ordinal( $number ) {
$suffix = array( 'th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th' );
if ( ( $number % 100 ) >= 11 && ( $number % 100 ) <= 13 ) {
$ordinal = $number . 'th';
} else {
$ordinal = $number . $suffix[ $number % 10 ];
}
return $ordinal;
}
}
add_action( 'widgets_init', function () {
register_widget( 'Express_Posts' );
} );