PluginProbe
Wp Social Login and Register Social Counter / 3.0.7
Wp Social Login and Register Social Counter v3.0.7
3.2.1 trunk 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.10 1.3.11 1.3.2 1.3.3 1.3.4 1.3.6 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.4 1.4.5 1.4.6 1.4.8 1.4.9 All 70 releases
wp-social / lib / provider / counter / twitter-counter.php
twitter-counter.php
117 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WP_Social\Lib\Provider\Counter;
4
5
6 class Twitter_Counter extends Counter {
7
8 public static $provider_key = 'twitter';
9
10 private $global_options;
11
12 public function need_to_call_legacy_function() {
13
14 return false;
15 }
16
17 public static function get_transient_key($user = '') {
18
19 return '_xs_social_'.self::$provider_key.'_count_'.trim($user);
20 }
21
22
23 public static function get_transient_timeout_key() {
24
25 return 'timeout_' . self::get_transient_key();
26 }
27
28
29 public static function get_last_cache_key() {
30
31 return '_xs_social_'.self::$provider_key.'_last_cached';
32 }
33
34
35 public function set_config_data($conf_array) {
36
37 $this->global_options = $conf_array;
38
39 return $this;
40 }
41
42
43 /**
44 *
45 * @param int $global_cache_time - default is 12 hours
46 * @return mixed
47 */
48 public function get_count($global_cache_time = 43200) {
49
50 if(empty($this->global_options['id']) || empty($this->global_options['api'])) {
51
52 /**
53 * Client does not set up his credential, so just show defaults value
54 */
55
56 return empty($this->global_options['data']['value']) ? 0 : $this->global_options['data']['value'];
57 }
58
59 /**
60 * At this point client has set up his credentials and want to grab show actual values
61 *
62 */
63 $username = $this->global_options['id'];
64 $tran_key = self::get_transient_key($username);
65 $result = 0;
66 $trans_value = get_transient($tran_key);
67
68 if(false === $trans_value) {
69
70 try {
71
72 $token = get_option('xs_counter_twitter_token', '');
73
74 $args = array(
75 'httpversion' => '1.1',
76 'blocking' => true,
77 'timeout' => 10,
78 'headers' => array(
79 'Authorization' => "Bearer $token",
80 'Accept-Language' => 'en',
81 ),
82 );
83
84 add_filter('https_ssl_verify', '__return_false');
85 $api_url = 'https://api.twitter.com/1.1/users/show.json?screen_name='.$username;
86 $response = xsc_remote_get($api_url, true, $args);
87
88 /**
89 * We will show actual count always if user gives the access token
90 * even if it is 0!
91 */
92 if(isset($response['followers_count'])) {
93 $result = intval($response['followers_count']);
94 }
95
96 $expiration_time = empty($global_cache_time) ? 43200: intval($global_cache_time);
97
98 set_transient($tran_key, $result, $expiration_time);
99
100 update_option(self::get_last_cache_key(), time());
101
102 } catch(\Exception $ex) {
103
104 /**
105 * todo - AR; need to get confirmation what should we do in case there are errors from Product Owner
106 * for now returning 0;
107 *
108 */
109 $result = 0;
110 }
111
112 return $result;
113 }
114
115 return $trans_value;
116 }
117 }