instagram-counter.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WP_Social\Lib\Counter; |
| 4 | |
| 5 | |
| 6 | class Instagram_Counter { |
| 7 | |
| 8 | public $enabled = false; |
| 9 | |
| 10 | /** |
| 11 | * If user id is empty then expired is false |
| 12 | * if transient time is less than time then it is true |
| 13 | * |
| 14 | * @var bool |
| 15 | */ |
| 16 | public $cache_expired = false; |
| 17 | |
| 18 | public $user_id = ''; |
| 19 | |
| 20 | private $debug = ''; |
| 21 | |
| 22 | |
| 23 | public function load() { |
| 24 | |
| 25 | $opt = get_option('xs_counter_providers_data', []); |
| 26 | |
| 27 | if(!empty($opt['social']['instagram']['enable'])) { |
| 28 | |
| 29 | $this->enabled = true; |
| 30 | |
| 31 | if(!empty($opt['social']['instagram']['id'])) { |
| 32 | |
| 33 | $this->user_id = $opt['social']['instagram']['id']; |
| 34 | |
| 35 | $get_transient_time = get_transient(self::get_transient_timeout_key()); |
| 36 | |
| 37 | $this->cache_expired = $get_transient_time < time(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Also set the def value and label and etc etc, if needed |
| 42 | */ |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | public static function get_last_cache_key() { |
| 49 | |
| 50 | return '_xs_social_instagram_last_cached'; |
| 51 | } |
| 52 | |
| 53 | public static function get_transient_key() { |
| 54 | |
| 55 | return '_xs_social_instagram_count_'; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | public static function get_transient_timeout_key() { |
| 60 | |
| 61 | return 'timeout_' . self::get_transient_key(); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | public static function is_expired() { |
| 66 | |
| 67 | $opt = get_option('xs_counter_providers_data', []); |
| 68 | |
| 69 | |
| 70 | $get_transient_time = get_transient(self::get_transient_timeout_key()); |
| 71 | |
| 72 | return $get_transient_time < time(); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | public static function get_user_id() { |
| 77 | |
| 78 | $xsc_options_save = get_option('xs_counter_providers_data', []); |
| 79 | |
| 80 | return empty($xsc_options_save['social']['instagram']['id']) ? '' : $xsc_options_save['social']['instagram']['id']; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * |
| 86 | * @param $count |
| 87 | * @param int $expire - we will cache this for given amount of second |
| 88 | * |
| 89 | * @return bool |
| 90 | */ |
| 91 | public function cache_instagram_return($count, $expire = 86400) { |
| 92 | |
| 93 | $tran_key = self::get_transient_key(); |
| 94 | |
| 95 | $conf['followed_by'] = $count; |
| 96 | $conf['fetch_time'] = time(); |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * If every thing goes okay |
| 101 | */ |
| 102 | set_transient($tran_key, $conf, $expire); |
| 103 | |
| 104 | update_option(self::get_last_cache_key(), $count); |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | function get_count($user_id) { |
| 111 | |
| 112 | $url = 'https://www.instagram.com/' . $user_id . '/?__a=1'; |
| 113 | |
| 114 | try { |
| 115 | $request = wp_remote_get($url); |
| 116 | |
| 117 | if(!is_wp_error($request)) { |
| 118 | |
| 119 | $body = wp_remote_retrieve_body($request); |
| 120 | $dt = json_decode($body); |
| 121 | } |
| 122 | |
| 123 | } catch(\Exception $ex) { |
| 124 | |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * @return string |
| 131 | */ |
| 132 | public function getDebug() { |
| 133 | return $this->debug; |
| 134 | } |
| 135 | |
| 136 | } |