PluginProbe
FeedWordPress / 2009.1112
FeedWordPress v2009.1112
trunk 0.8 0.9 0.91 0.95 0.96 0.97 0.98 0.981 0.99 0.991 0.992 0.993 2008.1030 2008.1101 2008.1105 2008.1214 2009.0612 2009.0613 2009.0618 2009.0707 2009.1111 2009.1112 2010.0127 2010.0528 All 65 releases
← All changes | feedwordpress.php +235 -118 2009.07072009.1112 View file →
@@ -1,10 +1,10 @@
1 1 <?php
2 2 /*
3 3 Plugin Name: FeedWordPress
4 -Plugin URI: http://projects.radgeek.com/feedwordpress
4 +Plugin URI: http://feedwordpress.radgeek.com/
5 5 Description: simple and flexible Atom/RSS syndication for WordPress
6 -Version: 2009.0707
6 +Version: 2009.1112
7 7 Author: Charles Johnson
8 8 Author URI: http://radgeek.com/
9 9 License: GPL
10 10 */
@@ -27,15 +27,22 @@
27 27 # or use a cron job.
28 28
29 29 # -- Don't change these unless you know what you're doing...
30 30
31 -define ('FEEDWORDPRESS_VERSION', '2009.0707');
31 +define ('FEEDWORDPRESS_VERSION', '2009.1112');
32 32 define ('FEEDWORDPRESS_AUTHOR_CONTACT', 'http://radgeek.com/contact');
33 +
34 +// Defaults
33 35 define ('DEFAULT_SYNDICATION_CATEGORY', 'Contributors');
36 +define ('DEFAULT_UPDATE_PERIOD', 60); // value in minutes
34 37
35 -$feedwordpress_debug = get_option('feedwordpress_debug');
36 -if (is_string($feedwordpress_debug)) :
37 - $feedwordpress_debug = ($feedwordpress_debug == 'yes');
38 +if (isset($_REQUEST['feedwordpress_debug'])) :
39 + $feedwordpress_debug = $_REQUEST['feedwordpress_debug'];
40 +else :
41 + $feedwordpress_debug = get_option('feedwordpress_debug');
42 + if (is_string($feedwordpress_debug)) :
43 + $feedwordpress_debug = ($feedwordpress_debug == 'yes');
44 + endif;
38 45 endif;
39 46 define ('FEEDWORDPRESS_DEBUG', $feedwordpress_debug);
40 47
41 48 define ('FEEDWORDPRESS_CAT_SEPARATOR_PATTERN', '/[:\n]/');
@@ -67,8 +74,10 @@
67 74 // used for more than testing purposes!
68 75 define('MAGPIE_CACHE_AGE', 1);
69 76 else :
70 77 define('MAGPIE_DEBUG', false);
78 +
79 + define('MAGPIE_CACHE_AGE', 1*60);
71 80 endif;
72 81
73 82 // Note that the rss-functions.php that comes prepackaged with WordPress is
74 83 // old & busted. For the new hotness, drop a copy of rss.php from
@@ -170,8 +179,9 @@
170 179 # Admin menu
171 180 add_action('admin_menu', 'fwp_add_pages');
172 181 add_action('admin_notices', 'fwp_check_debug');
173 182 add_action('admin_notices', 'fwp_check_magpie');
183 + add_action('init', 'feedwordpress_check_for_magpie_fix');
174 184
175 185 # Inbound XML-RPC update methods
176 186 add_filter('xmlrpc_methods', 'feedwordpress_xmlrpc_hook');
177 187
@@ -193,13 +203,26 @@
193 203 add_action('feedwordpress_update', 'log_feedwordpress_update_feeds', 100);
194 204 add_action('feedwordpress_check_feed', 'log_feedwordpress_check_feed', 100);
195 205 add_action('feedwordpress_update_complete', 'log_feedwordpress_update_complete', 100);
196 206 endif;
197 -
207 +
208 + if (FeedWordPress::update_requested() and FEEDWORDPRESS_DEBUG) :
209 + add_action('post_syndicated_item', 'debug_out_feedwordpress_post', 100);
210 + add_action('update_syndicated_item', 'debug_out_feedwordpress_update_post', 100);
211 + add_action('feedwordpress_update', 'debug_out_feedwordpress_update_feeds', 100);
212 + add_action('feedwordpress_check_feed', 'debug_out_feedwordpress_check_feed', 100);
213 + add_action('feedwordpress_update_complete', 'debug_out_feedwordpress_update_complete', 100);
214 + endif;
215 +
198 216 # Cron-less auto-update. Hooray!
199 - add_action('init', 'feedwordpress_auto_update');
200 - add_action('init', 'feedwordpress_check_for_magpie_fix');
201 -
217 + $autoUpdateHook = get_option('feedwordpress_automatic_updates');
218 + if ($autoUpdateHook != 'init') :
219 + $autoUpdateHook = 'shutdown';
220 + endif;
221 + add_action($autoUpdateHook, 'feedwordpress_auto_update');
222 +
223 + add_action('init', 'feedwordpress_update_magic_url');
224 +
202 225 # Default sanitizers
203 226 add_filter('syndicated_item_content', array('SyndicatedPost', 'resolve_relative_uris'), 0, 2);
204 227 add_filter('syndicated_item_content', array('SyndicatedPost', 'sanitize_content'), 0, 2);
205 228
@@ -220,8 +243,16 @@
220 243 elseif (isset($_POST['upgrade'])) :
221 244 $source = dirname(__FILE__)."/MagpieRSS-upgrade/rss.php";
222 245 $destination = ABSPATH . WPINC . '/rss.php';
223 246 $success = @copy($source, $destination);
247 +
248 + // Copy over rss-functions.php, too, to avoid collisions
249 + // on pre-lapsarian versions of WordPress.
250 + if ($success) :
251 + $source = dirname(__FILE__)."/MagpieRSS-upgrade/rss-functions.php";
252 + $destination = ABSPATH . WPINC . '/rss-functions.php';
253 + $success = @copy($source, $destination);
254 + endif;
224 255 $ret = (int) $success;
225 256 endif;
226 257
227 258 if (strpos($back_to, '?')===false) : $sep = '?';
@@ -237,13 +268,21 @@
237 268 if (FeedWordPress::stale()) :
238 269 $feedwordpress =& new FeedWordPress;
239 270 $feedwordpress->update();
240 271 endif;
241 -
272 +} /* feedwordpress_auto_update () */
273 +
274 +function feedwordpress_update_magic_url () {
275 + // Explicit update request in the HTTP request (e.g. from a cron job)
242 276 if (FeedWordPress::update_requested()) :
277 + $feedwordpress =& new FeedWordPress;
278 + $feedwordpress->update(FeedWordPress::update_requested_url());
279 +
280 + // Magic URL should return nothing but a 200 OK header packet
281 + // when successful.
243 282 exit;
244 283 endif;
245 -} /* feedwordpress_auto_update () */
284 +} /* feedwordpress_magic_update_url () */
246 285
247 286 ################################################################################
248 287 ## LOGGING FUNCTIONS: log status updates to error_log if you want it ###########
249 288 ################################################################################
@@ -280,23 +319,56 @@
280 319 .(is_null($delta) ? "Error: I don't syndicate that URI"
281 320 : implode(' and ', $mesg)));
282 321 }
283 322
323 +function debug_out_feedwordpress_post ($id) {
324 + $post = wp_get_single_post($id);
325 + print ("[".date('Y-m-d H:i:s')."][feedwordpress] posted "
326 + ."'{$post->post_title}' ({$post->post_date})\n");
327 +}
328 +
329 +function debug_out_feedwordpress_update_post ($id) {
330 + $post = wp_get_single_post($id);
331 + print ("[".date('Y-m-d H:i:s')."][feedwordpress] updated "
332 + ."'{$post->post_title}' ({$post->post_date})"
333 + ." (as of {$post->post_modified})\n");
334 +}
335 +
336 +function debug_out_feedwordpress_update_feeds ($uri) {
337 + print ("[".date('Y-m-d H:i:s')."][feedwordpress] update('$uri')\n");
338 +}
339 +
340 +function debug_out_feedwordpress_check_feed ($feed) {
341 + $uri = $feed['link/uri']; $name = $feed['link/name'];
342 + print ("[".date('Y-m-d H:i:s')."][feedwordpress] Examining $name <$uri>\n");
343 +}
344 +
345 +function debug_out_feedwordpress_update_complete ($delta) {
346 + $mesg = array();
347 + if (isset($delta['new'])) $mesg[] = 'added '.$delta['new'].' new posts';
348 + if (isset($delta['updated'])) $mesg[] = 'updated '.$delta['updated'].' existing posts';
349 + if (empty($mesg)) $mesg[] = 'nothing changed';
350 +
351 + print ("[".date('Y-m-d H:i:s')."][feedwordpress] "
352 + .(is_null($delta) ? "Error: I don't syndicate that URI"
353 + : implode(' and ', $mesg))."\n");
354 +}
355 +
284 356 ################################################################################
285 357 ## TEMPLATE API: functions to make your templates syndication-aware ############
286 358 ################################################################################
287 359
288 -function is_syndicated () { return (strlen(get_syndication_feed_id()) > 0); }
360 +function is_syndicated ($id = NULL) { return (strlen(get_syndication_feed_id($id)) > 0); }
289 361
290 -function get_syndication_source_link ($original = NULL) {
362 +function get_syndication_source_link ($original = NULL, $id = NULL) {
291 363 if (is_null($original)) : $original = FeedWordPress::use_aggregator_source_data();
292 364 endif;
293 365
294 - if ($original) : $vals = get_post_custom_values('syndication_source_uri_original');
366 + if ($original) : $vals = get_post_custom_values('syndication_source_uri_original', $id);
295 367 else : $vals = array();
296 368 endif;
297 369
298 - if (count($vals) == 0) : $vals = get_post_custom_values('syndication_source_uri');
370 + if (count($vals) == 0) : $vals = get_post_custom_values('syndication_source_uri', $id);
299 371 endif;
300 372
301 373 if (count($vals) > 0) : $ret = $vals[0]; else : $ret = NULL; endif;
302 374
@@ -302,37 +374,71 @@
302 374
303 375 return $ret;
304 376 } /* function get_syndication_source_link() */
305 377
306 -function the_syndication_source_link ($original = NULL) { echo get_syndication_source_link($original); }
378 +function the_syndication_source_link ($original = NULL, $id = NULL) {
379 + echo get_syndication_source_link($original, $id);
380 +}
307 381
308 -function get_syndication_source ($original = NULL) {
309 - if (is_null($original)) : $original = FeedWordPress::use_aggregator_source_data();
382 +function feedwordpress_display_url ($url, $before = 60, $after = 0) {
383 + $bits = parse_url($url);
384 +
385 + // Strip out crufty subdomains
386 + $bits['host'] = preg_replace('/^www[0-9]*\./i', '', $bits['host']);
387 +
388 + // Reassemble bit-by-bit with minimum of crufty elements
389 + $url = (isset($bits['user'])?$bits['user'].'@':'')
390 + .(isset($bits['host'])?$bits['host']:'')
391 + .(isset($bits['path'])?$bits['path']:'')
392 + .(isset($bits['query'])?'?'.$bits['query']:'');
393 +
394 + if (strlen($url) > ($before+$after)) :
395 + $url = substr($url, 0, $before).'…'.substr($url, 0 - $after, $after);
310 396 endif;
311 397
312 - if ($original) : $vals = get_post_custom_values('syndication_source_original');
313 - else : $vals = array();
398 + return $url;
399 +}
400 +
401 +function get_syndication_source ($original = NULL, $id = NULL) {
402 + if (is_null($original)) :
403 + $original = FeedWordPress::use_aggregator_source_data();
314 404 endif;
405 +
406 + if ($original) :
407 + $vals = get_post_custom_values('syndication_source_original', $id);
408 + else :
409 + $vals = array();
410 + endif;
315 411
316 - if (count($vals) == 0) : $vals = get_post_custom_values('syndication_source');
412 + if (count($vals) == 0) :
413 + $vals = get_post_custom_values('syndication_source', $id);
317 414 endif;
318 415
319 - if (count($vals) > 0) : $ret = $vals[0]; else : $ret = NULL; endif;
416 + if (count($vals) > 0) :
417 + $ret = $vals[0];
418 + else :
419 + $ret = NULL;
420 + endif;
320 421
422 + if (is_null($ret) or strlen(trim($ret)) == 0) :
423 + // Fall back to URL of blog
424 + $ret = feedwordpress_display_url(get_syndication_source_link());
425 + endif;
426 +
321 427 return $ret;
322 428 } /* function get_syndication_source() */
323 429
324 -function the_syndication_source ($original = NULL) { echo get_syndication_source($original); }
430 +function the_syndication_source ($original = NULL, $id = NULL) { echo get_syndication_source($original, $id); }
325 431
326 -function get_syndication_feed ($original = NULL) {
432 +function get_syndication_feed ($original = NULL, $id = NULL) {
327 433 if (is_null($original)) : $original = FeedWordPress::use_aggregator_source_data();
328 434 endif;
329 435
330 - if ($original) : $vals = get_post_custom_values('syndication_feed_original');
436 + if ($original) : $vals = get_post_custom_values('syndication_feed_original', $id);
331 437 else : $vals = array();
332 438 endif;
333 -
334 - if (count($vals) == 0) : $vals = get_post_custom_values('syndication_feed');
439 +
440 + if (count($vals) == 0) : $vals = get_post_custom_values('syndication_feed', $id);
335 441 endif;
336 442
337 443 if (count($vals) > 0) : $ret = $vals[0]; else : $ret = NULL; endif;
338 444
@@ -338,19 +444,19 @@
338 444
339 445 return $ret;
340 446 } /* function get_syndication_feed() */
341 447
342 -function the_syndication_feed ($original = NULL) { echo get_syndication_feed ($original); }
448 +function the_syndication_feed ($original = NULL, $id = NULL) { echo get_syndication_feed($original, $id); }
343 449
344 -function get_syndication_feed_guid ($original = NULL) {
450 +function get_syndication_feed_guid ($original = NULL, $id = NULL) {
345 451 if (is_null($original)) : $original = FeedWordPress::use_aggregator_source_data();
346 452 endif;
347 453
348 - if ($original) : $vals = get_post_custom_values('syndication_source_id_original');
454 + if ($original) : $vals = get_post_custom_values('syndication_source_id_original', $id);
349 455 else : $vals = array();
350 456 endif;
351 457
352 - if (count($vals) == 0) : $vals = array(get_feed_meta('feed/id'));
458 + if (count($vals) == 0) : $vals = array(get_feed_meta('feed/id', $id));
353 459 endif;
354 460
355 461 if (count($vals) > 0) : $ret = $vals[0]; else : $ret = NULL; endif;
356 462
@@ -356,18 +462,18 @@
356 462
357 463 return $ret;
358 464 } /* function get_syndication_feed_guid () */
359 465
360 -function the_syndication_feed_guid ($original = NULL) { echo get_syndication_feed_guid($original); }
466 +function the_syndication_feed_guid ($original = NULL, $id = NULL) { echo get_syndication_feed_guid($original, $id); }
361 467
362 -function get_syndication_feed_id () { list($u) = get_post_custom_values('syndication_feed_id'); return $u; }
363 -function the_syndication_feed_id () { echo get_syndication_feed_id(); }
468 +function get_syndication_feed_id ($id = NULL) { list($u) = get_post_custom_values('syndication_feed_id', $id); return $u; }
469 +function the_syndication_feed_id ($id = NULL) { echo get_syndication_feed_id($id); }
364 470
365 471 $feedwordpress_linkcache = array (); // only load links from database once
366 472
367 -function get_feed_meta ($key) {
473 +function get_feed_meta ($key, $id = NULL) {
368 474 global $wpdb, $feedwordpress_linkcache;
369 - $feed_id = get_syndication_feed_id();
475 + $feed_id = get_syndication_feed_id($id);
370 476
371 477 $ret = NULL;
372 478 if (strlen($feed_id) > 0):
373 479 if (isset($feedwordpress_linkcache[$feed_id])) :
@@ -381,13 +487,13 @@
381 487 endif;
382 488 return $ret;
383 489 } /* get_feed_meta() */
384 490
385 -function get_syndication_permalink () {
386 - list($u) = get_post_custom_values('syndication_permalink'); return $u;
491 +function get_syndication_permalink ($id = NULL) {
492 + list($u) = get_post_custom_values('syndication_permalink', $id); return $u;
387 493 }
388 -function the_syndication_permalink () {
389 - echo get_syndication_permalink();
494 +function the_syndication_permalink ($id = NULL) {
495 + echo get_syndication_permalink($id);
390 496 }
391 497
392 498 ################################################################################
393 499 ## FILTERS: syndication-aware handling of post data for templates and feeds ####
@@ -424,16 +530,16 @@
424 530 // In a post context....
425 531 if (is_syndicated()) :
426 532 ?>
427 533 <source>
428 - <title><?php the_syndication_source(); ?></title>
429 - <link rel="alternate" type="text/html" href="<?php the_syndication_source_link(); ?>" />
430 - <link rel="self" href="<?php the_syndication_feed(); ?>" />
534 + <title><?php print htmlspecialchars(get_syndication_source()); ?></title>
535 + <link rel="alternate" type="text/html" href="<?php print htmlspecialchars(get_syndication_source_link()); ?>" />
536 + <link rel="self" href="<?php print htmlspecialchars(get_syndication_feed()); ?>" />
431 537 <?php
432 538 $id = get_syndication_feed_guid();
433 539 if (strlen($id) > 0) :
434 540 ?>
435 - <id><?php print $id; ?></id>
541 + <id><?php print htmlspecialchars($id); ?></id>
436 542 <?php
437 543 endif;
438 544 $updated = get_feed_meta('feed/updated');
439 545 if (strlen($updated) > 0) : ?>
@@ -468,23 +574,14 @@
468 574 // add icon parameter
469 575 $menu[] = WP_PLUGIN_URL.'/'.$fwp_path.'/feedwordpress-tiny.png';
470 576 endif;
471 577
472 - if (fwp_test_wp_version(FWP_SCHEMA_26)) :
473 - $options = __('Settings');
474 - $longoptions = __('Syndication Settings');
475 - else :
476 - $options = __('Options');
477 - $longoptions = __('Syndication Options');
478 - endif;
479 -
480 578 call_user_func_array('add_menu_page', $menu);
481 - add_submenu_page($fwp_path.'/syndication.php', 'Syndicated Posts', 'Posts', $fwp_capability['manage_options'], $fwp_path.'/posts-page.php');
579 + add_submenu_page($fwp_path.'/syndication.php', 'Syndicated Feeds & Updates', 'Feeds & Updates', $fwp_capability['manage_options'], $fwp_path.'/feeds-page.php');
580 + add_submenu_page($fwp_path.'/syndication.php', 'Syndicated Posts & Links', 'Posts & Links', $fwp_capability['manage_options'], $fwp_path.'/posts-page.php');
482 581 add_submenu_page($fwp_path.'/syndication.php', 'Syndicated Authors', 'Authors', $fwp_capability['manage_options'], $fwp_path.'/authors-page.php');
483 - add_submenu_page($fwp_path.'/syndication.php', 'Categories & Tags', 'Categories & Tags', $fwp_capability['manage_options'], $fwp_path.'/categories-page.php');
484 - add_submenu_page($fwp_path.'/syndication.php', $longoptions, $options, $fwp_capability['manage_options'], $fwp_path.'/syndication-options.php');
485 -
486 - add_options_page($longoptions, 'Syndication', $fwp_capability['manage_options'], $fwp_path.'/syndication-options.php');
582 + add_submenu_page($fwp_path.'/syndication.php', 'Categories'.FEEDWORDPRESS_AND_TAGS, 'Categories'.FEEDWORDPRESS_AND_TAGS, $fwp_capability['manage_options'], $fwp_path.'/categories-page.php');
583 + add_submenu_page($fwp_path.'/syndication.php', 'FeedWordPress Back End', 'Back End', $fwp_capability['manage_options'], $fwp_path.'/backend-page.php');
487 584 } /* function fwp_add_pages () */
488 585
489 586 function fwp_check_debug () {
490 587 // This is a horrible fucking kludge that I have to do because the
@@ -507,9 +604,9 @@
507 604 <?php
508 605 endif;
509 606 } /* function fwp_check_debug () */
510 607
511 -define('EXPECTED_MAGPIE_VERSION', '2009.0618');
608 +define('EXPECTED_MAGPIE_VERSION', '2009.0725');
512 609 function fwp_check_magpie () {
513 610 if (isset($_REQUEST['feedwordpress_magpie_fix'])) :
514 611 if ($_REQUEST['feedwordpress_magpie_fix']=='ignored') :
515 612 ?>
@@ -786,9 +883,9 @@
786 883 if (!is_null($crash_ts) and (time() > $crash_ts)) : // Check whether we've exceeded the time limit
787 884 break;
788 885 endif;
789 886
790 - $pinged_that = (is_null($uri) or in_array($uri, array($feed->uri(), $feed->homepage())));
887 + $pinged_that = (is_null($uri) or ($uri=='*') or in_array($uri, array($feed->uri(), $feed->homepage())));
791 888
792 889 if (!is_null($uri)) : // A site-specific ping always updates
793 890 $timely = true;
794 891 else :
@@ -816,8 +913,11 @@
816 913 }
817 914
818 915 function stale () {
819 916 if (get_option('feedwordpress_automatic_updates')) :
917 + // Do our best to avoid possible simultaneous
918 + // updates by getting up-to-the-minute settings.
919 +
820 920 $last = get_option('feedwordpress_last_update_all');
821 921
822 922 // If we haven't updated all yet, give it a time window
823 923 if (false === $last) :
@@ -836,12 +936,8 @@
836 936 else :
837 937 FeedWordPress::critical_bug('FeedWordPress::stale::last', $last, __LINE__);
838 938 endif;
839 939
840 - // Explicit request for an update (e.g. from a cron job).
841 - elseif (FeedWordPress::update_requested()) :
842 - $ret = true;
843 -
844 940 else :
845 941 $ret = false;
846 942 endif;
847 943 return $ret;
@@ -847,11 +943,25 @@
847 943 return $ret;
848 944 } // FeedWordPress::stale()
849 945
850 946 function update_requested () {
851 - return (isset($_REQUEST['update_feedwordpress']) and $_REQUEST['update_feedwordpress']);
947 + return (
948 + isset($_REQUEST['update_feedwordpress'])
949 + and $_REQUEST['update_feedwordpress']
950 + );
852 951 } // FeedWordPress::update_requested()
853 952
953 + function update_requested_url () {
954 + $ret = null;
955 +
956 + if (($_REQUEST['update_feedwordpress']=='*')
957 + or (preg_match('|^http://.*|i', $_REQUEST['update_feedwordpress']))) :
958 + $ret = $_REQUEST['update_feedwordpress'];
959 + endif;
960 +
961 + return $ret;
962 + } // FeedWordPress::update_requested_url()
963 +
854 964 function syndicate_link ($name, $uri, $rss) {
855 965 global $wpdb;
856 966
857 967 // Get the category ID#
@@ -859,16 +969,29 @@
859 969
860 970 // WordPress gets cranky if there's no homepage URI
861 971 if (!isset($uri) or strlen($uri)<1) : $uri = $rss; endif;
862 972
863 - if (function_exists('wp_insert_link')) { // WordPress 2.x
973 + if (function_exists('wp_insert_link')) : // WordPress 2.x
974 + if (FeedWordPressCompatibility::test_version(0, FWP_SCHEMA_21)) :
975 + // Morons.
976 + $name = $wpdb->escape($name);
977 + $uri = $wpdb->escape($uri);
978 + $rss = $wpdb->escape($rss);
979 +
980 + // Comes in as a single category
981 + $linkCats = $cat_id;
982 + else :
983 + // Comes in as an array of categories
984 + $linkCats = array($cat_id);
985 + endif;
986 +
864 987 $link_id = wp_insert_link(array(
865 988 "link_name" => $name,
866 989 "link_url" => $uri,
867 - "link_category" => (fwp_test_wp_version(0, FWP_SCHEMA_21) ? $cat_id : array($cat_id)),
990 + "link_category" => $linkCats,
868 991 "link_rss" => $rss
869 992 ));
870 - } else { // WordPress 1.5.x
993 + else : // WordPress 1.5.x
871 994 $result = $wpdb->query("
872 995 INSERT INTO $wpdb->links
873 996 SET
874 997 link_name = '".$wpdb->escape($name)."',
@@ -876,12 +999,20 @@
876 999 link_category = '".$wpdb->escape($cat_id)."',
877 1000 link_rss = '".$wpdb->escape($rss)."'
878 1001 ");
879 1002 $link_id = $wpdb->insert_id;
880 - } // if
1003 + endif;
881 1004 return $link_id;
882 1005 } // function FeedWordPress::syndicate_link()
883 1006
1007 + /*static*/ function syndicated_status ($what, $default) {
1008 + $ret = get_option("feedwordpress_syndicated_{$what}_status");
1009 + if (!$ret) :
1010 + $ret = $default;
1011 + endif;
1012 + return $ret;
1013 + } /* FeedWordPress::syndicated_status() */
1014 +
884 1015 function on_unfamiliar ($what = 'author', $override = NULL) {
885 1016 $set = array(
886 1017 'author' => array('create', 'default', 'filter'),
887 1018 'category' => array('create', 'tag', 'default', 'filter'),
@@ -940,61 +1071,31 @@
940 1071 global $wpdb, $wp_db_version;
941 1072
942 1073 $cat_id = get_option('feedwordpress_cat_id');
943 1074
944 - // If we don't yet *have* the category, we'll have to create it
945 - if ($cat_id === false) :
946 - $cat = $wpdb->escape(DEFAULT_SYNDICATION_CATEGORY);
947 -
948 - // Look for something with the right name...
949 - // -----------------------------------------
1075 + // If we don't yet have the category ID stored, search by name
1076 + if (!$cat_id) :
1077 + $cat_id = FeedWordPressCompatibility::link_category_id(DEFAULT_SYNDICATION_CATEGORY);
950 1078
951 - // WordPress 2.3 introduces a new taxonomy/term API
952 - if (function_exists('is_term')) :
953 - $cat_id = is_term($cat, 'link_category');
954 - // WordPress 2.1 and 2.2 use a common table for both link and post categories
955 - elseif (fwp_test_wp_version(FWP_SCHEMA_21, FWP_SCHEMA_23)) :
956 - $cat_id = $wpdb->get_var("SELECT cat_id FROM {$wpdb->categories} WHERE cat_name='$cat'");
957 - // WordPress 1.5 and 2.0.x have a separate table for link categories
958 - elseif (fwp_test_wp_version(0, FWP_SCHEMA_21)):
959 - $cat_id = $wpdb->get_var("SELECT cat_id FROM {$wpdb->linkcategories} WHERE cat_name='$cat'");
960 - // This should never happen.
961 - else :
962 - FeedWordPress::critical_bug('FeedWordPress::link_category_id::wp_db_version', $wp_db_version, __LINE__);
1079 + if ($cat_id) :
1080 + // We found it; let's stamp it.
1081 + update_option('feedwordpress_cat_id', $cat_id);
963 1082 endif;
964 1083
965 - // If you still can't find anything, make it for yourself.
966 - // -------------------------------------------------------
967 - if (!$cat_id) :
968 - // WordPress 2.3+ term/taxonomy API
969 - if (function_exists('wp_insert_term')) :
970 - $term = wp_insert_term($cat, 'link_category');
971 - $cat_id = $term['term_id'];
972 - // WordPress 2.1, 2.2 category API. By the way, why the fuck is this API function only available in a wp-admin module?
973 - elseif (function_exists('wp_insert_category') and !fwp_test_wp_version(FWP_SCHEMA_20, FWP_SCHEMA_21)) :
974 - $cat_id = wp_insert_category(array('cat_name' => $cat));
975 - // WordPress 1.5 and 2.0.x
976 - elseif (fwp_test_wp_version(0, FWP_SCHEMA_21)) :
977 - $result = $wpdb->query("
978 - INSERT INTO $wpdb->linkcategories
979 - SET
980 - cat_id = 0,
981 - cat_name='$cat',
982 - show_images='N',
983 - show_description='N',
984 - show_rating='N',
985 - show_updated='N',
986 - sort_order='name'
987 - ");
988 - $cat_id = $wpdb->insert_id;
989 - // This should never happen.
990 - else :
991 - FeedWordPress::critical_bug('FeedWordPress::link_category_id::wp_db_version', $wp_db_version, __LINE__);
992 - endif;
993 - endif;
1084 + // If we *do* have the category ID stored, verify that it exists
1085 + else :
1086 + $cat_id = FeedWordPressCompatibility::link_category_id((int) $cat_id, 'cat_id');
1087 + endif;
1088 +
1089 + // If we could not find an appropriate link category,
1090 + // make a new one for ourselves.
1091 + if (!$cat_id) :
1092 + $cat_id = FeedWordPressCompatibility::insert_link_category(DEFAULT_SYNDICATION_CATEGORY);
994 1093
1094 + // Stamp it
995 1095 update_option('feedwordpress_cat_id', $cat_id);
996 1096 endif;
1097 +
997 1098 return $cat_id;
998 1099 } // function FeedWordPress::link_category_id()
999 1100
1000 1101 # Upgrades and maintenance...
@@ -1071,14 +1172,16 @@
1071 1172 $wpdb->query("DROP TABLE tmp_custom_values");
1072 1173
1073 1174 // Now fix the guids to avoid duplicate posts
1074 1175 echo "<ul>";
1075 - foreach ($this->feeds as $feed) :
1176 + foreach ($this->feeds as $syndicatedLink) :
1177 + $feed = $syndicatedLink->settings;
1076 1178 echo "<li>Fixing post meta-data for <cite>".$feed['link/name']."</cite> &#8230; "; flush();
1077 1179 $rss = @fetch_rss($feed['link/uri']);
1078 1180 if (is_array($rss->items)) :
1079 1181 foreach ($rss->items as $item) :
1080 - $guid = $wpdb->escape(FeedWordPress::guid($item, $feed)); // new GUID algorithm
1182 + $post = new SyndicatedPost($item, $syndicatedLink);
1183 + $guid = $wpdb->escape($post->guid()); // new GUID algorithm
1081 1184 $link = $wpdb->escape($item['link']);
1082 1185
1083 1186 $wpdb->query("
1084 1187 UPDATE `{$wpdb->posts}` SET guid='{$guid}' WHERE guid='{$link}'
@@ -1102,8 +1205,22 @@
1102 1205 CREATE INDEX {$wpdb->posts}_guid_idx ON {$wpdb->posts}(guid)
1103 1206 ");
1104 1207 } /* FeedWordPress::create_guid_index () */
1105 1208
1209 + function clear_cache () {
1210 + global $wpdb;
1211 +
1212 + // MagpieRSS stores its cached feeds in options table rows with
1213 + // name = `rss_{md5 of url}` and timestamps for cached feeds in
1214 + // table rows with name = `rss_{md5 of url}_ts`. The md5 is
1215 + // always 32 characters in length, so the total option_name is
1216 + // always over 32 characters.
1217 + $wpdb->query("
1218 + DELETE FROM {$wpdb->options}
1219 + WHERE LOCATE('rss_', option_name) AND LENGTH(option_name) > 32
1220 + ");
1221 + } /* FeedWordPress::clear_cache () */
1222 +
1106 1223 function magpie_version () {
1107 1224 if (!defined('MAGPIE_VERSION')) : $magpie_version = $GLOBALS['wp_version'].'-default';
1108 1225 else : $magpie_version = MAGPIE_VERSION;
1109 1226 endif;