PluginProbe
FeedWordPress / 2020.0118
FeedWordPress v2020.0118
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 | syndicatedpost.class.php +243 -276 trunk2020.0118 View file →
@@ -1,8 +1,8 @@
1 1 <?php
2 -require_once dirname(__FILE__) . '/feedtime.class.php';
3 -require_once dirname(__FILE__) . '/syndicatedpostterm.class.php';
4 -require_once dirname(__FILE__) . '/syndicatedpostxpathquery.class.php';
2 +require_once(dirname(__FILE__).'/feedtime.class.php');
3 +require_once(dirname(__FILE__).'/syndicatedpostterm.class.php');
4 +require_once(dirname(__FILE__).'/syndicatedpostxpathquery.class.php');
5 5
6 6 /**
7 7 * class SyndicatedPost: FeedWordPress uses to manage the conversion of
8 8 * incoming items from the feed parser into posts for the WordPress
@@ -14,24 +14,22 @@
14 14 *
15 15 * @version 2017.1018
16 16 */
17 17 class SyndicatedPost {
18 - /** @var MagpieRSS|null MagpieRSS representation. */
19 - var $item = null;
20 - /** @var SimplePie_Item|null SimplePie_Item representation. */
21 - var $entry = null;
18 + var $item = null; // MagpieRSS representation
19 + var $entry = null; // SimplePie_Item representation
22 20
23 21 var $link = null;
24 22 var $feed = null;
25 23 var $feedmeta = null;
26 24
27 - var $xmlns = array();
25 + var $xmlns = array ();
28 26
29 - var $post = array();
27 + var $post = array ();
30 28
31 - var $named = array();
32 - var $preset_terms = array();
33 - var $feed_terms = array();
29 + var $named = array ();
30 + var $preset_terms = array ();
31 + var $feed_terms = array ();
34 32
35 33 var $_freshness = null;
36 34 var $_wp_id = null;
37 35 var $_wp_post = null;
@@ -44,27 +42,29 @@
44 42 *
45 43 * @param array $item The item syndicated from the feed.
46 44 * @param SyndicatedLink $source The feed it was syndicated from.
47 45 */
48 - public function __construct( $item, $source ) {
49 - if ( empty( $item ) and empty( $source ) )
46 + public function __construct ($item, $source) {
47 + global $wpdb;
48 +
49 + if ( empty($item) and empty($source) )
50 50 return;
51 51
52 - if ( is_array( $item )
53 - and isset( $item['simplepie'] )
54 - and isset( $item['magpie'] ) ) :
52 + if (is_array($item)
53 + and isset($item['simplepie'])
54 + and isset($item['magpie'])) :
55 55 $this->entry = $item['simplepie'];
56 56 $this->item = $item['magpie'];
57 57 $item = $item['magpie'];
58 - elseif ( is_a( $item, 'SimplePie_Item' ) ) :
58 + elseif (is_a($item, 'SimplePie_Item')) :
59 59 $this->entry = $item;
60 60
61 61 // convert to Magpie for compat purposes
62 - $mpie = new MagpieFromSimplePie( $source->simplepie, $this->entry );
63 - $this->item = $mpie->get_item();
62 + $mp = new MagpieFromSimplePie($source->simplepie, $this->entry);
63 + $this->item = $mp->get_item();
64 64
65 65 // done with conversion object
66 - $mpie = NULL; unset( $mpie );
66 + $mp = NULL; unset($mp);
67 67 else :
68 68 $this->item = $item;
69 69 endif;
70 70
@@ -71,18 +71,18 @@
71 71 $this->link = $source;
72 72 $this->feed = $source->magpie;
73 73 $this->feedmeta = $source->settings;
74 74
75 - FeedWordPress::diagnostic( 'feed_items', 'Considering item [' . $this->guid() . '] "' . $this->entry->get_title().'"');
75 + FeedWordPress::diagnostic('feed_items', 'Considering item ['.$this->guid().'] "'.$this->entry->get_title().'"');
76 76
77 77 # Dealing with namespaces can get so fucking fucked.
78 78 $this->xmlns['forward'] = $source->magpie->_XMLNS_FAMILIAR;
79 79 $this->xmlns['reverse'] = array();
80 - foreach ( $this->xmlns['forward'] as $url => $ns ) :
81 - if ( ! isset( $this->xmlns['reverse'][ $ns ] ) ) :
82 - $this->xmlns['reverse'][ $ns ] = array();
80 + foreach ($this->xmlns['forward'] as $url => $ns) :
81 + if (!isset($this->xmlns['reverse'][$ns])) :
82 + $this->xmlns['reverse'][$ns] = array();
83 83 endif;
84 - $this->xmlns['reverse'][ $ns ][] = $url;
84 + $this->xmlns['reverse'][$ns][] = $url;
85 85 endforeach;
86 86
87 87 // Fucking SimplePie.
88 88 $this->xmlns['reverse']['rss'][] = '';
@@ -87,14 +87,14 @@
87 87 // Fucking SimplePie.
88 88 $this->xmlns['reverse']['rss'][] = '';
89 89
90 90 // Trigger global syndicated_item filter.
91 - $changed = apply_filters( 'syndicated_item', $this->item, $this );
91 + $changed = apply_filters('syndicated_item', $this->item, $this);
92 92 $this->item = $changed;
93 93
94 94 // Allow for feed-specific syndicated_item filters.
95 95 $changed = apply_filters(
96 - "syndicated_item_" . $source->uri(),
96 + "syndicated_item_".$source->uri(),
97 97 $this->item,
98 98 $this
99 99 );
100 100 $this->item = $changed;
@@ -99,9 +99,9 @@
99 99 );
100 100 $this->item = $changed;
101 101
102 102 # Filters can halt further processing by returning NULL
103 - if ( is_null( $this->item ) ) :
103 + if (is_null($this->item)) :
104 104 $this->post = NULL;
105 105 else :
106 106
107 107 # Note that nothing is run through esc_sql() here.
@@ -110,16 +110,15 @@
110 110 # to avoid screwing with syndicated_post filters
111 111
112 112 $this->post['post_title'] = apply_filters(
113 113 'syndicated_item_title',
114 - $this->entry->get_title(),
115 - $this
114 + $this->entry->get_title(), $this
116 115 );
117 116
117 +
118 118 $this->named['author'] = apply_filters(
119 119 'syndicated_item_author',
120 - $this->author(),
121 - $this
120 + $this->author(), $this
122 121 );
123 122 // This just gives us an alphanumeric name for the author.
124 123 // We look up (or create) the numeric ID for the author
125 124 // in SyndicatedPost::add().
@@ -125,63 +124,59 @@
125 124 // in SyndicatedPost::add().
126 125
127 126 $this->post['post_content'] = apply_filters(
128 127 'syndicated_item_content',
129 - $this->content(),
130 - $this
128 + $this->content(), $this
131 129 );
130 +
131 + $excerpt = apply_filters('syndicated_item_excerpt', $this->excerpt(), $this);
132 132
133 - $excerpt = apply_filters( 'syndicated_item_excerpt', $this->excerpt(), $this );
134 -
135 - if ( !empty( $excerpt ) ):
133 + if (!empty($excerpt)):
136 134 $this->post['post_excerpt'] = $excerpt;
137 135 endif;
138 136
139 137 // Dealing with timestamps in WordPress is so fucking fucked.
140 - $offset = (int) get_option( 'gmt_offset' ) * 60 * 60;
141 - $post_date_gmt = $this->published( array( 'default' => -1 ) );
142 - $post_modified_gmt = $this->updated( array( 'default' => -1 ) );
138 + $offset = (int) get_option('gmt_offset') * 60 * 60;
139 + $post_date_gmt = $this->published(array('default' => -1));
140 + $post_modified_gmt = $this->updated(array('default' => -1));
143 141
144 - $this->post['post_date_gmt'] = gmdate( 'Y-m-d H:i:s', $post_date_gmt );
145 - $this->post['post_date'] = gmdate( 'Y-m-d H:i:s', $post_date_gmt + $offset );
146 - $this->post['post_modified_gmt'] = gmdate( 'Y-m-d H:i:s', $post_modified_gmt );
147 - $this->post['post_modified'] = gmdate( 'Y-m-d H:i:s', $post_modified_gmt + $offset );
142 + $this->post['post_date_gmt'] = gmdate('Y-m-d H:i:s', $post_date_gmt);
143 + $this->post['post_date'] = gmdate('Y-m-d H:i:s', $post_date_gmt + $offset);
144 + $this->post['post_modified_gmt'] = gmdate('Y-m-d H:i:s', $post_modified_gmt);
145 + $this->post['post_modified'] = gmdate('Y-m-d H:i:s', $post_modified_gmt + $offset);
148 146
149 147 // Use feed-level preferences or the global default.
150 - $this->post['post_status'] = $this->link->syndicated_status( 'post', 'publish' );
151 - $this->post['comment_status'] = $this->link->syndicated_status( 'comment', 'closed' );
152 - $this->post['ping_status'] = $this->link->syndicated_status( 'ping', 'closed' );
148 + $this->post['post_status'] = $this->link->syndicated_status('post', 'publish');
149 + $this->post['comment_status'] = $this->link->syndicated_status('comment', 'closed');
150 + $this->post['ping_status'] = $this->link->syndicated_status('ping', 'closed');
153 151
154 152 // Unique ID (hopefully a unique tag: URI); failing that, the permalink
155 - $this->post['guid'] = apply_filters( 'syndicated_item_guid', $this->guid(), $this );
153 + $this->post['guid'] = apply_filters('syndicated_item_guid', $this->guid(), $this);
156 154
157 155 // User-supplied custom settings to apply to each post.
158 156 // Do first so that FWP-generated custom settings will
159 157 // overwrite if necessary; thus preventing any munging.
160 - $postMetaIn = $this->link->postmeta( array( "parsed" => true ) );
158 + $postMetaIn = $this->link->postmeta(array("parsed" => true));
161 159 $postMetaOut = array();
162 160
163 - foreach ( $postMetaIn as $key => $meta ) :
164 - $postMetaOut[ $key ] = $meta->do_substitutions( $this );
161 + foreach ($postMetaIn as $key => $meta) :
162 + $postMetaOut[$key] = $meta->do_substitutions($this);
165 163 endforeach;
166 164
167 - foreach ( $postMetaOut as $key => $values ) :
168 - if ( is_null( $values ) ) { // have chosen to replace value with empty string
169 - $values = [ '' ];
170 - }
171 - $this->post['meta'][ $key ] = array();
172 - foreach ( $values as $value ) :
173 - $this->post['meta'][ $key ][] = apply_filters( "syndicated_post_meta_{$key}", $value, $this );
165 + foreach ($postMetaOut as $key => $values) :
166 + $this->post['meta'][$key] = array();
167 + foreach ($values as $value) :
168 + $this->post['meta'][$key][] = apply_filters("syndicated_post_meta_{$key}", $value, $this);
174 169 endforeach;
175 170 endforeach;
176 171
177 172 // RSS 2.0 / Atom 1.0 enclosure support
178 173 $enclosures = $this->entry->get_enclosures();
179 - if ( is_array( $enclosures ) ) : foreach ( $enclosures as $enclosure ) :
174 + if (is_array($enclosures)) : foreach ($enclosures as $enclosure) :
180 175 $this->post['meta']['enclosure'][] =
181 - apply_filters( 'syndicated_item_enclosure_url', $enclosure->get_link(), $this ) . "\n".
182 - apply_filters( 'syndicated_item_enclosure_length', $enclosure->get_length(), $this ) . "\n".
183 - apply_filters( 'syndicated_item_enclosure_type', $enclosure->get_type(), $this );
176 + apply_filters('syndicated_item_enclosure_url', $enclosure->get_link(), $this)."\n".
177 + apply_filters('syndicated_item_enclosure_length', $enclosure->get_length(), $this)."\n".
178 + apply_filters('syndicated_item_enclosure_type', $enclosure->get_type(), $this);
184 179 endforeach; endif;
185 180
186 181 // In case you want to point back to the blog this was
187 182 // syndicated from.
@@ -203,18 +198,18 @@
203 198 );
204 199
205 200 // Make use of atom:source data, if present in an aggregated feed
206 201 $entry_source = $this->source();
207 - if ( ! is_null( $entry_source ) ) :
208 - foreach ( $entry_source as $what => $value ) :
209 - if ( ! is_null( $value ) ) :
210 - if ( 'title' == $what ) : $key = 'syndication_source';
211 - elseif ( 'feed' == $what ) : $key = 'syndication_feed';
212 - else : $key = "syndication_source_{$what}";
202 + if (!is_null($entry_source)) :
203 + foreach ($entry_source as $what => $value) :
204 + if (!is_null($value)) :
205 + if ($what=='title') : $key = 'syndication_source';
206 + elseif ($what=='feed') : $key = 'syndication_feed';
207 + else : $key = "syndication_source_${what}";
213 208 endif;
214 209
215 - $sourcemeta["{$key}_original"] = apply_filters(
216 - 'syndicated_item_original_source_' . $what,
210 + $sourcemeta["${key}_original"] = apply_filters(
211 + 'syndicated_item_original_source_'.$what,
217 212 $value,
218 213 $this
219 214 );
220 215 endif;
@@ -220,11 +215,11 @@
220 215 endif;
221 216 endforeach;
222 217 endif;
223 218
224 - foreach ( $sourcemeta as $meta_key => $value ) :
225 - if ( !is_null( $value ) ) :
226 - $this->post['meta'][ $meta_key ] = $value;
219 + foreach ($sourcemeta as $meta_key => $value) :
220 + if (!is_null($value)) :
221 + $this->post['meta'][$meta_key] = $value;
227 222 endif;
228 223 endforeach;
229 224
230 225 // Store information on human-readable and machine-readable comment URIs
@@ -229,31 +224,31 @@
229 224
230 225 // Store information on human-readable and machine-readable comment URIs
231 226
232 227 // Human-readable comment URI
233 - $commentLink = apply_filters( 'syndicated_item_comments', $this->comment_link(), $this );
234 - if ( ! is_null( $commentLink) ) : $this->post['meta']['rss:comments'] = $commentLink; endif;
228 + $commentLink = apply_filters('syndicated_item_comments', $this->comment_link(), $this);
229 + if (!is_null($commentLink)) : $this->post['meta']['rss:comments'] = $commentLink; endif;
235 230
236 231 // Machine-readable content feed URI
237 - $commentFeed = apply_filters( 'syndicated_item_commentrss', $this->comment_feed(), $this );
238 - if ( ! is_null( $commentFeed ) ) : $this->post['meta']['wfw:commentRSS'] = $commentFeed; endif;
232 + $commentFeed = apply_filters('syndicated_item_commentrss', $this->comment_feed(), $this);
233 + if (!is_null($commentFeed)) : $this->post['meta']['wfw:commentRSS'] = $commentFeed; endif;
239 234 // Yeah, yeah, now I know that it's supposed to be
240 235 // wfw:commentRss. Oh well. Path dependence, sucka.
241 236
242 237 // Store information to identify the feed that this came from
243 - if ( isset( $this->feedmeta['link/uri'] ) ) :
238 + if (isset($this->feedmeta['link/uri'])) :
244 239 $this->post['meta']['syndication_feed'] = $this->feedmeta['link/uri'];
245 240 endif;
246 - if ( isset( $this->feedmeta['link/id'] ) ) :
241 + if (isset($this->feedmeta['link/id'])) :
247 242 $this->post['meta']['syndication_feed_id'] = $this->feedmeta['link/id'];
248 243 endif;
249 244
250 - if ( isset( $this->item['source_link_self'] ) ) :
245 + if (isset($this->item['source_link_self'])) :
251 246 $this->post['meta']['syndication_feed_original'] = $this->item['source_link_self'];
252 247 endif;
253 248
254 249 // In case you want to know the external permalink...
255 - $this->post['meta']['syndication_permalink'] = apply_filters( 'syndicated_item_link', $this->permalink() );
250 + $this->post['meta']['syndication_permalink'] = apply_filters('syndicated_item_link', $this->permalink());
256 251
257 252 // Store a hash of the post content for checking whether something needs to be updated
258 253 $this->post['meta']['syndication_item_hash'] = $this->update_hash();
259 254
@@ -258,16 +253,12 @@
258 253 $this->post['meta']['syndication_item_hash'] = $this->update_hash();
259 254
260 255 // Categories, Tags, and other Terms: from settings assignments (global settings, subscription settings),
261 256 // and from feed assignments (item metadata, post content)
262 - $this->preset_terms = apply_filters( 'syndicated_item_preset_terms', $this->get_terms_from_settings(), $this );
263 - $this->feed_terms = apply_filters( 'syndicated_item_feed_terms', $this->get_terms_from_feeds(), $this );
257 + $this->preset_terms = apply_filters('syndicated_item_preset_terms', $this->get_terms_from_settings(), $this);
258 + $this->feed_terms = apply_filters('syndicated_item_feed_terms', $this->get_terms_from_feeds(), $this);
264 259
265 - $this->post['post_type'] = apply_filters(
266 - 'syndicated_post_type',
267 - $this->link->setting( 'syndicated post type', 'syndicated_post_type', 'post' ),
268 - $this
269 - );
260 + $this->post['post_type'] = apply_filters('syndicated_post_type', $this->link->setting('syndicated post type', 'syndicated_post_type', 'post'), $this);
270 261 endif;
271 262 } /* SyndicatedPost::__construct() */
272 263
273 264 #####################################
@@ -343,9 +334,9 @@
343 334
344 335 $matches = array();
345 336 foreach ($rss as $ns) :
346 337 $data = $this->link->simplepie->get_feed_tags($ns, 'channel');
347 - if ( !is_null($data)) :
338 + if (!is_null($data)) :
348 339 $matches = array_merge($matches, $data);
349 340 endif;
350 341 endforeach;
351 342 return $matches;
@@ -353,21 +344,21 @@
353 344
354 345 public function get_categories ($params = array()) {
355 346 return $this->entry->get_categories();
356 347 }
357 -
348 +
358 349 public function title ($params = array()) {
359 350 return $this->entry->get_title();
360 351 } /* SyndicatedPost::title () */
352 +
353 + public function content ($params = array()) {
361 354
362 - public function content ($params = array())
363 - {
364 355 $params = wp_parse_args($params, array(
365 - "full only" => false,
356 + "full only" => false,
366 357 ));
367 -
358 +
368 359 $content = NULL;
369 -
360 +
370 361 // FIXME: This is one of the main places in the code still using
371 362 // the outmoded SimplePie - to - Magpie construction. We could
372 363 // replace using SimplePie_Item::get_tags() here. (Or if really
373 364 // ambitious we could attempt to just use
@@ -372,9 +363,9 @@
372 363 // replace using SimplePie_Item::get_tags() here. (Or if really
373 364 // ambitious we could attempt to just use
374 365 // SimplePie_Item::get_content() with content-only set to TRUE
375 366 // and some sanitization in effect. -CJ 1jul14
376 -
367 +
377 368 // atom:content, standard method of providing full HTML content
378 369 // in Atom feeds.
379 370 if (isset($this->item['atom_content'])) :
380 371 $content = $this->item['atom_content'];
@@ -379,9 +370,9 @@
379 370 if (isset($this->item['atom_content'])) :
380 371 $content = $this->item['atom_content'];
381 372 elseif (isset($this->item['atom']['atom_content'])) :
382 373 $content = $this->item['atom']['atom_content'];
383 -
374 +
384 375 // Some exotics: back in the day, before widespread convergence
385 376 // on content:encoding, some RSS feeds took advantage of XML
386 377 // namespaces to use an inline xhtml:body or xhtml:div element
387 378 // for full-content HTML. (E.g. Sam Ruby's feed, IIRC.)
@@ -388,18 +379,18 @@
388 379 elseif (isset($this->item['xhtml']['body'])) :
389 380 $content = $this->item['xhtml']['body'];
390 381 elseif (isset($this->item['xhtml']['div'])) :
391 382 $content = $this->item['xhtml']['div'];
392 -
383 +
393 384 // content:encoded, most common method of providing full HTML in
394 385 // RSS 2.0 feeds.
395 386 elseif (isset($this->item['content']['encoded']) and $this->item['content']['encoded']):
396 387 $content = $this->item['content']['encoded'];
397 -
388 +
398 389 // Fall back on elements that sometimes may contain full HTML
399 390 // but sometimes not.
400 - elseif ( ! $params['full only']) :
401 -
391 + elseif (!$params['full only']) :
392 +
402 393 // description element is sometimes used for full HTML
403 394 // sometimes for summary text in RSS. (By the letter of
404 395 // the standard, it should just be for summary text.)
405 396 if (isset($this->item['description'])) :
@@ -404,11 +395,11 @@
404 395 // the standard, it should just be for summary text.)
405 396 if (isset($this->item['description'])) :
406 397 $content = $this->item['description'];
407 398 endif;
408 -
399 +
409 400 endif;
410 -
401 +
411 402 return $content;
412 403 } /* SyndicatedPost::content() */
413 404
414 405 public function excerpt () {
@@ -455,9 +446,9 @@
455 446 return $permalink;
456 447 } /* SyndicatedPost::permalink () */
457 448
458 449 public function created ($params = array()) {
459 - $unfiltered = false; // $default = NULL; // seems to be unused on this function (gwyneth 20230916)
450 + $unfiltered = false; $default = NULL;
460 451 extract($params);
461 452
462 453 $date = '';
463 454 if (isset($this->item['dc']['created'])) :
@@ -468,18 +459,18 @@
468 459 $date = $this->item['created'];
469 460 endif;
470 461
471 462 $time = new FeedTime($date);
472 - $tstamp = $time->timestamp();
473 - if ( ! $unfiltered) :
474 - apply_filters('syndicated_item_created', $tstamp, $this);
463 + $ts = $time->timestamp();
464 + if (!$unfiltered) :
465 + apply_filters('syndicated_item_created', $ts, $this);
475 466 endif;
476 - return $tstamp;
467 + return $ts;
477 468 } /* SyndicatedPost::created() */
478 469
479 470 public function published ($params = array(), $default = NULL) {
480 471 $fallback = true; $unfiltered = false;
481 - if ( !is_array($params)) : // Old style
472 + if (!is_array($params)) : // Old style
482 473 $fallback = $params;
483 474 else : // New style
484 475 extract($params);
485 476 endif;
@@ -484,9 +475,9 @@
484 475 extract($params);
485 476 endif;
486 477
487 478 $date = '';
488 - $tstamp = null;
479 + $ts = null;
489 480
490 481 # RSS is a fucking mess. Figure out whether we have a date in
491 482 # <dc:date>, <issued>, <pubDate>, etc., and get it into Unix
492 483 # epoch format for reformatting. If we can't find anything,
@@ -504,31 +495,31 @@
504 495 endif;
505 496
506 497 if (strlen($date) > 0) :
507 498 $time = new FeedTime($date);
508 - $tstamp = $time->timestamp();
499 + $ts = $time->timestamp();
509 500 elseif ($fallback) : // Fall back to <updated> / <modified> if present
510 - $tstamp = $this->updated(/*fallback=*/ false, /*default=*/ $default);
501 + $ts = $this->updated(/*fallback=*/ false, /*default=*/ $default);
511 502 endif;
512 503
513 504 # If everything failed, then default to the current time.
514 - if (is_null($tstamp)) :
505 + if (is_null($ts)) :
515 506 if (-1 == $default) :
516 - $tstamp = time();
507 + $ts = time();
517 508 else :
518 - $tstamp = $default;
509 + $ts = $default;
519 510 endif;
520 511 endif;
521 512
522 - if ( ! $unfiltered) :
523 - $tstamp = apply_filters('syndicated_item_published', $tstamp, $this);
513 + if (!$unfiltered) :
514 + $ts = apply_filters('syndicated_item_published', $ts, $this);
524 515 endif;
525 - return $tstamp;
516 + return $ts;
526 517 } /* SyndicatedPost::published() */
527 518
528 519 public function updated ($params = array(), $default = -1) {
529 520 $fallback = true; $unfiltered = false;
530 - if ( !is_array($params)) : // Old style
521 + if (!is_array($params)) : // Old style
531 522 $fallback = $params;
532 523 else : // New style
533 524 extract($params);
534 525 endif;
@@ -533,9 +524,9 @@
533 524 extract($params);
534 525 endif;
535 526
536 527 $date = '';
537 - $tstamp = null;
528 + $ts = null;
538 529
539 530 # As far as I know, only dcterms and Atom have reliable ways to
540 531 # specify when something was *modified* last. If neither is
541 532 # available, then we'll try to get the time of publication.
@@ -550,26 +541,26 @@
550 541 endif;
551 542
552 543 if (strlen($date) > 0) :
553 544 $time = new FeedTime($date);
554 - $tstamp = $time->timestamp();
545 + $ts = $time->timestamp();
555 546 elseif ($fallback) : // Fall back to issued / dc:date
556 - $tstamp = $this->published(/*fallback=*/ false, /*default=*/ $default);
547 + $ts = $this->published(/*fallback=*/ false, /*default=*/ $default);
557 548 endif;
558 549
559 550 # If everything failed, then default to the current time.
560 - if (is_null($tstamp)) :
551 + if (is_null($ts)) :
561 552 if (-1 == $default) :
562 - $tstamp = time();
553 + $ts = time();
563 554 else :
564 - $tstamp = $default;
555 + $ts = $default;
565 556 endif;
566 557 endif;
567 558
568 - if ( ! $unfiltered) :
569 - $tstamp = apply_filters('syndicated_item_updated', $tstamp, $this);
559 + if (!$unfiltered) :
560 + apply_filters('syndicated_item_updated', $ts, $this);
570 561 endif;
571 - return $tstamp;
562 + return $ts;
572 563 } /* SyndicatedPost::updated() */
573 564
574 565 var $_hashes = array();
575 566 function stored_hashes ($id = NULL) {
@@ -576,9 +567,9 @@
576 567 if (is_null($id)) :
577 568 $id = $this->wp_id();
578 569 endif;
579 570
580 - if ( !isset($this->_hashes[$id])) :
571 + if (!isset($this->_hashes[$id])) :
581 572 $this->_hashes[$id] = get_post_custom_values(
582 573 'syndication_item_hash', $id
583 574 );
584 575 if (is_null($this->_hashes[$id])) :
@@ -618,11 +609,10 @@
618 609 * @uses home_url()
619 610 * @uses apply_filters()
620 611 */
621 612 static function normalize_guid_prefix () {
622 - $scheme = 'http';
623 - $url = trailingslashit(home_url(/*path=*/ '', $scheme));
624 - return apply_filters('syndicated_item_guid_normalized_prefix', $url . '?guid=', $scheme);
613 + $url = trailingslashit(home_url(/*path=*/ '', /*scheme=*/ 'http'));
614 + return apply_filters('syndicated_item_guid_normalized_prefix', $url . '?guid=');
625 615 } /* SyndicatedPost::normalize_guid_prefix() */
626 616
627 617 static function normalize_guid ($guid) {
628 618 $guid = trim($guid);
@@ -631,16 +621,15 @@
631 621 elseif ((strlen(esc_url($guid)) == 0) or (esc_url($guid) != $guid)) :
632 622 $guid = SyndicatedPost::normalize_guid_prefix().md5($guid);
633 623 endif;
634 624 $guid = trim($guid);
635 -
625 +
636 626 return $guid;
637 627 } /* SyndicatedPost::normalize_guid() */
638 628
639 629 static function alternative_guid_prefix () {
640 - $scheme = 'https';
641 - $url = trailingslashit(home_url(/*path=*/ '', $scheme));
642 - return apply_filters('syndicated_item_guid_normalized_prefix', $url . '?guid=', $scheme);
630 + $url = trailingslashit(home_url(/*path=*/ '', /*scheme=*/ 'https'));
631 + return apply_filters('syndicated_item_guid_normalized_prefix', $url . '?guid=');
643 632 }
644 633 static function alternative_guid ($guid) {
645 634 $guid = trim($guid);
646 635 if (preg_match('/^[0-9a-z]{32}$/i', $guid)) : // MD5
@@ -648,9 +637,9 @@
648 637 elseif ((strlen(esc_url($guid)) == 0) or (esc_url($guid) != $guid)) :
649 638 $guid = SyndicatedPost::alternative_guid_prefix().md5($guid);
650 639 endif;
651 640 $guid = trim($guid);
652 -
641 +
653 642 return $guid;
654 643 } /* SyndicatedPost::normalize_guid() */
655 644
656 645 public function guid () {
@@ -684,9 +673,9 @@
684 673 // gets clipped by MySQL, uniqueness tests will fail
685 674 // forever after and the post will be endlessly
686 675 // reduplicated. So, instead, Guids Of A Certain Length
687 676 // are hashed down into a nice, manageable tag: URI.
688 - if ( !is_null($original_guid)) :
677 + if (!is_null($original_guid)) :
689 678 $guid .= ',2010-12-03:id.'.md5($original_guid);
690 679
691 680 // If we have a date of creation, then we can use that
692 681 // to uniquely identify the item. (On the other hand, if
@@ -692,9 +681,9 @@
692 681 // to uniquely identify the item. (On the other hand, if
693 682 // the feed producer was consicentious enough to
694 683 // generate dates of creation, she probably also was
695 684 // conscientious enough to generate unique identifiers.)
696 - elseif ( !is_null($this->created())) :
685 + elseif (!is_null($this->created())) :
697 686 $guid .= '://post.'.date('YmdHis', $this->created());
698 687
699 688 // Otherwise, use both the URI of the item, *and* the
700 689 // item's title. We have to use both because titles are
@@ -705,9 +694,9 @@
705 694 // this is about the best we can do.
706 695 else :
707 696 $link = $this->permalink();
708 697 if (is_null($link)) : $link = $this->link->uri(); endif;
709 - $guid .= '://'.md5($link.'/'.$this->title());
698 + $guid .= '://'.md5($link.'/'.$this->item['title']);
710 699 endif;
711 700 endif;
712 701 return $guid;
713 702 } /* SyndicatedPost::guid() */
@@ -715,9 +704,9 @@
715 704 public function author () {
716 705 $author = array ();
717 706
718 707 $aa = $this->entry->get_authors();
719 - if (is_countable($aa) and count($aa) > 0) :
708 + if (count($aa) > 0) :
720 709 $a = reset($aa);
721 710
722 711 $author = array(
723 712 'name' => $a->get_name(),
@@ -745,9 +734,9 @@
745 734 // e-mail address, but lots of people don't use
746 735 // it that way. So let's make of it what we can.
747 736 $author = parse_email_with_realname($this->item['author']);
748 737
749 - if ( !isset($author['name'])) :
738 + if (!isset($author['name'])) :
750 739 if (isset($author['email'])) :
751 740 $author['name'] = $author['email'];
752 741 else :
753 742 $author['name'] = $this->feed->channel['title'];
@@ -755,9 +744,9 @@
755 744 endif;
756 745 endif;
757 746 endif;
758 747
759 - if ( !isset($author['name']) or is_null($author['name'])) :
748 + if (!isset($author['name']) or is_null($author['name'])) :
760 749 // Nothing found. Try some crappy defaults.
761 750 if ($this->link->name()) :
762 751 $author['name'] = $this->link->name();
763 752 else :
@@ -797,9 +786,9 @@
797 786 * @since 2016.0331
798 787 * @return array of lists, each element has the taxonomy for a key ('category', 'post_tag', etc.),
799 788 * and a list of term codes (either alphanumeric names, or ID numbers encoded in a format that
800 789 * SyndicatedLink::category_ids() can understand) within that taxonomy
801 - *
790 + *
802 791 */
803 792 public function get_terms_from_settings () {
804 793 // Categories: start with default categories, if any.
805 794 $cats = array();
@@ -836,9 +825,9 @@
836 825
837 826 foreach ($taxonomies as $tax) :
838 827 // category and tag settings have already previously been handled
839 828 // but if this is from another taxonomy, then...
840 - if ( !isset($specials[$tax])) :
829 + if (!isset($specials[$tax])) :
841 830 $terms = array();
842 831
843 832 // See if we should get the globals
844 833 if ('no' != $this->link->setting("add/$tax", NULL, 'yes')) :
@@ -855,12 +844,12 @@
855 844 // That's all, folks.
856 845 $preset_terms[$tax] = $terms;
857 846 endif;
858 847 endforeach;
859 -
848 +
860 849 return $preset_terms;
861 850 } /* SyndicatedPost::get_terms_from_settings () */
862 -
851 +
863 852 /**
864 853 * SyndicatedPost::get_terms_from_feeds(): Return an array of terms to associate with the incoming
865 854 * post based on the contents of the subscribed feed (atom:category and rss:category elements, dc:subject
866 855 * elements, tags embedded using microformats in the post content, etc.)
@@ -868,15 +857,15 @@
868 857 * @since 2016.0331
869 858 * @return array of lists, each element has the taxonomy for a key ('category', 'post_tag', etc.),
870 859 * and a list of alphanumeric term names
871 860 */
872 - public function get_terms_from_feeds () {
861 + public function get_terms_from_feeds () {
873 862 // Now add categories from the post, if we have 'em
874 863 $cats = array();
875 864 $post_cats = $this->entry->get_categories();
876 865 if (is_array($post_cats)) : foreach ($post_cats as $cat) :
877 866 $cat_name = $cat->get_term();
878 - if ( ! $cat_name) : $cat_name = $cat->get_label(); endif;
867 + if (!$cat_name) : $cat_name = $cat->get_label(); endif;
879 868
880 869 if ($this->link->setting('cat_split', NULL, NULL)) :
881 870 $pcre = "\007".$this->feedmeta['cat_split']."\007";
882 871 $cats = array_merge(
@@ -913,9 +902,9 @@
913 902 $tags = array();
914 903 $content = $this->content();
915 904 $pattern = FeedWordPressHTML::tagWithAttributeRegex('a', 'rel', 'tag');
916 905 preg_match_all($pattern, $content, $refs, PREG_SET_ORDER);
917 - if (is_countable($refs) and count($refs) > 0) :
906 + if (count($refs) > 0) :
918 907 foreach ($refs as $ref) :
919 908 $tag = FeedWordPressHTML::tagWithAttributeMatch($ref);
920 909 $tags[] = $tag['content'];
921 910 endforeach;
@@ -985,21 +974,21 @@
985 974 * that will be returned to only those with a MIME type which
986 975 * matches this regular expression.
987 976 * @return array
988 977 */
989 - function enclosures( $type = '/.*/' ) {
978 + function enclosures ($type = '/.*/') {
990 979 $enclosures = array();
991 980
992 - if ( isset( $this->item['enclosure#'] ) ) :
981 + if (isset($this->item['enclosure#'])) :
993 982 // Loop through enclosure, enclosure#2, enclosure#3, ....
994 - for ( $i = 1; $i <= $this->item['enclosure#']; $i++ ) :
995 - $eid = ( ( $i > 1 ) ? "#{$i}" : "" ); // this was set as #{$id} — was that a typo? (gwyneth 20230919)
983 + for ($i = 1; $i <= $this->item['enclosure#']; $i++) :
984 + $eid = (($i > 1) ? "#{$id}" : "");
996 985
997 986 // Does it match the type we want?
998 - if ( preg_match($type, $this->item["enclosure{$eid}@type"] ) ) :
987 + if (preg_match($type, $this->item["enclosure{$eid}@type"])) :
999 988 $enclosures[] = array(
1000 - "url" => $this->item["enclosure{$eid}@url"],
1001 - "type" => $this->item["enclosure{$eid}@type"],
989 + "url" => $this->item["enclosure{$eid}@url"],
990 + "type" => $this->item["enclosure{$eid}@type"],
1002 991 "length" => $this->item["enclosure{$eid}@length"],
1003 992 );
1004 993 endif;
1005 994 endfor;
@@ -1028,9 +1017,9 @@
1028 1017 $ret['id'] = $id_tags[0]['data'];
1029 1018 endif;
1030 1019 endif;
1031 1020
1032 - if ( !is_null($what) and is_scalar($what)) :
1021 + if (!is_null($what) and is_scalar($what)) :
1033 1022 $ret = $ret[$what];
1034 1023 endif;
1035 1024 return $ret;
1036 1025 }
@@ -1235,8 +1224,9 @@
1235 1224 * 1 = post already syndicated, but needs to be updated to latest
1236 1225 * 2 = post has not yet been syndicated; needs to be created
1237 1226 */
1238 1227 function freshness ($format = 'number') {
1228 + global $wpdb;
1239 1229
1240 1230 if ($this->filtered()) : // This should never happen.
1241 1231 FeedWordPressDiagnostic::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1242 1232 endif;
@@ -1247,9 +1237,9 @@
1247 1237
1248 1238 $q = new WP_Query(array(
1249 1239 'fields' => '_synfresh', // id, guid, post_modified_gmt
1250 1240 'ignore_sticky_posts' => true,
1251 - 'guid' => $eguid, // it's always better to use the escaped version, right? (gwyneth 20230915)
1241 + 'guid' => $guid,
1252 1242 ));
1253 1243
1254 1244 $old_post = NULL;
1255 1245 if ($q->have_posts()) :
@@ -1268,23 +1258,23 @@
1268 1258 // Presume there is nothing new until we find
1269 1259 // something new.
1270 1260 $updated = false;
1271 1261 $live = false;
1272 -
1262 +
1273 1263 // Pull the list of existing revisions to get
1274 1264 // timestamps.
1275 1265 $revisions = wp_get_post_revisions($old_post->ID);
1276 1266 foreach ($revisions as $rev) :
1277 - $revisions_ts[] = mysql2date('G', $rev->post_modified_gmt);
1267 + $revisions_ts[] = mysql2date('G', $rev->post_modified_gmt);
1278 1268 endforeach;
1279 1269
1280 1270 $revisions_ts[] = mysql2date('G', $old_post->post_modified_gmt);
1281 1271 $last_rev_ts = end($revisions_ts);
1282 1272 $updated_ts = $this->updated(/*fallback=*/ true, /*default=*/ NULL);
1283 -
1273 +
1284 1274 // If we have an explicit updated timestamp,
1285 1275 // check that against existing stamps.
1286 - if ( !is_null($updated_ts)) :
1276 + if (!is_null($updated_ts)) :
1287 1277 $updated = !in_array($updated_ts, $revisions_ts);
1288 1278
1289 1279 // If this a newer revision, make it go
1290 1280 // live. If an older one, just record
@@ -1292,9 +1282,9 @@
1292 1282 $live = ($updated and ($updated_ts > $last_rev_ts));
1293 1283 endif;
1294 1284
1295 1285 // This is a revision we haven't seen before, judging by the date.
1296 -
1286 +
1297 1287 $updatedReason = NULL;
1298 1288 if ($updated) :
1299 1289 $updatedReason = preg_replace(
1300 1290 "/\s+/", " ",
@@ -1310,9 +1300,9 @@
1310 1300 else :
1311 1301 // Or the hash...
1312 1302 $hash = $this->update_hash();
1313 1303 $seen = $this->stored_hashes($old_post->ID);
1314 - if (is_countable($seen) and count($seen) > 0) :
1304 + if (count($seen) > 0) :
1315 1305 $updated = !in_array($hash, $seen); // Not seen yet?
1316 1306 else :
1317 1307 $updated = true; // Can't find syndication meta-data
1318 1308 endif;
@@ -1336,11 +1326,11 @@
1336 1326
1337 1327 $frozen = false;
1338 1328 if ($updated) : // Ignore if the post is frozen
1339 1329 $frozen = ('yes' == $this->link->setting('freeze updates', 'freeze_updates', NULL));
1340 - if ( ! $frozen) :
1341 - $frozen_value = get_post_meta($old_post->ID, '_syndication_freeze_updates', /*single=*/ true);
1342 - $frozen = ( !is_null($frozen_value) and ('yes' == $frozen_value));
1330 + if (!$frozen) :
1331 + $frozen_values = get_post_custom_values('_syndication_freeze_updates', $old_post->ID);
1332 + $frozen = (count($frozen_values) > 0 and 'yes' == $frozen_values[0]);
1343 1333
1344 1334 if ($frozen) :
1345 1335 $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A UPDATE LOCK ON THIS POST, EVEN THOUGH IT '.$updatedReason;
1346 1336 endif;
@@ -1347,13 +1337,13 @@
1347 1337 else :
1348 1338 $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A FEEDWORDPRESS UPDATE LOCK, EVEN THOUGH IT '.$updatedReason;
1349 1339 endif;
1350 1340 endif;
1351 - $live = ($live and ! $frozen);
1341 + $live = ($live and !$frozen);
1352 1342
1353 1343 if ($updated) :
1354 1344 FeedWordPress::diagnostic('feed_items:freshness', 'Item ['.$guid.'] "'.$this->entry->get_title().'" is an update of an existing post.');
1355 - if ( !is_null($updatedReason)) :
1345 + if (!is_null($updatedReason)) :
1356 1346 $updatedReason = preg_replace('/\s+/', ' ', $updatedReason);
1357 1347 FeedWordPress::diagnostic('feed_items:freshness:reasons', 'Item ['.$guid.'] "'.$this->entry->get_title().'" '.$updatedReason);
1358 1348 endif;
1359 1349
@@ -1374,9 +1364,9 @@
1374 1364 $this->_wp_id = $old_post->ID;
1375 1365 endif;
1376 1366 endif;
1377 1367 endif;
1378 -
1368 +
1379 1369 switch ($format) :
1380 1370 case 'status' :
1381 1371 switch ($this->_freshness) :
1382 1372 case -1:
@@ -1397,17 +1387,17 @@
1397 1387 case 'number' :
1398 1388 default :
1399 1389 $ret = $this->_freshness;
1400 1390 endswitch;
1401 -
1402 -
1391 +
1392 +
1403 1393 return $ret;
1404 1394 } /* SyndicatedPost::freshness () */
1405 -
1395 +
1406 1396 function has_fresh_content () {
1407 1397 return ( ! $this->filtered() and $this->freshness() != 0 );
1408 1398 } /* SyndicatedPost::has_fresh_content () */
1409 -
1399 +
1410 1400 function this_revision_needs_original_post ($freshness = NULL) {
1411 1401 if (is_null($freshness)) :
1412 1402 $freshness = $this->freshness();
1413 1403 endif;
@@ -1412,9 +1402,9 @@
1412 1402 $freshness = $this->freshness();
1413 1403 endif;
1414 1404 return ( $freshness >= 2 );
1415 1405 }
1416 -
1406 +
1417 1407 function this_revision_is_current ($freshness = NULL) {
1418 1408 if (is_null($freshness)) :
1419 1409 $freshness = $this->freshness();
1420 1410 endif;
@@ -1419,13 +1409,13 @@
1419 1409 $freshness = $this->freshness();
1420 1410 endif;
1421 1411 return ( $freshness >= 1 );
1422 1412 } /* SyndicatedPost::this_revision_is_current () */
1423 -
1413 +
1424 1414 function fresh_content_is_update () {
1425 1415 return ($this->freshness() < 2);
1426 1416 } /* SyndicatedPost::fresh_content_is_update () */
1427 -
1417 +
1428 1418 function fresh_storage_diagnostic () {
1429 1419 $ret = NULL;
1430 1420 switch ($this->freshness()) :
1431 1421 case -1 :
@@ -1436,13 +1426,13 @@
1436 1426 break;
1437 1427 case 2 :
1438 1428 default :
1439 1429 $ret = 'Inserting new post "'.$this->post['post_title'].'"';
1440 - break;
1430 + break;
1441 1431 endswitch;
1442 1432 return $ret;
1443 1433 } /* SyndicatedPost::fresh_storage_diagnostic() */
1444 -
1434 +
1445 1435 function fresh_storage_hook () {
1446 1436 $ret = NULL;
1447 1437 switch ($this->freshness()) :
1448 1438 case -1 :
@@ -1455,9 +1445,9 @@
1455 1445 break;
1456 1446 endswitch;
1457 1447 return $ret;
1458 1448 } /* SyndicatedPost::fresh_storage_hook () */
1459 -
1449 +
1460 1450 #################################################
1461 1451 #### INTERNAL STORAGE AND MANAGEMENT METHODS ####
1462 1452 #################################################
1463 1453
@@ -1476,9 +1466,9 @@
1476 1466 * SyndicatedPost::secure_author_id(). Look up, or create, a numeric ID
1477 1467 * for the author of the incoming post.
1478 1468 *
1479 1469 * side effect: int|NULL stored in $this->post['post_author']
1480 - * side effect: IF no valid author is found, NULL stored in $this->post
1470 + * side effect: IF no valid author is found, NULL stored in $this->post
1481 1471 * side effect: diagnostic output in case item is rejected with NULL author
1482 1472 *
1483 1473 * @used-by SyndicatedPost::store
1484 1474 *
@@ -1526,19 +1516,19 @@
1526 1516 foreach ($this->feed_terms as $what => $anTerms) :
1527 1517 // Default to using the inclusive procedures (for cats) rather than exclusive (for inline tags)
1528 1518 $taxes = (isset($mapping[$what]) ? $mapping[$what] : $mapping['category']);
1529 1519 $unfamiliar = $taxes['unfamiliar'];
1530 -
1531 - if ( !is_null($this->post)) : // Not filtered out yet
1520 +
1521 + if (!is_null($this->post)) : // Not filtered out yet
1532 1522 # -- Look up, or create, numeric ID for categories
1533 1523 $taxonomies = $this->link->setting("match/".$taxes['abbr'], 'match_'.$taxes['abbr'], $taxes['domain']);
1534 1524
1535 1525 // Eliminate dummy variables
1536 - $taxonomies = array_filter( $taxonomies, 'remove_dummy_zero' );
1526 + $taxonomies = array_filter($taxonomies, 'remove_dummy_zero');
1537 1527
1538 1528 // Allow FWP add-on filters to control the taxonomies we use to search for a term
1539 1529 $taxonomies = apply_filters("syndicated_post_terms_match", $taxonomies, $what, $this);
1540 - $taxonomies = apply_filters("syndicated_post_terms_match_{$what}", $taxonomies, $this);
1530 + $taxonomies = apply_filters("syndicated_post_terms_match_${what}", $taxonomies, $this);
1541 1531
1542 1532 // Allow FWP add-on filters to control with greater precision what happens on unmatched
1543 1533 $unmatched = apply_filters("syndicated_post_terms_unfamiliar",
1544 1534 $this->link->setting(
@@ -1566,9 +1556,9 @@
1566 1556 $valid = true;
1567 1557
1568 1558 // filter mode off, or at least one match
1569 1559 foreach ($terms as $tax => $term_ids) :
1570 - if ( !isset($termSet[$tax])) :
1560 + if (!isset($termSet[$tax])) :
1571 1561 $termSet[$tax] = array();
1572 1562 endif;
1573 1563 $termSet[$tax] = array_merge($termSet[$tax], $term_ids);
1574 1564 endforeach;
@@ -1580,9 +1570,9 @@
1580 1570 $this->post = NULL;
1581 1571 else : // We can proceed
1582 1572 $this->post['tax_input'] = array();
1583 1573 foreach ($termSet as $tax => $term_ids) :
1584 - if ( !isset($this->post['tax_input'][$tax])) :
1574 + if (!isset($this->post['tax_input'][$tax])) :
1585 1575 $this->post['tax_input'][$tax] = array();
1586 1576 endif;
1587 1577 $this->post['tax_input'][$tax] = array_merge(
1588 1578 $this->post['tax_input'][$tax],
@@ -1591,9 +1581,9 @@
1591 1581 endforeach;
1592 1582
1593 1583 // Now let's add on the feed and global presets
1594 1584 foreach ($this->preset_terms as $tax => $term_ids) :
1595 - if ( !isset($this->post['tax_input'][$tax])) :
1585 + if (!isset($this->post['tax_input'][$tax])) :
1596 1586 $this->post['tax_input'][$tax] = array();
1597 1587 endif;
1598 1588
1599 1589 $this->post['tax_input'][$tax] = array_merge (
@@ -1615,13 +1605,15 @@
1615 1605 *
1616 1606 * @uses SyndicatedPost::secure_author_id
1617 1607 */
1618 1608 public function store () {
1609 + global $wpdb;
1610 +
1619 1611 if ($this->filtered()) : // This should never happen.
1620 1612 FeedWordPressDiagnostic::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1621 1613 endif;
1622 1614
1623 - $freshness = $this->freshness(); // see above: this sets WP DB id in the process (gwyneth 20230919)
1615 + $freshness = $this->freshness();
1624 1616 if ($this->has_fresh_content()) :
1625 1617 $this->secure_author_id();
1626 1618 endif;
1627 1619
@@ -1640,9 +1632,9 @@
1640 1632 $post_name = (is_null($this->_wp_post) ? NULL : $this->_wp_post->post_name);
1641 1633
1642 1634 // Allow filters to set post slug. Props niska.
1643 1635 $post_name = apply_filters('syndicated_post_slug', $post_name, $this);
1644 - if ( !empty($post_name)) :
1636 + if (!empty($post_name)) :
1645 1637 $this->post['post_name'] = $post_name;
1646 1638 endif;
1647 1639
1648 1640 $this->post = apply_filters('syndicated_post', $this->post, $this);
@@ -1661,29 +1653,29 @@
1661 1653 /*callback=*/ array($this, 'add_rss_meta'),
1662 1654 /*priority=*/ -10000, /* very early */
1663 1655 /*arguments=*/ 3
1664 1656 );
1665 -
1657 +
1666 1658 $ret = false;
1667 1659 if ($this->has_fresh_content()) :
1668 1660 $diag = $this->fresh_storage_diagnostic();
1669 - if ( !is_null($diag)) :
1661 + if (!is_null($diag)) :
1670 1662 FeedWordPress::diagnostic('syndicated_posts', $diag);
1671 1663 endif;
1672 -
1664 +
1673 1665 $this->insert_post(/*update=*/ $this->fresh_content_is_update(), $this->freshness());
1674 1666
1675 1667 $hook = $this->fresh_storage_hook();
1676 - if ( !is_null($hook)) :
1668 + if (!is_null($hook)) :
1677 1669 do_action($hook, $this->wp_id(), $this);
1678 1670 endif;
1679 -
1671 +
1680 1672 $ret = $this->freshness('status');
1681 1673 endif;
1682 1674
1683 1675 // If this is a legit, non-filtered post, tag it as found on the
1684 1676 // feed regardless of fresh or stale status
1685 - if ( ! $this->filtered()) :
1677 + if (!$this->filtered()) :
1686 1678 $key = '_feedwordpress_retire_me_' . $this->link->id;
1687 1679 delete_post_meta($this->wp_id(), $key);
1688 1680
1689 1681 $status = get_post_field('post_status', $this->wp_id());
@@ -1709,13 +1701,15 @@
1709 1701 return $ret;
1710 1702 } /* function SyndicatedPost::store () */
1711 1703
1712 1704 function insert_post ($update = false, $freshness = 2) {
1705 + global $wpdb;
1706 +
1713 1707 $dbpost = $this->normalize_post(/*new=*/ true);
1714 1708
1715 1709 $ret = null;
1716 1710
1717 - if ( !is_null($dbpost)) :
1711 + if (!is_null($dbpost)) :
1718 1712 $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks
1719 1713
1720 1714 // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data
1721 1715 add_action('_wp_put_post_revision', array($this, 'fix_revision_meta'));
@@ -1769,28 +1763,28 @@
1769 1763 // the basic post record before we do anything else.
1770 1764 if ($this->this_revision_needs_original_post()) :
1771 1765 // *sigh*, for handling inconsistent slash expectations < 3.6
1772 1766 $sdbpost = $this->db_sanitize_post($dbpost);
1773 -
1767 +
1774 1768 // Go ahead and insert the first post record to
1775 1769 // anchor the revision history.
1776 1770
1777 1771 $this->_wp_id = wp_insert_post($sdbpost, /*return wp_error=*/ true);
1778 -
1772 +
1779 1773 $dbpost['ID'] = $this->_wp_id;
1780 1774 endif;
1781 -
1775 +
1782 1776 // Sanity check: if the attempt to insert post
1783 1777 // returned an error, then feeding that error
1784 1778 // object in to _wp_put_post_revision() would
1785 1779 // cause a fatal error. Better to break out.
1786 - if ( !is_wp_error($this->_wp_id)) :
1780 + if (!is_wp_error($this->_wp_id)) :
1787 1781 // Now that we've made sure the original exists, insert
1788 1782 // this version here as a revision.
1789 1783 $revision_id = _wp_put_post_revision($dbpost, /*autosave=*/ false);
1790 1784
1791 - if ( ! $this->this_revision_needs_original_post()) :
1792 -
1785 + if (!$this->this_revision_needs_original_post()) :
1786 +
1793 1787 if ($this->this_revision_is_current()) :
1794 1788
1795 1789 wp_restore_post_revision($revision_id);
1796 1790
@@ -1830,9 +1824,9 @@
1830 1824 add_filter('content_save_pre', $filter);
1831 1825 endforeach;
1832 1826
1833 1827 $this->validate_post_id($dbpost, $update, array(__CLASS__, __FUNCTION__));
1834 -
1828 +
1835 1829 $ret = $this->_wp_id;
1836 1830 endif;
1837 1831 return $ret;
1838 1832 } /* function SyndicatedPost::insert_post () */
@@ -1859,12 +1853,14 @@
1859 1853
1860 1854 /**
1861 1855 * SyndicatedPost::normalize_post()
1862 1856 *
1863 - * @param bool $new If true, this post is to be inserted anew. If false, it is an update of an existing post. (Unused)
1857 + * @param bool $new If true, this post is to be inserted anew. If false, it is an update of an existing post.
1864 1858 * @return array A normalized representation of the post ready to be inserted into the database or sent to the WordPress API functions
1865 1859 */
1866 - function normalize_post( $new = true ) {
1860 + function normalize_post ($new = true) {
1861 + global $wpdb;
1862 +
1867 1863 $out = $this->post;
1868 1864
1869 1865 $fullPost = $out['post_title'].$out['post_content'];
1870 1866 $fullPost .= (isset($out['post_excerpt']) ? $out['post_excerpt'] : '');
@@ -1898,19 +1894,19 @@
1898 1894 // that work for MySQL but not for PHP mb_check_encoding. So instead
1899 1895 // we must rely on WordPress setting blog_charset and hope that the user
1900 1896 // has got their database encoding set up to roughly match
1901 1897 $charset = get_option('blog_charset', 'utf8');
1902 -
1898 +
1903 1899 foreach ($out as $key => $value) :
1904 1900 if (is_string($value)) :
1905 -
1906 - if ( !function_exists('mb_check_encoding') or mb_check_encoding($value, $charset)) :
1901 +
1902 + if (!function_exists('mb_check_encoding') or mb_check_encoding($value, $charset)) :
1907 1903 $out[$key] = $value;
1908 1904 else :
1909 1905 $fromCharset = mb_detect_encoding($value, mb_detect_order(), /*strict=*/ true);
1910 1906 $out[$key] = mb_convert_encoding($value, $charset, $fromCharset);
1911 1907 endif;
1912 -
1908 +
1913 1909 elseif (is_array($value)) :
1914 1910 $out[$key] = $this->db_sanitize_post_check_encoding($value);
1915 1911
1916 1912 else :
@@ -1915,25 +1911,25 @@
1915 1911
1916 1912 else :
1917 1913 $out[$key] = $value;
1918 1914 endif;
1919 -
1915 +
1920 1916 endforeach;
1921 -
1917 +
1922 1918 return $out;
1923 1919 } /* SyndicatedPost::db_sanitize_post_check_encoding () */
1924 -
1920 +
1925 1921 function db_sanitize_post ($out) {
1926 1922 global $wp_db_version;
1927 -
1923 +
1928 1924 $out = $this->db_sanitize_post_check_encoding($out);
1929 -
1925 +
1930 1926 // < 3.6. Core API, including `wp_insert_post()`, expects
1931 1927 // properly slashed data. If `wp_slash()` exists, then
1932 1928 // this is after the big change-over in how data slashing
1933 1929 // was handled.
1934 - if ( !function_exists('wp_slash')) :
1935 -
1930 + if (!function_exists('wp_slash')) :
1931 +
1936 1932 foreach ($out as $key => $value) :
1937 1933 if (is_string($value)) :
1938 1934 $out[$key] = esc_sql($value);
1939 1935 else :
@@ -1939,28 +1935,28 @@
1939 1935 else :
1940 1936 $out[$key] = $value;
1941 1937 endif;
1942 1938 endforeach;
1943 -
1939 +
1944 1940 // For revisions [@23416,@23554), core API expects
1945 1941 // unslashed data. Cf. <https://core.trac.wordpress.org/browser/trunk/wp-includes/post.php?rev=23416>
1946 1942 // NOOP for those revisions.
1947 -
1943 +
1948 1944 // In revisions @23554 to present, `wp_insert_post()`
1949 1945 // expects slashed data once again.
1950 1946 // Cf. <https://core.trac.wordpress.org/changeset/23554/trunk/wp-includes/post.php?contextall=1>
1951 1947 // But at least now we can use the wp_slash API function to do that.
1952 1948 // Hooray.
1953 -
1949 +
1954 1950 elseif ($wp_db_version >= 23524) :
1955 -
1951 +
1956 1952 $out = wp_slash($out);
1957 -
1953 +
1958 1954 endif;
1959 1955
1960 1956 return $out;
1961 1957 }
1962 -
1958 +
1963 1959 /**
1964 1960 * SyndicatedPost::validate_post_id()
1965 1961 *
1966 1962 * @param array $dbpost An array representing the post we attempted to insert or update
@@ -1970,9 +1966,9 @@
1970 1966 if (is_array($ns)) : $ns = implode('::', $ns);
1971 1967 else : $ns = (string) $ns; endif;
1972 1968
1973 1969 // This should never happen.
1974 - if ( !is_numeric($this->_wp_id) or ($this->_wp_id == 0)) :
1970 + if (!is_numeric($this->_wp_id) or ($this->_wp_id == 0)) :
1975 1971 $verb = ($is_update ? 'update existing' : 'insert new');
1976 1972 $guid = $this->guid();
1977 1973 $url = $this->permalink();
1978 1974 $feed = $this->link->uri(array('add_params' => true));
@@ -1981,9 +1977,9 @@
1981 1977 // notice if we are in debug mode.
1982 1978 $mesg = "Failed to $verb item [$guid]. WordPress API returned no valid post ID.\n"
1983 1979 ."\t\tID = ".serialize($this->_wp_id)."\n"
1984 1980 ."\t\tURL = ".MyPHP::val($url)
1985 - ."\t\tFeed = ".MyPHP::val($feed);
1981 + ."\t\tFeed = ".MyPHP::val($feed);
1986 1982
1987 1983 FeedWordPress::diagnostic('updated_feeds:errors', "WordPress API error: $mesg");
1988 1984 FeedWordPress::diagnostic('feed_items:rejected', $mesg);
1989 1985
@@ -2022,22 +2018,24 @@
2022 2018 * by _wp_put_post_revision.
2023 2019 *
2024 2020 * @param int $revision_id The revision ID to fix up meta-data
2025 2021 */
2026 - function fix_revision_meta( $revision_id ) {
2027 - $post_author = (int) $this->post['post_author']; // is this a global? And if it is, where is it defined? (gwyneth 20230919)
2022 + function fix_revision_meta ($revision_id) {
2023 + global $wpdb;
2028 2024
2025 + $post_author = (int) $this->post['post_author'];
2026 +
2029 2027 $revision_id = (int) $revision_id;
2030 -
2028 +
2031 2029 // Let's fix the author.
2032 - set_post_field( 'post_author', $this->post['post_author'], $revision_id );
2033 -
2030 + set_post_field('post_author', $this->post['post_author'], $revision_id);
2031 +
2034 2032 // Let's fix the GUID to a dummy URL with the update hash.
2035 - set_post_field( 'guid', 'http://feedwordpress.radgeek.com/?rev=' . $this->update_hash(), $revision_id );
2033 + set_post_field('guid', 'http://feedwordpress.radgeek.com/?rev='.$this->update_hash(), $revision_id);
2036 2034
2037 2035 // Let's fire an event for add-ons and filters
2038 - do_action( 'syndicated_post_fix_revision_meta', $revision_id, $this );
2039 -
2036 + do_action('syndicated_post_fix_revision_meta', $revision_id, $this);
2037 +
2040 2038 } /* SyndicatedPost::fix_revision_meta () */
2041 2039
2042 2040 /**
2043 2041 * SyndicatedPost::add_terms() -- if FeedWordPress is processing an
@@ -2125,9 +2123,9 @@
2125 2123 * @param string $new_status Unused action parameter.
2126 2124 * @param string $old_status Unused action parameter.
2127 2125 * @param object $post The database record for the post just inserted.
2128 2126 */
2129 - function add_rss_meta($new_status, $old_status, $post) {
2127 + function add_rss_meta ($new_status, $old_status, $post) {
2130 2128 global $wpdb;
2131 2129 if ($new_status!='inherit') : // Bail if we are creating a revision.
2132 2130 FeedWordPress::diagnostic('syndicated_posts:meta_data', 'Adding post meta-data: {'.implode(", ", array_keys($this->post['meta'])).'}');
2133 2131
@@ -2153,19 +2151,14 @@
2153 2151 WHERE post_id='$postId' AND meta_key='$eKey'
2154 2152 ");
2155 2153
2156 2154 // Allow for either a single value or an array
2157 - if ( !is_array($values)) $values = array($values);
2155 + if (!is_array($values)) $values = array($values);
2158 2156 foreach ( $values as $value ) :
2159 2157 FeedWordPress::diagnostic('syndicated_posts:meta_data', "Adding post meta-datum to post [$postId]: [$key] = ".MyPHP::val($value, /*no newlines=*/ true));
2160 2158 add_post_meta($postId, $key, $value, /*unique=*/ false);
2161 2159 endforeach;
2162 2160 endforeach;
2163 -
2164 - if ( $result === false)
2165 - {
2166 - error_log( "delete query failed: " . $wpdb->last_error );
2167 - }
2168 2161 endif;
2169 2162 endif;
2170 2163 } /* SyndicatedPost::add_rss_meta () */
2171 2164
@@ -2194,9 +2187,9 @@
2194 2187 // as last resort
2195 2188
2196 2189 $candidates = array();
2197 2190 $candidates[] = $a['name'];
2198 - if ( !is_null($source)) : $candidates[] = $source['title']; endif;
2191 + if (!is_null($source)) : $candidates[] = $source['title']; endif;
2199 2192 $candidates[] = $this->link->name(/*fromFeed=*/ true);
2200 2193 $candidates[] = $this->link->name(/*fromFeed=*/ false);
2201 2194 if (strlen($this->link->homepage()) > 0) : $candidates[] = feedwordpress_display_url($this->link->homepage()); endif;
2202 2195 $candidates[] = feedwordpress_display_url($this->link->uri());
@@ -2206,9 +2199,9 @@
2206 2199 // or forbidden names.
2207 2200
2208 2201 $author = NULL;
2209 2202 foreach ($candidates as $candidate) {
2210 - if ( !is_null($candidate)
2203 + if (!is_null($candidate)
2211 2204 and (strlen(trim($candidate)) > 0)
2212 2205 and !in_array(strtolower(trim($candidate)), $forbidden)) :
2213 2206 $author = $candidate;
2214 2207 break;
@@ -2257,9 +2250,9 @@
2257 2250
2258 2251 $nice_author = sanitize_title($author);
2259 2252 $nice_author = apply_filters('pre_user_nicename', $nice_author);
2260 2253
2261 - $reg_author = esc_sql(preg_quote($author ?: "(unknown)"));
2254 + $reg_author = esc_sql(preg_quote($author));
2262 2255 $author = esc_sql($author);
2263 2256 $email = esc_sql($email);
2264 2257 $test_email = esc_sql($test_email);
2265 2258 $authorUrl = esc_sql($authorUrl);
@@ -2273,11 +2266,9 @@
2273 2266 $author_rule = NULL;
2274 2267 endif;
2275 2268
2276 2269 // User name is mapped to a particular author. If that author ID exists, use it.
2277 - // Note: get_userdata() is a pluggable function, so it might not be available yet;
2278 - // check if it exists before using it, or WP will throw a fatal error. (gwyneth 20220223)
2279 - if (is_numeric($author_rule) and function_exists('get_userdata') and get_userdata((int) $author_rule)) :
2270 + if (is_numeric($author_rule) and get_userdata((int) $author_rule)) :
2280 2271 $id = (int) $author_rule;
2281 2272
2282 2273 // User name is filtered out
2283 2274 elseif ('filter' == $author_rule) :
@@ -2344,9 +2335,8 @@
2344 2335 $userdata['role'] = 'contributor';
2345 2336
2346 2337 #-- loop. Keep trying to add the user until you get it
2347 2338 #-- right. Or until PHP crashes, I guess.
2348 - $insanity = 0;
2349 2339 do {
2350 2340 $id = wp_insert_user($userdata);
2351 2341 if (is_wp_error($id)) :
2352 2342 $codes = $id->get_error_code();
@@ -2352,17 +2342,11 @@
2352 2342 $codes = $id->get_error_code();
2353 2343 switch ($codes) :
2354 2344 case 'empty_user_login' :
2355 2345 case 'existing_user_login' :
2356 - case 'invalid_username' :
2357 2346 // Add a random disambiguator
2358 2347 $userdata['user_login'] .= substr(md5(uniqid(microtime())), 0, 6);
2359 2348 break;
2360 - case 'user_login_too_long' :
2361 - // Limit length to 53 characters; if we end up needing a random disambiguator,
2362 - // we should still have space to add it.
2363 - $userdata['user_login'] = mb_substr( $userdata['user_login'], 0, 53 );
2364 - break;
2365 2349 case 'user_nicename_too_long' :
2366 2350 // Add a limited 50 characters user_nicename based on user_login
2367 2351 $userdata['user_nicename'] = mb_substr( $userdata['user_login'], 0, 50 );
2368 2352 break;
@@ -2375,23 +2359,10 @@
2375 2359
2376 2360 // Reassemble
2377 2361 $userdata['user_email'] = $parts[0].'@'.$parts[1];
2378 2362 break;
2379 - default :
2380 - if ( $insanity > 10 ) :
2381 - // Try some settings that are unlikely to cause complaint...
2382 - $url = parse_url($hostUrl);
2383 -
2384 - $userdata['user_login'] = substr(md5(uniqid(microtime())), 0, 6);
2385 - $userdata['user_nicename'] = $userdata['user_login'];
2386 - $userdata['user_email'] = 'noreply@' . $url['host'];
2387 - elseif ( $insanity > 50 ) :
2388 - // Stop doing the same thing and expecting a different result
2389 - break;
2390 - endif;
2391 2363 endswitch;
2392 2364 endif;
2393 - $insanity = $insanity + 1;
2394 2365 } while (is_wp_error($id));
2395 2366
2396 2367 // $id should now contain the numeric ID of a newly minted
2397 2368 // user account. Let's mark them as having been generated
@@ -2397,17 +2368,13 @@
2397 2368 // user account. Let's mark them as having been generated
2398 2369 // by FeedWordPress in the usermeta table, as per the
2399 2370 // suggestion of @boonebgorges, in case we need to process,
2400 2371 // winnow, filter, or merge syndicated author accounts, &c.
2401 - if ( !is_wp_error($id)) :
2402 - add_user_meta($id, 'feedwordpress_generated', 1);
2403 - else :
2404 - $id = null;
2405 - endif;
2406 - // also see comment above regarding get_userdata(). (gwyneth 20220223)
2407 - elseif ( is_numeric( $unfamiliar_author ) and function_exists( 'get_userdata ') and get_userdata((int) $unfamiliar_author ) ) :
2372 + add_user_meta($id, 'feedwordpress_generated', 1);
2373 +
2374 + elseif (is_numeric($unfamiliar_author) and get_userdata((int) $unfamiliar_author)) :
2408 2375 $id = (int) $unfamiliar_author;
2409 - elseif ( $unfamiliar_author === 'default' ) :
2376 + elseif ($unfamiliar_author === 'default') :
2410 2377 $id = 1;
2411 2378 endif;
2412 2379 endif;
2413 2380 endif;