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
2 weeks ago
twitter-counter.php
1 year ago
youtube-counter.php
5 years ago
tiktok-counter.php
138 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WP_Social\Lib\Provider\Counter; |
| 4 | |
| 5 | require_once( WSLU_LOGIN_PLUGIN . 'lib/composer/vendor/autoload.php' ); |
| 6 | |
| 7 | use TikTok\User\User; |
| 8 | use TikTok\Request\Params; |
| 9 | |
| 10 | class Tiktok_Counter extends Counter { |
| 11 | |
| 12 | public static $provider_key = 'tiktok'; |
| 13 | private $global_options; |
| 14 | |
| 15 | /** |
| 16 | * If we need to call legacy function |
| 17 | * |
| 18 | * @return bool |
| 19 | */ |
| 20 | public function need_to_call_legacy_function() { |
| 21 | |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Get transient key |
| 27 | * |
| 28 | * @return string |
| 29 | */ |
| 30 | public static function get_transient_key() { |
| 31 | |
| 32 | return '_xs_social_tiktok_count_'; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get transient timeout key |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | public static function get_transient_timeout_key() { |
| 41 | |
| 42 | return 'timeout_' . self::get_transient_key(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get last cache key |
| 47 | * |
| 48 | * @return string |
| 49 | */ |
| 50 | public static function get_last_cache_key() { |
| 51 | |
| 52 | return '_xs_social_'.self::$provider_key.'_last_cached'; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Set config data |
| 57 | * |
| 58 | * @param array $conf_array |
| 59 | * |
| 60 | * @return $this |
| 61 | */ |
| 62 | public function set_config_data( $conf_array ) { |
| 63 | |
| 64 | $this->global_options = $conf_array; |
| 65 | |
| 66 | return $this; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get cached count |
| 71 | * |
| 72 | * @return int |
| 73 | */ |
| 74 | public function get_cached_count() { |
| 75 | |
| 76 | return $this->get_count( $this->cache_time ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get default count |
| 81 | * |
| 82 | * @return int |
| 83 | */ |
| 84 | public function get_default_fan_count() { |
| 85 | |
| 86 | return empty($this->global_options['data']['value']) ? 0 : $this->global_options['data']['value']; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get follwers count from API |
| 91 | * |
| 92 | * @param int $global_cache_time |
| 93 | * |
| 94 | * @return int |
| 95 | */ |
| 96 | public function get_count( $global_cache_time = 43200 ) { |
| 97 | |
| 98 | if( empty( $this->global_options['api'] ) ) { |
| 99 | |
| 100 | // Client does not set up his credential, so just show defaults value |
| 101 | return empty($this->global_options['data']['value']) ? 0 : $this->global_options['data']['value']; |
| 102 | } |
| 103 | |
| 104 | $access_token = isset( $this->global_options['api'] ) ? $this->global_options['api'] : ''; |
| 105 | $tran_key = self::get_transient_key( md5( $access_token ) ); |
| 106 | $trans_value = get_transient( $tran_key ); |
| 107 | |
| 108 | if( false === $trans_value ) { |
| 109 | |
| 110 | $config = array( // instantiation config params |
| 111 | 'access_token' => $access_token |
| 112 | ); |
| 113 | |
| 114 | // instantiate the user |
| 115 | $user = new User( $config ); |
| 116 | |
| 117 | $params = Params::getFieldsParam( |
| 118 | array( |
| 119 | 'follower_count', // scope user.info.stats |
| 120 | ) |
| 121 | ); |
| 122 | |
| 123 | // get user info |
| 124 | $userInfo = $user->getSelf( $params ); |
| 125 | $followers_count = isset($userInfo['data']['user']['follower_count']) ? $userInfo['data']['user']['follower_count'] : 0; |
| 126 | |
| 127 | //Updating transient cache |
| 128 | $expiration_time = empty( $global_cache_time ) ? 43200: intval( $global_cache_time ); |
| 129 | |
| 130 | set_transient( $tran_key, $followers_count, $expiration_time ); |
| 131 | update_option( self::get_last_cache_key(), time() ); |
| 132 | |
| 133 | } |
| 134 | |
| 135 | return $trans_value; |
| 136 | } |
| 137 | } |
| 138 |