Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. $(function() {
  2. $('.vote-btn').on('click', function() {
  3.  
  4. let page_id = $(this).data('page-id');
  5.  
  6. FB.getLoginStatus(function(response) {
  7. if(response.status === 'connected') {
  8. makeVote(page_id);
  9. } else {
  10. loginWithFacebook(),
  11. }
  12. }
  13. });
  14. };
  15.  
  16. function makeVote(id) {
  17. $.post('', {id: id}, function() {
  18. // Success
  19. }).fail(function() {
  20. // Fail
  21. }).always(function() {
  22. $('.loader').hide();
  23. });
  24. }
  25.  
  26. function logInWithFacebook() {
  27. FB.login(function(response) {
  28. if (response.authResponse) {
  29. alert('You are logged in & cookie set!');
  30. // Now you can redirect the user or do an AJAX request to
  31. // a PHP script that grabs the signed request from the cookie.
  32. } else {
  33. alert('User cancelled login or did not fully authorize.');
  34. }
  35. });
  36. return false;
  37. }
  38.  
  39. namespace AppControllers;
  40.  
  41. use SoberControllerController;
  42. use FacebookFacebook;
  43. use FacebookExceptionsFacebookResponseException;
  44. use FacebookExceptionsFacebookSDKException;
  45.  
  46. class TemplateVoting {
  47.  
  48. private $post_id:
  49. private $fb_id;
  50. private $wpdb;
  51.  
  52. public function __construct() {
  53. $this->wpdb = $GLOBALS['wpdb'];
  54. }
  55.  
  56. public function __after() {
  57. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  58.  
  59. $this->post_id = $_POST['id'];
  60. $this->addVote();
  61. }
  62. }
  63.  
  64. private function addVote() {
  65. if( !is_int( $this->fb_id() ) ) {
  66. return;
  67. }
  68. $this->wpdb->insert("wp_post_votings", [
  69. "post_id" => $this->post_id,
  70. "fb_id" => $this->fb_id(),
  71. ]);
  72. }
  73.  
  74. private function fb_id() {
  75. try {
  76. $response = $fb->get('/me?fields=id', $this->getToken());
  77. } catch(FacebookExceptionsFacebookResponseException $e) {
  78. return $e->getMessage();
  79. } catch(FacebookExceptionsFacebookSDKException $e) {
  80. return $e->getMessage();
  81. }
  82.  
  83. $user = $response->getGraphUser();
  84. return $user['id'];
  85. }
  86.  
  87. private function initApi() {
  88. $facebook = new Facebook([
  89. 'app_id' => env('FB_APP_ID'),
  90. 'app_secret' => env('FB_APP_SECRET'),
  91. 'default_graph_version' => env('v3.3'),
  92. ]);
  93. }
  94.  
  95. private function getToken() {
  96. $fb => $this-> initApi();
  97.  
  98. $helper = $fb->getJavaScriptHelper();
  99.  
  100. try {
  101. $accessToken = $helper->getAccessToken();
  102. } catch(FacebookResponseException $e) {
  103. return $e->getMessage();
  104. } catch(FacebookSDKException $e) {
  105. return $e->getMessage();
  106. }
  107.  
  108. if (!isset($accessToken)) {
  109. return '';
  110. }
  111.  
  112. return $accessToken->getValue();
  113. }
  114.  
  115. }
  116.  
  117. (function(d, s, id){
  118. var js, fjs = d.getElementsByTagName(s)[0];
  119. if (d.getElementById(id)) {return;}
  120. js = d.createElement(s); js.id = id;
  121. js.src = '//connect.facebook.net/en_US/sdk.js';
  122. fjs.parentNode.insertBefore(js, fjs);
  123. }(document, 'script', 'facebook-jssdk'));
  124.  
  125.  
  126. window.fbAsyncInit = function() {
  127. FB.init({
  128. appId: 'app_id', // True app_id is inserted in real project
  129. cookie: true, // This is important, it's not enabled by default
  130. version: 'v2.10'
  131. });
  132. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement