forked from setola/Wordpress-Theme-Utils-Classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterManager.class.php
141 lines (120 loc) · 3.35 KB
/
TwitterManager.class.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
<?php
/**
* Contains the definition of class TwitterManager
*/
/**
* Manages the connection with Twitter
* @author etessore
* @version 1.0.0
* @package classes
*/
class TwitterManager {
/**
* The OAuth customer key
* @var string
*/
public static $oauth_key;
/**
* The OAuth customer secret
* @var string
*/
public static $oauth_secret;
/**
* The OAuth token
* @var string
*/
public static $oauth_token;
/**
* The OAuth token secret
* @var string
*/
public static $oauth_token_secret;
/**
* The connection to Twitter APIs
* @var TwitterOAuth
*/
public static $connection;
/**
* Some transients for cache
* @var array
*/
public static $transients = array(
'tweets' => 'twitter_manager_my_tweets',
'oauth_token' => 'twitter_manager_oauth_token',
'oauth_token_secret' => 'twitter_manager_oauth_token_secret',
'oauth_temp_credentials' => 'twitter_manager_oauth_temp_credentials'
);
/**
* List of WP AJAX actions used by this feature
* @var array
*/
public static $actions = array(
'update_tweets' => array(
'action' => 'update_tweets',
'callback' => array(__CLASS__, 'on_ajax_update_tweets'),
),
'show_tweets' => array(
'action' => 'show_tweets',
'callback' => array(__CLASS__, 'on_ajax_show_tweets'),
),
);
/**
* Enables the connection with Twitter
*
* You need to create a new app and authorize it from your twitter account
*
* @param string $customer_key Consumer key
* @param string $customer_secret Consumer secret
* @param string $token Access token
* @param string $token_secret Access token secret
*/
public static function enable($customer_key, $customer_secret, $token, $token_secret){
self::$oauth_key = $customer_key;
self::$oauth_secret = $customer_secret;
self::$oauth_token = $token;
self::$oauth_token_secret = $token_secret;
foreach(self::$actions as $action){
add_action('wp_ajax_'.$action['action'], $action['callback']);
add_action('wp_ajax_nopriv_'.$action['action'], $action['callback']);
}
require_once WORDPRESS_THEME_UTILS_LIBRARIES_ABSOLUTE_PATH . 'twitteroauth/twitteroauth/twitteroauth.php';
self::$connection = new TwitterOAuth(
self::$oauth_key,
self::$oauth_secret,
self::$oauth_token,
self::$oauth_token_secret
);
}
/**
* Completely disables the connection with Twitter
*/
public static function disable(){
foreach(self::$actions as $action){
remove_action('wp_ajax_'.$action['action'], $action['callback']);
remove_action('wp_ajax_nopriv_'.$action['action'], $action['callback']);
}
}
/**
* Updates the list of tweets
*/
public static function on_ajax_update_tweets(){
$result = self::$connection->get('statuses/user_timeline');
if(isset($result->errors)){
wp_mail(
get_option('admin_email'),
__('Error on Twitter Manager', 'wtu_framework'),
'<pre>'.$result.'</pre>'
);
}
set_transient(self::$transients['tweets'], $result);
header('Content-type: application/json');
die(json_encode($result));
}
/**
* Shows the cached tweets
*/
public function on_ajax_show_tweets(){
header('Content-type: application/json');
die(json_encode(get_transient(self::$transients['tweets'])));
}
}