| 1 |
<?php |
| 2 |
/** |
| 3 |
* Widget Featured Posts |
| 4 |
* |
| 5 |
* @link https://codesupply.co |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Powerkit |
| 9 |
* @subpackage Powerkit/widgets |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Widget Featured Posts |
| 14 |
*/ |
| 15 |
class Powerkit_Featured_Posts_Widget extends WP_Widget { |
| 16 |
|
| 17 |
/** |
| 18 |
* Sets up a new widget instance. |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
|
| 22 |
$this->default_settings = apply_filters( |
| 23 |
'powerkit_widget_posts_settings', array( |
| 24 |
'title' => '', |
| 25 |
'template' => 'list', |
| 26 |
'post_type' => 'post', |
| 27 |
'posts_per_page' => 5, |
| 28 |
'orderby' => 'date', |
| 29 |
'order' => 'desc', |
| 30 |
'time_frame' => '', |
| 31 |
'category' => false, |
| 32 |
'post_meta' => array( 'date' ), |
| 33 |
'post_meta_compact' => false, |
| 34 |
'avoid_duplicate' => false, |
| 35 |
) |
| 36 |
); |
| 37 |
|
| 38 |
$widget_details = array( |
| 39 |
'classname' => 'powerkit_widget_posts', |
| 40 |
'description' => esc_html__( 'Display a list of your featured posts.', 'powerkit' ), |
| 41 |
); |
| 42 |
parent::__construct( 'powerkit_widget_posts', esc_html__( 'Featured Posts', 'powerkit' ), $widget_details ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Outputs the content for the current widget instance. |
| 47 |
* |
| 48 |
* @param array $args Display arguments including 'before_title', 'after_title', |
| 49 |
* 'before_widget', and 'after_widget'. |
| 50 |
* @param array $instance Settings for the current widget instance. |
| 51 |
*/ |
| 52 |
public function widget( $args, $instance ) { |
| 53 |
global $pk_posts; |
| 54 |
global $wp_query; |
| 55 |
|
| 56 |
if ( ! $pk_posts ) { |
| 57 |
$pk_posts = array(); |
| 58 |
} |
| 59 |
|
| 60 |
$params = array_merge( $this->default_settings, $instance ); |
| 61 |
|
| 62 |
if ( in_array( 'category', $params['post_meta'], true ) ) { |
| 63 |
$params['post_meta_category'] = true; |
| 64 |
} else { |
| 65 |
$params['post_meta_category'] = false; |
| 66 |
} |
| 67 |
|
| 68 |
$posts_args = array( |
| 69 |
'post_type' => $params['post_type'], |
| 70 |
'posts_per_page' => $params['posts_per_page'], |
| 71 |
'order' => $params['order'], |
| 72 |
'no_found_rows' => true, |
| 73 |
'post_status' => 'publish', |
| 74 |
'ignore_sticky_posts' => true, |
| 75 |
); |
| 76 |
|
| 77 |
if ( $params['category'] ) { |
| 78 |
$category = $params['category']; |
| 79 |
$posts_args['cat'] = $category; |
| 80 |
} |
| 81 |
|
| 82 |
// Avoid Duplicate. |
| 83 |
if ( $params['avoid_duplicate'] ) { |
| 84 |
$main_posts = array(); |
| 85 |
|
| 86 |
if ( isset( $wp_query->posts ) && $wp_query->posts ) { |
| 87 |
$main_posts = wp_list_pluck( $wp_query->posts, 'ID' ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( $main_posts ) { |
| 91 |
$posts_args['post__not_in'] = array_merge( $main_posts, $pk_posts ); |
| 92 |
} else { |
| 93 |
$posts_args['post__not_in'] = $pk_posts; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// Post order. |
| 98 |
$posts_args['orderby'] = $params['orderby']; |
| 99 |
|
| 100 |
$type_post_views = powerkit_post_views_enabled(); |
| 101 |
|
| 102 |
if ( $type_post_views && ( 'views' === $params['orderby'] || 'post_views' === $params['orderby'] ) ) { |
| 103 |
$posts_args['orderby'] = $type_post_views; |
| 104 |
// Don't hide posts without views. |
| 105 |
$posts_args['views_query']['hide_empty'] = false; |
| 106 |
} |
| 107 |
|
| 108 |
if ( $params['time_frame'] ) { |
| 109 |
$posts_args['date_query'] = array( |
| 110 |
array( |
| 111 |
'column' => 'post_date_gmt', |
| 112 |
'after' => $params['time_frame'] . ' ago', |
| 113 |
), |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
$posts = new WP_Query( apply_filters_ref_array( 'powerkit_widget_featured_posts_args', array( $posts_args, & $params ) ) ); |
| 118 |
|
| 119 |
if ( $posts->have_posts() ) { |
| 120 |
|
| 121 |
// Before Widget. |
| 122 |
echo $args['before_widget']; // XSS. |
| 123 |
|
| 124 |
// Title. |
| 125 |
if ( $params['title'] ) { |
| 126 |
echo $args['before_title'] . apply_filters( 'widget_title', $params['title'], $instance, $this->id_base ) . $args['after_title']; // XSS. |
| 127 |
} |
| 128 |
|
| 129 |
// Template. |
| 130 |
if ( in_array( $params['template'], array( 'list', 'numbered', 'large' ), true ) ) { |
| 131 |
$class = sprintf( 'pk-widget-posts-template-default pk-widget-posts-template-%s', $params['template'] ); |
| 132 |
} else { |
| 133 |
$class = sprintf( 'pk-widget-posts-template-%s', $params['template'] ); |
| 134 |
} |
| 135 |
|
| 136 |
// Number of Posts. |
| 137 |
$class .= sprintf( ' posts-per-page-%s', (int) $params['posts_per_page'] ); |
| 138 |
?> |
| 139 |
|
| 140 |
<div class="widget-body pk-widget-posts <?php echo esc_html( $class ); ?>"> |
| 141 |
<ul> |
| 142 |
<?php |
| 143 |
while ( $posts->have_posts() ) : |
| 144 |
$posts->the_post(); |
| 145 |
|
| 146 |
$pk_posts[] = get_the_ID(); |
| 147 |
?> |
| 148 |
<li class="pk-post-item"> |
| 149 |
<?php powerkit_widget_featured_posts_handler( $params['template'], $posts, $params, $instance ); ?> |
| 150 |
</li> |
| 151 |
<?php endwhile; ?> |
| 152 |
</ul> |
| 153 |
</div> |
| 154 |
|
| 155 |
<?php |
| 156 |
|
| 157 |
// After Widget. |
| 158 |
echo $args['after_widget']; // XSS. |
| 159 |
} |
| 160 |
|
| 161 |
wp_reset_postdata(); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Handles updating settings for the current widget instance. |
| 166 |
* |
| 167 |
* @param array $new_instance New settings for this instance as input by the user via |
| 168 |
* WP_Widget::form(). |
| 169 |
* @param array $old_instance Old settings for this instance. |
| 170 |
* @return array Settings to save or bool false to cancel saving. |
| 171 |
*/ |
| 172 |
public function update( $new_instance, $old_instance ) { |
| 173 |
$instance = $new_instance; |
| 174 |
|
| 175 |
// Post Meta. |
| 176 |
if ( ! isset( $instance['post_meta'] ) ) { |
| 177 |
$instance['post_meta'] = array(); |
| 178 |
} |
| 179 |
|
| 180 |
// Compact Post Meta. |
| 181 |
if ( ! isset( $instance['post_meta_compact'] ) ) { |
| 182 |
$instance['post_meta_compact'] = false; |
| 183 |
} |
| 184 |
|
| 185 |
// Avoid duplicate posts. |
| 186 |
if ( ! isset( $instance['avoid_duplicate'] ) ) { |
| 187 |
$instance['avoid_duplicate'] = false; |
| 188 |
} |
| 189 |
|
| 190 |
return apply_filters( 'powerkit_widget_posts_update', $instance ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Outputs the widget settings form. |
| 195 |
* |
| 196 |
* @param array $instance Current settings. |
| 197 |
*/ |
| 198 |
public function form( $instance ) { |
| 199 |
$params = array_merge( $this->default_settings, $instance ); |
| 200 |
|
| 201 |
$templates = apply_filters( 'powerkit_featured_posts_templates', array() ); |
| 202 |
?> |
| 203 |
<!-- Title --> |
| 204 |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'powerkit' ); ?></label> |
| 205 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $params['title'] ); ?>" /></p> |
| 206 |
|
| 207 |
<?php do_action( 'powerkit_widget_posts_form_before', $this, $params, $instance ); ?> |
| 208 |
|
| 209 |
<!-- Template --> |
| 210 |
<?php if ( $templates ) { ?> |
| 211 |
<p> |
| 212 |
<label for="<?php echo esc_attr( $this->get_field_id( 'template' ) ); ?>"><?php esc_html_e( 'Template:', 'powerkit' ); ?></label> |
| 213 |
<select name="<?php echo esc_attr( $this->get_field_name( 'template' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'template' ) ); ?>" class="widefat"> |
| 214 |
<?php |
| 215 |
foreach ( $templates as $slug => $template ) { |
| 216 |
$name = isset( $template['name'] ) ? $template['name'] : $slug; |
| 217 |
?> |
| 218 |
<option value="<?php echo esc_attr( $slug ); ?>" <?php selected( $params['template'], $slug ); ?>><?php echo esc_html( $name ); ?></option> |
| 219 |
<?php } ?> |
| 220 |
</select> |
| 221 |
</p> |
| 222 |
<?php } ?> |
| 223 |
|
| 224 |
<!-- Post Type --> |
| 225 |
<p> |
| 226 |
<label for="<?php echo esc_attr( $this->get_field_id( 'post_type' ) ); ?>"><?php esc_html_e( 'Post Type', 'powerkit' ); ?>:</label> |
| 227 |
<select name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'post_type' ) ); ?>" class="widefat"> |
| 228 |
<?php |
| 229 |
$post_types = get_post_types( array( |
| 230 |
'publicly_queryable' => 1, |
| 231 |
), 'objects' ); |
| 232 |
|
| 233 |
foreach ( $post_types as $name => $post_type ) { |
| 234 |
?> |
| 235 |
<option value="<?php echo esc_attr( $name ); ?>" <?php selected( $params['post_type'], $name ); ?>><?php echo esc_html( $post_type->labels->name ); ?></option> |
| 236 |
<?php } ?> |
| 237 |
</select> |
| 238 |
</p> |
| 239 |
|
| 240 |
<!-- Number of Posts --> |
| 241 |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'posts_per_page' ) ); ?>"><?php esc_html_e( 'Number of posts:', 'powerkit' ); ?></label> |
| 242 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'posts_per_page' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'posts_per_page' ) ); ?>" type="number" value="<?php echo esc_attr( $params['posts_per_page'] ); ?>" /></p> |
| 243 |
|
| 244 |
<!-- Order by --> |
| 245 |
<p> |
| 246 |
<label for="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>"><?php esc_html_e( 'Order by:', 'powerkit' ); ?></label> |
| 247 |
<select name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" class="widefat"> |
| 248 |
<option value="date" <?php selected( $params['orderby'], 'date' ); ?>><?php esc_html_e( 'Date', 'powerkit' ); ?></option> |
| 249 |
<option value="comment_count" <?php selected( $params['orderby'], 'comment_count' ); ?>><?php esc_html_e( 'Comments', 'powerkit' ); ?></option> |
| 250 |
<option value="rand" <?php selected( $params['orderby'], 'rand' ); ?>><?php esc_html_e( 'Random', 'powerkit' ); ?></option> |
| 251 |
|
| 252 |
<?php if ( powerkit_post_views_enabled() ) { ?> |
| 253 |
<option value="post_views" <?php selected( $params['orderby'], 'post_views' ); ?>><?php esc_html_e( 'Views', 'powerkit' ); ?></option> |
| 254 |
<?php } ?> |
| 255 |
</select> |
| 256 |
</p> |
| 257 |
|
| 258 |
<!-- Order --> |
| 259 |
<p> |
| 260 |
<label for="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>"><?php esc_html_e( 'Order', 'powerkit' ); ?>:</label> |
| 261 |
<select name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>" class="widefat"> |
| 262 |
<option value="desc" <?php selected( $params['order'], 'desc' ); ?>><?php esc_html_e( 'Descending', 'powerkit' ); ?></option> |
| 263 |
<option value="asc" <?php selected( $params['order'], 'asc' ); ?>><?php esc_html_e( 'Ascending', 'powerkit' ); ?></option> |
| 264 |
</select> |
| 265 |
</p> |
| 266 |
|
| 267 |
<!-- Time Frame --> |
| 268 |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'time_frame' ) ); ?>"><?php esc_html_e( 'Time frame:', 'powerkit' ); ?></label> |
| 269 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'time_frame' ) ); ?>" placeholder="<?php esc_html_e( '3 months', 'powerkit' ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'time_frame' ) ); ?>" type="text" value="<?php echo esc_attr( $params['time_frame'] ); ?>" /></p> |
| 270 |
|
| 271 |
<!-- Category --> |
| 272 |
<p> |
| 273 |
<label for="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>"><?php esc_html_e( 'Category:', 'powerkit' ); ?></label> |
| 274 |
<select name="<?php echo esc_attr( $this->get_field_name( 'category' ) ); ?>[]" id="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>" class="widefat" style="height: auto !important;" multiple="multiple" size="8"> |
| 275 |
<?php |
| 276 |
$cat_args = array( |
| 277 |
'hide_empty' => 0, |
| 278 |
'hierarchical' => 1, |
| 279 |
'selected' => (array) $params['category'], |
| 280 |
'walker' => new Powerkit_Add_Posts_Categories_Tree_Walker(), |
| 281 |
); |
| 282 |
|
| 283 |
$allowed_html = array( |
| 284 |
'option' => array( |
| 285 |
'class' => true, |
| 286 |
'value' => true, |
| 287 |
'selected' => true, |
| 288 |
), |
| 289 |
); |
| 290 |
|
| 291 |
echo wp_kses( walk_category_dropdown_tree( get_categories( $cat_args ), 0, $cat_args ), $allowed_html ); |
| 292 |
?> |
| 293 |
</select> |
| 294 |
</p> |
| 295 |
|
| 296 |
<!-- Post meta --> |
| 297 |
<h4><?php esc_html_e( 'Post meta:', 'powerkit' ); ?></h4> |
| 298 |
|
| 299 |
<?php |
| 300 |
$allowed_post_meta = powerkit_allowed_post_meta(); |
| 301 |
|
| 302 |
foreach ( $allowed_post_meta as $key => $caption ) { |
| 303 |
?> |
| 304 |
<p><input id="<?php echo esc_attr( $this->get_field_id( 'post_meta' ) ); ?>-<?php echo esc_attr( $key ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'post_meta' ) ); ?>[]" type="checkbox" value="<?php echo esc_attr( $key ); ?>" <?php checked( in_array( $key, (array) $params['post_meta'], true ) ? true : false ); ?> /> |
| 305 |
<label for="<?php echo esc_attr( $this->get_field_id( 'post_meta' ) ); ?>-<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $caption ); ?></label></p> |
| 306 |
<?php |
| 307 |
} |
| 308 |
?> |
| 309 |
|
| 310 |
<!-- Compact Post Meta --> |
| 311 |
<h4><?php esc_html_e( 'Compact post meta:', 'powerkit' ); ?></h4> |
| 312 |
<p><input id="<?php echo esc_attr( $this->get_field_id( 'post_meta_compact' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'post_meta_compact' ) ); ?>" type="checkbox" <?php checked( (bool) $params['post_meta_compact'] ); ?> /> |
| 313 |
<label for="<?php echo esc_attr( $this->get_field_id( 'post_meta_compact' ) ); ?>"><?php esc_html_e( 'Display compact post meta', 'powerkit' ); ?></label></p> |
| 314 |
|
| 315 |
<!-- Avoid duplicate posts --> |
| 316 |
<h4><?php esc_html_e( 'Avoid duplicate posts:', 'powerkit' ); ?></h4> |
| 317 |
<p><input id="<?php echo esc_attr( $this->get_field_id( 'avoid_duplicate' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'avoid_duplicate' ) ); ?>" type="checkbox" <?php checked( (bool) $params['avoid_duplicate'] ); ?> /> |
| 318 |
<label for="<?php echo esc_attr( $this->get_field_id( 'avoid_duplicate' ) ); ?>"><?php esc_html_e( 'Exclude duplicate posts', 'powerkit' ); ?></label></p> |
| 319 |
|
| 320 |
<?php do_action( 'powerkit_widget_posts_form_after', $this, $params, $instance ); ?> |
| 321 |
|
| 322 |
<?php |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Create HTML dropdown list of Categories. |
| 328 |
*/ |
| 329 |
class Powerkit_Add_Posts_Categories_Tree_Walker extends Walker_CategoryDropdown { |
| 330 |
|
| 331 |
/** |
| 332 |
* Starts the element output. |
| 333 |
* |
| 334 |
* @since 2.1.0 |
| 335 |
* @access public |
| 336 |
* |
| 337 |
* @see Walker::start_el() |
| 338 |
* |
| 339 |
* @param string $output Passed by reference. Used to append additional content. |
| 340 |
* @param object $category Category data object. |
| 341 |
* @param int $depth Depth of category. Used for padding. |
| 342 |
* @param array $args Uses 'selected', 'show_count', and 'value_field' keys, if they exist. |
| 343 |
* See wp_dropdown_categories(). |
| 344 |
* @param int $id Optional. ID of the current category. Default 0 (unused). |
| 345 |
*/ |
| 346 |
public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { |
| 347 |
$pad = $depth > 0 ? '- ' . str_repeat( ' ', $depth * 3 ) : ''; |
| 348 |
$selected = array_map( 'intval', $args['selected'] ); |
| 349 |
$cat_name = apply_filters( 'list_cats', $category->name, $category ); |
| 350 |
|
| 351 |
$output .= sprintf( |
| 352 |
'<option class="level-%1$s" value="%2$s" %4$s>%3$s</option>', |
| 353 |
esc_attr( $depth ), |
| 354 |
esc_attr( $category->term_id ), |
| 355 |
esc_html( $pad . $cat_name ), |
| 356 |
selected( in_array( $category->term_id, $selected, true ), true ) |
| 357 |
); |
| 358 |
$output .= "\n"; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Register Widget |
| 364 |
*/ |
| 365 |
function powerkit_widget_init_featured_posts() { |
| 366 |
register_widget( 'Powerkit_Featured_Posts_Widget' ); |
| 367 |
} |
| 368 |
add_action( 'widgets_init', 'powerkit_widget_init_featured_posts' ); |
| 369 |
|