PluginProbe
FeedWordPress / 2017.1020
FeedWordPress v2017.1020
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 +343 -446 trunk2017.1020 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,11 +1224,12 @@
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 - FeedWordPressDiagnostic::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1231 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1242 1232 endif;
1243 1233
1244 1234 if (is_null($this->_freshness)) : // Not yet checked and cached.
1245 1235 $guid = $this->post['guid'];
@@ -1247,17 +1237,15 @@
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()) :
1256 1246 while ($q->have_posts()) : $q->the_post();
1257 - if (get_post_type($q->post->ID) == $this->post['post_type']):
1258 - $old_post = $q->post;
1259 - endif;
1247 + $old_post = $q->post;
1260 1248 endwhile;
1261 1249 endif;
1262 1250
1263 1251 if (is_null($old_post)) : // No post with this guid
@@ -1268,23 +1256,23 @@
1268 1256 // Presume there is nothing new until we find
1269 1257 // something new.
1270 1258 $updated = false;
1271 1259 $live = false;
1272 -
1260 +
1273 1261 // Pull the list of existing revisions to get
1274 1262 // timestamps.
1275 1263 $revisions = wp_get_post_revisions($old_post->ID);
1276 1264 foreach ($revisions as $rev) :
1277 - $revisions_ts[] = mysql2date('G', $rev->post_modified_gmt);
1265 + $revisions_ts[] = mysql2date('G', $rev->post_modified_gmt);
1278 1266 endforeach;
1279 1267
1280 1268 $revisions_ts[] = mysql2date('G', $old_post->post_modified_gmt);
1281 1269 $last_rev_ts = end($revisions_ts);
1282 1270 $updated_ts = $this->updated(/*fallback=*/ true, /*default=*/ NULL);
1283 -
1271 +
1284 1272 // If we have an explicit updated timestamp,
1285 1273 // check that against existing stamps.
1286 - if ( !is_null($updated_ts)) :
1274 + if (!is_null($updated_ts)) :
1287 1275 $updated = !in_array($updated_ts, $revisions_ts);
1288 1276
1289 1277 // If this a newer revision, make it go
1290 1278 // live. If an older one, just record
@@ -1292,9 +1280,9 @@
1292 1280 $live = ($updated and ($updated_ts > $last_rev_ts));
1293 1281 endif;
1294 1282
1295 1283 // This is a revision we haven't seen before, judging by the date.
1296 -
1284 +
1297 1285 $updatedReason = NULL;
1298 1286 if ($updated) :
1299 1287 $updatedReason = preg_replace(
1300 1288 "/\s+/", " ",
@@ -1310,15 +1298,15 @@
1310 1298 else :
1311 1299 // Or the hash...
1312 1300 $hash = $this->update_hash();
1313 1301 $seen = $this->stored_hashes($old_post->ID);
1314 - if (is_countable($seen) and count($seen) > 0) :
1302 + if (count($seen) > 0) :
1315 1303 $updated = !in_array($hash, $seen); // Not seen yet?
1316 1304 else :
1317 1305 $updated = true; // Can't find syndication meta-data
1318 1306 endif;
1319 1307
1320 - if ($updated and FeedWordPressDiagnostic::is_on('feed_items:freshness:reasons')) :
1308 + if ($updated and FeedWordPress::diagnostic_on('feed_items:freshness:reasons')) :
1321 1309 // In the absence of definitive
1322 1310 // timestamp information, we
1323 1311 // just have to assume that a
1324 1312 // hash we haven't seen before
@@ -1336,11 +1324,11 @@
1336 1324
1337 1325 $frozen = false;
1338 1326 if ($updated) : // Ignore if the post is frozen
1339 1327 $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));
1328 + if (!$frozen) :
1329 + $frozen_values = get_post_custom_values('_syndication_freeze_updates', $old_post->ID);
1330 + $frozen = (count($frozen_values) > 0 and 'yes' == $frozen_values[0]);
1343 1331
1344 1332 if ($frozen) :
1345 1333 $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A UPDATE LOCK ON THIS POST, EVEN THOUGH IT '.$updatedReason;
1346 1334 endif;
@@ -1347,13 +1335,13 @@
1347 1335 else :
1348 1336 $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A FEEDWORDPRESS UPDATE LOCK, EVEN THOUGH IT '.$updatedReason;
1349 1337 endif;
1350 1338 endif;
1351 - $live = ($live and ! $frozen);
1339 + $live = ($live and !$frozen);
1352 1340
1353 1341 if ($updated) :
1354 1342 FeedWordPress::diagnostic('feed_items:freshness', 'Item ['.$guid.'] "'.$this->entry->get_title().'" is an update of an existing post.');
1355 - if ( !is_null($updatedReason)) :
1343 + if (!is_null($updatedReason)) :
1356 1344 $updatedReason = preg_replace('/\s+/', ' ', $updatedReason);
1357 1345 FeedWordPress::diagnostic('feed_items:freshness:reasons', 'Item ['.$guid.'] "'.$this->entry->get_title().'" '.$updatedReason);
1358 1346 endif;
1359 1347
@@ -1374,9 +1362,9 @@
1374 1362 $this->_wp_id = $old_post->ID;
1375 1363 endif;
1376 1364 endif;
1377 1365 endif;
1378 -
1366 +
1379 1367 switch ($format) :
1380 1368 case 'status' :
1381 1369 switch ($this->_freshness) :
1382 1370 case -1:
@@ -1397,17 +1385,17 @@
1397 1385 case 'number' :
1398 1386 default :
1399 1387 $ret = $this->_freshness;
1400 1388 endswitch;
1401 -
1402 -
1389 +
1390 +
1403 1391 return $ret;
1404 1392 } /* SyndicatedPost::freshness () */
1405 -
1393 +
1406 1394 function has_fresh_content () {
1407 1395 return ( ! $this->filtered() and $this->freshness() != 0 );
1408 1396 } /* SyndicatedPost::has_fresh_content () */
1409 -
1397 +
1410 1398 function this_revision_needs_original_post ($freshness = NULL) {
1411 1399 if (is_null($freshness)) :
1412 1400 $freshness = $this->freshness();
1413 1401 endif;
@@ -1412,9 +1400,9 @@
1412 1400 $freshness = $this->freshness();
1413 1401 endif;
1414 1402 return ( $freshness >= 2 );
1415 1403 }
1416 -
1404 +
1417 1405 function this_revision_is_current ($freshness = NULL) {
1418 1406 if (is_null($freshness)) :
1419 1407 $freshness = $this->freshness();
1420 1408 endif;
@@ -1419,13 +1407,13 @@
1419 1407 $freshness = $this->freshness();
1420 1408 endif;
1421 1409 return ( $freshness >= 1 );
1422 1410 } /* SyndicatedPost::this_revision_is_current () */
1423 -
1411 +
1424 1412 function fresh_content_is_update () {
1425 1413 return ($this->freshness() < 2);
1426 1414 } /* SyndicatedPost::fresh_content_is_update () */
1427 -
1415 +
1428 1416 function fresh_storage_diagnostic () {
1429 1417 $ret = NULL;
1430 1418 switch ($this->freshness()) :
1431 1419 case -1 :
@@ -1436,13 +1424,13 @@
1436 1424 break;
1437 1425 case 2 :
1438 1426 default :
1439 1427 $ret = 'Inserting new post "'.$this->post['post_title'].'"';
1440 - break;
1428 + break;
1441 1429 endswitch;
1442 1430 return $ret;
1443 1431 } /* SyndicatedPost::fresh_storage_diagnostic() */
1444 -
1432 +
1445 1433 function fresh_storage_hook () {
1446 1434 $ret = NULL;
1447 1435 switch ($this->freshness()) :
1448 1436 case -1 :
@@ -1455,9 +1443,9 @@
1455 1443 break;
1456 1444 endswitch;
1457 1445 return $ret;
1458 1446 } /* SyndicatedPost::fresh_storage_hook () */
1459 -
1447 +
1460 1448 #################################################
1461 1449 #### INTERNAL STORAGE AND MANAGEMENT METHODS ####
1462 1450 #################################################
1463 1451
@@ -1462,9 +1450,9 @@
1462 1450 #################################################
1463 1451
1464 1452 function wp_id () {
1465 1453 if ($this->filtered()) : // This should never happen.
1466 - FeedWordPressDiagnostic::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1454 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1467 1455 endif;
1468 1456
1469 1457 if (is_null($this->_wp_id) and is_null($this->_freshness)) :
1470 1458 $fresh = $this->freshness(); // sets WP DB id in the process
@@ -1471,165 +1459,124 @@
1471 1459 endif;
1472 1460 return $this->_wp_id;
1473 1461 }
1474 1462
1475 - /**
1476 - * SyndicatedPost::secure_author_id(). Look up, or create, a numeric ID
1477 - * for the author of the incoming post.
1478 - *
1479 - * side effect: int|NULL stored in $this->post['post_author']
1480 - * side effect: IF no valid author is found, NULL stored in $this->post
1481 - * side effect: diagnostic output in case item is rejected with NULL author
1482 - *
1483 - * @used-by SyndicatedPost::store
1484 - *
1485 - * @uses SyndicatedPost::post
1486 - * @uses SyndicatedPost::author_id
1487 - * @uses SyndicatedLink::setting
1488 - * @uses FeedWordPress::diagnostic
1489 - */
1490 - protected function secure_author_id () {
1491 - # -- Look up, or create, numeric ID for author
1492 - $this->post['post_author'] = $this->author_id (
1493 - $this->link->setting('unfamiliar author', 'unfamiliar_author', 'create')
1494 - );
1463 + public function store () {
1464 + global $wpdb;
1495 1465
1496 - if (is_null($this->post['post_author'])) :
1497 - FeedWordPress::diagnostic('feed_items:rejected', 'Filtered out item ['.$this->guid().'] without syndication: no author available');
1498 - $this->post = NULL;
1466 + if ($this->filtered()) : // This should never happen.
1467 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1499 1468 endif;
1500 - } /* SyndicatedPost::secure_author_id() */
1501 1469
1502 - /**
1503 - * SyndicatedPost::secure_term_ids(). Look up, or create, numeric IDs
1504 - * for the terms (categories, tags, etc.) assigned to the incoming post,
1505 - * whether by global settings, feed settings, or by the tags on the feed.
1506 - *
1507 - * side effect: array of term ids stored in $this->post['tax_input']
1508 - * side effect: IF settings or filters determine post should be filtered out,
1509 - * NULL stored in $this->post
1510 - *
1511 - * @used-by SyndicatedPost::store
1512 - *
1513 - * @uses apply_filters
1514 - * @uses SyndicatedLink::setting
1515 - * @uses SyndicatedPost::category_ids
1516 - * @uses SyndicatedPost::preset_terms
1517 - * @uses SyndicatedPost::post
1518 - */
1519 - protected function secure_term_ids () {
1520 - $mapping = apply_filters('syndicated_post_terms_mapping', array(
1521 - 'category' => array('abbr' => 'cats', 'unfamiliar' => 'category', 'domain' => array('category', 'post_tag')),
1522 - 'post_tag' => array('abbr' => 'tags', 'unfamiliar' => 'post_tag', 'domain' => array('post_tag')),
1523 - ), $this);
1470 + $freshness = $this->freshness();
1471 + if ($this->has_fresh_content()) :
1472 + # -- Look up, or create, numeric ID for author
1473 + $this->post['post_author'] = $this->author_id (
1474 + $this->link->setting('unfamiliar author', 'unfamiliar_author', 'create')
1475 + );
1524 1476
1525 - $termSet = array(); $valid = null;
1526 - foreach ($this->feed_terms as $what => $anTerms) :
1527 - // Default to using the inclusive procedures (for cats) rather than exclusive (for inline tags)
1528 - $taxes = (isset($mapping[$what]) ? $mapping[$what] : $mapping['category']);
1529 - $unfamiliar = $taxes['unfamiliar'];
1477 + if (is_null($this->post['post_author'])) :
1478 + FeedWordPress::diagnostic('feed_items:rejected', 'Filtered out item ['.$this->guid().'] without syndication: no author available');
1479 + $this->post = NULL;
1480 + endif;
1481 + endif;
1530 1482
1531 - if ( !is_null($this->post)) : // Not filtered out yet
1532 - # -- Look up, or create, numeric ID for categories
1533 - $taxonomies = $this->link->setting("match/".$taxes['abbr'], 'match_'.$taxes['abbr'], $taxes['domain']);
1483 + // We have to check again in case post has been filtered during
1484 + // the author_id lookup
1485 + if ($this->has_fresh_content()) :
1486 + $mapping = apply_filters('syndicated_post_terms_mapping', array(
1487 + 'category' => array('abbr' => 'cats', 'unfamiliar' => 'category', 'domain' => array('category', 'post_tag')),
1488 + 'post_tag' => array('abbr' => 'tags', 'unfamiliar' => 'post_tag', 'domain' => array('post_tag')),
1489 + ), $this);
1534 1490
1535 - // Eliminate dummy variables
1536 - $taxonomies = array_filter( $taxonomies, 'remove_dummy_zero' );
1491 + $termSet = array(); $valid = null;
1492 + foreach ($this->feed_terms as $what => $anTerms) :
1493 + // Default to using the inclusive procedures (for cats) rather than exclusive (for inline tags)
1494 + $taxes = (isset($mapping[$what]) ? $mapping[$what] : $mapping['category']);
1495 + $unfamiliar = $taxes['unfamiliar'];
1496 +
1497 + if (!is_null($this->post)) : // Not filtered out yet
1498 + # -- Look up, or create, numeric ID for categories
1499 + $taxonomies = $this->link->setting("match/".$taxes['abbr'], 'match_'.$taxes['abbr'], $taxes['domain']);
1537 1500
1538 - // Allow FWP add-on filters to control the taxonomies we use to search for a term
1539 - $taxonomies = apply_filters("syndicated_post_terms_match", $taxonomies, $what, $this);
1540 - $taxonomies = apply_filters("syndicated_post_terms_match_{$what}", $taxonomies, $this);
1501 + // Eliminate dummy variables
1502 + $taxonomies = array_filter($taxonomies, 'remove_dummy_zero');
1541 1503
1542 - // Allow FWP add-on filters to control with greater precision what happens on unmatched
1543 - $unmatched = apply_filters("syndicated_post_terms_unfamiliar",
1544 - $this->link->setting(
1545 - "unfamiliar {$unfamiliar}",
1546 - "unfamiliar_{$unfamiliar}",
1547 - 'create:'.$unfamiliar
1548 - ),
1549 - $what,
1550 - $this
1551 - );
1504 + // Allow FWP add-on filters to control the taxonomies we use to search for a term
1505 + $taxonomies = apply_filters("syndicated_post_terms_match", $taxonomies, $what, $this);
1506 + $taxonomies = apply_filters("syndicated_post_terms_match_${what}", $taxonomies, $this);
1552 1507
1553 - $terms = $this->category_ids (
1554 - $anTerms,
1555 - $unmatched,
1556 - /*taxonomies=*/ $taxonomies,
1557 - array(
1558 - 'singleton' => false, // I don't like surprises
1559 - 'filters' => true,
1560 - )
1561 - );
1508 + // Allow FWP add-on filters to control with greater precision what happens on unmatched
1509 + $unmatched = apply_filters("syndicated_post_terms_unfamiliar",
1510 + $this->link->setting(
1511 + "unfamiliar {$unfamiliar}",
1512 + "unfamiliar_{$unfamiliar}",
1513 + 'create:'.$unfamiliar
1514 + ),
1515 + $what,
1516 + $this
1517 + );
1562 1518
1563 - if (is_null($terms) or is_null($termSet)) :
1564 - // filtered out -- no matches
1565 - else :
1566 - $valid = true;
1519 + $terms = $this->category_ids (
1520 + $anTerms,
1521 + $unmatched,
1522 + /*taxonomies=*/ $taxonomies,
1523 + array(
1524 + 'singleton' => false, // I don't like surprises
1525 + 'filters' => true,
1526 + )
1527 + );
1567 1528
1568 - // filter mode off, or at least one match
1569 - foreach ($terms as $tax => $term_ids) :
1570 - if ( !isset($termSet[$tax])) :
1571 - $termSet[$tax] = array();
1572 - endif;
1573 - $termSet[$tax] = array_merge($termSet[$tax], $term_ids);
1574 - endforeach;
1575 - endif;
1576 - endif;
1577 - endforeach;
1529 + if (is_null($terms) or is_null($termSet)) :
1530 + // filtered out -- no matches
1531 + else :
1532 + $valid = true;
1578 1533
1579 - if (is_null($valid)) : // Plonked
1580 - $this->post = NULL;
1581 - else : // We can proceed
1582 - $this->post['tax_input'] = array();
1583 - foreach ($termSet as $tax => $term_ids) :
1584 - if ( !isset($this->post['tax_input'][$tax])) :
1585 - $this->post['tax_input'][$tax] = array();
1534 + // filter mode off, or at least one match
1535 + foreach ($terms as $tax => $term_ids) :
1536 + if (!isset($termSet[$tax])) :
1537 + $termSet[$tax] = array();
1538 + endif;
1539 + $termSet[$tax] = array_merge($termSet[$tax], $term_ids);
1540 + endforeach;
1541 + endif;
1586 1542 endif;
1587 - $this->post['tax_input'][$tax] = array_merge(
1588 - $this->post['tax_input'][$tax],
1589 - $term_ids
1590 - );
1591 1543 endforeach;
1592 1544
1593 - // Now let's add on the feed and global presets
1594 - foreach ($this->preset_terms as $tax => $term_ids) :
1595 - if ( !isset($this->post['tax_input'][$tax])) :
1596 - $this->post['tax_input'][$tax] = array();
1597 - endif;
1545 + if (is_null($valid)) : // Plonked
1546 + $this->post = NULL;
1547 + else : // We can proceed
1548 + $this->post['tax_input'] = array();
1549 + foreach ($termSet as $tax => $term_ids) :
1550 + if (!isset($this->post['tax_input'][$tax])) :
1551 + $this->post['tax_input'][$tax] = array();
1552 + endif;
1553 + $this->post['tax_input'][$tax] = array_merge(
1554 + $this->post['tax_input'][$tax],
1555 + $term_ids
1556 + );
1557 + endforeach;
1598 1558
1599 - $this->post['tax_input'][$tax] = array_merge (
1600 - $this->post['tax_input'][$tax],
1601 - $this->category_ids (
1602 - /*terms=*/ $term_ids,
1603 - /*unfamiliar=*/ 'create:'.$tax, // These are presets; for those added in a tagbox editor, the tag may not yet exist
1604 - /*taxonomies=*/ array($tax),
1605 - array(
1606 - 'singleton' => true,
1607 - ))
1608 - );
1609 - endforeach;
1610 - endif;
1611 - } /* SyndicatedPost::secure_term_ids() */
1559 + // Now let's add on the feed and global presets
1560 + foreach ($this->preset_terms as $tax => $term_ids) :
1561 + if (!isset($this->post['tax_input'][$tax])) :
1562 + $this->post['tax_input'][$tax] = array();
1563 + endif;
1612 1564
1613 - /**
1614 - * SyndicatedPost::store
1615 - *
1616 - * @uses SyndicatedPost::secure_author_id
1617 - */
1618 - public function store () {
1619 - if ($this->filtered()) : // This should never happen.
1620 - FeedWordPressDiagnostic::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1565 + $this->post['tax_input'][$tax] = array_merge (
1566 + $this->post['tax_input'][$tax],
1567 + $this->category_ids (
1568 + /*terms=*/ $term_ids,
1569 + /*unfamiliar=*/ 'create:'.$tax, // These are presets; for those added in a tagbox editor, the tag may not yet exist
1570 + /*taxonomies=*/ array($tax),
1571 + array(
1572 + 'singleton' => true,
1573 + ))
1574 + );
1575 + endforeach;
1576 + endif;
1621 1577 endif;
1622 1578
1623 - $freshness = $this->freshness(); // see above: this sets WP DB id in the process (gwyneth 20230919)
1624 - if ($this->has_fresh_content()) :
1625 - $this->secure_author_id();
1626 - endif;
1627 -
1628 - if ($this->has_fresh_content()) : // Was this filtered during author_id lookup?
1629 - $this->secure_term_ids();
1630 - endif;
1631 -
1632 1579 // We have to check again in case the post has been filtered
1633 1580 // during the category/tags/taxonomy terms lookup
1634 1581 if ($this->has_fresh_content()) :
1635 1582 // Filter some individual fields
@@ -1640,9 +1587,9 @@
1640 1587 $post_name = (is_null($this->_wp_post) ? NULL : $this->_wp_post->post_name);
1641 1588
1642 1589 // Allow filters to set post slug. Props niska.
1643 1590 $post_name = apply_filters('syndicated_post_slug', $post_name, $this);
1644 - if ( !empty($post_name)) :
1591 + if (!empty($post_name)) :
1645 1592 $this->post['post_name'] = $post_name;
1646 1593 endif;
1647 1594
1648 1595 $this->post = apply_filters('syndicated_post', $this->post, $this);
@@ -1661,29 +1608,29 @@
1661 1608 /*callback=*/ array($this, 'add_rss_meta'),
1662 1609 /*priority=*/ -10000, /* very early */
1663 1610 /*arguments=*/ 3
1664 1611 );
1665 -
1612 +
1666 1613 $ret = false;
1667 1614 if ($this->has_fresh_content()) :
1668 1615 $diag = $this->fresh_storage_diagnostic();
1669 - if ( !is_null($diag)) :
1616 + if (!is_null($diag)) :
1670 1617 FeedWordPress::diagnostic('syndicated_posts', $diag);
1671 1618 endif;
1672 -
1619 +
1673 1620 $this->insert_post(/*update=*/ $this->fresh_content_is_update(), $this->freshness());
1674 1621
1675 1622 $hook = $this->fresh_storage_hook();
1676 - if ( !is_null($hook)) :
1623 + if (!is_null($hook)) :
1677 1624 do_action($hook, $this->wp_id(), $this);
1678 1625 endif;
1679 -
1626 +
1680 1627 $ret = $this->freshness('status');
1681 1628 endif;
1682 1629
1683 1630 // If this is a legit, non-filtered post, tag it as found on the
1684 1631 // feed regardless of fresh or stale status
1685 - if ( ! $this->filtered()) :
1632 + if (!$this->filtered()) :
1686 1633 $key = '_feedwordpress_retire_me_' . $this->link->id;
1687 1634 delete_post_meta($this->wp_id(), $key);
1688 1635
1689 1636 $status = get_post_field('post_status', $this->wp_id());
@@ -1709,13 +1656,15 @@
1709 1656 return $ret;
1710 1657 } /* function SyndicatedPost::store () */
1711 1658
1712 1659 function insert_post ($update = false, $freshness = 2) {
1660 + global $wpdb;
1661 +
1713 1662 $dbpost = $this->normalize_post(/*new=*/ true);
1714 1663
1715 1664 $ret = null;
1716 1665
1717 - if ( !is_null($dbpost)) :
1666 + if (!is_null($dbpost)) :
1718 1667 $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks
1719 1668
1720 1669 // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data
1721 1670 add_action('_wp_put_post_revision', array($this, 'fix_revision_meta'));
@@ -1769,28 +1718,28 @@
1769 1718 // the basic post record before we do anything else.
1770 1719 if ($this->this_revision_needs_original_post()) :
1771 1720 // *sigh*, for handling inconsistent slash expectations < 3.6
1772 1721 $sdbpost = $this->db_sanitize_post($dbpost);
1773 -
1722 +
1774 1723 // Go ahead and insert the first post record to
1775 1724 // anchor the revision history.
1776 1725
1777 1726 $this->_wp_id = wp_insert_post($sdbpost, /*return wp_error=*/ true);
1778 -
1727 +
1779 1728 $dbpost['ID'] = $this->_wp_id;
1780 1729 endif;
1781 -
1730 +
1782 1731 // Sanity check: if the attempt to insert post
1783 1732 // returned an error, then feeding that error
1784 1733 // object in to _wp_put_post_revision() would
1785 1734 // cause a fatal error. Better to break out.
1786 - if ( !is_wp_error($this->_wp_id)) :
1735 + if (!is_wp_error($this->_wp_id)) :
1787 1736 // Now that we've made sure the original exists, insert
1788 1737 // this version here as a revision.
1789 1738 $revision_id = _wp_put_post_revision($dbpost, /*autosave=*/ false);
1790 1739
1791 - if ( ! $this->this_revision_needs_original_post()) :
1792 -
1740 + if (!$this->this_revision_needs_original_post()) :
1741 +
1793 1742 if ($this->this_revision_is_current()) :
1794 1743
1795 1744 wp_restore_post_revision($revision_id);
1796 1745
@@ -1830,30 +1779,18 @@
1830 1779 add_filter('content_save_pre', $filter);
1831 1780 endforeach;
1832 1781
1833 1782 $this->validate_post_id($dbpost, $update, array(__CLASS__, __FUNCTION__));
1834 -
1783 +
1835 1784 $ret = $this->_wp_id;
1836 1785 endif;
1837 1786 return $ret;
1838 1787 } /* function SyndicatedPost::insert_post () */
1839 1788
1840 - /**
1841 - * SyndicatedPost::insert_new(). Uses the data collected in this post object to insert
1842 - * a new post into the wp_posts table.
1843 - *
1844 - * @uses SyndicatedPost::insert_post
1845 - */
1846 1789 function insert_new () {
1847 1790 $this->insert_post(/*update=*/ false, 1);
1848 1791 } /* SyndicatedPost::insert_new() */
1849 1792
1850 - /**
1851 - * SyndicatedPost::insert_new(). Uses the data collected in this post object to update
1852 - * an existing post in the wp_posts table.
1853 - *
1854 - * @uses SyndicatedPost::insert_post
1855 - */
1856 1793 function update_existing () {
1857 1794 $this->insert_post(/*update=*/ true, 2);
1858 1795 } /* SyndicatedPost::update_existing() */
1859 1796
@@ -1859,12 +1796,14 @@
1859 1796
1860 1797 /**
1861 1798 * SyndicatedPost::normalize_post()
1862 1799 *
1863 - * @param bool $new If true, this post is to be inserted anew. If false, it is an update of an existing post. (Unused)
1800 + * @param bool $new If true, this post is to be inserted anew. If false, it is an update of an existing post.
1864 1801 * @return array A normalized representation of the post ready to be inserted into the database or sent to the WordPress API functions
1865 1802 */
1866 - function normalize_post( $new = true ) {
1803 + function normalize_post ($new = true) {
1804 + global $wpdb;
1805 +
1867 1806 $out = $this->post;
1868 1807
1869 1808 $fullPost = $out['post_title'].$out['post_content'];
1870 1809 $fullPost .= (isset($out['post_excerpt']) ? $out['post_excerpt'] : '');
@@ -1898,19 +1837,19 @@
1898 1837 // that work for MySQL but not for PHP mb_check_encoding. So instead
1899 1838 // we must rely on WordPress setting blog_charset and hope that the user
1900 1839 // has got their database encoding set up to roughly match
1901 1840 $charset = get_option('blog_charset', 'utf8');
1902 -
1841 +
1903 1842 foreach ($out as $key => $value) :
1904 1843 if (is_string($value)) :
1905 -
1906 - if ( !function_exists('mb_check_encoding') or mb_check_encoding($value, $charset)) :
1844 +
1845 + if (!function_exists('mb_check_encoding') or mb_check_encoding($value, $charset)) :
1907 1846 $out[$key] = $value;
1908 1847 else :
1909 1848 $fromCharset = mb_detect_encoding($value, mb_detect_order(), /*strict=*/ true);
1910 1849 $out[$key] = mb_convert_encoding($value, $charset, $fromCharset);
1911 1850 endif;
1912 -
1851 +
1913 1852 elseif (is_array($value)) :
1914 1853 $out[$key] = $this->db_sanitize_post_check_encoding($value);
1915 1854
1916 1855 else :
@@ -1915,25 +1854,25 @@
1915 1854
1916 1855 else :
1917 1856 $out[$key] = $value;
1918 1857 endif;
1919 -
1858 +
1920 1859 endforeach;
1921 -
1860 +
1922 1861 return $out;
1923 1862 } /* SyndicatedPost::db_sanitize_post_check_encoding () */
1924 -
1863 +
1925 1864 function db_sanitize_post ($out) {
1926 1865 global $wp_db_version;
1927 -
1866 +
1928 1867 $out = $this->db_sanitize_post_check_encoding($out);
1929 -
1868 +
1930 1869 // < 3.6. Core API, including `wp_insert_post()`, expects
1931 1870 // properly slashed data. If `wp_slash()` exists, then
1932 1871 // this is after the big change-over in how data slashing
1933 1872 // was handled.
1934 - if ( !function_exists('wp_slash')) :
1935 -
1873 + if (!function_exists('wp_slash')) :
1874 +
1936 1875 foreach ($out as $key => $value) :
1937 1876 if (is_string($value)) :
1938 1877 $out[$key] = esc_sql($value);
1939 1878 else :
@@ -1939,28 +1878,28 @@
1939 1878 else :
1940 1879 $out[$key] = $value;
1941 1880 endif;
1942 1881 endforeach;
1943 -
1882 +
1944 1883 // For revisions [@23416,@23554), core API expects
1945 1884 // unslashed data. Cf. <https://core.trac.wordpress.org/browser/trunk/wp-includes/post.php?rev=23416>
1946 1885 // NOOP for those revisions.
1947 -
1886 +
1948 1887 // In revisions @23554 to present, `wp_insert_post()`
1949 1888 // expects slashed data once again.
1950 1889 // Cf. <https://core.trac.wordpress.org/changeset/23554/trunk/wp-includes/post.php?contextall=1>
1951 1890 // But at least now we can use the wp_slash API function to do that.
1952 1891 // Hooray.
1953 -
1892 +
1954 1893 elseif ($wp_db_version >= 23524) :
1955 -
1894 +
1956 1895 $out = wp_slash($out);
1957 -
1896 +
1958 1897 endif;
1959 1898
1960 1899 return $out;
1961 1900 }
1962 -
1901 +
1963 1902 /**
1964 1903 * SyndicatedPost::validate_post_id()
1965 1904 *
1966 1905 * @param array $dbpost An array representing the post we attempted to insert or update
@@ -1970,9 +1909,9 @@
1970 1909 if (is_array($ns)) : $ns = implode('::', $ns);
1971 1910 else : $ns = (string) $ns; endif;
1972 1911
1973 1912 // This should never happen.
1974 - if ( !is_numeric($this->_wp_id) or ($this->_wp_id == 0)) :
1913 + if (!is_numeric($this->_wp_id) or ($this->_wp_id == 0)) :
1975 1914 $verb = ($is_update ? 'update existing' : 'insert new');
1976 1915 $guid = $this->guid();
1977 1916 $url = $this->permalink();
1978 1917 $feed = $this->link->uri(array('add_params' => true));
@@ -1981,9 +1920,9 @@
1981 1920 // notice if we are in debug mode.
1982 1921 $mesg = "Failed to $verb item [$guid]. WordPress API returned no valid post ID.\n"
1983 1922 ."\t\tID = ".serialize($this->_wp_id)."\n"
1984 1923 ."\t\tURL = ".MyPHP::val($url)
1985 - ."\t\tFeed = ".MyPHP::val($feed);
1924 + ."\t\tFeed = ".MyPHP::val($feed);
1986 1925
1987 1926 FeedWordPress::diagnostic('updated_feeds:errors', "WordPress API error: $mesg");
1988 1927 FeedWordPress::diagnostic('feed_items:rejected', $mesg);
1989 1928
@@ -1994,9 +1933,9 @@
1994 1933 from the feed at $feed
1995 1934
1996 1935 $ns::_wp_id
1997 1936 EOM;
1998 - FeedWordPressDiagnostic::noncritical_bug(
1937 + FeedWordPress::noncritical_bug(
1999 1938 /*message=*/ $mesg,
2000 1939 /*var =*/ array(
2001 1940 "\$this->_wp_id" => $this->_wp_id,
2002 1941 "\$dbpost" => $dbpost,
@@ -2022,22 +1961,24 @@
2022 1961 * by _wp_put_post_revision.
2023 1962 *
2024 1963 * @param int $revision_id The revision ID to fix up meta-data
2025 1964 */
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)
1965 + function fix_revision_meta ($revision_id) {
1966 + global $wpdb;
2028 1967
1968 + $post_author = (int) $this->post['post_author'];
1969 +
2029 1970 $revision_id = (int) $revision_id;
2030 -
1971 +
2031 1972 // Let's fix the author.
2032 - set_post_field( 'post_author', $this->post['post_author'], $revision_id );
2033 -
1973 + set_post_field('post_author', $this->post['post_author'], $revision_id);
1974 +
2034 1975 // 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 );
1976 + set_post_field('guid', 'http://feedwordpress.radgeek.com/?rev='.$this->update_hash(), $revision_id);
2036 1977
2037 1978 // Let's fire an event for add-ons and filters
2038 - do_action( 'syndicated_post_fix_revision_meta', $revision_id, $this );
2039 -
1979 + do_action('syndicated_post_fix_revision_meta', $revision_id, $this);
1980 +
2040 1981 } /* SyndicatedPost::fix_revision_meta () */
2041 1982
2042 1983 /**
2043 1984 * SyndicatedPost::add_terms() -- if FeedWordPress is processing an
@@ -2125,9 +2066,9 @@
2125 2066 * @param string $new_status Unused action parameter.
2126 2067 * @param string $old_status Unused action parameter.
2127 2068 * @param object $post The database record for the post just inserted.
2128 2069 */
2129 - function add_rss_meta($new_status, $old_status, $post) {
2070 + function add_rss_meta ($new_status, $old_status, $post) {
2130 2071 global $wpdb;
2131 2072 if ($new_status!='inherit') : // Bail if we are creating a revision.
2132 2073 FeedWordPress::diagnostic('syndicated_posts:meta_data', 'Adding post meta-data: {'.implode(", ", array_keys($this->post['meta'])).'}');
2133 2074
@@ -2153,19 +2094,14 @@
2153 2094 WHERE post_id='$postId' AND meta_key='$eKey'
2154 2095 ");
2155 2096
2156 2097 // Allow for either a single value or an array
2157 - if ( !is_array($values)) $values = array($values);
2098 + if (!is_array($values)) $values = array($values);
2158 2099 foreach ( $values as $value ) :
2159 2100 FeedWordPress::diagnostic('syndicated_posts:meta_data', "Adding post meta-datum to post [$postId]: [$key] = ".MyPHP::val($value, /*no newlines=*/ true));
2160 2101 add_post_meta($postId, $key, $value, /*unique=*/ false);
2161 2102 endforeach;
2162 2103 endforeach;
2163 -
2164 - if ( $result === false)
2165 - {
2166 - error_log( "delete query failed: " . $wpdb->last_error );
2167 - }
2168 2104 endif;
2169 2105 endif;
2170 2106 } /* SyndicatedPost::add_rss_meta () */
2171 2107
@@ -2194,9 +2130,9 @@
2194 2130 // as last resort
2195 2131
2196 2132 $candidates = array();
2197 2133 $candidates[] = $a['name'];
2198 - if ( !is_null($source)) : $candidates[] = $source['title']; endif;
2134 + if (!is_null($source)) : $candidates[] = $source['title']; endif;
2199 2135 $candidates[] = $this->link->name(/*fromFeed=*/ true);
2200 2136 $candidates[] = $this->link->name(/*fromFeed=*/ false);
2201 2137 if (strlen($this->link->homepage()) > 0) : $candidates[] = feedwordpress_display_url($this->link->homepage()); endif;
2202 2138 $candidates[] = feedwordpress_display_url($this->link->uri());
@@ -2205,16 +2141,15 @@
2205 2141 // Pick the first one that works from the list, screening against empty
2206 2142 // or forbidden names.
2207 2143
2208 2144 $author = NULL;
2209 - foreach ($candidates as $candidate) {
2210 - if ( !is_null($candidate)
2211 - and (strlen(trim($candidate)) > 0)
2212 - and !in_array(strtolower(trim($candidate)), $forbidden)) :
2213 - $author = $candidate;
2214 - break;
2145 + while (is_null($author) and ($candidate = each($candidates))) :
2146 + if (!is_null($candidate['value'])
2147 + and (strlen(trim($candidate['value'])) > 0)
2148 + and !in_array(strtolower(trim($candidate['value'])), $forbidden)) :
2149 + $author = $candidate['value'];
2215 2150 endif;
2216 - }
2151 + endwhile;
2217 2152
2218 2153 $email = (isset($a['email']) ? $a['email'] : NULL);
2219 2154 $authorUrl = (isset($a['uri']) ? $a['uri'] : NULL);
2220 2155
@@ -2257,9 +2192,9 @@
2257 2192
2258 2193 $nice_author = sanitize_title($author);
2259 2194 $nice_author = apply_filters('pre_user_nicename', $nice_author);
2260 2195
2261 - $reg_author = esc_sql(preg_quote($author ?: "(unknown)"));
2196 + $reg_author = esc_sql(preg_quote($author));
2262 2197 $author = esc_sql($author);
2263 2198 $email = esc_sql($email);
2264 2199 $test_email = esc_sql($test_email);
2265 2200 $authorUrl = esc_sql($authorUrl);
@@ -2273,11 +2208,9 @@
2273 2208 $author_rule = NULL;
2274 2209 endif;
2275 2210
2276 2211 // 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)) :
2212 + if (is_numeric($author_rule) and get_userdata((int) $author_rule)) :
2280 2213 $id = (int) $author_rule;
2281 2214
2282 2215 // User name is filtered out
2283 2216 elseif ('filter' == $author_rule) :
@@ -2318,11 +2251,11 @@
2318 2251 if (is_null($id)) :
2319 2252 if ($unfamiliar_author === 'create') :
2320 2253 $userdata = array();
2321 2254
2322 - #-- we need *something* for the email here or WordPress
2323 - #-- is liable to pitch a fit. So, make something up if
2324 - #-- necessary. (Ugh.)
2255 + // WordPress 3 is going to pitch a fit if we attempt to register
2256 + // more than one user account with an empty e-mail address, so we
2257 + // need *something* here. Ugh.
2325 2258 if (strlen($email) == 0 or FeedWordPress::is_null_email($email)) :
2326 2259 $url = parse_url($hostUrl);
2327 2260 $email = $nice_author.'@'.$url['host'];
2328 2261 endif;
@@ -2334,20 +2267,15 @@
2334 2267 $userdata['user_pass'] = substr(md5(uniqid(microtime())), 0, 6); // just something random to lock it up
2335 2268 $userdata['user_email'] = $email;
2336 2269 $userdata['user_url'] = $authorUrl;
2337 2270 $userdata['nickname'] = $author;
2338 -
2339 2271 $parts = preg_split('/\s+/', trim($author), 2);
2340 2272 if (isset($parts[0])) : $userdata['first_name'] = $parts[0]; endif;
2341 2273 if (isset($parts[1])) : $userdata['last_name'] = $parts[1]; endif;
2342 -
2343 2274 $userdata['display_name'] = $author;
2344 2275 $userdata['role'] = 'contributor';
2345 2276
2346 - #-- loop. Keep trying to add the user until you get it
2347 - #-- right. Or until PHP crashes, I guess.
2348 - $insanity = 0;
2349 - do {
2277 + do { // Keep trying until you get it right. Or until PHP crashes, I guess.
2350 2278 $id = wp_insert_user($userdata);
2351 2279 if (is_wp_error($id)) :
2352 2280 $codes = $id->get_error_code();
2353 2281 switch ($codes) :
@@ -2352,23 +2280,17 @@
2352 2280 $codes = $id->get_error_code();
2353 2281 switch ($codes) :
2354 2282 case 'empty_user_login' :
2355 2283 case 'existing_user_login' :
2356 - case 'invalid_username' :
2357 2284 // Add a random disambiguator
2358 2285 $userdata['user_login'] .= substr(md5(uniqid(microtime())), 0, 6);
2359 2286 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 2287 case 'user_nicename_too_long' :
2366 - // Add a limited 50 characters user_nicename based on user_login
2288 + // Add a limited 50 caracters user_nicename based on user_login
2367 2289 $userdata['user_nicename'] = mb_substr( $userdata['user_login'], 0, 50 );
2368 2290 break;
2369 2291 case 'existing_user_email' :
2370 - // Disassemble email for username, host
2292 + // No disassemble!
2371 2293 $parts = explode('@', $userdata['user_email'], 2);
2372 2294
2373 2295 // Add a random disambiguator as a gmail-style username extension
2374 2296 $parts[0] .= '+'.substr(md5(uniqid(microtime())), 0, 6);
@@ -2375,39 +2297,14 @@
2375 2297
2376 2298 // Reassemble
2377 2299 $userdata['user_email'] = $parts[0].'@'.$parts[1];
2378 2300 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 2301 endswitch;
2392 2302 endif;
2393 - $insanity = $insanity + 1;
2394 2303 } while (is_wp_error($id));
2395 -
2396 - // $id should now contain the numeric ID of a newly minted
2397 - // user account. Let's mark them as having been generated
2398 - // by FeedWordPress in the usermeta table, as per the
2399 - // suggestion of @boonebgorges, in case we need to process,
2400 - // 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 ) ) :
2304 + elseif (is_numeric($unfamiliar_author) and get_userdata((int) $unfamiliar_author)) :
2408 2305 $id = (int) $unfamiliar_author;
2409 - elseif ( $unfamiliar_author === 'default' ) :
2306 + elseif ($unfamiliar_author === 'default') :
2410 2307 $id = 1;
2411 2308 endif;
2412 2309 endif;
2413 2310 endif;