Flexible Posts Widget - Version 1.0

Version Description

  • First public release
Download this release

Release Info

Developer dpe415
Plugin Icon wp plugin Flexible Posts Widget
Version 1.0
Comparing to
See all releases

Version 1.0

flexible-posts-widget.php ADDED
@@ -0,0 +1,258 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Flexible Posts Widget
4
+ Plugin URI: http://wordpress.org/extend/plugins/flexible-posts-widget/
5
+ Author: David Paul Ellenwood
6
+ Author URI: http://dpedesign.com
7
+ Requires at least: 3.2
8
+ Tested up to: 3.3.1
9
+ Stable tag: 1.0
10
+ Tags: widget, widgets, posts, recent posts, thumbnails, custom post types, custom taxonomies
11
+ Description: An advanced posts display widget with many options: post by taxonomy & term or post type, thumbnails, order & order by, customizable templates
12
+ */
13
+
14
+ // Block direct requests
15
+ if ( !defined('ABSPATH') )
16
+ die('-1');
17
+
18
+ // Load the widget on widgets_init
19
+ function dpe_load_flexible_posts_widget() {
20
+ register_widget('DPE_Flexible_Posts_Widget');
21
+ }
22
+ add_action('widgets_init', 'dpe_load_flexible_posts_widget');
23
+
24
+ /**
25
+ * Flexible Posts Widget Class
26
+ */
27
+ class DPE_Flexible_Posts_Widget extends WP_Widget {
28
+
29
+ /** constructor */
30
+ function DPE_Flexible_Posts_Widget() {
31
+ parent::WP_Widget(false, $name = 'Flexible Posts Widget');
32
+ }
33
+
34
+ /** @see WP_Widget::widget */
35
+ function widget($args, $instance) {
36
+ extract( $args );
37
+ $title = apply_filters('widget_title', $instance['title']);
38
+ $taxonomy = esc_attr( $instance['taxonomy'] );
39
+ $term = esc_attr( $instance['term'] );
40
+ $number = esc_attr( (int)$instance['number'] );
41
+ $thumb_size = esc_attr( $instance['thumb_size'] );
42
+ $thumbnail = esc_attr( $instance['thumbnail'] );
43
+ $posttype = esc_attr( $instance['posttype'] );
44
+ $orderby = esc_attr( $instance['orderby'] );
45
+ $order = esc_attr( $instance['order'] );
46
+ $template = esc_attr( $instance['template'] );
47
+
48
+ if( empty($template) )
49
+ $template = 'widget';
50
+
51
+ // Setup our query
52
+ if( !empty($term) ) {
53
+ $args = array(
54
+ 'tax_query' => array(
55
+ array(
56
+ 'taxonomy' => $taxonomy,
57
+ 'field' => 'slug',
58
+ 'terms' => $term,
59
+ )
60
+ ),
61
+ 'post_status' => array('publish', 'private', 'inherit'),
62
+ 'posts_per_page' => $number,
63
+ 'orderby' => $orderby,
64
+ 'order' => $order,
65
+ );
66
+ } else {
67
+ $args = array(
68
+ 'post_status' => array('publish', 'private', 'inherit'),
69
+ 'post_type' => $posttype,
70
+ 'posts_per_page' => $number,
71
+ 'orderby' => $orderby,
72
+ 'order' => $order,
73
+ );
74
+ }
75
+
76
+ // Get the posts for this instance
77
+ $flexible_posts = new WP_Query( $args );
78
+
79
+ include( $this->getTemplateHierarchy( $template ) );
80
+
81
+ wp_reset_postdata();
82
+
83
+ }
84
+
85
+ /** @see WP_Widget::update */
86
+ function update($new_instance, $old_instance) {
87
+ global $posttypes;
88
+ $instance = $old_instance;
89
+ $instance['title'] = strip_tags( $new_instance['title'] );
90
+ $instance['taxonomy'] = $new_instance['taxonomy'];
91
+ $instance['term'] = strip_tags( $new_instance['term'] );
92
+ $instance['number'] = strip_tags( $new_instance['number'] );
93
+ $instance['thumb_size'] = $new_instance['thumb_size'];
94
+ $instance['thumbnail'] = $new_instance['thumbnail'];
95
+ $instance['posttype'] = $new_instance['posttype'];
96
+ $instance['orderby'] = $new_instance['orderby'];
97
+ $instance['order'] = $new_instance['order'];
98
+ $instance['template'] = strip_tags( $new_instance['template'] );
99
+ return $instance;
100
+ }
101
+
102
+ /** @see WP_Widget::form */
103
+ function form($instance) {
104
+
105
+ $posttypes = get_post_types('', 'objects');
106
+ $taxonomies = get_taxonomies('', 'objects');
107
+
108
+ $orderbys = array(
109
+ 'ID',
110
+ 'title',
111
+ 'date',
112
+ 'rand',
113
+ 'menu_order',
114
+ );
115
+
116
+ $orders = array(
117
+ 'ASC',
118
+ 'DESC',
119
+ );
120
+
121
+ $thumb_sizes = get_intermediate_image_sizes();
122
+
123
+ if( !empty($instance) ) {
124
+ $title = esc_attr ($instance['title'] );
125
+ $taxonomy = esc_attr( $instance['taxonomy'] );
126
+ $term = esc_attr( $instance['term'] );
127
+ $number = esc_attr( $instance['number'] );
128
+ $thumb_size = esc_attr( $instance['thumb_size'] );
129
+ $thumbnail = esc_attr( $instance['thumbnail'] );
130
+ $posttype = esc_attr( $instance['posttype'] );
131
+ $orderby = esc_attr( $instance['orderby'] );
132
+ $order = esc_attr( $instance['order'] );
133
+ $template = esc_attr( $instance['template'] );
134
+ }
135
+
136
+ if( empty($orderby) )
137
+ $orderby = 'date';
138
+
139
+ if( empty($order) )
140
+ $order = 'DESC';
141
+
142
+ if( empty($template) )
143
+ $template = 'widget';
144
+
145
+ ?>
146
+ <p>
147
+ <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:'); ?></label>
148
+ <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; ?>" />
149
+ </p>
150
+ <h4 style="border-bottom:1px solid #dfdfdf;">Get posts by:</h4>
151
+ <p><strong>By taxonomy &amp; term</strong></p>
152
+ <p>
153
+ <label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Select a taxonomy'); ?></label>
154
+ <select class="widefat" name="<?php echo $this->get_field_name('taxonomy'); ?>" id="<?php echo $this->get_field_id('taxonomy'); ?>">
155
+ <?php
156
+ foreach ($taxonomies as $option) {
157
+ echo '<option value="' . $option->name . '" id="' . $option->name . '"', $taxonomy == $option->name ? ' selected="selected"' : '', '>', $option->name, '</option>';
158
+ }
159
+ ?>
160
+ </select>
161
+ </p>
162
+ <p>
163
+ <label for="<?php echo $this->get_field_id('term'); ?>"><?php _e('Term &quot;slug&quot; (not the name)'); ?></label>
164
+ <input class="widefat" id="<?php echo $this->get_field_id('term'); ?>" name="<?php echo $this->get_field_name('term'); ?>" type="text" value="<?php echo $term; ?>" />
165
+ <span style="padding-top:3px;" class="description"><?php _e('Leave blank to ignore taxonomies &amp; terms'); ?></span>
166
+ </p>
167
+ <hr style="margin-bottom:12px; border:none; border-bottom:1px solid #dfdfdf" />
168
+ <p><strong>By post type:</strong></p>
169
+ <p>
170
+ <label for="<?php echo $this->get_field_id('posttype'); ?>"><?php _e('Post type'); ?></label>
171
+ <select class="widefat" name="<?php echo $this->get_field_name('posttype'); ?>" id="<?php echo $this->get_field_id('posttype'); ?>">
172
+ <?php
173
+ foreach ($posttypes as $option) {
174
+ echo '<option value="' . $option->name . '" id="' . $option->name . '"', $posttype == $option->name ? ' selected="selected"' : '', '>', $option->name, '</option>';
175
+ }
176
+ ?>
177
+ </select>
178
+ <span style="padding-top:3px;" class="description">Ignored if a term is provided.</span>
179
+ </p>
180
+ <h4 style="border-bottom:1px solid #dfdfdf;">Display options</h4>
181
+ <p>
182
+ <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number to Show:'); ?></label>
183
+ <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
184
+ </p>
185
+ <p>
186
+ <input id="<?php echo $this->get_field_id('thumbnail'); ?>" name="<?php echo $this->get_field_name('thumbnail'); ?>" type="checkbox" value="1" <?php checked( '1', $thumbnail ); ?>/>
187
+ <label for="<?php echo $this->get_field_id('thumbnail'); ?>"><?php _e('Display thumbnails?'); ?></label>
188
+ </p>
189
+ <p>
190
+ <label for="<?php echo $this->get_field_id('thumb_size'); ?>"><?php _e('Select a thumbnail size to show'); ?></label>
191
+ <select class="widefat" name="<?php echo $this->get_field_name('thumb_size'); ?>" id="<?php echo $this->get_field_id('thumb_size'); ?>">
192
+ <?php
193
+ foreach ($thumb_sizes as $option) {
194
+ echo '<option value="' . $option . '" id="' . $option . '"', $thumb_size == $option ? ' selected="selected"' : '', '>', $option, '</option>';
195
+ }
196
+ ?>
197
+ </select>
198
+ </p>
199
+ <p>
200
+ <label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e('Order post by:'); ?></label>
201
+ <select class="widefat" name="<?php echo $this->get_field_name('orderby'); ?>" id="<?php echo $this->get_field_id('orderby'); ?>">
202
+ <?php
203
+ foreach ($orderbys as $option) {
204
+ echo '<option value="' . $option . '" id="' . $option . '"', $orderby == $option ? ' selected="selected"' : '', '>', $option, '</option>';
205
+ }
206
+ ?>
207
+ </select>
208
+ </p>
209
+ <p>
210
+ <label for="<?php echo $this->get_field_id('order'); ?>"><?php _e('Order:'); ?></label>
211
+ <select class="widefat" name="<?php echo $this->get_field_name('order'); ?>" id="<?php echo $this->get_field_id('order'); ?>">
212
+ <?php
213
+ foreach ($orders as $option) {
214
+ echo '<option value="' . $option . '" id="' . $option . '"', $order == $option ? ' selected="selected"' : '', '>', $option, '</option>';
215
+ }
216
+ ?>
217
+ </select>
218
+ <br /><span style="padding-top:3px;" class="description"><?php _e('ASC = 1,2,3 or A,B,C<br />DESC = 3,2,1 or C,B,A'); ?></span>
219
+ </p>
220
+ <p>
221
+ <label for="<?php echo $this->get_field_id('template'); ?>"><?php _e('Template filename:'); ?></label>
222
+ <input id="<?php echo $this->get_field_id('template'); ?>" name="<?php echo $this->get_field_name('template'); ?>" type="text" value="<?php echo $template; ?>" />
223
+ <br /><span style="padding-top:3px;" class="description"><?php _e('Template filename without extension.'); ?></span>
224
+ </p>
225
+ <?php
226
+ }
227
+
228
+ /**
229
+ * Loads theme files in appropriate hierarchy: 1) child theme,
230
+ * 2) parent template, 3) plugin resources. will look in the flexible-posts-widget/
231
+ * directory in a theme and the views/ directory in the plugin
232
+ *
233
+ * Function generously borrowed from the amazing image-widget
234
+ * by Matt Wiebe at Modern Tribe, Inc.
235
+ * http://wordpress.org/extend/plugins/image-widget/
236
+ *
237
+ * @param string $template template file to search for
238
+ * @return template path
239
+ **/
240
+
241
+ function getTemplateHierarchy($template) {
242
+ // whether or not .php was added
243
+ $template_slug = rtrim($template, '.php');
244
+ $template = $template_slug . '.php';
245
+
246
+ if ( $theme_file = locate_template(array('flexible-posts-widget/'.$template)) ) {
247
+ $file = $theme_file;
248
+ } else {
249
+ $file = 'views/' . $template;
250
+ }
251
+ //return apply_filters( 'dpe_template_flexible-posts_'.$template, $file); // - Maybe we'll add this in the future
252
+ return $file;
253
+ }
254
+
255
+
256
+ } // class DPE_Flexible_Posts_Widget
257
+
258
+ ?>
readme.txt ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Flexible Posts Widget ===
2
+ Author: David Paul Ellenwood
3
+ Author URI: http://dpedesign.com
4
+ Plugin URI: http://wordpress.org/extend/plugins/flexible-posts-widget/
5
+ Tags: widget, widgets, posts, recent posts, thumbnails, custom post types, custom taxonomies
6
+ Contributors: David Paul Ellenwood
7
+ Requires at least: 3.2
8
+ Tested up to: 3.3.1
9
+ Stable tag: 1.0
10
+
11
+ An advanced posts display widget with many options: post by taxonomy & term or post type, thumbnails, order & order by, customizable templates
12
+
13
+ == Description ==
14
+
15
+ The default WordPress Recent Posts widget is exceptionally basic. Flexible Posts Widget extends this widget with many per-widget customizable options.
16
+
17
+ Flexible Posts Widget was born as I found myself always needing a simple way to display a selection of posts from any taxonomy or post type as a list of "Recent" or "Related" posts.
18
+
19
+ = Features & options =
20
+
21
+ * Customizable widget title
22
+ * Get posts by either a taxonomy & term *OR* by any available post type.
23
+ * Control the number of posts displayed.
24
+ * Option to display the post thumbnail (feature image).
25
+ * Selectable post thumbnail size from available image sizes.
26
+ * Selectable sort order by (Date, ID, Title, Menu Order, Random) and order (ASC, DESC).
27
+ * The widget's HTML output can be customized by user-defined templates located in the current theme folder.
28
+ * Currently, no added CSS or JavaScripts added to your site. Style the widget how ever you'd like!
29
+
30
+
31
+ == Installation ==
32
+
33
+ 1. Upload the 'fleible-posts-widget' folder to the `/wp-content/plugins/` directory.
34
+ 1. Activate the plugin through the 'Plugins' menu in WordPress.
35
+ 1. Go to 'Appearance' > 'Widgets' and place the widget into a sidebar to begin configuring it.
36
+
37
+ = To use a customized HTML output template =
38
+
39
+ 1. Create a folder called `flexible-posts-widget` in the root of your theme folder.
40
+ 1. Copy `widget.php` from within this plugin's `views` folder into your theme's new `flexible-posts-widget` folder.
41
+ 1. Optional: Rename your theme's `widget.php` template file to a more descriptive name of your choice.
42
+ 1. Go to 'Appearance' > 'Widgets' page in WordPress and configure a new Flexible Posts Widget
43
+ 1. Enter the name of the template file you added to your theme *without* the .php extension in the 'Template Filename' field.
44
+
45
+
46
+ == Other Notes ==
47
+
48
+ = Default vs. Custom Templates =
49
+
50
+ Flexible Posts Widget comes with a default template for the widget output. If you would like to alter the widget display code, create a new folder called `flexible-posts-widget` in your template directory and copy over the "views/widget.php" file.
51
+
52
+ Edit the new file in your theme to your desired HTML layout. Please do not edit the one in the plugin folder as that will cause conflicts when you update the plugin to the latest release.
53
+
54
+ = Wish List =
55
+
56
+ Plugin updates & future features list
57
+
58
+ * Fix debug notices for undefined indexes on widgets admin screen.
59
+ * Dynamically populate available terms based on a selected taxonomy.
60
+ * Make the "Get Posts By" section selectable and only show the chosen method: Taxonomy & Term or Post Type.
61
+ * Dynamically populate the "Template Filename" field based on the templates available.
62
+ * Add default styles for the widget display & an option to load or not load them (?)
63
+
64
+
65
+ == Frequently Asked Questions ==
66
+
67
+ = Questions, Support & Bug Reports =
68
+ To get answers to your questions, request help or submit a bug report, please visit the forum: http://wordpress.org/tags/flexible-posts-widget/
69
+
70
+
71
+ == Screenshots ==
72
+
73
+ 1. Flexible Posts Widget admin screen.
74
+ 1. Example Flexible Posts Widget showing the post thumbnail and title wrapped in a link to the post.
75
+
76
+
77
+
78
+ == Changelog ==
79
+
80
+ = 1.0 =
81
+ * First public release
screenshot-1.png ADDED
Binary file
screenshot-2.png ADDED
Binary file
views/widget.php ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Flexible Posts Widget: Default widget template
4
+ */
5
+
6
+ // Block direct requests
7
+ if ( !defined('ABSPATH') )
8
+ die('-1');
9
+
10
+ echo $before_widget;
11
+
12
+ if ( $title )
13
+ echo $before_title . $title . $after_title;
14
+
15
+ if( $flexible_posts->have_posts() ):
16
+ ?>
17
+ <ul class="dpe-flexible-posts">
18
+ <?php while ( $flexible_posts->have_posts() ) : $flexible_posts->the_post(); ?>
19
+ <li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
20
+ <a href="<?php echo the_permalink(); ?>">
21
+ <?php
22
+ if($thumbnail == true)
23
+ the_post_thumbnail($thumb_size);
24
+ ?>
25
+ <h4 class="title"><?php the_title(); ?></h4>
26
+ </a>
27
+ </li>
28
+ <?php endwhile; ?>
29
+ </ul><!-- .dpe-flexible-posts -->
30
+ <?php
31
+ echo $after_widget;
32
+
33
+ else: ?>
34
+ <div class="no-posts dpe-flexible-posts">
35
+ <p>No post found</p>
36
+ </div>
37
+ <?php
38
+ endif; // End have_posts()
39
+ ?>