-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoopit-importer.php
377 lines (276 loc) · 12.4 KB
/
scoopit-importer.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
<?php
/*
Plugin Name: Scoop.It Importer
Plugin URI: http://june22.eu
Description: Import automatically all curated posts from a specific Scoop.It topic as WordPress CPT
Version: 1.3.3
Author: Thomas Charbit
Author URI: https://twitter.com/thomascharbit
Author Email: [email protected]
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
class ScoopitImporter {
const SLUG = 'scoopit-importer';
private $defaults = array (
'scoopit_consumer_key' => null,
'scoopit_consumer_secret' => null,
'scoopit_account' => null,
'scoopit_topic' => null,
'recurrence' => 'hourly',
'post_type' => 'post',
'post_author' => 1,
'post_status' => 'draft'
);
private $post_statuses = array (
'draft',
'publish',
'pending',
'private'
);
private $settings;
/*--------------------------------------------*
* Constructor
*--------------------------------------------*/
/**
* Initializes the plugin by setting localization, filters, and administration functions.
*/
function __construct() {
// Load plugin text domain
add_action( 'init', array($this, 'si_textdomain' ) );
// Register hooks that are fired when the plugin is activated, deactivated.
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
// Hook for cron job
add_action('scoopit_scheduled_hook', array(&$this, 'scoopit_import_topic'));
// Add more time intervals for cron job
add_filter( 'cron_schedules', array(&$this, 'more_schedules') );
// Add conf page to admin menu
add_action('admin_menu', array(&$this, 'admin_menu'));
} // end constructor
/**
* Fired when the plugin is activated.
*/
public function activate( $network_wide ) {
$this->get_settings();
wp_schedule_event( time(), $this->settings['recurrence'], 'scoopit_scheduled_hook');
} // end activate
/**
* Fired when the plugin is deactivated.
*/
public function deactivate( $network_wide ) {
wp_clear_scheduled_hook('scoopit_scheduled_hook');
} // end deactivate
/**
* Loads the plugin text domain for translation
*/
public function si_textdomain() {
$domain = 'scoopit-importer-locale';
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR.'/'.$domain.'/'.$domain.'-'.$locale.'.mo' );
load_plugin_textdomain( $domain, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
} // end plugin_textdomain
/*--------------------------------------------*
* Core Functions
*---------------------------------------------*/
public function admin_menu() {
add_options_page(
'Scoop.It Importer',
'Scoop.It Importer',
'manage_options',
self::SLUG ,
array(&$this, 'admin_page')
);
}
public function admin_page() {
// Make sure the user has the required permissions to view the settings.
if (!current_user_can('manage_options')) {
wp_die('Sorry, you don\'t have the permissions to access this page.');
}
$this->get_settings();
// Form submit handler
if ( isset( $_POST['scoopit-force'] ) && check_admin_referer( 'scoopit-form-force' ) ) {
do_action('scoopit_scheduled_hook');
}
// Form submit handler
if ( isset( $_POST['scoopit-submit'] ) && check_admin_referer( 'scoopit-form' ) ) {
$this->settings = array_merge( $this->settings, array(
'scoopit_consumer_key' => $_POST['si_app_consumer_key'],
'scoopit_consumer_secret' => $_POST['si_app_consumer_secret'],
'scoopit_account' => $_POST['si_user_account'],
'post_type' => $_POST['si_post_type'],
'post_author' => $_POST['si_post_author'],
'post_status' => $_POST['si_post_status']
));
if ( ( isset($_POST['si_topic'] ) ) && ( !empty($_POST['si_topic'] ) ) )
$this->settings['scoopit_topic'] = $_POST['si_topic'];
else $this->settings['scoopit_topic'] = null;
if ($this->settings['recurrence'] != $_POST['si_recurrence']) {
$this->settings['recurrence'] = $_POST['si_recurrence'];
wp_clear_scheduled_hook( 'scoopit_scheduled_hook' );
wp_schedule_event( time(), $this->settings['recurrence'], 'scoopit_scheduled_hook' );
}
}
include_once( plugin_dir_path(__FILE__) . 'Scoopit-PHP/ScoopIt.php' );
$scoop = new ScoopIt( new WpTokenStore(), $this->settings['scoopit_consumer_key'], $this->settings['scoopit_consumer_secret'] );
// test app credientials
try {
$scoop->test();
if ( isset( $_GET['scoopit-logout'] ) && $scoop->isLoggedIn() ) $scoop->logout();
if ( isset( $_GET['oauth_token'] ) && ( isset( $_GET['oauth_verifier'] ) ) ) $scoop->login();
// test username
try {
$useraccount_data = $scoop->resolve( 'User', $this->settings['scoopit_account'] );
$profile = $scoop->profile($useraccount_data->id);
$topics = $profile->user->curatedTopics;
}
catch ( ScoopAuthenticationException $resolveUserError ) {
$this->settings['scoopit_topic'] = null;
}
}
catch ( ScoopAuthenticationException $appCredentialsError ) {
$this->settings['scoopit_topic'] = null;
}
$this->save_settings();
include( plugin_dir_path(__FILE__) . 'admin.php' );
}
public function more_schedules( $schedules ) {
$more_schedules = array(
'fiveminutes' => array(
'interval' => 300,
'display' => __('Every 5 minutes')
),
'fifteenminutes' => array(
'interval' => 900,
'display' => __('Every 15 minutes')
),
'twicehourly' => array(
'interval' => 1800,
'display' => __('Twice Hourly')
)
);
return array_merge( $schedules, $more_schedules );
}
public function scoopit_import_topic() {
global $wpdb;
$this->get_settings();
// do nothing if no topic was configured
if ( $this->settings['scoopit_topic'] === NULL ) return false;
// load scoopit
include_once( plugin_dir_path(__FILE__) . 'Scoopit-PHP/ScoopIt.php' );
$scoop = new ScoopIt( new WpTokenStore(), $this->settings['scoopit_consumer_key'], $this->settings['scoopit_consumer_secret'] );
// get when we did last update
$lastupdate_timestamp = get_option( 'scoopitimporter.last_update' );
if ( ( !$lastupdate_timestamp ) || ( $lastupdate_timestamp== '' ) ) $lastupdate_timestamp = 0;
// get existing scoopit posts IDs in worpdress
$query = $wpdb->prepare("
SELECT meta_value
FROM $wpdb->posts p
JOIN $wpdb->postmeta meta
ON p.ID = meta.post_ID
WHERE p.post_type = %s
AND meta.meta_key = %s
",
$this->settings['post_type'],
'_scoopit_id'
);
$existing_posts = $wpdb->get_col( $query );
$create_post_error = false;
$current_time = current_time( 'timestamp' );
try {
// get new posts from scoopit
$topic = $scoop->topic( $this->settings['scoopit_topic'], 999, 0, 0, $lastupdate_timestamp );
$curated_posts = array_reverse($topic->curatedPosts );
$create_post_error = false;
foreach ( $curated_posts as $curated_post ) {
// check if already exists
if ( !in_array($curated_post->id, $existing_posts) ) {
if ( isset($curated_post->largeImageUrl) ) {
$attachment_id = $this->get_image($curated_post->largeImageUrl, 0, $curated_post->title);
}
$post_data = array(
'post_type' => $this->settings['post_type'],
'post_title' => $curated_post->title,
'post_content' => $curated_post->htmlContent,
'post_author' => $this->settings['post_author'],
'post_status' => $this->settings['post_status'],
// convert GMT to local time
'post_date' => date( 'Y-m-d H:i:s', $curated_post->curationDate / 1000 + ( get_option( 'gmt_offset' ) * 3600 ) )
);
$post_data = apply_filters( 'scoopit_wp_insert_post_data', $post_data, $curated_post );
// insert post
$post_id = wp_insert_post( $post_data );
// store original scoopit id as custom field
if ( $post_id > 0 ) {
add_post_meta( $post_id,'_scoopit_id', $curated_post->id );
if ( isset($attachment_id) && ! is_wp_error($attachment_id) ) {
add_post_meta( $post_id,'_thumbnail_id', $attachment_id );
wp_update_post( array('ID' => $attachment_id, 'post_parent' => $post_id));
}
do_action( 'scoopit_after_wp_insert_post', $post_id, $curated_post );
}
else $create_post_error = true;
}
}
// import successful, lets update our update timestamp
if ( !$create_post_error ) {
update_option( 'scoopitimporter.last_update', (string) $current_time );
}
}
catch ( ScoopAuthenticationException $resolveUserError ) {
var_dump( $resolveUserError );
}
}
private function get_settings() {
$this->settings = get_option( 'scoopitimporter.settings' );
if ( !is_array($this->settings) ) $this->settings = array();
$this->settings = array_merge( $this->defaults, $this->settings );
}
private function save_settings() {
update_option( 'scoopitimporter.settings', $this->settings );
}
private function clear_settings() {
delete_option( 'scoopitimporter.settings' );
}
private function get_image($file, $post_id, $desc = null) {
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$id = null;
if ( ! empty( $file ) ) {
// Download file to temp location
$tmp = download_url( $file );
$file_ext_and_type = $this->file_ext_and_type( $tmp );
$file_array['name'] = $desc.'.' . $file_ext_and_type['ext'];
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do the validation and storage stuff
$id = media_handle_sideload( $file_array, $post_id, $desc );
// If error storing permanently, unlink
if ( is_wp_error($id) ) {
@unlink($file_array['tmp_name']);
}
return $id;
}
else return false;
}
private function file_ext_and_type($full_path_to_image='') {
$extension = 'null';
if($image_type = exif_imagetype($full_path_to_image))
{
$extension = image_type_to_extension($image_type, false);
}
$known_replacements = array(
'jpeg' => 'jpg',
'tiff' => 'tif',
);
$extension = str_replace(array_keys($known_replacements), array_values($known_replacements), $extension);
return array('ext' => $extension, 'type' => $image_type);
}
}
$ScoopitImporter = new ScoopitImporter();