Bez popisu

twitter.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. use NSL\Notices;
  3. class NextendSocialProviderTwitter extends NextendSocialProvider {
  4. /** @var NextendSocialProviderTwitterClient */
  5. protected $client;
  6. protected $color = '#4ab3f4';
  7. protected $svg = '<svg xmlns="http://www.w3.org/2000/svg"><path fill="#fff" d="M16.327 3.007A5.07 5.07 0 0 1 20.22 4.53a8.207 8.207 0 0 0 2.52-.84l.612-.324a4.78 4.78 0 0 1-1.597 2.268 2.356 2.356 0 0 1-.54.384v.012A9.545 9.545 0 0 0 24 5.287v.012a7.766 7.766 0 0 1-1.67 1.884l-.768.612a13.896 13.896 0 0 1-9.874 13.848c-2.269.635-4.655.73-6.967.276a16.56 16.56 0 0 1-2.895-.936 10.25 10.25 0 0 1-1.394-.708L0 20.023a8.44 8.44 0 0 0 1.573.06c.48-.084.96-.06 1.405-.156a10.127 10.127 0 0 0 2.956-1.056 5.41 5.41 0 0 0 1.333-.852 4.44 4.44 0 0 1-1.465-.264 4.9 4.9 0 0 1-3.12-3.108c.73.134 1.482.1 2.198-.096a3.457 3.457 0 0 1-1.609-.636A4.651 4.651 0 0 1 .953 9.763c.168.072.336.156.504.24.334.127.68.22 1.033.276.216.074.447.095.673.06H3.14c-.248-.288-.653-.468-.901-.78a4.91 4.91 0 0 1-1.105-4.404 5.62 5.62 0 0 1 .528-1.26c.008 0 .017.012.024.012.13.182.28.351.445.504a8.88 8.88 0 0 0 1.465 1.38 14.43 14.43 0 0 0 6.018 2.868 9.065 9.065 0 0 0 2.21.288 4.448 4.448 0 0 1 .025-2.28 4.771 4.771 0 0 1 2.786-3.252 5.9 5.9 0 0 1 1.093-.336l.6-.072z"/></svg>';
  8. protected $sync_fields = array(
  9. 'description' => array(
  10. 'label' => 'Bio',
  11. 'node' => 'me'
  12. ),
  13. 'lang' => array(
  14. 'label' => 'Language',
  15. 'node' => 'me'
  16. ),
  17. 'location' => array(
  18. 'label' => 'Location',
  19. 'node' => 'me'
  20. ),
  21. 'created_at' => array(
  22. 'label' => 'Register date',
  23. 'node' => 'me'
  24. ),
  25. 'profile_url' => array(
  26. 'label' => 'Profile URL',
  27. 'node' => 'me'
  28. ),
  29. 'screen_name' => array(
  30. 'label' => 'Screen name',
  31. 'node' => 'me'
  32. ),
  33. 'url' => array(
  34. 'label' => 'Owned website',
  35. 'node' => 'me'
  36. )
  37. );
  38. public function __construct() {
  39. $this->id = 'twitter';
  40. $this->label = 'Twitter';
  41. $this->path = dirname(__FILE__);
  42. $this->requiredFields = array(
  43. 'consumer_key' => 'Consumer Key',
  44. 'consumer_secret' => 'Consumer Secret'
  45. );
  46. $this->oauthRedirectBehavior = 'default_redirect_but_app_has_restriction';
  47. parent::__construct(array(
  48. 'consumer_key' => '',
  49. 'consumer_secret' => '',
  50. 'login_label' => 'Continue with <b>Twitter</b>',
  51. 'register_label' => 'Sign up with <b>Twitter</b>',
  52. 'link_label' => 'Link account with <b>Twitter</b>',
  53. 'unlink_label' => 'Unlink account from <b>Twitter</b>',
  54. 'profile_image_size' => 'normal'
  55. ));
  56. }
  57. protected function forTranslation() {
  58. __('Continue with <b>Twitter</b>', 'nextend-facebook-connect');
  59. __('Sign up with <b>Twitter</b>', 'nextend-facebook-connect');
  60. __('Link account with <b>Twitter</b>', 'nextend-facebook-connect');
  61. __('Unlink account from <b>Twitter</b>', 'nextend-facebook-connect');
  62. }
  63. public function validateSettings($newData, $postedData) {
  64. $newData = parent::validateSettings($newData, $postedData);
  65. foreach ($postedData AS $key => $value) {
  66. switch ($key) {
  67. case 'tested':
  68. if ($postedData[$key] == '1' && (!isset($newData['tested']) || $newData['tested'] != '0')) {
  69. $newData['tested'] = 1;
  70. } else {
  71. $newData['tested'] = 0;
  72. }
  73. break;
  74. case 'consumer_key':
  75. case 'consumer_secret':
  76. $newData[$key] = trim(sanitize_text_field($value));
  77. if ($this->settings->get($key) !== $newData[$key]) {
  78. $newData['tested'] = 0;
  79. }
  80. if (empty($newData[$key])) {
  81. Notices::addError(sprintf(__('The %1$s entered did not appear to be a valid. Please enter a valid %2$s.', 'nextend-facebook-connect'), $this->requiredFields[$key], $this->requiredFields[$key]));
  82. }
  83. break;
  84. case 'profile_image_size':
  85. $newData[$key] = trim(sanitize_text_field($value));
  86. break;
  87. }
  88. }
  89. return $newData;
  90. }
  91. /**
  92. * @return NextendSocialProviderTwitterClient
  93. */
  94. public function getClient() {
  95. if ($this->client === null) {
  96. require_once dirname(__FILE__) . '/twitter-client.php';
  97. $this->client = new NextendSocialProviderTwitterClient($this->id, $this->settings->get('consumer_key'), $this->settings->get('consumer_secret'));
  98. $this->client->setRedirectUri($this->getRedirectUriForOAuthFlow());
  99. }
  100. return $this->client;
  101. }
  102. /**
  103. * @return array|mixed|object
  104. * @throws Exception
  105. */
  106. protected function getCurrentUserInfo() {
  107. $response = $this->getClient()
  108. ->get('account/verify_credentials', array(
  109. 'include_email' => 'true',
  110. 'include_entities' => 'false',
  111. 'skip_status' => 'true'
  112. ));
  113. if (isset($response['id']) && isset($response['id_str'])) {
  114. // On 32bit and Windows server, we must copy id_str to id as the id int representation won't be OK
  115. $response['id'] = $response['id_str'];
  116. }
  117. return $response;
  118. }
  119. public function getMe() {
  120. return $this->authUserData;
  121. }
  122. /**
  123. * @param $key
  124. *
  125. * @return string
  126. */
  127. public function getAuthUserData($key) {
  128. switch ($key) {
  129. case 'id':
  130. return $this->authUserData['id'];
  131. case 'email':
  132. return !empty($this->authUserData['email']) ? $this->authUserData['email'] : '';
  133. case 'name':
  134. return $this->authUserData['name'];
  135. case 'username':
  136. return $this->authUserData['screen_name'];
  137. case 'first_name':
  138. $name = explode(' ', $this->getAuthUserData('name'), 2);
  139. return isset($name[0]) ? $name[0] : '';
  140. case 'last_name':
  141. $name = explode(' ', $this->getAuthUserData('name'), 2);
  142. return isset($name[1]) ? $name[1] : '';
  143. case 'picture':
  144. $profile_image_size = $this->settings->get('profile_image_size');
  145. $profile_image = $this->authUserData['profile_image_url_https'];
  146. $avatar_url = '';
  147. if (!empty($profile_image)) {
  148. switch ($profile_image_size) {
  149. case 'mini':
  150. $avatar_url = str_replace('_normal.', '_' . $profile_image_size . '.', $profile_image);
  151. break;
  152. case 'normal':
  153. $avatar_url = $profile_image;
  154. break;
  155. case 'bigger':
  156. $avatar_url = str_replace('_normal.', '_' . $profile_image_size . '.', $profile_image);
  157. break;
  158. case 'original':
  159. $avatar_url = str_replace('_normal.', '.', $profile_image);
  160. break;
  161. }
  162. }
  163. return $avatar_url;
  164. }
  165. return parent::getAuthUserData($key);
  166. }
  167. public function syncProfile($user_id, $provider, $access_token) {
  168. if ($this->needUpdateAvatar($user_id)) {
  169. if ($this->getAuthUserData('picture')) {
  170. $this->updateAvatar($user_id, $this->getAuthUserData('picture'));
  171. }
  172. }
  173. $this->storeAccessToken($user_id, $access_token);
  174. }
  175. public function deleteLoginPersistentData() {
  176. parent::deleteLoginPersistentData();
  177. if ($this->client !== null) {
  178. $this->client->deleteLoginPersistentData();
  179. }
  180. }
  181. public function getAvatar($user_id) {
  182. if (!$this->isUserConnected($user_id)) {
  183. return false;
  184. }
  185. $picture = $this->getUserData($user_id, 'profile_picture');
  186. if (!$picture || $picture == '') {
  187. return false;
  188. }
  189. return $picture;
  190. }
  191. }
  192. NextendSocialLogin::addProvider(new NextendSocialProviderTwitter);