PluginProbe
FeedWordPress / 2011.0721
FeedWordPress v2011.0721
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 +436 -50 2009.06122011.0721 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));
@@ -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,22 +160,29 @@
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 - if (isset($this->settings['update/hold']) and ($this->settings['update/hold']=='ping')) :
171 + if ($this->setting('update/hold')=='ping') :
132 172 $stale = false; // don't update on any timed updates; pings only
133 - elseif (isset($this->settings['update/hold']) and ($this->settings['update/hold']=='next')) :
173 + elseif ($this->setting('update/hold')=='next') :
134 174 $stale = true; // update on the next timed update
135 - elseif (!isset($this->settings['update/ttl']) or !isset($this->settings['update/last'])) :
175 + elseif ( !$this->setting('update/last') ) :
136 176 $stale = true; // initial update
177 + elseif ($feedwordpress->force_update_all()) :
178 + $stale = true; // forced general updating
137 179 else :
138 - $after = ((int) $this->settings['update/last'])
139 - +((int) $this->settings['update/ttl'] * 60);
180 + $after = (
181 + (int) $this->setting('update/last')
182 + + (int) $this->setting('update/fudge')
183 + + ((int) $this->setting('update/ttl') * 60)
184 + );
140 185 $stale = (time() >= $after);
141 186 endif;
142 187 return $stale;
143 188 } /* SyndicatedLink::stale () */
@@ -144,9 +189,26 @@
144 189
145 190 function poll ($crash_ts = NULL) {
146 191 global $wpdb;
147 192
148 - $this->magpie = fetch_rss($this->link->link_rss);
193 + $url = $this->uri(array('add_params' => true));
194 + FeedWordPress::diagnostic('updated_feeds', 'Polling feed ['.$url.']');
195 +
196 + $timeout = $this->setting('fetch timeout', 'feedwordpress_fetch_timeout', FEEDWORDPRESS_FETCH_TIMEOUT_DEFAULT);
197 +
198 + $this->simplepie = apply_filters(
199 + 'syndicated_feed',
200 + FeedWordPress::fetch($url, array('timeout' => $timeout)),
201 + $this
202 + );
203 +
204 + // Filter compatibility mode
205 + if (is_wp_error($this->simplepie)) :
206 + $this->magpie = $this->simplepie;
207 + else :
208 + $this->magpie = new MagpieFromSimplePie($this->simplepie, NULL);
209 + endif;
210 +
149 211 $new_count = NULL;
150 212
151 213 $resume = FeedWordPress::affirmative($this->settings, 'update/unfinished');
152 214 if ($resume) :
@@ -156,9 +218,46 @@
156 218 // begin at the beginning
157 219 $processed = array();
158 220 endif;
159 221
160 - if (is_object($this->magpie)) :
222 + if (is_wp_error($this->simplepie)) :
223 + $new_count = $this->simplepie;
224 + // Error; establish an error setting.
225 + $theError = array();
226 + $theError['ts'] = time();
227 + $theError['since'] = time();
228 + $theError['object'] = $this->simplepie;
229 +
230 + $oldError = $this->setting('update/error', NULL, NULL);
231 + if (is_string($oldError)) :
232 + $oldError = unserialize($oldError);
233 + endif;
234 +
235 + if (!is_null($oldError)) :
236 + // Copy over the in-error-since timestamp
237 + $theError['since'] = $oldError['since'];
238 +
239 + // If this is a repeat error, then we should take
240 + // a step back before we try to fetch it again.
241 + $this->settings['update/last'] = time();
242 + $this->settings['update/ttl'] = $this->automatic_ttl();
243 + $this->settings['update/ttl'] = apply_filters('syndicated_feed_ttl', $this->settings['update/ttl'], $this);
244 + $this->settings['update/ttl'] = apply_filters('syndicated_feed_ttl_from_error', $this->settings['update/ttl'], $this);
245 +
246 + $this->settings['update/timed'] = 'automatically';
247 + endif;
248 +
249 + do_action('syndicated_feed_error', $theError, $oldError, $this);
250 +
251 + $this->settings['update/error'] = serialize($theError);
252 + $this->save_settings(/*reload=*/ true);
253 +
254 + elseif (is_object($this->simplepie)) :
255 + // Success; clear out error setting, if any.
256 + if (isset($this->settings['update/error'])) :
257 + unset($this->settings['update/error']);
258 + endif;
259 +
161 260 $new_count = array('new' => 0, 'updated' => 0);
162 261
163 262 # -- Update Link metadata live from feed
164 263 $channel = $this->magpie->channel;
@@ -185,18 +284,25 @@
185 284 endif;
186 285
187 286 $this->settings = array_merge($this->settings, $this->flatten_array($channel));
188 287
189 - $this->settings['update/last'] = time(); $ttl = $this->ttl();
288 + $this->settings['update/last'] = time();
289 + list($ttl, $xml) = $this->ttl(/*return element=*/ true);
290 +
190 291 if (!is_null($ttl)) :
191 292 $this->settings['update/ttl'] = $ttl;
293 + $this->settings['update/xml'] = $xml;
192 294 $this->settings['update/timed'] = 'feed';
193 295 else :
194 - $this->settings['update/ttl'] = rand(30, 120); // spread over time interval for staggered updates
296 + $ttl = $this->automatic_ttl();
297 + $this->settings['update/ttl'] = $ttl;
298 + $this->settings['update/xml'] = NULL;
195 299 $this->settings['update/timed'] = 'automatically';
196 300 endif;
301 + $this->settings['update/fudge'] = rand(0, ($ttl/3))*60;
302 + $this->settings['update/ttl'] = apply_filters('syndicated_feed_ttl', $this->setting('update/ttl'), $this);
197 303
198 - if (!isset($this->settings['update/hold']) or $this->settings['update/hold']!='ping') :
304 + if (!$this->setting('update/hold') != 'ping') :
199 305 $this->settings['update/hold'] = 'scheduled';
200 306 endif;
201 307
202 308 $this->settings['update/unfinished'] = 'yes';
@@ -210,15 +316,25 @@
210 316 UPDATE $wpdb->links
211 317 SET $update_set
212 318 WHERE link_id='$this->id'
213 319 ");
320 + do_action('update_syndicated_feed', $this->id, $this);
214 321
215 322 # -- Add new posts from feed and update any updated posts
216 323 $crashed = false;
217 324
218 - if (is_array($this->magpie->items)) :
219 - foreach ($this->magpie->items as $item) :
220 - $post =& new SyndicatedPost($item, $this);
325 + $posts = apply_filters(
326 + 'syndicated_feed_items',
327 + $this->simplepie->get_items(),
328 + &$this
329 + );
330 +
331 + $this->magpie->originals = $posts;
332 +
333 + if (is_array($posts)) :
334 + foreach ($posts as $key => $item) :
335 + $post = new SyndicatedPost($item, $this);
336 +
221 337 if (!$resume or !in_array(trim($post->guid()), $processed)) :
222 338 $processed[] = $post->guid();
223 339 if (!$post->filtered()) :
224 340 $new = $post->store();
@@ -229,11 +345,15 @@
229 345 $crashed = true;
230 346 break;
231 347 endif;
232 348 endif;
349 + unset($post);
233 350 endforeach;
234 351 endif;
235 -
352 + $suffix = ($crashed ? 'crashed' : 'completed');
353 + do_action('update_syndicated_feed_items', $this->id, $this);
354 + do_action("update_syndicated_feed_items_${suffix}", $this->id, $this);
355 +
236 356 // Copy back any changes to feed settings made in the course of updating (e.g. new author rules)
237 357 $to_notes = $this->settings;
238 358
239 359 $this->settings['update/processed'] = $processed;
@@ -248,29 +368,103 @@
248 368 UPDATE $wpdb->links
249 369 SET $update_set
250 370 WHERE link_id='$this->id'
251 371 ");
372 +
373 + do_action("update_syndicated_feed_completed", $this->id, $this);
252 374 endif;
253 375
376 + // All done; let's clean up.
377 + $this->magpie = NULL;
378 +
379 + // Avoid circular-reference memory leak in PHP < 5.3.
380 + // Cf. <http://simplepie.org/wiki/faq/i_m_getting_memory_leaks>
381 + if (method_exists($this->simplepie, '__destruct')) :
382 + $this->simplepie->__destruct();
383 + endif;
384 + $this->simplepie = NULL;
385 +
254 386 return $new_count;
255 387 } /* SyndicatedLink::poll() */
256 388
389 + /**
390 + * Updates the URL for the feed syndicated by this link.
391 + *
392 + * @param string $url The new feed URL to use for this source.
393 + * @return bool TRUE on success, FALSE on failure.
394 + */
395 + function set_uri ($url) {
396 + global $wpdb;
397 +
398 + if ($this->found()) :
399 + // Update link_rss
400 + $result = $wpdb->query("
401 + UPDATE $wpdb->links
402 + SET
403 + link_rss = '".$wpdb->escape($url)."'
404 + WHERE link_id = '".$wpdb->escape($this->id)."'
405 + ");
406 +
407 + $ret = ($result ? true : false);
408 + else :
409 + $ret = false;
410 + endif;
411 + return $ret;
412 + } /* SyndicatedLink::set_uri () */
413 +
414 + function deactivate () {
415 + global $wpdb;
416 +
417 + $wpdb->query($wpdb->prepare("
418 + UPDATE $wpdb->links SET link_visible = 'N' WHERE link_id = %d
419 + ", (int) $this->id));
420 + } /* SyndicatedLink::deactivate () */
421 +
422 + function delete () {
423 + global $wpdb;
424 +
425 + $wpdb->query($wpdb->prepare("
426 + DELETE FROM $wpdb->postmeta WHERE meta_key='syndication_feed_id'
427 + AND meta_value = '%s'
428 + ", $this->id));
429 +
430 + $wpdb->query($wpdb->prepare("
431 + DELETE FROM $wpdb->links WHERE link_id = %d
432 + ", (int) $this->id));
433 +
434 + $this->id = NULL;
435 + } /* SyndicatedLink::delete () */
436 +
437 + function nuke () {
438 + global $wpdb;
439 +
440 + // Make a list of the items syndicated from this feed...
441 + $post_ids = $wpdb->get_col($wpdb->prepare("
442 + SELECT post_id FROM $wpdb->postmeta
443 + WHERE meta_key = 'syndication_feed_id'
444 + AND meta_value = '%s'
445 + ", $this->id));
446 +
447 + // ... and kill them all
448 + if (count($post_ids) > 0) :
449 + foreach ($post_ids as $post_id) :
450 + // Force scrubbing of deleted post
451 + // rather than sending to Trashcan
452 + wp_delete_post(
453 + /*postid=*/ $post_id,
454 + /*force_delete=*/ true
455 + );
456 + endforeach;
457 + endif;
458 +
459 + $this->delete();
460 + } /* SyndicatedLink::nuke () */
461 +
257 462 function map_name_to_new_user ($name, $newuser_name) {
258 463 global $wpdb;
259 464
260 465 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);
466 + $newuser_id = fwp_insert_new_user($newuser_name);
273 467 if (is_numeric($newuser_id)) :
274 468 if (is_null($name)) : // Unfamiliar author
275 469 $this->settings['unfamiliar author'] = $newuser_id;
276 470 else :
@@ -283,8 +477,11 @@
283 477 // TODO: Add some error reporting
284 478 endif;
285 479 } /* SyndicatedLink::map_name_to_new_user () */
286 480
481 + function imploded_settings () {
482 + return array('cats', 'tags', 'match/cats', 'match/tags', 'match/filter');
483 + }
287 484 function settings_to_notes () {
288 485 $to_notes = $this->settings;
289 486
290 487 unset($to_notes['link/id']); // Magic setting; don't save
@@ -297,15 +494,22 @@
297 494 if (isset($to_notes['update/processed']) and (is_array($to_notes['update/processed']))) :
298 495 $to_notes['update/processed'] = implode("\n", $to_notes['update/processed']);
299 496 endif;
300 497
301 - if (is_array($to_notes['cats'])) :
302 - $to_notes['cats'] = implode(FEEDWORDPRESS_CAT_SEPARATOR, $to_notes['cats']);
498 + foreach ($this->imploded_settings() as $what) :
499 + if (isset($to_notes[$what]) and is_array($to_notes[$what])) :
500 + $to_notes[$what] = implode(
501 + FEEDWORDPRESS_CAT_SEPARATOR,
502 + $to_notes[$what]
503 + );
504 + endif;
505 + endforeach;
506 +
507 + if (isset($to_notes['terms']) and is_array($to_notes['terms'])) :
508 + // Serialize it.
509 + $to_notes['terms'] = serialize($to_notes['terms']);
303 510 endif;
304 - if (is_array($to_notes['tags'])) :
305 - $to_notes['tags'] = implode(FEEDWORDPRESS_CAT_SEPARATOR, $to_notes['tags']);
306 - endif;
307 -
511 +
308 512 // Collapse the author mapping rule structure back into a flat string
309 513 if (isset($to_notes['map authors'])) :
310 514 $ma = array();
311 515 foreach ($to_notes['map authors'] as $rule_type => $author_rules) :
@@ -322,17 +526,161 @@
322 526 endforeach;
323 527 return $notes;
324 528 } /* SyndicatedLink::settings_to_notes () */
325 529
326 - function uri () {
327 - return (is_object($this->link) ? $this->link->link_rss : NULL);
530 + function save_settings ($reload = false) {
531 + global $wpdb;
532 +
533 + // Save channel-level meta-data
534 + foreach (array('link_name', 'link_description', 'link_url') as $what) :
535 + $alter[] = "{$what} = '".$wpdb->escape($this->link->{$what})."'";
536 + endforeach;
537 +
538 + // Save settings to the notes field
539 + $alter[] = "link_notes = '".$wpdb->escape($this->settings_to_notes())."'";
540 +
541 + // Update the properties of the link from settings changes, etc.
542 + $update_set = implode(", ", $alter);
543 +
544 + $result = $wpdb->query("
545 + UPDATE $wpdb->links
546 + SET $update_set
547 + WHERE link_id='$this->id'
548 + ");
549 +
550 + if ($reload) :
551 + // force reload of link information from DB
552 + if (function_exists('clean_bookmark_cache')) :
553 + clean_bookmark_cache($this->id);
554 + endif;
555 + endif;
556 + } /* SyndicatedLink::save_settings () */
557 +
558 + /**
559 + * Retrieves the value of a setting, allowing for a global setting to be
560 + * used as a fallback, or a constant value, or both.
561 + *
562 + * @param string $name The link setting key
563 + * @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.
564 + * @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.
565 + * @return bool TRUE on success, FALSE on failure.
566 + */
567 + function setting ($name, $fallback_global = NULL, $fallback_value = NULL, $default = 'default') {
568 + $ret = NULL;
569 + if (isset($this->settings[$name])) :
570 + $ret = $this->settings[$name];
571 + endif;
572 +
573 + $no_value = (
574 + is_null($ret)
575 + or (is_string($ret) and strtolower($ret)==$default)
576 + );
577 +
578 + if ($no_value and !is_null($fallback_global)) :
579 + // Avoid duplication of this correction
580 + $fallback_global = preg_replace('/^feedwordpress_/', '', $fallback_global);
581 +
582 + $ret = get_option('feedwordpress_'.$fallback_global, /*default=*/ NULL);
583 + endif;
584 +
585 + $no_value = (
586 + is_null($ret)
587 + or (is_string($ret) and strtolower($ret)==$default)
588 + );
589 +
590 + if ($no_value and !is_null($fallback_value)) :
591 + $ret = $fallback_value;
592 + endif;
593 + return $ret;
594 + } /* SyndicatedLink::setting () */
595 +
596 + function update_setting ($name, $value, $default = 'default') {
597 + if (!is_null($value) and $value != $default) :
598 + $this->settings[$name] = $value;
599 + else : // Zap it.
600 + unset($this->settings[$name]);
601 + endif;
602 + } /* SyndicatedLink::update_setting () */
603 +
604 + function uri ($params = array()) {
605 + $params = shortcode_atts(array(
606 + 'add_params' => false,
607 + ), $params);
608 +
609 + $uri = (is_object($this->link) ? $this->link->link_rss : NULL);
610 + if (!is_null($uri) and strlen($uri) > 0 and $params['add_params']) :
611 + $qp = maybe_unserialize($this->setting('query parameters', array()));
612 +
613 + // For high-tech HTTP feed request kung fu
614 + $qp = apply_filters('syndicated_feed_parameters', $qp, $uri, $this);
615 +
616 + $q = array();
617 + if (is_array($qp) and count($qp) > 0) :
618 + foreach ($qp as $pair) :
619 + $q[] = urlencode($pair[0]).'='.urlencode($pair[1]);
620 + endforeach;
621 +
622 + // Are we appending to a URI that already has params?
623 + $sep = ((strpos('?', $uri)===false) ? '?' : '&');
624 +
625 + // Tack it on
626 + $uri .= $sep . implode("&", $q);
627 + endif;
628 + endif;
629 +
630 + return $uri;
328 631 } /* SyndicatedLink::uri () */
329 632
330 - function homepage () {
331 - return (isset($this->settings['feed/link']) ? $this->settings['feed/link'] : NULL);
633 + function property_cascade ($fromFeed, $link_field, $setting, $simplepie_method) {
634 + $value = NULL;
635 + if ($fromFeed) :
636 + if (isset($this->settings[$setting])) :
637 + $value = $this->settings[$setting];
638 + elseif (is_object($this->simplepie)
639 + and method_exists($this->simplepie, $simplepie_method)) :
640 + $value = $this->simplepie->{$simplepie_method}();
641 + endif;
642 + else :
643 + $value = $this->link->{$link_field};
644 + endif;
645 + return $value;
646 + } /* SyndicatedLink::property_cascade () */
647 +
648 + function homepage ($fromFeed = true) {
649 + return $this->property_cascade($fromFeed, 'link_url', 'feed/link', 'get_link');
332 650 } /* SyndicatedLink::homepage () */
333 651
334 - function ttl () {
652 + function name ($fromFeed = true) {
653 + return $this->property_cascade($fromFeed, 'link_name', 'feed/title', 'get_title');
654 + } /* SyndicatedLink::name () */
655 +
656 + function guid () {
657 + $ret = $this->setting('feed/id', NULL, $this->uri());
658 +
659 + // If we can get it live from the feed, do so.
660 + if (is_object($this->simplepie)) :
661 + $search = array(
662 + array(SIMPLEPIE_NAMESPACE_ATOM_10, 'id'),
663 + array(SIMPLEPIE_NAMESPACE_ATOM_03, 'id'),
664 + array(SIMPLEPIE_NAMESPACE_RSS_20, 'guid'),
665 + array(SIMPLEPIE_NAMESPACE_DC_11, 'identifier'),
666 + array(SIMPLEPIE_NAMESPACE_DC_10, 'identifier'),
667 + );
668 +
669 + foreach ($search as $pair) :
670 + if ($id_tags = $this->simplepie->get_feed_tags($pair[0], $pair[1])) :
671 + $ret = $id_tags[0]['data'];
672 + break;
673 + elseif ($id_tags = $this->simplepie->get_channel_tags($pair[0], $pair[1])) :
674 + $ret = $id_tags[0]['data'];
675 + break;
676 + endif;
677 + endforeach;
678 + endif;
679 + return $ret;
680 + }
681 +
682 + function ttl ($return_element = false) {
335 683 if (is_object($this->magpie)) :
336 684 $channel = $this->magpie->channel;
337 685 else :
338 686 $channel = array();
@@ -342,8 +690,9 @@
342 690 // "ttl stands for time to live. It's a number of
343 691 // minutes that indicates how long a channel can be
344 692 // cached before refreshing from the source."
345 693 // <http://blogs.law.harvard.edu/tech/rss#ltttlgtSubelementOfLtchannelgt>
694 + $xml = 'rss:ttl';
346 695 $ret = $channel['ttl'];
347 696 elseif (isset($channel['sy']['updatefrequency']) or isset($channel['sy']['updateperiod'])) :
348 697 $period_minutes = array (
349 698 'hourly' => 60, /* minutes in an hour */
@@ -369,15 +718,38 @@
369 718 if (isset($channel['sy']['updatefrequency'])) : $freq = (int) $channel['sy']['updatefrequency'];
370 719 else : $freq = 1;
371 720 endif;
372 721
722 + $xml = 'sy:updateFrequency';
373 723 $ret = (int) ($period_minutes[$period] / $freq);
374 724 else :
725 + $xml = NULL;
375 726 $ret = NULL;
376 727 endif;
377 - return $ret;
728 +
729 + if ('yes'==$this->setting('update/minimum', 'update_minimum', 'no')) :
730 + $min = (int) $this->setting('update/window', 'update_window', DEFAULT_UPDATE_PERIOD);
731 +
732 + if ($min > $ret) :
733 + $ret = NULL;
734 + endif;
735 + endif;
736 + return ($return_element ? array($ret, $xml) : $ret);
378 737 } /* SyndicatedLink::ttl() */
379 738
739 + function automatic_ttl () {
740 + // spread out over a time interval for staggered updates
741 + $updateWindow = $this->setting('update/window', 'update_window', DEFAULT_UPDATE_PERIOD);
742 + if (!is_numeric($updateWindow) or ($updateWindow < 1)) :
743 + $updateWindow = DEFAULT_UPDATE_PERIOD;
744 + endif;
745 +
746 + // We get a fudge of 1/3 of window from elsewhere. We'll do some more
747 + // fudging here.
748 + $fudgedInterval = $updateWindow+rand(-($updateWindow/6), 5*($updateWindow/12));
749 + return apply_filters('syndicated_feed_automatic_ttl', $fudgedInterval, $this);
750 + } /* SyndicatedLink::automatic_ttl () */
751 +
380 752 // SyndicatedLink::flatten_array (): flatten an array. Useful for
381 753 // hierarchical and namespaced elements.
382 754 //
383 755 // Given an array which may contain array or object elements in it,
@@ -415,17 +787,31 @@
415 787 endif;
416 788 return $ret;
417 789 } /* SyndicatedLink::hardcode () */
418 790
419 - function syndicated_status ($what, $default) {
791 + function syndicated_status ($what, $default, $fallback = true) {
420 792 global $wpdb;
421 793
422 - $ret = get_option("feedwordpress_syndicated_{$what}_status");
794 + // Use local setting if we have it
423 795 if ( isset($this->settings["$what status"]) ) :
424 796 $ret = $this->settings["$what status"];
425 - elseif (!$ret) :
797 +
798 + // Or fall back to global default if we can
799 + elseif ($fallback) :
800 + $ret = FeedWordPress::syndicated_status($what, $default);
801 +
802 + // Or use default value if we can't.
803 + else :
426 804 $ret = $default;
805 +
427 806 endif;
807 +
428 808 return $wpdb->escape(trim(strtolower($ret)));
429 809 } /* SyndicatedLink:syndicated_status () */
810 +
811 + function taxonomies () {
812 + $post_type = $this->setting('syndicated post type', 'syndicated_post_type', 'post');
813 + return get_object_taxonomies(array('object_type' => $post_type), 'names');
814 + } /* SyndicatedLink::taxonomies () */
815 +
430 816 } // class SyndicatedLink
431 817