-
-
Save faishal/cd087ae639c8f98063e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'widgets_init', 'nbclatino_top_posts_widget_init' ); | |
function nbclatino_top_posts_widget_init() { | |
// Check if our vip stats function is available | |
if ( !function_exists( 'wpcom_vip_get_stats_array' ) && ! function_exists( 'stats_get_csv' ) ) | |
return; | |
unregister_widget( 'Jetpack_Top_Posts_Widget' ); | |
register_widget( 'NBCLatino_Top_Posts_Widget' ); | |
} | |
class NBCLatino_Top_Posts_Widget extends Jetpack_Top_Posts_Widget { | |
var $alt_option_name = 'widget_stats_topposts'; | |
var $default_title = ''; | |
function __construct() { | |
$this->defaults = array( | |
'title' => __( 'Top Posts & Pages', 'jetpack' ), | |
'display' => 'text', | |
'count' => 10, | |
'num_days' => 2, | |
'filter_cat' => '', | |
); | |
parent::__construct(); | |
add_filter( 'widget_display_callback', array( $this, 'get_instance' ), 10, 2 ); | |
} | |
function get_instance( $instance, $obj ) { | |
if ( $obj instanceof $this ) | |
$this->instance = $instance; | |
return $instance; | |
} | |
function form( $instance ) { | |
$instance = wp_parse_args( $instance, $this->defaults ); | |
$instance['count'] = $this->limit_count( $instance ); | |
$instance['num_days'] = $this->limit_count( $instance, false ); | |
if ( ! isset( $instance['display'] ) || ! in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) | |
$instance['display'] = 'text'; | |
extract( $instance ); | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php esc_html_e( 'Maximum number of posts to show (no more than 10):', 'jetpack' ); ?></label> | |
<input id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" type="number" value="<?php echo (int) $count; ?>" min="1" max="10" /> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'num_days' ); ?>"><?php esc_html_e( 'Number of days for analytics to pull from:', 'jetpack' ); ?></label> | |
<input id="<?php echo $this->get_field_id( 'num_days' ); ?>" name="<?php echo $this->get_field_name( 'num_days' ); ?>" type="number" value="<?php echo (int) $num_days; ?>" min="1" max="1825" /> | |
</p> | |
<p> | |
<label><?php esc_html_e( 'Display as:', 'jetpack' ); ?></label> | |
<ul> | |
<li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-text" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="text" <?php checked( 'text', $display ); ?> /> <?php esc_html_e( 'Text List', 'jetpack' ); ?></label></li> | |
<li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-list" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="list" <?php checked( 'list', $display ); ?> /> <?php esc_html_e( 'Image List', 'jetpack' ); ?></label></li> | |
<li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-grid" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="grid" <?php checked( 'grid', $display ); ?> /> <?php esc_html_e( 'Image Grid', 'jetpack' ); ?></label></li> | |
</ul> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'filter_cat' ); ?>"><?php esc_html_e( 'Filter by Category:', 'jetpack' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'filter_cat' ); ?>" name="<?php echo $this->get_field_name( 'filter_cat' ); ?>" type="text" value="<?php echo esc_attr( $filter_cat ); ?>" /> | |
</p> | |
<?php | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = wp_parse_args( array(), $this->defaults ); | |
$instance['title'] = wp_kses( $new_instance['title'], array() ); | |
$instance['filter_cat'] = sanitize_text_field( $new_instance['filter_cat'] ); | |
// if ( $instance['title'] === $this->default_title ) { | |
// $instance['title'] = false; // Store as false in case of language change | |
// } | |
$instance['count'] = $this->limit_count( $new_instance ); | |
$instance['num_days'] = $this->limit_count( $new_instance, false ); | |
if ( isset( $new_instance['display'] ) && in_array( $new_instance['display'], array( 'grid', 'list', 'text' ) ) ) { | |
$instance['display'] = $new_instance['display']; | |
} else { | |
$instance['display'] = 'text'; | |
} | |
return $instance; | |
} | |
function get_by_views( $count ) { | |
$num_days = $this->limit_count( $this->instance, false ); | |
$post_view_posts = false; | |
// http://vip.wordpress.com/documentation/wordpress-com-stats/ | |
if ( function_exists( 'wpcom_vip_top_posts_array' ) ) | |
$post_view_posts = wpcom_vip_top_posts_array( absint( $num_days ), 10 ); | |
elseif ( function_exists( 'stats_get_csv' ) ) | |
$post_view_posts = stats_get_csv( 'postviews', array( 'days' => absint( $num_days ), 'limit' => 10 ) ); | |
if ( !$post_view_posts ) { | |
return array(); | |
} | |
$post_view_ids = array_filter( wp_list_pluck( $post_view_posts, 'post_id' ) ); | |
if ( !$post_view_ids ) { | |
return array(); | |
} | |
$post_view_ids = array_filter( $post_view_ids, array( $this, 'cat_filter' ) ); | |
return $this->get_posts( $post_view_ids, $count ); | |
} | |
function cat_filter( $id ) { | |
if ( ! isset( $this->instance ) || ! isset( $this->instance['cat_filter'] ) ) | |
if ( ! ( $cats = get_the_category( $id ) ) ) | |
return false; | |
$cats = wp_list_pluck( $cats, 'name' ); | |
if ( in_array( $this->instance['filter_cat'], $cats ) ) | |
return true; | |
return false; | |
} | |
function limit_count( $instance, $key = 'count' ) { | |
$key = 'count' === $key ? 'count' : 'num_days'; | |
$max = 'count' === $key ? 10 : 1825; | |
$count = isset( $instance[ $key ] ) ? (int) $instance[ $key ] : 0; | |
if ( $count < 1 || $max < $count ) { | |
$count = $max; | |
} | |
return $count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment