Support » Plugin: kk Star Ratings » Slow loading

  • Resolved Jee6

    (@jee6)


    Hi,

    I like your plugin but i found something strange, the loading is sometime slow and each image in my post won’t load until your plugins appear.

    2 images is affected and they are only 3kb each.

    I’m running on a VPS and i don’t get this slow loading anywhere, i added the big GD star plugin to compare and GD is loading fast.

    Sometime the stars load fast and sometime i can wait 2 or 3 seconds.

    Any idea about this problem?

    http://wordpress.org/extend/plugins/kk-star-ratings/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Experiencing the same thing. The plugin looks very promising, but the loading on an archive page with 15 articles and ratings takes a bit too long.

    I fix it: coaching.dariusz-licznerski.pl

    p.s. This problem is because admin-ajax.php is called for each ID. It takes time for many IDs…

    how did you fix this? I needed this fix for my site too >>>

    http://www.nylease.com/lease-specials/

    please advise

    SashaSolo,

    You have only 6 ratings per page (per one load) so it is not so slow in your case. I had about 80 ratings per page (per one load), so it took years to load all ratings at once…

    You have 6 admin-ajax.php calls right now, not so much, but if you want to speed up your page you have to call admin-ajax.php only once.

    Regards,
    Darek

    Darek L,

    Yes, he knows. His question is HOW to do this 😉

    Mine as well…

    Okoth1,

    Websites are open source code, so you can view it at any time. The kk star ratings js file you can see here: js.js. Also you need to change the php file on the other side (not so much changes there, just get the array and ask database for ratings).

    Steps:
    1. The js file counts how many IDs you have on the page.
    2. Then the js file sends to the php file the number of IDs and also all IDs numbers (so they don’t have to be from 1 to eg. 80, you can have e.g. 1,2,3 space and from 10 other IDs).
    3. The php file gets the number of IDs and all IDs numbers.
    4. Then the php file checks ratings for all IDs in the database.
    5. The php file packs the IDs and ratings to array and sends it to the js file.
    6. Then the js file gets ratings from array and calls modified function kksr_fetch which I copied and changed into kksr_fetch_all.

    Hope it helps. Not so pretty coding style, rather quick fix. Probably official patch should be more advanced, I guess.

    Regards,
    Darek

    Thanks Darek L,

    I’ll see if I can get it to work as well.

    Sorry, but I still don’t understand what to change in the index.php to make the stars appear.

    Okoth1,

    I’ve changed the “kksr_ajax” function in “index.php” file in the “kk-star-ratings” directory.

    Little description of changes:
    I just copied the body of the function “kksr_ajax” and add extra “if” and “for” loop when the ID is minus (e.g. id=-80). If the id is minus I know that js file sends to the php array with all IDs and want back ratings for all of them. Simply if the id is plus (e.g. id=500), I know that this is single call for ID, so the else from if is unchanged part.

    So this is all the rocket science 😉

    I am not 100% sure if it solve your problem and will be ok in your case (posts). Test it carefully somewhere before. Probably it can be done much better. I posted the changed fragment below so see it before and make your own changes based on this.

    public function kksr_ajax()
    		{
    			header('Content-type: application/json; charset=utf-8');
    			check_ajax_referer($this->id);
    
    			$Response = array();
    
    			$total_stars = is_numeric(parent::get_options('kksr_stars')) ? parent::get_options('kksr_stars') : 5;
    
    			$pid = $_POST['id'];
    			$stars = $_POST['stars'];
    			$ip = $_SERVER['REMOTE_ADDR'];
    
    			if ($pid<0)
    			{
    				$pid=-$pid;
    				$ID_all = explode("-",$_POST['all']);
    
    				for ($i=1;$i<=$pid;$i++)
    				{
    					$Response[$i] = array();
    
    					$ratings = get_post_meta($ID_all[$i-1], '_kksr_ratings', true) ? get_post_meta($ID_all[$i-1], '_kksr_ratings', true) : 0;
    					$casts = get_post_meta($ID_all[$i-1], '_kksr_casts', true) ? get_post_meta($ID_all[$i-1], '_kksr_casts', true) : 0;
    
    					if($stars==0 && $ratings==0)
    					{
    						$Response[$i]['legend'] = parent::get_options('kksr_init_msg');
    						$Response[$i]['disable'] = 'false';
    						$Response[$i]['fuel'] = '0';
    						do_action('kksr_init', $ID_all[$i-1], false, false);
    					}
    					else
    					{
    						$nratings = $ratings + ($stars/($total_stars/5));
    						$ncasts = $casts + ($stars>0);
    						$avg = $nratings ? round($nratings/$ncasts,1) : 0;
    						$per = $nratings ? round((($nratings/$ncasts)/5)*100) : 0;
    						$Response[$i]['disable'] = 'false';
    						if($stars)
    						{
    							$Ips = get_post_meta($ID_all[$i-1], '_kksr_ips', true) ? unserialize(base64_decode(get_post_meta($ID_all[$i-1], '_kksr_ips', true))) : array();
    							if(!in_array($ip, $Ips))
    							{
    								$Ips[] = $ip;
    							}
    							$ips = base64_encode(serialize($Ips));
    							update_post_meta($ID_all[$i-1], '_kksr_ratings', $nratings);
    							update_post_meta($ID_all[$i-1], '_kksr_casts', $ncasts);
    							update_post_meta($ID_all[$i-1], '_kksr_ips', $ips);
    							update_post_meta($ID_all[$i-1], '_kksr_avg', $avg);
    							$Response[$i]['disable'] = parent::get_options('kksr_unique') ? 'true' : 'false';
    							do_action('kksr_rate', $ID_all[$i-1], $stars, $ip);
    						}
    						else
    						{
    							do_action('kksr_init', $ID_all[$i-1], ($avg*($total_stars/5)).'/'.$total_stars, $ncasts);
    						}
    						$legend = parent::get_options('kksr_legend');
    						$legend = str_replace('[total]', $ncasts, $legend);
    						$legend = str_replace('[avg]', ($avg*($total_stars/5)).'/'.$total_stars, $legend);
    						$legend = str_replace('[s]', $ncasts==1?'':'s', $legend);
    						$Response[$i]['legend'] = str_replace('[per]',$per.'%', $legend);
    						$Response[$i]['fuel'] = $per;
    					}
    				$Response[$i]['success'] = 'true';
    				}
    			}
    			else {
    				$ratings = get_post_meta($pid, '_kksr_ratings', true) ? get_post_meta($pid, '_kksr_ratings', true) : 0;
    				$casts = get_post_meta($pid, '_kksr_casts', true) ? get_post_meta($pid, '_kksr_casts', true) : 0;
    
    				if($stars==0 && $ratings==0)
    				{
    					$Response['legend'] = parent::get_options('kksr_init_msg');
    					$Response['disable'] = 'false';
    					$Response['fuel'] = '0';
    					do_action('kksr_init', $pid, false, false);
    				}
    				else
    				{
    					$nratings = $ratings + ($stars/($total_stars/5));
    					$ncasts = $casts + ($stars>0);
    					$avg = $nratings ? round($nratings/$ncasts,1) : 0;
    					$per = $nratings ? round((($nratings/$ncasts)/5)*100) : 0;
    					$Response['disable'] = 'false';
    					if($stars)
    					{
    						$Ips = get_post_meta($pid, '_kksr_ips', true) ? unserialize(base64_decode(get_post_meta($pid, '_kksr_ips', true))) : array();
    						if(!in_array($ip, $Ips))
    						{
    							$Ips[] = $ip;
    						}
    						$ips = base64_encode(serialize($Ips));
    						update_post_meta($pid, '_kksr_ratings', $nratings);
    						update_post_meta($pid, '_kksr_casts', $ncasts);
    						update_post_meta($pid, '_kksr_ips', $ips);
    						update_post_meta($pid, '_kksr_avg', $avg);
    						$Response['disable'] = parent::get_options('kksr_unique') ? 'true' : 'false';
    						do_action('kksr_rate', $pid, $stars, $ip);
    					}
    					else
    					{
    						do_action('kksr_init', $pid, ($avg*($total_stars/5)).'/'.$total_stars, $ncasts);
    					}
    					$legend = parent::get_options('kksr_legend');
    					$legend = str_replace('[total]', $ncasts, $legend);
    					$legend = str_replace('[avg]', ($avg*($total_stars/5)).'/'.$total_stars, $legend);
    					$legend = str_replace('[s]', $ncasts==1?'':'s', $legend);
    					$Response['legend'] = str_replace('[per]',$per.'%', $legend);
    					$Response['fuel'] = $per;
    				}
    			$Response['success'] = 'true';
    			}
    			echo json_encode($Response);
    			die();
    		}

    Regards,
    Darek

    Hi Darek,

    I got it to work. Thanks for all the efforts!

    I like this plugin, is simple and has all the needed options, so I was very sad when I saw it has slow loading problem.

    Okoth1, great to hear that You got it to work too. Good luck.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Slow loading’ is closed to new replies.