PluginProbe
FeedWordPress / 2012.1212
FeedWordPress v2012.1212
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 +267 -605 trunk2012.1212 View file →
@@ -24,9 +24,9 @@
24 24 # Values have linebreak characters escaped with C-style
25 25 # backslashes (so, for example, a newline becomes "\n").
26 26 #
27 27 # The value of `cats` is used as a newline-separated list of
28 -# default categories for any post coming from a particular feed.
28 +# default categories for any post coming from a particular feed.
29 29 # (In the example above, any posts from this feed will be placed
30 30 # in the "computers" and "web" categories--*in addition to* any
31 31 # categories that may already be applied to the posts.)
32 32 #
@@ -32,55 +32,55 @@
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 -require_once dirname( __FILE__ ) . '/feedwordpressparsedpostmeta.class.php';
36 +require_once(dirname(__FILE__).'/magpiefromsimplepie.class.php');
37 +require_once(dirname(__FILE__).'/feedwordpressparsedpostmeta.class.php');
38 38
39 39 class SyndicatedLink {
40 40 var $id = null;
41 41 var $link = null;
42 42 var $settings = array ();
43 - public $simplepie = null;
43 + var $simplepie = null;
44 44 var $magpie = null;
45 45
46 - public function __construct( $link ) {
46 + function SyndicatedLink ($link) {
47 + global $wpdb;
48 +
47 49 if (is_object($link)) :
48 50 $this->link = $link;
49 51 $this->id = $link->link_id;
50 52 else :
51 - $this->id = $link;
53 + $this->id = $link;
52 54 $this->link = get_bookmark($link);
53 55 endif;
54 56
55 - if (is_object($this->link)) :
56 - if (strlen($this->link->link_rss) > 0) :
57 - $this->get_settings_from_notes();
58 - endif;
57 + if (strlen($this->link->link_rss) > 0) :
58 + $this->get_settings_from_notes();
59 59 endif;
60 -
60 +
61 61 add_filter('feedwordpress_update_complete', array($this, 'process_retirements'), 1000, 1);
62 - } /* SyndicatedLink::__construct () */
63 -
64 - public function found () {
62 + } /* SyndicatedLink::SyndicatedLink () */
63 +
64 + function found () {
65 65 return is_object($this->link) and !is_wp_error($this->link);
66 66 } /* SyndicatedLink::found () */
67 67
68 - public function id () {
68 + function id () {
69 69 return (is_object($this->link) ? $this->link->link_id : NULL);
70 70 }
71 -
72 - public function stale () {
71 +
72 + function stale () {
73 73 global $feedwordpress;
74 -
74 +
75 75 $stale = true;
76 76 if ($this->setting('update/hold')=='ping') :
77 77 $stale = false; // don't update on any timed updates; pings only
78 78 elseif ($this->setting('update/hold')=='next') :
79 79 $stale = true; // update on the next timed update
80 - elseif ( ! $this->setting('update/last') ) :
80 + elseif ( !$this->setting('update/last') ) :
81 81 $stale = true; // initial update
82 - elseif ( !empty($feedwordpress) and $feedwordpress->force_update_all()) :
82 + elseif ($feedwordpress->force_update_all()) :
83 83 $stale = true; // forced general updating
84 84 else :
85 85 $after = (
86 86 (int) $this->setting('update/last')
@@ -91,9 +91,14 @@
91 91 endif;
92 92 return $stale;
93 93 } /* SyndicatedLink::stale () */
94 94
95 - public function fetch () {
95 + function poll ($crash_ts = NULL) {
96 + global $wpdb;
97 +
98 + $url = $this->uri(array('add_params' => true));
99 + FeedWordPress::diagnostic('updated_feeds', 'Polling feed ['.$url.']');
100 +
96 101 $timeout = $this->setting('fetch timeout', 'feedwordpress_fetch_timeout', FEEDWORDPRESS_FETCH_TIMEOUT_DEFAULT);
97 102
98 103 $this->simplepie = apply_filters(
99 104 'syndicated_feed',
@@ -106,39 +111,9 @@
106 111 $this->magpie = $this->simplepie;
107 112 else :
108 113 $this->magpie = new MagpieFromSimplePie($this->simplepie, NULL);
109 114 endif;
110 - } /* SyndicatedLink::fetch () */
111 115
112 - public function live_posts () {
113 - if ( !is_object($this->simplepie)) :
114 - $this->fetch();
115 - endif;
116 -
117 - if (is_object($this->simplepie) and method_exists($this->simplepie, 'get_items')) :
118 - $ret = apply_filters(
119 - 'syndicated_feed_items',
120 - $this->simplepie->get_items(),
121 - $this
122 - );
123 - else :
124 - $ret = $this->simplepie;
125 - endif;
126 - return $ret;
127 - } /* SyndicatedLink::live_posts () */
128 -
129 - protected function pause_updates () {
130 - return ('yes'==$this->setting("update/pause", "update_pause", 'no'));
131 - } /* SyndicatedLink::pause_updates () */
132 -
133 - public function poll ($crash_ts = NULL) {
134 - global $wpdb;
135 -
136 - $url = $this->uri(array('add_params' => true, 'fetch' => true));
137 - FeedWordPress::diagnostic('updated_feeds', 'Polling feed ['.$url.']');
138 -
139 - $this->fetch();
140 -
141 116 $new_count = NULL;
142 117
143 118 $resume = ('yes'==$this->setting('update/unfinished'));
144 119 if ($resume) :
@@ -161,12 +136,12 @@
161 136 if (is_string($oldError)) :
162 137 $oldError = unserialize($oldError);
163 138 endif;
164 139
165 - if ( !is_null($oldError)) :
140 + if (!is_null($oldError)) :
166 141 // Copy over the in-error-since timestamp
167 142 $theError['since'] = $oldError['since'];
168 -
143 +
169 144 // If this is a repeat error, then we should
170 145 // take a step back before we try to fetch it
171 146 // again.
172 147 $this->update_setting('update/last', time(), NULL);
@@ -172,65 +147,79 @@
172 147 $this->update_setting('update/last', time(), NULL);
173 148 $ttl = $this->automatic_ttl();
174 149 $ttl = apply_filters('syndicated_feed_ttl', $ttl, $this);
175 150 $ttl = apply_filters('syndicated_feed_ttl_from_error', $ttl, $this);
176 - $this->update_setting('update/ttl', $ttl);
151 + $this->update_setting('update/ttl', $ttl, $this);
177 152 $this->update_setting('update/timed', 'automatically');
178 153 endif;
179 -
154 +
180 155 do_action('syndicated_feed_error', $theError, $oldError, $this);
181 -
156 +
182 157 $this->update_setting('update/error', serialize($theError));
183 158 $this->save_settings(/*reload=*/ true);
184 159
185 160 elseif (is_object($this->simplepie)) :
186 -
187 161 // Success; clear out error setting, if any.
188 162 $this->update_setting('update/error', NULL);
189 163
190 - $new_count = array('new' => 0, 'updated' => 0, 'stored' => 0);
164 + $new_count = array('new' => 0, 'updated' => 0);
191 165
192 166 # -- Update Link metadata live from feed
193 167 $channel = $this->magpie->channel;
194 168
195 - if ( !isset($channel['id'])) :
169 + if (!isset($channel['id'])) :
196 170 $channel['id'] = $this->link->link_rss;
197 171 endif;
198 -
172 +
199 173 $update = array();
200 - if ( ! $this->hardcode('url') and isset($channel['link'])) :
201 - $update[] = "link_url = '".esc_sql($channel['link'])."'";
174 + if (!$this->hardcode('url') and isset($channel['link'])) :
175 + $update[] = "link_url = '".$wpdb->escape($channel['link'])."'";
202 176 endif;
203 -
204 - if ( ! $this->hardcode('name') and isset($channel['title'])) :
205 - $update[] = "link_name = '".esc_sql($channel['title'])."'";
177 +
178 + if (!$this->hardcode('name') and isset($channel['title'])) :
179 + $update[] = "link_name = '".$wpdb->escape($channel['title'])."'";
206 180 endif;
207 -
208 - if ( ! $this->hardcode('description')) :
181 +
182 + if (!$this->hardcode('description')) :
209 183 if (isset($channel['tagline'])) :
210 - $update[] = "link_description = '".esc_sql($channel['tagline'])."'";
184 + $update[] = "link_description = '".$wpdb->escape($channel['tagline'])."'";
211 185 elseif (isset($channel['description'])) :
212 - $update[] = "link_description = '".esc_sql($channel['description'])."'";
186 + $update[] = "link_description = '".$wpdb->escape($channel['description'])."'";
213 187 endif;
214 188 endif;
215 189
216 - $this->update_setting('link/feed_type', $this->simplepie->get_type());
217 -
218 190 $this->merge_settings($channel, 'feed/');
219 191
220 192 $this->update_setting('update/last', time());
221 - $this->do_update_ttl();
193 + list($ttl, $xml) = $this->ttl(/*return element=*/ true);
194 +
195 + if (!is_null($ttl)) :
196 + $this->update_setting('update/ttl', $ttl);
197 + $this->update_setting('update/xml', $xml);
198 + $this->update_setting('update/timed', 'feed');
199 + else :
200 + $ttl = $this->automatic_ttl();
201 + $this->update_setting('update/ttl', $ttl);
202 + $this->update_setting('update/xml', NULL);
203 + $this->update_setting('update/timed', 'automatically');
204 + endif;
205 + $this->update_setting('update/fudge', rand(0, ($ttl/3))*60);
206 + $this->update_setting('update/ttl', apply_filters(
207 + 'syndicated_feed_ttl',
208 + $this->setting('update/ttl'),
209 + $this
210 + ));
222 211
223 - if ( ! $this->setting('update/hold') != 'ping') :
212 + if (!$this->setting('update/hold') != 'ping') :
224 213 $this->update_setting('update/hold', 'scheduled');
225 214 endif;
226 215
227 216 $this->update_setting('update/unfinished', 'yes');
228 217
229 - $update[] = "link_notes = '".esc_sql($this->settings_to_notes())."'";
218 + $update[] = "link_notes = '".$wpdb->escape($this->settings_to_notes())."'";
230 219
231 220 $update_set = implode(',', $update);
232 -
221 +
233 222 // Update the properties of the link from the feed information
234 223 $result = $wpdb->query("
235 224 UPDATE $wpdb->links
236 225 SET $update_set
@@ -240,15 +229,19 @@
240 229
241 230 # -- Add new posts from feed and update any updated posts
242 231 $crashed = false;
243 232
244 - $posts = $this->live_posts();
233 + $posts = apply_filters(
234 + 'syndicated_feed_items',
235 + $this->simplepie->get_items(),
236 + $this
237 + );
245 238
246 239 $this->magpie->originals = $posts;
247 240
248 241 // If this is a complete feed, rather than an incremental feed, we
249 242 // need to prepare to mark everything for presumptive retirement.
250 - if ($this->is_non_incremental()) :
243 + if ($this->is_incremental()) :
251 244 $q = new WP_Query(array(
252 245 'fields' => '_synfrom',
253 246 'post_status__not' => 'fwpretired',
254 247 'ignore_sticky_posts' => true,
@@ -258,37 +251,34 @@
258 251 foreach ($q->posts as $p) :
259 252 update_post_meta($p->ID, '_feedwordpress_retire_me_'.$this->id, '1');
260 253 endforeach;
261 254 endif;
262 -
255 +
263 256 if (is_array($posts)) :
264 257 foreach ($posts as $key => $item) :
265 - if ( ! $this->pause_updates()) :
266 - $post = new SyndicatedPost($item, $this);
258 + $post = new SyndicatedPost($item, $this);
267 259
268 - if ( ! $resume or !in_array(trim($post->guid()), $processed)) :
269 - $processed[] = $post->guid();
270 - if ( ! $post->filtered()) :
271 - $new = $post->store();
272 - if ( $new !== false ) $new_count[$new]++;
273 - endif;
260 + if (!$resume or !in_array(trim($post->guid()), $processed)) :
261 + $processed[] = $post->guid();
262 + if (!$post->filtered()) :
263 + $new = $post->store();
264 + if ( $new !== false ) $new_count[$new]++;
265 + endif;
274 266
275 - if ( !is_null($crash_ts) and (time() > $crash_ts)) :
276 - $crashed = true;
277 - break;
278 - endif;
267 + if (!is_null($crash_ts) and (time() > $crash_ts)) :
268 + $crashed = true;
269 + break;
279 270 endif;
280 -
281 - unset($post);
282 271 endif;
272 + unset($post);
283 273 endforeach;
284 274 endif;
285 -
275 +
286 276 if ('yes'==$this->setting('tombstones', 'tombstones', 'yes')) :
287 277 // Check for use of Atom tombstones. Spec:
288 278 // <http://tools.ietf.org/html/draft-snell-atompub-tombstones-18>
289 279 $tombstones = $this->simplepie->get_feed_tags('http://purl.org/atompub/tombstones/1.0', 'deleted-entry');
290 - if ( !is_null($tombstones) && count($tombstones) > 0) :
280 + if (count($tombstones) > 0) :
291 281 foreach ($tombstones as $tombstone) :
292 282 $ref = NULL;
293 283 foreach (array('', 'http://purl.org/atompub/tombstones/1.0') as $ns) :
294 284 if (isset($tombstone['attribs'][$ns])
@@ -295,9 +285,9 @@
295 285 and isset($tombstone['attribs'][$ns]['ref'])) :
296 286 $ref = $tombstone['attribs'][$ns]['ref'];
297 287 endif;
298 288 endforeach;
299 -
289 +
300 290 $q = new WP_Query(array(
301 291 'ignore_sticky_posts' => true,
302 292 'guid' => $ref,
303 293 'meta_key' => 'syndication_feed_id',
@@ -302,9 +292,9 @@
302 292 'guid' => $ref,
303 293 'meta_key' => 'syndication_feed_id',
304 294 'meta_value' => $this->id, // Only allow a feed to tombstone its own entries.
305 295 ));
306 -
296 +
307 297 foreach ($q->posts as $p) :
308 298 $old_status = $p->post_status;
309 299 FeedWordPress::diagnostic('syndicated_posts', 'Retiring existing post # '.$p->ID.' "'.$p->post_title.'" due to Atom tombstone element in feed.');
310 300 set_post_field('post_status', 'fwpretired', $p->ID);
@@ -309,19 +299,19 @@
309 299 FeedWordPress::diagnostic('syndicated_posts', 'Retiring existing post # '.$p->ID.' "'.$p->post_title.'" due to Atom tombstone element in feed.');
310 300 set_post_field('post_status', 'fwpretired', $p->ID);
311 301 wp_transition_post_status('fwpretired', $old_status, $p);
312 302 endforeach;
313 -
303 +
314 304 endforeach;
315 305 endif;
316 306 endif;
317 -
307 +
318 308 $suffix = ($crashed ? 'crashed' : 'completed');
319 309 do_action('update_syndicated_feed_items', $this->id, $this);
320 - do_action("update_syndicated_feed_items_{$suffix}", $this->id, $this);
310 + do_action("update_syndicated_feed_items_${suffix}", $this->id, $this);
321 311
322 312 $this->update_setting('update/processed', $processed);
323 - if ( ! $crashed) :
313 + if (!$crashed) :
324 314 $this->update_setting('update/unfinished', 'no');
325 315 endif;
326 316 $this->update_setting('link/item count', count($posts));
327 317
@@ -326,29 +316,23 @@
326 316 $this->update_setting('link/item count', count($posts));
327 317
328 318 // Copy back any changes to feed settings made in the
329 319 // course of updating (e.g. new author rules)
330 - $update_set = "link_notes = '" . esc_sql($this->settings_to_notes())."'";
331 -
320 + $update_set = "link_notes = '".$wpdb->escape($this->settings_to_notes())."'";
321 +
332 322 // Update the properties of the link from the feed information
333 323 $result = $wpdb->query("
334 - UPDATE {$wpdb->links}
335 - SET {$update_set}
336 - WHERE link_id='{$this->id}'
324 + UPDATE $wpdb->links
325 + SET $update_set
326 + WHERE link_id='$this->id'
337 327 ");
338 - if ( FALSE === $result ) :
339 - FeedWordPress::diagnostic(
340 - 'SyndicatedLink/poll',
341 - 'Database update failed',
342 - );
343 - endif;
344 -
328 +
345 329 do_action("update_syndicated_feed_completed", $this->id, $this);
346 330 endif;
347 -
331 +
348 332 // All done; let's clean up.
349 333 $this->magpie = NULL;
350 -
334 +
351 335 // Avoid circular-reference memory leak in PHP < 5.3.
352 336 // Cf. <http://simplepie.org/wiki/faq/i_m_getting_memory_leaks>
353 337 if (method_exists($this->simplepie, '__destruct')) :
354 338 $this->simplepie->__destruct();
@@ -353,47 +337,15 @@
353 337 if (method_exists($this->simplepie, '__destruct')) :
354 338 $this->simplepie->__destruct();
355 339 endif;
356 340 $this->simplepie = NULL;
357 -
341 +
358 342 return $new_count;
359 343 } /* SyndicatedLink::poll() */
360 344
361 - /**
362 - * Update the time to live of this link.
363 - */
364 - public function do_update_ttl(): void {
365 - // Get ttl and xml elements, return them if available
366 - list( $ttl, $xml ) = $this->ttl( /*return element=*/ true);
345 + function process_retirements ($delta) {
346 + global $post;
367 347
368 - // Check if ttl is not null
369 - if ( !is_null( $ttl ) ) {
370 - $this->update_setting( 'update/ttl', $ttl );
371 - $this->update_setting( 'update/xml', $xml );
372 - $this->update_setting( 'update/timed', 'feed' );
373 - } else {
374 - // Fallback to automatic ttl if null
375 - $ttl = $this->automatic_ttl();
376 - $this->update_setting( 'update/ttl', $ttl );
377 - $this->update_setting( 'update/xml', null ); // Explicit null
378 - $this->update_setting( 'update/timed', 'automatically' );
379 - }
380 -
381 - // Adding a random fudge value (ensure it works across versions)
382 - $this->update_setting( 'update/fudge', rand( 0, (int) ( $ttl / 3 ) ) * 60 );
383 -
384 - // Apply filter to ttl (should be compatible with all PHP 8.x versions)
385 - $this->update_setting(
386 - 'update/ttl',
387 - apply_filters(
388 - 'syndicated_feed_ttl',
389 - $this->setting( 'update/ttl' ),
390 - $this
391 - )
392 - );
393 - } /* SyndicatedLink::do_update_ttl () */
394 -
395 - public function process_retirements ($delta) {
396 348 $q = new WP_Query(array(
397 349 'fields' => '_synfrom',
398 350 'post_status__not' => 'fwpretired',
399 351 'ignore_sticky_posts' => true,
@@ -408,12 +360,11 @@
408 360 wp_transition_post_status('fwpretired', $old_status, $p);
409 361 delete_post_meta($p->ID, '_feedwordpress_retire_me_'.$this->id);
410 362 endforeach;
411 363 endif;
412 -
413 364 return $delta;
414 - } /* SyndicatedLink::process_retirements () */
415 -
365 + }
366 +
416 367 /**
417 368 * Updates the URL for the feed syndicated by this link.
418 369 *
419 370 * @param string $url The new feed URL to use for this source.
@@ -418,9 +369,9 @@
418 369 *
419 370 * @param string $url The new feed URL to use for this source.
420 371 * @return bool TRUE on success, FALSE on failure.
421 372 */
422 - public function set_uri ($url) {
373 + function set_uri ($url) {
423 374 global $wpdb;
424 375
425 376 if ($this->found()) :
426 377 // Update link_rss
@@ -426,12 +377,12 @@
426 377 // Update link_rss
427 378 $result = $wpdb->query("
428 379 UPDATE $wpdb->links
429 380 SET
430 - link_rss = '".esc_sql($url)."'
431 - WHERE link_id = '".esc_sql($this->id)."'
381 + link_rss = '".$wpdb->escape($url)."'
382 + WHERE link_id = '".$wpdb->escape($this->id)."'
432 383 ");
433 -
384 +
434 385 $ret = ($result ? true : false);
435 386 else :
436 387 $ret = false;
437 388 endif;
@@ -436,55 +387,35 @@
436 387 $ret = false;
437 388 endif;
438 389 return $ret;
439 390 } /* SyndicatedLink::set_uri () */
440 -
441 - public function deactivate () {
391 +
392 + function deactivate () {
442 393 global $wpdb;
443 -
394 +
444 395 $wpdb->query($wpdb->prepare("
445 396 UPDATE $wpdb->links SET link_visible = 'N' WHERE link_id = %d
446 397 ", (int) $this->id));
447 398 } /* SyndicatedLink::deactivate () */
448 -
449 - /**
450 - * SyndicatedLink::delete() deletes a subscription from the WordPress links
451 - * table. Any posts that were syndicated through that subscription will still
452 - * be present in the wp_posts table; but postmeta fields that refer to the
453 - * syndication feed's numeric id (which will no longer be valid) will be
454 - * deleted. For most purposes, the posts remaining will be treated as if they
455 - * were locally authored posts rather than syndicated posts.
456 - *
457 - * @global $wpdb
458 - * @uses wpdb::query
459 - */
460 - public function delete () {
399 +
400 + function delete () {
461 401 global $wpdb;
462 -
402 +
463 403 $wpdb->query($wpdb->prepare("
464 404 DELETE FROM $wpdb->postmeta WHERE meta_key='syndication_feed_id'
465 405 AND meta_value = '%s'
466 406 ", $this->id));
467 -
407 +
468 408 $wpdb->query($wpdb->prepare("
469 409 DELETE FROM $wpdb->links WHERE link_id = %d
470 410 ", (int) $this->id));
471 -
411 +
472 412 $this->id = NULL;
473 413 } /* SyndicatedLink::delete () */
474 -
475 - /**
476 - * SyndicatedLink::nuke() deletes a subscription AND all of the
477 - * posts syndicated through that subscription.
478 - *
479 - * @global $wpdb
480 - * @uses wpdb::get_col
481 - * @uses wp_delete_post
482 - * @uses SyndicatedLink::delete
483 - */
484 - public function nuke () {
414 +
415 + function nuke () {
485 416 global $wpdb;
486 -
417 +
487 418 // Make a list of the items syndicated from this feed...
488 419 $post_ids = $wpdb->get_col($wpdb->prepare("
489 420 SELECT post_id FROM $wpdb->postmeta
490 421 WHERE meta_key = 'syndication_feed_id'
@@ -489,9 +420,9 @@
489 420 SELECT post_id FROM $wpdb->postmeta
490 421 WHERE meta_key = 'syndication_feed_id'
491 422 AND meta_value = '%s'
492 423 ", $this->id));
493 -
424 +
494 425 // ... and kill them all
495 426 if (count($post_ids) > 0) :
496 427 foreach ($post_ids as $post_id) :
497 428 // Force scrubbing of deleted post
@@ -501,13 +432,13 @@
501 432 /*force_delete=*/ true
502 433 );
503 434 endforeach;
504 435 endif;
505 -
436 +
506 437 $this->delete();
507 438 } /* SyndicatedLink::nuke () */
508 -
509 - public function map_name_to_new_user ($name, $newuser_name) {
439 +
440 + function map_name_to_new_user ($name, $newuser_name) {
510 441 global $wpdb;
511 442
512 443 if (strlen($newuser_name) > 0) :
513 444 $newuser_id = fwp_insert_new_user($newuser_name);
@@ -526,13 +457,13 @@
526 457 // TODO: Add some error reporting
527 458 endif;
528 459 } /* SyndicatedLink::map_name_to_new_user () */
529 460
530 - protected function imploded_settings () {
461 + function imploded_settings () {
531 462 return array('cats', 'tags', 'match/cats', 'match/tags', 'match/filter');
532 - } /* SyndicatedLink::imploded_settings () */
533 -
534 - protected function get_settings_from_notes () {
463 + }
464 +
465 + function get_settings_from_notes () {
535 466 // Read off feed settings from link_notes
536 467 $notes = explode("\n", $this->link->link_notes);
537 468 foreach ($notes as $note):
538 469 $pair = explode(": ", $note, 2);
@@ -537,9 +468,9 @@
537 468 foreach ($notes as $note):
538 469 $pair = explode(": ", $note, 2);
539 470 $key = (isset($pair[0]) ? $pair[0] : null);
540 471 $value = (isset($pair[1]) ? $pair[1] : null);
541 - if ( !is_null($key) and !is_null($value)) :
472 + if (!is_null($key) and !is_null($value)) :
542 473 // Unescape and trim() off the whitespace.
543 474 // Thanks to Ray Lischner for pointing out the
544 475 // need to trim off whitespace.
545 476 $this->settings[$key] = stripcslashes (trim($value));
@@ -549,9 +480,9 @@
549 480 // "Magic" feed settings
550 481 $this->settings['link/uri'] = $this->link->link_rss;
551 482 $this->settings['link/name'] = $this->link->link_name;
552 483 $this->settings['link/id'] = $this->link->link_id;
553 -
484 +
554 485 // `hardcode categories` and `unfamiliar categories` are
555 486 // deprecated in favor of `unfamiliar category`
556 487 if (
557 488 isset($this->settings['unfamiliar categories'])
@@ -568,9 +499,9 @@
568 499
569 500 // Set this up automagically for del.icio.us
570 501 $bits = parse_url($this->link->link_rss);
571 502 $tagspacers = array('del.icio.us', 'feeds.delicious.com');
572 - if ( !isset($this->settings['cat_split']) and in_array($bits['host'], $tagspacers)) :
503 + if (!isset($this->settings['cat_split']) and in_array($bits['host'], $tagspacers)) :
573 504 $this->settings['cat_split'] = '\s'; // Whitespace separates multiple tags in del.icio.us RSS feeds
574 505 endif;
575 506
576 507 // Simple lists
@@ -585,10 +516,10 @@
585 516
586 517 if (isset($this->settings['terms'])) :
587 518 // Look for new format
588 519 $this->settings['terms'] = maybe_unserialize($this->settings['terms']);
589 -
590 - if ( !is_array($this->settings['terms'])) :
520 +
521 + if (!is_array($this->settings['terms'])) :
591 522 // Deal with old format instead. Ugh.
592 523
593 524 // Split on two *or more* consecutive breaks
594 525 // because in the old format, a taxonomy
@@ -617,16 +548,15 @@
617 548 if (isset($this->settings['map authors'])) :
618 549 $author_rules = explode("\n\n", $this->settings['map authors']);
619 550 $ma = array();
620 551 foreach ($author_rules as $rule) :
621 - $parts = array_pad(explode("\n", $rule), 3, '');
622 - list($rule_type, $author_name, $author_action) = $parts;
623 -
552 + list($rule_type, $author_name, $author_action) = explode("\n", $rule);
553 +
624 554 // Normalize for case and whitespace
625 555 $rule_type = strtolower(trim($rule_type));
626 556 $author_name = strtolower(trim($author_name));
627 557 $author_action = strtolower(trim($author_action));
628 -
558 +
629 559 $ma[$rule_type][$author_name] = $author_action;
630 560 endforeach;
631 561 $this->settings['map authors'] = $ma;
632 562 endif;
@@ -631,10 +561,10 @@
631 561 $this->settings['map authors'] = $ma;
632 562 endif;
633 563
634 564 } /* SyndicatedLink::get_settings_from_notes () */
635 -
636 - protected function settings_to_notes () {
565 +
566 + function settings_to_notes () {
637 567 $to_notes = $this->settings;
638 568
639 569 unset($to_notes['link/id']); // Magic setting; don't save
640 570 unset($to_notes['link/uri']); // Magic setting; don't save
@@ -654,14 +584,14 @@
654 584 $to_notes[$what]
655 585 );
656 586 endif;
657 587 endforeach;
658 -
588 +
659 589 if (isset($to_notes['terms']) and is_array($to_notes['terms'])) :
660 590 // Serialize it.
661 591 $to_notes['terms'] = serialize($to_notes['terms']);
662 592 endif;
663 -
593 +
664 594 // Collapse the author mapping rule structure back into a flat string
665 595 if (isset($to_notes['map authors'])) :
666 596 $ma = array();
667 597 foreach ($to_notes['map authors'] as $rule_type => $author_rules) :
@@ -678,18 +608,18 @@
678 608 endforeach;
679 609 return $notes;
680 610 } /* SyndicatedLink::settings_to_notes () */
681 611
682 - public function save_settings ($reload = false) {
612 + function save_settings ($reload = false) {
683 613 global $wpdb;
684 614
685 615 // Save channel-level meta-data
686 616 foreach (array('link_name', 'link_description', 'link_url') as $what) :
687 - $alter[] = "{$what} = '".esc_sql($this->link->{$what})."'";
617 + $alter[] = "{$what} = '".$wpdb->escape($this->link->{$what})."'";
688 618 endforeach;
689 619
690 620 // Save settings to the notes field
691 - $alter[] = "link_notes = '".esc_sql($this->settings_to_notes())."'";
621 + $alter[] = "link_notes = '".$wpdb->escape($this->settings_to_notes())."'";
692 622
693 623 // Update the properties of the link from settings changes, etc.
694 624 $update_set = implode(", ", $alter);
695 625
@@ -697,14 +627,9 @@
697 627 UPDATE $wpdb->links
698 628 SET $update_set
699 629 WHERE link_id='$this->id'
700 630 ");
701 -
702 - if ( $result === false )
703 - {
704 - error_log("query failed: " . $wpdb->last_error);
705 - }
706 -
631 +
707 632 if ($reload) :
708 633 // force reload of link information from DB
709 634 if (function_exists('clean_bookmark_cache')) :
710 635 clean_bookmark_cache($this->id);
@@ -715,180 +640,106 @@
715 640 /**
716 641 * Retrieves the value of a setting, allowing for a global setting to be
717 642 * used as a fallback, or a constant value, or both.
718 643 *
719 - * @param string $name The link setting key.
720 - * @param mixed $fallback_global If the link setting is nonexistent or
721 - * marked as a use-default value, fall back
722 - * to the value of this global setting.
723 - * @param mixed $fallback_value If the link setting and the global setting are
724 - * nonexistent or marked as ause-default value,
725 - * fall back to this constant value.
726 - *
727 - * @return mixed|null Should *not* return a boolean!!
644 + * @param string $name The link setting key
645 + * @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.
646 + * @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.
647 + * @return bool TRUE on success, FALSE on failure.
728 648 */
729 - public function setting( $name, $fallback_global = NULL, $fallback_value = NULL, $default = 'default' ) {
649 + function setting ($name, $fallback_global = NULL, $fallback_value = NULL, $default = 'default') {
730 650 $ret = NULL;
731 - if ( isset( $this->settings[ $name ] ) ) :
732 - $ret = $this->settings[ $name ];
651 + if (isset($this->settings[$name])) :
652 + $ret = $this->settings[$name];
733 653 endif;
734 -
654 +
735 655 $no_value = (
736 - is_null( $ret )
737 - or ( is_string( $ret ) and strtolower( $ret ) == $default )
656 + is_null($ret)
657 + or (is_string($ret) and strtolower($ret)==$default)
738 658 );
739 659
740 - if ( $no_value and ! is_null( $fallback_global ) ) :
660 + if ($no_value and !is_null($fallback_global)) :
741 661 // Avoid duplication of this correction
742 - $fallback_global = preg_replace( '/^feedwordpress_/', '', $fallback_global );
743 -
744 - // Occasionally we'll get an array back. Convert it to a string
745 - if ( is_array( $fallback_global ) && sizeof( $fallback_global ) ) :
746 - $fallback_global = reset( $fallback_global );
747 - endif;
748 -
749 - if ( ! empty( $fallback_global ) ) :
750 - $ret = get_option( 'feedwordpress_' . $fallback_global, /*default=*/ NULL );
751 - endif;
662 + $fallback_global = preg_replace('/^feedwordpress_/', '', $fallback_global);
663 +
664 + $ret = get_option('feedwordpress_'.$fallback_global, /*default=*/ NULL);
752 665 endif;
753 666
754 667 $no_value = (
755 - is_null( $ret )
756 - or ( is_string( $ret ) and strtolower( $ret ) == $default )
668 + is_null($ret)
669 + or (is_string($ret) and strtolower($ret)==$default)
757 670 );
758 671
759 - if ( $no_value and ! is_null( $fallback_value ) ) :
672 + if ($no_value and !is_null($fallback_value)) :
760 673 $ret = $fallback_value;
761 674 endif;
762 675 return $ret;
763 - } /* SyndicatedLink::setting() */
676 + } /* SyndicatedLink::setting () */
764 677
765 - /**
766 - * Merge current settings with array of additional data.
767 - *
768 - * @param array $data Settings to merge with.
769 - * @param string $prefix Hierarchical prefix (e.g. "feed/" as in "feed/a/b/c").
770 - * @param string $separator Hierarchy separator (e.g., '/').
771 - *
772 - * @see SyndicatedLink::flatten_array()
773 - */
774 - public function merge_settings( $data, $prefix, $separator = '/' ) {
775 - $dd = $this->flatten_array( $data, $prefix, $separator );
776 - $this->settings = array_merge( $this->settings, $dd );
678 + function merge_settings ($data, $prefix, $separator = '/') {
679 + $dd = $this->flatten_array($data, $prefix, $separator);
680 + $this->settings = array_merge($this->settings, $dd);
777 681 } /* SyndicatedLink::merge_settings () */
778 -
779 - public function update_setting( $name, $value, $default = 'default' ) {
780 - if ( !is_null( $value ) and $value != $default ) :
781 - $this->settings[ $name ] = $value;
682 +
683 + function update_setting ($name, $value, $default = 'default') {
684 + if (!is_null($value) and $value != $default) :
685 + $this->settings[$name] = $value;
782 686 else : // Zap it.
783 - unset( $this->settings[ $name ] );
687 + unset($this->settings[$name]);
784 688 endif;
785 689 } /* SyndicatedLink::update_setting () */
786 -
787 - public function is_non_incremental () {
690 +
691 + function is_incremental () {
788 692 return ('complete'==$this->setting('update_incremental', 'update_incremental', 'incremental'));
789 - } /* SyndicatedLink::is_non_incremental () */
790 -
791 - public function get_feed_type () {
792 - $type_code = $this->setting('link/feed_type');
793 -
794 - // list derived from: <http://simplepie.org/api/class-SimplePie.html>, retrieved 2020/01/18
795 - $bitmasks = array(
796 - SIMPLEPIE_TYPE_RSS_090 => 'RSS 0.90',
797 - SIMPLEPIE_TYPE_RSS_091_NETSCAPE => 'RSS 0.91 (Netscape)',
798 - SIMPLEPIE_TYPE_RSS_091_USERLAND => 'RSS 0.91 (Userland)',
799 - SIMPLEPIE_TYPE_RSS_091 => 'RSS 0.91',
800 - SIMPLEPIE_TYPE_RSS_092 => 'RSS 0.92',
801 - SIMPLEPIE_TYPE_RSS_093 => 'RSS 0.93',
802 - SIMPLEPIE_TYPE_RSS_094 => 'RSS 0.94',
803 - SIMPLEPIE_TYPE_RSS_10 => 'RSS 1.0',
804 - SIMPLEPIE_TYPE_RSS_20 => 'RSS 2.0.x',
805 - SIMPLEPIE_TYPE_RSS_RDF => 'RDF-based RSS',
806 - SIMPLEPIE_TYPE_RSS_SYNDICATION => 'Non-RDF-based RSS',
807 - SIMPLEPIE_TYPE_RSS_ALL => 'Any version of RSS',
808 - SIMPLEPIE_TYPE_ATOM_03 => 'Atom 0.3',
809 - SIMPLEPIE_TYPE_ATOM_10 => 'Atom 1.0',
810 - SIMPLEPIE_TYPE_ATOM_ALL => 'Atom (any version)',
811 - SIMPLEPIE_TYPE_ALL => 'Supported Feed (unspecified format)',
812 - );
813 -
814 - $type = "Unknown or unsupported format";
815 - foreach ($bitmasks as $format_flag => $format_string) :
816 - if (is_numeric($format_flag)) : // Guard against failure of constants to be defined.
817 - if ($type_code & $format_flag) :
818 - $type = $format_string;
819 - break; // foreach
820 - endif;
821 - endif;
822 - endforeach;
823 -
824 - return $type;
825 - } /* SyndicatedLink::get_feed_type () */
826 -
827 - public function uri ($params = array()) {
693 + } /* SyndicatedLink::is_incremental () */
694 +
695 + function uri ($params = array()) {
828 696 $params = wp_parse_args($params, array(
829 697 'add_params' => false,
830 - 'fetch' => false,
831 698 ));
832 -
833 - // Initialize $qp (= array for added query parameters, if any)
834 - $qp = array();
835 -
836 - $link_rss = (is_object($this->link) ? $this->link->link_rss : NULL);
837 -
838 - // $link_rss stores the URI for the subscription as stored in the feed's record.
839 - // $uri stores the effective URI of the request including any/all added query parameters
840 - $uri = $link_rss;
841 - if ( !is_null($uri) and strlen($uri) > 0 and $params['add_params']) :
699 +
700 + $uri = (is_object($this->link) ? $this->link->link_rss : NULL);
701 + if (!is_null($uri) and strlen($uri) > 0 and $params['add_params']) :
842 702 $qp = maybe_unserialize($this->setting('query parameters', array()));
843 -
703 +
844 704 // For high-tech HTTP feed request kung fu
845 705 $qp = apply_filters('syndicated_feed_parameters', $qp, $uri, $this);
846 -
847 - // $qp is an array of key-value pairs stored as arrays of format [$key, $value]
706 +
848 707 $q = array();
849 708 if (is_array($qp) and count($qp) > 0) :
850 709 foreach ($qp as $pair) :
851 710 $q[] = urlencode($pair[0]).'='.urlencode($pair[1]);
852 711 endforeach;
853 -
712 +
854 713 // Are we appending to a URI that already has params?
855 714 $sep = ((strpos($uri, "?")===false) ? '?' : '&');
856 -
715 +
857 716 // Tack it on
858 717 $uri .= $sep . implode("&", $q);
859 - endif;
718 + endif;
860 719 endif;
861 -
862 - // Do we have any filters that apply here?
863 - $uri = apply_filters('syndicated_link_uri', $uri, $link_rss, $qp, $params, $this);
864 -
865 - // Return the filtered link URI.
720 +
866 721 return $uri;
867 722 } /* SyndicatedLink::uri () */
868 723
869 - public function username () {
724 + function username () {
870 725 return $this->setting('http username', 'http_username', NULL);
871 726 } /* SyndicatedLink::username () */
872 -
873 - public function password () {
727 +
728 + function password () {
874 729 return $this->setting('http password', 'http_password', NULL);
875 730 } /* SyndicatedLink::password () */
876 -
877 - public function authentication_method () {
878 - // Retrieve the authentication method from settings
879 - $auth = $this->setting( 'http auth method', null );
880 -
881 - // If the value is '-' or empty, treat it as NULL
882 - if ( '-' === $auth || empty( $auth ) ) {
883 - $auth = null;
884 - }
885 -
731 +
732 + function authentication_method () {
733 + $auth = $this->setting('http auth method', NULL);
734 + if (('-' == $auth) or (strlen($auth)==0)) :
735 + $auth = NULL;
736 + endif;
886 737 return $auth;
887 738 } /* SyndicatedLink::authentication_method () */
888 -
889 - var $postmeta = array();
890 - public function postmeta ($params = array()) {
739 +
740 + var $postmeta = array();
741 + function postmeta ($params = array()) {
891 742 $params = wp_parse_args($params, /*defaults=*/ array(
892 743 "field" => NULL,
893 744 "parsed" => false,
894 745 "force" => false,
@@ -899,49 +750,48 @@
899 750 $default_custom_settings = get_option('feedwordpress_custom_settings');
900 751 if ($default_custom_settings and !is_array($default_custom_settings)) :
901 752 $default_custom_settings = unserialize($default_custom_settings);
902 753 endif;
903 - if ( !is_array($default_custom_settings)) :
754 + if (!is_array($default_custom_settings)) :
904 755 $default_custom_settings = array();
905 756 endif;
906 -
757 +
907 758 // Next, get the settings for this particular feed.
908 759 $custom_settings = $this->setting('postmeta', NULL, NULL);
909 760 if ($custom_settings and !is_array($custom_settings)) :
910 761 $custom_settings = unserialize($custom_settings);
911 762 endif;
912 - if ( !is_array($custom_settings)) :
763 + if (!is_array($custom_settings)) :
913 764 $custom_settings = array();
914 765 endif;
915 -
766 +
916 767 $this->postmeta[/*parsed=*/ false] = array_merge($default_custom_settings, $custom_settings);
917 768 $this->postmeta[/*parsed=*/ true] = array();
918 -
769 +
919 770 // Now, run through and parse them all.
920 771 foreach ($this->postmeta[/*parsed=*/ false] as $key => $meta) :
921 - $meta = apply_filters("syndicated_link_post_meta_{$key}_pre", $meta, $this);
772 + $meta = apply_filters("syndicated_link_post_meta_${key}_pre", $meta, $this);
922 773 $this->postmeta[/*parsed=*/ false][$key] = $meta;
923 774 $this->postmeta[/*parsed=*/ true][$key] = new FeedWordPressParsedPostMeta($meta);
924 775 endforeach;
925 776 endif;
926 777
927 - $ret = $this->postmeta[!! $params['parsed']];
778 + $ret = $this->postmeta[!!$params['parsed']];
928 779 if (is_string($params['field'])) :
929 780 $ret = $ret[$params['field']];
930 781 endif;
931 782 return $ret;
932 783 } /* SyndicatedLink::postmeta () */
933 -
934 - public function property_cascade ($fromFeed, $link_field, $setting, $method) {
784 +
785 + function property_cascade ($fromFeed, $link_field, $setting, $method) {
935 786 $value = NULL;
936 787 if ($fromFeed) :
937 788 $value = $this->setting($setting, NULL, NULL, NULL);
938 -
939 - $simp_pie = $this->simplepie;
940 - $callable = ( is_object( $simp_pie ) and method_exists( $simp_pie, $method ) );
941 - if ( is_null( $value ) and $callable ) :
942 -// $fallback = $s->{$method}(); // what is fallback supposed to be here? (gwyneth 20230816)
943 - $value = $simp_pie->{$method}(); // makes more sense this way!
789 +
790 + $s = $this->simplepie;
791 + $callable = (is_object($s) and method_exists($s, $method));
792 + if (is_null($value) and $callable) :
793 + $fallback = $s->{$method}();
944 794 endif;
945 795 else :
946 796 $value = $this->link->{$link_field};
947 797 endif;
@@ -946,24 +796,23 @@
946 796 $value = $this->link->{$link_field};
947 797 endif;
948 798 return $value;
949 799 } /* SyndicatedLink::property_cascade () */
950 -
951 - public function homepage ($fromFeed = true) {
800 +
801 + function homepage ($fromFeed = true) {
952 802 return $this->property_cascade($fromFeed, 'link_url', 'feed/link', 'get_link');
953 803 } /* SyndicatedLink::homepage () */
954 804
955 - public function name ($fromFeed = true) {
805 + function name ($fromFeed = true) {
956 806 return $this->property_cascade($fromFeed, 'link_name', 'feed/title', 'get_title');
957 807 } /* SyndicatedLink::name () */
958 808
959 - public function guid () {
809 + function guid () {
960 810 $ret = $this->setting('feed/id', NULL, $this->uri());
961 -
811 +
962 812 // If we can get it live from the feed, do so.
963 813 if (is_object($this->simplepie)) :
964 814 $search = array(
965 - array('', 'id'),
966 815 array(SIMPLEPIE_NAMESPACE_ATOM_10, 'id'),
967 816 array(SIMPLEPIE_NAMESPACE_ATOM_03, 'id'),
968 817 array(SIMPLEPIE_NAMESPACE_RSS_20, 'guid'),
969 818 array(SIMPLEPIE_NAMESPACE_DC_11, 'identifier'),
@@ -968,9 +817,9 @@
968 817 array(SIMPLEPIE_NAMESPACE_RSS_20, 'guid'),
969 818 array(SIMPLEPIE_NAMESPACE_DC_11, 'identifier'),
970 819 array(SIMPLEPIE_NAMESPACE_DC_10, 'identifier'),
971 820 );
972 -
821 +
973 822 foreach ($search as $pair) :
974 823 if ($id_tags = $this->simplepie->get_feed_tags($pair[0], $pair[1])) :
975 824 $ret = $id_tags[0]['data'];
976 825 break;
@@ -982,63 +831,9 @@
982 831 endif;
983 832 return $ret;
984 833 }
985 834
986 - public function links ($params = array()) {
987 - $params = wp_parse_args($params, array(
988 - "rel" => NULL,
989 - ));
990 -
991 - $fLinks = array();
992 - $search = array(
993 - array('', 'link'),
994 - array(SIMPLEPIE_NAMESPACE_ATOM_10, 'link'),
995 - array(SIMPLEPIE_NAMESPACE_ATOM_03, 'link'),
996 - );
997 -
998 - foreach ($search as $pair) :
999 - if ($link_tags = $this->simplepie->get_feed_tags($pair[0], $pair[1])) :
1000 - $fLinks = array_merge($fLinks, $link_tags);
1001 - endif;
1002 - if ($link_tags = $this->simplepie->get_channel_tags($pair[0], $pair[1])) :
1003 - $fLinks = array_merge($fLinks, $link_tags);
1004 - endif;
1005 - endforeach;
1006 -
1007 - $ret = array();
1008 - foreach ($fLinks as $link) :
1009 - $filter = false;
1010 - if ( !is_null($params['rel'])) :
1011 - $filter = true;
1012 -
1013 - if (isset($link['attribs'])) :
1014 - // Get a list of NSes from the search
1015 - foreach ($search as $pair) :
1016 - $ns = $pair[0];
1017 -
1018 - if (isset($link['attribs'][$ns])
1019 - and isset($link['attribs'][$ns]['rel'])
1020 - ) :
1021 - $rel = strtolower(trim($link['attribs'][$ns]['rel']));
1022 - $fRel = strtolower(trim($params['rel']));
1023 -
1024 - if ($rel == $fRel) :
1025 - $filter = false;
1026 - endif;
1027 - endif;
1028 - endforeach;
1029 - endif;
1030 - endif;
1031 -
1032 - if ( ! $filter) :
1033 - $ret[] = $link;
1034 - endif;
1035 - endforeach;
1036 -
1037 - return $ret;
1038 - }
1039 -
1040 - public function ttl ($return_element = false) {
835 + function ttl ($return_element = false) {
1041 836 if (is_object($this->magpie)) :
1042 837 $channel = $this->magpie->channel;
1043 838 else :
1044 839 $channel = array();
@@ -1047,9 +842,9 @@
1047 842 if (isset($channel['ttl'])) :
1048 843 // "ttl stands for time to live. It's a number of
1049 844 // minutes that indicates how long a channel can be
1050 845 // cached before refreshing from the source."
1051 - // <http://blogs.law.harvard.edu/tech/rss#ltttlgtSubelementOfLtchannelgt>
846 + // <http://blogs.law.harvard.edu/tech/rss#ltttlgtSubelementOfLtchannelgt>
1052 847 $xml = 'rss:ttl';
1053 848 $ret = $channel['ttl'];
1054 849 elseif (isset($channel['sy']['updatefrequency']) or isset($channel['sy']['updateperiod'])) :
1055 850 $period_minutes = array (
@@ -1063,13 +858,13 @@
1063 858 // "sy:updatePeriod: Describes the period over which the
1064 859 // channel format is updated. Acceptable values are:
1065 860 // hourly, daily, weekly, monthly, yearly. If omitted,
1066 861 // daily is assumed." <http://web.resource.org/rss/1.0/modules/syndication/>
1067 - if (isset($channel['sy']['updateperiod'])) : $period = trim($channel['sy']['updateperiod']);
862 + if (isset($channel['sy']['updateperiod'])) : $period = $channel['sy']['updateperiod'];
1068 863 else : $period = 'daily';
1069 864 endif;
1070 -
1071 - // "sy:updateFrequency: Used to describe the frequency
865 +
866 + // "sy:updateFrequency: Used to describe the frequency
1072 867 // of updates in relation to the update period. A
1073 868 // positive integer indicates how many times in that
1074 869 // period the channel is updated. ... If omitted a value
1075 870 // of 1 is assumed." <http://web.resource.org/rss/1.0/modules/syndication/>
@@ -1075,219 +870,86 @@
1075 870 // of 1 is assumed." <http://web.resource.org/rss/1.0/modules/syndication/>
1076 871 if (isset($channel['sy']['updatefrequency'])) : $freq = (int) $channel['sy']['updatefrequency'];
1077 872 else : $freq = 1;
1078 873 endif;
1079 -
1080 - // normalize the period name...
1081 - $period = strtolower(trim($period));
1082 -
1083 - // do we recognize the alphanumeric period name? if not, then guess
1084 - // a responsible default, e.g. roughly hourly
1085 - $mins = (isset($period_minutes[$period]) ? $period_minutes[$period] : 67);
1086 -
874 +
1087 875 $xml = 'sy:updateFrequency';
1088 - $ret = (int) ($mins / $freq);
1089 -
876 + $ret = (int) ($period_minutes[$period] / $freq);
1090 877 else :
1091 878 $xml = NULL;
1092 879 $ret = NULL;
1093 880 endif;
1094 -
881 +
1095 882 if ('yes'==$this->setting('update/minimum', 'update_minimum', 'no')) :
1096 883 $min = (int) $this->setting('update/window', 'update_window', DEFAULT_UPDATE_PERIOD);
1097 -
884 +
1098 885 if ($min > $ret) :
1099 886 $ret = NULL;
1100 887 endif;
1101 888 endif;
1102 - return ( $return_element ? array( $ret, $xml ) : $ret );
889 + return ($return_element ? array($ret, $xml) : $ret);
1103 890 } /* SyndicatedLink::ttl() */
1104 891
1105 - /**
1106 - * Sets the automatic time interval for
1107 - *
1108 - * @return
1109 - */
1110 - public function automatic_ttl() {
892 + function automatic_ttl () {
1111 893 // spread out over a time interval for staggered updates
1112 - $updateWindow = $this->setting( 'update/window', 'update_window', DEFAULT_UPDATE_PERIOD );
1113 - if ( ! is_numeric( $updateWindow ) or ( $updateWindow < 1 ) ) :
894 + $updateWindow = $this->setting('update/window', 'update_window', DEFAULT_UPDATE_PERIOD);
895 + if (!is_numeric($updateWindow) or ($updateWindow < 1)) :
1114 896 $updateWindow = DEFAULT_UPDATE_PERIOD;
1115 897 endif;
1116 898
1117 899 // We get a fudge of 1/3 of window from elsewhere. We'll do some more
1118 900 // fudging here.
1119 - $fudgedInterval = $updateWindow + rand( - ( $updateWindow / 6 ), 5 * ( $updateWindow / 12 ) );
1120 - return apply_filters( 'syndicated_feed_automatic_ttl', $fudgedInterval, $this );
1121 - } /* SyndicatedLink::automatic_ttl() */
901 + $fudgedInterval = $updateWindow+rand(-($updateWindow/6), 5*($updateWindow/12));
902 + return apply_filters('syndicated_feed_automatic_ttl', $fudgedInterval, $this);
903 + } /* SyndicatedLink::automatic_ttl () */
1122 904
1123 - /**
1124 - * SyndicatedLink::flatten_array (): flatten an array. Useful for
1125 - * hierarchical and namespaced elements.
1126 - *
1127 - * Given an array which may contain array or object elements in it,
1128 - * return a "flattened" array: a one-dimensional array of scalars
1129 - * containing each of the scalar elements contained within the array
1130 - * structure. Thus, for example, if $a['b']['c']['d'] == 'e', then the
1131 - * returned array for FeedWordPress::flatten_array($a) will contain a key
1132 - * $a['feed/b/c/d'] with value 'e'.
1133 - *
1134 - * @param array $arr Array to flatten.
1135 - * @param string $prefix Hierarchical prefix (e.g. "feed/" as in "feed/a/b/c").
1136 - * @param string $separator Hierarchy separator (e.g., '/').
1137 - * @return array Flattened one-dimensional array.
1138 - */
1139 - public function flatten_array( $arr, $prefix = 'feed/', $separator = '/' ) {
1140 - $ret = array();
1141 - if ( is_array( $arr ) ) :
1142 - foreach ( $arr as $key => $value ) :
1143 - if ( is_scalar( $value ) ) :
1144 - $ret[ $prefix.$key ] = $value;
905 + // SyndicatedLink::flatten_array (): flatten an array. Useful for
906 + // hierarchical and namespaced elements.
907 + //
908 + // Given an array which may contain array or object elements in it,
909 + // return a "flattened" array: a one-dimensional array of scalars
910 + // containing each of the scalar elements contained within the array
911 + // structure. Thus, for example, if $a['b']['c']['d'] == 'e', then the
912 + // returned array for FeedWordPress::flatten_array($a) will contain a key
913 + // $a['feed/b/c/d'] with value 'e'.
914 + function flatten_array ($arr, $prefix = 'feed/', $separator = '/') {
915 + $ret = array ();
916 + if (is_array($arr)) :
917 + foreach ($arr as $key => $value) :
918 + if (is_scalar($value)) :
919 + $ret[$prefix.$key] = $value;
1145 920 else :
1146 - $ret = array_merge( $ret, $this->flatten_array( $value, $prefix.$key.$separator, $separator ) );
921 + $ret = array_merge($ret, $this->flatten_array($value, $prefix.$key.$separator, $separator));
1147 922 endif;
1148 923 endforeach;
1149 924 endif;
1150 925 return $ret;
1151 - } /* SyndicatedLink::flatten_array() */
926 + } /* SyndicatedLink::flatten_array () */
1152 927
1153 - /**
1154 - * Check if a setting is hardcoded or not.
1155 - *
1156 - * @param string $what The setting we're checking for.
1157 - *
1158 - * @return bool True if the setting's value is 'yes', false otherwise.
1159 - */
1160 - public function hardcode( $what ) {
1161 - return 'yes' == $this->setting( 'hardcode ' . $what, 'hardcode_' . $what, NULL );
1162 - // Simplified logic. (gwyneth 20230920)
928 + function hardcode ($what) {
929 +
930 + $ret = $this->setting('hardcode '.$what, 'hardcode_'.$what, NULL);
931 +
932 + if ('yes' == $ret) :
933 + $ret = true;
934 + else :
935 + $ret = false;
936 + endif;
937 + return $ret;
1163 938 } /* SyndicatedLink::hardcode () */
1164 939
1165 - /**
1166 - * Returns the syndicated status for one item.
1167 - *
1168 - * @param string $what Item's name.
1169 - * @param mixed $default Default value for this item.
1170 - * @param boolean $fallback If true, falls back to global setting.
1171 - *
1172 - * @return string SQL-ready escaped status.
1173 - */
1174 - public function syndicated_status( $what, $default, $fallback = true ) {
1175 - $g_set = ( $fallback ? 'syndicated_' . $what . '_status' : NULL );
1176 - $ret = $this->setting( $what . ' status', $g_set, $default );
940 + function syndicated_status ($what, $default, $fallback = true) {
941 + global $wpdb;
1177 942
1178 - return esc_sql( trim( strtolower( $ret ) ) );
943 + $g_set = ($fallback ? 'syndicated_' . $what . '_status' : NULL);
944 + $ret = $this->setting($what.' status', $g_set, $default);
945 +
946 + return $wpdb->escape(trim(strtolower($ret)));
1179 947 } /* SyndicatedLink:syndicated_status () */
1180 -
1181 - public function taxonomies () {
948 +
949 + function taxonomies () {
1182 950 $post_type = $this->setting('syndicated post type', 'syndicated_post_type', 'post');
1183 951 return get_object_taxonomies(array('object_type' => $post_type), 'names');
1184 952 } /* SyndicatedLink::taxonomies () */
1185 -
1186 - /**
1187 - * category_ids: look up (and create) category ids from a list of
1188 - * categories
1189 - *
1190 - * @param array $cats
1191 - * @param string $unfamiliar_category
1192 - * @param array|null $taxonomies
1193 - * @return array
1194 - */
1195 - public function category_ids ($post, $cats, $unfamiliar_category = 'create', $taxonomies = NULL, $params = array()) {
1196 - $singleton = (isset($params['singleton']) ? $params['singleton'] : true);
1197 - $allowFilters = (isset($params['filters']) ? $params['filters'] : false);
1198 -
1199 - $catTax = 'category';
1200 -
1201 - if (is_null($taxonomies)) :
1202 - $taxonomies = array('category');
1203 - endif;
1204 -
1205 - // We need to normalize whitespace because (1) trailing
1206 - // whitespace can cause PHP and MySQL not to see eye to eye on
1207 - // VARCHAR comparisons for some versions of MySQL (cf.
1208 - // <http://dev.mysql.com/doc/mysql/en/char.html>), and (2)
1209 - // because I doubt most people want to make a semantic
1210 - // distinction between 'Computers' and 'Computers '
1211 - $cats = array_map('trim', $cats);
1212 -
1213 - $terms = array();
1214 - foreach ($taxonomies as $tax) :
1215 - $terms[$tax] = array();
1216 - endforeach;
1217 -
1218 - foreach ($cats as $cat_name) :
1219 - if (strlen(trim($cat_name)) < 1) :
1220 - continue;
1221 - endif;
1222 -
1223 - $oTerm = new SyndicatedPostTerm($cat_name, $taxonomies, $post);
1224 -
1225 - if ($oTerm->is_familiar()) :
1226 -
1227 - $tax = $oTerm->taxonomy();
1228 - if ( !isset($terms[$tax])) :
1229 - $terms[$tax] = array();
1230 - endif;
1231 - $terms[$tax][] = $oTerm->id();
1232 -
1233 - else :
1234 -
1235 - if ('tag'==$unfamiliar_category) :
1236 - $unfamiliar_category = 'create:post_tag';
1237 - endif;
1238 -
1239 - if (preg_match('/^create(:(.*))?$/i', $unfamiliar_category, $ref)) :
1240 - $tax = $catTax; // Default
1241 -
1242 - if (isset($ref[2])
1243 - and strlen($ref[2]) > 2) :
1244 - $tax = $ref[2];
1245 - endif;
1246 -
1247 - $inserted = $oTerm->insert($tax);
1248 - if ( !is_null($inserted)) :
1249 - if ( !isset($terms[$tax])) :
1250 - $terms[$tax] = array();
1251 - endif;
1252 - $terms[$tax][] = $inserted;
1253 - else :
1254 -
1255 - endif; // !is_null($inserted)
1256 - endif; // preg_match(...)
1257 -
1258 - endif; /* ($oTerm->is_familiar()) */
1259 - endforeach;
1260 -
1261 - $filtersOn = $allowFilters;
1262 - if ($allowFilters) :
1263 - $filters = array_filter(
1264 - $this->setting('match/filter', 'match_filter', array()),
1265 - 'remove_dummy_zero'
1266 - );
1267 - $filtersOn = ($filtersOn and is_array($filters) and (count($filters) > 0));
1268 - endif;
1269 -
1270 - // Check for filter conditions
1271 - foreach ($terms as $tax => $term_ids) :
1272 - if ($filtersOn
1273 - and (count($term_ids)==0)
1274 - and in_array($tax, $filters)) :
1275 - $terms = NULL; // Drop the post
1276 - break;
1277 - else :
1278 - $terms[$tax] = array_unique($term_ids);
1279 - endif;
1280 - endforeach;
1281 -
1282 - if ($singleton and count($terms)==1) : // If we only searched one, just return the term IDs
1283 - $terms = end($terms);
1284 - endif;
1285 -
1286 - FeedWordPress::diagnostic(
1287 - 'syndicated_posts:categories',
1288 - 'Category: MAPPED term names '.json_encode($cats).' to IDs: '.json_encode($terms)
1289 - );
1290 - return $terms;
1291 - } /* SyndicatedLink::category_ids () */
1292 -} /* class SyndicatedLink */
953 +
954 +} // class SyndicatedLink
1293 955