forked from radgeek/feedwordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedwordpress.php
2640 lines (2209 loc) · 87.4 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: 2018.0117
Author: C. Johnson
Author URI: https://feedwordpress.radgeek.com/contact/
License: GPL
*/
/**
* @package FeedWordPress
* @version 2018.0117
*/
# This plugin uses code derived from:
# - wp-rss-aggregate.php by Kellan Elliot-McCrea <[email protected]>
# - SimplePie feed parser by Ryan Parman, Geoffrey Sneddon, Ryan McCue, et al.
# - MagpieRSS feed parser by Kellan Elliot-McCrea <[email protected]>
# - Ultra-Liberal Feed Finder by Mark Pilgrim <[email protected]>
# - WordPress Blog Tool and Publishing Platform <http://wordpress.org/>
# - Github contributors @Flynsarmy, @BandonRandon, @david-robinson-practiceweb,
# @daidais, @thegreatmichael, @stedaniels, @alexiskulash, @quassy, @zoul0813,
# @timmmmyboy, @vobornik, and @inanimatt
# according to the terms of the GNU General Public License.
####################################################################################
## CONSTANTS & DEFAULTS ############################################################
####################################################################################
define ('FEEDWORDPRESS_VERSION', '2018.0117');
define ('FEEDWORDPRESS_AUTHOR_CONTACT', 'http://feedwordpress.radgeek.com/contact');
if (!defined('FEEDWORDPRESS_BLEG')) :
define ('FEEDWORDPRESS_BLEG', true);
endif;
define('FEEDWORDPRESS_BLEG_BTC', '15EsQ9QMZtLytsaVYZUaUCmrkSMaxZBTso');
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);
if (isset($_REQUEST['feedwordpress_debug'])) :
$feedwordpress_debug = $_REQUEST['feedwordpress_debug'];
else :
$feedwordpress_debug = get_option('feedwordpress_debug');
if (is_string($feedwordpress_debug)) :
$feedwordpress_debug = ($feedwordpress_debug == 'yes');
endif;
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');
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);
define('FEEDWORDPRESS_CACHE_AGE', 1*60);
define('FEEDWORDPRESS_FETCH_TIMEOUT_DEFAULT', 20);
endif;
####################################################################################
## CORE DEPENDENCIES & PLUGIN MODULES ##############################################
####################################################################################
// Dependencies: modules packaged with WordPress core
$wpCoreDependencies = array(
"class:SimplePie" => "class-simplepie",
"class:WP_Feed_Cache" => "class-wp-feed-cache",
"class:WP_Feed_Cache_Transient" => "class-wp-feed-cache-transient",
"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;
// Dependences: modules packaged with FeedWordPress plugin
$dir = dirname(__FILE__);
require_once("${dir}/externals/myphp/myphp.class.php");
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}/feedwordpress-content-type-sniffer.class.php");
require_once("${dir}/inspectpostmeta.class.php");
require_once("${dir}/syndicationdataqueries.class.php");
require_once("${dir}/feedwordpie.class.php");
require_once("${dir}/feedwordpie_item.class.php");
require_once("${dir}/feedwordpress_file.class.php");
require_once("${dir}/feedwordpress_parser.class.php");
require_once("${dir}/feedwordpressrpc.class.php");
require_once("${dir}/feedwordpresshttpauthenticator.class.php");
require_once("${dir}/feedwordpresslocalpost.class.php");
####################################################################################
## GLOBAL PARAMETERS ###############################################################
####################################################################################
// $fwp_post used to be a global variable used to make it easier to cope
// with the frustrating sometime presence of "Magic Quotes" in earlier
// versions of PHP (<http://php.net/manual/en/security.magicquotes.php>).
// Magic quotes were DEPRECATED as of PHP 5.3.0, and REMOVED as of PHP
// 5.4.0, so for the time being $fwp_post just gets a copy of $_POST.
global $fwp_post;
if (is_array($_POST)) :
$fwp_post = $_POST;
else:
$fwp_post = null;
endif;
// 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!
# 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($feedwordpress, 'admin_init'));
add_action('admin_menu', 'fwp_add_pages');
add_action('admin_notices', 'fwp_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($feedwordpress, 'admin_footer'));
# 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');
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 = $feedwordpress->automatic_update_hook();
if (!is_null($autoUpdateHook)) :
add_action($autoUpdateHook, array($feedwordpress, 'auto_update'));
endif;
add_action('init', array($feedwordpress, 'init'));
add_action('wp_loaded', array($feedwordpress, 'wp_loaded'));
add_action('shutdown', array($feedwordpress, 'email_diagnostic_log'));
add_action('shutdown', array($feedwordpress, 'feedwordpress_cleanup'));
add_action('wp_dashboard_setup', array($feedwordpress, '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($feedwordpress, 'admin_api'));
add_action('all_admin_notices', array($feedwordpress, 'all_admin_notices'));
// Use our the cache settings that we want.
add_filter('wp_feed_cache_transient_lifetime', array('FeedWordPress', 'cache_lifetime'));
else :
# Hook in the menus, which will just point to the upgrade interface
add_action('admin_menu', 'fwp_add_pages');
endif; // if (!FeedWordPress::needs_upgrade())
register_deactivation_hook(__FILE__, 'feedwordpress_deactivate');
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 ###########
################################################################################
function debug_out_human_readable_bytes ($quantity) {
$quantity = (int) $quantity;
$magnitude = 'B';
$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: ".debug_out_human_readable_bytes(memory_get_usage()));
endif;
if (function_exists('memory_get_peak_usage')) :
FeedWordPress::diagnostic ('memory_usage', "Memory: Peak usage: ".debug_out_human_readable_bytes(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 htmlspecialchars(get_syndication_source()); ?></title>
<link rel="alternate" type="text/html" href="<?php print htmlspecialchars(get_syndication_source_link()); ?>" />
<link rel="self" href="<?php print htmlspecialchars(get_syndication_feed()); ?>" />
<?php
$id = get_syndication_feed_guid();
if (strlen($id) > 0) :
?>
<id><?php print htmlspecialchars($id); ?></id>
<?php
endif;
$updated = get_feed_meta('feed/updated');
if (strlen($updated) > 0) : ?>
<updated><?php print $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
* @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, $id;
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() */
################################################################################
## ADMIN MENU ADD-ONS: register Dashboard management pages #####################
################################################################################
function fwp_add_pages () {
global $feedwordpress;
$menu_cap = FeedWordPress::menu_cap();
$settings_cap = FeedWordPress::menu_cap(/*sub=*/ true);
$syndicationMenu = FeedWordPress::path('syndication.php');
add_menu_page(
'Syndicated Sites', 'Syndication',
$menu_cap,
$syndicationMenu,
NULL,
plugins_url( '/'.FeedWordPress::path('feedwordpress-tiny.png') )
);
do_action('feedwordpress_admin_menu_pre_feeds', $menu_cap, $settings_cap);
add_submenu_page(
$syndicationMenu, 'Syndicated Sites', 'Syndicated Sites',
$settings_cap, $syndicationMenu
);
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($feedwordpress, 'row_actions'), 10, 2);
add_filter('post_row_actions', array($feedwordpress, 'row_actions'), 10, 2);
} /* function fwp_add_pages () */
function fwp_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.
if (isset($_POST['feedwordpress_debug'])) :
$feedwordpress_debug = $_POST['feedwordpress_debug'];
else :
$feedwordpress_debug = get_option('feedwordpress_debug');
endif;
if ($feedwordpress_debug==='yes') :
?>
<div class="error">
<p><strong>FeedWordPress warning.</strong> Debugging mode is <strong>ON</strong>.
While it remains on, FeedWordPress displays many diagnostic error messages,
warnings, and notices that are ordinarily suppressed, and also turns off all
caching of feeds. Use with caution: this setting is absolutely inappropriate
for a production server.</p>
</div>
<?php
endif;
} /* function fwp_check_debug () */
################################################################################
## fwp_hold_pings() and fwp_release_pings(): Outbound XML-RPC ping reform ####
## ... 'coz it's rude to send 500 pings the first time your aggregator runs ####
################################################################################
$fwp_held_ping = NULL; // NULL: not holding pings yet
function fwp_hold_pings () {
global $fwp_held_ping;
if (is_null($fwp_held_ping)):
$fwp_held_ping = 0; // 0: ready to hold pings; none yet received
endif;
}
function fwp_release_pings () {
global $fwp_held_ping;
if ($fwp_held_ping):
if (function_exists('wp_schedule_single_event')) :
wp_schedule_single_event(time(), 'do_pings');
else :
generic_ping($fwp_held_ping);
endif;
endif;
$fwp_held_ping = NULL; // NULL: not holding pings anymore
}
function fwp_do_pings () {
if (!is_null($fwp_held_ping) and $post_id) : // Defer until we're done updating
$fwp_held_ping = $post_id;
elseif (function_exists('do_all_pings')) :
do_all_pings();
else :
generic_ping($fwp_held_ping);
endif;
}
function fwp_publish_post_hook ($post_id) {
global $fwp_held_ping;
if (!is_null($fwp_held_ping)) : // Syndicated post. Don't mark with _pingme
if ( defined('XMLRPC_REQUEST') )
do_action('xmlrpc_publish_post', $post_id);
if ( defined('APP_REQUEST') )
do_action('app_publish_post', $post_id);
if ( defined('WP_IMPORTING') )
return;
// Defer sending out pings until we finish updating
$fwp_held_ping = $post_id;
else :
if (function_exists('_publish_post_hook')) : // WordPress 2.3
_publish_post_hook($post_id);
endif;
endif;
}
function feedwordpress_add_post_edit_controls () {
global $feedwordpress;
global $inspectPostMeta;
// Put in Manual Editing checkbox
add_action('add_meta_boxes', 'feedwordpress_post_add_meta_boxes', 10, 2);
add_filter('user_can_richedit', array($feedwordpress, 'user_can_richedit'), 1000, 1);
if (FeedWordPressDiagnostic::is_on('syndicated_posts:static_meta_data')) :
$inspectPostMeta = new InspectPostMeta;
endif;
} // function feedwordpress_add_post_edit_controls ()
function feedwordpress_post_add_meta_boxes ($post_type, $post) {
add_meta_box(
'feedwordpress-post-controls',
__('Syndication'),
'feedwordpress_post_edit_controls',
$post_type,
'side',
'high'
);
}
function feedwordpress_post_edit_controls () {
global $post;
$frozen_values = get_post_custom_values('_syndication_freeze_updates', $post->ID);
$frozen_post = ($frozen_values !== null and count($frozen_values) > 0 and 'yes' == $frozen_values[0]);
if (is_syndicated($post->ID)) :
?>
<p>This is a syndicated post, which originally appeared at
<cite><?php print esc_html(get_syndication_source(NULL, $post->ID)); ?></cite>.
<a href="<?php print esc_html(get_syndication_permalink($post->ID)); ?>">View original post</a>.</p>
<?php do_action('feedwordpress_post_edit_controls_pre', $post); ?>
<p><input type="hidden" name="feedwordpress_noncename" id="feedwordpress_noncename" value="<?php print wp_create_nonce(plugin_basename(__FILE__)); ?>" />
<label><input type="checkbox" name="freeze_updates" value="yes" <?php if ($frozen_post) : ?>checked="checked"<?php endif; ?> /> <strong>Manual editing.</strong>
If set, FeedWordPress will not overwrite the changes you make manually
to this post, if the syndicated content is updated on the
feed.</label></p>
<?php do_action('feedwordpress_post_edit_controls', $post); ?>
<?php
else :
?>
<p>This post was created locally at this website.</p>
<?php
endif;
} /* function feedwordpress_post_edit_controls () */
function feedwordpress_save_post_edit_controls ( $post_id ) {
global $post;
if (!isset($_POST['feedwordpress_noncename']) or !wp_verify_nonce($_POST['feedwordpress_noncename'], plugin_basename(__FILE__))) :
return $post_id;
endif;
// Verify if this is an auto save routine. If it is our form has
// not been submitted, so we don't want to do anything.
if ( defined('DOING_AUTOSAVE') and DOING_AUTOSAVE ) :
return $post_id;
endif;
// The data in $_POST is for applying only to the post actually
// in the edit window, i.e. $post
if ($post_id != $post->ID) :
return $post_id;
endif;
// Check permissions
$cap[0] = 'edit_post';
$cap[1] = 'edit_' . $_POST['post_type'];
if (
!current_user_can( $cap[0], $post_id )
and !current_user_can( $cap[1], $post_id )
) :
return $post_id;
endif;
// OK, we're golden. Now let's save some data.
if (isset($_POST['freeze_updates'])) :
update_post_meta($post_id, '_syndication_freeze_updates', $_POST['freeze_updates']);
$ret = $_POST['freeze_updates'];
// If you make manual edits through the WordPress editing
// UI then they should be run through normal WP formatting
// filters.
update_post_meta($post_id, '_feedwordpress_formatting_filters', 'yes');
else :
delete_post_meta($post_id, '_syndication_freeze_updates');
$ret = NULL;
endif;
do_action('feedwordpress_save_edit_controls', $post_id);
return $ret;
} // function feedwordpress_save_edit_controls
################################################################################
## class FeedWordPressBoilerplateReformatter ###################################
################################################################################
class FeedWordPressBoilerplateReformatter {
var $id, $element;
public function __construct ($id = NULL, $element = 'post') {
$this->id = $id;
$this->element = $element;
}
function shortcode_methods () {
return array(
'source' => 'source_link',
'source-name' => 'source_name',
'source-url' => 'source_url',
'original-link' => 'original_link',
'original-url' => 'original_url',
'author' => 'source_author_link',
'author-name' => 'source_author',
'feed-setting' => 'source_setting',
);
}
function do_shortcode ($template) {
$codes = $this->shortcode_methods();
// Register shortcodes relative to this object/post ID/element.
foreach ($codes as $code => $method) :
add_shortcode($code, array($this, $method));
endforeach;
$template = do_shortcode($template);
// Unregister shortcodes.
foreach ($codes as $code => $method) :
remove_shortcode($code);
endforeach;
return $template;
}
function source_name ($atts) {
$param = shortcode_atts(array(
'original' => NULL,
), $atts);
return get_syndication_source($param['original'], $this->id);
}
function source_url ($atts) {
$param = shortcode_atts(array(
'original' => NULL,
), $atts);
return get_syndication_source_link($param['original'], $this->id);
}
function source_link ($atts) {
switch (strtolower($atts[0])) :
case '-name' :
$ret = $this->source_name($atts);
break;
case '-url' :
$ret = $this->source_url($atts);
break;
default :
$param = shortcode_atts(array(
'original' => NULL,
), $atts);
if ('title' == $this->element) :
$ret = $this->source_name($atts);
else :
$ret = '<a href="'.htmlspecialchars($this->source_url($atts)).'">'.htmlspecialchars($this->source_name($atts)).'</a>';
endif;
endswitch;
return $ret;
}
function source_setting ($atts) {
$param = shortcode_atts(array(
'key' => NULL,
), $atts);
return get_feed_meta($param['key'], $this->id);
}
function original_link ($atts, $text) {
$url = $this->original_url($atts);
return '<a href="'.esc_url($url).'">'.do_shortcode($text).'</a>';
}
function original_url ($atts) {
return get_syndication_permalink($this->id);
}
function source_author ($atts) {
return get_the_author();
}
function source_author_link ($atts) {
switch (strtolower($atts[0])) :
case '-name' :
$ret = $this->source_author($atts);
break;
default :
global $authordata; // Janky.
if ('title' == $this->element) :
$ret = $this->source_author($atts);
else :
$ret = get_the_author();
$url = get_author_posts_url((int) $authordata->ID, (int) $authordata->user_nicename);
if ($url) :
$ret = '<a href="'.$url.'" '
.'title="Read other posts by '.esc_html($authordata->display_name).'">'
.$ret
.'</a>';
endif;
endif;
endswitch;
return $ret;
}
}
function add_boilerplate_reformat ($template, $element, $id = NULL) {
if ('post' == $element and !preg_match('/< (p|div) ( \s [^>]+ )? >/xi', $template)) :
$template = '<p class="syndicated-attribution">'.$template.'</p>';
endif;
$ref = new FeedWordPressBoilerplateReformatter($id, $element);
return $ref->do_shortcode($template);
}
function add_boilerplate_simple ($element, $title, $id = NULL) {
if (is_syndicated($id)) :
$meta = get_feed_meta('boilerplate rules', $id);
if ($meta and !is_array($meta)) : $meta = unserialize($meta); endif;
if (!is_array($meta) or empty($meta)) :
$meta = get_option('feedwordpress_boilerplate');
endif;
if (is_array($meta) and !empty($meta)) :
foreach ($meta as $rule) :
if ($element==$rule['element']) :
$rule['template'] = add_boilerplate_reformat($rule['template'], $element, $id);
if ('before'==$rule['placement']) :
$title = $rule['template'] . ' ' . $title;
else :
$title = $title . ' ' . $rule['template'];
endif;
endif;
endforeach;
endif;
endif;
return $title;
}
function add_boilerplate_title ($title, $id = NULL) {
return add_boilerplate_simple('title', $title, $id);
}
function add_boilerplate_excerpt ($title, $id = NULL) {
return add_boilerplate_simple('excerpt', $title, $id);
}
function add_boilerplate_content ($content) {
if (is_syndicated()) :
$meta = get_feed_meta('boilerplate rules');
if ($meta and !is_array($meta)) : $meta = unserialize($meta); endif;
if (!is_array($meta) or empty($meta)) :
$meta = get_option('feedwordpress_boilerplate');
endif;
if (is_array($meta) and !empty($meta)) :
foreach ($meta as $rule) :
if ('post'==$rule['element']) :
$rule['template'] = add_boilerplate_reformat($rule['template'], 'post');
if ('before'==$rule['placement']) :
$content = $rule['template'] . "\n" . $content;
else :
$content = $content . "\n" . $rule['template'];
endif;
endif;
endforeach;
endif;
endif;
return $content;
}
################################################################################
## 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