Created
March 16, 2017 19:29
-
-
Save appscreo/93a03cfc81bd6cc28a99b2e539e1b045 to your computer and use it in GitHub Desktop.
How to replace the default comments counter with Facebook comments counter in Easy Social Share Buttons for WordPress (requires version 4.1 or newer). Code can be added anywhere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function essb_counter_request_external( $encUrl ) { | |
$counter_curl_fix = essb_option_value('counter_curl_fix'); | |
$options = array( | |
CURLOPT_RETURNTRANSFER => true, // return web page | |
CURLOPT_HEADER => false, // don't return headers | |
//CURLOPT_FOLLOWLOCATION => true, // follow redirects | |
CURLOPT_ENCODING => "", // handle all encodings | |
CURLOPT_USERAGENT => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'essb', // who am i | |
CURLOPT_AUTOREFERER => true, // set referer on redirect | |
CURLOPT_CONNECTTIMEOUT => 5, // timeout on connect | |
CURLOPT_TIMEOUT => 10, // timeout on response | |
CURLOPT_MAXREDIRS => 3, // stop after 3 redirects | |
CURLOPT_SSL_VERIFYHOST => 0, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_FAILONERROR => false, | |
CURLOPT_NOSIGNAL => 1, | |
); | |
$ch = curl_init(); | |
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) { | |
$options[CURLOPT_FOLLOWLOCATION] = true; | |
} | |
$options[CURLOPT_URL] = $encUrl; | |
curl_setopt_array($ch, $options); | |
// force ip v4 - uncomment this | |
try { | |
//print 'curl state = '.$counter_curl_fix; | |
if ($counter_curl_fix != 'true') { | |
curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); | |
} | |
} | |
catch (Exception $e) { | |
} | |
$content = curl_exec( $ch ); | |
$err = curl_errno( $ch ); | |
$errmsg = curl_error( $ch ); | |
curl_close( $ch ); | |
if ($errmsg != '' || $err != '') { | |
print_r($errmsg); | |
} | |
return $content; | |
} | |
function essb_get_facebook_comments_count($url) { | |
$parse_url = 'https://graph.facebook.com/?id='.$url; | |
$facebook_token = essb_option_value('facebook_counter_token'); | |
if ($facebook_token != '') { | |
$parse_url = 'https://graph.facebook.com/?id='.$url.'&access_token=' . sanitize_text_field($facebook_token); | |
} | |
$content = essb_counter_request_external ( $parse_url ); | |
//print " facebook output = ".$content; | |
$result = 0; | |
$result_comments = 0; | |
if ($content != '') { | |
$content = json_decode ( $content, true ); | |
$data_parsers = $content; | |
$result = isset ( $data_parsers ['share'] ['comment_count'] ) ? intval ( $data_parsers ['share'] ['comment_count'] ) : 0; | |
} | |
return $result; | |
} | |
if (!function_exists('essb_cache_counters_parse')) { | |
function essb_cache_counters_parse($cached_counters = array()) { | |
global $post; | |
if (!isset($post)) return $cached_counters; | |
$url = get_permalink($post->ID); | |
$post_id = $post->ID; | |
$cumulative_total = 0; | |
foreach ($cached_counters as $network => $shares) { | |
if ($network == 'total') continue; | |
$shares = isset($cached_counters[$network]) ? $cached_counters[$network] : 0; | |
if ($network == 'comment') { | |
$shares = essb_get_facebook_comments_count($url); | |
} | |
$cached_counters[$network] = $shares; | |
$cumulative_total += intval($shares); | |
} | |
$cached_counters['total'] = $cumulative_total; | |
return $cached_counters; | |
} | |
add_filter('essb4_get_cached_counters', 'essb_cache_counters_parse'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment