forked from ttrsshaxx/wp-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-rss.php
740 lines (560 loc) · 19.8 KB
/
wp-rss.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
<?php
/*
Plugin Name: RSS Feed Fetch
Description: Fetches RSS feeds and saves posts through a wp-cli command.
Author: Alain Jacomet
Version: 1.0.0
*/
register_activation_hook(__FILE__, 'wp_rss_activation');
add_action('wp_rss_daily_fetch_event', 'wp_rss_fetch');
function wp_rss_activation() {
wp_schedule_event(time(), 'daily', 'wp_rss_fetch_all');
}
function wp_rss_fetch_all() {
$rss = new RSSFeed();
$rss->fetch_and_save_all();
}
register_deactivation_hook(__FILE__, 'wp_rss_deactivate_event');
function wp_rss_deactivate_event() {
wp_clear_scheduled_hook('wp_rss_fetch_all');
}
if ( defined('WP_DEBUG_LOG') && WP_DEBUG_LOG === true && WP_DEBUG === true ) {
add_action('rsff_log', function($message) {
error_log($message);
});
}
if (!class_exists('RSSFeed')):
class RSSFeed {
/***
* The settings variable is configurable on a per-instance case and will be saved automatically to the database
* after everything is done.
*
* # Configuring an instance
*
* ```
* $rss = new RSSFeed();
* $rss->settings['post_status'] = 'draft';
* $rss->add('http://feed.url/');
* $rss->fetch();
* ```
*
* @var array
*/
public $settings;
/***
* Default settings to be used internally in case there is no option on the database specifying how to run or no
* custom settings have been defined in the instance.
*
* @var array
*/
private $default_settings = array(
'feeds' => array( 'http://allvoices.com/hub/118/feed.xml' ), // An array of default feeds
'post_category' => array( 0 ), // The category to assign to posts (empty array for none)
'post_author' => 1, // The author to assign to posts
'post_status' => 'draft', // The post status after it has been imported
'enclosures' => true, // Process enclosures
'_times_run' => 0, // Internal: amount of times it has run
'_managed_posts' => array( ), // Internal: Ids of the posts currently managed
'_last_run' => 0, // Internal: date of the last run
'_option_name' => 'rssff_options' // Internal: option name on the database
);
public function __construct() {
// Fetch the settings and place them as an instance variable later use
$this->settings = $this->getConfiguration();
$this->log('WP-RSS: Initialized RSSFeed object');
}
public function __destruct() {
$this->log('WP-RSS: Destructing RSSFeed object');
}
/***
* A stub that sanitizes an option before setting
* @return string
*/
public function sanitizeOption($option, $value) {
return json_decode($value);
}
/***
* Configurable logging function (to configure when using with WPCLI)
* @param string $message
* @return bool
*/
public function log($message = '') {
do_action('rsff_log', $message);
return true;
}
/***
* Clear the settings from the database
*
* @return bool
*/
public function clear_settings() {
$option = $this->settings['_option_name'];
$return = delete_option($option);
if ($return == true)
$this->log('WP-RSS: Cleared all settings');
else
$this->log('WP-RSS: Failed to clear settings');
return $return;
}
/***
* Save to the database and finish
*
* @return bool
*/
public function saveAndExit() {
$settings = $this->settings;
$settings['_times_run']++;
$settings['_last_run'] = time();
return $this->saveConfiguration($settings);
}
/***
* Save the settings object to the database
* @return bool
*/
public function saveConfiguration($settings) {
$settings = $this->settings;
$option = $settings['_option_name'];
return update_option($option, $settings);
}
/***
* Adds a new feed to the process queue. Returns the new number of feeds.
*
* @param $feed_uri
* @return int
*/
public function add($feed_uri) {
$return = array_push($this->settings['feeds'], $feed_uri);
if ($return > 0)
$this->log('WP-RSS: Successfully added ' . $feed_uri);
else
$this->log('WP-RSS: Failure to add feed');
$this->saveAndExit();
return $return;
}
/***
* Returns the feeds array
* @return mixed
*/
public function get_feeds() {
$settings = $this->settings;
return $settings['feeds'];
}
/***
* Returns the codes to be used for managing the posts
*
* @param $feed_index
* @return array
*/
public function get_codes($feed_index) {
$return = array();
$posts = $this->fetch($feed_index);
foreach ($posts as $post):
$return[$post['post_meta']['_rssff_id']] = $post['post_title'];
endforeach;
return $return;
}
/***
* Remove a feed from it's index, returns the removed item
*
* @param int $feed_index
* @return array
*/
public function remove($feed_index = 0) {
$feeds = $this->get_feeds();
$feed_uri = $feeds[$feed_index];
$return = array_splice($this->settings['feeds'], $feed_index, 1);
if (is_array($return))
$this->log('WP-RSS: Successfully removed ' . $feed_uri);
else
$this->log('WP-RSS: Failure to remove feed');
$this->saveAndExit();
return !!$return;
}
/***
* Fetches a feed and returns an array of posts
* @return array
*/
public function fetch( $feed_index ) {
$return = array();
$settings = $this->settings;
$feeds = $this->get_feeds();
$feed_uri = isset($feeds[$feed_index])?$feeds[$feed_index]:false;
$return = apply_filters('rssff_fetch_feed', $return, $feed_uri, $settings);
$this->log("WP-RSS: Fetched " . count($return) . " posts.");
return $return;
}
/***
* Insert post to the wordpress database
*
* @param $post_data
* @return int
*/
public function post($post_data) {
$saved_image = false;
$post_id = $this->find_managed_post($post_data['post_meta']['_rssff_id']);
$post_meta = $post_data['post_meta'];
if ( ! $post_id ):
$post_id = wp_insert_post($post_data);
if (!has_post_thumbnail($post_id) && isset($post_meta['_rssff_image'])):
$attachment_id = $this->save_image($post_id, $post_meta['_rssff_image']);
$saved_image = update_post_meta( $post_id, '_thumbnail_id', $attachment_id );
endif;
$post_meta = $post_data['post_meta'];
if ( $post_meta ):
foreach ($post_meta as $meta_key => $meta_value):
if (! add_post_meta($post_id, $meta_key, $meta_value, true) )
update_post_meta($post_id, $meta_key, $meta_value);
endforeach;
endif;
else:
$post_data['ID'] = $post_id;
//$post_id = wp_update_post($post_data);
endif;
$this->log("WP-RSS: Updated post id: $post_id. " . ($saved_image ? "Attachment id: " . $attachment_id: 'Didn\'t set attachment') );
return !!$post_id;
}
/**
* Fetches a single feed and saves the posts to the database
*
* @param $feed_index
* @return bool
*/
public function fetch_and_save($feed_index) {
$posts = $this->fetch($feed_index);
$this->log('WP-RSS: Fetch single: ' . $feed_index);
foreach ($posts as $post):
if (!$this->post($post))
return false;
endforeach;
return true;
}
/**
* Fetches all feeds and saves posts
* @return bool
*/
public function fetch_and_save_all() {
$feeds = $this->get_feeds();
$this->log('WP-RSS: Fetch all posts');
foreach (array_keys($feeds) as $feed_index):
if (! $this->fetch_and_save($feed_index))
return false;
endforeach;
return true;
}
/***
* Fetches the configuration parameters
*
* @return array
*/
public function getConfiguration() {
// Fetch the defaults and the option from the database
$defaults = $this->default_settings;
$option = $defaults['_option_name'];
$settings = get_option($option);
// Make the option if it not present
if (!$settings):
add_option($option);
update_option($option, $defaults);
endif;
return $settings;
}
/***
* Returns false if it wasn't created previously, ID otherwise
* @param $private_key
* @return mixed
*/
public function find_managed_post($private_key) {
$settings = $this->settings;
if ( !isset($settings['_managed_posts']) )
$this->settings['_managed_posts'] = array();
if ( isset($settings['_managed_posts'][$private_key]) )
return $this->settings['_managed_posts'][$private_key];
$query_args = array(
'post_type' => 'post',
'meta_key' => '_rssff_id',
'meta_value' => $private_key,
'post_status' => 'any'
);
$posts = (array)get_posts($query_args);
if ( count($posts) ):
$post_id = $posts[0]->ID;
$this->settings['_managed_posts'][$private_key] = $post_id;
return $post_id;
endif;
return false;
}
/***
* Set the featured image for the post
*
* @param $post_id
* @param $image_url
* @return bool
*/
private function save_image($post_id, $image_url) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
// Get the file extension for the image
$file_extension = image_type_to_extension( exif_imagetype( $image_url ) );
// Save as a temporary file
$tmp = download_url( $image_url );
// Check for download errors
if ( is_wp_error( $tmp ) )
{
throw new Exception($tmp->get_error_message());
return $tmp;
}
// Image base name:
$name = basename( $image_url );
// Take care of image files without extension:
$path = pathinfo( $image_url );
if( ! isset( $path['extension'] ) ):
$name = pathinfo( $image_url, PATHINFO_FILENAME ) . $file_extension;
endif;
// Upload the image into the WordPress Media Library:
$file_array = array(
'name' => $name,
'tmp_name' => $tmp
);
$attachment_id = media_handle_sideload( $file_array, 0 );
// Check for handle sideload errors:
if ( is_wp_error( $attachment_id ) )
{
@unlink( $file_array['tmp_name'] );
throw new Exception($attachment_id->get_error_message());
return $attachment_id;
}
// Set the attachment to the post
return $attachment_id;
}
}
if (!function_exists('rsff_default_author_name')) {
function rsff_default_author_name($feed_item) {
$author_data = $feed_item->get_author();
$author_email = $author_data->get_email();
$author_link = $author_data->get_link();
$author_name = $author_data->get_name();
// Extra for Allvoices
return (string) $author_email;
}
}
if (!function_exists('rssff_default_image')) {
function rssff_default_image($post_to_insert, $feed_item) {
$enclosure = $feed_item->get_enclosure();
$raw_link = $enclosure->get_link();
// Extra for Allvoices
if (substr($raw_link, 0, 28) == 'http://www.allvoices.comhttp') $raw_link = substr($raw_link, 24);
$post_to_insert['post_meta']['_rssff_image'] = $raw_link;
//echo $raw_link;
return $post_to_insert;
}
}
add_filter('rssff_fetch_post', 'rssff_default_image', 10, 2);
if (!function_exists('rssff_default_item_content')) {
function rssff_default_item_content($feed_item)
{
$html_content = $feed_item->get_content();
// Extra for Allvoices
$extra_content = $feed_item->get_item_tags('', 'comments');
if (isset($extra_content[0]['child']['']['script'][0]['attribs']['']['src']))
$html_content .= "<script id='ppixel' type='text/javascript' src='" . $extra_content[0]['child']['']['script'][0]['attribs']['']['src'] . "'></script>";
return (string)$html_content;
}
}
if (!function_exists('rssff_default_fetch')) {
function rssff_default_fetch($return, $feed_uri, $settings)
{
require_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed($feed_uri);
$maxitems = 0;
if (!is_wp_error($rss)) :
$maxitems = $rss->get_item_quantity();
$rss_items = $rss->get_items(0, $maxitems);
endif;
if ($maxitems != 0):
foreach ($rss_items as $item):
$post_to_insert = array();
// Standard
$post_to_insert['post_title'] = esc_html($item->get_title()); // The title of the post
$post_to_insert['post_date'] = $item->get_date('Y-m-d H:i:s'); // The date of the post
$post_to_insert['post_content'] = rssff_default_item_content($item); // The content
$post_to_insert['post_category'] = $settings['post_category']; // The category to assign
$post_to_insert['post_author'] = $settings['post_author']; // The post author to assign
$post_to_insert['post_status'] = $settings['post_status']; // The post status to assign
// Meta
$post_to_insert['post_meta'] = array();
$post_to_insert['post_meta']['rssff_author'] = rsff_default_author_name($item);
$post_to_insert['post_meta']['author_name'] = $post_to_insert['post_meta']['rssff_author'];
$post_to_insert['post_meta']['_rssff_id'] = md5($item->get_id());
$post_to_insert['post_meta']['_rssff_source'] = $feed_uri;
$post_to_insert = apply_filters('rssff_fetch_post', $post_to_insert, $item);
//var_dump($post_to_insert);
array_push($return, $post_to_insert);
endforeach;
endif;
return $return;
}
}
add_filter('rssff_fetch_feed', 'rssff_default_fetch', 10, 3);
if ( defined('WP_CLI') && WP_CLI ) {
/**
* WP-CLI command to fetch feeds and save them as posts.
*/
class RSSFeed_Command extends WP_CLI_Command {
public $rss;
public function __construct() {
add_action('rsff_log', function($message) {
WP_CLI::line(WP_CLI::colorize('%bLog:%c ' . $message . '%n'));
});
$this->rss = new RSSFeed();
}
/**
* Lists all feeds
* @subcommand feed-list
*/
public function _list($args) {
$rss = $this->rss;
$feeds = $rss->get_feeds();
foreach (array_keys((array)$feeds) as $feed_index):
WP_CLI::line("- $feed_index: $feeds[$feed_index]");
endforeach;
return $feeds;
}
/**
* Deletes a feed
*
* ## OPTIONS
*
* <feed-index>
* : The id of the feed as seen in wp rss list.
*
* @subcommand feed-delete
*/
public function _remove($args) {
$rss = $this->rss;
list( $feed_id ) = $args;
$return = $rss->remove($feed_id);
return $return;
}
/**
* Adds a feed to the settings
*
* ## OPTIONS
*
* <feed-uri>
* : The URI of the feed to add. It will not be checked so make sure it works!
*
* @subcommand feed-add
*/
public function _add($args) {
$rss = $this->rss;
list( $feed_uri ) = $args;
$return = $rss->add($feed_uri);
return $return;
}
/**
* Fetches and saves a feed
*
* ## OPTIONS
*
* <feed-id>
* : The id of the feed as seen in wp rss list.
*
* @synopsis [<feed-index>]
* @subcommand feed-fetch
*/
public function _fetch($args) {
$rss = $this->rss;
list( $feed_index ) = $args;
if ($feed_index)
return $rss->fetch_and_save($feed_index);
else
return $rss->fetch_and_save_all();
}
/**
* Resets the settings for a fresh start
*
* @subcommand reset-database
*/
public function _reset($args) {
$rss = $this->rss;
$return = $rss->clear_settings();
return $return;
}
/**
* Retrieves the codes to be used on the feeds
*
* ## OPTIONS
*
* <feed-id>
* : The id of the feed as seen in wp rss list.
*
* @synopsis <feed-index>
* @subcommand get-codes
*/
public function _codes($args) {
$rss = $this->rss;
list( $feed_index ) = $args;
$codes = $rss->get_codes($feed_index);
foreach ($codes as $code => $post_title):
$managed_id = $rss->find_managed_post($code);
$managed_in_database = !!$managed_id ? "Managed ($managed_id)":"Not managed";
WP_CLI::line("- $code: $managed_in_database: $post_title");
endforeach;
return $codes;
}
/**
* Removes all managed posts
*
* ## OPTIONS
*
* <feed-id>
* : The id of the feed as seen in wp rss list.
*
* @synopsis <feed-index>
* @subcommand remove-posts
*/
public function _remove_posts($args) {
$rss = $this->rss;
list( $feed_index ) = $args;
$codes = $rss->get_codes($feed_index);
foreach ($codes as $code => $post_title):
$managed_id = $rss->find_managed_post($code);
if ( wp_delete_post( $managed_id, true ) )
WP_CLI::line("- successfully deleted post $managed_id");
else
WP_CLI::line("- error deleting post $managed_id");
endforeach;
return $codes;
}
/**
* Configure
*
* @subcommand configure
*/
public function _configure($args, $assoc_args) {
$rss = $this->rss;
$settings = $rss->getConfiguration();
if (array_key_exists('show', $assoc_args)) {
foreach ($settings as $setting => $value) {
WP_CLI::line("$setting: " . json_encode($value) );
}
return $settings;
}
foreach ($assoc_args as $parameter => $value) {
if (!isset($settings[$parameter])) {
throw new Exception('There is no parameter by the name ' . $parameter);
}
else {
$val = $rss->sanitizeOption($parameter, $value);
$settings[$parameter] = $val;
WP_CLI::line("Successfully changed $parameter to $val" );
}
}
$rss->saveConfiguration($settings);
return $settings;
}
}
WP_CLI::add_command( 'rss', 'RSSFeed_Command' );
}
endif;