forked from radgeek/feedwordpress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
feedwordpress.php
2125 lines (1794 loc) · 70.3 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: 2012.1218
Author: Charles Johnson
Author URI: http://radgeek.com/
License: GPL
*/
/**
* @package FeedWordPress
* @version 2012.1218
*/
# This 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/>
# according to the terms of the GNU General Public License.
#
# INSTALLATION: see readme.txt or <http://projects.radgeek.com/install>
#
# USAGE: once FeedWordPress is installed, you manage just about everything from
# the WordPress Dashboard, under the Syndication menu. To ensure that fresh
# content is added as it becomes available, you can convince your contributors
# to put your XML-RPC URI (if WordPress is installed at
# <http://www.zyx.com/blog>, XML-RPC requests should be sent to
# <http://www.zyx.com/blog/xmlrpc.php>), or update manually under the
# Syndication menu, or set up automatic updates under Syndication --> Settings,
# or use a cron job.
# -- Don't change these unless you know what you're doing...
define ('FEEDWORDPRESS_VERSION', '2012.1218');
define ('FEEDWORDPRESS_AUTHOR_CONTACT', 'http://radgeek.com/contact');
if (!defined('FEEDWORDPRESS_BLEG')) :
define ('FEEDWORDPRESS_BLEG', true);
endif;
// Defaults
define ('DEFAULT_SYNDICATION_CATEGORY', 'Contributors');
define ('DEFAULT_UPDATE_PERIOD', 60); // value in minutes
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 ('FWP_SCHEMA_HAS_USERMETA', 2966);
define ('FWP_SCHEMA_USES_ARGS_TAXONOMY', 12694); // Revision # for using $args['taxonomy'] to get link categories
define ('FWP_SCHEMA_30', 12694); // Database schema # for WP 3.0
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;
// Use our the cache settings that we want.
add_filter('wp_feed_cache_transient_lifetime', array('FeedWordPress', 'cache_lifetime'));
// Ensure that we have SimplePie loaded up and ready to go.
// We no longer need a MagpieRSS upgrade module. Hallelujah!
if (!class_exists('SimplePie')) :
require_once(ABSPATH . WPINC . '/class-simplepie.php');
endif;
require_once(ABSPATH . WPINC . '/class-feed.php');
if (!function_exists('wp_insert_user')) :
require_once (ABSPATH . WPINC . '/registration.php'); // for wp_insert_user
endif;
$dir = dirname(__FILE__);
require_once("${dir}/admin-ui.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");
// Magic quotes are just about the stupidest thing ever.
if (is_array($_POST)) :
$fwp_post = $_POST;
if (get_magic_quotes_gpc()) :
$fwp_post = stripslashes_deep($fwp_post);
endif;
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;
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');
# 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);
$feedwordpress = new FeedWordPress;
# 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('shutdown', array($feedwordpress, 'email_diagnostic_log'));
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);
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())
################################################################################
## LOGGING FUNCTIONS: log status updates to error_log if you want it ###########
################################################################################
class FeedWordPressDiagnostic {
function feed_error ($error, $old, $link) {
$wpError = $error['object'];
$url = $link->uri();
$mesgs = $wpError->get_error_messages();
foreach ($mesgs as $mesg) :
$mesg = esc_html($mesg);
FeedWordPress::diagnostic(
'updated_feeds:errors',
"Feed Error: [${url}] update returned error: $mesg"
);
$hours = get_option('feedwordpress_diagnostics_persistent_errors_hours', 2);
$span = ($error['ts'] - $error['since']);
if ($span >= ($hours * 60 * 60)) :
$since = date('r', $error['since']);
$mostRecent = date('r', $error['ts']);
FeedWordPress::diagnostic(
'updated_feeds:errors:persistent',
"Feed Update Error: [${url}] returning errors"
." since ${since}:<br/><code>$mesg</code>",
$url, $error['since'], $error['ts']
);
endif;
endforeach;
}
function admin_emails ($id = '') {
$users = get_users_of_blog($id);
$recipients = array();
foreach ($users as $user) :
$user_id = (isset($user->user_id) ? $user->user_id : $user->ID);
$dude = new WP_User($user_id);
if ($dude->has_cap('administrator')) :
if ($dude->user_email) :
$recipients[] = $dude->user_email;
endif;
endif;
endforeach;
return $recipients;
}
} /* class FeedWordPressDiagnostic */
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 (FeedWordPress::diagnostic_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() */
################################################################################
## TEMPLATE API: functions to make your templates syndication-aware ############
################################################################################
/**
* is_syndicated: Tests whether the current post in a Loop context, or a post
* given by ID number, was syndicated by FeedWordPress. Useful for templates
* to determine whether or not to retrieve syndication-related meta-data in
* displaying a post.
*
* @param int $id The post to check for syndicated status. Defaults to the current post in a Loop context.
* @return bool TRUE if the post's meta-data indicates it was syndicated; FALSE otherwise
*/
function is_syndicated ($id = NULL) {
$p = new FeedWordPressLocalPost($id);
return $p->is_syndicated();
} /* function is_syndicated() */
function feedwordpress_display_url ($url, $before = 60, $after = 0) {
$bits = parse_url($url);
// Strip out crufty subdomains
if (isset($bits['host'])) :
$bits['host'] = preg_replace('/^www[0-9]*\./i', '', $bits['host']);
endif;
// Reassemble bit-by-bit with minimum of crufty elements
$url = (isset($bits['user'])?$bits['user'].'@':'')
.(isset($bits['host'])?$bits['host']:'')
.(isset($bits['path'])?$bits['path']:'')
.(isset($uri_bits['port'])?':'.$uri_bits['port']:'')
.(isset($bits['query'])?'?'.$bits['query']:'');
if (strlen($url) > ($before+$after)) :
$url = substr($url, 0, $before).'…'.substr($url, 0 - $after, $after);
endif;
return $url;
} /* feedwordpress_display_url () */
function get_syndication_source_property ($original, $id, $local, $remote = NULL) {
$p = new FeedWordPressLocalPost($id);
return $p->meta($local, array("unproxy" => $original, "unproxied setting" => $remote));
} /* function get_syndication_source_property () */
function get_syndication_source_link ($original = NULL, $id = NULL) {
$p = new FeedWordPressLocalPost($id);
return $p->syndication_source_link($original);
} /* function get_syndication_source_link() */
function the_syndication_source_link ($original = NULL, $id = NULL) {
echo get_syndication_source_link($original, $id);
} /* function the_syndication_source_link() */
function get_syndication_source ($original = NULL, $id = NULL) {
$p = new FeedWordPressLocalPost($id);
return $p->syndication_source($original);
} /* function get_syndication_source() */
function the_syndication_source ($original = NULL, $id = NULL) {
echo get_syndication_source($original, $id);
} /* function the_syndication_source () */
function get_syndication_feed ($original = NULL, $id = NULL) {
$p = new FeedWordPressLocalPost($id);
return $p->syndication_feed($original);
} /* function get_syndication_feed() */
function the_syndication_feed ($original = NULL, $id = NULL) {
echo get_syndication_feed($original, $id);
} /* function the_syndication_feed() */
function get_syndication_feed_guid ($original = NULL, $id = NULL) {
$p = new FeedWordPressLocalPost($id);
return $p->syndication_feed_guid($original);
} /* function get_syndication_feed_guid () */
function the_syndication_feed_guid ($original = NULL, $id = NULL) {
echo get_syndication_feed_guid($original, $id);
} /* function the_syndication_feed_guid () */
function get_syndication_feed_id ($id = NULL) {
$p = new FeedWordPressLocalPost($id);
return $p->feed_id();
} /* function get_syndication_feed_id () */
function the_syndication_feed_id ($id = NULL) {
echo get_syndication_feed_id($id);
} /* function the_syndication_feed_id () */
function get_syndication_feed_object ($id = NULL) {
$p = new FeedWordPressLocalPost($id);
return $p->feed();
} /* function get_syndication_feed_object() */
function get_feed_meta ($key, $id = NULL) {
$ret = NULL;
$link = get_syndication_feed_object($id);
if (is_object($link) and isset($link->settings[$key])) :
$ret = $link->settings[$key];
endif;
return $ret;
} /* function get_feed_meta() */
function get_syndication_permalink ($id = NULL) {
$p = new FeedWordPressLocalPost($id);
return $p->syndication_permalink();
} /* function get_syndication_permalink () */
function the_syndication_permalink ($id = NULL) {
echo get_syndication_permalink($id);
} /* function the_syndication_permalink () */
/**
* get_local_permalink: returns a string containing the internal permalink
* for a post (whether syndicated or not) on your local WordPress installation.
* This may be useful if you want permalinks to point to the original source of
* an article for most purposes, but want to retrieve a URL for the local
* representation of the post for one or two limited purposes (for example,
* linking to a comments page on your local aggregator site).
*
* @param $id The numerical ID of the post to get the permalink for. If empty,
* defaults to the current post in a Loop context.
* @return string The URL of the local permalink for this post.
*
* @uses get_permalink()
* @global $feedwordpress_the_original_permalink
*
* @since 2010.0217
*/
function get_local_permalink ($id = NULL) {
global $feedwordpress_the_original_permalink;
// get permalink, and thus activate filter and force global to be filled
// with original URL.
$url = get_permalink($id);
return $feedwordpress_the_original_permalink;
} /* get_local_permalink() */
/**
* the_original_permalink: displays the contents of get_original_permalink()
*
* @param $id The numerical ID of the post to get the permalink for. If empty,
* defaults to the current post in a Loop context.
*
* @uses get_local_permalinks()
* @uses apply_filters
*
* @since 2010.0217
*/
function the_local_permalink ($id = NULL) {
print apply_filters('the_permalink', get_local_permalink($id));
} /* function the_local_permalink() */
################################################################################
## 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 ($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 () {
$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,
WP_PLUGIN_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')
);
} /* 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;
// Put in Manual Editing checkbox
add_meta_box('feedwordpress-post-controls', __('Syndication'), 'feedwordpress_post_edit_controls', 'post', 'side', 'high');
add_filter('user_can_richedit', array($feedwordpress, 'user_can_richedit'), 1000, 1);
if (FeedWordPress::diagnostic_on('syndicated_posts:static_meta_data')) :
$GLOBALS['inspectPostMeta'] = new InspectPostMeta;
endif;
} // function FeedWordPress::postEditControls
function feedwordpress_post_edit_controls () {
global $post;
$frozen_values = get_post_custom_values('_syndication_freeze_updates', $post->ID);
$frozen_post = (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>
<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
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;
// Check permissions
if ( !current_user_can( 'edit_'.$_POST['post_type'], $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'];
else :
delete_post_meta($post_id, '_syndication_freeze_updates');
$ret = NULL;
endif;
return $ret;
} // function feedwordpress_save_edit_controls
################################################################################
## 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;
# function FeedWordPress (): Contructor; retrieve a list of feeds
function FeedWordPress () {
$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;
$this->httpauth = new FeedWordPressHTTPAuthenticator;
} // FeedWordPress::FeedWordPress ()
function subscribed ($id) {
return (isset($this->feedurls[$id]) or isset($this->feeds[$id]));
} /* FeedWordPress::subscribed () */
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::subscriptions () */
# function update (): polls for updates on one or more Contributor feeds
#
# 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.
#
function update ($uri = null, $crash_ts = null) {