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