Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add today views counter for posts and pages #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions postviews-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ function views_default_templates(template) {
<td valign="top">
<strong><?php _e( 'Views Template:', 'wp-postviews' ); ?></strong><br /><br />
<?php _e( 'Allowed Variables:', 'wp-postviews' ); ?><br />
- %VIEW_COUNT%<br />
- %VIEW_COUNT%<br />
- %VIEW_COUNT_TODAY%<br />
- %VIEW_COUNT_ROUNDED%<br /><br />
<input type="button" name="RestoreDefault" value="<?php _e( 'Restore Default Template', 'wp-postviews' ); ?>" onclick="views_default_templates( 'template' );" class="button" />
</td>
Expand All @@ -119,7 +120,8 @@ function views_default_templates(template) {
<td valign="top">
<strong><?php _e( 'Most Viewed Template:', 'wp-postviews' ); ?></strong><br /><br />
<?php _e( 'Allowed Variables:', 'wp-postviews' ); ?><br />
- %VIEW_COUNT%<br />
- %VIEW_COUNT%<br />
- %VIEW_COUNT_TODAY%<br />
- %VIEW_COUNT_ROUNDED%<br />
- %POST_TITLE%<br />
- %POST_DATE%<br />
Expand Down
83 changes: 73 additions & 10 deletions wp-postviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ function process_postviews() {
$should_count = apply_filters( 'postviews_should_count', $should_count, $id );
if( $should_count && ( ( isset( $views_options['use_ajax'] ) && (int) $views_options['use_ajax'] === 0 ) || ( !defined( 'WP_CACHE' ) || !WP_CACHE ) ) ) {
update_post_meta( $id, 'views', $post_views + 1 );
process_today_views($id);
do_action( 'postviews_increment_views', $post_views + 1 );
}
}
Expand Down Expand Up @@ -203,10 +204,11 @@ function should_views_be_displayed($views_options = null) {

### Function: Display The Post Views
function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
$post_views = (int) get_post_meta( get_the_ID(), 'views', true );
$post_views = (int) get_post_meta( get_the_ID(), 'views', true );
$today_views = (int) today_views();
$views_options = get_option('views_options');
if ($always || should_views_be_displayed($views_options)) {
$output = $prefix.str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views) ), stripslashes( $views_options['template'] ) ).$postfix;
$output = $prefix.str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%', '%VIEW_COUNT_TODAY%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views), number_format_i18n( $today_views ) ), stripslashes( $views_options['template'] ) ).$postfix;
if($display) {
echo apply_filters('the_views', $output);
} else {
Expand All @@ -228,7 +230,9 @@ function views_shortcode( $atts ) {
}
$views_options = get_option( 'views_options' );
$post_views = (int) get_post_meta( $id, 'views', true );
$output = str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views) ), stripslashes( $views_options['template'] ) );
$today_views = (int) today_views();

$output = str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%', '%VIEW_COUNT_TODAY%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views), number_format_i18n( $today_views ) ), stripslashes( $views_options['template'] ) );

