PluginProbe
FeedWordPress / 2010.0602
FeedWordPress v2010.0602
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 | syndicatedlink.class.php +177 -18 2009.06132010.0602 View file →
@@ -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));
@@ -122,9 +126,9 @@
122 126 endif;
123 127 } /* SyndicatedLink::SyndicatedLink () */
124 128
125 129 function found () {
126 - return is_object($this->link);
130 + return is_object($this->link) and !is_wp_error($this->link);
127 131 } /* SyndicatedLink::found () */
128 132
129 133 function stale () {
130 134 $stale = true;
@@ -144,9 +148,23 @@
144 148
145 149 function poll ($crash_ts = NULL) {
146 150 global $wpdb;
147 151
148 - $this->magpie = fetch_rss($this->link->link_rss);
152 + FeedWordPress::diagnostic('updated_feeds', 'Polling feed <'.$this->link->link_rss.'>');
153 +
154 + $this->simplepie = apply_filters(
155 + 'syndicated_feed',
156 + FeedWordPress::fetch($this->link->link_rss),
157 + $this
158 + );
159 +
160 + // Filter compatibility mode
161 + if (is_wp_error($this->simplepie)) :
162 + $this->magpie = $this->simplepie;
163 + else :
164 + $this->magpie = new MagpieFromSimplePie($this->simplepie);
165 + endif;
166 +
149 167 $new_count = NULL;
150 168
151 169 $resume = FeedWordPress::affirmative($this->settings, 'update/unfinished');
152 170 if ($resume) :
@@ -156,9 +174,11 @@
156 174 // begin at the beginning
157 175 $processed = array();
158 176 endif;
159 177
160 - if (is_object($this->magpie)) :
178 + if (is_wp_error($this->simplepie)) :
179 + $new_count = $this->simplepie;
180 + elseif (is_object($this->simplepie)) :
161 181 $new_count = array('new' => 0, 'updated' => 0);
162 182
163 183 # -- Update Link metadata live from feed
164 184 $channel = $this->magpie->channel;
@@ -190,11 +210,23 @@
190 210 if (!is_null($ttl)) :
191 211 $this->settings['update/ttl'] = $ttl;
192 212 $this->settings['update/timed'] = 'feed';
193 213 else :
194 - $this->settings['update/ttl'] = rand(30, 120); // spread over time interval for staggered updates
214 + // spread out over a time interval for staggered updates
215 + $updateWindow = $this->setting(
216 + 'update/window',
217 + 'update_window',
218 + DEFAULT_UPDATE_PERIOD
219 + );
220 + if (!is_numeric($updateWindow) or ($updateWindow < 1)) :
221 + $updateWindow = DEFAULT_UPDATE_PERIOD;
222 + endif;
223 +
224 + $fudgedInterval = $updateWindow+rand(0, 2*($updateWindow/3));
225 + $this->settings['update/ttl'] = apply_filters('syndicated_feed_automatic_ttl', $fudgedInterval, $this);
195 226 $this->settings['update/timed'] = 'automatically';
196 227 endif;
228 + $this->settings['update/ttl'] = apply_filters('syndicated_feed_ttl', $this->settings['update/ttl'], $this);
197 229
198 230 if (!isset($this->settings['update/hold']) or $this->settings['update/hold']!='ping') :
199 231 $this->settings['update/hold'] = 'scheduled';
200 232 endif;
@@ -210,15 +242,26 @@
210 242 UPDATE $wpdb->links
211 243 SET $update_set
212 244 WHERE link_id='$this->id'
213 245 ");
246 + do_action('update_syndicated_feed', $this->id, $this);
214 247
215 248 # -- Add new posts from feed and update any updated posts
216 249 $crashed = false;
217 250
218 - if (is_array($this->magpie->items)) :
219 - foreach ($this->magpie->items as $item) :
220 - $post =& new SyndicatedPost($item, $this);
251 + $posts = apply_filters(
252 + 'syndicated_feed_items',
253 + $this->magpie->originals,
254 + $this
255 + );
256 + if (is_array($posts)) :
257 + foreach ($posts as $key => $original) :
258 + $item = $this->magpie->items[$key];
259 + $post = new SyndicatedPost(array(
260 + 'simplepie' => $original,
261 + 'magpie' => $item,
262 + ), $this);
263 +
221 264 if (!$resume or !in_array(trim($post->guid()), $processed)) :
222 265 $processed[] = $post->guid();
223 266 if (!$post->filtered()) :
224 267 $new = $post->store();
@@ -229,11 +272,15 @@
229 272 $crashed = true;
230 273 break;
231 274 endif;
232 275 endif;
276 + unset($post);
233 277 endforeach;
234 278 endif;
235 -
279 + $suffix = ($crashed ? 'crashed' : 'completed');
280 + do_action('update_syndicated_feed_items', $this->id, $this);
281 + do_action("update_syndicated_feed_items_${suffix}", $this->id, $this);
282 +
236 283 // Copy back any changes to feed settings made in the course of updating (e.g. new author rules)
237 284 $to_notes = $this->settings;
238 285
239 286 $this->settings['update/processed'] = $processed;
@@ -248,13 +295,40 @@
248 295 UPDATE $wpdb->links
249 296 SET $update_set
250 297 WHERE link_id='$this->id'
251 298 ");
299 +
300 + do_action("update_syndicated_feed_completed", $this->id, $this);
252 301 endif;
253 302
254 303 return $new_count;
255 304 } /* SyndicatedLink::poll() */
256 305
306 + /**
307 + * Updates the URL for the feed syndicated by this link.
308 + *
309 + * @param string $url The new feed URL to use for this source.
310 + * @return bool TRUE on success, FALSE on failure.
311 + */
312 + function set_uri ($url) {
313 + global $wpdb;
314 +
315 + if ($this->found()) :
316 + // Update link_rss
317 + $result = $wpdb->query("
318 + UPDATE $wpdb->links
319 + SET
320 + link_rss = '".$wpdb->escape($url)."'
321 + WHERE link_id = '".$wpdb->escape($this->id)."'
322 + ");
323 +
324 + $ret = ($result ? true : false);
325 + else :
326 + $ret = false;
327 + endif;
328 + return $ret;
329 + } /* SyndicatedLink::set_uri () */
330 +
257 331 function map_name_to_new_user ($name, $newuser_name) {
258 332 global $wpdb;
259 333
260 334 if (strlen($newuser_name) > 0) :
@@ -297,12 +371,12 @@
297 371 if (isset($to_notes['update/processed']) and (is_array($to_notes['update/processed']))) :
298 372 $to_notes['update/processed'] = implode("\n", $to_notes['update/processed']);
299 373 endif;
300 374
301 - if (is_array($to_notes['cats'])) :
375 + if (isset($to_notes['cats']) and is_array($to_notes['cats'])) :
302 376 $to_notes['cats'] = implode(FEEDWORDPRESS_CAT_SEPARATOR, $to_notes['cats']);
303 377 endif;
304 - if (is_array($to_notes['tags'])) :
378 + if (isset($to_notes['tags']) and is_array($to_notes['tags'])) :
305 379 $to_notes['tags'] = implode(FEEDWORDPRESS_CAT_SEPARATOR, $to_notes['tags']);
306 380 endif;
307 381
308 382 // Collapse the author mapping rule structure back into a flat string
@@ -322,16 +396,93 @@
322 396 endforeach;
323 397 return $notes;
324 398 } /* SyndicatedLink::settings_to_notes () */
325 399
400 + function save_settings ($reload = false) {
401 + global $wpdb;
402 +
403 + // Save channel-level meta-data
404 + foreach (array('link_name', 'link_description', 'link_url') as $what) :
405 + $alter[] = "{$what} = '".$wpdb->escape($this->link->{$what})."'";
406 + endforeach;
407 +
408 + // Save settings to the notes field
409 + $alter[] = "link_notes = '".$wpdb->escape($this->settings_to_notes())."'";
410 +
411 + // Update the properties of the link from settings changes, etc.
412 + $update_set = implode(", ", $alter);
413 +
414 + $result = $wpdb->query("
415 + UPDATE $wpdb->links
416 + SET $update_set
417 + WHERE link_id='$this->id'
418 + ");
419 +
420 + if ($reload) :
421 + // force reload of link information from DB
422 + if (function_exists('clean_bookmark_cache')) :
423 + clean_bookmark_cache($this->id);
424 + endif;
425 + endif;
426 + } /* SyndicatedLink::save_settings () */
427 +
428 + /**
429 + * Retrieves the value of a setting, allowing for a global setting to be
430 + * used as a fallback, or a constant value, or both.
431 + *
432 + * @param string $name The link setting key
433 + * @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.
434 + * @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.
435 + * @return bool TRUE on success, FALSE on failure.
436 + */
437 + function setting ($name, $fallback_global = NULL, $fallback_value = NULL) {
438 + $ret = NULL;
439 + if (isset($this->settings[$name])) :
440 + $ret = $this->settings[$name];
441 + endif;
442 +
443 + $no_value = (
444 + is_null($ret)
445 + or (is_string($ret) and strtolower($ret)=='default')
446 + );
447 +
448 + if ($no_value and !is_null($fallback_global)) :
449 + $ret = get_option('feedwordpress_'.$fallback_global, /*default=*/ NULL);
450 + endif;
451 +
452 + $no_value = (
453 + is_null($ret)
454 + or (is_string($ret) and strtolower($ret)=='default')
455 + );
456 +
457 + if ($no_value and !is_null($fallback_value)) :
458 + $ret = $fallback_value;
459 + endif;
460 + return $ret;
461 + } /* SyndicatedLink::setting () */
462 +
326 463 function uri () {
327 464 return (is_object($this->link) ? $this->link->link_rss : NULL);
328 465 } /* SyndicatedLink::uri () */
329 466
330 - function homepage () {
331 - return (isset($this->settings['feed/link']) ? $this->settings['feed/link'] : NULL);
467 + function homepage ($fromFeed = true) {
468 + if ($fromFeed) :
469 + $url = (isset($this->settings['feed/link']) ? $this->settings['feed/link'] : NULL);
470 + else :
471 + $url = $this->link->link_url;
472 + endif;
473 + return $url;
332 474 } /* SyndicatedLink::homepage () */
333 475
476 + function name ($fromFeed = true) {
477 + if ($fromFeed) :
478 + $name = (isset($this->settings['feed/title']) ? $this->settings['feed/title'] : NULL);
479 + else :
480 + $name = $this->link->link_name;
481 + endif;
482 + return $name;
483 + } /* SyndicatedLink::name () */
484 +
334 485 function ttl () {
335 486 if (is_object($this->magpie)) :
336 487 $channel = $this->magpie->channel;
337 488 else :
@@ -415,17 +566,25 @@
415 566 endif;
416 567 return $ret;
417 568 } /* SyndicatedLink::hardcode () */
418 569
419 - function syndicated_status ($what, $default) {
570 + function syndicated_status ($what, $default, $fallback = true) {
420 571 global $wpdb;
421 572
422 - $ret = get_option("feedwordpress_syndicated_{$what}_status");
573 + // Use local setting if we have it
423 574 if ( isset($this->settings["$what status"]) ) :
424 575 $ret = $this->settings["$what status"];
425 - elseif (!$ret) :
576 +
577 + // Or fall back to global default if we can
578 + elseif ($fallback) :
579 + $ret = FeedWordPress::syndicated_status($what, $default);
580 +
581 + // Or use default value if we can't.
582 + else :
426 583 $ret = $default;
584 +
427 585 endif;
586 +
428 587 return $wpdb->escape(trim(strtolower($ret)));
429 588 } /* SyndicatedLink:syndicated_status () */
430 589 } // class SyndicatedLink
431 590