PluginProbe ʕ •ᴥ•ʔ
Wp Social Login and Register Social Counter / 3.2.1
Wp Social Login and Register Social Counter v3.2.1
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 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.8.1 1.8.2 1.8.3 1.8.5 1.8.6 1.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.8 2.2.9 3.0.0 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0
wp-social / lib / provider / counter / youtube-counter.php
wp-social / lib / provider / counter Last commit date
comments-counter.php 5 years ago counter-interface.php 5 years ago counter.php 5 years ago dribbble-counter.php 5 years ago facebook-counter.php 3 years ago instagram-counter.php 10 months ago no-provider-counter.php 5 years ago pinterest-counter.php 2 years ago posts-counter.php 5 years ago tiktok-counter.php 1 week ago twitter-counter.php 1 year ago youtube-counter.php 5 years ago
youtube-counter.php
108 lines
1 <?php
2
3 namespace WP_Social\Lib\Provider\Counter;
4
5
6 class Youtube_Counter extends Counter {
7
8 public static $provider_key = 'youtube';
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['key'])) {
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 $username = $this->global_options['id'];
60 $api_key = $this->global_options['key'];
61 $type = $this->global_options['type'];
62
63 $tran_key = self::get_transient_key(md5($type.$username));
64 $trans_value = get_transient($tran_key);
65
66 if(false === $trans_value) {
67
68 try {
69
70 if($type == 'Channel') {
71 $url = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&id='.trim($username).'&key='.trim($api_key);
72 } else {
73 $url = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername='.trim($username).'&key='.trim($api_key);
74 }
75
76 $data = xsc_remote_get($url);
77
78 $subscriber = empty($data['items'][0]['statistics']['subscriberCount']) ? 0 : intval($data['items'][0]['statistics']['subscriberCount']);
79 //$videos = empty($data['items'][0]['statistics']['videoCount']) ? 0 : intval($data['items'][0]['statistics']['videoCount']);
80 //$views = empty($data['items'][0]['statistics']['viewCount']) ? 0 : intval($data['items'][0]['statistics']['viewCount']);
81
82
83 /**
84 * Updating transient cache
85 */
86
87 $expiration_time = empty($global_cache_time) ? 43200: intval($global_cache_time);
88
89 set_transient($tran_key, $subscriber, $expiration_time);
90
91 update_option(self::get_last_cache_key(), time());
92
93 } catch(Exception $e) {
94
95 /**
96 * todo - AR; need to get confirmation what shoud we do in case there are errors from Product Owner
97 * for now returning 0;
98 *
99 */
100 $subscriber = 0;
101 }
102
103 return $subscriber;
104 }
105
106 return $trans_value;
107 }
108 }