WordPress.org

Ready to get started?Download WordPress

Forums

Popular Posts Tabbed Widget for Jetpack
[resolved] POPULAR TODAY shows "Sorry. No data yet." (12 posts)

  1. Jerichos
    Member
    Posted 9 months ago #

    Greetings!
    I'm facing a problem with Popular Posts Tabbed Widget. In Popular today it shows "Sorry. No data yet." for some time. It happens because my website's time zone is not far the same as my local computer's time zone.

    In Jetpack stats it shows 25 June, and my local date is already 26 June for 6 hours. I suppose visitors from different countries are facing the same problem as I do.

    So, I Think the problem is in date, but personally I can not handle this issue. Could you please help me with that.

    Thank you!

    Best Regards.

    http://wordpress.org/extend/plugins/popular-posts-tab-widget-for-jetpack/

  2. Ryann Micua
    Member
    Plugin Author

    Posted 9 months ago #

    Hi Jerichos,

    Have you tried setting Popular Posts date range to Monthly instead of daily? Hopefully that should alleviate the issue a bit. Clicking on "Today" may still show the "no data" text.

    Also in your WordPress' site date, you could try to set it to your timezone or something.

  3. Jerichos
    Member
    Posted 9 months ago #

    Yes, I've tried to set Date range to weekly and it works just fine, but I think that visitors, who live to West from my WordPress time zone will face the same problem if they click Today's popular news.

    Thank you.

  4. Jerichos
    Member
    Posted 9 months ago #

    Is it possible to pick date from my WordPress instead of my local date?

    Thank you.

  5. Ryann Micua
    Member
    Plugin Author

    Posted 9 months ago #

    Yes, like I said, the today thing will have a problem and I really can't do much about it since the stats are taken from jetpack stats. So, if there are no stats for today from jetpack then there will be nothing to show. I'll probably add an option to set the "today" to actaully be 2 days later on.

    Not sure what you mean about your 2nd question.

  6. Jerichos
    Member
    Posted 9 months ago #

    The thing is that jetpack shows Today's top posts without any problem, but this plugin tries to get top list from the next day if user's local date is after WordPress timezone's date.

    In my last post I meant something like to get jetpack's day's top posts using it's own function now() or something. I don't know how to do it, i'm just suggesting)))

    I made this widget to work satisfactorily by changing my wordpress local timezone to UTC+14.

    Thank you for your responds.

  7. Ryann Micua
    Member
    Plugin Author

    Posted 9 months ago #

    uh, that's what I suggested in my first reply - change your timezone. Jetpack's Today's top posts *should* be the same as in the popular plugin. Reason for that is because the plugin is just using the same functions as what jetpack itself uses.

  8. Jerichos
    Member
    Posted 9 months ago #

    Jetpack's Today's top posts *should* be the same as in the popular plugin

    But it's not because it depends on date of my computer.

    Ok, I changed WordPress timezone to my local timezone, which is currently UTC+3 and it works just fine for me. But if I were in Kiritimati, Christmas Island, Kiribati (UTC/GMT +14) there would be 27 June already.

    In my admin panel I can see all popular news for today (for me it's 26 June right now), but if I change date on my device to 27 June (which is true for Kiritimati) and try to access my web page, plugin shows "Sorry. No data yet.".

    Changing timezone in WordPress settings to UTC +14 is not a very good idea as well, because the opposite thing happens - it shows popular posts, indeed, but for me it shows yesterday's most popular stats.

    Problem resolved by changing WordPress timezone to UTC+0 and changing function "now" at popular-posts-tab-widget-for-jetpack.php

    static function now(){
    		return current_time('mysql');
    	}

    to this

    static function now(){
    		return current_time('mysql', 1);
    	}

    now this function returns current server's UTC time

    Thank you.

  9. Ryann Micua
    Member
    Plugin Author

    Posted 9 months ago #

    Thank you Jericho for taking the time to go through the code Jericho. Appreciate it.

    There's just one thing though. This function:

    static function now(){
       return current_time('mysql');
    }

    isn't used anywhere for getting the list of popular posts. It's only used for getting the list of most commented post ;)

    Since you can certainly read code, here's the function that actually does the work on the popular posts tab:

    /**
     * Uses data gathered by Jetpack stats and stored in WordPress.com servers
     */
    static function get_posts_by_wp_com( $args ){
    
    	if( !self::$_stats_enabled || !function_exists('stats_get_csv'))
    		return array();
    
    	$defaults = array(
    		'limit' => 5,
    		'range' => 'all', //daily|weekly|monthly|all
    		'post_type' => 'post',
    		'date_format' => get_option('date_format')
    		);
    	$args = wp_parse_args( (array) $args, $defaults );
    
    	$limit = intval( $args['limit'] );
    
    	/** TODO: limit $limit to 100? **/
    
    	$days = 2;
    	switch( $args['range'] ){
    		case 'weekly':  $days = 7; break;
    		case 'monthly': $days = 30; break;
    		case 'daily' :  $days = 1; break;
    		case 'all':
    		default:        $days = -1; break; //get all
    	}
    
    	/** we only limit to 50 posts. but change this if you want **/
    	$top_posts = stats_get_csv( 'postviews', array( 'days' => $days, 'limit' => 50 ) );
    
    	if( !$top_posts ){
    		return array();
    	}
    
    	/** Store post_id into array **/
    	$post_view_ids = array_filter( wp_list_pluck( $top_posts, 'post_id' ) );
    	if ( !$post_view_ids ) {
    		return array();
    	}
    
    	// cache
    	get_posts( array( 'include' => join( ',', array_unique( $post_view_ids ) ) ) );
    
    	// return posts list
    	$posts = array();
    	$counter = 0;
    	foreach( $top_posts as $top_post ){
    
    		//should only trigger for homepage
    		if(empty($top_post['post_id']))
    			continue;
    
    		$post = get_post( $top_post['post_id'] );
    
    		if ( !$post )
    			continue;
    
    		if( $args['post_type'] != $post->post_type )
    			continue;
    
    		$permalink = get_permalink( $post->ID );
    		$postdate = date_i18n( $args['date_format'], strtotime( $post->post_date ) );
    		$views = number_format_i18n( $top_post['views'] );
    
    		if ( empty( $post->post_title ) ) {
    			$title_source = $post->post_content;
    			$title = wp_html_excerpt( $title_source, 50 );
    			$title .= '…';
    		} else {
    			$title = $post->post_title;
    		}
    
    		$data = array(
    			'title' => $title,
    			'permalink' => $permalink,
    			'views' => $views,
    			'id' => $post->ID,
    			'postdate' => $postdate
    		);
    
    		$posts[] = $data;
    		$counter++;
    
    		if( $counter == $limit )
    			break;
    
    	}
    
    	return $posts;
    }

    The most relevant function call is this:

    $top_posts = stats_get_csv( 'postviews', array( 'days' => $days, 'limit' => 50 ) );

    stats_get_csv() is the function that grabs the popular posts from jetpack stats server. what the plugin then does is to remove the the non-blog-post pages leaving only blog posts.

    So, if you're saying that it's working now then it's because you set your timezone to utc+0 - which I'm pretty sure is what jetpack stats is based off of - and not because you edited the now() function which has nothing to do with popular posts.

  10. Jerichos
    Member
    Posted 9 months ago #

    I'm not so good at PHP, I work more with C#.
    I just looked at this query where function now is present

    /* RANGE: Daily */
    
    			// views
    			if ($args['order_by'] == "views" || $args['order_by'] == "avg" || $args['stats_tag']['views']) {
    				$join .= " LEFT JOIN (SELECT id, SUM(pageviews) AS 'pageviews', day FROM (SELECT id, pageviews, day FROM {$table}cache WHERE day > DATE_SUB('".self::now()."', INTERVAL 1 DAY) ORDER BY day) sv GROUP BY id) v ON p.ID = v.id ";
    
    				$fields .= ", IFNULL(v.pageviews, 0) AS 'pageviews' ";
    			}

    Thank You.

  11. Ryann Micua
    Member
    Plugin Author

    Posted 9 months ago #

    actually, that's unused code :D I was thinking of being able to integrate with one other *popular posts* plugin that's why I left that option in there. but I can tell you right now, that section is unused.

    If you want to do a trace, start with this function:

    function widget($args, $instance)

    that's what wp calls to display the actual widget markup in the frontend.

    and oh, I think c# is awesome.

  12. Jerichos
    Member
    Posted 9 months ago #

    I think c# is awesome.

    I love it either =)))

    Anyway I Love your widget and now It look to work fine, Thanks

Reply

You must log in to post.

About this Plugin

About this Topic