: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @author Justin Tadlock <justin@justintadlock.com>
* @copyright Copyright (c) 2010 - 2014, Justin Tadlock
* @link http://themehybrid.com/plugins/entry-views
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
class EV_Widget_Entry_Views extends WP_Widget {
* Default arguments for the widget settings.
public $defaults = array();
* Set up the widget's unique name, ID, class, description, and other options.
/* Set up the widget options. */
'classname' => 'widget-entry-views',
'description' => esc_html__( 'Display posts based on their view count.', 'entry-views' )
/* Set up the widget control options. */
$control_options = array(
__( 'Entry Views', 'entry-views' ),
'title' => esc_attr__( 'Views', 'entry-views' ),
'show_view_count' => true
* Outputs the widget based on the arguments input through the widget controls.
function widget( $sidebar, $instance ) {
/* Set the $args for wp_get_archives() to the $instance array. */
$args = wp_parse_args( $instance, $this->defaults );
/* Output the sidebar's $before_widget wrapper. */
echo $sidebar['before_widget'];
/* If a title was input by the user, display it. */
if ( !empty( $args['title'] ) )
echo $sidebar['before_title'] . apply_filters( 'widget_title', $args['title'], $instance, $this->id_base ) . $sidebar['after_title'];
/* Query the most/least viewed posts. */
'post_type' => $args['post_type'],
'posts_per_page' => $args['posts_per_page'],
'order' => $args['order'],
'orderby' => 'meta_value_num',
'ignore_sticky_posts' => true,
'meta_key' => ev_get_meta_key()
if ( $loop->have_posts() ) : ?>
<ul class="ev-entry-views-list">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<a href="' . get_permalink() . '">', '</a>' ); ?>
<?php if ( true == $args['show_view_count'] ) : ?>
<?php ev_post_views( array( 'text' => '(%s)', 'wrap' => '<span class="entry-view-count">%2$s</span>' ) ); ?>
</ul><!-- .ev-entry-views-list -->
/* Close the sidebar's widget wrapper. */
echo $sidebar['after_widget'];
* The update callback for the widget control options. This method is used to sanitize and/or
* validate the options before saving them into the database.
* @param array $new_instance
* @param array $old_instance
function update( $new_instance, $old_instance ) {
$instance['title'] = strip_tags( $new_instance['title'] );
/* Array map sanitize key. */
$instance['post_type'] = array_map( 'sanitize_key', $new_instance['post_type'] );
$order = array( 'ASC', 'DESC' );
$instance['order'] = in_array( $new_instance['order'], $order ) ? $new_instance['order'] : 'DESC';
$instance['posts_per_page'] = absint( $new_instance['posts_per_page'] );
$instance['show_view_count'] = isset( $new_instance['show_view_count'] ) ? 1 : 0;
/* Return sanitized options. */
* Displays the widget control options in the Widgets admin screen.
function form( $instance ) {
/* Merge the user-selected arguments with the defaults. */
$instance = wp_parse_args( (array) $instance, $this->defaults );
$_post_types = get_post_types( array( 'public' => true ), 'objects' );
foreach ( $_post_types as $_post_type ) {
if ( post_type_supports( $_post_type->name, 'entry-views' ) )
$post_types[] = $_post_type;
/* Create an array of order options. */
'ASC' => esc_attr__( 'Ascending', 'entry-views' ),
'DESC' => esc_attr__( 'Descending', 'entry-views' )
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'entry-views' ); ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" placeholder="<?php echo esc_attr( $this->defaults['title'] ); ?>" />
<label for="<?php echo $this->get_field_id( 'post_type' ); ?>"><?php _e( 'Post Type:', 'entry-views' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id( 'post_type' ); ?>" name="<?php echo $this->get_field_name( 'post_type' ); ?>[]" size="4" multiple="multiple">
<?php foreach ( $post_types as $post_type ) { ?>
<option value="<?php echo esc_attr( $post_type->name ); ?>" <?php selected( in_array( $post_type->name, (array)$instance['post_type'] ) ); ?>><?php echo esc_html( $post_type->labels->singular_name ); ?></option>
<label for="<?php echo $this->get_field_id( 'posts_per_page' ); ?>"><?php _e( 'Limit:', 'entry-views' ); ?></label>
<input type="number" class="widefat code" size="5" min="1" id="<?php echo $this->get_field_id( 'posts_per_page' ); ?>" name="<?php echo $this->get_field_name( 'posts_per_page' ); ?>" value="<?php echo esc_attr( $instance['posts_per_page'] ); ?>" placeholder="10" />
<label for="<?php echo $this->get_field_id( 'order' ); ?>"><?php _e( 'Order:', 'entry-views' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id( 'order' ); ?>" name="<?php echo $this->get_field_name( 'order' ); ?>">
<?php foreach ( $order as $option_value => $option_label ) { ?>
<option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option>
<label for="<?php echo $this->get_field_id( 'show_view_count' ); ?>">
<input class="checkbox" type="checkbox" <?php checked( $instance['show_view_count'], true ); ?> id="<?php echo $this->get_field_id( 'show_view_count' ); ?>" name="<?php echo $this->get_field_name( 'show_view_count' ); ?>" /> <?php _e( 'Show view count?', 'entry-views' ); ?></label>