| @@ -32,12 +32,15 @@ | ||
| 32 | 32 | # |
| 33 | 33 | # Values of keys in link_notes are accessible from templates using |
| 34 | 34 | # the function `get_feed_meta($key)` if this plugin is activated. |
| 35 | 35 | |
| 36 | +require_once(dirname(__FILE__).'/magpiefromsimplepie.class.php'); | |
| 37 | + | |
| 36 | 38 | class SyndicatedLink { |
| 37 | 39 | var $id = null; |
| 38 | 40 | var $link = null; |
| 39 | 41 | var $settings = array (); |
| 42 | + var $simplepie = null; | |
| 40 | 43 | var $magpie = null; |
| 41 | 44 | |
| 42 | 45 | function SyndicatedLink ($link) { |
| 43 | 46 | global $wpdb; |
| @@ -60,11 +63,12 @@ | ||
| 60 | 63 | if (strlen($this->link->link_rss) > 0) : |
| 61 | 64 | // Read off feed settings from link_notes |
| 62 | 65 | $notes = explode("\n", $this->link->link_notes); |
| 63 | 66 | foreach ($notes as $note): |
| 64 | - list($key, $value) = explode(": ", $note, 2); | |
| 65 | - | |
| 66 | - if (strlen($key) > 0) : | |
| 67 | + $pair = explode(": ", $note, 2); | |
| 68 | + $key = (isset($pair[0]) ? $pair[0] : null); | |
| 69 | + $value = (isset($pair[1]) ? $pair[1] : null); | |
| 70 | + if (!is_null($key) and !is_null($value)) : | |
| 67 | 71 | // Unescape and trim() off the whitespace. |
| 68 | 72 | // Thanks to Ray Lischner for pointing out the |
| 69 | 73 | // need to trim off whitespace. |
| 70 | 74 | $this->settings[$key] = stripcslashes (trim($value)); |
| @@ -96,14 +100,48 @@ | ||
| 96 | 100 | if (!isset($this->settings['cat_split']) and in_array($bits['host'], $tagspacers)) : |
| 97 | 101 | $this->settings['cat_split'] = '\s'; // Whitespace separates multiple tags in del.icio.us RSS feeds |
| 98 | 102 | endif; |
| 99 | 103 | |
| 100 | - if (isset($this->settings['cats'])): | |
| 101 | - $this->settings['cats'] = preg_split(FEEDWORDPRESS_CAT_SEPARATOR_PATTERN, $this->settings['cats']); | |
| 104 | + // Simple lists | |
| 105 | + foreach ($this->imploded_settings() as $what) : | |
| 106 | + if (isset($this->settings[$what])): | |
| 107 | + $this->settings[$what] = explode( | |
| 108 | + FEEDWORDPRESS_CAT_SEPARATOR, | |
| 109 | + $this->settings[$what] | |
| 110 | + ); | |
| 111 | + endif; | |
| 112 | + endforeach; | |
| 113 | + | |
| 114 | + if (isset($this->settings['terms'])) : | |
| 115 | + // Look for new format | |
| 116 | + $this->settings['terms'] = maybe_unserialize($this->settings['terms']); | |
| 117 | + | |
| 118 | + if (!is_array($this->settings['terms'])) : | |
| 119 | + // Deal with old format instead. Ugh. | |
| 120 | + | |
| 121 | + // Split on two *or more* consecutive breaks | |
| 122 | + // because in the old format, a taxonomy | |
| 123 | + // without any associated terms would | |
| 124 | + // produce tax_name#1\n\n\ntax_name#2\nterm, | |
| 125 | + // and the naive split on the first \n\n | |
| 126 | + // would screw up the tax_name#2 list. | |
| 127 | + // | |
| 128 | + // Props to David Morris for pointing this | |
| 129 | + // out. | |
| 130 | + | |
| 131 | + $this->settings['terms'] = preg_split( | |
| 132 | + "/".FEEDWORDPRESS_CAT_SEPARATOR."{2,}/", | |
| 133 | + $this->settings['terms'] | |
| 134 | + ); | |
| 135 | + $terms = array(); | |
| 136 | + foreach ($this->settings['terms'] as $line) : | |
| 137 | + $line = explode(FEEDWORDPRESS_CAT_SEPARATOR, $line); | |
| 138 | + $tax = array_shift($line); | |
| 139 | + $terms[$tax] = $line; | |
| 140 | + endforeach; | |
| 141 | + $this->settings['terms'] = $terms; | |
| 142 | + endif; | |
| 102 | 143 | endif; |
| 103 | - if (isset($this->settings['tags'])): | |
| 104 | - $this->settings['tags'] = preg_split(FEEDWORDPRESS_CAT_SEPARATOR_PATTERN, $this->settings['tags']); | |
| 105 | - endif; | |
| 106 | 144 | |
| 107 | 145 | if (isset($this->settings['map authors'])) : |
| 108 | 146 | $author_rules = explode("\n\n", $this->settings['map authors']); |
| 109 | 147 | $ma = array(); |
| @@ -122,12 +160,14 @@ | ||
| 122 | 160 | endif; |
| 123 | 161 | } /* SyndicatedLink::SyndicatedLink () */ |
| 124 | 162 | |
| 125 | 163 | function found () { |
| 126 | - return is_object($this->link); | |
| 164 | + return is_object($this->link) and !is_wp_error($this->link); | |
| 127 | 165 | } /* SyndicatedLink::found () */ |
| 128 | 166 | |
| 129 | 167 | function stale () { |
| 168 | + global $feedwordpress; | |
| 169 | + | |
| 130 | 170 | $stale = true; |
| 131 | 171 | if (isset($this->settings['update/hold']) and ($this->settings['update/hold']=='ping')) : |
| 132 | 172 | $stale = false; // don't update on any timed updates; pings only |
| 133 | 173 | elseif (isset($this->settings['update/hold']) and ($this->settings['update/hold']=='next')) : |
| @@ -133,8 +173,10 @@ | ||
| 133 | 173 | elseif (isset($this->settings['update/hold']) and ($this->settings['update/hold']=='next')) : |
| 134 | 174 | $stale = true; // update on the next timed update |
| 135 | 175 | elseif (!isset($this->settings['update/ttl']) or !isset($this->settings['update/last'])) : |
| 136 | 176 | $stale = true; // initial update |
| 177 | + elseif ($feedwordpress->force_update_all()) : | |
| 178 | + $stale = true; // forced general updating | |
| 137 | 179 | else : |
| 138 | 180 | $after = ((int) $this->settings['update/last']) |
| 139 | 181 | +((int) $this->settings['update/ttl'] * 60); |
| 140 | 182 | $stale = (time() >= $after); |
| @@ -144,9 +186,26 @@ | ||
| 144 | 186 | |
| 145 | 187 | function poll ($crash_ts = NULL) { |
| 146 | 188 | global $wpdb; |
| 147 | 189 | |
| 148 | - $this->magpie = fetch_rss($this->link->link_rss); | |
| 190 | + $url = $this->uri(array('add_params' => true)); | |
| 191 | + FeedWordPress::diagnostic('updated_feeds', 'Polling feed ['.$url.']'); | |
| 192 | + | |
| 193 | + $timeout = $this->setting('fetch timeout', 'feedwordpress_fetch_timeout', FEEDWORDPRESS_FETCH_TIMEOUT_DEFAULT); | |
| 194 | + | |
| 195 | + $this->simplepie = apply_filters( | |
| 196 | + 'syndicated_feed', | |
| 197 | + FeedWordPress::fetch($url, array('timeout' => $timeout)), | |
| 198 | + $this | |
| 199 | + ); | |
| 200 | + | |
| 201 | + // Filter compatibility mode | |
| 202 | + if (is_wp_error($this->simplepie)) : | |
| 203 | + $this->magpie = $this->simplepie; | |
| 204 | + else : | |
| 205 | + $this->magpie = new MagpieFromSimplePie($this->simplepie, NULL); | |
| 206 | + endif; | |
| 207 | + | |
| 149 | 208 | $new_count = NULL; |
| 150 | 209 | |
| 151 | 210 | $resume = FeedWordPress::affirmative($this->settings, 'update/unfinished'); |
| 152 | 211 | if ($resume) : |
| @@ -156,9 +215,46 @@ | ||
| 156 | 215 | // begin at the beginning |
| 157 | 216 | $processed = array(); |
| 158 | 217 | endif; |
| 159 | 218 | |
| 160 | - if (is_object($this->magpie)) : | |
| 219 | + if (is_wp_error($this->simplepie)) : | |
| 220 | + $new_count = $this->simplepie; | |
| 221 | + // Error; establish an error setting. | |
| 222 | + $theError = array(); | |
| 223 | + $theError['ts'] = time(); | |
| 224 | + $theError['since'] = time(); | |
| 225 | + $theError['object'] = $this->simplepie; | |
| 226 | + | |
| 227 | + $oldError = $this->setting('update/error', NULL, NULL); | |
| 228 | + if (is_string($oldError)) : | |
| 229 | + $oldError = unserialize($oldError); | |
| 230 | + endif; | |
| 231 | + | |
| 232 | + if (!is_null($oldError)) : | |
| 233 | + // Copy over the in-error-since timestamp | |
| 234 | + $theError['since'] = $oldError['since']; | |
| 235 | + | |
| 236 | + // If this is a repeat error, then we should take | |
| 237 | + // a step back before we try to fetch it again. | |
| 238 | + $this->settings['update/last'] = time(); | |
| 239 | + $this->settings['update/ttl'] = $this->automatic_ttl(); | |
| 240 | + $this->settings['update/ttl'] = apply_filters('syndicated_feed_ttl', $this->settings['update/ttl'], $this); | |
| 241 | + $this->settings['update/ttl'] = apply_filters('syndicated_feed_ttl_from_error', $this->settings['update/ttl'], $this); | |
| 242 | + | |
| 243 | + $this->settings['update/timed'] = 'automatically'; | |
| 244 | + endif; | |
| 245 | + | |
| 246 | + do_action('syndicated_feed_error', $theError, $oldError, $this); | |
| 247 | + | |
| 248 | + $this->settings['update/error'] = serialize($theError); | |
| 249 | + $this->save_settings(/*reload=*/ true); | |
| 250 | + | |
| 251 | + elseif (is_object($this->simplepie)) : | |
| 252 | + // Success; clear out error setting, if any. | |
| 253 | + if (isset($this->settings['update/error'])) : | |
| 254 | + unset($this->settings['update/error']); | |
| 255 | + endif; | |
| 256 | + | |
| 161 | 257 | $new_count = array('new' => 0, 'updated' => 0); |
| 162 | 258 | |
| 163 | 259 | # -- Update Link metadata live from feed |
| 164 | 260 | $channel = $this->magpie->channel; |
| @@ -190,11 +286,12 @@ | ||
| 190 | 286 | if (!is_null($ttl)) : |
| 191 | 287 | $this->settings['update/ttl'] = $ttl; |
| 192 | 288 | $this->settings['update/timed'] = 'feed'; |
| 193 | 289 | else : |
| 194 | - $this->settings['update/ttl'] = rand(30, 120); // spread over time interval for staggered updates | |
| 290 | + $this->settings['update/ttl'] = $this->automatic_ttl(); | |
| 195 | 291 | $this->settings['update/timed'] = 'automatically'; |
| 196 | 292 | endif; |
| 293 | + $this->settings['update/ttl'] = apply_filters('syndicated_feed_ttl', $this->settings['update/ttl'], $this); | |
| 197 | 294 | |
| 198 | 295 | if (!isset($this->settings['update/hold']) or $this->settings['update/hold']!='ping') : |
| 199 | 296 | $this->settings['update/hold'] = 'scheduled'; |
| 200 | 297 | endif; |
| @@ -210,15 +307,25 @@ | ||
| 210 | 307 | UPDATE $wpdb->links |
| 211 | 308 | SET $update_set |
| 212 | 309 | WHERE link_id='$this->id' |
| 213 | 310 | "); |
| 311 | + do_action('update_syndicated_feed', $this->id, $this); | |
| 214 | 312 | |
| 215 | 313 | # -- Add new posts from feed and update any updated posts |
| 216 | 314 | $crashed = false; |
| 217 | 315 | |
| 218 | - if (is_array($this->magpie->items)) : | |
| 219 | - foreach ($this->magpie->items as $item) : | |
| 220 | - $post =& new SyndicatedPost($item, $this); | |
| 316 | + $posts = apply_filters( | |
| 317 | + 'syndicated_feed_items', | |
| 318 | + $this->simplepie->get_items(), | |
| 319 | + &$this | |
| 320 | + ); | |
| 321 | + | |
| 322 | + $this->magpie->originals = $posts; | |
| 323 | + | |
| 324 | + if (is_array($posts)) : | |
| 325 | + foreach ($posts as $key => $item) : | |
| 326 | + $post = new SyndicatedPost($item, $this); | |
| 327 | + | |
| 221 | 328 | if (!$resume or !in_array(trim($post->guid()), $processed)) : |
| 222 | 329 | $processed[] = $post->guid(); |
| 223 | 330 | if (!$post->filtered()) : |
| 224 | 331 | $new = $post->store(); |
| @@ -229,11 +336,15 @@ | ||
| 229 | 336 | $crashed = true; |
| 230 | 337 | break; |
| 231 | 338 | endif; |
| 232 | 339 | endif; |
| 340 | + unset($post); | |
| 233 | 341 | endforeach; |
| 234 | 342 | endif; |
| 235 | - | |
| 343 | + $suffix = ($crashed ? 'crashed' : 'completed'); | |
| 344 | + do_action('update_syndicated_feed_items', $this->id, $this); | |
| 345 | + do_action("update_syndicated_feed_items_${suffix}", $this->id, $this); | |
| 346 | + | |
| 236 | 347 | // Copy back any changes to feed settings made in the course of updating (e.g. new author rules) |
| 237 | 348 | $to_notes = $this->settings; |
| 238 | 349 | |
| 239 | 350 | $this->settings['update/processed'] = $processed; |
| @@ -248,29 +359,103 @@ | ||
| 248 | 359 | UPDATE $wpdb->links |
| 249 | 360 | SET $update_set |
| 250 | 361 | WHERE link_id='$this->id' |
| 251 | 362 | "); |
| 363 | + | |
| 364 | + do_action("update_syndicated_feed_completed", $this->id, $this); | |
| 252 | 365 | endif; |
| 253 | 366 | |
| 367 | + // All done; let's clean up. | |
| 368 | + $this->magpie = NULL; | |
| 369 | + | |
| 370 | + // Avoid circular-reference memory leak in PHP < 5.3. | |
| 371 | + // Cf. <http://simplepie.org/wiki/faq/i_m_getting_memory_leaks> | |
| 372 | + if (method_exists($this->simplepie, '__destruct')) : | |
| 373 | + $this->simplepie->__destruct(); | |
| 374 | + endif; | |
| 375 | + $this->simplepie = NULL; | |
| 376 | + | |
| 254 | 377 | return $new_count; |
| 255 | 378 | } /* SyndicatedLink::poll() */ |
| 256 | 379 | |
| 380 | + /** | |
| 381 | + * Updates the URL for the feed syndicated by this link. | |
| 382 | + * | |
| 383 | + * @param string $url The new feed URL to use for this source. | |
| 384 | + * @return bool TRUE on success, FALSE on failure. | |
| 385 | + */ | |
| 386 | + function set_uri ($url) { | |
| 387 | + global $wpdb; | |
| 388 | + | |
| 389 | + if ($this->found()) : | |
| 390 | + // Update link_rss | |
| 391 | + $result = $wpdb->query(" | |
| 392 | + UPDATE $wpdb->links | |
| 393 | + SET | |
| 394 | + link_rss = '".$wpdb->escape($url)."' | |
| 395 | + WHERE link_id = '".$wpdb->escape($this->id)."' | |
| 396 | + "); | |
| 397 | + | |
| 398 | + $ret = ($result ? true : false); | |
| 399 | + else : | |
| 400 | + $ret = false; | |
| 401 | + endif; | |
| 402 | + return $ret; | |
| 403 | + } /* SyndicatedLink::set_uri () */ | |
| 404 | + | |
| 405 | + function deactivate () { | |
| 406 | + global $wpdb; | |
| 407 | + | |
| 408 | + $wpdb->query($wpdb->prepare(" | |
| 409 | + UPDATE $wpdb->links SET link_visible = 'N' WHERE link_id = %d | |
| 410 | + ", (int) $this->id)); | |
| 411 | + } /* SyndicatedLink::deactivate () */ | |
| 412 | + | |
| 413 | + function delete () { | |
| 414 | + global $wpdb; | |
| 415 | + | |
| 416 | + $wpdb->query($wpdb->prepare(" | |
| 417 | + DELETE FROM $wpdb->postmeta WHERE meta_key='syndication_feed_id' | |
| 418 | + AND meta_value = '%s' | |
| 419 | + ", $this->id)); | |
| 420 | + | |
| 421 | + $wpdb->query($wpdb->prepare(" | |
| 422 | + DELETE FROM $wpdb->links WHERE link_id = %d | |
| 423 | + ", (int) $this->id)); | |
| 424 | + | |
| 425 | + $this->id = NULL; | |
| 426 | + } /* SyndicatedLink::delete () */ | |
| 427 | + | |
| 428 | + function nuke () { | |
| 429 | + global $wpdb; | |
| 430 | + | |
| 431 | + // Make a list of the items syndicated from this feed... | |
| 432 | + $post_ids = $wpdb->get_col($wpdb->prepare(" | |
| 433 | + SELECT post_id FROM $wpdb->postmeta | |
| 434 | + WHERE meta_key = 'syndication_feed_id' | |
| 435 | + AND meta_value = '%s' | |
| 436 | + ", $this->id)); | |
| 437 | + | |
| 438 | + // ... and kill them all | |
| 439 | + if (count($post_ids) > 0) : | |
| 440 | + foreach ($post_ids as $post_id) : | |
| 441 | + // Force scrubbing of deleted post | |
| 442 | + // rather than sending to Trashcan | |
| 443 | + wp_delete_post( | |
| 444 | + /*postid=*/ $post_id, | |
| 445 | + /*force_delete=*/ true | |
| 446 | + ); | |
| 447 | + endforeach; | |
| 448 | + endif; | |
| 449 | + | |
| 450 | + $this->delete(); | |
| 451 | + } /* SyndicatedLink::nuke () */ | |
| 452 | + | |
| 257 | 453 | function map_name_to_new_user ($name, $newuser_name) { |
| 258 | 454 | global $wpdb; |
| 259 | 455 | |
| 260 | 456 | if (strlen($newuser_name) > 0) : |
| 261 | - $userdata = array(); | |
| 262 | - $userdata['ID'] = NULL; | |
| 263 | - | |
| 264 | - $userdata['user_login'] = sanitize_user($newuser_name); | |
| 265 | - $userdata['user_login'] = apply_filters('pre_user_login', $userdata['user_login']); | |
| 266 | - | |
| 267 | - $userdata['user_nicename'] = sanitize_title($newuser_name); | |
| 268 | - $userdata['user_nicename'] = apply_filters('pre_user_nicename', $userdata['user_nicename']); | |
| 269 | - | |
| 270 | - $userdata['display_name'] = $wpdb->escape($newuser_name); | |
| 271 | - | |
| 272 | - $newuser_id = wp_insert_user($userdata); | |
| 457 | + $newuser_id = fwp_insert_new_user($newuser_name); | |
| 273 | 458 | if (is_numeric($newuser_id)) : |
| 274 | 459 | if (is_null($name)) : // Unfamiliar author |
| 275 | 460 | $this->settings['unfamiliar author'] = $newuser_id; |
| 276 | 461 | else : |
| @@ -283,8 +468,11 @@ | ||
| 283 | 468 | // TODO: Add some error reporting |
| 284 | 469 | endif; |
| 285 | 470 | } /* SyndicatedLink::map_name_to_new_user () */ |
| 286 | 471 | |
| 472 | + function imploded_settings () { | |
| 473 | + return array('cats', 'tags', 'match/cats', 'match/tags', 'match/filter'); | |
| 474 | + } | |
| 287 | 475 | function settings_to_notes () { |
| 288 | 476 | $to_notes = $this->settings; |
| 289 | 477 | |
| 290 | 478 | unset($to_notes['link/id']); // Magic setting; don't save |
| @@ -297,15 +485,22 @@ | ||
| 297 | 485 | if (isset($to_notes['update/processed']) and (is_array($to_notes['update/processed']))) : |
| 298 | 486 | $to_notes['update/processed'] = implode("\n", $to_notes['update/processed']); |
| 299 | 487 | endif; |
| 300 | 488 | |
| 301 | - if (is_array($to_notes['cats'])) : | |
| 302 | - $to_notes['cats'] = implode(FEEDWORDPRESS_CAT_SEPARATOR, $to_notes['cats']); | |
| 489 | + foreach ($this->imploded_settings() as $what) : | |
| 490 | + if (isset($to_notes[$what]) and is_array($to_notes[$what])) : | |
| 491 | + $to_notes[$what] = implode( | |
| 492 | + FEEDWORDPRESS_CAT_SEPARATOR, | |
| 493 | + $to_notes[$what] | |
| 494 | + ); | |
| 495 | + endif; | |
| 496 | + endforeach; | |
| 497 | + | |
| 498 | + if (isset($to_notes['terms']) and is_array($to_notes['terms'])) : | |
| 499 | + // Serialize it. | |
| 500 | + $to_notes['terms'] = serialize($to_notes['terms']); | |
| 303 | 501 | endif; |
| 304 | - if (is_array($to_notes['tags'])) : | |
| 305 | - $to_notes['tags'] = implode(FEEDWORDPRESS_CAT_SEPARATOR, $to_notes['tags']); | |
| 306 | - endif; | |
| 307 | - | |
| 502 | + | |
| 308 | 503 | // Collapse the author mapping rule structure back into a flat string |
| 309 | 504 | if (isset($to_notes['map authors'])) : |
| 310 | 505 | $ma = array(); |
| 311 | 506 | foreach ($to_notes['map authors'] as $rule_type => $author_rules) : |
| @@ -322,16 +517,160 @@ | ||
| 322 | 517 | endforeach; |
| 323 | 518 | return $notes; |
| 324 | 519 | } /* SyndicatedLink::settings_to_notes () */ |
| 325 | 520 | |
| 326 | - function uri () { | |
| 327 | - return (is_object($this->link) ? $this->link->link_rss : NULL); | |
| 521 | + function save_settings ($reload = false) { | |
| 522 | + global $wpdb; | |
| 523 | + | |
| 524 | + // Save channel-level meta-data | |
| 525 | + foreach (array('link_name', 'link_description', 'link_url') as $what) : | |
| 526 | + $alter[] = "{$what} = '".$wpdb->escape($this->link->{$what})."'"; | |
| 527 | + endforeach; | |
| 528 | + | |
| 529 | + // Save settings to the notes field | |
| 530 | + $alter[] = "link_notes = '".$wpdb->escape($this->settings_to_notes())."'"; | |
| 531 | + | |
| 532 | + // Update the properties of the link from settings changes, etc. | |
| 533 | + $update_set = implode(", ", $alter); | |
| 534 | + | |
| 535 | + $result = $wpdb->query(" | |
| 536 | + UPDATE $wpdb->links | |
| 537 | + SET $update_set | |
| 538 | + WHERE link_id='$this->id' | |
| 539 | + "); | |
| 540 | + | |
| 541 | + if ($reload) : | |
| 542 | + // force reload of link information from DB | |
| 543 | + if (function_exists('clean_bookmark_cache')) : | |
| 544 | + clean_bookmark_cache($this->id); | |
| 545 | + endif; | |
| 546 | + endif; | |
| 547 | + } /* SyndicatedLink::save_settings () */ | |
| 548 | + | |
| 549 | + /** | |
| 550 | + * Retrieves the value of a setting, allowing for a global setting to be | |
| 551 | + * used as a fallback, or a constant value, or both. | |
| 552 | + * | |
| 553 | + * @param string $name The link setting key | |
| 554 | + * @param mixed $fallback_global If the link setting is nonexistent or marked as a use-default value, fall back to the value of this global setting. | |
| 555 | + * @param mixed $fallback_value If the link setting and the global setting are nonexistent or marked as a use-default value, fall back to this constant value. | |
| 556 | + * @return bool TRUE on success, FALSE on failure. | |
| 557 | + */ | |
| 558 | + function setting ($name, $fallback_global = NULL, $fallback_value = NULL, $default = 'default') { | |
| 559 | + $ret = NULL; | |
| 560 | + if (isset($this->settings[$name])) : | |
| 561 | + $ret = $this->settings[$name]; | |
| 562 | + endif; | |
| 563 | + | |
| 564 | + $no_value = ( | |
| 565 | + is_null($ret) | |
| 566 | + or (is_string($ret) and strtolower($ret)==$default) | |
| 567 | + ); | |
| 568 | + | |
| 569 | + if ($no_value and !is_null($fallback_global)) : | |
| 570 | + // Avoid duplication of this correction | |
| 571 | + $fallback_global = preg_replace('/^feedwordpress_/', '', $fallback_global); | |
| 572 | + | |
| 573 | + $ret = get_option('feedwordpress_'.$fallback_global, /*default=*/ NULL); | |
| 574 | + endif; | |
| 575 | + | |
| 576 | + $no_value = ( | |
| 577 | + is_null($ret) | |
| 578 | + or (is_string($ret) and strtolower($ret)==$default) | |
| 579 | + ); | |
| 580 | + | |
| 581 | + if ($no_value and !is_null($fallback_value)) : | |
| 582 | + $ret = $fallback_value; | |
| 583 | + endif; | |
| 584 | + return $ret; | |
| 585 | + } /* SyndicatedLink::setting () */ | |
| 586 | + | |
| 587 | + function update_setting ($name, $value, $default = 'default') { | |
| 588 | + if (!is_null($value) and $value != $default) : | |
| 589 | + $this->settings[$name] = $value; | |
| 590 | + else : // Zap it. | |
| 591 | + unset($this->settings[$name]); | |
| 592 | + endif; | |
| 593 | + } /* SyndicatedLink::update_setting () */ | |
| 594 | + | |
| 595 | + function uri ($params = array()) { | |
| 596 | + $params = shortcode_atts(array( | |
| 597 | + 'add_params' => false, | |
| 598 | + ), $params); | |
| 599 | + | |
| 600 | + $uri = (is_object($this->link) ? $this->link->link_rss : NULL); | |
| 601 | + if (!is_null($uri) and strlen($uri) > 0 and $params['add_params']) : | |
| 602 | + $qp = maybe_unserialize($this->setting('query parameters', array())); | |
| 603 | + | |
| 604 | + // For high-tech HTTP feed request kung fu | |
| 605 | + $qp = apply_filters('syndicated_feed_parameters', $qp, $uri, $this); | |
| 606 | + | |
| 607 | + $q = array(); | |
| 608 | + if (is_array($qp) and count($qp) > 0) : | |
| 609 | + foreach ($qp as $pair) : | |
| 610 | + $q[] = urlencode($pair[0]).'='.urlencode($pair[1]); | |
| 611 | + endforeach; | |
| 612 | + | |
| 613 | + // Are we appending to a URI that already has params? | |
| 614 | + $sep = ((strpos('?', $uri)===false) ? '?' : '&'); | |
| 615 | + | |
| 616 | + // Tack it on | |
| 617 | + $uri .= $sep . implode("&", $q); | |
| 618 | + endif; | |
| 619 | + endif; | |
| 620 | + | |
| 621 | + return $uri; | |
| 328 | 622 | } /* SyndicatedLink::uri () */ |
| 329 | 623 | |
| 330 | - function homepage () { | |
| 331 | - return (isset($this->settings['feed/link']) ? $this->settings['feed/link'] : NULL); | |
| 624 | + function property_cascade ($fromFeed, $link_field, $setting, $simplepie_method) { | |
| 625 | + $value = NULL; | |
| 626 | + if ($fromFeed) : | |
| 627 | + if (isset($this->settings[$setting])) : | |
| 628 | + $value = $this->settings[$setting]; | |
| 629 | + elseif (is_object($this->simplepie) | |
| 630 | + and method_exists($this->simplepie, $simplepie_method)) : | |
| 631 | + $value = $this->simplepie->{$simplepie_method}(); | |
| 632 | + endif; | |
| 633 | + else : | |
| 634 | + $value = $this->link->{$link_field}; | |
| 635 | + endif; | |
| 636 | + return $value; | |
| 637 | + } /* SyndicatedLink::property_cascade () */ | |
| 638 | + | |
| 639 | + function homepage ($fromFeed = true) { | |
| 640 | + return $this->property_cascade($fromFeed, 'link_url', 'feed/link', 'get_link'); | |
| 332 | 641 | } /* SyndicatedLink::homepage () */ |
| 333 | 642 | |
| 643 | + function name ($fromFeed = true) { | |
| 644 | + return $this->property_cascade($fromFeed, 'link_name', 'feed/title', 'get_title'); | |
| 645 | + } /* SyndicatedLink::name () */ | |
| 646 | + | |
| 647 | + function guid () { | |
| 648 | + $ret = $this->setting('feed/id', NULL, $this->uri()); | |
| 649 | + | |
| 650 | + // If we can get it live from the feed, do so. | |
| 651 | + if (is_object($this->simplepie)) : | |
| 652 | + $search = array( | |
| 653 | + array(SIMPLEPIE_NAMESPACE_ATOM_10, 'id'), | |
| 654 | + array(SIMPLEPIE_NAMESPACE_ATOM_03, 'id'), | |
| 655 | + array(SIMPLEPIE_NAMESPACE_RSS_20, 'guid'), | |
| 656 | + array(SIMPLEPIE_NAMESPACE_DC_11, 'identifier'), | |
| 657 | + array(SIMPLEPIE_NAMESPACE_DC_10, 'identifier'), | |
| 658 | + ); | |
| 659 | + | |
| 660 | + foreach ($search as $pair) : | |
| 661 | + if ($id_tags = $this->simplepie->get_feed_tags($pair[0], $pair[1])) : | |
| 662 | + $ret = $id_tags[0]['data']; | |
| 663 | + break; | |
| 664 | + elseif ($id_tags = $this->simplepie->get_channel_tags($pair[0], $pair[1])) : | |
| 665 | + $ret = $id_tags[0]['data']; | |
| 666 | + break; | |
| 667 | + endif; | |
| 668 | + endforeach; | |
| 669 | + endif; | |
| 670 | + return $ret; | |
| 671 | + } | |
| 672 | + | |
| 334 | 673 | function ttl () { |
| 335 | 674 | if (is_object($this->magpie)) : |
| 336 | 675 | $channel = $this->magpie->channel; |
| 337 | 676 | else : |
| @@ -376,8 +715,19 @@ | ||
| 376 | 715 | endif; |
| 377 | 716 | return $ret; |
| 378 | 717 | } /* SyndicatedLink::ttl() */ |
| 379 | 718 | |
| 719 | + function automatic_ttl () { | |
| 720 | + // spread out over a time interval for staggered updates | |
| 721 | + $updateWindow = $this->setting('update/window', 'update_window', DEFAULT_UPDATE_PERIOD); | |
| 722 | + if (!is_numeric($updateWindow) or ($updateWindow < 1)) : | |
| 723 | + $updateWindow = DEFAULT_UPDATE_PERIOD; | |
| 724 | + endif; | |
| 725 | + | |
| 726 | + $fudgedInterval = $updateWindow+rand(0, 2*($updateWindow/3)); | |
| 727 | + return apply_filters('syndicated_feed_automatic_ttl', $fudgedInterval, $this); | |
| 728 | + } /* SyndicatedLink::automatic_ttl () */ | |
| 729 | + | |
| 380 | 730 | // SyndicatedLink::flatten_array (): flatten an array. Useful for |
| 381 | 731 | // hierarchical and namespaced elements. |
| 382 | 732 | // |
| 383 | 733 | // Given an array which may contain array or object elements in it, |
| @@ -415,17 +765,31 @@ | ||
| 415 | 765 | endif; |
| 416 | 766 | return $ret; |
| 417 | 767 | } /* SyndicatedLink::hardcode () */ |
| 418 | 768 | |
| 419 | - function syndicated_status ($what, $default) { | |
| 769 | + function syndicated_status ($what, $default, $fallback = true) { | |
| 420 | 770 | global $wpdb; |
| 421 | 771 | |
| 422 | - $ret = get_option("feedwordpress_syndicated_{$what}_status"); | |
| 772 | + // Use local setting if we have it | |
| 423 | 773 | if ( isset($this->settings["$what status"]) ) : |
| 424 | 774 | $ret = $this->settings["$what status"]; |
| 425 | - elseif (!$ret) : | |
| 775 | + | |
| 776 | + // Or fall back to global default if we can | |
| 777 | + elseif ($fallback) : | |
| 778 | + $ret = FeedWordPress::syndicated_status($what, $default); | |
| 779 | + | |
| 780 | + // Or use default value if we can't. | |
| 781 | + else : | |
| 426 | 782 | $ret = $default; |
| 783 | + | |
| 427 | 784 | endif; |
| 785 | + | |
| 428 | 786 | return $wpdb->escape(trim(strtolower($ret))); |
| 429 | 787 | } /* SyndicatedLink:syndicated_status () */ |
| 788 | + | |
| 789 | + function taxonomies () { | |
| 790 | + $post_type = $this->setting('syndicated post type', 'syndicated_post_type', 'post'); | |
| 791 | + return get_object_taxonomies(array('object_type' => $post_type), 'names'); | |
| 792 | + } /* SyndicatedLink::taxonomies () */ | |
| 793 | + | |
| 430 | 794 | } // class SyndicatedLink |
| 431 | 795 | |