0

My wordpress site uses a tiled format and displays 10 posts on the homepage; once you start scrolling it will load more with infinite scroll.

I'm trying to reduce the page load time, as it is about 2.5 seconds with this code added; and 1 second without it.

What I want to do is grab the amount of social shares each post has, each hour, and display it on the homepage. I've tried to figure out a fast/easier way to do this, but it seems like this is the only way I know of.

Being that it displays the 10 posts, it will have to make 2 requests per post listed, so 20 total requests into the database. That seems like a lot to me, and it seems like there is some easy way to do this that I am missing.

Any help?

function getFBShares($postid, $url){

//Grab Results from fb_shares
    $fbcount  = get_post_meta( $postid, 'fb_shares', true );

    if( ! empty( $fbcount ) ) {
        $fbtime  = get_post_meta( $postid, 'share_time', true );
        //Compare timestamp with one hour ago
        if ($fbtime < current_time('timestamp') - 60*60){
            $fql = "SELECT share_count FROM link_stat WHERE url = '".$url."'";
            $apifql = "https://api.facebook.com/method/fql.query?format=json&query=".urlencode( $fql );
            $json = file_get_contents( $apifql );
            $data = json_decode($json);
            $fbcount = $data[0]->share_count;
            update_post_meta($postid,'fb_shares',$fbcount);
            update_post_meta($postid,'share_time',current_time('timestamp'));
            return $fbcount;
        }
        else {
            return $fbcount;
        }

    }
    else {
        //If there is no result, save $fbcount in 'fb_shares' column with current timestamp
        $fql = "SELECT share_count FROM link_stat WHERE url = '".$url."'";
        $apifql = "https://api.facebook.com/method/fql.query?format=json&query=".urlencode( $fql );
        $json = file_get_contents( $apifql );
        $data = json_decode($json);
        $fbcount = $data[0]->share_count;
        update_post_meta($postid,'fb_shares',$fbcount);
        update_post_meta($postid,'share_time',current_time('timestamp'));
        return $fbcount;
    }
}

function getPlusOne($postid, $url){

    //Grab Results from gp_shares
    $gpcount  = get_post_meta( $postid, 'gp_shares', true );

    if( ! empty( $gpcount ) ) {
        $gbtime  = get_post_meta( $postid, 'share_time', true );
        //Compare timestamp with one hour ago
        if ($gbtime < current_time('timestamp') - 60*60){
            $html =  file_get_contents( "https://plusone.google.com/_/+1/fastbutton?url=".urlencode($url));
            $doc = new DOMDocument();   $doc->loadHTML($html);
            $counter=$doc->getElementById('aggregateCount');
            $gpcount = $counter->nodeValue;
            update_post_meta($postid,'gp_shares',$gpcount);
            update_post_meta($postid,'share_time',current_time('timestamp'));
            return $gpcount;
        }
        else {
            return $gpcount;
        }

    }
    else {
        //If there is no result, save $gpcount in 'gp_shares'
        $html =  file_get_contents( "https://plusone.google.com/_/+1/fastbutton?url=".urlencode($url));
        $doc = new DOMDocument();   $doc->loadHTML($html);
        $counter=$doc->getElementById('aggregateCount');
        $gpcount = $counter->nodeValue;
        update_post_meta($postid,'gp_shares',$gpcount);
        update_post_meta($postid,'share_time',current_time('timestamp'));
        return $gpcount;
    }
}

function getMyTweets($postid, $url){

    //Grab Results from social_count
    $tweetcount  = get_post_meta( $postid, 'tweet_shares', true );

    if( ! empty( $tweetcount ) ) {
        $tweettime  = get_post_meta( $postid, 'share_time', true );
        //Compare timestamp with one hour ago
        if ($tweettime < current_time('timestamp') - 60*60){
            $json = file_get_contents( "http://urls.api.twitter.com/1/urls/count.json?url=".$url );
            $ajsn = json_decode($json, true);
            $tweetcount = $ajsn['count'];
            update_post_meta($postid,'tweet_shares',$tweetcount);
            update_post_meta($postid,'share_time',current_time('timestamp'));
            return $tweetcount;
        }
        else {
            return $tweetcount;
        }

    }
    else {
        //If there is no result, save $tweetcount in 'tweet_shares' column with current timestamp
        $json = file_get_contents( "http://urls.api.twitter.com/1/urls/count.json?url=".$url );
        $ajsn = json_decode($json, true);
        $tweetcount = $ajsn['count'];
        update_post_meta($postid,'tweet_shares',$tweetcount);
        update_post_meta($postid,'share_time',current_time('timestamp'));
        return $tweetcount;
    }
}
4

0 回答 0