| 1 |
<?php |
| 2 |
/** |
| 3 |
* Posts Widget |
| 4 |
* |
| 5 |
* @package Essential_Widgets |
| 6 |
*/ |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Makes a custom Widget for Displaying About |
| 11 |
* |
| 12 |
* Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets |
| 13 |
* |
| 14 |
* @package Essential_Widgets |
| 15 |
*/ |
| 16 |
class EW_Posts extends WP_Widget { |
| 17 |
|
| 18 |
/** |
| 19 |
* Holds widget settings defaults, populated in constructor. |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
protected $defaults; |
| 24 |
|
| 25 |
function __construct() { |
| 26 |
|
| 27 |
$this->defaults = array( |
| 28 |
'title' => esc_attr__( 'Posts', 'essential-widgets' ), |
| 29 |
'post_type' => array( 'post' ), |
| 30 |
'order' => 'DESC', |
| 31 |
'orderby' => 'date', |
| 32 |
'number' => 10, |
| 33 |
'show_date' => false, |
| 34 |
'show_author' => false |
| 35 |
); |
| 36 |
|
| 37 |
$widget_ops = array( |
| 38 |
'classname' => 'essential-widgets ew-posts ewposts', |
| 39 |
'description' => esc_html__( 'Displays a list of posts.', 'essential-widgets' ), |
| 40 |
); |
| 41 |
|
| 42 |
$control_ops = array( |
| 43 |
'id_base' => 'ew-posts', |
| 44 |
); |
| 45 |
|
| 46 |
parent::__construct( |
| 47 |
'ew-posts', // Base ID |
| 48 |
esc_html__( 'EW: Posts', 'essential-widgets' ), // Name |
| 49 |
$widget_ops, |
| 50 |
$control_ops |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Displays the widget control options in the Widgets admin screen. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* @access public |
| 59 |
* @param array $instance |
| 60 |
* @param void |
| 61 |
*/ |
| 62 |
public function form( $instance ) { |
| 63 |
|
| 64 |
// Merge the user-selected arguments with the defaults. |
| 65 |
$instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 66 |
|
| 67 |
// <select> element options. |
| 68 |
$types = get_post_types( array( 'public' => true ), 'objects' ); |
| 69 |
|
| 70 |
$order = array( |
| 71 |
'ASC' => esc_attr__( 'Ascending', 'essential-widgets' ), |
| 72 |
'DESC' => esc_attr__( 'Descending', 'essential-widgets' ) |
| 73 |
); |
| 74 |
|
| 75 |
$orderby = array( |
| 76 |
'author' => esc_attr__( 'Author', 'essential-widgets' ), |
| 77 |
'name' => esc_attr__( 'Slug', 'essential-widgets' ), |
| 78 |
'none' => esc_attr__( 'None', 'essential-widgets' ), |
| 79 |
'type' => esc_attr__( 'Type', 'essential-widgets' ), |
| 80 |
'date' => esc_attr__( 'Date', 'essential-widgets' ), |
| 81 |
'ID' => esc_attr__( 'ID', 'essential-widgets' ), |
| 82 |
'modified' => esc_attr__( 'Date (Modified)', 'essential-widgets' ), |
| 83 |
'parent' => esc_attr__( 'Parent', 'essential-widgets' ), |
| 84 |
'comment_count' => esc_attr__( 'Comment Count', 'essential-widgets' ), |
| 85 |
'menu_order' => esc_attr__( 'Menu Order', 'essential-widgets' ), |
| 86 |
'title' => esc_attr__( 'Title', 'essential-widgets' ) |
| 87 |
); ?> |
| 88 |
|
| 89 |
<p> |
| 90 |
<label> |
| 91 |
<?php esc_html_e( 'Title:', 'essential-widgets' ); ?> |
| 92 |
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" placeholder="<?php echo esc_attr( $this->defaults['title'] ); ?>" /> |
| 93 |
</label> |
| 94 |
</p> |
| 95 |
|
| 96 |
<?php esc_html_e( 'Post Type:', 'essential-widgets' ); ?> |
| 97 |
|
| 98 |
<div class="wp-tab-panel"> |
| 99 |
<ul> |
| 100 |
<?php foreach ( $types as $type ) : ?> |
| 101 |
|
| 102 |
<li> |
| 103 |
<label> |
| 104 |
<input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>[]" value="<?php echo esc_attr( $type->name ); ?>" <?php checked( in_array( $type->name, (array)$instance['post_type'] ) ); ?> /> |
| 105 |
<?php echo esc_html( $type->labels->singular_name ); ?> |
| 106 |
</label> |
| 107 |
</li> |
| 108 |
|
| 109 |
<?php endforeach; ?> |
| 110 |
</ul> |
| 111 |
</div> |
| 112 |
|
| 113 |
<p> |
| 114 |
<label> |
| 115 |
<?php esc_html_e( 'Number:', 'essential-widgets' ); ?> |
| 116 |
<input type="number" min="1" size="3" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" value="<?php echo esc_attr( $instance['number'] ); ?>" placeholder="<?php echo esc_attr( $this->defaults['number'] ); ?>" /> |
| 117 |
</label> |
| 118 |
</p> |
| 119 |
|
| 120 |
<p> |
| 121 |
<label> |
| 122 |
<?php esc_html_e( 'Order:', 'essential-widgets' ); ?> |
| 123 |
|
| 124 |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>"> |
| 125 |
|
| 126 |
<?php foreach ( $order as $option_value => $option_label ) : ?> |
| 127 |
|
| 128 |
<option value="<?php echo $option_value; ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo $option_label; ?></option> |
| 129 |
|
| 130 |
<?php endforeach; ?> |
| 131 |
|
| 132 |
</select> |
| 133 |
</label> |
| 134 |
</p> |
| 135 |
|
| 136 |
<p> |
| 137 |
<label> |
| 138 |
<?php esc_html_e( 'Order By:', 'essential-widgets' ); ?> |
| 139 |
|
| 140 |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>"> |
| 141 |
|
| 142 |
<?php foreach ( $orderby as $option_value => $option_label ) : ?> |
| 143 |
|
| 144 |
<option value="<?php echo $option_value; ?>" <?php selected( $instance['orderby'], $option_value ); ?>><?php echo $option_label; ?></option> |
| 145 |
|
| 146 |
<?php endforeach; ?> |
| 147 |
|
| 148 |
</select> |
| 149 |
</label> |
| 150 |
</p> |
| 151 |
|
| 152 |
<p> |
| 153 |
<label> |
| 154 |
<input type="checkbox" <?php checked( $instance['show_date'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'show_date' ) ); ?>" /> |
| 155 |
<?php esc_html_e( 'Display post date?', 'essential-widgets' ); ?> |
| 156 |
</label> |
| 157 |
</p> |
| 158 |
|
| 159 |
<p> |
| 160 |
<label> |
| 161 |
<input type="checkbox" <?php checked( $instance['show_author'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'show_author' ) ); ?>" /> |
| 162 |
<?php esc_html_e( 'Display post author?', 'essential-widgets' ); ?> |
| 163 |
</label> |
| 164 |
</p> |
| 165 |
<?php } |
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* update the particular instant |
| 170 |
* |
| 171 |
* This function should check that $new_instance is set correctly. |
| 172 |
* The newly calculated value of $instance should be returned. |
| 173 |
* If "false" is returned, the instance won't be saved/updated. |
| 174 |
* |
| 175 |
* $new_instance New settings for this instance as input by the user via form() |
| 176 |
* $old_instance Old settings for this instance |
| 177 |
* Settings to save or bool false to cancel saving |
| 178 |
*/ |
| 179 |
function update( $new_instance, $old_instance ) { |
| 180 |
|
| 181 |
$instance = $old_instance; |
| 182 |
|
| 183 |
// Sanitize title. |
| 184 |
$instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 185 |
|
| 186 |
// Sanitize key. |
| 187 |
$instance['post_type'] = array_map( 'sanitize_key', $new_instance['post_type'] ); |
| 188 |
|
| 189 |
// Whitelist options. |
| 190 |
$order = array( 'ASC', 'DESC' ); |
| 191 |
$orderby = array( 'author', 'name', 'none', 'type', 'date', 'ID', 'modified', 'parent', 'comment_count', 'menu_order', 'title' ); |
| 192 |
|
| 193 |
$instance['order'] = in_array( $new_instance['order'], $order ) ? $new_instance['order'] : 'DESC'; |
| 194 |
$instance['orderby'] = in_array( $new_instance['orderby'], $orderby ) ? $new_instance['orderby'] : 'date'; |
| 195 |
|
| 196 |
// Integers. |
| 197 |
$instance['number'] = intval( $new_instance['number'] ); |
| 198 |
|
| 199 |
// Checkboxes. |
| 200 |
$instance['show_date'] = isset( $new_instance['show_date'] ) ? 1 : 0; |
| 201 |
$instance['show_author'] = isset( $new_instance['show_author'] ) ? 1 : 0; |
| 202 |
|
| 203 |
// Return sanitized options. |
| 204 |
return $instance; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Displays the Widget in the front-end. |
| 209 |
* |
| 210 |
* $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 211 |
* $instance The settings for the particular instance of the widget |
| 212 |
*/ |
| 213 |
function widget( $args, $instance ) { |
| 214 |
|
| 215 |
// Set the $args for wp_get_archives() to the $instance array. |
| 216 |
$instance = wp_parse_args( $instance, $this->defaults ); |
| 217 |
|
| 218 |
$loop = new \WP_Query( |
| 219 |
array( |
| 220 |
'posts_per_page' => $instance['number'], |
| 221 |
'post_type' => $instance['post_type'], |
| 222 |
'order' => $instance['order'], |
| 223 |
'orderby' => $instance['orderby'], |
| 224 |
'no_found_rows' => true, |
| 225 |
'post_status' => 'publish', |
| 226 |
'ignore_sticky_posts' => true, |
| 227 |
) |
| 228 |
); |
| 229 |
|
| 230 |
if ( $loop->have_posts() ) : ?> |
| 231 |
<?php echo $args['before_widget']; ?> |
| 232 |
|
| 233 |
<?php |
| 234 |
if ( ! empty( $instance['title'] ) ) |
| 235 |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title']; |
| 236 |
?> |
| 237 |
|
| 238 |
<ul> |
| 239 |
|
| 240 |
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?> |
| 241 |
|
| 242 |
<li> |
| 243 |
<a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a> |
| 244 |
|
| 245 |
<?php if ( $instance['show_author'] ) : ?> |
| 246 |
<span class="post-author"><?php echo ' by ' . get_the_author(); ?></span> |
| 247 |
<?php endif; ?> |
| 248 |
|
| 249 |
<?php if ( $instance['show_date'] ) : ?> |
| 250 |
<span class="post-date"><?php echo get_the_date(); ?></span> |
| 251 |
<?php endif; ?> |
| 252 |
</li> |
| 253 |
|
| 254 |
<?php endwhile; ?> |
| 255 |
|
| 256 |
</ul> |
| 257 |
|
| 258 |
<?php echo $args['after_widget']; ?> |
| 259 |
|
| 260 |
<?php wp_reset_postdata(); |
| 261 |
|
| 262 |
endif; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Intiate About_Widget Class. |
| 268 |
* |
| 269 |
* @since 1.0.0 |
| 270 |
*/ |
| 271 |
function ew_posts_register() { |
| 272 |
register_widget( 'EW_Posts' ); |
| 273 |
} |
| 274 |
add_action( 'widgets_init', 'ew_posts_register' ); |
| 275 |
|