__( 'Display popular posts', 'where-did-they-go-from-here' ),
'classname' => 'tptn_posts_list_widget',
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
)
);
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$instance['title'] = isset( $instance['title'] ) ? $instance['title'] : '';
$instance['limit'] = isset( $instance['limit'] ) ? $instance['limit'] : '';
$instance['offset'] = isset( $instance['offset'] ) ? $instance['offset'] : '';
$instance['disp_list_count'] = isset( $instance['disp_list_count'] ) ? $instance['disp_list_count'] : '';
$instance['show_excerpt'] = isset( $instance['show_excerpt'] ) ? $instance['show_excerpt'] : '';
$instance['show_author'] = isset( $instance['show_author'] ) ? $instance['show_author'] : '';
$instance['show_date'] = isset( $instance['show_date'] ) ? $instance['show_date'] : '';
$instance['post_thumb_op'] = isset( $instance['post_thumb_op'] ) ? $instance['post_thumb_op'] : 'text_only';
$instance['thumb_height'] = isset( $instance['thumb_height'] ) ? $instance['thumb_height'] : '';
$instance['thumb_width'] = isset( $instance['thumb_width'] ) ? $instance['thumb_width'] : '';
$instance['daily'] = isset( $instance['daily'] ) ? $instance['daily'] : 'overall';
$instance['daily_range'] = isset( $instance['daily_range'] ) ? $instance['daily_range'] : '';
$instance['hour_range'] = isset( $instance['hour_range'] ) ? $instance['hour_range'] : '';
$instance['include_cat_ids'] = isset( $instance['include_cat_ids'] ) ? $instance['include_cat_ids'] : '';
$instance['include_post_ids'] = isset( $instance['include_post_ids'] ) ? $instance['include_post_ids'] : '';
// Parse the Post types.
$post_types = array();
// If post_types is empty or contains a query string then use parse_str else consider it comma-separated.
if ( ! empty( $instance['post_types'] ) && false === strpos( $instance['post_types'], '=' ) ) {
$post_types = explode( ',', $instance['post_types'] );
} elseif ( ! empty( $instance['post_types'] ) ) {
parse_str( $instance['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 );
?>
id="get_field_id( 'disp_list_count' ) ); ?>" name="get_field_name( 'disp_list_count' ) ); ?>" />
id="get_field_id( 'show_excerpt' ) ); ?>" name="get_field_name( 'show_excerpt' ) ); ?>" />
id="get_field_id( 'show_author' ) ); ?>" name="get_field_name( 'show_author' ) ); ?>" />
id="get_field_id( 'show_date' ) ); ?>" name="get_field_name( 'show_date' ) ); ?>" />
true,
)
);
$post_types = isset( $new_instance['post_types'] ) ? $new_instance['post_types'] : array();
$post_types = array_intersect( $wp_post_types, (array) $post_types );
$instance['post_types'] = implode( ',', $post_types );
// Save include_categories.
$include_categories = wp_parse_id_list( $new_instance['include_cat_ids'] );
foreach ( $include_categories as $cat_name ) {
$cat = get_term_by( 'term_taxonomy_id', $cat_name );
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 ) ? \WebberZone\Top_Ten\Util\Helpers::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;
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = (string) $this->id;
}
if ( Display::exclude_on( $post, $args ) ) {
return;
}
$this->front_end_styles( $args, $instance );
$default_title = wp_strip_all_tags( tptn_get_option( 'title' ) );
$title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$limit = ! empty( $instance['limit'] ) ? $instance['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;
$post_thumb_op = isset( $instance['post_thumb_op'] ) ? $instance['post_thumb_op'] : 'text_only';
$thumb_height = ! empty( $instance['thumb_height'] ) ? absint( $instance['thumb_height'] ) : tptn_get_option( 'thumb_height' );
$thumb_width = ! empty( $instance['thumb_width'] ) ? absint( $instance['thumb_width'] ) : tptn_get_option( 'thumb_width' );
$disp_list_count = isset( $instance['disp_list_count'] ) ? $instance['disp_list_count'] : '';
$show_excerpt = isset( $instance['show_excerpt'] ) ? $instance['show_excerpt'] : '';
$show_author = isset( $instance['show_author'] ) ? $instance['show_author'] : '';
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : '';
$post_types = ! empty( $instance['post_types'] ) ? $instance['post_types'] : tptn_get_option( 'post_types' );
$include_cat_ids = isset( $instance['include_cat_ids'] ) ? $instance['include_cat_ids'] : '';
$include_post_ids = isset( $instance['include_post_ids'] ) ? $instance['include_post_ids'] : '';
$output = $args['before_widget'];
$output .= $args['before_title'] . $title . $args['after_title'];
$arguments = array(
'is_widget' => 1,
'instance_id' => $args['widget_id'],
'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,
'thumb_size' => array( $thumb_width, $thumb_height ),
'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 .= Display::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
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function front_end_styles( $args, $instance ) {
$style_array = \WebberZone\Top_Ten\Frontend\Styles_Handler::get_style();
if ( ! empty( $style_array['name'] ) ) {
$style = $style_array['name'];
$extra_css = $style_array['extra_css'];
wp_register_style(
"tptn-style-{$style}-{$args['widget_id']}",
false,
array( "tptn-style-{$style}" ),
TOP_TEN_VERSION
);
$thumb_height = ( ! empty( $instance['thumb_height'] ) ) ? absint( $instance['thumb_height'] ) : \tptn_get_option( 'thumb_height' );
$thumb_width = ( ! empty( $instance['thumb_width'] ) ) ? absint( $instance['thumb_width'] ) : \tptn_get_option( 'thumb_width' );
$width_value = $thumb_width ? $thumb_width : 1;
$height_value = $thumb_height ? $thumb_height : 1;
$aspect_ratio = sprintf( '%d / %d', $width_value, $height_value );
// Scope thumbnail sizing to this widget instance so layout CSS can react to it.
$extra_css .= "
.tptn_posts_widget-{$args['widget_id']} {
--tptn-thumb-width: {$thumb_width}px;
--tptn-thumb-height: {$thumb_height}px;
--tptn-thumb-aspect-ratio: {$aspect_ratio};
}
.tptn_posts_widget-{$args['widget_id']} li > a.tptn_link {
width: min( var(--tptn-thumb-width), 100% );
max-width: 100%;
}
.tptn_posts_widget-{$args['widget_id']} img.tptn_thumb {
width: min( var(--tptn-thumb-width), 100% ) !important;
height: var(--tptn-thumb-height) !important;
max-width: 100%;
max-height: var(--tptn-thumb-height);
aspect-ratio: var(--tptn-thumb-aspect-ratio);
object-fit: cover;
}
";
wp_enqueue_style( "tptn-style-{$style}-{$args['widget_id']}" );
wp_add_inline_style( "tptn-style-{$style}-{$args['widget_id']}", $extra_css );
}
}
}