| @@ -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: 2010.0127 | |
| 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', '2010.0127'); | |
| 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]/'); |
| @@ -52,8 +59,9 @@ | ||
| 52 | 59 | define ('FWP_SCHEMA_25', 7558); // Database schema # for WP 2.5 |
| 53 | 60 | define ('FWP_SCHEMA_26', 8201); // Database schema # for WP 2.6 |
| 54 | 61 | define ('FWP_SCHEMA_27', 9872); // Database schema # for WP 2.7 |
| 55 | 62 | define ('FWP_SCHEMA_28', 11548); // Database schema # for WP 2.8 |
| 63 | +define ('FWP_SCHEMA_29', 12329); // Database schema # for WP 2.9 | |
| 56 | 64 | |
| 57 | 65 | if (FEEDWORDPRESS_DEBUG) : |
| 58 | 66 | // Help us to pick out errors, if any. |
| 59 | 67 | ini_set('error_reporting', E_ALL & ~E_NOTICE); |
| @@ -67,8 +75,10 @@ | ||
| 67 | 75 | // used for more than testing purposes! |
| 68 | 76 | define('MAGPIE_CACHE_AGE', 1); |
| 69 | 77 | else : |
| 70 | 78 | define('MAGPIE_DEBUG', false); |
| 79 | + | |
| 80 | + define('MAGPIE_CACHE_AGE', 1*60); | |
| 71 | 81 | endif; |
| 72 | 82 | |
| 73 | 83 | // Note that the rss-functions.php that comes prepackaged with WordPress is |
| 74 | 84 | // old & busted. For the new hotness, drop a copy of rss.php from |
| @@ -112,13 +122,20 @@ | ||
| 112 | 122 | else : // Something went wrong. Let's just guess. |
| 113 | 123 | $fwp_path = 'feedwordpress'; |
| 114 | 124 | endif; |
| 115 | 125 | |
| 126 | +function feedwordpress_admin_scripts () { | |
| 127 | + wp_enqueue_script('post'); // for magic tag and category boxes | |
| 128 | + wp_enqueue_script('admin-forms'); // for checkbox selection | |
| 129 | +} | |
| 130 | + | |
| 116 | 131 | // If this is a FeedWordPress admin page, queue up scripts for AJAX functions that FWP uses |
| 117 | 132 | // If it is a display page or a non-FeedWordPress admin page, don't. |
| 118 | 133 | if (is_admin() and isset($_REQUEST['page']) and preg_match("|^{$fwp_path}/|", $_REQUEST['page'])) : |
| 119 | 134 | if (function_exists('wp_enqueue_script')) : |
| 120 | - if (isset($wp_db_version) and $wp_db_version >= FWP_SCHEMA_25) : | |
| 135 | + if (FeedWordPressCompatibility::test_version(FWP_SCHEMA_29)) : | |
| 136 | + add_action('admin_print_scripts', 'feedwordpress_admin_scripts'); | |
| 137 | + elseif (FeedWordPressCompatibility::test_version(FWP_SCHEMA_25)) : | |
| 121 | 138 | wp_enqueue_script('post'); // for magic tag and category boxes |
| 122 | 139 | wp_enqueue_script('thickbox'); // for fold-up boxes |
| 123 | 140 | wp_enqueue_script('admin-forms'); // for checkbox selection |
| 124 | 141 | else : |
| @@ -170,9 +187,13 @@ | ||
| 170 | 187 | # Admin menu |
| 171 | 188 | add_action('admin_menu', 'fwp_add_pages'); |
| 172 | 189 | add_action('admin_notices', 'fwp_check_debug'); |
| 173 | 190 | add_action('admin_notices', 'fwp_check_magpie'); |
| 191 | + add_action('init', 'feedwordpress_check_for_magpie_fix'); | |
| 174 | 192 | |
| 193 | + add_action('admin_menu', 'feedwordpress_add_post_edit_controls'); | |
| 194 | + add_action('save_post', 'feedwordpress_save_post_edit_controls'); | |
| 195 | + | |
| 175 | 196 | # Inbound XML-RPC update methods |
| 176 | 197 | add_filter('xmlrpc_methods', 'feedwordpress_xmlrpc_hook'); |
| 177 | 198 | |
| 178 | 199 | # Outbound XML-RPC ping reform |
| @@ -193,13 +214,26 @@ | ||
| 193 | 214 | add_action('feedwordpress_update', 'log_feedwordpress_update_feeds', 100); |
| 194 | 215 | add_action('feedwordpress_check_feed', 'log_feedwordpress_check_feed', 100); |
| 195 | 216 | add_action('feedwordpress_update_complete', 'log_feedwordpress_update_complete', 100); |
| 196 | 217 | endif; |
| 197 | - | |
| 218 | + | |
| 219 | + if (FeedWordPress::update_requested() and FEEDWORDPRESS_DEBUG) : | |
| 220 | + add_action('post_syndicated_item', 'debug_out_feedwordpress_post', 100); | |
| 221 | + add_action('update_syndicated_item', 'debug_out_feedwordpress_update_post', 100); | |
| 222 | + add_action('feedwordpress_update', 'debug_out_feedwordpress_update_feeds', 100); | |
| 223 | + add_action('feedwordpress_check_feed', 'debug_out_feedwordpress_check_feed', 100); | |
| 224 | + add_action('feedwordpress_update_complete', 'debug_out_feedwordpress_update_complete', 100); | |
| 225 | + endif; | |
| 226 | + | |
| 198 | 227 | # Cron-less auto-update. Hooray! |
| 199 | - add_action('init', 'feedwordpress_auto_update'); | |
| 200 | - add_action('init', 'feedwordpress_check_for_magpie_fix'); | |
| 201 | - | |
| 228 | + $autoUpdateHook = get_option('feedwordpress_automatic_updates'); | |
| 229 | + if ($autoUpdateHook != 'init') : | |
| 230 | + $autoUpdateHook = 'shutdown'; | |
| 231 | + endif; | |
| 232 | + add_action($autoUpdateHook, 'feedwordpress_auto_update'); | |
| 233 | + | |
| 234 | + add_action('init', 'feedwordpress_update_magic_url'); | |
| 235 | + | |
| 202 | 236 | # Default sanitizers |
| 203 | 237 | add_filter('syndicated_item_content', array('SyndicatedPost', 'resolve_relative_uris'), 0, 2); |
| 204 | 238 | add_filter('syndicated_item_content', array('SyndicatedPost', 'sanitize_content'), 0, 2); |
| 205 | 239 | |
| @@ -220,8 +254,16 @@ | ||
| 220 | 254 | elseif (isset($_POST['upgrade'])) : |
| 221 | 255 | $source = dirname(__FILE__)."/MagpieRSS-upgrade/rss.php"; |
| 222 | 256 | $destination = ABSPATH . WPINC . '/rss.php'; |
| 223 | 257 | $success = @copy($source, $destination); |
| 258 | + | |
| 259 | + // Copy over rss-functions.php, too, to avoid collisions | |
| 260 | + // on pre-lapsarian versions of WordPress. | |
| 261 | + if ($success) : | |
| 262 | + $source = dirname(__FILE__)."/MagpieRSS-upgrade/rss-functions.php"; | |
| 263 | + $destination = ABSPATH . WPINC . '/rss-functions.php'; | |
| 264 | + $success = @copy($source, $destination); | |
| 265 | + endif; | |
| 224 | 266 | $ret = (int) $success; |
| 225 | 267 | endif; |
| 226 | 268 | |
| 227 | 269 | if (strpos($back_to, '?')===false) : $sep = '?'; |
| @@ -237,13 +279,43 @@ | ||
| 237 | 279 | if (FeedWordPress::stale()) : |
| 238 | 280 | $feedwordpress =& new FeedWordPress; |
| 239 | 281 | $feedwordpress->update(); |
| 240 | 282 | endif; |
| 241 | - | |
| 283 | +} /* feedwordpress_auto_update () */ | |
| 284 | + | |
| 285 | +function feedwordpress_update_magic_url () { | |
| 286 | + global $wpdb; | |
| 287 | + | |
| 288 | + // Explicit update request in the HTTP request (e.g. from a cron job) | |
| 242 | 289 | if (FeedWordPress::update_requested()) : |
| 290 | + $feedwordpress =& new FeedWordPress; | |
| 291 | + $feedwordpress->update(FeedWordPress::update_requested_url()); | |
| 292 | + | |
| 293 | + if (FEEDWORDPRESS_DEBUG and count($wpdb->queries) > 0) : | |
| 294 | + $mysqlTime = 0.0; | |
| 295 | + $byTime = array(); | |
| 296 | + foreach ($wpdb->queries as $query) : | |
| 297 | + $time = $query[1] * 1000000.0; | |
| 298 | + $mysqlTime += $query[1]; | |
| 299 | + if (!isset($byTime[$time])) : $byTime[$time] = array(); endif; | |
| 300 | + $byTime[$time][] = $query[0]. ' // STACK: ' . $query[2]; | |
| 301 | + endforeach; | |
| 302 | + krsort($byTime); | |
| 303 | + | |
| 304 | + foreach ($byTime as $time => $querySet) : | |
| 305 | + foreach ($querySet as $query) : | |
| 306 | + print "[".(sprintf('%4.4f', $time/1000.0)) . "ms] $query\n"; | |
| 307 | + endforeach; | |
| 308 | + endforeach; | |
| 309 | + echo "[feedwordpress] $wpdb->num_queries queries. $mysqlTime seconds in MySQL. Total of "; timer_stop(1); print " seconds."; | |
| 310 | + endif; | |
| 311 | + | |
| 312 | + | |
| 313 | + // Magic URL should return nothing but a 200 OK header packet | |
| 314 | + // when successful. | |
| 243 | 315 | exit; |
| 244 | 316 | endif; |
| 245 | -} /* feedwordpress_auto_update () */ | |
| 317 | +} /* feedwordpress_magic_update_url () */ | |
| 246 | 318 | |
| 247 | 319 | ################################################################################ |
| 248 | 320 | ## LOGGING FUNCTIONS: log status updates to error_log if you want it ########### |
| 249 | 321 | ################################################################################ |
| @@ -280,23 +352,56 @@ | ||
| 280 | 352 | .(is_null($delta) ? "Error: I don't syndicate that URI" |
| 281 | 353 | : implode(' and ', $mesg))); |
| 282 | 354 | } |
| 283 | 355 | |
| 356 | +function debug_out_feedwordpress_post ($id) { | |
| 357 | + $post = wp_get_single_post($id); | |
| 358 | + print ("[".date('Y-m-d H:i:s')."][feedwordpress] posted " | |
| 359 | + ."'{$post->post_title}' ({$post->post_date})\n"); | |
| 360 | +} | |
| 361 | + | |
| 362 | +function debug_out_feedwordpress_update_post ($id) { | |
| 363 | + $post = wp_get_single_post($id); | |
| 364 | + print ("[".date('Y-m-d H:i:s')."][feedwordpress] updated " | |
| 365 | + ."'{$post->post_title}' ({$post->post_date})" | |
| 366 | + ." (as of {$post->post_modified})\n"); | |
| 367 | +} | |
| 368 | + | |
| 369 | +function debug_out_feedwordpress_update_feeds ($uri) { | |
| 370 | + print ("[".date('Y-m-d H:i:s')."][feedwordpress] update('$uri')\n"); | |
| 371 | +} | |
| 372 | + | |
| 373 | +function debug_out_feedwordpress_check_feed ($feed) { | |
| 374 | + $uri = $feed['link/uri']; $name = $feed['link/name']; | |
| 375 | + print ("[".date('Y-m-d H:i:s')."][feedwordpress] Examining $name <$uri>\n"); | |
| 376 | +} | |
| 377 | + | |
| 378 | +function debug_out_feedwordpress_update_complete ($delta) { | |
| 379 | + $mesg = array(); | |
| 380 | + if (isset($delta['new'])) $mesg[] = 'added '.$delta['new'].' new posts'; | |
| 381 | + if (isset($delta['updated'])) $mesg[] = 'updated '.$delta['updated'].' existing posts'; | |
| 382 | + if (empty($mesg)) $mesg[] = 'nothing changed'; | |
| 383 | + | |
| 384 | + print ("[".date('Y-m-d H:i:s')."][feedwordpress] " | |
| 385 | + .(is_null($delta) ? "Error: I don't syndicate that URI" | |
| 386 | + : implode(' and ', $mesg))."\n"); | |
| 387 | +} | |
| 388 | + | |
| 284 | 389 | ################################################################################ |
| 285 | 390 | ## TEMPLATE API: functions to make your templates syndication-aware ############ |
| 286 | 391 | ################################################################################ |
| 287 | 392 | |
| 288 | -function is_syndicated () { return (strlen(get_syndication_feed_id()) > 0); } | |
| 393 | +function is_syndicated ($id = NULL) { return (strlen(get_syndication_feed_id($id)) > 0); } | |
| 289 | 394 | |
| 290 | -function get_syndication_source_link ($original = NULL) { | |
| 395 | +function get_syndication_source_link ($original = NULL, $id = NULL) { | |
| 291 | 396 | if (is_null($original)) : $original = FeedWordPress::use_aggregator_source_data(); |
| 292 | 397 | endif; |
| 293 | 398 | |
| 294 | - if ($original) : $vals = get_post_custom_values('syndication_source_uri_original'); | |
| 399 | + if ($original) : $vals = get_post_custom_values('syndication_source_uri_original', $id); | |
| 295 | 400 | else : $vals = array(); |
| 296 | 401 | endif; |
| 297 | 402 | |
| 298 | - if (count($vals) == 0) : $vals = get_post_custom_values('syndication_source_uri'); | |
| 403 | + if (count($vals) == 0) : $vals = get_post_custom_values('syndication_source_uri', $id); | |
| 299 | 404 | endif; |
| 300 | 405 | |
| 301 | 406 | if (count($vals) > 0) : $ret = $vals[0]; else : $ret = NULL; endif; |
| 302 | 407 | |
| @@ -302,37 +407,71 @@ | ||
| 302 | 407 | |
| 303 | 408 | return $ret; |
| 304 | 409 | } /* function get_syndication_source_link() */ |
| 305 | 410 | |
| 306 | -function the_syndication_source_link ($original = NULL) { echo get_syndication_source_link($original); } | |
| 411 | +function the_syndication_source_link ($original = NULL, $id = NULL) { | |
| 412 | + echo get_syndication_source_link($original, $id); | |
| 413 | +} | |
| 307 | 414 | |
| 308 | -function get_syndication_source ($original = NULL) { | |
| 309 | - if (is_null($original)) : $original = FeedWordPress::use_aggregator_source_data(); | |
| 415 | +function feedwordpress_display_url ($url, $before = 60, $after = 0) { | |
| 416 | + $bits = parse_url($url); | |
| 417 | + | |
| 418 | + // Strip out crufty subdomains | |
| 419 | + $bits['host'] = preg_replace('/^www[0-9]*\./i', '', $bits['host']); | |
| 420 | + | |
| 421 | + // Reassemble bit-by-bit with minimum of crufty elements | |
| 422 | + $url = (isset($bits['user'])?$bits['user'].'@':'') | |
| 423 | + .(isset($bits['host'])?$bits['host']:'') | |
| 424 | + .(isset($bits['path'])?$bits['path']:'') | |
| 425 | + .(isset($bits['query'])?'?'.$bits['query']:''); | |
| 426 | + | |
| 427 | + if (strlen($url) > ($before+$after)) : | |
| 428 | + $url = substr($url, 0, $before).'…'.substr($url, 0 - $after, $after); | |
| 310 | 429 | endif; |
| 311 | 430 | |
| 312 | - if ($original) : $vals = get_post_custom_values('syndication_source_original'); | |
| 313 | - else : $vals = array(); | |
| 431 | + return $url; | |
| 432 | +} | |
| 433 | + | |
| 434 | +function get_syndication_source ($original = NULL, $id = NULL) { | |
| 435 | + if (is_null($original)) : | |
| 436 | + $original = FeedWordPress::use_aggregator_source_data(); | |
| 314 | 437 | endif; |
| 438 | + | |
| 439 | + if ($original) : | |
| 440 | + $vals = get_post_custom_values('syndication_source_original', $id); | |
| 441 | + else : | |
| 442 | + $vals = array(); | |
| 443 | + endif; | |
| 315 | 444 | |
| 316 | - if (count($vals) == 0) : $vals = get_post_custom_values('syndication_source'); | |
| 445 | + if (count($vals) == 0) : | |
| 446 | + $vals = get_post_custom_values('syndication_source', $id); | |
| 317 | 447 | endif; |
| 318 | 448 | |
| 319 | - if (count($vals) > 0) : $ret = $vals[0]; else : $ret = NULL; endif; | |
| 449 | + if (count($vals) > 0) : | |
| 450 | + $ret = $vals[0]; | |
| 451 | + else : | |
| 452 | + $ret = NULL; | |
| 453 | + endif; | |
| 320 | 454 | |
| 455 | + if (is_null($ret) or strlen(trim($ret)) == 0) : | |
| 456 | + // Fall back to URL of blog | |
| 457 | + $ret = feedwordpress_display_url(get_syndication_source_link()); | |
| 458 | + endif; | |
| 459 | + | |
| 321 | 460 | return $ret; |
| 322 | 461 | } /* function get_syndication_source() */ |
| 323 | 462 | |
| 324 | -function the_syndication_source ($original = NULL) { echo get_syndication_source($original); } | |
| 463 | +function the_syndication_source ($original = NULL, $id = NULL) { echo get_syndication_source($original, $id); } | |
| 325 | 464 | |
| 326 | -function get_syndication_feed ($original = NULL) { | |
| 465 | +function get_syndication_feed ($original = NULL, $id = NULL) { | |
| 327 | 466 | if (is_null($original)) : $original = FeedWordPress::use_aggregator_source_data(); |
| 328 | 467 | endif; |
| 329 | 468 | |
| 330 | - if ($original) : $vals = get_post_custom_values('syndication_feed_original'); | |
| 469 | + if ($original) : $vals = get_post_custom_values('syndication_feed_original', $id); | |
| 331 | 470 | else : $vals = array(); |
| 332 | 471 | endif; |
| 333 | - | |
| 334 | - if (count($vals) == 0) : $vals = get_post_custom_values('syndication_feed'); | |
| 472 | + | |
| 473 | + if (count($vals) == 0) : $vals = get_post_custom_values('syndication_feed', $id); | |
| 335 | 474 | endif; |
| 336 | 475 | |
| 337 | 476 | if (count($vals) > 0) : $ret = $vals[0]; else : $ret = NULL; endif; |
| 338 | 477 | |
| @@ -338,19 +477,19 @@ | ||
| 338 | 477 | |
| 339 | 478 | return $ret; |
| 340 | 479 | } /* function get_syndication_feed() */ |
| 341 | 480 | |
| 342 | -function the_syndication_feed ($original = NULL) { echo get_syndication_feed ($original); } | |
| 481 | +function the_syndication_feed ($original = NULL, $id = NULL) { echo get_syndication_feed($original, $id); } | |
| 343 | 482 | |
| 344 | -function get_syndication_feed_guid ($original = NULL) { | |
| 483 | +function get_syndication_feed_guid ($original = NULL, $id = NULL) { | |
| 345 | 484 | if (is_null($original)) : $original = FeedWordPress::use_aggregator_source_data(); |
| 346 | 485 | endif; |
| 347 | 486 | |
| 348 | - if ($original) : $vals = get_post_custom_values('syndication_source_id_original'); | |
| 487 | + if ($original) : $vals = get_post_custom_values('syndication_source_id_original', $id); | |
| 349 | 488 | else : $vals = array(); |
| 350 | 489 | endif; |
| 351 | 490 | |
| 352 | - if (count($vals) == 0) : $vals = array(get_feed_meta('feed/id')); | |
| 491 | + if (count($vals) == 0) : $vals = array(get_feed_meta('feed/id', $id)); | |
| 353 | 492 | endif; |
| 354 | 493 | |
| 355 | 494 | if (count($vals) > 0) : $ret = $vals[0]; else : $ret = NULL; endif; |
| 356 | 495 | |
| @@ -356,18 +495,18 @@ | ||
| 356 | 495 | |
| 357 | 496 | return $ret; |
| 358 | 497 | } /* function get_syndication_feed_guid () */ |
| 359 | 498 | |
| 360 | -function the_syndication_feed_guid ($original = NULL) { echo get_syndication_feed_guid($original); } | |
| 499 | +function the_syndication_feed_guid ($original = NULL, $id = NULL) { echo get_syndication_feed_guid($original, $id); } | |
| 361 | 500 | |
| 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(); } | |
| 501 | +function get_syndication_feed_id ($id = NULL) { list($u) = get_post_custom_values('syndication_feed_id', $id); return $u; } | |
| 502 | +function the_syndication_feed_id ($id = NULL) { echo get_syndication_feed_id($id); } | |
| 364 | 503 | |
| 365 | 504 | $feedwordpress_linkcache = array (); // only load links from database once |
| 366 | 505 | |
| 367 | -function get_feed_meta ($key) { | |
| 506 | +function get_feed_meta ($key, $id = NULL) { | |
| 368 | 507 | global $wpdb, $feedwordpress_linkcache; |
| 369 | - $feed_id = get_syndication_feed_id(); | |
| 508 | + $feed_id = get_syndication_feed_id($id); | |
| 370 | 509 | |
| 371 | 510 | $ret = NULL; |
| 372 | 511 | if (strlen($feed_id) > 0): |
| 373 | 512 | if (isset($feedwordpress_linkcache[$feed_id])) : |
| @@ -381,13 +520,13 @@ | ||
| 381 | 520 | endif; |
| 382 | 521 | return $ret; |
| 383 | 522 | } /* get_feed_meta() */ |
| 384 | 523 | |
| 385 | -function get_syndication_permalink () { | |
| 386 | - list($u) = get_post_custom_values('syndication_permalink'); return $u; | |
| 524 | +function get_syndication_permalink ($id = NULL) { | |
| 525 | + list($u) = get_post_custom_values('syndication_permalink', $id); return $u; | |
| 387 | 526 | } |
| 388 | -function the_syndication_permalink () { | |
| 389 | - echo get_syndication_permalink(); | |
| 527 | +function the_syndication_permalink ($id = NULL) { | |
| 528 | + echo get_syndication_permalink($id); | |
| 390 | 529 | } |
| 391 | 530 | |
| 392 | 531 | ################################################################################ |
| 393 | 532 | ## FILTERS: syndication-aware handling of post data for templates and feeds #### |
| @@ -424,16 +563,16 @@ | ||
| 424 | 563 | // In a post context.... |
| 425 | 564 | if (is_syndicated()) : |
| 426 | 565 | ?> |
| 427 | 566 | <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(); ?>" /> | |
| 567 | + <title><?php print htmlspecialchars(get_syndication_source()); ?></title> | |
| 568 | + <link rel="alternate" type="text/html" href="<?php print htmlspecialchars(get_syndication_source_link()); ?>" /> | |
| 569 | + <link rel="self" href="<?php print htmlspecialchars(get_syndication_feed()); ?>" /> | |
| 431 | 570 | <?php |
| 432 | 571 | $id = get_syndication_feed_guid(); |
| 433 | 572 | if (strlen($id) > 0) : |
| 434 | 573 | ?> |
| 435 | - <id><?php print $id; ?></id> | |
| 574 | + <id><?php print htmlspecialchars($id); ?></id> | |
| 436 | 575 | <?php |
| 437 | 576 | endif; |
| 438 | 577 | $updated = get_feed_meta('feed/updated'); |
| 439 | 578 | if (strlen($updated) > 0) : ?> |
| @@ -468,23 +607,14 @@ | ||
| 468 | 607 | // add icon parameter |
| 469 | 608 | $menu[] = WP_PLUGIN_URL.'/'.$fwp_path.'/feedwordpress-tiny.png'; |
| 470 | 609 | endif; |
| 471 | 610 | |
| 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 | 611 | 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'); | |
| 612 | + add_submenu_page($fwp_path.'/syndication.php', 'Syndicated Feeds & Updates', 'Feeds & Updates', $fwp_capability['manage_options'], $fwp_path.'/feeds-page.php'); | |
| 613 | + add_submenu_page($fwp_path.'/syndication.php', 'Syndicated Posts & Links', 'Posts & Links', $fwp_capability['manage_options'], $fwp_path.'/posts-page.php'); | |
| 482 | 614 | 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'); | |
| 615 | + add_submenu_page($fwp_path.'/syndication.php', 'Categories'.FEEDWORDPRESS_AND_TAGS, 'Categories'.FEEDWORDPRESS_AND_TAGS, $fwp_capability['manage_options'], $fwp_path.'/categories-page.php'); | |
| 616 | + add_submenu_page($fwp_path.'/syndication.php', 'FeedWordPress Back End', 'Back End', $fwp_capability['manage_options'], $fwp_path.'/backend-page.php'); | |
| 487 | 617 | } /* function fwp_add_pages () */ |
| 488 | 618 | |
| 489 | 619 | function fwp_check_debug () { |
| 490 | 620 | // This is a horrible fucking kludge that I have to do because the |
| @@ -507,9 +637,9 @@ | ||
| 507 | 637 | <?php |
| 508 | 638 | endif; |
| 509 | 639 | } /* function fwp_check_debug () */ |
| 510 | 640 | |
| 511 | -define('EXPECTED_MAGPIE_VERSION', '2009.0618'); | |
| 641 | +define('EXPECTED_MAGPIE_VERSION', '2010.0122'); | |
| 512 | 642 | function fwp_check_magpie () { |
| 513 | 643 | if (isset($_REQUEST['feedwordpress_magpie_fix'])) : |
| 514 | 644 | if ($_REQUEST['feedwordpress_magpie_fix']=='ignored') : |
| 515 | 645 | ?> |
| @@ -653,8 +783,65 @@ | ||
| 653 | 783 | endif; |
| 654 | 784 | endif; |
| 655 | 785 | } |
| 656 | 786 | |
| 787 | + function feedwordpress_add_post_edit_controls () { | |
| 788 | + add_meta_box('feedwordpress-post-controls', __('Syndication'), 'feedwordpress_post_edit_controls', 'post', 'side', 'high'); | |
| 789 | + } // function FeedWordPress::postEditControls | |
| 790 | + | |
| 791 | + function feedwordpress_post_edit_controls () { | |
| 792 | + global $post; | |
| 793 | + | |
| 794 | + $frozen_values = get_post_custom_values('_syndication_freeze_updates', $post->ID); | |
| 795 | + $frozen_post = (count($frozen_values) > 0 and 'yes' == $frozen_values[0]); | |
| 796 | + | |
| 797 | + if (is_syndicated($post->ID)) : | |
| 798 | + ?> | |
| 799 | + <p>This is a syndicated post, which originally appeared at | |
| 800 | + <cite><?php print wp_specialchars(get_syndication_source(NULL, $post->ID)); ?></cite>. | |
| 801 | + <a href="<?php print wp_specialchars(get_syndication_permalink($post->ID)); ?>">View original post</a>.</p> | |
| 802 | + | |
| 803 | + <p><input type="hidden" name="feedwordpress_noncename" id="feedwordpress_noncename" value="<?php print wp_create_nonce(plugin_basename(__FILE__)); ?>" /> | |
| 804 | + <label><input type="checkbox" name="freeze_updates" value="yes" <?php if ($frozen_post) : ?>checked="checked"<?php endif; ?> /> <strong>Manual editing.</strong> | |
| 805 | + If set, FeedWordPress will not overwrite the changes you make manually | |
| 806 | + to this post, if the syndicated content is updated on the | |
| 807 | + feed.</label></p> | |
| 808 | + <?php | |
| 809 | + else : | |
| 810 | + ?> | |
| 811 | + <p>This post was created locally at this website.</p> | |
| 812 | + <?php | |
| 813 | + endif; | |
| 814 | + } // function feedwordpress_post_edit_controls () */ | |
| 815 | + | |
| 816 | + function feedwordpress_save_post_edit_controls ( $post_id ) { | |
| 817 | + global $post; | |
| 818 | + | |
| 819 | + if (!isset($_POST['feedwordpress_noncename']) or !wp_verify_nonce($_POST['feedwordpress_noncename'], plugin_basename(__FILE__))) : | |
| 820 | + return $post_id; | |
| 821 | + endif; | |
| 822 | + | |
| 823 | + // Verify if this is an auto save routine. If it is our form has | |
| 824 | + // not been submitted, so we don't want to do anything. | |
| 825 | + if ( defined('DOING_AUTOSAVE') and DOING_AUTOSAVE ) : | |
| 826 | + return $post_id; | |
| 827 | + endif; | |
| 828 | + | |
| 829 | + // Check permissions | |
| 830 | + if ( !current_user_can( 'edit_'.$_POST['post_type'], $post_id) ) : | |
| 831 | + return $post_id; | |
| 832 | + endif; | |
| 833 | + | |
| 834 | + // OK, we're golden. Now let's save some data. | |
| 835 | + if (isset($_POST['freeze_updates'])) : | |
| 836 | + update_post_meta($post_id, '_syndication_freeze_updates', $_POST['freeze_updates']); | |
| 837 | + else : | |
| 838 | + delete_post_meta($post_id, '_syndication_freeze_updates'); | |
| 839 | + endif; | |
| 840 | + | |
| 841 | + return $_POST['freeze_updates']; | |
| 842 | + } // function feedwordpress_save_edit_controls | |
| 843 | + | |
| 657 | 844 | ################################################################################ |
| 658 | 845 | ## class FeedWordPress ######################################################### |
| 659 | 846 | ################################################################################ |
| 660 | 847 | |
| @@ -786,9 +973,9 @@ | ||
| 786 | 973 | if (!is_null($crash_ts) and (time() > $crash_ts)) : // Check whether we've exceeded the time limit |
| 787 | 974 | break; |
| 788 | 975 | endif; |
| 789 | 976 | |
| 790 | - $pinged_that = (is_null($uri) or in_array($uri, array($feed->uri(), $feed->homepage()))); | |
| 977 | + $pinged_that = (is_null($uri) or ($uri=='*') or in_array($uri, array($feed->uri(), $feed->homepage()))); | |
| 791 | 978 | |
| 792 | 979 | if (!is_null($uri)) : // A site-specific ping always updates |
| 793 | 980 | $timely = true; |
| 794 | 981 | else : |
| @@ -816,8 +1003,11 @@ | ||
| 816 | 1003 | } |
| 817 | 1004 | |
| 818 | 1005 | function stale () { |
| 819 | 1006 | if (get_option('feedwordpress_automatic_updates')) : |
| 1007 | + // Do our best to avoid possible simultaneous | |
| 1008 | + // updates by getting up-to-the-minute settings. | |
| 1009 | + | |
| 820 | 1010 | $last = get_option('feedwordpress_last_update_all'); |
| 821 | 1011 | |
| 822 | 1012 | // If we haven't updated all yet, give it a time window |
| 823 | 1013 | if (false === $last) : |
| @@ -836,12 +1026,8 @@ | ||
| 836 | 1026 | else : |
| 837 | 1027 | FeedWordPress::critical_bug('FeedWordPress::stale::last', $last, __LINE__); |
| 838 | 1028 | endif; |
| 839 | 1029 | |
| 840 | - // Explicit request for an update (e.g. from a cron job). | |
| 841 | - elseif (FeedWordPress::update_requested()) : | |
| 842 | - $ret = true; | |
| 843 | - | |
| 844 | 1030 | else : |
| 845 | 1031 | $ret = false; |
| 846 | 1032 | endif; |
| 847 | 1033 | return $ret; |
| @@ -847,11 +1033,25 @@ | ||
| 847 | 1033 | return $ret; |
| 848 | 1034 | } // FeedWordPress::stale() |
| 849 | 1035 | |
| 850 | 1036 | function update_requested () { |
| 851 | - return (isset($_REQUEST['update_feedwordpress']) and $_REQUEST['update_feedwordpress']); | |
| 1037 | + return ( | |
| 1038 | + isset($_REQUEST['update_feedwordpress']) | |
| 1039 | + and $_REQUEST['update_feedwordpress'] | |
| 1040 | + ); | |
| 852 | 1041 | } // FeedWordPress::update_requested() |
| 853 | 1042 | |
| 1043 | + function update_requested_url () { | |
| 1044 | + $ret = null; | |
| 1045 | + | |
| 1046 | + if (($_REQUEST['update_feedwordpress']=='*') | |
| 1047 | + or (preg_match('|^http://.*|i', $_REQUEST['update_feedwordpress']))) : | |
| 1048 | + $ret = $_REQUEST['update_feedwordpress']; | |
| 1049 | + endif; | |
| 1050 | + | |
| 1051 | + return $ret; | |
| 1052 | + } // FeedWordPress::update_requested_url() | |
| 1053 | + | |
| 854 | 1054 | function syndicate_link ($name, $uri, $rss) { |
| 855 | 1055 | global $wpdb; |
| 856 | 1056 | |
| 857 | 1057 | // Get the category ID# |
| @@ -859,16 +1059,29 @@ | ||
| 859 | 1059 | |
| 860 | 1060 | // WordPress gets cranky if there's no homepage URI |
| 861 | 1061 | if (!isset($uri) or strlen($uri)<1) : $uri = $rss; endif; |
| 862 | 1062 | |
| 863 | - if (function_exists('wp_insert_link')) { // WordPress 2.x | |
| 1063 | + if (function_exists('wp_insert_link')) : // WordPress 2.x | |
| 1064 | + if (FeedWordPressCompatibility::test_version(0, FWP_SCHEMA_21)) : | |
| 1065 | + // Morons. | |
| 1066 | + $name = $wpdb->escape($name); | |
| 1067 | + $uri = $wpdb->escape($uri); | |
| 1068 | + $rss = $wpdb->escape($rss); | |
| 1069 | + | |
| 1070 | + // Comes in as a single category | |
| 1071 | + $linkCats = $cat_id; | |
| 1072 | + else : | |
| 1073 | + // Comes in as an array of categories | |
| 1074 | + $linkCats = array($cat_id); | |
| 1075 | + endif; | |
| 1076 | + | |
| 864 | 1077 | $link_id = wp_insert_link(array( |
| 865 | 1078 | "link_name" => $name, |
| 866 | 1079 | "link_url" => $uri, |
| 867 | - "link_category" => (fwp_test_wp_version(0, FWP_SCHEMA_21) ? $cat_id : array($cat_id)), | |
| 1080 | + "link_category" => $linkCats, | |
| 868 | 1081 | "link_rss" => $rss |
| 869 | 1082 | )); |
| 870 | - } else { // WordPress 1.5.x | |
| 1083 | + else : // WordPress 1.5.x | |
| 871 | 1084 | $result = $wpdb->query(" |
| 872 | 1085 | INSERT INTO $wpdb->links |
| 873 | 1086 | SET |
| 874 | 1087 | link_name = '".$wpdb->escape($name)."', |
| @@ -876,12 +1089,20 @@ | ||
| 876 | 1089 | link_category = '".$wpdb->escape($cat_id)."', |
| 877 | 1090 | link_rss = '".$wpdb->escape($rss)."' |
| 878 | 1091 | "); |
| 879 | 1092 | $link_id = $wpdb->insert_id; |
| 880 | - } // if | |
| 1093 | + endif; | |
| 881 | 1094 | return $link_id; |
| 882 | 1095 | } // function FeedWordPress::syndicate_link() |
| 883 | 1096 | |
| 1097 | + /*static*/ function syndicated_status ($what, $default) { | |
| 1098 | + $ret = get_option("feedwordpress_syndicated_{$what}_status"); | |
| 1099 | + if (!$ret) : | |
| 1100 | + $ret = $default; | |
| 1101 | + endif; | |
| 1102 | + return $ret; | |
| 1103 | + } /* FeedWordPress::syndicated_status() */ | |
| 1104 | + | |
| 884 | 1105 | function on_unfamiliar ($what = 'author', $override = NULL) { |
| 885 | 1106 | $set = array( |
| 886 | 1107 | 'author' => array('create', 'default', 'filter'), |
| 887 | 1108 | 'category' => array('create', 'tag', 'default', 'filter'), |
| @@ -888,8 +1109,10 @@ | ||
| 888 | 1109 | ); |
| 889 | 1110 | |
| 890 | 1111 | if (is_string($override)) : |
| 891 | 1112 | $ret = strtolower($override); |
| 1113 | + else : | |
| 1114 | + $ret = NULL; | |
| 892 | 1115 | endif; |
| 893 | 1116 | |
| 894 | 1117 | if (!is_numeric($override) and !in_array($ret, $set[$what])) : |
| 895 | 1118 | $ret = get_option('feedwordpress_unfamiliar_'.$what); |
| @@ -940,61 +1163,31 @@ | ||
| 940 | 1163 | global $wpdb, $wp_db_version; |
| 941 | 1164 | |
| 942 | 1165 | $cat_id = get_option('feedwordpress_cat_id'); |
| 943 | 1166 | |
| 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 | - // ----------------------------------------- | |
| 1167 | + // If we don't yet have the category ID stored, search by name | |
| 1168 | + if (!$cat_id) : | |
| 1169 | + $cat_id = FeedWordPressCompatibility::link_category_id(DEFAULT_SYNDICATION_CATEGORY); | |
| 950 | 1170 | |
| 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__); | |
| 1171 | + if ($cat_id) : | |
| 1172 | + // We found it; let's stamp it. | |
| 1173 | + update_option('feedwordpress_cat_id', $cat_id); | |
| 963 | 1174 | endif; |
| 964 | 1175 | |
| 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; | |
| 1176 | + // If we *do* have the category ID stored, verify that it exists | |
| 1177 | + else : | |
| 1178 | + $cat_id = FeedWordPressCompatibility::link_category_id((int) $cat_id, 'cat_id'); | |
| 1179 | + endif; | |
| 1180 | + | |
| 1181 | + // If we could not find an appropriate link category, | |
| 1182 | + // make a new one for ourselves. | |
| 1183 | + if (!$cat_id) : | |
| 1184 | + $cat_id = FeedWordPressCompatibility::insert_link_category(DEFAULT_SYNDICATION_CATEGORY); | |
| 994 | 1185 | |
| 1186 | + // Stamp it | |
| 995 | 1187 | update_option('feedwordpress_cat_id', $cat_id); |
| 996 | 1188 | endif; |
| 1189 | + | |
| 997 | 1190 | return $cat_id; |
| 998 | 1191 | } // function FeedWordPress::link_category_id() |
| 999 | 1192 | |
| 1000 | 1193 | # Upgrades and maintenance... |
| @@ -1071,14 +1264,16 @@ | ||
| 1071 | 1264 | $wpdb->query("DROP TABLE tmp_custom_values"); |
| 1072 | 1265 | |
| 1073 | 1266 | // Now fix the guids to avoid duplicate posts |
| 1074 | 1267 | echo "<ul>"; |
| 1075 | - foreach ($this->feeds as $feed) : | |
| 1268 | + foreach ($this->feeds as $syndicatedLink) : | |
| 1269 | + $feed = $syndicatedLink->settings; | |
| 1076 | 1270 | echo "<li>Fixing post meta-data for <cite>".$feed['link/name']."</cite> … "; flush(); |
| 1077 | 1271 | $rss = @fetch_rss($feed['link/uri']); |
| 1078 | 1272 | if (is_array($rss->items)) : |
| 1079 | 1273 | foreach ($rss->items as $item) : |
| 1080 | - $guid = $wpdb->escape(FeedWordPress::guid($item, $feed)); // new GUID algorithm | |
| 1274 | + $post = new SyndicatedPost($item, $syndicatedLink); | |
| 1275 | + $guid = $wpdb->escape($post->guid()); // new GUID algorithm | |
| 1081 | 1276 | $link = $wpdb->escape($item['link']); |
| 1082 | 1277 | |
| 1083 | 1278 | $wpdb->query(" |
| 1084 | 1279 | UPDATE `{$wpdb->posts}` SET guid='{$guid}' WHERE guid='{$link}' |
| @@ -1102,8 +1297,22 @@ | ||
| 1102 | 1297 | CREATE INDEX {$wpdb->posts}_guid_idx ON {$wpdb->posts}(guid) |
| 1103 | 1298 | "); |
| 1104 | 1299 | } /* FeedWordPress::create_guid_index () */ |
| 1105 | 1300 | |
| 1301 | + function clear_cache () { | |
| 1302 | + global $wpdb; | |
| 1303 | + | |
| 1304 | + // MagpieRSS stores its cached feeds in options table rows with | |
| 1305 | + // name = `rss_{md5 of url}` and timestamps for cached feeds in | |
| 1306 | + // table rows with name = `rss_{md5 of url}_ts`. The md5 is | |
| 1307 | + // always 32 characters in length, so the total option_name is | |
| 1308 | + // always over 32 characters. | |
| 1309 | + $wpdb->query(" | |
| 1310 | + DELETE FROM {$wpdb->options} | |
| 1311 | + WHERE LOCATE('rss_', option_name) AND LENGTH(option_name) > 32 | |
| 1312 | + "); | |
| 1313 | + } /* FeedWordPress::clear_cache () */ | |
| 1314 | + | |
| 1106 | 1315 | function magpie_version () { |
| 1107 | 1316 | if (!defined('MAGPIE_VERSION')) : $magpie_version = $GLOBALS['wp_version'].'-default'; |
| 1108 | 1317 | else : $magpie_version = MAGPIE_VERSION; |
| 1109 | 1318 | endif; |