__( 'Display popular posts', 'where-did-they-go-from-here' ),
'customize_selective_refresh' => true,
'classname' => 'tptn_posts_list_widget',
)
);
add_action( 'wp_enqueue_scripts', array( $this, 'front_end_styles' ) );
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$limit = isset( $instance['limit'] ) ? esc_attr( $instance['limit'] ) : '';
$offset = isset( $instance['offset'] ) ? esc_attr( $instance['offset'] ) : '';
$disp_list_count = isset( $instance['disp_list_count'] ) ? esc_attr( $instance['disp_list_count'] ) : '';
$show_excerpt = isset( $instance['show_excerpt'] ) ? esc_attr( $instance['show_excerpt'] ) : '';
$show_author = isset( $instance['show_author'] ) ? esc_attr( $instance['show_author'] ) : '';
$show_date = isset( $instance['show_date'] ) ? esc_attr( $instance['show_date'] ) : '';
$post_thumb_op = isset( $instance['post_thumb_op'] ) ? esc_attr( $instance['post_thumb_op'] ) : 'text_only';
$thumb_height = isset( $instance['thumb_height'] ) ? esc_attr( $instance['thumb_height'] ) : '';
$thumb_width = isset( $instance['thumb_width'] ) ? esc_attr( $instance['thumb_width'] ) : '';
$daily = isset( $instance['daily'] ) ? esc_attr( $instance['daily'] ) : 'overall';
$daily_range = isset( $instance['daily_range'] ) ? esc_attr( $instance['daily_range'] ) : '';
$hour_range = isset( $instance['hour_range'] ) ? esc_attr( $instance['hour_range'] ) : '';
$include_categories = isset( $instance['include_categories'] ) ? esc_attr( $instance['include_categories'] ) : '';
$include_cat_ids = isset( $instance['include_cat_ids'] ) ? esc_attr( $instance['include_cat_ids'] ) : '';
$include_post_ids = isset( $instance['include_post_ids'] ) ? esc_attr( $instance['include_post_ids'] ) : '';
// Parse the Post types.
$post_types = array();
if ( isset( $instance['post_types'] ) ) {
$post_types = $instance['post_types'];
parse_str( $post_types, $post_types ); // Save post types in $post_types variable.
}
$wp_post_types = get_post_types(
array(
'public' => true,
)
);
$posts_types_inc = array_intersect( $wp_post_types, $post_types );
?>
:
:
true,
)
);
$post_types = ( isset( $new_instance['post_types'] ) ) ? $new_instance['post_types'] : array();
$post_types = array_intersect( $wp_post_types, $post_types );
$instance['post_types'] = http_build_query( $post_types, '', '&' );
// Save include_categories.
$include_categories = array_unique( str_getcsv( $new_instance['include_categories'] ) );
foreach ( $include_categories as $cat_name ) {
$cat = get_term_by( 'name', $cat_name, 'category' );
if ( isset( $cat->term_taxonomy_id ) ) {
$include_cat_ids[] = $cat->term_taxonomy_id;
$include_cat_names[] = $cat->name;
}
}
$instance['include_cat_ids'] = isset( $include_cat_ids ) ? join( ',', $include_cat_ids ) : '';
$instance['include_categories'] = isset( $include_cat_names ) ? tptn_str_putcsv( $include_cat_names ) : '';
/**
* Filters Update widget options array.
*
* @since 2.0.0
*
* @param array $instance Widget options array
*/
return apply_filters( 'tptn_widget_options_update', $instance );
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
global $post;
// Get the post meta.
if ( isset( $post ) ) {
$tptn_post_meta = get_post_meta( $post->ID, 'tptn_post_meta', true );
if ( isset( $tptn_post_meta['disable_here'] ) && ( $tptn_post_meta['disable_here'] ) ) {
return;
}
}
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? wp_strip_all_tags( tptn_get_option( 'title' ) ) : $instance['title'] );
$limit = isset( $instance['limit'] ) ? $instance['limit'] : tptn_get_option( 'limit' );
if ( empty( $limit ) ) {
$limit = tptn_get_option( 'limit' );
}
$offset = isset( $instance['offset'] ) ? $instance['offset'] : 0;
$daily_range = ( empty( $instance['daily_range'] ) ) ? tptn_get_option( 'daily_range' ) : $instance['daily_range'];
$hour_range = ( empty( $instance['hour_range'] ) ) ? tptn_get_option( 'hour_range' ) : $instance['hour_range'];
$daily = ( isset( $instance['daily'] ) && ( 'daily' === $instance['daily'] ) ) ? true : false;
$output = $args['before_widget'];
$output .= $args['before_title'] . $title . $args['after_title'];
$post_thumb_op = isset( $instance['post_thumb_op'] ) ? esc_attr( $instance['post_thumb_op'] ) : 'text_only';
$thumb_height = ( isset( $instance['thumb_height'] ) && '' !== $instance['thumb_height'] ) ? absint( $instance['thumb_height'] ) : tptn_get_option( 'thumb_height' );
$thumb_width = ( isset( $instance['thumb_width'] ) && '' !== $instance['thumb_width'] ) ? absint( $instance['thumb_width'] ) : tptn_get_option( 'thumb_width' );
$disp_list_count = isset( $instance['disp_list_count'] ) ? esc_attr( $instance['disp_list_count'] ) : '';
$show_excerpt = isset( $instance['show_excerpt'] ) ? esc_attr( $instance['show_excerpt'] ) : '';
$show_author = isset( $instance['show_author'] ) ? esc_attr( $instance['show_author'] ) : '';
$show_date = isset( $instance['show_date'] ) ? esc_attr( $instance['show_date'] ) : '';
$post_types = isset( $instance['post_types'] ) ? $instance['post_types'] : tptn_get_option( 'post_types' );
$include_cat_ids = isset( $instance['include_cat_ids'] ) ? esc_attr( $instance['include_cat_ids'] ) : '';
$include_post_ids = isset( $instance['include_post_ids'] ) ? esc_attr( $instance['include_post_ids'] ) : '';
$arguments = array(
'is_widget' => 1,
'instance_id' => $this->number,
'heading' => 0,
'limit' => $limit,
'offset' => $offset,
'daily' => $daily,
'daily_range' => $daily_range,
'hour_range' => $hour_range,
'show_excerpt' => $show_excerpt,
'show_author' => $show_author,
'show_date' => $show_date,
'post_thumb_op' => $post_thumb_op,
'thumb_height' => $thumb_height,
'thumb_width' => $thumb_width,
'disp_list_count' => $disp_list_count,
'post_types' => $post_types,
'include_cat_ids' => $include_cat_ids,
'include_post_ids' => $include_post_ids,
);
/**
* Filters arguments passed to tptn_pop_posts for the widget.
*
* @since 2.0.0
*
* @param array $arguments Widget options array
*/
$arguments = apply_filters( 'tptn_widget_options', $arguments );
$output .= tptn_pop_posts( $arguments );
$output .= $args['after_widget'];
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Add styles to the front end if the widget is active.
*
* @since 2.3.0
*/
public function front_end_styles() {
if ( ! 'left_thumbs' === tptn_get_option( 'tptn_styles' ) ) {
return;
}
// We need to process all instances because this function gets to run only once.
$widget_settings = get_option( $this->option_name );
foreach ( (array) $widget_settings as $instance => $options ) {
// Identify instance.
$widget_id = "{$this->id_base}-{$instance}";
// Check if it's our instance.
if ( ! is_active_widget( false, $widget_id, $this->id_base, true ) ) {
continue; // Not active.
}
$thumb_height = ( isset( $options['thumb_height'] ) && '' !== $options['thumb_height'] ) ? absint( $options['thumb_height'] ) : tptn_get_option( 'thumb_height' );
$thumb_width = ( isset( $options['thumb_width'] ) && '' !== $options['thumb_width'] ) ? absint( $options['thumb_width'] ) : tptn_get_option( 'thumb_width' );
// Enqueue the custom css for the thumb width and height for this specific widget.
$custom_css = "
.tptn_posts_widget{$instance} img.tptn_thumb {
width: {$thumb_width}px !important;
height: {$thumb_height}px !important;
}
";
wp_add_inline_style( 'tptn-style-left-thumbs', $custom_css );
}
}
}
/**
* Initialise the widget.
*/
function tptn_register_widget() {
register_widget( 'Top_Ten_Widget' );
}
add_action( 'widgets_init', 'tptn_register_widget', 1 );