return apply_filters( 'the_views', $output );
}
Expand All @@ -253,6 +257,7 @@ function get_least_viewed( $mode = '', $limit = 10, $chars = 0, $display = true

// Post Views.
$post_views = get_post_meta( get_the_ID(), 'views', true );
$today_views = today_views();

// Post Title.
$post_title = get_the_title();
Expand All @@ -269,7 +274,8 @@ function get_least_viewed( $mode = '', $limit = 10, $chars = 0, $display = true

$temp = stripslashes( $views_options['most_viewed_template'] );
$temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_TODAY%', number_format_i18n( $today_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
$temp = str_replace( '%POST_TITLE%', $post_title, $temp );
$temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
$temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp );
Expand Down Expand Up @@ -314,6 +320,7 @@ function get_most_viewed( $mode = '', $limit = 10, $chars = 0, $display = true )

// Post Views.
$post_views = get_post_meta( get_the_ID(), 'views', true );
$today_views = today_views();

// Post Title.
$post_title = get_the_title();
Expand All @@ -330,7 +337,8 @@ function get_most_viewed( $mode = '', $limit = 10, $chars = 0, $display = true )

$temp = stripslashes( $views_options['most_viewed_template'] );
$temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_TODAY%', number_format_i18n( $today_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
$temp = str_replace( '%POST_TITLE%', $post_title, $temp );
$temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
$temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp );
Expand Down Expand Up @@ -376,6 +384,7 @@ function get_least_viewed_category( $category_id = 0, $mode = '', $limit = 10, $

// Post Views.
$post_views = get_post_meta( get_the_ID(), 'views', true );
$today_views = today_views();

// Post Title.
$post_title = get_the_title();
Expand All @@ -392,7 +401,8 @@ function get_least_viewed_category( $category_id = 0, $mode = '', $limit = 10, $

$temp = stripslashes( $views_options['most_viewed_template'] );
$temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_TODAY%', number_format_i18n( $today_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
$temp = str_replace( '%POST_TITLE%', $post_title, $temp );
$temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
$temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp );
Expand Down Expand Up @@ -438,6 +448,7 @@ function get_most_viewed_category( $category_id = 0, $mode = '', $limit = 10, $c

// Post Views.
$post_views = get_post_meta( get_the_ID(), 'views', true );
$today_views = today_views();

// Post Title.
$post_title = get_the_title();
Expand All @@ -454,7 +465,8 @@ function get_most_viewed_category( $category_id = 0, $mode = '', $limit = 10, $c

$temp = stripslashes( $views_options['most_viewed_template'] );
$temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_TODAY%', number_format_i18n( $today_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
$temp = str_replace( '%POST_TITLE%', $post_title, $temp );
$temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
$temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp );
Expand Down Expand Up @@ -499,6 +511,7 @@ function get_least_viewed_tag( $tag_id = 0, $mode = '', $limit = 10, $chars = 0,

// Post Views.
$post_views = get_post_meta( get_the_ID(), 'views', true );
$today_views = today_views();

// Post Title.
$post_title = get_the_title();
Expand All @@ -515,7 +528,8 @@ function get_least_viewed_tag( $tag_id = 0, $mode = '', $limit = 10, $chars = 0,

$temp = stripslashes( $views_options['most_viewed_template'] );
$temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_TODAY%', number_format_i18n( $today_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
$temp = str_replace( '%POST_TITLE%', $post_title, $temp );
$temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
$temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp );
Expand Down Expand Up @@ -560,7 +574,8 @@ function get_most_viewed_tag( $tag_id = 0, $mode = '', $limit = 10, $chars = 0,
$most_viewed->the_post();

// Post Views.
$post_views = get_post_meta( get_the_ID(), 'views', true );
$post_views = get_post_meta( get_the_ID(), 'views', true );
$today_views = today_views();

// Post Title.
$post_title = get_the_title();
Expand All @@ -576,7 +591,8 @@ function get_most_viewed_tag( $tag_id = 0, $mode = '', $limit = 10, $chars = 0,
}

$temp = stripslashes( $views_options['most_viewed_template'] );
$temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_TODAY%', number_format_i18n( $today_views ), $temp );
$temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
$temp = str_replace( '%POST_TITLE%', $post_title, $temp );
$temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
Expand Down Expand Up @@ -1007,9 +1023,56 @@ function views_activation( $network_wide ) {
} else {
add_option( $option_name, $option );
}

// Set Default View Meta
post_views_default();
}

### Function: Parse View Options
function views_options_parse( $key ) {
return ! empty( $_POST[ $key ] ) ? $_POST[ $key ] : null;
}

### Function: Count Today Post Views
function process_today_views($id) {
$day_of_week = date("l");
$today_views = get_post_meta($id,"today_views",true);
$statics = explode("-", $today_views);
$count = $statics[1]+1;

if ($statics[0] === $day_of_week)
update_post_meta($id,"today_views",$day_of_week . '-' . $count );
else
update_post_meta($id,"today_views",$day_of_week . '-' . 1);

}
### Function: Show Today Post Views
if(!function_exists('today_views')) {
function today_views($display = false)
{
$today_views = get_post_meta(get_the_ID(),"today_views",true);
$views = explode("-", $today_views);
if ($display)
echo $views[1];
else
return $views[1];

}
}
### Function: Set Default Value To 'views' For Order By Meta
if(!function_exists('post_views_default')) {
function post_views_default()
{
$args = array(
'posts_per_page' => -1,
'post_type' => array( 'post', 'page' )
);
$viewsToZero = new WP_Query( $args );

foreach ( $viewsToZero->posts as $post ){

if ( ! get_post_meta( $post->ID, 'views', true ) )
add_post_meta( $post->ID, 'views', 0, true );
}
}
}