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
5 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
pinterest-counter.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WP_Social\Lib\Provider\Counter; |
| 4 | |
| 5 | class Pinterest_Counter extends Counter { |
| 6 | |
| 7 | public static $provider_key = 'pinterest'; |
| 8 | |
| 9 | private $global_options; |
| 10 | |
| 11 | public function need_to_call_legacy_function() { |
| 12 | |
| 13 | return false; |
| 14 | } |
| 15 | |
| 16 | public static function get_transient_key($user = '') { |
| 17 | |
| 18 | return '_xs_social_'.self::$provider_key.'_count_'.trim($user); |
| 19 | } |
| 20 | |
| 21 | |
| 22 | public static function get_transient_timeout_key() { |
| 23 | |
| 24 | return 'timeout_' . self::get_transient_key(); |
| 25 | } |
| 26 | |
| 27 | |
| 28 | public static function get_last_cache_key() { |
| 29 | |
| 30 | return '_xs_social_'.self::$provider_key.'_last_cached'; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | public function set_config_data($conf_array) { |
| 35 | |
| 36 | $this->global_options = $conf_array; |
| 37 | |
| 38 | return $this; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * |
| 44 | * @param int $global_cache_time - default is 12 hours |
| 45 | * @return mixed |
| 46 | */ |
| 47 | public function get_count($global_cache_time = 43200) { |
| 48 | |
| 49 | if(empty($this->global_options['username'])) { |
| 50 | |
| 51 | /** |
| 52 | * Client does not set up his credential, so just show defaults value |
| 53 | */ |
| 54 | |
| 55 | return empty($this->global_options['data']['value']) ? 0 : $this->global_options['data']['value']; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * At this point client has set up his credentials and want to grab show actual values |
| 60 | * |
| 61 | */ |
| 62 | $username = $this->global_options['username']; |
| 63 | $tran_key = self::get_transient_key($username); |
| 64 | $result = 0; |
| 65 | $trans_value = get_transient($tran_key); |
| 66 | |
| 67 | if(false === $trans_value) { |
| 68 | |
| 69 | /** |
| 70 | * Either key is not exists or value is expired! |
| 71 | * |
| 72 | */ |
| 73 | |
| 74 | |
| 75 | try { |
| 76 | |
| 77 | /** |
| 78 | * todo - For now previous code is working, so why bother writing new codes :P |
| 79 | */ |
| 80 | $url = 'https://www.pinterest.com/'.trim($username); |
| 81 | $html = xsc_remote_get($url, false); |
| 82 | $prev = libxml_use_internal_errors(true); |
| 83 | $doc = new \DOMDocument(); |
| 84 | @$doc->loadHTML($html); |
| 85 | libxml_use_internal_errors($prev); |
| 86 | |
| 87 | $metas = $doc->getElementsByTagName('meta'); |
| 88 | |
| 89 | for($i = 0; $i < $metas->length; $i++) { |
| 90 | $meta = $metas->item($i); |
| 91 | if($meta->getAttribute('name') == 'pinterestapp:followers') { |
| 92 | $result = $meta->getAttribute('content'); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Updating transient cache |
| 99 | */ |
| 100 | |
| 101 | $expiration_time = empty($global_cache_time) ? 43200: intval($global_cache_time); |
| 102 | |
| 103 | set_transient($tran_key, $result, $expiration_time); |
| 104 | update_option(self::get_last_cache_key(), time()); |
| 105 | |
| 106 | } catch(Exception $e) { |
| 107 | |
| 108 | /** |
| 109 | * todo - AR; need to get confirmation what shoud we do in case there are errors from Product Owner |
| 110 | * for now returning 0; |
| 111 | * |
| 112 | */ |
| 113 | $result = 0; |
| 114 | } |
| 115 | |
| 116 | return $result; |
| 117 | } |
| 118 | |
| 119 | return $trans_value; |
| 120 | } |
| 121 | } |