Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(function() {
- $('.vote-btn').on('click', function() {
- let page_id = $(this).data('page-id');
- FB.getLoginStatus(function(response) {
- if(response.status === 'connected') {
- makeVote(page_id);
- } else {
- loginWithFacebook(),
- }
- }
- });
- };
- function makeVote(id) {
- $.post('', {id: id}, function() {
- // Success
- }).fail(function() {
- // Fail
- }).always(function() {
- $('.loader').hide();
- });
- }
- function logInWithFacebook() {
- FB.login(function(response) {
- if (response.authResponse) {
- alert('You are logged in & cookie set!');
- // Now you can redirect the user or do an AJAX request to
- // a PHP script that grabs the signed request from the cookie.
- } else {
- alert('User cancelled login or did not fully authorize.');
- }
- });
- return false;
- }
- namespace AppControllers;
- use SoberControllerController;
- use FacebookFacebook;
- use FacebookExceptionsFacebookResponseException;
- use FacebookExceptionsFacebookSDKException;
- class TemplateVoting {
- private $post_id:
- private $fb_id;
- private $wpdb;
- public function __construct() {
- $this->wpdb = $GLOBALS['wpdb'];
- }
- public function __after() {
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $this->post_id = $_POST['id'];
- $this->addVote();
- }
- }
- private function addVote() {
- if( !is_int( $this->fb_id() ) ) {
- return;
- }
- $this->wpdb->insert("wp_post_votings", [
- "post_id" => $this->post_id,
- "fb_id" => $this->fb_id(),
- ]);
- }
- private function fb_id() {
- try {
- $response = $fb->get('/me?fields=id', $this->getToken());
- } catch(FacebookExceptionsFacebookResponseException $e) {
- return $e->getMessage();
- } catch(FacebookExceptionsFacebookSDKException $e) {
- return $e->getMessage();
- }
- $user = $response->getGraphUser();
- return $user['id'];
- }
- private function initApi() {
- $facebook = new Facebook([
- 'app_id' => env('FB_APP_ID'),
- 'app_secret' => env('FB_APP_SECRET'),
- 'default_graph_version' => env('v3.3'),
- ]);
- }
- private function getToken() {
- $fb => $this-> initApi();
- $helper = $fb->getJavaScriptHelper();
- try {
- $accessToken = $helper->getAccessToken();
- } catch(FacebookResponseException $e) {
- return $e->getMessage();
- } catch(FacebookSDKException $e) {
- return $e->getMessage();
- }
- if (!isset($accessToken)) {
- return '';
- }
- return $accessToken->getValue();
- }
- }
- (function(d, s, id){
- var js, fjs = d.getElementsByTagName(s)[0];
- if (d.getElementById(id)) {return;}
- js = d.createElement(s); js.id = id;
- js.src = '//connect.facebook.net/en_US/sdk.js';
- fjs.parentNode.insertBefore(js, fjs);
- }(document, 'script', 'facebook-jssdk'));
- window.fbAsyncInit = function() {
- FB.init({
- appId: 'app_id', // True app_id is inserted in real project
- cookie: true, // This is important, it's not enabled by default
- version: 'v2.10'
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement