This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
accesspress-social-counter.php
589 lines (513 loc) · 25.6 KB
/
accesspress-social-counter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php defined('ABSPATH') or die("No script kiddies please!");
/**
* Plugin Name: AccessPress Social Counter
* Plugin URI: https://accesspressthemes.com/wordpress-plugins/accesspress-social-counter/
* Description: A plugin to display your social accounts fans, subscribers and followers number on your website with handful of backend settings and interface.
* Version: 1.3.6
* Author: AccessPress Themes
* Author URI: http://accesspressthemes.com
* Text Domain: aps-counter
* Domain Path: /languages/
* Network: false
* License: GPL2
*/
/**
* Declartion of necessary constants for plugin
* */
if (!defined('SC_IMAGE_DIR')) {
define('SC_IMAGE_DIR', plugin_dir_url(__FILE__) . 'images');
}
if (!defined('SC_JS_DIR')) {
define('SC_JS_DIR', plugin_dir_url(__FILE__) . 'js');
}
if (!defined('SC_CSS_DIR')) {
define('SC_CSS_DIR', plugin_dir_url(__FILE__) . 'css');
}
if (!defined('SC_VERSION')) {
define('SC_VERSION', '1.3.6');
}
/**
* Register of widgets
* */
include_once('inc/backend/widget.php');
if (!class_exists('SC_Class')) {
class SC_Class {
var $apsc_settings;
/**
* Initializes the plugin functions
*/
function __construct() {
$this->apsc_settings = get_option('apsc_settings');
register_activation_hook(__FILE__, array($this, 'load_default_settings')); //loads default settings for the plugin while activating the plugin
add_action('init', array($this, 'plugin_text_domain')); //loads text domain for translation ready
add_action('init', array($this, 'session_init')); //starts the session
add_action('admin_menu', array($this, 'add_sc_menu')); //adds plugin menu in wp-admin
add_action('admin_enqueue_scripts', array($this, 'register_admin_assets')); //registers admin assests such as js and css
add_action('wp_enqueue_scripts', array($this, 'register_frontend_assets')); //registers js and css for frontend
add_action('admin_post_apsc_settings_action', array($this, 'apsc_settings_action')); //recieves the posted values from settings form
add_action('admin_post_apsc_restore_default', array($this, 'apsc_restore_default')); //restores default settings;
add_action('widgets_init', array($this, 'register_apsc_widget')); //registers the widget
add_shortcode('aps-counter', array($this, 'apsc_shortcode')); //adds a shortcode
add_shortcode('aps-get-count',array($this,'apsc_count_shortcode')); //
add_action('admin_post_apsc_delete_cache', array($this, 'apsc_delete_cache')); //deletes the counter values from cache
}
/**
* Plugin Translation
*/
function plugin_text_domain() {
load_plugin_textdomain('aps-counter', false, basename(dirname(__FILE__)) . '/languages/');
}
/**
* Load Default Settings
* */
function load_default_settings() {
if (!get_option('apsc_settings')) {
$apsc_settings = $this->get_default_settings();
update_option('apsc_settings', $apsc_settings);
}
}
/**
* Plugin Admin Menu
*/
function add_sc_menu() {
add_menu_page(__('AccessPress Social Counter', 'aps-counter'), __('AccessPress Social Counter', 'aps-counter'), 'manage_options', 'ap-social-counter', array($this, 'sc_settings'), SC_IMAGE_DIR.'/sc-icon.png');
}
/**
* Plugin Main Settings Page
*/
function sc_settings() {
include('inc/backend/settings.php');
}
/**
* Registering of backend js and css
*/
function register_admin_assets() {
if (isset($_GET['page']) && $_GET['page'] == 'ap-social-counter') {
wp_enqueue_style('sc-admin-css', SC_CSS_DIR . '/backend.css', array(), SC_VERSION);
wp_enqueue_script('sc-admin-js', SC_JS_DIR . '/backend.js', array('jquery', 'jquery-ui-sortable'), SC_VERSION);
}
wp_enqueue_style('fontawesome-css', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css',false,SC_VERSION);
}
/**
* Registers Frontend Assets
* */
function register_frontend_assets() {
wp_enqueue_style('apsc-font-awesome','https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css',array(),SC_VERSION);
wp_enqueue_style('apsc-frontend-css', SC_CSS_DIR . '/frontend.css', array(), SC_VERSION);
}
/**
* Saves settings to database
*/
function apsc_settings_action() {
if (!empty($_POST) && wp_verify_nonce($_POST['apsc_settings_nonce'], 'apsc_settings_action')) {
include('inc/backend/save-settings.php');
}
}
/**
* Prints array in pre format
*/
function print_array($array) {
echo "<pre>";
print_r($array);
echo "</pre>";
}
/**
* Starts the session
*/
function session_init() {
if (!session_id()) {
session_start();
}
}
/**
* Restores the default
*/
function apsc_restore_default() {
if (!empty($_GET) && wp_verify_nonce($_GET['_wpnonce'], 'apsc-restore-default-nonce')) {
$apsc_settings = $this->get_default_settings();
update_option('apsc_settings', $apsc_settings);
$_SESSION['apsc_message'] = __('Default Settings Restored Successfully', 'aps-counter');
wp_redirect(admin_url() . 'admin.php?page=ap-social-counter');
}
}
/**
* Returns Default Settings
*/
function get_default_settings() {
$apsc_settings = array('social_profile' => array('facebook' => array('page_id' => ''),
'twitter' => array('username' => '', 'consumer_key' => '', 'consumer_secret' => '', 'access_token' => '', 'access_token_secret' => ''),
'googlePlus' => array('page_id' => '', 'api_key' => ''),
'instagram' => array('username' => '', 'access_token' => '','user_id'=>''),
'youtube' => array('username' => '', 'channel_url' => ''),
'soundcloud' => array('username' => '', 'client_id' => ''),
'dribbble' => array('username' => ''),
),
'profile_order' => array('facebook', 'twitter', 'googlePlus', 'instagram', 'youtube', 'soundcloud', 'dribbble', 'posts', 'comments'),
'social_profile_theme' => 'theme-1',
'counter_format'=>'comma',
'cache_period' => ''
);
return $apsc_settings;
}
/**
* AccessPress Social Counter Widget
*/
function register_apsc_widget() {
register_widget('APSC_Widget');
}
/**
* Adds Shortcode
*/
function apsc_shortcode($atts) {
ob_start();
include('inc/frontend/shortcode.php');
$html = ob_get_contents();
ob_get_clean();
return $html;
}
/**
* Clears the counter cache
*/
function apsc_delete_cache() {
if (!empty($_GET) && wp_verify_nonce($_GET['_wpnonce'], 'apsc-cache-nonce')) {
$transient_array = array('apsc_facebook', 'apsc_twitter', 'apsc_youtube', 'apsc_instagram', 'apsc_googlePlus', 'apsc_soundcloud', 'apsc_dribbble', 'apsc_posts', 'apsc_comments');
foreach ($transient_array as $transient) {
delete_transient($transient);
}
$_SESSION['apsc_message'] = __('Cache Deleted Successfully', 'aps-counter');
wp_redirect(admin_url() . 'admin.php?page=ap-social-counter');
}
}
/**
*
* @param type $user
* @param type $consumer_key
* @param type $consumer_secret
* @param type $oauth_access_token
* @param type $oauth_access_token_secret
* @return string
*/
function authorization($user, $consumer_key, $consumer_secret, $oauth_access_token, $oauth_access_token_secret) {
$query = 'screen_name=' . $user;
$signature = $this->signature($query, $consumer_key, $consumer_secret, $oauth_access_token, $oauth_access_token_secret);
return $this->header($signature);
}
/**
*
* @param type $url
* @param type $query
* @param type $method
* @param type $params
* @return type string
*/
function signature_base_string($url, $query, $method, $params) {
$return = array();
ksort($params);
foreach ($params as $key => $value) {
$return[] = $key . '=' . $value;
}
return $method . "&" . rawurlencode($url) . '&' . rawurlencode(implode('&', $return)) . '%26' . rawurlencode($query);
}
/**
*
* @param type $query
* @param type $consumer_key
* @param type $consumer_secret
* @param type $oauth_access_token
* @param type $oauth_access_token_secret
* @return type array
*/
function signature($query, $consumer_key, $consumer_secret, $oauth_access_token, $oauth_access_token_secret) {
$oauth = array(
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => hash_hmac('sha1', time(), true),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
$api_url = 'https://api.twitter.com/1.1/users/show.json';
$base_info = $this->signature_base_string($api_url, $query, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
return $oauth;
}
/**
* Build the header.
*
* @param array $signature OAuth signature.
*
* @return string OAuth Authorization.
*/
public function header($signature) {
$return = 'OAuth ';
$values = array();
foreach ($signature as $key => $value) {
$values[] = $key . '="' . rawurlencode($value) . '"';
}
$return .= implode(', ', $values);
return $return;
}
/**
* Returns twitter count
*/
function get_twitter_count() {
$apsc_settings = $this->apsc_settings;
$user = $apsc_settings['social_profile']['twitter']['username'];
$api_url = 'https://api.twitter.com/1.1/users/show.json';
$params = array(
'method' => 'GET',
'sslverify' => false,
'timeout' => 60,
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => $this->authorization(
$user, $apsc_settings['social_profile']['twitter']['consumer_key'], $apsc_settings['social_profile']['twitter']['consumer_secret'], $apsc_settings['social_profile']['twitter']['access_token'], $apsc_settings['social_profile']['twitter']['access_token_secret']
)
)
);
$connection = wp_remote_get($api_url . '?screen_name=' . $user, $params);
if (is_wp_error($connection)) {
$count = 0;
} else {
$_data = json_decode($connection['body'], true);
if (isset($_data['followers_count'])) {
$count = intval($_data['followers_count']);
} else {
$count = 0;
}
}
return $count;
}
/**
*
* @param int $count
* @param string $format
*/
function get_formatted_count($count, $format) {
if($count==''){
return '';
}
switch ($format) {
case 'comma':
$count = number_format($count);
break;
case 'short':
$count = $this->abreviateTotalCount($count);
break;
default:
break;
}
return $count;
}
/**
*
* @param integer $value
* @return string
*/
function abreviateTotalCount($value) {
$abbreviations = array(12 => 'T', 9 => 'B', 6 => 'M', 3 => 'K', 0 => '');
foreach ($abbreviations as $exponent => $abbreviation) {
if ($value >= pow(10, $exponent)) {
return round(floatval($value / pow(10, $exponent)), 1) . $abbreviation;
}
}
}
function facebook_count($url){
// Query in FQL
$fql = "SELECT like_count ";
$fql .= " FROM link_stat WHERE url = '$url'";
$fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql);
// Facebook Response is in JSON
$response = file_get_contents($fqlURL);
$response = json_decode($response);
return $response[0]->like_count;
}
function get_count($social_media){
$count = 0;
$apsc_settings = $this->apsc_settings;
$cache_period = ($apsc_settings['cache_period'] != '') ? $apsc_settings['cache_period']*60*60 : 24 * 60 * 60;
switch($social_media){
case 'facebook':
$facebook_page_id = $apsc_settings['social_profile']['facebook']['page_id'];
$facebook_count = get_transient('apsc_facebook');
if (false === $facebook_count) {
$api_url = 'https://www.facebook.com/' . $facebook_page_id;
$count = $this->facebook_count($api_url);
set_transient('apsc_facebook', $count, $cache_period);
} else {
$count = $facebook_count;
}
$default_count = isset($apsc_settings['social_profile']['facebook']['default_count'])?$apsc_settings['social_profile']['facebook']['default_count']:0;
$count = ($count==0)?$default_count:$count;
if($count!=0){
set_transient('apsc_facebook',$count,$cache_period);
}
break;
case 'twitter':
$twitter_count = get_transient('apsc_twitter');
if (false === $twitter_count) {
$count = ($this->get_twitter_count());
set_transient('apsc_twitter', $count, $cache_period);
} else {
$count = $twitter_count;
}
break;
case 'googlePlus':
$social_profile_url = 'https://plus.google.com/' . $apsc_settings['social_profile']['googlePlus']['page_id'];
$googlePlus_count = get_transient('apsc_googlePlus');
if (false === $googlePlus_count) {
$api_url = 'https://www.googleapis.com/plus/v1/people/' . $apsc_settings['social_profile']['googlePlus']['page_id'] . '?key=' . $apsc_settings['social_profile']['googlePlus']['api_key'];
$params = array(
'sslverify' => false,
'timeout' => 60
);
$connection = wp_remote_get($api_url, $params);
if (is_wp_error($connection)) {
$count = 0;
} else {
$_data = json_decode($connection['body'], true);
if (isset($_data['circledByCount'])) {
$count = (intval($_data['circledByCount']));
set_transient('apsc_googlePlus', $count,$cache_period);
} else {
$count = 0;
}
}
} else {
$count = $googlePlus_count;
}
break;
case 'instagram':
$username = $apsc_settings['social_profile']['instagram']['username'];
$user_id = $apsc_settings['social_profile']['instagram']['user_id'];
$social_profile_url = 'https://instagram.com/' . $username;
$instagram_count = get_transient('apsc_instagram');
if (false === $instagram_count) {
$access_token = $apsc_settings['social_profile']['instagram']['access_token'];
$api_url = 'https://api.instagram.com/v1/users/' . $user_id . '?access_token=' . $access_token;
$params = array(
'sslverify' => false,
'timeout' => 60
);
$connection = wp_remote_get($api_url, $params);
if (is_wp_error($connection)) {
$count = 0;
} else {
$response = json_decode($connection['body'], true);
if (
isset($response['meta']['code']) && 200 == $response['meta']['code'] && isset($response['data']['counts']['followed_by'])
) {
$count = (intval($response['data']['counts']['followed_by']));
set_transient('apsc_instagram',$count,$cache_period);
} else {
$count = 0;
}
}
} else {
$count = $instagram_count;
}
break;
case 'youtube':
$social_profile_url = esc_url($apsc_settings['social_profile']['youtube']['channel_url']);
$count = $apsc_settings['social_profile']['youtube']['subscribers_count'];
break;
case 'soundcloud':
$username = $apsc_settings['social_profile']['soundcloud']['username'];
$social_profile_url = 'https://soundcloud.com/' . $username;
$soundcloud_count = get_transient('apsc_soundcloud');
if (false === $soundcloud_count) {
$api_url = 'https://api.soundcloud.com/users/' . $username . '.json?client_id=' . $apsc_settings['social_profile']['soundcloud']['client_id'];
$params = array(
'sslverify' => false,
'timeout' => 60
);
$connection = wp_remote_get($api_url, $params);
if (is_wp_error($connection)) {
$count = 0;
} else {
$response = json_decode($connection['body'], true);
if (isset($response['followers_count'])) {
$count = (intval($response['followers_count']));
set_transient( 'apsc_soundcloud',$count,$cache_period );
} else {
$count = 0;
}
}
} else {
$count = $soundcloud_count;
}
break;
case 'dribbble':
$social_profile_url = 'http://dribbble.com/'.$apsc_settings['social_profile']['dribbble']['username'];
$dribbble_count = get_transient('apsc_dribbble');
if (false === $dribbble_count) {
$username = $apsc_settings['social_profile']['dribbble']['username'];
$api_url = 'http://api.dribbble.com/' . $username;
$params = array(
'sslverify' => false,
'timeout' => 60
);
$connection = wp_remote_get($api_url, $params);
if (is_wp_error($connection)) {
$count = 0;
} else {
$response = json_decode($connection['body'], true);
if (isset($response['followers_count'])) {
$count = (intval($response['followers_count']));
set_transient('apsc_dribbble',$count,$cache_period );
} else {
$count = 0;
}
}
} else {
$count = $dribbble_count;
}
break;
case 'posts':
$posts_count = get_transient('apsc_posts');
if (false === $posts_count) {
$posts_count = wp_count_posts();
$count = $posts_count->publish;
set_transient('apsc_posts', $count, $cache_period);
} else {
$count = $posts_count;
}
break;
case 'comments':
$comments_count = get_transient('apsc_comments');
if (false === $comments_count) {
$data = wp_count_comments();
$count = ($data->approved);
set_transient('apsc_comments', $count, $cache_period);
} else {
$count = $comments_count;
}
break;
default:
break;
}
return $count;
}
/**
*
* Counter Only Shortcode
* */
function apsc_count_shortcode($atts){
if(isset($atts['social_media'])){
$count = $this->get_count($atts['social_media']);
if(isset($atts['count_format']) && $count!=''){
$count = $this->get_formatted_count($count,$atts['count_format']);
}
return $count;
}
}
}
$sc_object = new SC_Class(); //initialization of plugin
}
add_action( 'wp_enqueue_scripts', 'remove_font_awesome', 99 );
function remove_font_awesome(){
wp_dequeue_style( 'apsc-font-awesome-css' );
}
remove_action('wp_enqueue_scripts', array('SC_Class', 'register_frontend_assets'),99);