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
4 years ago
no-provider-counter.php
5 years ago
pinterest-counter.php
5 years ago
posts-counter.php
5 years ago
twitter-counter.php
5 years ago
youtube-counter.php
5 years ago
instagram-counter.php
131 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WP_Social\Lib\Provider\Counter; |
| 4 | |
| 5 | class Instagram_Counter extends Counter { |
| 6 | |
| 7 | public static $provider_key = 'instagram'; |
| 8 | |
| 9 | private $global_options; |
| 10 | |
| 11 | |
| 12 | public function need_to_call_legacy_function() { |
| 13 | |
| 14 | return false; |
| 15 | } |
| 16 | |
| 17 | |
| 18 | public static function get_transient_key() { |
| 19 | |
| 20 | return '_xs_social_instagram_count_'; |
| 21 | } |
| 22 | |
| 23 | |
| 24 | public static function get_transient_timeout_key() { |
| 25 | |
| 26 | return 'timeout_' . self::get_transient_key(); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | public static function get_last_cache_key() { |
| 31 | |
| 32 | return '_xs_social_'.self::$provider_key.'_last_cached'; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | public function set_config_data($conf_array) { |
| 37 | |
| 38 | $this->global_options = $conf_array; |
| 39 | |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | public function get_cached_count() { |
| 44 | |
| 45 | return $this->get_count($this->cache_time); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * todo - move it to parent class and make it available for all |
| 50 | * |
| 51 | */ |
| 52 | public function get_default_fan_count() { |
| 53 | |
| 54 | return empty($this->global_options['data']['value']) ? 0 : $this->global_options['data']['value']; |
| 55 | } |
| 56 | |
| 57 | public function get_count($global_cache_time = 43200) { |
| 58 | |
| 59 | if(empty($this->global_options['access_token']) || empty($this->global_options['user_name']) || empty($this->global_options['user_id'])) { |
| 60 | |
| 61 | /** |
| 62 | * Client does not set up his access token, so just show defaults value |
| 63 | */ |
| 64 | |
| 65 | return empty($this->global_options['data']['value']) ? 0 : $this->global_options['data']['value']; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * At this point client has set up his credentials and want to grab show actual values |
| 71 | * |
| 72 | */ |
| 73 | $tran_key = self::get_transient_key(); |
| 74 | $trans_value = get_transient($tran_key); |
| 75 | |
| 76 | if(false === $trans_value) { |
| 77 | |
| 78 | $access_token = $this->global_options['access_token']; |
| 79 | $user_id = $this->global_options['user_id']; |
| 80 | $username = $this->global_options['user_name']; |
| 81 | |
| 82 | $url='https://graph.facebook.com/v11.0/'.$user_id.'?fields=business_discovery.username('.$username.'){username,follows_count,followers_count}&access_token=' . $access_token .''; |
| 83 | |
| 84 | try { |
| 85 | |
| 86 | $get_request = wp_remote_get($url, array('timeout' => 40, 'sslverify' => false,)); |
| 87 | |
| 88 | if(is_wp_error($get_request)) { |
| 89 | |
| 90 | $result = -1; |
| 91 | |
| 92 | } else { |
| 93 | |
| 94 | $body = wp_remote_retrieve_body($get_request); |
| 95 | $dt = json_decode($body); |
| 96 | |
| 97 | /** |
| 98 | * todo - this is a temporary fix for immediate release |
| 99 | */ |
| 100 | if(!empty($dt->error)) { |
| 101 | |
| 102 | $result = $this->get_default_fan_count(); |
| 103 | |
| 104 | } else { |
| 105 | |
| 106 | $result = empty($dt['business_discovery']['followers_count']) ? 0 : intval($dt['business_discovery']['followers_count']); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | $expiration_time = empty($global_cache_time) ? 43200: intval($global_cache_time); |
| 111 | |
| 112 | set_transient($tran_key, $result, $expiration_time); |
| 113 | update_option(self::get_last_cache_key(), time()); |
| 114 | |
| 115 | } catch(Exception $e) { |
| 116 | |
| 117 | /** |
| 118 | * todo - AR; need to get confirmation what should we do in case there are errors from Product Owner |
| 119 | * for now returning 0; |
| 120 | * |
| 121 | */ |
| 122 | $result = 0; |
| 123 | } |
| 124 | |
| 125 | return $result; |
| 126 | } |
| 127 | |
| 128 | return $trans_value; |
| 129 | } |
| 130 | } |
| 131 |