PluginProbe
Powerkit – Supercharge your WordPress Site / 2.2.9
Powerkit – Supercharge your WordPress Site v2.2.9
3.1.1 3.1.0 3.0.9 3.0.8 trunk 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 87 releases
powerkit / modules / social-links / helpers / helper-powerkit-links-api.php

helper-powerkit-links-api.php in Powerkit – Supercharge your WordPress Site 2.2.9, at modules/social-links/helpers/helper-powerkit-links-api.php

1,703 lines 40.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Get Social Links
4 *
5 * @package Powerkit
6 * @subpackage Modules/Helper
7 */
8
9 /**
10 * Social Counter Class
11 */
12 class Powerkit_Links_Social_Counter {
13
14 /**
15 * Social config
16 *
17 * @since 1.0.0
18 * @var array $config Config.
19 */
20 public $config = array();
21
22 /**
23 * Transient Prefix
24 *
25 * @since 1.0.0
26 * @var string $cache_timeout Cache Timeout (minutes).
27 */
28 public $trans_prefix = 'powerkit_social_links_counter_';
29
30 /**
31 * Cache Results
32 *
33 * @since 1.0.0
34 * @var int $cache_timeout Cache Timeout (minutes).
35 */
36 public $cache_timeout = 720;
37
38 /**
39 * Initialize the class and set its properties.
40 *
41 * @since 1.0.0
42 */
43 public function __construct() {
44 $this->run_api();
45 }
46
47 /**
48 * Run users name , api keys and clients id
49 *
50 * @since 1.0.0
51 */
52 public function run_api() {
53
54 $api_config = new Powerkit_Links_Api_Config();
55
56 // Cache Timeout.
57 $this->cache_timeout = $api_config::$cache_timeout;
58
59 // Users.
60 $this->config = $api_config::$config;
61 }
62
63 /**
64 * Curl request data
65 *
66 * @since 1.0.0
67 * @param array $args Api params.
68 */
69 public function curl_get_api( $args ) {
70
71 // Get Cached Data.
72 if ( 'forcibly' === $args['cache'] ) {
73 return get_option( $this->trans_prefix . $args['network'] );
74 }
75
76 $data = null;
77
78 if ( $args['cache'] && $this->cache_timeout > 0 ) {
79 $data = get_transient( $this->trans_prefix . $args['network'] );
80 }
81
82 if ( ! $data ) {
83
84 // Generate Url.
85 $params = http_build_query( $args['params'] );
86 $remote_url = $params ? $args['url'] . '?' . $params : $args['url'];
87
88 // Request_params.
89 $request_params = array(
90 'timeout' => 120,
91 'redirection' => 10,
92 'sslverify' => false,
93 'user-agent' => $this->get_random_user_agent(),
94 'headers' => array(
95 'Accept-language' => 'en',
96 ),
97 );
98
99 $response = wp_safe_remote_get( $remote_url, $request_params );
100
101 if ( is_wp_error( $response ) ) {
102 return false;
103 }
104
105 // Retrieve data.
106 $data = wp_remote_retrieve_body( $response );
107
108 // JSON Decode.
109 if ( $args['decode'] ) {
110 $data = json_decode( $data );
111 }
112 }
113
114 return $data;
115 }
116
117 /**
118 * Safe processing of results
119 *
120 * @since 1.0.0
121 * @param string $network Social Network.
122 * @param mixed $result Followers Data.
123 */
124 public function safe_result( $network, $result ) {
125
126 $option_name = $this->trans_prefix . $network;
127
128 if ( ! is_array( $result ) || ! isset( $result['count'] ) || ! $result['count'] ) {
129
130 $counter = (int) get_option( '_backup_counter_' . $option_name );
131 $backup = (array) get_option( '_backup_' . $option_name );
132
133 if ( $counter <= 5 && isset( $backup['count'] ) && $backup['count'] ) {
134
135 set_transient( $option_name, $backup, 60 * $this->cache_timeout );
136
137 update_option( '_backup_counter_' . $option_name, ++$counter );
138
139 return $backup;
140
141 } elseif ( isset( $backup['count'] ) && $backup['count'] ) {
142
143 $result['count'] = $backup['count'];
144
145 } else {
146 delete_option( '_backup_' . $option_name );
147 }
148 }
149
150 return $result;
151 }
152
153 /**
154 * Maybe Set Cache
155 *
156 * @since 1.0.0
157 * @param string $network Social Network.
158 * @param int $data Followers Data.
159 * @param int $cache Cache Results.
160 */
161 public function maybe_set_cache( $network, $data, $cache = true ) {
162
163 $option_name = $this->trans_prefix . $network;
164
165 if ( $cache && $this->cache_timeout > 0 ) {
166 set_transient( $option_name, (array) $data, 60 * $this->cache_timeout );
167
168 // Backup data.
169 if ( is_array( $data ) && isset( $data['count'] ) && $data['count'] ) {
170
171 delete_option( '_backup_counter_' . $option_name );
172
173 update_option( '_backup_' . $option_name, (array) $data );
174 }
175 }
176 }
177
178 /**
179 * Get pinterest followers count
180 *
181 * @since 1.0.0
182 * @param bool $cache Cache Results.
183 */
184 public function get_pinterest( $cache = true ) {
185
186 $network = 'pinterest';
187 $result = array(
188 'count' => 0,
189 );
190
191 // Check username.
192 if ( ! $this->config['pinterest_user'] ) {
193 $result['error'] = esc_html__( 'Please enter a pinterest user name.', 'powerkit' );
194 return $result;
195 }
196
197 // Get Count.
198 $data = $this->curl_get_api( array(
199 'network' => $network,
200 'url' => 'https://api.pinterest.com/v3/pidgets/users/' . $this->config['pinterest_user'] . '/pins',
201 'params' => array(),
202 'cache' => $cache,
203 'decode' => true,
204 ));
205
206 // Cached Count.
207 if ( is_array( $data ) && isset( $data['count'] ) ) {
208 return $data;
209 }
210
211 // Set Count.
212 if ( is_object( $data ) && isset( $data->data->user->follower_count ) ) {
213
214 // Live Count.
215 $result['count'] = $data->data->user->follower_count;
216 } elseif ( is_object( $data ) && isset( $data->status ) && 'failure' === $data->status ) {
217
218 // API Error.
219 if ( isset( $data->message ) ) {
220 $result['error'] = $data->message;
221 }
222 }
223
224 // Maybe Set Cache.
225 $this->maybe_set_cache( $network, $result, $cache );
226
227 return $result;
228 }
229
230 /**
231 * Get dribbble followers count
232 *
233 * @since 1.0.0
234 * @param bool $cache Cache Results.
235 */
236 public function get_dribbble( $cache = true ) {
237
238 $network = 'dribbble';
239 $result = array(
240 'count' => 0,
241 );
242
243 // Check username.
244 if ( ! $this->config['dribbble_user'] ) {
245 $result['error'] = esc_html__( 'Please enter a dribbble user name.', 'powerkit' );
246 return $result;
247 }
248
249 // Check token.
250 if ( ! powerkit_connect( 'dribbble_token' ) ) {
251 return $result;
252 }
253
254 // Get Count.
255 $data = $this->curl_get_api( array(
256 'network' => $network,
257 'url' => 'https://api.dribbble.com/v1/users/' . $this->config['dribbble_user'],
258 'params' => array(
259 'access_token' => powerkit_connect( 'dribbble_token' ),
260 ),
261 'cache' => $cache,
262 'decode' => true,
263 ));
264
265 // Cached Count.
266 if ( is_array( $data ) && isset( $data['count'] ) ) {
267 return $data;
268 }
269
270 // Set Count.
271 if ( isset( $data->message ) ) {
272
273 // API Error.
274 $result['error'] = $data->message;
275
276 } elseif ( isset( $data->followers_count ) ) {
277
278 // Live Count.
279 $result['count'] = $data->followers_count;
280 }
281
282 // Maybe Set Cache.
283 $this->maybe_set_cache( $network, $result, $cache );
284
285 return $result;
286 }
287
288 /**
289 * Get facebook fans count
290 *
291 * @since 1.0.0
292 * @param bool $cache Cache Results.
293 */
294 public function get_facebook( $cache = true ) {
295
296 $network = 'facebook';
297 $result = array(
298 'count' => 0,
299 );
300
301 // Check username.
302 if ( ! $this->config['facebook_user'] ) {
303 $result['error'] = esc_html__( 'Please enter a Facebook user name.', 'powerkit' );
304
305 return $result;
306 }
307
308 // Check Facebook access token.
309 if ( powerkit_connect( 'facebook_app_access_token' ) ) {
310
311 $url = sprintf( 'https://graph.facebook.com/%s?fields=fan_count&access_token=%s', $this->config['facebook_user'], powerkit_connect( 'facebook_app_access_token' ) );
312
313 // Get Count.
314 $data = $this->curl_get_api( array(
315 'network' => $network,
316 'url' => $url,
317 'params' => array(),
318 'cache' => $cache,
319 'decode' => true,
320 ) );
321
322 // Cached Count.
323 if ( is_array( $data ) && isset( $data['count'] ) ) {
324 return $data;
325 }
326
327 // Set Count.
328 if ( isset( $data->error->message ) ) {
329
330 // API Error.
331 $result['error'] = $data->error->message;
332
333 } elseif ( isset( $data->fan_count ) ) {
334
335 // Live Count.
336 $result['count'] = $data->fan_count;
337 }
338
339 // Set message.
340 if ( isset( $result['error'] ) ) {
341 $result['error'] .= sprintf( '%s <a href="%s">%s</a>', esc_html__( ' Please check your ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=facebook' ), esc_html__( ' Facebook Settings', 'powerkit' ) );
342
343 if ( false !== strpos( $result['error'], 'Some of the aliases you requested do not exist' ) ) {
344 $result['error'] = esc_html__( 'Please check your User ID.', 'powerkit' );
345 }
346
347 if ( false !== strpos( $result['error'], 'Page Public Content Access' ) ) {
348 $result['error'] = esc_html__( 'Please make sure you own the page and allowed your Facebook app to manage it.', 'powerkit' );
349 }
350 }
351 } else {
352
353 $url = add_query_arg( array(
354 'href' => rawurlencode( 'https://www.facebook.com/' . $this->config['facebook_user'] ),
355 'domain' => rawurlencode( home_url() ),
356 'origin' => rawurlencode( home_url() ),
357 'adapt_container_width' => 'true',
358 'relation' => 'parent.parent',
359 'container_width' => '300',
360 'hide_cover' => 'false',
361 'locale' => 'en_US',
362 'sdk' => 'joey',
363 'show_facepile' => 'false',
364 'show_posts' => 'false',
365 'small_header' => 'false',
366 'width' => '500px',
367 'app_id' => powerkit_connect( 'facebook_app_id' ),
368 ), 'https://www.facebook.com/plugins/page.php' );
369
370 // Get Count.
371 $data = $this->curl_get_api( array(
372 'url' => $url,
373 'network' => $network,
374 'cache' => $cache,
375 'params' => array(),
376 'decode' => false,
377 ));
378
379 // Cached Count.
380 if ( is_array( $data ) && isset( $data['count'] ) ) {
381 return $data;
382 }
383
384 // Set Count.
385 $facebook_result = preg_match( '/<div[^>]*?>([^>]*?)like/s', $data, $facebook_data );
386 if ( $facebook_result ) {
387 $facebook_data_full = array_shift( $facebook_data );
388 $facebook_data_json = array_shift( $facebook_data );
389
390 // Live Count.
391 $result['count'] = (int) $this->extract_numbers_from_string( strip_tags( $facebook_data_json ) );
392 } else {
393 $result['error'] = esc_html__( 'Data not found.', 'powerkit' );
394 }
395
396 // Set Message.
397 if ( isset( $result['error'] ) ) {
398 $result['error'] .= sprintf( '%s <a href="%s">%s</a>', esc_html__( ' Please check your ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=facebook' ), esc_html__( ' Facebook Settings', 'powerkit' ) );
399 }
400 }
401
402 // Maybe Set Cache.
403 $this->maybe_set_cache( $network, $result, $cache );
404
405 return $result;
406 }
407
408 /**
409 * Get instagram followers count
410 *
411 * @since 1.0.0
412 * @param bool $cache Cache Results.
413 */
414 public function get_instagram( $cache = true ) {
415
416 $network = 'instagram';
417 $result = array(
418 'count' => 0,
419 );
420
421 // Check Instagram User.
422 if ( ! $this->config['instagram_user'] ) {
423 $result['error'] = esc_html__( 'Please enter an Instagram User.', 'powerkit' );
424
425 return $result;
426 }
427
428 // Get Count.
429 if ( powerkit_connect( 'instagram_app_access_token' ) ) {
430
431 Powerkit_Connect::access_token_refresh( 'instagram' );
432
433 if ( powerkit_connect( 'instagram_app_username' ) !== $this->config['instagram_user'] ) {
434 $result['error'] = esc_html__( 'Please check your Instagram User, you do not have access to this account.', 'powerkit' );
435 }
436
437 // Get information about.
438 if ( 'business' === powerkit_connect( 'instagram_app_type' ) ) {
439 $url = add_query_arg( array(
440 'fields' => 'biography,id,username,website,followers_count,media_count,profile_picture_url,name',
441 'access_token' => powerkit_connect( 'instagram_app_access_token' ),
442 ), 'https://graph.facebook.com/' . powerkit_connect( 'instagram_app_user_id' ) );
443 } else {
444 $url = add_query_arg( array(
445 'fields' => 'id,username,media_count',
446 'access_token' => powerkit_connect( 'instagram_app_access_token' ),
447 ), 'https://graph.instagram.com/me' );
448 }
449
450 // Get Count.
451 $data = $this->curl_get_api( array(
452 'network' => $network,
453 'url' => $url,
454 'params' => array(),
455 'cache' => $cache,
456 'decode' => true,
457 ));
458
459 // Cached Count.
460 if ( is_array( $data ) && isset( $data['count'] ) ) {
461 return $data;
462 }
463
464 // Set count.
465 if ( isset( $data->error->message ) ) {
466
467 // API Error.
468 $result['error'] = $data->error->message;
469
470 } elseif ( isset( $data->followers_count ) ) {
471
472 $result['data'] = $data;
473
474 // Live Count.
475 $result['count'] = $data->followers_count;
476 } else {
477
478 if ( 'personal' === powerkit_connect( 'instagram_app_type' ) ) {
479 $result['error'] = sprintf( __( ' Please add the number of followers <a href="%s" target="_blank">manually</a> or <a href="%s" target="_blank">connect</a> Instagram Business account.', 'powerkit' ), admin_url( 'options-general.php?page=powerkit_social_links' ), powerkit_get_page_url( 'connect&tab=instagram' ) );
480 } else {
481 $result['error'] = sprintf( '%s <a href="%s">%s</a>', esc_html__( ' Please check your ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=instagram' ), esc_html__( ' Instagram Settings', 'powerkit' ) );
482 }
483 }
484 } else {
485 $data = $this->curl_get_api( array(
486 'url' => 'https://www.instagram.com/' . $this->config['instagram_user'],
487 'network' => $network,
488 'cache' => $cache,
489 'params' => array(),
490 'decode' => false,
491 ) );
492
493 // Cached Count.
494 if ( is_array( $data ) && isset( $data['count'] ) ) {
495 return $data;
496 }
497
498 // Set Count.
499 // Get the serialized data string present in the page script.
500 preg_match( '/window\._sharedData = (.*);<\/script>/', $data, $ins_data );
501
502 $ins_data_full = array_shift( $ins_data );
503 $ins_data_json = array_shift( $ins_data );
504
505 if ( $ins_data_json ) {
506 $instagram_json = json_decode( $ins_data_json, true );
507
508 // Live Count.
509 if ( ! empty( $instagram_json['entry_data']['ProfilePage'][0]['graphql']['user']['edge_followed_by']['count'] ) ) {
510
511 $result['data'] = $instagram_json;
512
513 $result['count'] = (int) $instagram_json['entry_data']['ProfilePage'][0]['graphql']['user']['edge_followed_by']['count'];
514 } else {
515 $result['error'] = esc_html__( 'Data not found.', 'powerkit' );
516 }
517 } else {
518 $result['error'] = esc_html__( 'Data not found.', 'powerkit' );
519 }
520
521 // Set Message.
522 if ( isset( $result['error'] ) ) {
523
524 $result['error'] .= sprintf( '%s <a href="%s">%s</a>', esc_html__( ' Please check your ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=instagram' ), esc_html__( ' Instagram Settings', 'powerkit' ) );
525 }
526 }
527
528 // Maybe Set Cache.
529 $this->maybe_set_cache( $network, $result, $cache );
530
531 return $result;
532 }
533
534 /**
535 * Get youtube subscribers count
536 *
537 * @since 1.0.0
538 * @param bool $cache Cache Results.
539 */
540 public function get_youtube( $cache = true ) {
541
542 $network = 'youtube';
543 $result = array(
544 'count' => 0,
545 );
546
547 // Check username.
548 if ( ! $this->config['youtube_slug'] ) {
549 $result['error'] = esc_html__( 'Please enter an YouTube User.', 'powerkit' );
550 return $result;
551 }
552
553 // Check token.
554 if ( ! powerkit_connect( 'youtube_key' ) ) {
555 return $result;
556 }
557
558 // Generate Params.
559 switch ( $this->config['youtube_channel_type'] ) {
560 case 'channel':
561 $params = array(
562 'part' => 'snippet,contentDetails,statistics',
563 'id' => $this->config['youtube_slug'],
564 'key' => powerkit_connect( 'youtube_key' ),
565 );
566 break;
567
568 // User.
569 default:
570 $params = array(
571 'part' => 'snippet,contentDetails,statistics',
572 'forUsername' => $this->config['youtube_slug'],
573 'key' => powerkit_connect( 'youtube_key' ),
574 );
575 break;
576 }
577
578 // Get Count.
579 $data = $this->curl_get_api( array(
580 'network' => $network,
581 'url' => 'https://www.googleapis.com/youtube/v3/channels/',
582 'params' => $params,
583 'cache' => $cache,
584 'decode' => true,
585 ));
586
587 // Cached Count.
588 if ( is_array( $data ) && isset( $data['count'] ) ) {
589 return $data;
590 }
591
592 // Set Count.
593 if ( isset( $data->error->message ) ) {
594
595 // API Error.
596 $result['error'] = $data->error->message;
597
598 } elseif ( isset( $data->items[0]->statistics->subscriberCount ) ) {
599
600 $result['data'] = $data;
601
602 // Live Count.
603 $result['count'] = $data->items[0]->statistics->subscriberCount;
604
605 } elseif ( isset( $data->items ) && empty( $data->items ) ) {
606
607 // API Error 2.
608 $result['error'] = esc_html__( 'Please check your username or channel.', 'powerkit' );
609
610 }
611
612 // Maybe Set Cache.
613 $this->maybe_set_cache( $network, $result, $cache );
614
615 return $result;
616 }
617
618 /**
619 * Get telegram followers count
620 *
621 * @since 1.0.0
622 * @param bool $cache Cache Results.
623 */
624 public function get_telegram( $cache = true ) {
625
626 $network = 'telegram';
627 $result = array(
628 'count' => 0,
629 );
630
631 // Check chat id.
632 if ( ! $this->config['telegram_chat'] ) {
633 $result['error'] = esc_html__( 'Please enter an Telegram Channel ID.', 'powerkit' );
634 return $result;
635 }
636
637 // Check token.
638 if ( ! powerkit_connect( 'telegram_token' ) ) {
639 return $result;
640 }
641
642 // Get Count.
643 $data = $this->curl_get_api( array(
644 'network' => $network,
645 'url' => 'https://api.telegram.org/bot' . powerkit_connect( 'telegram_token' ) . '/getChatMembersCount',
646 'params' => array(
647 'chat_id' => '@' . $this->config['telegram_chat'],
648 ),
649 'cache' => $cache,
650 'decode' => true,
651 ) );
652
653 // Cached Count.
654 if ( is_array( $data ) && isset( $data['count'] ) ) {
655 return $data;
656 }
657
658 // Set Count.
659 if ( isset( $data->message ) ) {
660
661 // API Error.
662 $result['error'] = $data->message;
663
664 } elseif ( isset( $data->result ) ) {
665
666 // Live Count.
667 $result['count'] = $data->result;
668 }
669
670 // Maybe Set Cache.
671 $this->maybe_set_cache( $network, $result, $cache );
672
673 return $result;
674 }
675
676 /**
677 * Get soundcloud followers count
678 *
679 * @since 1.0.0
680 * @param bool $cache Cache Results.
681 */
682 public function get_soundcloud( $cache = true ) {
683
684 $network = 'soundcloud';
685 $result = array(
686 'count' => 0,
687 );
688
689 // Check username.
690 if ( ! $this->config['soundcloud_user_id'] ) {
691 $result['error'] = esc_html__( 'Please enter a SoundCloud User ID.', 'powerkit' );
692 return $result;
693 }
694
695 // Check token.
696 if ( ! powerkit_connect( 'soundcloud_client_id' ) ) {
697 return $result;
698 }
699
700 // Get Count.
701 $data = $this->curl_get_api( array(
702 'network' => $network,
703 'url' => 'http://api.soundcloud.com/users/' . $this->config['soundcloud_user_id'],
704 'params' => array(
705 'client_id' => powerkit_connect( 'soundcloud_client_id' ),
706 ),
707 'cache' => $cache,
708 'decode' => true,
709 ));
710
711 // Cached Count.
712 if ( is_array( $data ) && isset( $data['count'] ) ) {
713 return $data;
714 }
715
716 // Set Count.
717 if ( ! $data ) {
718
719 // API Error.
720 $result['error'] = esc_html__( 'SoundCloud API Error.', 'powerkit' );
721
722 } elseif ( isset( $data->followers_count ) ) {
723
724 // Live Count.
725 $result['count'] = $data->followers_count;
726 }
727
728 // Maybe Set Cache.
729 $this->maybe_set_cache( $network, $result, $cache );
730
731 return $result;
732 }
733
734 /**
735 * Get vimeo followers count
736 *
737 * @since 1.0.0
738 * @param bool $cache Cache Results.
739 */
740 public function get_vimeo( $cache = true ) {
741
742 $network = 'vimeo';
743 $result = array(
744 'count' => 0,
745 );
746
747 // Check username.
748 if ( ! $this->config['vimeo_user'] ) {
749 $result['error'] = esc_html__( 'Please enter a Vimeo User ID.', 'powerkit' );
750 return $result;
751 }
752
753 // Check token.
754 if ( ! powerkit_connect( 'vimeo_token' ) ) {
755 return $result;
756 }
757
758 // Get Count.
759 $data = $this->curl_get_api( array(
760 'network' => $network,
761 'url' => 'https://api.vimeo.com/users/' . $this->config['vimeo_user'] . '/followers',
762 'params' => array(
763 'access_token' => powerkit_connect( 'vimeo_token' ),
764 ),
765 'cache' => $cache,
766 'decode' => true,
767 ));
768
769 // Cached Count.
770 if ( is_array( $data ) && isset( $data['count'] ) ) {
771 return $data;
772 }
773
774 // Set Count.
775 if ( isset( $data->error ) ) {
776
777 // API Error.
778 $result['error'] = $data->error;
779
780 } elseif ( isset( $data->total ) ) {
781
782 // Live Count.
783 $result['count'] = $data->total;
784 }
785
786 // Maybe Set Cache.
787 $this->maybe_set_cache( $network, $result, $cache );
788
789 return $result;
790 }
791
792 /**
793 * Get twitter followers count
794 *
795 * @since 1.0.0
796 * @param bool $cache Cache Results.
797 */
798 public function get_twitter( $cache = true ) {
799 $network = 'twitter';
800 $result = array(
801 'count' => 0,
802 );
803
804 // Check username.
805 if ( ! $this->config['twitter_user'] ) {
806 $result['error'] = esc_html__( 'Please enter a Twitter User.', 'powerkit' );
807 return $result;
808 }
809
810 // Check Twitter Access Token.
811 if ( ! powerkit_connect( 'twitter_app_oauth_token' ) ) {
812 return $result;
813 }
814
815 // Check Twitter Access Token Secret.
816 if ( ! powerkit_connect( 'twitter_app_oauth_token_secret' ) ) {
817 return $result;
818 }
819
820 $data = null;
821
822 // Get Cached Data.
823 if ( $cache && $this->cache_timeout > 0 ) {
824 $data = get_transient( $this->trans_prefix . $network );
825 }
826
827 // Cached Count.
828 if ( is_array( $data ) && isset( $data['count'] ) ) {
829 return $data;
830 }
831
832 // Get Count.
833 if ( ! $data ) {
834 $twitter_connect = new Powerkit_Twitter_API();
835 $twitter_connect->init( array(
836 'consumer_key' => powerkit_connect( 'twitter_app_consumer_key' ),
837 'consumer_secret' => powerkit_connect( 'twitter_app_consumer_secret' ),
838 'access_token' => powerkit_connect( 'twitter_app_oauth_token' ),
839 'access_token_secret' => powerkit_connect( 'twitter_app_oauth_token_secret' ),
840 ), 'timeline' );
841
842 $twitter_connect->set_url_base();
843 $twitter_connect->set_get_fields( array(
844 'screen_name' => $this->config['twitter_user'],
845 'count' => '1',
846 'exclude_replies' => false,
847 ) );
848
849 $twitter_connect->perform_request();
850
851 $response_code = wp_remote_retrieve_response_code( $twitter_connect->json );
852 $response_message = wp_remote_retrieve_response_message( $twitter_connect->json );
853 $response_body = json_decode( wp_remote_retrieve_body( $twitter_connect->json ) );
854
855 /* Set Data Followers */
856 if ( 200 !== $response_code && ! empty( $response_message ) ) {
857
858 $result['error'] = $response_message;
859
860 } elseif ( 200 !== $response_code ) {
861
862 $result['error'] = sprintf( '%s <a href="%s">%s</a>', esc_html__( 'Twitter data is not set. Please check your Twitter User or ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=twitter' ), esc_html__( ' Twitter Settings', 'powerkit' ) );
863
864 } elseif ( empty( $response_body ) ) {
865
866 $result['error'] = esc_html__( 'There are no tweets in your account.', 'powerkit' );
867
868 } else {
869 $data = $response_body;
870 }
871 }
872
873 // Set Count.
874 if ( is_array( $data ) && isset( $data['count'] ) ) {
875
876 // Cached Count.
877 $result['count'] = $data['count'];
878
879 } elseif ( isset( $data->errors ) ) {
880
881 // API Error.
882 foreach ( $data->errors as $error ) {
883 if ( isset( $error->message ) ) {
884 $result['error'] = $error->message;
885
886 break;
887 }
888 }
889 } elseif ( is_array( $data ) && isset( $data[0]->user->followers_count ) ) {
890
891 // Live Count.
892 $result['count'] = $data[0]->user->followers_count;
893 }
894
895 // Maybe Set Cache.
896 $this->maybe_set_cache( $network, $result, $cache );
897
898 return $result;
899 }
900
901 /**
902 * Get github followers count
903 *
904 * @since 1.0.0
905 * @param bool $cache Cache Results.
906 */
907 public function get_github( $cache = true ) {
908
909 $network = 'github';
910 $result = array(
911 'count' => 0,
912 );
913
914 // Check username.
915 if ( ! $this->config['github_user'] ) {
916 $result['error'] = esc_html__( 'Please enter a GitHub User ID.', 'powerkit' );
917 return $result;
918 }
919
920 // Check client id.
921 if ( ! powerkit_connect( 'github_client_id' ) ) {
922 return $result;
923 }
924
925 // Check client id.
926 if ( ! powerkit_connect( 'github_client_secret' ) ) {
927 return $result;
928 }
929
930 // Get Count.
931 $data = $this->curl_get_api( array(
932 'network' => $network,
933 'url' => 'https://api.github.com/users/' . $this->config['github_user'],
934 'params' => array(
935 'client_id' => powerkit_connect( 'github_client_id' ),
936 'client_secret' => powerkit_connect( 'github_client_secret' ),
937 ),
938 'cache' => $cache,
939 'decode' => true,
940 ));
941
942 // Cached Count.
943 if ( is_array( $data ) && isset( $data['count'] ) ) {
944 return $data;
945 }
946
947 // Set Count.
948 if ( isset( $data->message ) ) {
949
950 // API Error.
951 $result['error'] = $data->message;
952
953 } elseif ( isset( $data->followers ) ) {
954
955 // Live Count.
956 $result['count'] = $data->followers;
957 }
958
959 // Maybe Set Cache.
960 $this->maybe_set_cache( $network, $result, $cache );
961
962 return $result;
963 }
964
965 /**
966 * Get behance followers count
967 *
968 * @since 1.0.0
969 * @param bool $cache Cache Results.
970 */
971 public function get_behance( $cache = true ) {
972
973 $network = 'behance';
974 $result = array(
975 'count' => 0,
976 );
977
978 // Check username.
979 if ( ! $this->config['behance_user'] ) {
980 $result['error'] = esc_html__( 'Please enter a Behance User ID.', 'powerkit' );
981 return $result;
982 }
983
984 // Check client id.
985 if ( ! powerkit_connect( 'behance_client_id' ) ) {
986 return $result;
987 }
988
989 // Get Count.
990 $data = $this->curl_get_api( array(
991 'network' => $network,
992 'url' => 'https://api.behance.net/v2/users/' . $this->config['behance_user'],
993 'params' => array(
994 'client_id' => powerkit_connect( 'behance_client_id' ),
995 ),
996 'cache' => $cache,
997 'decode' => true,
998 ));
999
1000 // Cached Count.
1001 if ( is_array( $data ) && isset( $data['count'] ) ) {
1002 return $data;
1003 }
1004
1005 // Set Count.
1006 if ( isset( $data->valid ) && 0 === $data->valid ) {
1007
1008 // API Error.
1009 if ( isset( $data->messages ) ) {
1010 foreach ( (array) $data->messages as $message ) {
1011 if ( 'error' === $message->type ) {
1012 $result['error'] = $message->message;
1013 }
1014 }
1015 } else {
1016 $result['error'] = esc_html__( 'Please check your username and client_id.', 'powerkit' );
1017 }
1018 } elseif ( isset( $data->user->stats->followers ) ) {
1019
1020 // Live Count.
1021 $result['count'] = $data->user->stats->followers;
1022 }
1023
1024 // Maybe Set Cache.
1025 $this->maybe_set_cache( $network, $result, $cache );
1026
1027 return $result;
1028 }
1029
1030 /**
1031 * Get twitch followers count
1032 *
1033 * @since 1.0.0
1034 * @param bool $cache Cache Results.
1035 */
1036 public function get_twitch( $cache = true ) {
1037
1038 $network = 'twitch';
1039 $result = array(
1040 'count' => 0,
1041 );
1042
1043 // Check channel id.
1044 if ( ! $this->config['twitch_user_id'] ) {
1045 $result['error'] = esc_html__( 'Please enter a Twitch User ID.', 'powerkit' );
1046 return $result;
1047 }
1048
1049 // Check client id.
1050 if ( ! powerkit_connect( 'twitch_client_id' ) ) {
1051 return $result;
1052 }
1053
1054 // Get Count.
1055 $data = $this->curl_get_api( array(
1056 'network' => $network,
1057 'url' => 'https://api.twitch.tv/kraken/channels/' . $this->config['twitch_user_id'],
1058 'params' => array(
1059 'client_id' => powerkit_connect( 'twitch_client_id' ),
1060 ),
1061 'cache' => $cache,
1062 'decode' => true,
1063 ) );
1064
1065 // Cached Count.
1066 if ( is_array( $data ) && isset( $data['count'] ) ) {
1067 return $data;
1068 }
1069
1070 // Set Count.
1071 if ( isset( $data->message ) ) {
1072
1073 // API Error.
1074 $result['error'] = $data->message;
1075
1076 } elseif ( isset( $data->followers ) ) {
1077
1078 // Live Count.
1079 $result['count'] = $data->followers;
1080 }
1081
1082 // Maybe Set Cache.
1083 $this->maybe_set_cache( $network, $result, $cache );
1084
1085 return $result;
1086 }
1087
1088 /**
1089 * Get flickr count
1090 *
1091 * @since 1.0.0
1092 * @param bool $cache Cache Results.
1093 */
1094 public function get_flickr( $cache = true ) {
1095
1096 $network = 'flickr';
1097 $result = array(
1098 'count' => 0,
1099 );
1100
1101 // Check username.
1102 if ( ! $this->config['flickr_user_id'] ) {
1103 $result['error'] = esc_html__( 'Please enter a flickr user id.', 'powerkit' );
1104
1105 return $result;
1106 }
1107
1108 // Get Count.
1109 $data = $this->curl_get_api( array(
1110 'url' => 'https://www.flickr.com/photos/' . $this->config['flickr_user_id'],
1111 'network' => $network,
1112 'cache' => $cache,
1113 'params' => array(),
1114 'decode' => false,
1115 ));
1116
1117 // Cached Count.
1118 if ( is_array( $data ) && isset( $data['count'] ) ) {
1119 return $data;
1120 }
1121
1122 // Set Count.
1123 $data = str_replace( '.', '', $data );
1124 $data = str_replace( 'K', '000', $data );
1125 $data = str_replace( 'M', '000000', $data );
1126
1127 $flickr_result = preg_match( '/<p class="followers[^"]*?">(.*?)\s[^<]*?<em>/s', $data, $flickr_data );
1128
1129 if ( $flickr_result ) {
1130 $flickr_data_full = array_shift( $flickr_data );
1131 $flickr_data_number = array_shift( $flickr_data );
1132
1133 // Live Count.
1134 $result['count'] = (int) $flickr_data_number;
1135 } else {
1136 $result['error'] = esc_html__( 'Data not found. Please check your user ID.', 'powerkit' );
1137 }
1138
1139 // Maybe Set Cache.
1140 $this->maybe_set_cache( $network, $result, $cache );
1141
1142 return $result;
1143 }
1144
1145 /**
1146 * Get medium followers count
1147 *
1148 * @since 1.0.0
1149 * @param bool $cache Cache Results.
1150 */
1151 public function get_medium( $cache = true ) {
1152
1153 $network = 'medium';
1154 $result = array(
1155 'count' => 0,
1156 );
1157
1158 // Check user.
1159 if ( ! $this->config['medium_user'] ) {
1160 $result['error'] = esc_html__( 'Please enter a Medium User.', 'powerkit' );
1161 return $result;
1162 }
1163
1164 // Get Count.
1165 $data = $this->curl_get_api( array(
1166 'network' => $network,
1167 'url' => 'https://medium.com/' . $this->config['medium_user'],
1168 'params' => array(
1169 'format' => 'json',
1170 ),
1171 'cache' => $cache,
1172 'decode' => false,
1173 ) );
1174
1175 // Cached Count.
1176 if ( is_array( $data ) && isset( $data['count'] ) ) {
1177 return $data;
1178 }
1179
1180 // Set Count.
1181 if ( isset( $data->message ) ) {
1182
1183 // API Error.
1184 $result['error'] = $data->message;
1185
1186 } elseif ( $data ) {
1187 $data = str_replace( '])}while(1);</x>', '', $data );
1188
1189 $data = json_decode( $data, true );
1190
1191 if ( isset( $data['payload']['user']['userId'] ) ) {
1192
1193 $user_id = $data['payload']['user']['userId'];
1194
1195 if ( isset( $data['payload']['references']['SocialStats'][ $user_id ]['usersFollowedByCount'] ) ) {
1196 // Live Count.
1197 $result['count'] = $data['payload']['references']['SocialStats'][ $user_id ]['usersFollowedByCount'];
1198 }
1199 }
1200 }
1201
1202 // Maybe Set Cache.
1203 $this->maybe_set_cache( $network, $result, $cache );
1204
1205 return $result;
1206 }
1207
1208 /**
1209 * Get reddit subscribers count
1210 *
1211 * @since 1.0.0
1212 * @param bool $cache Cache Results.
1213 */
1214 public function get_reddit( $cache = true ) {
1215
1216 $network = 'reddit';
1217 $result = array(
1218 'count' => null,
1219 );
1220
1221 // Check channel id.
1222 if ( ! $this->config['reddit_user'] ) {
1223 $result['error'] = esc_html__( 'Please enter Reddit User or Subreddit Name.', 'powerkit' );
1224 return $result;
1225 }
1226
1227 if ( 'subreddit' === $this->config['reddit_type'] ) {
1228 $url = sprintf( 'https://www.reddit.com/r/%s/about.json', $this->config['reddit_user'] );
1229 } else {
1230 $url = sprintf( 'https://www.reddit.com/user/%s/about.json', $this->config['reddit_user'] );
1231 }
1232
1233 // Get Count.
1234 $data = $this->curl_get_api( array(
1235 'network' => $network,
1236 'url' => $url,
1237 'params' => array(),
1238 'cache' => $cache,
1239 'decode' => true,
1240 ) );
1241
1242 // Cached Count.
1243 if ( is_array( $data ) && isset( $data['count'] ) ) {
1244 return $data;
1245 }
1246
1247 // Set Count.
1248 if ( 'subreddit' === $this->config['reddit_type'] ) {
1249 if ( isset( $data->data->subscribers ) ) {
1250 // Live Count.
1251 $result['count'] = $data->data->subscribers;
1252 }
1253 } else {
1254 if ( isset( $data->data->link_karma ) ) {
1255 // Live Count.
1256 $result['count'] += $data->data->link_karma;
1257 }
1258 if ( isset( $data->data->comment_karma ) ) {
1259 // Live Count.
1260 $result['count'] += $data->data->comment_karma;
1261 }
1262 }
1263
1264 // Maybe Set Cache.
1265 $this->maybe_set_cache( $network, $result, $cache );
1266
1267 return $result;
1268 }
1269
1270 /**
1271 * Get ok followers count
1272 *
1273 * @since 1.0.0
1274 * @param bool $cache Cache Results.
1275 */
1276 public function get_ok( $cache = true ) {
1277
1278 $network = 'ok';
1279 $result = array(
1280 'count' => 0,
1281 );
1282
1283 // Check username.
1284 if ( ! $this->config['ok_slug'] ) {
1285 $result['error'] = esc_html__( 'Please enter a Odnoklassniki Slug or ID.', 'powerkit' );
1286 return $result;
1287 }
1288
1289 // Check application key.
1290 if ( ! powerkit_connect( 'ok_application_key' ) ) {
1291 return $result;
1292 }
1293
1294 // Check access token.
1295 if ( ! powerkit_connect( 'ok_access_token' ) ) {
1296 return $result;
1297 }
1298
1299 // Check session secret key.
1300 if ( ! powerkit_connect( 'ok_session_secret_key' ) ) {
1301 return $result;
1302 }
1303
1304 $data = null;
1305
1306 $object_id = null;
1307
1308 // Get info by slug.
1309 if ( 'profile_name' === $this->config['ok_type'] || 'group_name' === $this->config['ok_type'] ) {
1310 $params = array(
1311 'application_key' => powerkit_connect( 'ok_application_key' ),
1312 'format' => 'json',
1313 'method' => 'url.getInfo',
1314 'url' => 'https://ok.ru/' . $this->config['ok_slug'],
1315 );
1316
1317 $query = http_build_query( $params, '', '' );
1318
1319 $params['access_token'] = powerkit_connect( 'ok_access_token' );
1320
1321 $params['sig'] = md5( urldecode( $query . powerkit_connect( 'ok_session_secret_key' ) ) );
1322
1323 // Get Count.
1324 $data = $this->curl_get_api( array(
1325 'network' => $network,
1326 'url' => 'https://api.ok.ru/fb.do',
1327 'params' => $params,
1328 'cache' => $cache,
1329 'decode' => true,
1330 ));
1331
1332 if ( isset( $data->objectId ) ) {
1333 $object_id = $data->objectId;
1334 }
1335 } else {
1336 $object_id = $this->config['ok_slug'];
1337 }
1338
1339 if ( $object_id ) {
1340 if ( 'group' === $this->config['ok_type'] || 'group_name' === $this->config['ok_type'] ) {
1341 $params = array(
1342 'application_key' => powerkit_connect( 'ok_application_key' ),
1343 'counterTypes' => 'members',
1344 'format' => 'json',
1345 'group_id' => $object_id,
1346 'method' => 'group.getCounters',
1347 );
1348
1349 $query = http_build_query( $params, '', '' );
1350
1351 $params['access_token'] = powerkit_connect( 'ok_access_token' );
1352
1353 $params['sig'] = md5( urldecode( $query . powerkit_connect( 'ok_session_secret_key' ) ) );
1354
1355 // Get Count.
1356 $data = $this->curl_get_api( array(
1357 'network' => $network,
1358 'url' => 'https://api.ok.ru/fb.do',
1359 'params' => $params,
1360 'cache' => $cache,
1361 'decode' => true,
1362 ));
1363 }
1364
1365 if ( 'profile' === $this->config['ok_type'] || 'profile_name' === $this->config['ok_type'] ) {
1366 $params = array(
1367 'application_key' => powerkit_connect( 'ok_application_key' ),
1368 'fid' => $object_id,
1369 'format' => 'json',
1370 'method' => 'friends.get',
1371 );
1372
1373 $query = http_build_query( $params, '', '' );
1374
1375 $params['access_token'] = powerkit_connect( 'ok_access_token' );
1376
1377 $params['sig'] = md5( urldecode( $query . powerkit_connect( 'ok_session_secret_key' ) ) );
1378
1379 // Get Count.
1380 $data = $this->curl_get_api( array(
1381 'network' => $network,
1382 'url' => 'https://api.ok.ru/fb.do',
1383 'params' => $params,
1384 'cache' => $cache,
1385 'decode' => true,
1386 ));
1387
1388 if ( is_array( $data ) && count( $data ) > 0 ) {
1389 $data = (int) count( $data );
1390 }
1391 }
1392 }
1393
1394 // Cached Count.
1395 if ( is_array( $data ) && isset( $data['count'] ) ) {
1396 return $data;
1397 }
1398
1399 // Set Count.
1400 if ( is_object( $data ) && isset( $data->error_msg ) ) {
1401
1402 // API Error.
1403 $result['error'] = $data->error_msg;
1404
1405 } elseif ( is_object( $data ) && isset( $data->counters->members ) ) {
1406
1407 // Live Count.
1408 $result['count'] = $data->counters->members;
1409 } elseif ( is_numeric( $data ) ) {
1410
1411 // Live Count.
1412 $result['count'] = $data;
1413 }
1414
1415 // Maybe Set Cache.
1416 $this->maybe_set_cache( $network, $result, $cache );
1417
1418 return $result;
1419 }
1420
1421 /**
1422 * Get vk followers count
1423 *
1424 * @since 1.0.0
1425 * @param bool $cache Cache Results.
1426 */
1427 public function get_vk( $cache = true ) {
1428
1429 $network = 'vk';
1430 $result = array(
1431 'count' => 0,
1432 );
1433
1434 // Check vk id.
1435 if ( ! $this->config['vk_id'] ) {
1436 $result['error'] = esc_html__( 'Please enter a Group/Page ID.', 'powerkit' );
1437 return $result;
1438 }
1439
1440 // Check token.
1441 if ( ! powerkit_connect( 'vk_token' ) ) {
1442 return $result;
1443 }
1444
1445 // Get Count.
1446 if ( 'user' === $this->config['vk_type'] ) {
1447
1448 $data = $this->curl_get_api( array(
1449 'network' => $network,
1450 'url' => 'https://api.vk.com/method/users.get',
1451 'params' => array(
1452 'access_token' => powerkit_connect( 'vk_token' ),
1453 'user_ids' => $this->config['vk_id'],
1454 'fields' => 'counters',
1455 'v' => '5.101',
1456 ),
1457 'cache' => $cache,
1458 'decode' => true,
1459 ));
1460
1461 // Cached Count.
1462 if ( is_array( $data ) && isset( $data['count'] ) ) {
1463 return $data;
1464 }
1465
1466 // Set Count.
1467 if ( isset( $data->error->error_msg ) ) {
1468
1469 // API Error.
1470 $result['error'] = $data->error->error_msg;
1471
1472 } elseif ( isset( $data->response[0]->counters->friends ) ) {
1473
1474 // Live Count.
1475 $result['count'] = $data->response[0]->counters->friends;
1476 }
1477 } else {
1478
1479 $data = $this->curl_get_api( array(
1480 'network' => $network,
1481 'url' => 'https://api.vk.com/method/groups.getById',
1482 'params' => array(
1483 'access_token' => powerkit_connect( 'vk_token' ),
1484 'group_id' => $this->config['vk_id'],
1485 'fields' => 'members_count',
1486 'v' => '5.101',
1487 ),
1488 'cache' => $cache,
1489 'decode' => true,
1490 ));
1491
1492 // Cached Count.
1493 if ( is_array( $data ) && isset( $data['count'] ) ) {
1494 return $data;
1495 }
1496
1497 // Set Count.
1498 if ( isset( $data->error->error_msg ) ) {
1499
1500 // API Error.
1501 $result['error'] = $data->error->error_msg;
1502
1503 } elseif ( isset( $data->response[0]->members_count ) ) {
1504
1505 // Live Count.
1506 $result['count'] = $data->response[0]->members_count;
1507 }
1508 }
1509
1510 // Maybe Set Cache.
1511 $this->maybe_set_cache( $network, $result, $cache );
1512
1513 return $result;
1514 }
1515 /**
1516 * Get strava followers count
1517 *
1518 * @since 1.0.0
1519 * @param bool $cache Cache Results.
1520 */
1521 public function get_strava( $cache = true ) {
1522
1523 $network = 'strava';
1524 $result = array(
1525 'count' => 0,
1526 );
1527
1528 // Check user.
1529 if ( ! $this->config['strava_user'] ) {
1530 $result['error'] = esc_html__( 'Please enter a Strava User.', 'powerkit' );
1531 return $result;
1532 }
1533
1534 // Get Count.
1535 $data = $this->curl_get_api( array(
1536 'network' => $network,
1537 'url' => 'https://www.strava.com/athletes/' . $this->config['strava_user'],
1538 'params' => array(
1539 'format' => 'json',
1540 ),
1541 'cache' => $cache,
1542 'decode' => false,
1543 ) );
1544
1545 // Cached Count.
1546 if ( is_array( $data ) && isset( $data['count'] ) ) {
1547 return $data;
1548 }
1549
1550 // Set Count.
1551 $strava_result = preg_match( '/followersCount&quot;:(\d*?),&quot;/s', $data, $strava_data );
1552
1553 if ( $strava_result ) {
1554 $strava_data_full = array_shift( $strava_data );
1555 $strava_data_number = array_shift( $strava_data );
1556
1557 // Live Count.
1558 $result['count'] = (int) $strava_data_number;
1559 } else {
1560 $result['error'] = esc_html__( 'Data not found. Please check your user ID.', 'powerkit' );
1561 }
1562
1563 // Maybe Set Cache.
1564 $this->maybe_set_cache( $network, $result, $cache );
1565
1566 return $result;
1567 }
1568
1569 /**
1570 * Get random user agent
1571 *
1572 * @since 1.0.0
1573 */
1574 public function get_random_user_agent() {
1575 $user_agents = array(
1576 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
1577 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36',
1578 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1',
1579 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246',
1580 );
1581
1582 $random = rand( 0, count( $user_agents ) - 1 );
1583
1584 return $user_agents[ $random ];
1585 }
1586
1587 /**
1588 * Parse all characters in a string and retrieve only the numeric ones.
1589 *
1590 * @param string $td_string The string.
1591 */
1592 private function extract_numbers_from_string( $td_string ) {
1593 $buffy = null;
1594 $td_string = preg_split( '//u', $td_string, -1, PREG_SPLIT_NO_EMPTY );
1595
1596 foreach ( $td_string as $td_char ) {
1597 if ( is_numeric( $td_char ) ) {
1598 $buffy .= $td_char;
1599 }
1600 }
1601 return $buffy;
1602 }
1603
1604 /**
1605 * Error format
1606 *
1607 * @param int $value The message.
1608 */
1609 public function error_format( $value = null ) {
1610 return (string) $value;
1611 }
1612
1613 /**
1614 * Count format
1615 *
1616 * @param int $value Count number.
1617 */
1618 public function count_format( $value = 0 ) {
1619 $result = '';
1620 $value = (int) $value;
1621
1622 $count_format = apply_filters( 'powerkit_social_links_count_format', true );
1623
1624 if ( $count_format ) {
1625 if ( $value > 999 && $value <= 999999 ) {
1626 $result = round( $value / 1000 ) . esc_html__( 'K', 'powerkit' );
1627 } elseif ( $value > 999999 ) {
1628 $result = number_format( $value / 1000000, 1, '.', '' ) . esc_html__( 'M', 'powerkit' );
1629
1630 $result = str_replace( '.0', '', $result );
1631 } else {
1632 $result = $value;
1633 }
1634 } else {
1635 $result = $value;
1636 }
1637
1638 return $result;
1639 }
1640
1641 /**
1642 * Get Network Count
1643 *
1644 * @param string $network Social network name.
1645 * @param bool $cache Cache Results.
1646 */
1647 public function get_count( $network, $cache = true ) {
1648 $count_function = 'get_' . str_replace( '-', '_', $network );
1649
1650 // Get Count.
1651 if ( method_exists( $this, $count_function ) ) {
1652 $data = $this->$count_function( $cache );
1653
1654 return $this->safe_result( $network, $data );
1655 }
1656 }
1657 }
1658
1659 /**
1660 * Get Count
1661 *
1662 * @param string $network Social network name.
1663 * @param bool $labels Show network labels.
1664 * @param bool $cache Cache results.
1665 * @param bool $array_format Format of output.
1666 */
1667 function powerkit_social_links_get_count( $network = '', $labels = true, $cache = true, $array_format = false ) {
1668
1669 // Get Count.
1670 $counter = new Powerkit_Links_Social_Counter();
1671
1672 // Manual Count Override.
1673 $count_override = get_option( sprintf( 'powerkit_social_links_%s_override', $network ) );
1674
1675 if ( $count_override ) {
1676 return array(
1677 'count' => $array_format ? $counter->count_format( $count_override ) : $count_override,
1678 );
1679 }
1680
1681 $result = $counter->get_count( $network, $cache );
1682
1683 $result = apply_filters( 'powerkit_social_links_count', $result, $network );
1684
1685 if ( $array_format ) {
1686
1687 if ( isset( $result['count'] ) ) {
1688 $result['count'] = $counter->count_format( $result['count'] );
1689 }
1690
1691 return $result;
1692 } else {
1693
1694 if ( isset( $result['error'] ) ) {
1695 $output = $counter->error_format( $result['error'] );
1696 } else {
1697 $output = $counter->count_format( $result['count'] );
1698 }
1699
1700 return $output;
1701 }
1702 }
1703