-
Notifications
You must be signed in to change notification settings - Fork 63
/
feedwordpress.php
2491 lines (2084 loc) · 84.9 KB
/
feedwordpress.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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: FeedWordPress
Plugin URI: http://feedwordpress.radgeek.com/
Description: simple and flexible Atom/RSS syndication for WordPress
Version: 2024.0511
Author: C. Johnson
Author URI: https://feedwordpress.radgeek.com/contact/
License: GPL
*/
/**
* @package FeedWordPress
* @version 2024.0511
*/
# This plugin uses code derived from:
# - wp-rss-aggregate.php by Kellan Elliot-McCrea <kellan@protest.net>
# - SimplePie feed parser by Ryan Parman, Geoffrey Sneddon, Ryan McCue, et al.
# - MagpieRSS feed parser by Kellan Elliot-McCrea <kellan@protest.net>
# - Ultra-Liberal Feed Finder by Mark Pilgrim <mark@diveintomark.org>
# - WordPress Blog Tool and Publishing Platform <http://wordpress.org/>
# - Github contributors @Flynsarmy, @BandonRandon, @david-robinson-practiceweb,
# @daidais, @thegreatmichael, @stedaniels, @alexiskulash, @quassy, @zoul0813,
# @timmmmyboy, @vobornik, @inanimatt, @tristanleboss, @martinburchell,
# @bigalownz, @oppiansteve, and @GwynethLlewelyn
# according to the terms of the GNU General Public License.
####################################################################################
## CONSTANTS & DEFAULTS ############################################################
####################################################################################
define ('FEEDWORDPRESS_VERSION', '2024.0511');
define ('FEEDWORDPRESS_AUTHOR_CONTACT', 'https://feedwordpress.radgeek.com/contact' );
if ( ! defined( 'FEEDWORDPRESS_BLEG' ) ) :
define ( 'FEEDWORDPRESS_BLEG', true );
endif;
define( 'FEEDWORDPRESS_BLEG_BTC_pre_2020', '15EsQ9QMZtLytsaVYZUaUCmrkSMaxZBTso' );
define( 'FEEDWORDPRESS_BLEG_BTC_2020', '1NB1ebYVb68Har4WijmE8gKnZ47NptCqtB' ); // 2020.0201
define( 'FEEDWORDPRESS_BLEG_BTC', '1HCDdeGcR66EPxkPT2rbdTd1ezh27pmjPR' ); // 2021.0713
define( 'FEEDWORDPRESS_BLEG_PAYPAL', '22PAJZZCK5Z3W' );
// Defaults
define( 'DEFAULT_SYNDICATION_CATEGORY', 'Contributors' );
define( 'DEFAULT_UPDATE_PERIOD', 60 ); // value in minutes
define( 'FEEDWORDPRESS_DEFAULT_CHECKIN_INTERVAL', DEFAULT_UPDATE_PERIOD / 10 );
// Dependencies: modules packaged with FeedWordPress plugin.
/** @var string Path to parent directory */
$dir = dirname( __FILE__ );
require_once "{$dir}/externals/myphp/myphp.class.php";
/** @var bool|string Set to either true or 'yes' if debugging is set. */
$feedwordpress_debug = FeedWordPress::param( 'feedwordpress_debug', get_option( 'feedwordpress_debug' ) );
if ( is_string( $feedwordpress_debug ) ) :
$feedwordpress_debug = ( $feedwordpress_debug == 'yes' );
endif;
define ( 'FEEDWORDPRESS_DEBUG', $feedwordpress_debug );
$feedwordpress_compatibility = true;
define ( 'FEEDWORDPRESS_COMPATIBILITY', $feedwordpress_compatibility );
define ( 'FEEDWORDPRESS_CAT_SEPARATOR_PATTERN', '/[:\n]/' );
define ( 'FEEDWORDPRESS_CAT_SEPARATOR', "\n" );
// define ('FEEDVALIDATOR_URI', 'http://feedvalidator.org/check.cgi'); // Link dead (gwyneth 20210617)
define ( 'FEEDVALIDATOR_URI', 'https://validator.w3.org/feed/check.cgi' ); // Falling back to the W3C validator link
define ( 'FEEDWORDPRESS_FRESHNESS_INTERVAL', 10 * 60 ); // Every ten minutes
define( 'FEEDWORDPRESS_BOILERPLATE_DEFAULT_HOOK_ORDER', 11000 ); // at the tail end of the filtering process
if ( FEEDWORDPRESS_DEBUG ) :
// Help us to pick out errors, if any.
ini_set( 'error_reporting', E_ALL & ~E_NOTICE );
ini_set( 'display_errors', true );
// When testing we don't want cache issues to interfere. But this is
// a VERY BAD SETTING for a production server. Webmasters will eat your
// face for breakfast if you use it, and the baby Jesus will cry. So
// make sure FEEDWORDPRESS_DEBUG is FALSE for any site that will be
// used for more than testing purposes!
define( 'FEEDWORDPRESS_CACHE_AGE', 1 );
define( 'FEEDWORDPRESS_CACHE_LIFETIME', 1 );
define( 'FEEDWORDPRESS_FETCH_TIMEOUT_DEFAULT', 60 );
else :
// Hold onto data all day for conditional GET purposes,
// but consider it stale after 1 min (requiring a conditional GET)
define( 'FEEDWORDPRESS_CACHE_LIFETIME', 24 * 60 * 60 ); // aka one day.
define( 'FEEDWORDPRESS_CACHE_AGE', 1 * 60 );
define( 'FEEDWORDPRESS_FETCH_TIMEOUT_DEFAULT', 20 );
endif;
####################################################################################
## CORE DEPENDENCIES & PLUGIN MODULES ##############################################
####################################################################################
/** @var array<string> Dependencies: modules packaged with WordPress core */
$wpCoreDependencies = array(
"class:SimplePie" => "class-simplepie",
"class:WP_SimplePie_File" => "class-wp-simplepie-file",
"class:WP_SimplePie_Sanitize_KSES" => "class-wp-simplepie-sanitize-kses",
"function:wp_insert_user" => "registration",
);
// Ensure that we have SimplePie loaded up and ready to go
// along with the WordPress auxiliary classes.
$unmetCoreDependencies = array();
foreach ( $wpCoreDependencies as $unit => $fileSlug ) :
list( $unitType, $unitName ) = explode( ":", $unit, 2 );
$dependencyMet = ( ('class' == $unitType ) ? class_exists( $unitName ) : function_exists( $unitName ) );
if ( ! $dependencyMet ) :
$phpFileName = ABSPATH . WPINC . "/{$fileSlug}.php";
if ( file_exists( $phpFileName ) ) :
require_once $phpFileName;
else :
$unmetCoreDependencies[] = $unitName;
endif;
endif;
endforeach;
// Fallback garbage-pail module used in WP < 4.7 which may meet our dependencies
if ( count( $unmetCoreDependencies ) > 0 ) :
require_once ABSPATH . WPINC . "/class-feed.php";
endif;
// Dependencies: modules packaged with FeedWordPress plugin
$dir = dirname( __FILE__ );
require_once "{$dir}/feedwordpressadminpage.class.php";
require_once "{$dir}/feedwordpresssettingsui.class.php";
require_once "{$dir}/feedwordpressdiagnostic.class.php";
require_once "{$dir}/admin-ui.php";
require_once "{$dir}/template-functions.php";
require_once "{$dir}/feedwordpresssyndicationpage.class.php";
require_once "{$dir}/compatability.php"; // Legacy API
require_once "{$dir}/syndicatedpost.class.php";
require_once "{$dir}/syndicatedlink.class.php";
require_once "{$dir}/feedwordpresshtml.class.php";
require_once "{$dir}/inspectpostmeta.class.php";
require_once "{$dir}/syndicationdataqueries.class.php";
require_once "{$dir}/extend/SimplePie/feedwordpie.class.php";
require_once "{$dir}/extend/SimplePie/feedwordpie_cache.class.php";
require_once "{$dir}/extend/SimplePie/feedwordpie_item.class.php";
require_once "{$dir}/extend/SimplePie/feedwordpie_file.class.php";
require_once "{$dir}/extend/SimplePie/feedwordpie_parser.class.php";
require_once "{$dir}/extend/SimplePie/feedwordpie_content_type_sniffer.class.php";
require_once "{$dir}/feedwordpressrpc.class.php";
require_once "{$dir}/feedwordpresshttpauthenticator.class.php";
require_once "{$dir}/feedwordpresslocalpost.class.php";
####################################################################################
## GLOBAL PARAMETERS ###############################################################
####################################################################################
// Get the path relative to the plugins directory in which FWP is stored
preg_match (
'|'.preg_quote( WP_PLUGIN_DIR ) . '/(.+)$|',
dirname( __FILE__ ),
$ref
);
if ( isset( $ref[1] ) ) :
$fwp_path = $ref[1];
else : // Something went wrong. Let's just guess.
$fwp_path = 'feedwordpress';
endif;
####################################################################################
## FEEDWORDPRESS: INITIALIZE OBJECT AND FILTERS ####################################
####################################################################################
$feedwordpress = new FeedWordPress;
if ( ! $feedwordpress->needs_upgrade() ) : // only work if the conditions are safe!
$feedwordpress->add_filters();
# Inbound XML-RPC update methods
$feedwordpressRPC = new FeedWordPressRPC;
# Outbound XML-RPC ping reform
remove_action( 'publish_post', 'generic_ping' ); // WP 1.5.x
remove_action( 'do_pings', 'do_all_pings', 10, 1 ); // WP 2.1, 2.2
remove_action( 'publish_post', '_publish_post_hook', 5, 1 ); // WP 2.3
add_action( 'publish_post', 'fwp_publish_post_hook', 5, 1 );
add_action( 'do_pings', 'fwp_do_pings', 10, 1 );
add_action( 'feedwordpress_update', 'fwp_hold_pings' );
add_action( 'feedwordpress_update_complete', 'fwp_release_pings' );
else :
# Hook in the menus, which will just point to the upgrade interface
add_action( 'admin_menu', array( $feedwordpress, 'add_pages' ) );
endif; // if ( !FeedWordPress::needs_upgrade())
register_deactivation_hook( __FILE__, 'feedwordpress_deactivate' );
/**
* Hook to deactivate FeedWordPress.
*
* @return int|false @see wp_clear_scheduled_hook()
*/
function feedwordpress_deactivate() {
wp_clear_scheduled_hook( 'fwp_scheduled_update_checkin' );
} /* feedwordpress_deactivate () */
################################################################################
## LOGGING FUNCTIONS: log status updates to error_log if you want it ###########
################################################################################
/**
* Divides bytes into units of higher magnitude (e.g KB, MB, etc).
*
* @param int|string $quantity Quantity in bytes to be displayed. Can be a string that only includes numeric digits.
*
* @return string Formatted string with quantity and unit.
*
* @deprecated use the WordPress built-in `size_format()` function instead! (gwyneth 20230918)
*/
function debug_out_human_readable_bytes( $quantity ) {
if ( ! is_numeric( $quantity ) ) :
return __( "(wrong quantity)" );
endif;
$quantity = intval( $quantity );
$magnitude = 'B';
/** @var array Two-letter abbreviations of the units in increasing magnitude. */
$orders = array( 'KB', 'MB', 'GB', 'TB' );
while ( ( $quantity > ( 1024 * 100 ) ) and ( count( $orders ) > 0 ) ) :
$quantity = floor( $quantity / 1024 );
$magnitude = array_shift( $orders );
endwhile;
return "{$quantity} {$magnitude}";
}
function debug_out_feedwordpress_footer() {
if ( FeedWordPressDiagnostic::is_on('memory_usage') ) :
if ( function_exists('memory_get_usage') ) :
FeedWordPress::diagnostic( 'memory_usage', "Memory: Current usage: " . size_format( memory_get_usage() ) );
endif;
if ( function_exists('memory_get_peak_usage') ) :
FeedWordPress::diagnostic('memory_usage', "Memory: Peak usage: " . size_format( memory_get_peak_usage() ) );
endif;
endif;
} /* debug_out_feedwordpress_footer() */
################################################################################
## FILTERS: syndication-aware handling of post data for templates and feeds ####
################################################################################
$feedwordpress_the_syndicated_content = null;
$feedwordpress_the_original_permalink = null;
function feedwordpress_preserve_syndicated_content ($text) {
global $feedwordpress_the_syndicated_content;
$p = new FeedWordPressLocalPost;
if ( ! $p->is_exposed_to_formatting_filters()) :
$feedwordpress_the_syndicated_content = $text;
else :
$feedwordpress_the_syndicated_content = null;
endif;
return $text;
}
function feedwordpress_restore_syndicated_content ($text) {
global $feedwordpress_the_syndicated_content;
if ( !is_null($feedwordpress_the_syndicated_content) ) :
$text = $feedwordpress_the_syndicated_content;
endif;
return $text;
}
function feedwordpress_item_feed_data () {
// In a post context....
if (is_syndicated()) :
?>
<source>
<title><?php print esc_html( get_syndication_source() ); ?></title>
<link rel="alternate" type="text/html" href="<?php print esc_url( get_syndication_source_link() ); ?>" />
<link rel="self" href="<?php print esc_url( get_syndication_feed() ); ?>" />
<?php
$id = get_syndication_feed_guid();
if ( strlen( $id ) > 0 ) :
?>
<id><?php print esc_xml( $id ); ?></id>
<?php
endif;
$updated = get_feed_meta('feed/updated');
if ( strlen( $updated ) > 0 ) : ?>
<updated><?php print esc_xml( $updated ); ?></updated>
<?php
endif;
?>
</source>
<?php
endif;
}
/**
* syndication_permalink: Allow WordPress to use the original remote URL of
* syndicated posts as their permalink. Can be turned on or off by by setting in
* Syndication => Posts & Links. Saves the old internal permalink in a global
* variable for later use.
*
* @param string $permalink The internal permalink
* @param mixed|null $post Post object
* @param bool $leavename Unused
* @param bool $sample Unused
* @return string The new permalink. Same as the old if the post is not
* syndicated, or if FWP is set to use internal permalinks, or if the post
* was syndicated, but didn't have a proper permalink recorded.
*
* @uses SyndicatedLink::setting()
* @uses get_syndication_permalink()
* @uses get_syndication_feed_object()
* @uses url_to_postid()
* @global $id
* @global $feedwordpress_the_original_permalink
*/
function syndication_permalink($permalink = '', $post = null, $leavename = false, $sample = false ) {
global $id;
global $feedwordpress_the_original_permalink;
// Save the local permalink in case we need to retrieve it later.
$feedwordpress_the_original_permalink = $permalink;
if (is_object($post) and isset($post->ID) and !empty($post->ID)) :
// Use the post ID we've been provided with.
$postId = $post->ID;
elseif (is_string($permalink) and strlen($permalink) > 0) :
// Map this permalink to a post ID so we can get the correct
// permalink even outside of the Post Loop. Props Björn.
$postId = url_to_postid($permalink);
else :
// If the permalink string is empty but Post Loop context
// provides an id.
$postId = $id;
endif;
$munge = false;
$link = get_syndication_feed_object($postId);
if (is_object($link)) :
$munge = ($link->setting('munge permalink', 'munge_permalink', 'yes') != 'no');
endif;
if ($munge):
$uri = get_syndication_permalink($postId);
$permalink = ((strlen($uri) > 0) ? $uri : $permalink);
endif;
return $permalink;
} /* function syndication_permalink () */
/**
* syndication_permalink_escaped: Escape XML special characters in syndicated
* permalinks when used in feed contexts and HTML display contexts.
*
* @param string $permalink
* @return string
*
* @uses is_syndicated()
* @uses FeedWordPress::munge_permalinks()
*
*/
function syndication_permalink_escaped ($permalink) {
/* FIXME: This should review link settings not just global settings */
if (is_syndicated() and FeedWordPress::munge_permalinks()) :
// This is a foreign link; WordPress can't vouch for its not
// having any entities that need to be &-escaped. So we'll do
// it here.
$permalink = esc_html($permalink);
endif;
return $permalink;
} /* function syndication_permalink_escaped() */
/**
* syndication_comments_feed_link: Escape XML special characters in comments
* feed links
*
* @param string $link
* @return string
*
* @uses is_syndicated()
* @uses FeedWordPress::munge_permalinks()
*/
function syndication_comments_feed_link ($link) {
global $feedwordpress_the_original_permalink;
if (is_syndicated() and FeedWordPress::munge_permalinks()) :
// If the source post provided a comment feed URL using
// wfw:commentRss or atom:link/@rel="replies" we can make use of
// that value here.
$source = get_syndication_feed_object();
$replacement = null;
if (is_object($source) && $source->setting('munge comments feed links', 'munge_comments_feed_links', 'yes') != 'no') :
$commentFeeds = get_post_custom_values('wfw:commentRSS');
if (
is_array($commentFeeds)
and (count($commentFeeds) > 0)
and (strlen($commentFeeds[0]) > 0)
) :
$replacement = $commentFeeds[0];
// This is a foreign link; WordPress can't vouch for its not
// having any entities that need to be &-escaped. So we'll do it
// here.
$replacement = esc_html($replacement);
endif;
endif;
if (is_null($replacement)) :
// Q: How can we get the proper feed format, since the
// format is, stupidly, not passed to the filter?
// A: Kludge kludge kludge kludge!
$fancy_permalinks = ('' != get_option('permalink_structure'));
if ($fancy_permalinks) :
preg_match('|/feed(/([^/]+))?/?$|', $link, $ref);
$format = (isset($ref[2]) ? $ref[2] : '');
if (strlen($format) == 0) : $format = get_default_feed(); endif;
$replacement = trailingslashit($feedwordpress_the_original_permalink) . 'feed';
if ($format != get_default_feed()) :
$replacement .= '/'.$format;
endif;
$replacement = user_trailingslashit($replacement, 'single_feed');
else :
// No fancy permalinks = no problem
// WordPress doesn't call get_permalink() to
// generate the comment feed URL, so the
// comments feed link is never munged by FWP.
endif;
endif;
if ( !is_null($replacement)) : $link = $replacement; endif;
endif;
return $link;
} /* function syndication_comments_feed_link() */
require_once("{$dir}/feedwordpress.pings.functions.php");
require_once("{$dir}/feedwordpress.wp-admin.post-edit.functions.php");
################################################################################
## class FeedWordPressBoilerplateReformatter ###################################
################################################################################
require_once("{$dir}/feedwordpressboilerplatereformatter.class.php");
require_once("{$dir}/feedwordpressboilerplatereformatter.shortcode.functions.php");
################################################################################
## class FeedWordPress #########################################################
################################################################################
// class FeedWordPress: handles feed updates and plugs in to the XML-RPC interface
class FeedWordPress {
var $strip_attrs = array (
array('[a-z]+', 'style'),
array('[a-z]+', 'target'),
);
var $uri_attrs = array (
array('a', 'href'),
array('applet', 'codebase'),
array('area', 'href'),
array('blockquote', 'cite'),
array('body', 'background'),
array('del', 'cite'),
array('form', 'action'),
array('frame', 'longdesc'),
array('frame', 'src'),
array('iframe', 'longdesc'),
array('iframe', 'src'),
array('head', 'profile'),
array('img', 'longdesc'),
array('img', 'src'),
array('img', 'usemap'),
array('input', 'src'),
array('input', 'usemap'),
array('ins', 'cite'),
array('link', 'href'),
array('object', 'classid'),
array('object', 'codebase'),
array('object', 'data'),
array('object', 'usemap'),
array('q', 'cite'),
array('script', 'src')
);
var $feeds = null;
var $feedurls = null;
var $httpauth = null;
/**
* FeedWordPress::__construct (): Construct FeedWordPress singleton object
* and retrieves a list of feeds for later reference.
*
* @uses FeedWordPressHTTPAuthenticator
*/
public function __construct () {
$this->feeds = array ();
$this->feedurls = array();
$links = FeedWordPress::syndicated_links();
if ($links): foreach ($links as $link):
$id = intval($link->link_id);
$url = $link->link_rss;
// Store for later reference.
$this->feeds[$id] = $id;
if (strlen($url) > 0) :
$this->feedurls[$url] = $id;
endif;
endforeach; endif;
// System-related event hooks
add_filter('cron_schedules', array($this, 'cron_schedules'), 10, 1);
// FeedWordPress-related event hooks
add_filter('feedwordpress_update_complete', array($this, 'process_retirements'), 1000, 1);
$this->httpauth = new FeedWordPressHTTPAuthenticator;
} /* FeedWordPress::__construct () */
/**
* FeedWordPress::add_filters() connects FeedWordPress to WordPress lifecycle
* events by setting up action and filter hooks.
*
* @uses get_option()
* @uses add_filter()
* @uses add_action()
* @uses remove_filter()
*/
public function add_filters () {
# Syndicated items are generally received in output-ready (X)HTML and
# should not be folded, crumpled, mutilated, or spindled by WordPress
# formatting filters. But we don't want to interfere with filters for
# any locally-authored posts, either.
#
# What WordPress should really have is a way for upstream filters to
# stop downstream filters from running at all. Since it doesn't, and
# since a downstream filter can't access the original copy of the text
# that is being filtered, what we will do here is (1) save a copy of the
# original text upstream, before any other filters run, and then (2)
# retrieve that copy downstream, after all the other filters run, *if*
# this is a syndicated post
add_filter('the_content', 'feedwordpress_preserve_syndicated_content', -10000);
add_filter('the_content', 'feedwordpress_restore_syndicated_content', 10000);
add_action('atom_entry', 'feedwordpress_item_feed_data');
# Filter in original permalinks if the user wants that
add_filter('post_link', 'syndication_permalink', /*priority=*/ 1, /*arguments=*/ 3);
add_filter('post_type_link', 'syndication_permalink', /*priority=*/ 1, /*arguments=*/ 4);
# When foreign URLs are used for permalinks in feeds or display
# contexts, they need to be escaped properly.
add_filter('the_permalink', 'syndication_permalink_escaped');
add_filter('the_permalink_rss', 'syndication_permalink_escaped');
add_filter('post_comments_feed_link', 'syndication_comments_feed_link');
# WTF? By default, wp_insert_link runs incoming link_url and link_rss
# URIs through default filters that include `wp_kses()`. But `wp_kses()`
# just happens to escape any occurrence of & to & -- which just
# happens to fuck up any URI with a & to separate GET parameters.
remove_filter('pre_link_rss', 'wp_filter_kses');
remove_filter('pre_link_url', 'wp_filter_kses');
# Boilerplate functionality: hook in to title, excerpt, and content to add boilerplate text
$hookOrder = get_option('feedwordpress_boilerplate_hook_order', FEEDWORDPRESS_BOILERPLATE_DEFAULT_HOOK_ORDER);
add_filter(
/*hook=*/ 'the_title',
/*function=*/ 'add_boilerplate_title',
/*priority=*/ $hookOrder,
/*arguments=*/ 2
);
add_filter(
/*hook=*/ 'get_the_excerpt',
/*function=*/ 'add_boilerplate_excerpt',
/*priority=*/ $hookOrder,
/*arguments=*/ 1
);
add_filter(
/*hook=*/ 'the_content',
/*function=*/ 'add_boilerplate_content',
/*priority=*/ $hookOrder,
/*arguments=*/ 1
);
add_filter(
/*hook=*/ 'the_content_rss',
/*function=*/ 'add_boilerplate_content',
/*priority=*/ $hookOrder,
/*arguments=*/ 1
);
# Admin menu
add_action('admin_init', array($this, 'admin_init'));
add_action('admin_menu', array($this, 'add_pages'));
add_action('admin_notices', array($this, 'check_debug'));
add_action('admin_menu', 'feedwordpress_add_post_edit_controls');
add_action('save_post', 'feedwordpress_save_post_edit_controls');
add_action('admin_footer', array($this, 'admin_footer'));
add_action('syndicated_feed_error', array('FeedWordPressDiagnostic', 'feed_error'), 100, 3);
add_action('wp_footer', 'debug_out_feedwordpress_footer', -100);
add_action('admin_footer', 'debug_out_feedwordpress_footer', -100);
# Cron-less auto-update. Hooray!
$autoUpdateHook = $this->automatic_update_hook();
if ( !is_null($autoUpdateHook)) :
add_action($autoUpdateHook, array($this, 'auto_update'));
endif;
add_action('init', array($this, 'init'));
add_action('wp_loaded', array($this, 'wp_loaded'));
add_action('shutdown', array($this, 'email_diagnostic_log'));
add_action('shutdown', array($this, 'feedwordpress_cleanup'));
add_action('wp_dashboard_setup', array($this, 'dashboard_setup'));
# Default sanitizers
add_filter('syndicated_item_content', array('SyndicatedPost', 'resolve_relative_uris'), 0, 2);
add_filter('syndicated_item_content', array('SyndicatedPost', 'sanitize_content'), 0, 2);
add_action('plugins_loaded', array($this, 'admin_api'));
add_action('all_admin_notices', array($this, 'all_admin_notices'));
// Use the cache settings that we want, from a static method
add_filter('wp_feed_cache_transient_lifetime', array(get_class($this), 'cache_lifetime'));
} /* FeedWordPress::add_filters () */
################################################################################
## ADMIN MENU ADD-ONS: register Dashboard management pages #####################
################################################################################
/**
* FeedWordPress::add_pages(): set up WordPress admin interface pages thru
* hooking in Syndication menu and submenus
*
* @uses FeedWordPress::menu_cap()
* @uses FeedWordPress::path()
* @uses add_menu_page()
* @uses add_submenu_page()
* @uses do_action()
* @uses add_filter()
*/
public function add_pages()
{
$menu_cap = FeedWordPress::menu_cap();
$settings_cap = FeedWordPress::menu_cap( /*sub=*/ true );
$syndicationMenu = FeedWordPress::path( 'syndication.php' );
add_menu_page(
/* page_title */ 'Syndicated Sites',
/* menu_title */ 'Syndication',
/* capability */ $menu_cap,
/* menu_slug */ $syndicationMenu,
/* function */ NULL,
/* icon_url */ $this->plugin_dir_url( /* 'assets/images/feedwordpress-tiny.png' */ 'assets/images/icon.svg' )
/* position */
);
do_action( 'feedwordpress_admin_menu_pre_feeds', $menu_cap, $settings_cap );
add_submenu_page(
/* parent_slug */ $syndicationMenu,
/* page_title */ 'Syndicated Sites',
/* menu_title */ 'Syndicated Sites',
/* capability */ $settings_cap,
/* menu_slug */ $syndicationMenu,
/* function */
);
do_action('feedwordpress_admin_menu_pre_feeds', $menu_cap, $settings_cap);
add_submenu_page(
$syndicationMenu, 'Syndicated Feeds & Updates', 'Feeds & Updates',
$settings_cap, FeedWordPress::path('feeds-page.php')
);
do_action('feedwordpress_admin_menu_pre_posts', $menu_cap, $settings_cap);
add_submenu_page(
$syndicationMenu, 'Syndicated Posts & Links', 'Posts & Links',
$settings_cap, FeedWordPress::path('posts-page.php')
);
do_action('feedwordpress_admin_menu_pre_authors', $menu_cap, $settings_cap);
add_submenu_page(
$syndicationMenu, 'Syndicated Authors', 'Authors',
$settings_cap, FeedWordPress::path('authors-page.php')
);
do_action('feedwordpress_admin_menu_pre_categories', $menu_cap, $settings_cap);
add_submenu_page(
$syndicationMenu, 'Categories & Tags', 'Categories & Tags',
$settings_cap, FeedWordPress::path('categories-page.php')
);
do_action('feedwordpress_admin_menu_pre_performance', $menu_cap, $settings_cap);
add_submenu_page(
$syndicationMenu, 'FeedWordPress Performance', 'Performance',
$settings_cap, FeedWordPress::path('performance-page.php')
);
do_action('feedwordpress_admin_menu_pre_diagnostics', $menu_cap, $settings_cap);
add_submenu_page(
$syndicationMenu, 'FeedWordPress Diagnostics', 'Diagnostics',
$settings_cap, FeedWordPress::path('diagnostics-page.php')
);
add_filter('page_row_actions', array($this, 'row_actions'), 10, 2);
add_filter('post_row_actions', array($this, 'row_actions'), 10, 2);
} /* function FeedWordPress::add_pages () */
public function check_debug() {
// This is a horrible fucking kludge that I have to do because the
// admin notice code is triggered before the code that updates the
// setting.
$feedwordpress_debug = FeedWordPress::param( 'feedwordpress_debug', get_option( 'feedwordpress_debug' ) );
FeedWordPressSettingsUI::get_template_part( 'notice-debug-mode', $feedwordpress_debug, 'html' );
} /* function FeedWordPress::check_debug () */
/**
* FeedWordPress::subscribed (): Check whether a feed is currently in the
* subscription list for FeedWordPress.
*
* @param mixed $id Numeric ID of a WordPress link object or URL of a feed
* @return bool TRUE if currently subscribed; FALSE otherwise.
*/
public function subscribed ($id) {
return (isset($this->feedurls[$id]) or isset($this->feeds[$id]));
} /* FeedWordPress::subscribed () */
/**
* FeedWordPress::subscription (): Get the SyndicatedLink object for a
* given URL or numeric ID, if we have either an active subscription to
* it; or a de-activated subscription.
*
* @param mixed $which Numeric ID for a WordPress link object or string URL for a feed
* @return mixed SyndicatedLink object if subscription is found; null if not
*/
public function subscription ($which) {
$sub = null;
if (is_string($which) and isset($this->feedurls[$which])) :
$which = $this->feedurls[$which];
endif;
if (isset($this->feeds[$which])) :
$sub = $this->feeds[$which];
endif;
// If it's not in the in-memory cache already, try to load it from DB.
// This is necessary to fill requests for subscriptions that we don't
// cache in memory, e.g. for deactivated feeds.
if (is_null($sub)) :
$sub = get_bookmark($which);
endif;
// Load 'er up if you haven't already.
if ( !is_null($sub) and !($sub InstanceOf SyndicatedLink)) :
$link = new SyndicatedLink($sub);
$this->feeds[$which] = $link;
$sub = $link;
endif;
return $sub;
} /* FeedWordPress::subscription () */
/** function update (): polls for updates on one or more Contributor feeds
@desc
# Arguments:
- $uri (string): Either the URI of the feed to poll, the URI of the
(human-readable) website whose feed you want to poll, or null.
If $uri is null, then FeedWordPress will poll any feeds that are
ready for polling. It will not poll feeds that are marked as
"Invisible" Links (signifying that the subscription has been
de-activated), or feeds that are not yet stale according to their
TTL setting (which is either set in the feed, or else set
randomly within a window of 30 minutes - 2 hours).
# Returns:
- Normally returns an associative array, with 'new' => the number
of new posts added during the update, and 'updated' => the number
of old posts that were updated during the update. If both numbers
are zero, there was no change since the last poll on that URI.
- Returns null if URI it was passed was not a URI that this
installation of FeedWordPress syndicates.
# Effects:
- One or more feeds are polled for updates
- If the feed Link does not have a hardcoded name set, its Link
Name is synchronized with the feed's title element
- If the feed Link does not have a hardcoded URI set, its Link URI
is synchronized with the feed's human-readable link element
- If the feed Link does not have a hardcoded description set, its
Link Description is synchronized with the feed's description,
tagline, or subtitle element.
- The time of polling is recorded in the feed's settings, and the
TTL (time until the feed is next available for polling) is set
either from the feed (if it is supplied in the ttl or syndication
module elements) or else from a randomly-generated time window
(between 30 minutes and 2 hours).
- New posts from the polled feed are added to the WordPress store.
- Updates to existing posts since the last poll are mirrored in the
WordPress store.
@param string|null $uri Either the URI of the feed to poll, the URI of the (human-readable) website whose feed you want to poll, or null.
@param mixed|null $crash_ts Unknown purpose.
@return array|null Associative array, with 'new' => # of new posts added during update, and 'updated' => # of old posts that were updated. If both are zero, there was no change since çast update.
*/
public function update( $uri = null, $crash_ts = null ) {
if ( FeedWordPress::needs_upgrade() ) : // Will make duplicate posts if we don't hold off
return null;
endif;
if ( ! is_null( $uri ) and $uri != '*' ) :
$uri = trim( $uri );
else : // Update all
if ( $this->update_hooked ) :
$diag = $this->update_hooked;
else :
$diag = 'Initiating a MANUAL check-in on the update schedule at ' . date( 'r', time() );
endif;
$this->diagnostic( 'update_schedule:check', $diag );
update_option( 'feedwordpress_last_update_all', time() );
endif;
do_action( 'feedwordpress_update', $uri );
if ( is_null( $crash_ts ) ) :
$crash_ts = $this->crash_ts();
endif;
// Randomize order for load balancing purposes
$feed_set = array_keys( $this->feeds );
shuffle( $feed_set );
$updateWindow = (int) get_option( 'feedwordpress_update_window', DEFAULT_UPDATE_PERIOD ) * 60 /* sec/min */;
$interval = (int) get_option( 'feedwordpress_freshness', FEEDWORDPRESS_FRESHNESS_INTERVAL );
$portion = max(
(int) ceil( count( $feed_set ) / ( $updateWindow / $interval ) ),
10
);
$max_polls = apply_filters(
'feedwordpress_polls_per_update',
get_option(
'feedwordpress_polls_per_update', $portion
),
$uri
);
$feed_set = apply_filters( 'feedwordpress_update_feeds', $feed_set, $uri );
// Loop through and check for new posts
$delta = null;
$remaining = $max_polls;
foreach ( $feed_set as $feed_id) :
$feed = $this->subscription( $feed_id );
// Try to catch a very unusual condition where the $feed comes as NULL (gwyneth 20230919)
if ( ! empty( $feed ) ) :
$this->diagnostic( 'update_schedule', "Feed " . $feed_id . " returned an empty feed" );
endif;
// Has this process overstayed its welcome?
if (
// Over time limit?
( ! is_null( $crash_ts ) and ( time() > $crash_ts ) )
// Over feed count?
or ( 0 == $remaining )
) :
break;
endif;
$pinged_that = ( is_null( $uri ) or ( $uri == '*' ) or in_array( $uri, array( $feed->uri(), $feed->homepage() ) ) );
if ( ! is_null( $uri ) ) : // A site-specific ping always updates
$timely = true;
else :
$timely = $feed->stale();
endif;
// If at least one feed was hit for updating...
if ( $pinged_that and is_null( $delta ) ) :
// ... don't return error condition
$delta = array( 'new' => 0, 'updated' => 0, 'stored' => 0 );
endif;
if ( $pinged_that and $timely ) :
$remaining = $remaining - 1;
do_action( 'feedwordpress_check_feed', $feed->settings );
$start_ts = time();
$added = $feed->poll( $crash_ts );
do_action( 'feedwordpress_check_feed_complete', $feed->settings, $added, time() - $start_ts );
if ( is_array( $added ) ) : // Success
foreach ( $added as $key => $count ) :
$delta[ $key ] += $added[ $key ];
endforeach;
endif;
endif;
endforeach;
do_action( 'feedwordpress_update_complete', $delta );
return $delta;
} /* FeedWordPress::update () */
/**
* Checks if we're over the update time limit.
*
* @todo is returning null advisable? (gwyneth 20230916)
*
* @param int|null $default Default value, called when the corresponding FWP option is not set.
*
* @return int|null
*/
public function crash_ts( $default = null ) {
$crash_dt = (int) get_option( 'feedwordpress_update_time_limit', 0 );
if ( $crash_dt > 0 ) :
$crash_ts = time() + $crash_dt;
else :
$crash_ts = $default;
endif;
return $crash_ts;
} /* FeedWordPress::crash_ts () */
/**
* Checks if we have a secret key set in the options; if not, generate one.
*
* @return string Secret key, either from options or auto-generated.
*
* @todo Only 6 characters? That is rather easy to guess... Also, uniqid() can return 13 or 23
* characters, what's the point of using MD5 in this case? (gwyneth 20230916)
*/
public function secret_key() {
$secret = get_option( 'feedwordpress_secret_key', false );
if ( ! $secret) : // Generate random key.
$secret = substr( md5( uniqid( microtime() ) ), 0, 6 );
update_option( 'feedwordpress_secret_key', $secret );
endif;
return $secret;
} /* FeedWordPress::secret_key () */
/**
* Returns true if we have set a secret FWP key.
*
* @return bool True if we have a secret key, false otherwise
*
* @uses MyPHP::request()
*/
public function has_secret() {
return ( MyPHP::request( 'feedwordpress_key' ) == $this->secret_key() );
} /* FeedWordPress::has_secret () */
var $update_hooked = null;
/**
* Activates the hook for automatic plugin updates, if requested.
*
* @param Array $params Parameters to send to the hook.
*
* @return string Represents the hook's name. There is a slight chance that this is null, though. (gwyneth 20230916)
*
* @uses wp_parse_args()
* @uses MyPHP::request()
*/
public function automatic_update_hook( $params = array() ) {
$params = wp_parse_args( $params, array( // Defaults
'setting only' => false,
));
$hook = get_option( 'feedwordpress_automatic_updates', null );
$method = 'FeedWordPress option';