Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • I posted a temporary workaround in this thread if you’re interested: https://wordpress.org/support/topic/facebook-counter-not-working-please-help-me-to-fix?replies=5

    I was able to implement a workaround on my WordPress installation, albeit hardcoded. A Facebook app is required for this to work. Please note that there might be an issue with Facebook API v2.4 which results in the necessary data not being returned (only “id” and “name” are returned), so until they fix this, you need to make use of an app that was created prior to 2.4, so creating a new Facebook app right now will NOT work at this point. You can see API version in the Dashboard at https://developers.facebook.com

    I’ve created this simple tool in PHP to create a long-lived access token which should never expire (app in development mode) or in 2 months (approved app in public/production mode). It requires the PHP Facebook SDK which you can get from https://github.com/facebook/facebook-php-sdk-v4/archive/4.0-dev.zip

    Upload the script and the SDK files to a server. You may modify the authentication scopes as desired. In the Facebook app settings, configure the “Valid OAuth redirect URIs” (under Settings > Advanced) to point to the URL of the PHP file. So for example, if you access the script at http://localhost/tokenhelper.php, then that should also be your redirect URL. These URLs must match exactly.

    <?php
    
    session_start();
    
    function get_value($key) {
        if (isset($_POST[$key])) {
            return $_POST[$key];
        }
    
        if (isset($_SESSION[$key])) {
            return $_SESSION[$key];
        }
    
        return '';
    }
    
    function set_value($key, $value) {
        $_SESSION[$key] = $value;
        return $value;
    }
    
    ?>
    <html>
        <head>
            <title>Obtain Access Token</title>
        </head>
        <body>
            <?php
    
            define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/facebook-php-sdk-v4-4.0-dev/src/Facebook/');
            require __DIR__ . '/facebook-php-sdk-v4-4.0-dev/autoload.php';
    
            use Facebook\Entities\AccessToken;
            use Facebook\FacebookSession;
            use Facebook\FacebookRedirectLoginHelper;
            use Facebook\FacebookRequest;
    
            $redirectUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
    
            if (isset($_POST['get_tokens'])) {
                $appId = set_value('app_id', $_POST['app_id']);
                $appSecret = set_value('app_secret', $_POST['app_secret']);
    
                FacebookSession::setDefaultApplication($appId, $appSecret);
                $helper = new FacebookRedirectLoginHelper($redirectUrl);
                $userToken = trim($_POST['user_token']);
    
                if (strlen($userToken) == 0) {
                    // Obtain a new user token
                    $scopes = array('manage_pages');
                    $loginUrl = $helper->getLoginUrl($scopes);
    
                    echo "Login at <a href=\"$loginUrl\" target=\"_blank\">$loginUrl</a> to obtain a user access token.<br /><br />";
                }
            }
    
            if (isset($_GET['code']) && strlen(trim($_GET['code'])) > 0) {
                $appId = get_value('app_id');
                $appSecret = get_value('app_secret');
                FacebookSession::setDefaultApplication($appId, $appSecret);
    
                $helper = new FacebookRedirectLoginHelper($redirectUrl);
                $session = $helper->getSessionFromRedirect();
    
                // User access token obtained
                $accessToken = $session->getAccessToken();
                $longLivedAccessToken = $accessToken->extend();
    
                $request = new FacebookRequest($session, 'GET', '/me/accounts?fields=name,access_token,perms');
                $pageList = $request->execute()->getGraphObject()->asArray();
    
                set_value('user_token', (string) $accessToken);
                set_value('long_user_token', (string) $longLivedAccessToken);
                set_value('page_tokens', print_r($pageList, true));
    
                header('Location: token.php');
            }
    
            ?>
            <form method="post" action="token.php">
            App ID
            <br /><input type="text" name="app_id" value="<?php echo get_value('app_id') ?>" /><br /><br />
            App Secret
            <br /><input type="text" name="app_secret" value="<?php echo get_value('app_secret') ?>" /><br /><br />
            User Token
            <br /><input type="text" name="user_token" value="<?php echo get_value('user_token') ?>" size="50" /><br /><br />
            Long-term User Token
            <br /><input type="text" name="long_user_token" value="<?php echo get_value('long_user_token') ?>" size="50" />
            <br /><br />
            <textarea rows="10" cols="80"><?php echo get_value('page_tokens') ?></textarea><br /><br />
            <input type="submit" name="get_tokens" value="Get Tokens" />
        </form>
        </body>
    </html>

    Once you get the results, you will see the list of pages that you have access to along with their corresponding access tokens and IDs in the textarea field.

    To verify your access tokens, paste them to the Access Token debugger at https://developers.facebook.com/tools/debug/access_token
    This will show you the expiration time, allowed scopes and other information.

    Once that’s complete, you’ll need to edit the plugin files. Go to your WordPress Dashboard > Plugins > Editor. Select the “Social Count Plus” plugin from the dropdown and edit the social-count-plus/includes/counters/class-social-count-plus-facebook-counter.php file (If you don’t see it right away, click on the social-count-plus/includes/social-count-plus-functions.php file first).

    Look for the line:
    protected $api_url = 'https://graph.facebook.com/';
    Change it to
    protected $api_url = 'https://graph.facebook.com/v2.3/';

    Look for the line:
    $this->connection = wp_remote_get( $this->api_url . $settings['facebook_id'], $params );
    and change it to:
    $this->connection = wp_remote_get( $this->api_url . $settings['facebook_id'] . '?access_token=<access_token_here>', $params );
    where <access_token_here> is the access token that you obtained for the Facebook page.

    Save the file and run a System report (WordPress Dashboard > Settings > Social Count Plus > System Status > Get System Report), and you will see the results for the Facebook request (assuming Facebook is enabled and a numeric Facebook ID) is set.

    Hope this helps until there’s a proper fix from the plugin author. 🙂

Viewing 2 replies - 1 through 2 (of 2 total)