PluginProbe
FeedWordPress / 2016.1211
FeedWordPress v2016.1211
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 +203 -338 trunk2016.1211 View file →
@@ -32,10 +32,10 @@
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,9 +42,11 @@
42 42 var $settings = array ();
43 43 public $simplepie = null;
44 44 var $magpie = null;
45 45
46 - public function __construct( $link ) {
46 + function __construct( $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,26 +53,28 @@
51 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 () */
62 + } /* SyndicatedLink::SyndicatedLink () */
63 63
64 - public function found () {
64 + function SyndicatedLink( $link ) {
65 + self::__construct( $link );
66 + }
67 +
68 + function found () {
65 69 return is_object($this->link) and !is_wp_error($this->link);
66 70 } /* SyndicatedLink::found () */
67 71
68 - public function id () {
72 + function id () {
69 73 return (is_object($this->link) ? $this->link->link_id : NULL);
70 74 }
71 75
72 - public function stale () {
76 + function stale () {
73 77 global $feedwordpress;
74 78
75 79 $stale = true;
76 80 if ($this->setting('update/hold')=='ping') :
@@ -76,11 +80,11 @@
76 80 if ($this->setting('update/hold')=='ping') :
77 81 $stale = false; // don't update on any timed updates; pings only
78 82 elseif ($this->setting('update/hold')=='next') :
79 83 $stale = true; // update on the next timed update
80 - elseif ( ! $this->setting('update/last') ) :
84 + elseif ( !$this->setting('update/last') ) :
81 85 $stale = true; // initial update
82 - elseif ( !empty($feedwordpress) and $feedwordpress->force_update_all()) :
86 + elseif ($feedwordpress->force_update_all()) :
83 87 $stale = true; // forced general updating
84 88 else :
85 89 $after = (
86 90 (int) $this->setting('update/last')
@@ -91,9 +95,9 @@
91 95 endif;
92 96 return $stale;
93 97 } /* SyndicatedLink::stale () */
94 98
95 - public function fetch () {
99 + function fetch () {
96 100 $timeout = $this->setting('fetch timeout', 'feedwordpress_fetch_timeout', FEEDWORDPRESS_FETCH_TIMEOUT_DEFAULT);
97 101
98 102 $this->simplepie = apply_filters(
99 103 'syndicated_feed',
@@ -106,12 +110,11 @@
106 110 $this->magpie = $this->simplepie;
107 111 else :
108 112 $this->magpie = new MagpieFromSimplePie($this->simplepie, NULL);
109 113 endif;
110 - } /* SyndicatedLink::fetch () */
111 -
112 - public function live_posts () {
113 - if ( !is_object($this->simplepie)) :
114 + }
115 + function live_posts () {
116 + if (!is_object($this->simplepie)) :
114 117 $this->fetch();
115 118 endif;
116 119
117 120 if (is_object($this->simplepie) and method_exists($this->simplepie, 'get_items')) :
@@ -123,22 +126,18 @@
123 126 else :
124 127 $ret = $this->simplepie;
125 128 endif;
126 129 return $ret;
127 - } /* SyndicatedLink::live_posts () */
130 + }
128 131
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) {
132 + function poll ($crash_ts = NULL) {
134 133 global $wpdb;
135 134
136 135 $url = $this->uri(array('add_params' => true, 'fetch' => true));
137 136 FeedWordPress::diagnostic('updated_feeds', 'Polling feed ['.$url.']');
138 -
137 +
139 138 $this->fetch();
140 -
139 +
141 140 $new_count = NULL;
142 141
143 142 $resume = ('yes'==$this->setting('update/unfinished'));
144 143 if ($resume) :
@@ -161,9 +160,9 @@
161 160 if (is_string($oldError)) :
162 161 $oldError = unserialize($oldError);
163 162 endif;
164 163
165 - if ( !is_null($oldError)) :
164 + if (!is_null($oldError)) :
166 165 // Copy over the in-error-since timestamp
167 166 $theError['since'] = $oldError['since'];
168 167
169 168 // If this is a repeat error, then we should
@@ -172,9 +171,9 @@
172 171 $this->update_setting('update/last', time(), NULL);
173 172 $ttl = $this->automatic_ttl();
174 173 $ttl = apply_filters('syndicated_feed_ttl', $ttl, $this);
175 174 $ttl = apply_filters('syndicated_feed_ttl_from_error', $ttl, $this);
176 - $this->update_setting('update/ttl', $ttl);
175 + $this->update_setting('update/ttl', $ttl, $this);
177 176 $this->update_setting('update/timed', 'automatically');
178 177 endif;
179 178
180 179 do_action('syndicated_feed_error', $theError, $oldError, $this);
@@ -186,27 +185,27 @@
186 185
187 186 // Success; clear out error setting, if any.
188 187 $this->update_setting('update/error', NULL);
189 188
190 - $new_count = array('new' => 0, 'updated' => 0, 'stored' => 0);
189 + $new_count = array('new' => 0, 'updated' => 0);
191 190
192 191 # -- Update Link metadata live from feed
193 192 $channel = $this->magpie->channel;
194 193
195 - if ( !isset($channel['id'])) :
194 + if (!isset($channel['id'])) :
196 195 $channel['id'] = $this->link->link_rss;
197 196 endif;
198 197
199 198 $update = array();
200 - if ( ! $this->hardcode('url') and isset($channel['link'])) :
199 + if (!$this->hardcode('url') and isset($channel['link'])) :
201 200 $update[] = "link_url = '".esc_sql($channel['link'])."'";
202 201 endif;
203 202
204 - if ( ! $this->hardcode('name') and isset($channel['title'])) :
203 + if (!$this->hardcode('name') and isset($channel['title'])) :
205 204 $update[] = "link_name = '".esc_sql($channel['title'])."'";
206 205 endif;
207 206
208 - if ( ! $this->hardcode('description')) :
207 + if (!$this->hardcode('description')) :
209 208 if (isset($channel['tagline'])) :
210 209 $update[] = "link_description = '".esc_sql($channel['tagline'])."'";
211 210 elseif (isset($channel['description'])) :
212 211 $update[] = "link_description = '".esc_sql($channel['description'])."'";
@@ -212,16 +211,31 @@
212 211 $update[] = "link_description = '".esc_sql($channel['description'])."'";
213 212 endif;
214 213 endif;
215 214
216 - $this->update_setting('link/feed_type', $this->simplepie->get_type());
217 -
218 215 $this->merge_settings($channel, 'feed/');
219 216
220 217 $this->update_setting('update/last', time());
221 - $this->do_update_ttl();
218 + list($ttl, $xml) = $this->ttl(/*return element=*/ true);
222 219
223 - if ( ! $this->setting('update/hold') != 'ping') :
220 + if (!is_null($ttl)) :
221 + $this->update_setting('update/ttl', $ttl);
222 + $this->update_setting('update/xml', $xml);
223 + $this->update_setting('update/timed', 'feed');
224 + else :
225 + $ttl = $this->automatic_ttl();
226 + $this->update_setting('update/ttl', $ttl);
227 + $this->update_setting('update/xml', NULL);
228 + $this->update_setting('update/timed', 'automatically');
229 + endif;
230 + $this->update_setting('update/fudge', rand(0, ($ttl/3))*60);
231 + $this->update_setting('update/ttl', apply_filters(
232 + 'syndicated_feed_ttl',
233 + $this->setting('update/ttl'),
234 + $this
235 + ));
236 +
237 + if (!$this->setting('update/hold') != 'ping') :
224 238 $this->update_setting('update/hold', 'scheduled');
225 239 endif;
226 240
227 241 $this->update_setting('update/unfinished', 'yes');
@@ -241,9 +255,9 @@
241 255 # -- Add new posts from feed and update any updated posts
242 256 $crashed = false;
243 257
244 258 $posts = $this->live_posts();
245 -
259 +
246 260 $this->magpie->originals = $posts;
247 261
248 262 // If this is a complete feed, rather than an incremental feed, we
249 263 // need to prepare to mark everything for presumptive retirement.
@@ -261,26 +275,25 @@
261 275 endif;
262 276
263 277 if (is_array($posts)) :
264 278 foreach ($posts as $key => $item) :
265 - if ( ! $this->pause_updates()) :
266 - $post = new SyndicatedPost($item, $this);
279 + $post = new SyndicatedPost($item, $this);
267 280
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;
281 + if (!$resume or !in_array(trim($post->guid()), $processed)) :
282 + $processed[] = $post->guid();
283 + if (!$post->filtered()) :
284 + $new = $post->store();
285 + if ( $new !== false ) $new_count[$new]++;
286 + endif;
274 287
275 - if ( !is_null($crash_ts) and (time() > $crash_ts)) :
276 - $crashed = true;
277 - break;
278 - endif;
288 + if (!is_null($crash_ts) and (time() > $crash_ts)) :
289 + $crashed = true;
290 + break;
279 291 endif;
292 + endif;
280 293
281 - unset($post);
282 - endif;
294 + unset($post);
295 +
283 296 endforeach;
284 297 endif;
285 298
286 299 if ('yes'==$this->setting('tombstones', 'tombstones', 'yes')) :
@@ -286,9 +299,9 @@
286 299 if ('yes'==$this->setting('tombstones', 'tombstones', 'yes')) :
287 300 // Check for use of Atom tombstones. Spec:
288 301 // <http://tools.ietf.org/html/draft-snell-atompub-tombstones-18>
289 302 $tombstones = $this->simplepie->get_feed_tags('http://purl.org/atompub/tombstones/1.0', 'deleted-entry');
290 - if ( !is_null($tombstones) && count($tombstones) > 0) :
303 + if (count($tombstones) > 0) :
291 304 foreach ($tombstones as $tombstone) :
292 305 $ref = NULL;
293 306 foreach (array('', 'http://purl.org/atompub/tombstones/1.0') as $ns) :
294 307 if (isset($tombstone['attribs'][$ns])
@@ -316,12 +329,12 @@
316 329 endif;
317 330
318 331 $suffix = ($crashed ? 'crashed' : 'completed');
319 332 do_action('update_syndicated_feed_items', $this->id, $this);
320 - do_action("update_syndicated_feed_items_{$suffix}", $this->id, $this);
333 + do_action("update_syndicated_feed_items_${suffix}", $this->id, $this);
321 334
322 335 $this->update_setting('update/processed', $processed);
323 - if ( ! $crashed) :
336 + if (!$crashed) :
324 337 $this->update_setting('update/unfinished', 'no');
325 338 endif;
326 339 $this->update_setting('link/item count', count($posts));
327 340
@@ -326,22 +339,16 @@
326 339 $this->update_setting('link/item count', count($posts));
327 340
328 341 // Copy back any changes to feed settings made in the
329 342 // course of updating (e.g. new author rules)
330 - $update_set = "link_notes = '" . esc_sql($this->settings_to_notes())."'";
343 + $update_set = "link_notes = '".esc_sql($this->settings_to_notes())."'";
331 344
332 345 // Update the properties of the link from the feed information
333 346 $result = $wpdb->query("
334 - UPDATE {$wpdb->links}
335 - SET {$update_set}
336 - WHERE link_id='{$this->id}'
347 + UPDATE $wpdb->links
348 + SET $update_set
349 + WHERE link_id='$this->id'
337 350 ");
338 - if ( FALSE === $result ) :
339 - FeedWordPress::diagnostic(
340 - 'SyndicatedLink/poll',
341 - 'Database update failed',
342 - );
343 - endif;
344 351
345 352 do_action("update_syndicated_feed_completed", $this->id, $this);
346 353 endif;
347 354
@@ -357,43 +364,11 @@
357 364
358 365 return $new_count;
359 366 } /* SyndicatedLink::poll() */
360 367
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);
368 + function process_retirements ($delta) {
369 + global $post;
367 370
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 371 $q = new WP_Query(array(
397 372 'fields' => '_synfrom',
398 373 'post_status__not' => 'fwpretired',
399 374 'ignore_sticky_posts' => true,
@@ -410,9 +385,9 @@
410 385 endforeach;
411 386 endif;
412 387
413 388 return $delta;
414 - } /* SyndicatedLink::process_retirements () */
389 + }
415 390
416 391 /**
417 392 * Updates the URL for the feed syndicated by this link.
418 393 *
@@ -418,9 +393,9 @@
418 393 *
419 394 * @param string $url The new feed URL to use for this source.
420 395 * @return bool TRUE on success, FALSE on failure.
421 396 */
422 - public function set_uri ($url) {
397 + function set_uri ($url) {
423 398 global $wpdb;
424 399
425 400 if ($this->found()) :
426 401 // Update link_rss
@@ -437,9 +412,9 @@
437 412 endif;
438 413 return $ret;
439 414 } /* SyndicatedLink::set_uri () */
440 415
441 - public function deactivate () {
416 + function deactivate () {
442 417 global $wpdb;
443 418
444 419 $wpdb->query($wpdb->prepare("
445 420 UPDATE $wpdb->links SET link_visible = 'N' WHERE link_id = %d
@@ -445,20 +420,9 @@
445 420 UPDATE $wpdb->links SET link_visible = 'N' WHERE link_id = %d
446 421 ", (int) $this->id));
447 422 } /* SyndicatedLink::deactivate () */
448 423
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 () {
424 + function delete () {
461 425 global $wpdb;
462 426
463 427 $wpdb->query($wpdb->prepare("
464 428 DELETE FROM $wpdb->postmeta WHERE meta_key='syndication_feed_id'
@@ -471,18 +435,9 @@
471 435
472 436 $this->id = NULL;
473 437 } /* SyndicatedLink::delete () */
474 438
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 () {
439 + function nuke () {
485 440 global $wpdb;
486 441
487 442 // Make a list of the items syndicated from this feed...
488 443 $post_ids = $wpdb->get_col($wpdb->prepare("
@@ -505,9 +460,9 @@
505 460
506 461 $this->delete();
507 462 } /* SyndicatedLink::nuke () */
508 463
509 - public function map_name_to_new_user ($name, $newuser_name) {
464 + function map_name_to_new_user ($name, $newuser_name) {
510 465 global $wpdb;
511 466
512 467 if (strlen($newuser_name) > 0) :
513 468 $newuser_id = fwp_insert_new_user($newuser_name);
@@ -526,13 +481,13 @@
526 481 // TODO: Add some error reporting
527 482 endif;
528 483 } /* SyndicatedLink::map_name_to_new_user () */
529 484
530 - protected function imploded_settings () {
485 + function imploded_settings () {
531 486 return array('cats', 'tags', 'match/cats', 'match/tags', 'match/filter');
532 - } /* SyndicatedLink::imploded_settings () */
487 + }
533 488
534 - protected function get_settings_from_notes () {
489 + function get_settings_from_notes () {
535 490 // Read off feed settings from link_notes
536 491 $notes = explode("\n", $this->link->link_notes);
537 492 foreach ($notes as $note):
538 493 $pair = explode(": ", $note, 2);
@@ -537,9 +492,9 @@
537 492 foreach ($notes as $note):
538 493 $pair = explode(": ", $note, 2);
539 494 $key = (isset($pair[0]) ? $pair[0] : null);
540 495 $value = (isset($pair[1]) ? $pair[1] : null);
541 - if ( !is_null($key) and !is_null($value)) :
496 + if (!is_null($key) and !is_null($value)) :
542 497 // Unescape and trim() off the whitespace.
543 498 // Thanks to Ray Lischner for pointing out the
544 499 // need to trim off whitespace.
545 500 $this->settings[$key] = stripcslashes (trim($value));
@@ -568,9 +523,9 @@
568 523
569 524 // Set this up automagically for del.icio.us
570 525 $bits = parse_url($this->link->link_rss);
571 526 $tagspacers = array('del.icio.us', 'feeds.delicious.com');
572 - if ( !isset($this->settings['cat_split']) and in_array($bits['host'], $tagspacers)) :
527 + if (!isset($this->settings['cat_split']) and in_array($bits['host'], $tagspacers)) :
573 528 $this->settings['cat_split'] = '\s'; // Whitespace separates multiple tags in del.icio.us RSS feeds
574 529 endif;
575 530
576 531 // Simple lists
@@ -586,9 +541,9 @@
586 541 if (isset($this->settings['terms'])) :
587 542 // Look for new format
588 543 $this->settings['terms'] = maybe_unserialize($this->settings['terms']);
589 544
590 - if ( !is_array($this->settings['terms'])) :
545 + if (!is_array($this->settings['terms'])) :
591 546 // Deal with old format instead. Ugh.
592 547
593 548 // Split on two *or more* consecutive breaks
594 549 // because in the old format, a taxonomy
@@ -617,10 +572,9 @@
617 572 if (isset($this->settings['map authors'])) :
618 573 $author_rules = explode("\n\n", $this->settings['map authors']);
619 574 $ma = array();
620 575 foreach ($author_rules as $rule) :
621 - $parts = array_pad(explode("\n", $rule), 3, '');
622 - list($rule_type, $author_name, $author_action) = $parts;
576 + list($rule_type, $author_name, $author_action) = explode("\n", $rule);
623 577
624 578 // Normalize for case and whitespace
625 579 $rule_type = strtolower(trim($rule_type));
626 580 $author_name = strtolower(trim($author_name));
@@ -632,9 +586,9 @@
632 586 endif;
633 587
634 588 } /* SyndicatedLink::get_settings_from_notes () */
635 589
636 - protected function settings_to_notes () {
590 + function settings_to_notes () {
637 591 $to_notes = $this->settings;
638 592
639 593 unset($to_notes['link/id']); // Magic setting; don't save
640 594 unset($to_notes['link/uri']); // Magic setting; don't save
@@ -678,9 +632,9 @@
678 632 endforeach;
679 633 return $notes;
680 634 } /* SyndicatedLink::settings_to_notes () */
681 635
682 - public function save_settings ($reload = false) {
636 + function save_settings ($reload = false) {
683 637 global $wpdb;
684 638
685 639 // Save channel-level meta-data
686 640 foreach (array('link_name', 'link_description', 'link_url') as $what) :
@@ -698,13 +652,8 @@
698 652 SET $update_set
699 653 WHERE link_id='$this->id'
700 654 ");
701 655
702 - if ( $result === false )
703 - {
704 - error_log("query failed: " . $wpdb->last_error);
705 - }
706 -
707 656 if ($reload) :
708 657 // force reload of link information from DB
709 658 if (function_exists('clean_bookmark_cache')) :
710 659 clean_bookmark_cache($this->id);
@@ -715,117 +664,65 @@
715 664 /**
716 665 * Retrieves the value of a setting, allowing for a global setting to be
717 666 * used as a fallback, or a constant value, or both.
718 667 *
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!!
668 + * @param string $name The link setting key
669 + * @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.
670 + * @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.
671 + * @return bool TRUE on success, FALSE on failure.
728 672 */
729 - public function setting( $name, $fallback_global = NULL, $fallback_value = NULL, $default = 'default' ) {
673 + function setting ($name, $fallback_global = NULL, $fallback_value = NULL, $default = 'default') {
730 674 $ret = NULL;
731 - if ( isset( $this->settings[ $name ] ) ) :
732 - $ret = $this->settings[ $name ];
675 + if (isset($this->settings[$name])) :
676 + $ret = $this->settings[$name];
733 677 endif;
734 678
735 679 $no_value = (
736 - is_null( $ret )
737 - or ( is_string( $ret ) and strtolower( $ret ) == $default )
680 + is_null($ret)
681 + or (is_string($ret) and strtolower($ret)==$default)
738 682 );
739 683
740 - if ( $no_value and ! is_null( $fallback_global ) ) :
684 + if ($no_value and !is_null($fallback_global)) :
741 685 // Avoid duplication of this correction
742 - $fallback_global = preg_replace( '/^feedwordpress_/', '', $fallback_global );
686 + $fallback_global = preg_replace('/^feedwordpress_/', '', $fallback_global);
743 687
744 688 // 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;
689 + if ( is_array($fallback_global) && sizeof($fallback_global) )
690 + $fallback_global = reset($fallback_global);
748 691
749 - if ( ! empty( $fallback_global ) ) :
750 - $ret = get_option( 'feedwordpress_' . $fallback_global, /*default=*/ NULL );
751 - endif;
692 + if ( !empty($fallback_global) )
693 + $ret = get_option('feedwordpress_'.$fallback_global, /*default=*/ NULL);
752 694 endif;
753 695
754 696 $no_value = (
755 - is_null( $ret )
756 - or ( is_string( $ret ) and strtolower( $ret ) == $default )
697 + is_null($ret)
698 + or (is_string($ret) and strtolower($ret)==$default)
757 699 );
758 700
759 - if ( $no_value and ! is_null( $fallback_value ) ) :
701 + if ($no_value and !is_null($fallback_value)) :
760 702 $ret = $fallback_value;
761 703 endif;
762 704 return $ret;
763 - } /* SyndicatedLink::setting() */
705 + } /* SyndicatedLink::setting () */
764 706
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 );
707 + function merge_settings ($data, $prefix, $separator = '/') {
708 + $dd = $this->flatten_array($data, $prefix, $separator);
709 + $this->settings = array_merge($this->settings, $dd);
777 710 } /* SyndicatedLink::merge_settings () */
778 711
779 - public function update_setting( $name, $value, $default = 'default' ) {
780 - if ( !is_null( $value ) and $value != $default ) :
781 - $this->settings[ $name ] = $value;
712 + function update_setting ($name, $value, $default = 'default') {
713 + if (!is_null($value) and $value != $default) :
714 + $this->settings[$name] = $value;
782 715 else : // Zap it.
783 - unset( $this->settings[ $name ] );
716 + unset($this->settings[$name]);
784 717 endif;
785 718 } /* SyndicatedLink::update_setting () */
786 719
787 - public function is_non_incremental () {
720 + function is_non_incremental () {
788 721 return ('complete'==$this->setting('update_incremental', 'update_incremental', 'incremental'));
789 722 } /* SyndicatedLink::is_non_incremental () */
790 723
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()) {
724 + function uri ($params = array()) {
828 725 $params = wp_parse_args($params, array(
829 726 'add_params' => false,
830 727 'fetch' => false,
831 728 ));
@@ -831,15 +728,15 @@
831 728 ));
832 729
833 730 // Initialize $qp (= array for added query parameters, if any)
834 731 $qp = array();
835 -
836 - $link_rss = (is_object($this->link) ? $this->link->link_rss : NULL);
837 -
732 +
733 + $link_rss = (is_object($this->link) ? $this->link->link_rss : NULL);
734 +
838 735 // $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
736 + // $uri stores the effective URI of the request including any/all added query parameters
840 737 $uri = $link_rss;
841 - if ( !is_null($uri) and strlen($uri) > 0 and $params['add_params']) :
738 + if (!is_null($uri) and strlen($uri) > 0 and $params['add_params']) :
842 739 $qp = maybe_unserialize($this->setting('query parameters', array()));
843 740
844 741 // For high-tech HTTP feed request kung fu
845 742 $qp = apply_filters('syndicated_feed_parameters', $qp, $uri, $this);
@@ -861,34 +758,30 @@
861 758
862 759 // Do we have any filters that apply here?
863 760 $uri = apply_filters('syndicated_link_uri', $uri, $link_rss, $qp, $params, $this);
864 761
865 - // Return the filtered link URI.
762 + // Return the filtered link URI.
866 763 return $uri;
867 764 } /* SyndicatedLink::uri () */
868 765
869 - public function username () {
766 + function username () {
870 767 return $this->setting('http username', 'http_username', NULL);
871 768 } /* SyndicatedLink::username () */
872 769
873 - public function password () {
770 + function password () {
874 771 return $this->setting('http password', 'http_password', NULL);
875 772 } /* SyndicatedLink::password () */
876 773
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 -
774 + function authentication_method () {
775 + $auth = $this->setting('http auth method', NULL);
776 + if (('-' == $auth) or (strlen($auth)==0)) :
777 + $auth = NULL;
778 + endif;
886 779 return $auth;
887 780 } /* SyndicatedLink::authentication_method () */
888 -
781 +
889 782 var $postmeta = array();
890 - public function postmeta ($params = array()) {
783 + function postmeta ($params = array()) {
891 784 $params = wp_parse_args($params, /*defaults=*/ array(
892 785 "field" => NULL,
893 786 "parsed" => false,
894 787 "force" => false,
@@ -899,9 +792,9 @@
899 792 $default_custom_settings = get_option('feedwordpress_custom_settings');
900 793 if ($default_custom_settings and !is_array($default_custom_settings)) :
901 794 $default_custom_settings = unserialize($default_custom_settings);
902 795 endif;
903 - if ( !is_array($default_custom_settings)) :
796 + if (!is_array($default_custom_settings)) :
904 797 $default_custom_settings = array();
905 798 endif;
906 799
907 800 // Next, get the settings for this particular feed.
@@ -908,9 +801,9 @@
908 801 $custom_settings = $this->setting('postmeta', NULL, NULL);
909 802 if ($custom_settings and !is_array($custom_settings)) :
910 803 $custom_settings = unserialize($custom_settings);
911 804 endif;
912 - if ( !is_array($custom_settings)) :
805 + if (!is_array($custom_settings)) :
913 806 $custom_settings = array();
914 807 endif;
915 808
916 809 $this->postmeta[/*parsed=*/ false] = array_merge($default_custom_settings, $custom_settings);
@@ -917,15 +810,15 @@
917 810 $this->postmeta[/*parsed=*/ true] = array();
918 811
919 812 // Now, run through and parse them all.
920 813 foreach ($this->postmeta[/*parsed=*/ false] as $key => $meta) :
921 - $meta = apply_filters("syndicated_link_post_meta_{$key}_pre", $meta, $this);
814 + $meta = apply_filters("syndicated_link_post_meta_${key}_pre", $meta, $this);
922 815 $this->postmeta[/*parsed=*/ false][$key] = $meta;
923 816 $this->postmeta[/*parsed=*/ true][$key] = new FeedWordPressParsedPostMeta($meta);
924 817 endforeach;
925 818 endif;
926 819
927 - $ret = $this->postmeta[!! $params['parsed']];
820 + $ret = $this->postmeta[!!$params['parsed']];
928 821 if (is_string($params['field'])) :
929 822 $ret = $ret[$params['field']];
930 823 endif;
931 824 return $ret;
@@ -930,18 +823,17 @@
930 823 endif;
931 824 return $ret;
932 825 } /* SyndicatedLink::postmeta () */
933 826
934 - public function property_cascade ($fromFeed, $link_field, $setting, $method) {
827 + function property_cascade ($fromFeed, $link_field, $setting, $method) {
935 828 $value = NULL;
936 829 if ($fromFeed) :
937 830 $value = $this->setting($setting, NULL, NULL, NULL);
938 831
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!
832 + $s = $this->simplepie;
833 + $callable = (is_object($s) and method_exists($s, $method));
834 + if (is_null($value) and $callable) :
835 + $fallback = $s->{$method}();
944 836 endif;
945 837 else :
946 838 $value = $this->link->{$link_field};
947 839 endif;
@@ -947,17 +839,17 @@
947 839 endif;
948 840 return $value;
949 841 } /* SyndicatedLink::property_cascade () */
950 842
951 - public function homepage ($fromFeed = true) {
843 + function homepage ($fromFeed = true) {
952 844 return $this->property_cascade($fromFeed, 'link_url', 'feed/link', 'get_link');
953 845 } /* SyndicatedLink::homepage () */
954 846
955 - public function name ($fromFeed = true) {
847 + function name ($fromFeed = true) {
956 848 return $this->property_cascade($fromFeed, 'link_name', 'feed/title', 'get_title');
957 849 } /* SyndicatedLink::name () */
958 850
959 - public function guid () {
851 + function guid () {
960 852 $ret = $this->setting('feed/id', NULL, $this->uri());
961 853
962 854 // If we can get it live from the feed, do so.
963 855 if (is_object($this->simplepie)) :
@@ -982,13 +874,13 @@
982 874 endif;
983 875 return $ret;
984 876 }
985 877
986 - public function links ($params = array()) {
878 + function links ($params = array()) {
987 879 $params = wp_parse_args($params, array(
988 880 "rel" => NULL,
989 881 ));
990 -
882 +
991 883 $fLinks = array();
992 884 $search = array(
993 885 array('', 'link'),
994 886 array(SIMPLEPIE_NAMESPACE_ATOM_10, 'link'),
@@ -993,9 +885,9 @@
993 885 array('', 'link'),
994 886 array(SIMPLEPIE_NAMESPACE_ATOM_10, 'link'),
995 887 array(SIMPLEPIE_NAMESPACE_ATOM_03, 'link'),
996 888 );
997 -
889 +
998 890 foreach ($search as $pair) :
999 891 if ($link_tags = $this->simplepie->get_feed_tags($pair[0], $pair[1])) :
1000 892 $fLinks = array_merge($fLinks, $link_tags);
1001 893 endif;
@@ -1002,13 +894,13 @@
1002 894 if ($link_tags = $this->simplepie->get_channel_tags($pair[0], $pair[1])) :
1003 895 $fLinks = array_merge($fLinks, $link_tags);
1004 896 endif;
1005 897 endforeach;
1006 -
898 +
1007 899 $ret = array();
1008 900 foreach ($fLinks as $link) :
1009 901 $filter = false;
1010 - if ( !is_null($params['rel'])) :
902 + if (!is_null($params['rel'])) :
1011 903 $filter = true;
1012 904
1013 905 if (isset($link['attribs'])) :
1014 906 // Get a list of NSes from the search
@@ -1013,15 +905,15 @@
1013 905 if (isset($link['attribs'])) :
1014 906 // Get a list of NSes from the search
1015 907 foreach ($search as $pair) :
1016 908 $ns = $pair[0];
1017 -
909 +
1018 910 if (isset($link['attribs'][$ns])
1019 911 and isset($link['attribs'][$ns]['rel'])
1020 912 ) :
1021 913 $rel = strtolower(trim($link['attribs'][$ns]['rel']));
1022 914 $fRel = strtolower(trim($params['rel']));
1023 -
915 +
1024 916 if ($rel == $fRel) :
1025 917 $filter = false;
1026 918 endif;
1027 919 endif;
@@ -1027,10 +919,10 @@
1027 919 endif;
1028 920 endforeach;
1029 921 endif;
1030 922 endif;
1031 -
1032 - if ( ! $filter) :
923 +
924 + if (!$filter) :
1033 925 $ret[] = $link;
1034 926 endif;
1035 927 endforeach;
1036 928
@@ -1035,10 +927,10 @@
1035 927 endforeach;
1036 928
1037 929 return $ret;
1038 930 }
1039 -
1040 - public function ttl ($return_element = false) {
931 +
932 + function ttl ($return_element = false) {
1041 933 if (is_object($this->magpie)) :
1042 934 $channel = $this->magpie->channel;
1043 935 else :
1044 936 $channel = array();
@@ -1063,9 +955,9 @@
1063 955 // "sy:updatePeriod: Describes the period over which the
1064 956 // channel format is updated. Acceptable values are:
1065 957 // hourly, daily, weekly, monthly, yearly. If omitted,
1066 958 // daily is assumed." <http://web.resource.org/rss/1.0/modules/syndication/>
1067 - if (isset($channel['sy']['updateperiod'])) : $period = trim($channel['sy']['updateperiod']);
959 + if (isset($channel['sy']['updateperiod'])) : $period = $channel['sy']['updateperiod'];
1068 960 else : $period = 'daily';
1069 961 endif;
1070 962
1071 963 // "sy:updateFrequency: Used to describe the frequency
@@ -1076,18 +968,10 @@
1076 968 if (isset($channel['sy']['updatefrequency'])) : $freq = (int) $channel['sy']['updatefrequency'];
1077 969 else : $freq = 1;
1078 970 endif;
1079 971
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 -
1087 972 $xml = 'sy:updateFrequency';
1088 - $ret = (int) ($mins / $freq);
1089 -
973 + $ret = (int) ($period_minutes[$period] / $freq);
1090 974 else :
1091 975 $xml = NULL;
1092 976 $ret = NULL;
1093 977 endif;
@@ -1098,88 +982,69 @@
1098 982 if ($min > $ret) :
1099 983 $ret = NULL;
1100 984 endif;
1101 985 endif;
1102 - return ( $return_element ? array( $ret, $xml ) : $ret );
986 + return ($return_element ? array($ret, $xml) : $ret);
1103 987 } /* SyndicatedLink::ttl() */
1104 988
1105 - /**
1106 - * Sets the automatic time interval for
1107 - *
1108 - * @return
1109 - */
1110 - public function automatic_ttl() {
989 + function automatic_ttl () {
1111 990 // 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 ) ) :
991 + $updateWindow = $this->setting('update/window', 'update_window', DEFAULT_UPDATE_PERIOD);
992 + if (!is_numeric($updateWindow) or ($updateWindow < 1)) :
1114 993 $updateWindow = DEFAULT_UPDATE_PERIOD;
1115 994 endif;
1116 995
1117 996 // We get a fudge of 1/3 of window from elsewhere. We'll do some more
1118 997 // 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() */
998 + $fudgedInterval = $updateWindow+rand(-($updateWindow/6), 5*($updateWindow/12));
999 + return apply_filters('syndicated_feed_automatic_ttl', $fudgedInterval, $this);
1000 + } /* SyndicatedLink::automatic_ttl () */
1122 1001
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;
1002 + // SyndicatedLink::flatten_array (): flatten an array. Useful for
1003 + // hierarchical and namespaced elements.
1004 + //
1005 + // Given an array which may contain array or object elements in it,
1006 + // return a "flattened" array: a one-dimensional array of scalars
1007 + // containing each of the scalar elements contained within the array
1008 + // structure. Thus, for example, if $a['b']['c']['d'] == 'e', then the
1009 + // returned array for FeedWordPress::flatten_array($a) will contain a key
1010 + // $a['feed/b/c/d'] with value 'e'.
1011 + function flatten_array ($arr, $prefix = 'feed/', $separator = '/') {
1012 + $ret = array ();
1013 + if (is_array($arr)) :
1014 + foreach ($arr as $key => $value) :
1015 + if (is_scalar($value)) :
1016 + $ret[$prefix.$key] = $value;
1145 1017 else :
1146 - $ret = array_merge( $ret, $this->flatten_array( $value, $prefix.$key.$separator, $separator ) );
1018 + $ret = array_merge($ret, $this->flatten_array($value, $prefix.$key.$separator, $separator));
1147 1019 endif;
1148 1020 endforeach;
1149 1021 endif;
1150 1022 return $ret;
1151 - } /* SyndicatedLink::flatten_array() */
1023 + } /* SyndicatedLink::flatten_array () */
1152 1024
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)
1025 + function hardcode ($what) {
1026 +
1027 + $ret = $this->setting('hardcode '.$what, 'hardcode_'.$what, NULL);
1028 +
1029 + if ('yes' == $ret) :
1030 + $ret = true;
1031 + else :
1032 + $ret = false;
1033 + endif;
1034 + return $ret;
1163 1035 } /* SyndicatedLink::hardcode () */
1164 1036
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 );
1037 + function syndicated_status ($what, $default, $fallback = true) {
1038 + global $wpdb;
1177 1039
1178 - return esc_sql( trim( strtolower( $ret ) ) );
1040 + $g_set = ($fallback ? 'syndicated_' . $what . '_status' : NULL);
1041 + $ret = $this->setting($what.' status', $g_set, $default);
1042 +
1043 + return esc_sql(trim(strtolower($ret)));
1179 1044 } /* SyndicatedLink:syndicated_status () */
1180 1045
1181 - public function taxonomies () {
1046 + function taxonomies () {
1182 1047 $post_type = $this->setting('syndicated post type', 'syndicated_post_type', 'post');
1183 1048 return get_object_taxonomies(array('object_type' => $post_type), 'names');
1184 1049 } /* SyndicatedLink::taxonomies () */
1185 1050
@@ -1191,9 +1056,9 @@
1191 1056 * @param string $unfamiliar_category
1192 1057 * @param array|null $taxonomies
1193 1058 * @return array
1194 1059 */
1195 - public function category_ids ($post, $cats, $unfamiliar_category = 'create', $taxonomies = NULL, $params = array()) {
1060 + function category_ids ($post, $cats, $unfamiliar_category = 'create', $taxonomies = NULL, $params = array()) {
1196 1061 $singleton = (isset($params['singleton']) ? $params['singleton'] : true);
1197 1062 $allowFilters = (isset($params['filters']) ? $params['filters'] : false);
1198 1063
1199 1064 $catTax = 'category';
@@ -1224,9 +1089,9 @@
1224 1089
1225 1090 if ($oTerm->is_familiar()) :
1226 1091
1227 1092 $tax = $oTerm->taxonomy();
1228 - if ( !isset($terms[$tax])) :
1093 + if (!isset($terms[$tax])) :
1229 1094 $terms[$tax] = array();
1230 1095 endif;
1231 1096 $terms[$tax][] = $oTerm->id();
1232 1097
@@ -1244,10 +1109,10 @@
1244 1109 $tax = $ref[2];
1245 1110 endif;
1246 1111
1247 1112 $inserted = $oTerm->insert($tax);
1248 - if ( !is_null($inserted)) :
1249 - if ( !isset($terms[$tax])) :
1113 + if (!is_null($inserted)) :
1114 + if (!isset($terms[$tax])) :
1250 1115 $terms[$tax] = array();
1251 1116 endif;
1252 1117 $terms[$tax][] = $inserted;
1253 1118 else :
@@ -1288,6 +1153,6 @@
1288 1153 'Category: MAPPED term names '.json_encode($cats).' to IDs: '.json_encode($terms)
1289 1154 );
1290 1155 return $terms;
1291 1156 } /* SyndicatedLink::category_ids () */
1292 -} /* class SyndicatedLink */
1157 +} // class SyndicatedLink
1293 1158