-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.php
226 lines (187 loc) · 6.69 KB
/
index.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
<?php
/*
Plugin Name: TNW Social Count
Plugin URI: http://stijlfabriek.com
Description: Save the number of Twitter retweets, Facebook likes, Google Plus and LinkedIn shares as post_meta information.
Version: 1.0
Author: Pablo Román for The Next Web
Author URI: http://stijlfabriek.com
*/
include dirname(__FILE__).'/config.php';
include dirname(__FILE__).'/frontend/share-button.php';
function tnwsc_setup()
{
include dirname(__FILE__).'/config.php';
foreach($tnwsc_wp_options as $key => $value) {
update_option($key, $value);
}
}
function tnwsc_deactivate()
{
include dirname(__FILE__).'/config.php';
foreach($tnwsc_wp_options as $key => $value) {
delete_option($key);
}
$hook = "tnwsc_sync";
wp_clear_scheduled_hook( $hook );
}
function tnwsc_log($message)
{
$tnwsc_log_path = get_option( 'tnwsc_log_path' );
if( is_writable( dirname( $tnwsc_log_path ) ) ) {
error_log( date('Y-m-d H:i:s', time())." - ".$message."\n", 3, $tnwsc_log_path);
}
}
function tnwsc_process( $all_posts = false )
{
tnwsc_log('Request: tnwsc_process()');
// Schedule next sync inmediately
if( get_option( 'tnwsc_active_sync' ) == 1) {
tnwsc_schedule_sync();
}
$tnwsc_services = get_option( 'tnwsc_services' );
$tnwsc_debug = get_option( 'tnwsc_debug' );
$posts = tnwsc_get_posts( $all_posts );
if( $posts ) {
foreach( $posts as $post ) {
$permalink = get_permalink( $post->ID );
$debug_info = '';
foreach( $tnwsc_services as $service_name => $enabled ) {
if( $enabled ) {
$count = tnwsc_get_count( $permalink, $service_name );
$debug_info .= ' / '.$service_name." (".$count.")";
if($tnwsc_debug == 0) {
tnwsc_update_post_meta( $post->ID, $service_name, $count );
}
}
}
tnwsc_log("Request: ".$permalink. $debug_info);
}
}
return count($posts);
}
function filter_by_date( $where = '' )
{
$post_range = get_option( 'tnwsc_post_range' );
$where .= " AND post_date > '" . date( 'Y-m-d H:i:s', time() - $post_range ) . "'";
return $where;
}
function tnwsc_get_posts( $all_posts = false )
{
if(!$all_posts) {
add_filter( 'posts_where', 'filter_by_date' );
}
$posts = query_posts( $query_string.'post_type=post&post_status=publish&posts_per_page=-1&' );
if(!$all_posts) {
remove_filter( 'posts_where', 'filter_by_date' );
}
return $posts;
}
function tnwsc_get_count( $permalink, $service )
{
global $tnwsc_config;
if( $service === 'google' ) {
return tnwsc_do_curl( $permalink, $service );
} else {
// If there are extra params in the url, append them to the permalink first
if( isset( $tnwsc_config['services'][$service]["params"] ) ) {
$permalink = sprintf( $tnwsc_config['services'][$service]["params"], $permalink );
}
$url = sprintf( $tnwsc_config['services'][$service]["url"], urlencode ( $permalink ) );
return tnwsc_do_curl( $url, $service );
}
}
function tnwsc_do_curl($url, $service)
{
global $tnwsc_config;
$social_count = 0;
// Google+ is an special, hack-ish case
if( $service == 'google' )
{
// GET +1s. Credits to Tom Anthony: http://www.tomanthony.co.uk/blog/google_plus_one_button_seo_count_api/
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, "https://clients6.google.com/rpc" );
curl_setopt( $curl, CURLOPT_POST, 1 );
curl_setopt( $curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]' );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Content-type: application/json' ) );
$curl_results = curl_exec ( $curl );
curl_close ( $curl );
$json = json_decode($curl_results, true);
$social_count = intval( $json[0]['result']['metadata']['globalCounts']['count'] );
} else {
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec( $ch );
if( curl_errno( $ch ) ) {
$error = print_r( curl_error( $ch ), true );
// TODO: Notify curl errors via email or log files
} else {
switch( $service ) {
case 'facebook':
$return = json_decode( $return, true );
$social_count = ( isset( $return['data'][0]['total_count'] ) ) ? $return['data'][0]['total_count'] : 0;
// TODO: Better handling of errors
if( isset( $return['error'] ) ) {
tnwsc_log( "Error ".$return["code"]." (".$return["type"].") - ".$return["message"] );
}
break;
case 'twitter':
$return = json_decode( $return, true );
$social_count = ( isset($return['count'] ) ) ? $return['count'] : 0;
break;
case 'linkedin':
$return = json_decode( str_replace( 'IN.Tags.Share.handleCount(', '', str_replace( ');', '', $return ) ), true );
$social_count = ( isset( $return['count'] ) ) ? $return['count'] : 0;
break;
}
}
}
return intval( $social_count );
}
function tnwsc_update_post_meta( $post_id, $service, $count )
{
$key = 'tnwsc_'.$service;
$old_count = get_post_meta( $post_id, $key, true);
// Check if the previous value is smaller. If it is, do not update, it's probably an error from the cURL call to the API
if( $count > $old_count ) {
update_post_meta( $post_id, 'tnwsc_'.$service, $count );
}
}
function tnwsc_schedule_sync( $immediate = false )
{
global $tnwsc_wp_options;
$hook = "tnwsc_sync";
$tnwsc_sync_frequency = get_option( 'tnwsc_sync_frequency' ) ? get_option( 'tnwsc_sync_frequency' ) : $tnwsc_wp_options['tnwsc_sync_frequency'];
tnwsc_log( "tnwsc_schedule_sync() - Synching in ".$tnwsc_sync_frequency." seconds" );
if ( $immediate ) {
wp_schedule_single_event( time() -1, $hook );
} else {
//schedule the next sync in typical fashion
wp_schedule_single_event( time() + $tnwsc_sync_frequency, $hook );
}
}
// Backend actions
add_action( 'tnwsc_sync', 'tnwsc_process' );
register_activation_hook( __FILE__, 'tnwsc_setup' );
register_deactivation_hook( __FILE__, 'tnwsc_deactivate' );
// Frontend actions
add_action('wp_enqueue_scripts', 'tnwsc_frontend_init');
// admin menu hook
function tnwsc_admin_menu() {
add_options_page( 'TNW Social Count Options', 'TNW Social Count', 'manage_options', 'tnwsc-admin', 'tnwsc_admin_page');
}
/// admin page hook
function tnwsc_admin_page(){
include dirname(__FILE__).'/admin.php';
}
// register admin pages
add_action('admin_menu', 'tnwsc_admin_menu');
function tnwsc_plugin_action_links( $links, $file ) {
if ( $file == plugin_basename( dirname(__FILE__).'/index.php' ) ) {
$links[] = '<a href="admin.php?page=tnwsc-admin">'.__('Settings').'</a>';
}
return $links;
}
add_filter( "plugin_action_links", 'tnwsc_plugin_action_links', 10, 2 );