• Hello,

    We have the below plugin installed to display total share counts of our articles on social media on our article pages.

    It works well, but the only problem is that sometimes the Facebook/Linkedin etc APIs can be slow or down completely and this results in long page generation times which can often overload our server and stop pages being generated.

    I was wondering if there was a quick and simple way to set a timeout on the calls to the external sites so that the plugin ignores them if they take, for example, more than 0.5 seconds?

    I have pasted the whole plugin file below as I’m not sure which bit would need editing (my PHP knowledge is limited!)

    Thanks for any help in advance.
    Martin

    <?php
    
    /*
    Plugin Name: WPShare Counter
    Plugin URI: https://github.com/phalkmin/WPShare-Counter
    Description: This plugin allows the user to display the amount of times that an URL have been shared on different social networks. Right now it supports Facebook, Twitter, Google Plus and LinkedIn. (based from GusFune's work https://github.com/gusfune/shares-counter)
    Author: GraveHeart
    Version: 0.9
    
    */ 
    
    function instalar_o_trem() {
    	add_option('scfacebook', 'true');
    	add_option('sctwitter', 'true');
    	add_option('scgplus', 'true');
    	add_option('sclinkedin', 'true');
    }
    
    //incluindo fun??es no menu
    function cria_menu_sc() {
          add_options_page(
                           'Share Counter',         //Title
                           'Share Counter',         //Sub-menu title
                           'manage_options', //Security
                           __FILE__,         //File to open
                           'sharecounter_code_options'  //Function to call
                          );
    }
    
    function sharecounter_code_options() {
          echo '<div class="wrap"><h2>Share Counter Options</h2>';
    	if ($_REQUEST['submit']) {
    		update_sharecounter_options();
    	        }
    		print_sharecounter_form();
         echo '</div>';
    }
    
    function print_sharecounter_form () {
    	$scfacebook = get_option('scfacebook');
    	$sctwitter = get_option('sctwitter');
    	$scgplus = get_option('scgplus');
    	$sclinkedin = get_option('sclinkedin');   
    
    	if ($scfacebook == "true") { $fbtrue = "checked"; } else { $fbfalse = "checked"; }
    	if ($sctwitter == "true") { $twtrue = "checked"; } else { $twfalse = "checked"; }
    	if ($scgplus == "true") { $gptrue = "checked"; } else { $gpfalse = "checked"; }
    	if ($sclinkedin == "true") { $lktrue = "checked"; } else { $lkfalse = "checked"; }
    
    	echo "<form method=\"post\">";
    	echo "<label>Facebook:</label><br /><input type=\"radio\" name=\"scfacebook\" value=\"true\" " . $fbtrue . "> Count <input type=\"radio\" name=\"scfacebook\" value=\"false\" " . $fbfalse . "> Don't Count <br /><br />";
    	echo "<label>Twitter:</label><br /><input type=\"radio\" name=\"sctwitter\" value=\"true\" " . $twtrue . "> Count <input type=\"radio\" name=\"sctwitter\" value=\"false\" " . $twfalse . " > Don't Count<br /><br />";
    	echo "<label>Google Plus:</label><br /><input type=\"radio\" name=\"scgplus\" value=\"true\" " . $gptrue . "> Count <input type=\"radio\" name=\"scgplus\" value=\"false\" " . $gpfalse . " > Don't Count<br /><br />";
    	echo "<label>Linkedin:</label><br /><input type=\"radio\" name=\"sclinkedin\" value=\"true\" " . $lktrue . "> Count <input type=\"radio\" name=\"sclinkedin\" value=\"false\" " . $lkfalse . " > Don't Count<br /><br />";
    
    	echo "<br /> <input type=\"submit\" name=\"submit\" value=\"Submit\" />  </form>";
    
     }
    
     function update_sharecounter_options() {
    
    	update_option('scfacebook', $_REQUEST['scfacebook']);
    	update_option('sctwitter', $_REQUEST['sctwitter']);
    	update_option('scgplus', $_REQUEST['scgplus']);
    	update_option('sclinkedin', $_REQUEST['sclinkedin']);
    
                echo '<div id="message" class="updated fade">';
                echo '<p>Updated</p>';
                echo '</div>';
    
      }
    
    function sharesCounter($echo = true) {
    	$shares = 0;
    	$scfacebook = get_option('scfacebook');
    	$sctwitter = get_option('sctwitter');
    	$sclinkedin = get_option('sclinkedin');
    	$scgplus = get_option('scgplus');
    	$url = get_permalink();
    
    	if ( $scfacebook ) {
    		$url_fb = "https://api.facebook.com/restserver.php?method=links.getStats&urls=" . $url;
    		$data_fb = simplexml_load_file($url_fb);
    		if ( isset($data_fb->link_stat->total_count) ) {
    			$shares = $shares + $data_fb->link_stat->total_count;
    		}
    	}
    
    	if ( $sctwitter ) {
    		$url_tw = "https://opensharecount.com/count.json?url=" . $url;
    		$data_tw = json_decode(file_get_contents($url_tw));
    		if ( isset($data_tw->count) ) {
    			$shares = $shares + $data_tw->count;
    		}
    	}
    
    	if ( $scgplus ) {
    		$curl = curl_init();
    		curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
    		curl_setopt($curl, CURLOPT_POST, 1);
    		curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
    		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    		$curl_results = curl_exec ($curl);
    		curl_close ($curl);
    		$json = json_decode($curl_results, true);
    
    		if ( isset($json) && isset($json[0]['result']['metadata']['globalCounts']['count']) ) {
    			$result = intval($json[0]['result']['metadata']['globalCounts']['count']);
    			$shares = $shares + $result;
    		}
    	}
    
    	if ( $sclinkedin ) {
    		$url_ln = "https://www.linkedin.com/countserv/count/share?url=" . $url . "&format=json";
    		if ( isset($data_ln->count) ) {
    			$shares = $shares + $data_ln->count;
    		}
    	}
    	if ( $echo ) {
    		echo intval($shares);
    	} else {
    		return intval($shares);
    	}
    }
    
    register_activation_hook( __FILE__, 'instalar_o_trem' );
    add_action('admin_menu','cria_menu_sc');
    
    ?>
  • The topic ‘Add a timeout to plugin?’ is closed to new replies.