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