PluginProbe
FeedWordPress / 2017.1004
FeedWordPress v2017.1004
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 +356 -465 trunk2017.1004 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
@@ -11,27 +11,25 @@
11 11 * as some utility methods for extracting useful data from many
12 12 * different feed formats, which may be useful to FeedWordPress users
13 13 * who make use of feed data in PHP add-ons and filters.
14 14 *
15 - * @version 2017.1018
15 + * @version 2017.1004
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,45 +42,47 @@
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 + function __construct ($item, &$source) {
47 + global $wpdb;
48 +
49 + if ( empty($item) && 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 - $this->link = $source;
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 #####################################
@@ -298,9 +289,9 @@
298 289 * @param string $path
299 290 * @returns array of string values representing contents of matching
300 291 * elements or attributes
301 292 */
302 - public function query ($path) {
293 + function query ($path) {
303 294 $xq = new SyndicatedPostXPathQuery(array("path" => $path));
304 295
305 296 $feedChannel = array_merge(
306 297 $this->get_feed_root_element(),
@@ -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;
@@ -350,24 +341,24 @@
350 341 endforeach;
351 342 return $matches;
352 343 } /* SyndicatedPost::get_feed_channel_elements() */
353 344
354 - public function get_categories ($params = array()) {
345 + function get_categories ($params = array()) {
355 346 return $this->entry->get_categories();
356 347 }
357 -
358 - public function title ($params = array()) {
348 +
349 + function title ($params = array()) {
359 350 return $this->entry->get_title();
360 351 } /* SyndicatedPost::title () */
352 +
353 + 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,15 +395,15 @@
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 - public function excerpt () {
405 + function excerpt () {
415 406 # Identify and sanitize excerpt: atom:summary, or rss:description
416 407 $excerpt = $this->entry->get_description();
417 408
418 409 # Many RSS feeds use rss:description, inadvisably, to
@@ -441,23 +432,17 @@
441 432
442 433 return $excerpt;
443 434 } /* SyndicatedPost::excerpt() */
444 435
445 - /**
446 - * SyndicatedPost::permalink: returns the permalink for the post, as provided by the
447 - * source feed.
448 - *
449 - * @return string The URL of the original permalink for this syndicated post
450 - */
451 - public function permalink () {
436 + function permalink () {
452 437 // Handles explicit <link> elements and also RSS 2.0 cases with
453 438 // <guid isPermaLink="true">, etc. Hooray!
454 439 $permalink = $this->entry->get_link();
455 440 return $permalink;
456 - } /* SyndicatedPost::permalink () */
441 + }
457 442
458 - public function created ($params = array()) {
459 - $unfiltered = false; // $default = NULL; // seems to be unused on this function (gwyneth 20230916)
443 + function created ($params = array()) {
444 + $unfiltered = false; $default = NULL;
460 445 extract($params);
461 446
462 447 $date = '';
463 448 if (isset($this->item['dc']['created'])) :
@@ -468,18 +453,18 @@
468 453 $date = $this->item['created'];
469 454 endif;
470 455
471 456 $time = new FeedTime($date);
472 - $tstamp = $time->timestamp();
473 - if ( ! $unfiltered) :
474 - apply_filters('syndicated_item_created', $tstamp, $this);
457 + $ts = $time->timestamp();
458 + if (!$unfiltered) :
459 + apply_filters('syndicated_item_created', $ts, $this);
475 460 endif;
476 - return $tstamp;
461 + return $ts;
477 462 } /* SyndicatedPost::created() */
478 463
479 - public function published ($params = array(), $default = NULL) {
464 + function published ($params = array(), $default = NULL) {
480 465 $fallback = true; $unfiltered = false;
481 - if ( !is_array($params)) : // Old style
466 + if (!is_array($params)) : // Old style
482 467 $fallback = $params;
483 468 else : // New style
484 469 extract($params);
485 470 endif;
@@ -484,9 +469,9 @@
484 469 extract($params);
485 470 endif;
486 471
487 472 $date = '';
488 - $tstamp = null;
473 + $ts = null;
489 474
490 475 # RSS is a fucking mess. Figure out whether we have a date in
491 476 # <dc:date>, <issued>, <pubDate>, etc., and get it into Unix
492 477 # epoch format for reformatting. If we can't find anything,
@@ -504,31 +489,31 @@
504 489 endif;
505 490
506 491 if (strlen($date) > 0) :
507 492 $time = new FeedTime($date);
508 - $tstamp = $time->timestamp();
493 + $ts = $time->timestamp();
509 494 elseif ($fallback) : // Fall back to <updated> / <modified> if present
510 - $tstamp = $this->updated(/*fallback=*/ false, /*default=*/ $default);
495 + $ts = $this->updated(/*fallback=*/ false, /*default=*/ $default);
511 496 endif;
512 497
513 498 # If everything failed, then default to the current time.
514 - if (is_null($tstamp)) :
499 + if (is_null($ts)) :
515 500 if (-1 == $default) :
516 - $tstamp = time();
501 + $ts = time();
517 502 else :
518 - $tstamp = $default;
503 + $ts = $default;
519 504 endif;
520 505 endif;
521 506
522 - if ( ! $unfiltered) :
523 - $tstamp = apply_filters('syndicated_item_published', $tstamp, $this);
507 + if (!$unfiltered) :
508 + $ts = apply_filters('syndicated_item_published', $ts, $this);
524 509 endif;
525 - return $tstamp;
510 + return $ts;
526 511 } /* SyndicatedPost::published() */
527 512
528 - public function updated ($params = array(), $default = -1) {
513 + function updated ($params = array(), $default = -1) {
529 514 $fallback = true; $unfiltered = false;
530 - if ( !is_array($params)) : // Old style
515 + if (!is_array($params)) : // Old style
531 516 $fallback = $params;
532 517 else : // New style
533 518 extract($params);
534 519 endif;
@@ -533,9 +518,9 @@
533 518 extract($params);
534 519 endif;
535 520
536 521 $date = '';
537 - $tstamp = null;
522 + $ts = null;
538 523
539 524 # As far as I know, only dcterms and Atom have reliable ways to
540 525 # specify when something was *modified* last. If neither is
541 526 # available, then we'll try to get the time of publication.
@@ -550,26 +535,26 @@
550 535 endif;
551 536
552 537 if (strlen($date) > 0) :
553 538 $time = new FeedTime($date);
554 - $tstamp = $time->timestamp();
539 + $ts = $time->timestamp();
555 540 elseif ($fallback) : // Fall back to issued / dc:date
556 - $tstamp = $this->published(/*fallback=*/ false, /*default=*/ $default);
541 + $ts = $this->published(/*fallback=*/ false, /*default=*/ $default);
557 542 endif;
558 543
559 544 # If everything failed, then default to the current time.
560 - if (is_null($tstamp)) :
545 + if (is_null($ts)) :
561 546 if (-1 == $default) :
562 - $tstamp = time();
547 + $ts = time();
563 548 else :
564 - $tstamp = $default;
549 + $ts = $default;
565 550 endif;
566 551 endif;
567 552
568 - if ( ! $unfiltered) :
569 - $tstamp = apply_filters('syndicated_item_updated', $tstamp, $this);
553 + if (!$unfiltered) :
554 + apply_filters('syndicated_item_updated', $ts, $this);
570 555 endif;
571 - return $tstamp;
556 + return $ts;
572 557 } /* SyndicatedPost::updated() */
573 558
574 559 var $_hashes = array();
575 560 function stored_hashes ($id = NULL) {
@@ -576,9 +561,9 @@
576 561 if (is_null($id)) :
577 562 $id = $this->wp_id();
578 563 endif;
579 564
580 - if ( !isset($this->_hashes[$id])) :
565 + if (!isset($this->_hashes[$id])) :
581 566 $this->_hashes[$id] = get_post_custom_values(
582 567 'syndication_item_hash', $id
583 568 );
584 569 if (is_null($this->_hashes[$id])) :
@@ -618,11 +603,10 @@
618 603 * @uses home_url()
619 604 * @uses apply_filters()
620 605 */
621 606 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);
607 + $url = trailingslashit(home_url(/*path=*/ '', /*scheme=*/ 'http'));
608 + return apply_filters('syndicated_item_guid_normalized_prefix', $url . '?guid=');
625 609 } /* SyndicatedPost::normalize_guid_prefix() */
626 610
627 611 static function normalize_guid ($guid) {
628 612 $guid = trim($guid);
@@ -631,16 +615,15 @@
631 615 elseif ((strlen(esc_url($guid)) == 0) or (esc_url($guid) != $guid)) :
632 616 $guid = SyndicatedPost::normalize_guid_prefix().md5($guid);
633 617 endif;
634 618 $guid = trim($guid);
635 -
619 +
636 620 return $guid;
637 621 } /* SyndicatedPost::normalize_guid() */
638 622
639 623 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);
624 + $url = trailingslashit(home_url(/*path=*/ '', /*scheme=*/ 'https'));
625 + return apply_filters('syndicated_item_guid_normalized_prefix', $url . '?guid=');
643 626 }
644 627 static function alternative_guid ($guid) {
645 628 $guid = trim($guid);
646 629 if (preg_match('/^[0-9a-z]{32}$/i', $guid)) : // MD5
@@ -648,13 +631,13 @@
648 631 elseif ((strlen(esc_url($guid)) == 0) or (esc_url($guid) != $guid)) :
649 632 $guid = SyndicatedPost::alternative_guid_prefix().md5($guid);
650 633 endif;
651 634 $guid = trim($guid);
652 -
635 +
653 636 return $guid;
654 637 } /* SyndicatedPost::normalize_guid() */
655 638
656 - public function guid () {
639 + function guid () {
657 640 $guid = null;
658 641 if (isset($this->item['id'])): // Atom 0.3 / 1.0
659 642 $guid = $this->item['id'];
660 643 elseif (isset($this->item['atom']['id'])) : // Namespaced Atom
@@ -684,9 +667,9 @@
684 667 // gets clipped by MySQL, uniqueness tests will fail
685 668 // forever after and the post will be endlessly
686 669 // reduplicated. So, instead, Guids Of A Certain Length
687 670 // are hashed down into a nice, manageable tag: URI.
688 - if ( !is_null($original_guid)) :
671 + if (!is_null($original_guid)) :
689 672 $guid .= ',2010-12-03:id.'.md5($original_guid);
690 673
691 674 // If we have a date of creation, then we can use that
692 675 // to uniquely identify the item. (On the other hand, if
@@ -692,9 +675,9 @@
692 675 // to uniquely identify the item. (On the other hand, if
693 676 // the feed producer was consicentious enough to
694 677 // generate dates of creation, she probably also was
695 678 // conscientious enough to generate unique identifiers.)
696 - elseif ( !is_null($this->created())) :
679 + elseif (!is_null($this->created())) :
697 680 $guid .= '://post.'.date('YmdHis', $this->created());
698 681
699 682 // Otherwise, use both the URI of the item, *and* the
700 683 // item's title. We have to use both because titles are
@@ -705,19 +688,19 @@
705 688 // this is about the best we can do.
706 689 else :
707 690 $link = $this->permalink();
708 691 if (is_null($link)) : $link = $this->link->uri(); endif;
709 - $guid .= '://'.md5($link.'/'.$this->title());
692 + $guid .= '://'.md5($link.'/'.$this->item['title']);
710 693 endif;
711 694 endif;
712 695 return $guid;
713 696 } /* SyndicatedPost::guid() */
714 697
715 - public function author () {
698 + function author () {
716 699 $author = array ();
717 700
718 701 $aa = $this->entry->get_authors();
719 - if (is_countable($aa) and count($aa) > 0) :
702 + if (count($aa) > 0) :
720 703 $a = reset($aa);
721 704
722 705 $author = array(
723 706 'name' => $a->get_name(),
@@ -745,9 +728,9 @@
745 728 // e-mail address, but lots of people don't use
746 729 // it that way. So let's make of it what we can.
747 730 $author = parse_email_with_realname($this->item['author']);
748 731
749 - if ( !isset($author['name'])) :
732 + if (!isset($author['name'])) :
750 733 if (isset($author['email'])) :
751 734 $author['name'] = $author['email'];
752 735 else :
753 736 $author['name'] = $this->feed->channel['title'];
@@ -755,9 +738,9 @@
755 738 endif;
756 739 endif;
757 740 endif;
758 741
759 - if ( !isset($author['name']) or is_null($author['name'])) :
742 + if (!isset($author['name']) or is_null($author['name'])) :
760 743 // Nothing found. Try some crappy defaults.
761 744 if ($this->link->name()) :
762 745 $author['name'] = $this->link->name();
763 746 else :
@@ -797,9 +780,9 @@
797 780 * @since 2016.0331
798 781 * @return array of lists, each element has the taxonomy for a key ('category', 'post_tag', etc.),
799 782 * and a list of term codes (either alphanumeric names, or ID numbers encoded in a format that
800 783 * SyndicatedLink::category_ids() can understand) within that taxonomy
801 - *
784 + *
802 785 */
803 786 public function get_terms_from_settings () {
804 787 // Categories: start with default categories, if any.
805 788 $cats = array();
@@ -836,9 +819,9 @@
836 819
837 820 foreach ($taxonomies as $tax) :
838 821 // category and tag settings have already previously been handled
839 822 // but if this is from another taxonomy, then...
840 - if ( !isset($specials[$tax])) :
823 + if (!isset($specials[$tax])) :
841 824 $terms = array();
842 825
843 826 // See if we should get the globals
844 827 if ('no' != $this->link->setting("add/$tax", NULL, 'yes')) :
@@ -855,12 +838,12 @@
855 838 // That's all, folks.
856 839 $preset_terms[$tax] = $terms;
857 840 endif;
858 841 endforeach;
859 -
842 +
860 843 return $preset_terms;
861 844 } /* SyndicatedPost::get_terms_from_settings () */
862 -
845 +
863 846 /**
864 847 * SyndicatedPost::get_terms_from_feeds(): Return an array of terms to associate with the incoming
865 848 * post based on the contents of the subscribed feed (atom:category and rss:category elements, dc:subject
866 849 * elements, tags embedded using microformats in the post content, etc.)
@@ -868,15 +851,15 @@
868 851 * @since 2016.0331
869 852 * @return array of lists, each element has the taxonomy for a key ('category', 'post_tag', etc.),
870 853 * and a list of alphanumeric term names
871 854 */
872 - public function get_terms_from_feeds () {
855 + public function get_terms_from_feeds () {
873 856 // Now add categories from the post, if we have 'em
874 857 $cats = array();
875 858 $post_cats = $this->entry->get_categories();
876 859 if (is_array($post_cats)) : foreach ($post_cats as $cat) :
877 860 $cat_name = $cat->get_term();
878 - if ( ! $cat_name) : $cat_name = $cat->get_label(); endif;
861 + if (!$cat_name) : $cat_name = $cat->get_label(); endif;
879 862
880 863 if ($this->link->setting('cat_split', NULL, NULL)) :
881 864 $pcre = "\007".$this->feedmeta['cat_split']."\007";
882 865 $cats = array_merge(
@@ -913,9 +896,9 @@
913 896 $tags = array();
914 897 $content = $this->content();
915 898 $pattern = FeedWordPressHTML::tagWithAttributeRegex('a', 'rel', 'tag');
916 899 preg_match_all($pattern, $content, $refs, PREG_SET_ORDER);
917 - if (is_countable($refs) and count($refs) > 0) :
900 + if (count($refs) > 0) :
918 901 foreach ($refs as $ref) :
919 902 $tag = FeedWordPressHTML::tagWithAttributeMatch($ref);
920 903 $tags[] = $tag['content'];
921 904 endforeach;
@@ -985,21 +968,21 @@
985 968 * that will be returned to only those with a MIME type which
986 969 * matches this regular expression.
987 970 * @return array
988 971 */
989 - function enclosures( $type = '/.*/' ) {
972 + function enclosures ($type = '/.*/') {
990 973 $enclosures = array();
991 974
992 - if ( isset( $this->item['enclosure#'] ) ) :
975 + if (isset($this->item['enclosure#'])) :
993 976 // 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)
977 + for ($i = 1; $i <= $this->item['enclosure#']; $i++) :
978 + $eid = (($i > 1) ? "#{$id}" : "");
996 979
997 980 // Does it match the type we want?
998 - if ( preg_match($type, $this->item["enclosure{$eid}@type"] ) ) :
981 + if (preg_match($type, $this->item["enclosure{$eid}@type"])) :
999 982 $enclosures[] = array(
1000 - "url" => $this->item["enclosure{$eid}@url"],
1001 - "type" => $this->item["enclosure{$eid}@type"],
983 + "url" => $this->item["enclosure{$eid}@url"],
984 + "type" => $this->item["enclosure{$eid}@type"],
1002 985 "length" => $this->item["enclosure{$eid}@length"],
1003 986 );
1004 987 endif;
1005 988 endfor;
@@ -1028,9 +1011,9 @@
1028 1011 $ret['id'] = $id_tags[0]['data'];
1029 1012 endif;
1030 1013 endif;
1031 1014
1032 - if ( !is_null($what) and is_scalar($what)) :
1015 + if (!is_null($what) and is_scalar($what)) :
1033 1016 $ret = $ret[$what];
1034 1017 endif;
1035 1018 return $ret;
1036 1019 }
@@ -1235,11 +1218,12 @@
1235 1218 * 1 = post already syndicated, but needs to be updated to latest
1236 1219 * 2 = post has not yet been syndicated; needs to be created
1237 1220 */
1238 1221 function freshness ($format = 'number') {
1222 + global $wpdb;
1239 1223
1240 1224 if ($this->filtered()) : // This should never happen.
1241 - FeedWordPressDiagnostic::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1225 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1242 1226 endif;
1243 1227
1244 1228 if (is_null($this->_freshness)) : // Not yet checked and cached.
1245 1229 $guid = $this->post['guid'];
@@ -1247,17 +1231,15 @@
1247 1231
1248 1232 $q = new WP_Query(array(
1249 1233 'fields' => '_synfresh', // id, guid, post_modified_gmt
1250 1234 'ignore_sticky_posts' => true,
1251 - 'guid' => $eguid, // it's always better to use the escaped version, right? (gwyneth 20230915)
1235 + 'guid' => $guid,
1252 1236 ));
1253 1237
1254 1238 $old_post = NULL;
1255 1239 if ($q->have_posts()) :
1256 1240 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;
1241 + $old_post = $q->post;
1260 1242 endwhile;
1261 1243 endif;
1262 1244
1263 1245 if (is_null($old_post)) : // No post with this guid
@@ -1268,23 +1250,23 @@
1268 1250 // Presume there is nothing new until we find
1269 1251 // something new.
1270 1252 $updated = false;
1271 1253 $live = false;
1272 -
1254 +
1273 1255 // Pull the list of existing revisions to get
1274 1256 // timestamps.
1275 1257 $revisions = wp_get_post_revisions($old_post->ID);
1276 1258 foreach ($revisions as $rev) :
1277 - $revisions_ts[] = mysql2date('G', $rev->post_modified_gmt);
1259 + $revisions_ts[] = mysql2date('G', $rev->post_modified_gmt);
1278 1260 endforeach;
1279 1261
1280 1262 $revisions_ts[] = mysql2date('G', $old_post->post_modified_gmt);
1281 1263 $last_rev_ts = end($revisions_ts);
1282 1264 $updated_ts = $this->updated(/*fallback=*/ true, /*default=*/ NULL);
1283 -
1265 +
1284 1266 // If we have an explicit updated timestamp,
1285 1267 // check that against existing stamps.
1286 - if ( !is_null($updated_ts)) :
1268 + if (!is_null($updated_ts)) :
1287 1269 $updated = !in_array($updated_ts, $revisions_ts);
1288 1270
1289 1271 // If this a newer revision, make it go
1290 1272 // live. If an older one, just record
@@ -1292,9 +1274,9 @@
1292 1274 $live = ($updated and ($updated_ts > $last_rev_ts));
1293 1275 endif;
1294 1276
1295 1277 // This is a revision we haven't seen before, judging by the date.
1296 -
1278 +
1297 1279 $updatedReason = NULL;
1298 1280 if ($updated) :
1299 1281 $updatedReason = preg_replace(
1300 1282 "/\s+/", " ",
@@ -1310,15 +1292,15 @@
1310 1292 else :
1311 1293 // Or the hash...
1312 1294 $hash = $this->update_hash();
1313 1295 $seen = $this->stored_hashes($old_post->ID);
1314 - if (is_countable($seen) and count($seen) > 0) :
1296 + if (count($seen) > 0) :
1315 1297 $updated = !in_array($hash, $seen); // Not seen yet?
1316 1298 else :
1317 1299 $updated = true; // Can't find syndication meta-data
1318 1300 endif;
1319 1301
1320 - if ($updated and FeedWordPressDiagnostic::is_on('feed_items:freshness:reasons')) :
1302 + if ($updated and FeedWordPress::diagnostic_on('feed_items:freshness:reasons')) :
1321 1303 // In the absence of definitive
1322 1304 // timestamp information, we
1323 1305 // just have to assume that a
1324 1306 // hash we haven't seen before
@@ -1336,11 +1318,11 @@
1336 1318
1337 1319 $frozen = false;
1338 1320 if ($updated) : // Ignore if the post is frozen
1339 1321 $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));
1322 + if (!$frozen) :
1323 + $frozen_values = get_post_custom_values('_syndication_freeze_updates', $old_post->ID);
1324 + $frozen = (count($frozen_values) > 0 and 'yes' == $frozen_values[0]);
1343 1325
1344 1326 if ($frozen) :
1345 1327 $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A UPDATE LOCK ON THIS POST, EVEN THOUGH IT '.$updatedReason;
1346 1328 endif;
@@ -1347,13 +1329,13 @@
1347 1329 else :
1348 1330 $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A FEEDWORDPRESS UPDATE LOCK, EVEN THOUGH IT '.$updatedReason;
1349 1331 endif;
1350 1332 endif;
1351 - $live = ($live and ! $frozen);
1333 + $live = ($live and !$frozen);
1352 1334
1353 1335 if ($updated) :
1354 1336 FeedWordPress::diagnostic('feed_items:freshness', 'Item ['.$guid.'] "'.$this->entry->get_title().'" is an update of an existing post.');
1355 - if ( !is_null($updatedReason)) :
1337 + if (!is_null($updatedReason)) :
1356 1338 $updatedReason = preg_replace('/\s+/', ' ', $updatedReason);
1357 1339 FeedWordPress::diagnostic('feed_items:freshness:reasons', 'Item ['.$guid.'] "'.$this->entry->get_title().'" '.$updatedReason);
1358 1340 endif;
1359 1341
@@ -1374,9 +1356,9 @@
1374 1356 $this->_wp_id = $old_post->ID;
1375 1357 endif;
1376 1358 endif;
1377 1359 endif;
1378 -
1360 +
1379 1361 switch ($format) :
1380 1362 case 'status' :
1381 1363 switch ($this->_freshness) :
1382 1364 case -1:
@@ -1397,17 +1379,17 @@
1397 1379 case 'number' :
1398 1380 default :
1399 1381 $ret = $this->_freshness;
1400 1382 endswitch;
1401 -
1402 -
1383 +
1384 +
1403 1385 return $ret;
1404 1386 } /* SyndicatedPost::freshness () */
1405 -
1387 +
1406 1388 function has_fresh_content () {
1407 1389 return ( ! $this->filtered() and $this->freshness() != 0 );
1408 1390 } /* SyndicatedPost::has_fresh_content () */
1409 -
1391 +
1410 1392 function this_revision_needs_original_post ($freshness = NULL) {
1411 1393 if (is_null($freshness)) :
1412 1394 $freshness = $this->freshness();
1413 1395 endif;
@@ -1412,9 +1394,9 @@
1412 1394 $freshness = $this->freshness();
1413 1395 endif;
1414 1396 return ( $freshness >= 2 );
1415 1397 }
1416 -
1398 +
1417 1399 function this_revision_is_current ($freshness = NULL) {
1418 1400 if (is_null($freshness)) :
1419 1401 $freshness = $this->freshness();
1420 1402 endif;
@@ -1419,13 +1401,13 @@
1419 1401 $freshness = $this->freshness();
1420 1402 endif;
1421 1403 return ( $freshness >= 1 );
1422 1404 } /* SyndicatedPost::this_revision_is_current () */
1423 -
1405 +
1424 1406 function fresh_content_is_update () {
1425 1407 return ($this->freshness() < 2);
1426 1408 } /* SyndicatedPost::fresh_content_is_update () */
1427 -
1409 +
1428 1410 function fresh_storage_diagnostic () {
1429 1411 $ret = NULL;
1430 1412 switch ($this->freshness()) :
1431 1413 case -1 :
@@ -1436,13 +1418,13 @@
1436 1418 break;
1437 1419 case 2 :
1438 1420 default :
1439 1421 $ret = 'Inserting new post "'.$this->post['post_title'].'"';
1440 - break;
1422 + break;
1441 1423 endswitch;
1442 1424 return $ret;
1443 1425 } /* SyndicatedPost::fresh_storage_diagnostic() */
1444 -
1426 +
1445 1427 function fresh_storage_hook () {
1446 1428 $ret = NULL;
1447 1429 switch ($this->freshness()) :
1448 1430 case -1 :
@@ -1455,9 +1437,9 @@
1455 1437 break;
1456 1438 endswitch;
1457 1439 return $ret;
1458 1440 } /* SyndicatedPost::fresh_storage_hook () */
1459 -
1441 +
1460 1442 #################################################
1461 1443 #### INTERNAL STORAGE AND MANAGEMENT METHODS ####
1462 1444 #################################################
1463 1445
@@ -1462,9 +1444,9 @@
1462 1444 #################################################
1463 1445
1464 1446 function wp_id () {
1465 1447 if ($this->filtered()) : // This should never happen.
1466 - FeedWordPressDiagnostic::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1448 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1467 1449 endif;
1468 1450
1469 1451 if (is_null($this->_wp_id) and is_null($this->_freshness)) :
1470 1452 $fresh = $this->freshness(); // sets WP DB id in the process
@@ -1471,165 +1453,124 @@
1471 1453 endif;
1472 1454 return $this->_wp_id;
1473 1455 }
1474 1456
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 - );
1457 + public function store () {
1458 + global $wpdb;
1495 1459
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;
1460 + if ($this->filtered()) : // This should never happen.
1461 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1499 1462 endif;
1500 - } /* SyndicatedPost::secure_author_id() */
1501 1463
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);
1464 + $freshness = $this->freshness();
1465 + if ($this->has_fresh_content()) :
1466 + # -- Look up, or create, numeric ID for author
1467 + $this->post['post_author'] = $this->author_id (
1468 + $this->link->setting('unfamiliar author', 'unfamiliar_author', 'create')
1469 + );
1524 1470
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'];
1471 + if (is_null($this->post['post_author'])) :
1472 + FeedWordPress::diagnostic('feed_items:rejected', 'Filtered out item ['.$this->guid().'] without syndication: no author available');
1473 + $this->post = NULL;
1474 + endif;
1475 + endif;
1530 1476
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']);
1477 + // We have to check again in case post has been filtered during
1478 + // the author_id lookup
1479 + if ($this->has_fresh_content()) :
1480 + $mapping = apply_filters('syndicated_post_terms_mapping', array(
1481 + 'category' => array('abbr' => 'cats', 'unfamiliar' => 'category', 'domain' => array('category', 'post_tag')),
1482 + 'post_tag' => array('abbr' => 'tags', 'unfamiliar' => 'post_tag', 'domain' => array('post_tag')),
1483 + ), $this);
1534 1484
1535 - // Eliminate dummy variables
1536 - $taxonomies = array_filter( $taxonomies, 'remove_dummy_zero' );
1485 + $termSet = array(); $valid = null;
1486 + foreach ($this->feed_terms as $what => $anTerms) :
1487 + // Default to using the inclusive procedures (for cats) rather than exclusive (for inline tags)
1488 + $taxes = (isset($mapping[$what]) ? $mapping[$what] : $mapping['category']);
1489 + $unfamiliar = $taxes['unfamiliar'];
1490 +
1491 + if (!is_null($this->post)) : // Not filtered out yet
1492 + # -- Look up, or create, numeric ID for categories
1493 + $taxonomies = $this->link->setting("match/".$taxes['abbr'], 'match_'.$taxes['abbr'], $taxes['domain']);
1537 1494
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);
1495 + // Eliminate dummy variables
1496 + $taxonomies = array_filter($taxonomies, 'remove_dummy_zero');
1541 1497
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 - );
1498 + // Allow FWP add-on filters to control the taxonomies we use to search for a term
1499 + $taxonomies = apply_filters("syndicated_post_terms_match", $taxonomies, $what, $this);
1500 + $taxonomies = apply_filters("syndicated_post_terms_match_${what}", $taxonomies, $this);
1552 1501
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 - );
1502 + // Allow FWP add-on filters to control with greater precision what happens on unmatched
1503 + $unmatched = apply_filters("syndicated_post_terms_unfamiliar",
1504 + $this->link->setting(
1505 + "unfamiliar {$unfamiliar}",
1506 + "unfamiliar_{$unfamiliar}",
1507 + 'create:'.$unfamiliar
1508 + ),
1509 + $what,
1510 + $this
1511 + );
1562 1512
1563 - if (is_null($terms) or is_null($termSet)) :
1564 - // filtered out -- no matches
1565 - else :
1566 - $valid = true;
1513 + $terms = $this->category_ids (
1514 + $anTerms,
1515 + $unmatched,
1516 + /*taxonomies=*/ $taxonomies,
1517 + array(
1518 + 'singleton' => false, // I don't like surprises
1519 + 'filters' => true,
1520 + )
1521 + );
1567 1522
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;
1523 + if (is_null($terms) or is_null($termSet)) :
1524 + // filtered out -- no matches
1525 + else :
1526 + $valid = true;
1578 1527
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();
1528 + // filter mode off, or at least one match
1529 + foreach ($terms as $tax => $term_ids) :
1530 + if (!isset($termSet[$tax])) :
1531 + $termSet[$tax] = array();
1532 + endif;
1533 + $termSet[$tax] = array_merge($termSet[$tax], $term_ids);
1534 + endforeach;
1535 + endif;
1586 1536 endif;
1587 - $this->post['tax_input'][$tax] = array_merge(
1588 - $this->post['tax_input'][$tax],
1589 - $term_ids
1590 - );
1591 1537 endforeach;
1592 1538
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;
1539 + if (is_null($valid)) : // Plonked
1540 + $this->post = NULL;
1541 + else : // We can proceed
1542 + $this->post['tax_input'] = array();
1543 + foreach ($termSet as $tax => $term_ids) :
1544 + if (!isset($this->post['tax_input'][$tax])) :
1545 + $this->post['tax_input'][$tax] = array();
1546 + endif;
1547 + $this->post['tax_input'][$tax] = array_merge(
1548 + $this->post['tax_input'][$tax],
1549 + $term_ids
1550 + );
1551 + endforeach;
1598 1552
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() */
1553 + // Now let's add on the feed and global presets
1554 + foreach ($this->preset_terms as $tax => $term_ids) :
1555 + if (!isset($this->post['tax_input'][$tax])) :
1556 + $this->post['tax_input'][$tax] = array();
1557 + endif;
1612 1558
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__);
1559 + $this->post['tax_input'][$tax] = array_merge (
1560 + $this->post['tax_input'][$tax],
1561 + $this->category_ids (
1562 + /*terms=*/ $term_ids,
1563 + /*unfamiliar=*/ 'create:'.$tax, // These are presets; for those added in a tagbox editor, the tag may not yet exist
1564 + /*taxonomies=*/ array($tax),
1565 + array(
1566 + 'singleton' => true,
1567 + ))
1568 + );
1569 + endforeach;
1570 + endif;
1621 1571 endif;
1622 1572
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 1573 // We have to check again in case the post has been filtered
1633 1574 // during the category/tags/taxonomy terms lookup
1634 1575 if ($this->has_fresh_content()) :
1635 1576 // Filter some individual fields
@@ -1640,9 +1581,9 @@
1640 1581 $post_name = (is_null($this->_wp_post) ? NULL : $this->_wp_post->post_name);
1641 1582
1642 1583 // Allow filters to set post slug. Props niska.
1643 1584 $post_name = apply_filters('syndicated_post_slug', $post_name, $this);
1644 - if ( !empty($post_name)) :
1585 + if (!empty($post_name)) :
1645 1586 $this->post['post_name'] = $post_name;
1646 1587 endif;
1647 1588
1648 1589 $this->post = apply_filters('syndicated_post', $this->post, $this);
@@ -1661,29 +1602,29 @@
1661 1602 /*callback=*/ array($this, 'add_rss_meta'),
1662 1603 /*priority=*/ -10000, /* very early */
1663 1604 /*arguments=*/ 3
1664 1605 );
1665 -
1606 +
1666 1607 $ret = false;
1667 1608 if ($this->has_fresh_content()) :
1668 1609 $diag = $this->fresh_storage_diagnostic();
1669 - if ( !is_null($diag)) :
1610 + if (!is_null($diag)) :
1670 1611 FeedWordPress::diagnostic('syndicated_posts', $diag);
1671 1612 endif;
1672 -
1613 +
1673 1614 $this->insert_post(/*update=*/ $this->fresh_content_is_update(), $this->freshness());
1674 1615
1675 1616 $hook = $this->fresh_storage_hook();
1676 - if ( !is_null($hook)) :
1617 + if (!is_null($hook)) :
1677 1618 do_action($hook, $this->wp_id(), $this);
1678 1619 endif;
1679 -
1620 +
1680 1621 $ret = $this->freshness('status');
1681 1622 endif;
1682 1623
1683 1624 // If this is a legit, non-filtered post, tag it as found on the
1684 1625 // feed regardless of fresh or stale status
1685 - if ( ! $this->filtered()) :
1626 + if (!$this->filtered()) :
1686 1627 $key = '_feedwordpress_retire_me_' . $this->link->id;
1687 1628 delete_post_meta($this->wp_id(), $key);
1688 1629
1689 1630 $status = get_post_field('post_status', $this->wp_id());
@@ -1709,13 +1650,15 @@
1709 1650 return $ret;
1710 1651 } /* function SyndicatedPost::store () */
1711 1652
1712 1653 function insert_post ($update = false, $freshness = 2) {
1654 + global $wpdb;
1655 +
1713 1656 $dbpost = $this->normalize_post(/*new=*/ true);
1714 1657
1715 1658 $ret = null;
1716 1659
1717 - if ( !is_null($dbpost)) :
1660 + if (!is_null($dbpost)) :
1718 1661 $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks
1719 1662
1720 1663 // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data
1721 1664 add_action('_wp_put_post_revision', array($this, 'fix_revision_meta'));
@@ -1769,28 +1712,28 @@
1769 1712 // the basic post record before we do anything else.
1770 1713 if ($this->this_revision_needs_original_post()) :
1771 1714 // *sigh*, for handling inconsistent slash expectations < 3.6
1772 1715 $sdbpost = $this->db_sanitize_post($dbpost);
1773 -
1716 +
1774 1717 // Go ahead and insert the first post record to
1775 1718 // anchor the revision history.
1776 1719
1777 1720 $this->_wp_id = wp_insert_post($sdbpost, /*return wp_error=*/ true);
1778 -
1721 +
1779 1722 $dbpost['ID'] = $this->_wp_id;
1780 1723 endif;
1781 -
1724 +
1782 1725 // Sanity check: if the attempt to insert post
1783 1726 // returned an error, then feeding that error
1784 1727 // object in to _wp_put_post_revision() would
1785 1728 // cause a fatal error. Better to break out.
1786 - if ( !is_wp_error($this->_wp_id)) :
1729 + if (!is_wp_error($this->_wp_id)) :
1787 1730 // Now that we've made sure the original exists, insert
1788 1731 // this version here as a revision.
1789 1732 $revision_id = _wp_put_post_revision($dbpost, /*autosave=*/ false);
1790 1733
1791 - if ( ! $this->this_revision_needs_original_post()) :
1792 -
1734 + if (!$this->this_revision_needs_original_post()) :
1735 +
1793 1736 if ($this->this_revision_is_current()) :
1794 1737
1795 1738 wp_restore_post_revision($revision_id);
1796 1739
@@ -1830,30 +1773,18 @@
1830 1773 add_filter('content_save_pre', $filter);
1831 1774 endforeach;
1832 1775
1833 1776 $this->validate_post_id($dbpost, $update, array(__CLASS__, __FUNCTION__));
1834 -
1777 +
1835 1778 $ret = $this->_wp_id;
1836 1779 endif;
1837 1780 return $ret;
1838 1781 } /* function SyndicatedPost::insert_post () */
1839 1782
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 1783 function insert_new () {
1847 1784 $this->insert_post(/*update=*/ false, 1);
1848 1785 } /* SyndicatedPost::insert_new() */
1849 1786
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 1787 function update_existing () {
1857 1788 $this->insert_post(/*update=*/ true, 2);
1858 1789 } /* SyndicatedPost::update_existing() */
1859 1790
@@ -1859,12 +1790,14 @@
1859 1790
1860 1791 /**
1861 1792 * SyndicatedPost::normalize_post()
1862 1793 *
1863 - * @param bool $new If true, this post is to be inserted anew. If false, it is an update of an existing post. (Unused)
1794 + * @param bool $new If true, this post is to be inserted anew. If false, it is an update of an existing post.
1864 1795 * @return array A normalized representation of the post ready to be inserted into the database or sent to the WordPress API functions
1865 1796 */
1866 - function normalize_post( $new = true ) {
1797 + function normalize_post ($new = true) {
1798 + global $wpdb;
1799 +
1867 1800 $out = $this->post;
1868 1801
1869 1802 $fullPost = $out['post_title'].$out['post_content'];
1870 1803 $fullPost .= (isset($out['post_excerpt']) ? $out['post_excerpt'] : '');
@@ -1898,19 +1831,19 @@
1898 1831 // that work for MySQL but not for PHP mb_check_encoding. So instead
1899 1832 // we must rely on WordPress setting blog_charset and hope that the user
1900 1833 // has got their database encoding set up to roughly match
1901 1834 $charset = get_option('blog_charset', 'utf8');
1902 -
1835 +
1903 1836 foreach ($out as $key => $value) :
1904 1837 if (is_string($value)) :
1905 -
1906 - if ( !function_exists('mb_check_encoding') or mb_check_encoding($value, $charset)) :
1838 +
1839 + if (!function_exists('mb_check_encoding') or mb_check_encoding($value, $charset)) :
1907 1840 $out[$key] = $value;
1908 1841 else :
1909 1842 $fromCharset = mb_detect_encoding($value, mb_detect_order(), /*strict=*/ true);
1910 1843 $out[$key] = mb_convert_encoding($value, $charset, $fromCharset);
1911 1844 endif;
1912 -
1845 +
1913 1846 elseif (is_array($value)) :
1914 1847 $out[$key] = $this->db_sanitize_post_check_encoding($value);
1915 1848
1916 1849 else :
@@ -1915,25 +1848,25 @@
1915 1848
1916 1849 else :
1917 1850 $out[$key] = $value;
1918 1851 endif;
1919 -
1852 +
1920 1853 endforeach;
1921 -
1854 +
1922 1855 return $out;
1923 1856 } /* SyndicatedPost::db_sanitize_post_check_encoding () */
1924 -
1857 +
1925 1858 function db_sanitize_post ($out) {
1926 1859 global $wp_db_version;
1927 -
1860 +
1928 1861 $out = $this->db_sanitize_post_check_encoding($out);
1929 -
1862 +
1930 1863 // < 3.6. Core API, including `wp_insert_post()`, expects
1931 1864 // properly slashed data. If `wp_slash()` exists, then
1932 1865 // this is after the big change-over in how data slashing
1933 1866 // was handled.
1934 - if ( !function_exists('wp_slash')) :
1935 -
1867 + if (!function_exists('wp_slash')) :
1868 +
1936 1869 foreach ($out as $key => $value) :
1937 1870 if (is_string($value)) :
1938 1871 $out[$key] = esc_sql($value);
1939 1872 else :
@@ -1939,28 +1872,28 @@
1939 1872 else :
1940 1873 $out[$key] = $value;
1941 1874 endif;
1942 1875 endforeach;
1943 -
1876 +
1944 1877 // For revisions [@23416,@23554), core API expects
1945 1878 // unslashed data. Cf. <https://core.trac.wordpress.org/browser/trunk/wp-includes/post.php?rev=23416>
1946 1879 // NOOP for those revisions.
1947 -
1880 +
1948 1881 // In revisions @23554 to present, `wp_insert_post()`
1949 1882 // expects slashed data once again.
1950 1883 // Cf. <https://core.trac.wordpress.org/changeset/23554/trunk/wp-includes/post.php?contextall=1>
1951 1884 // But at least now we can use the wp_slash API function to do that.
1952 1885 // Hooray.
1953 -
1886 +
1954 1887 elseif ($wp_db_version >= 23524) :
1955 -
1888 +
1956 1889 $out = wp_slash($out);
1957 -
1890 +
1958 1891 endif;
1959 1892
1960 1893 return $out;
1961 1894 }
1962 -
1895 +
1963 1896 /**
1964 1897 * SyndicatedPost::validate_post_id()
1965 1898 *
1966 1899 * @param array $dbpost An array representing the post we attempted to insert or update
@@ -1970,9 +1903,9 @@
1970 1903 if (is_array($ns)) : $ns = implode('::', $ns);
1971 1904 else : $ns = (string) $ns; endif;
1972 1905
1973 1906 // This should never happen.
1974 - if ( !is_numeric($this->_wp_id) or ($this->_wp_id == 0)) :
1907 + if (!is_numeric($this->_wp_id) or ($this->_wp_id == 0)) :
1975 1908 $verb = ($is_update ? 'update existing' : 'insert new');
1976 1909 $guid = $this->guid();
1977 1910 $url = $this->permalink();
1978 1911 $feed = $this->link->uri(array('add_params' => true));
@@ -1981,9 +1914,9 @@
1981 1914 // notice if we are in debug mode.
1982 1915 $mesg = "Failed to $verb item [$guid]. WordPress API returned no valid post ID.\n"
1983 1916 ."\t\tID = ".serialize($this->_wp_id)."\n"
1984 1917 ."\t\tURL = ".MyPHP::val($url)
1985 - ."\t\tFeed = ".MyPHP::val($feed);
1918 + ."\t\tFeed = ".MyPHP::val($feed);
1986 1919
1987 1920 FeedWordPress::diagnostic('updated_feeds:errors', "WordPress API error: $mesg");
1988 1921 FeedWordPress::diagnostic('feed_items:rejected', $mesg);
1989 1922
@@ -1994,9 +1927,9 @@
1994 1927 from the feed at $feed
1995 1928
1996 1929 $ns::_wp_id
1997 1930 EOM;
1998 - FeedWordPressDiagnostic::noncritical_bug(
1931 + FeedWordPress::noncritical_bug(
1999 1932 /*message=*/ $mesg,
2000 1933 /*var =*/ array(
2001 1934 "\$this->_wp_id" => $this->_wp_id,
2002 1935 "\$dbpost" => $dbpost,
@@ -2022,22 +1955,24 @@
2022 1955 * by _wp_put_post_revision.
2023 1956 *
2024 1957 * @param int $revision_id The revision ID to fix up meta-data
2025 1958 */
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)
1959 + function fix_revision_meta ($revision_id) {
1960 + global $wpdb;
2028 1961
1962 + $post_author = (int) $this->post['post_author'];
1963 +
2029 1964 $revision_id = (int) $revision_id;
2030 -
1965 +
2031 1966 // Let's fix the author.
2032 - set_post_field( 'post_author', $this->post['post_author'], $revision_id );
2033 -
1967 + set_post_field('post_author', $this->post['post_author'], $revision_id);
1968 +
2034 1969 // 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 );
1970 + set_post_field('guid', 'http://feedwordpress.radgeek.com/?rev='.$this->update_hash(), $revision_id);
2036 1971
2037 1972 // Let's fire an event for add-ons and filters
2038 - do_action( 'syndicated_post_fix_revision_meta', $revision_id, $this );
2039 -
1973 + do_action('syndicated_post_fix_revision_meta', $revision_id, $this);
1974 +
2040 1975 } /* SyndicatedPost::fix_revision_meta () */
2041 1976
2042 1977 /**
2043 1978 * SyndicatedPost::add_terms() -- if FeedWordPress is processing an
@@ -2125,9 +2060,9 @@
2125 2060 * @param string $new_status Unused action parameter.
2126 2061 * @param string $old_status Unused action parameter.
2127 2062 * @param object $post The database record for the post just inserted.
2128 2063 */
2129 - function add_rss_meta($new_status, $old_status, $post) {
2064 + function add_rss_meta ($new_status, $old_status, $post) {
2130 2065 global $wpdb;
2131 2066 if ($new_status!='inherit') : // Bail if we are creating a revision.
2132 2067 FeedWordPress::diagnostic('syndicated_posts:meta_data', 'Adding post meta-data: {'.implode(", ", array_keys($this->post['meta'])).'}');
2133 2068
@@ -2153,19 +2088,14 @@
2153 2088 WHERE post_id='$postId' AND meta_key='$eKey'
2154 2089 ");
2155 2090
2156 2091 // Allow for either a single value or an array
2157 - if ( !is_array($values)) $values = array($values);
2092 + if (!is_array($values)) $values = array($values);
2158 2093 foreach ( $values as $value ) :
2159 2094 FeedWordPress::diagnostic('syndicated_posts:meta_data', "Adding post meta-datum to post [$postId]: [$key] = ".MyPHP::val($value, /*no newlines=*/ true));
2160 2095 add_post_meta($postId, $key, $value, /*unique=*/ false);
2161 2096 endforeach;
2162 2097 endforeach;
2163 -
2164 - if ( $result === false)
2165 - {
2166 - error_log( "delete query failed: " . $wpdb->last_error );
2167 - }
2168 2098 endif;
2169 2099 endif;
2170 2100 } /* SyndicatedPost::add_rss_meta () */
2171 2101
@@ -2194,9 +2124,9 @@
2194 2124 // as last resort
2195 2125
2196 2126 $candidates = array();
2197 2127 $candidates[] = $a['name'];
2198 - if ( !is_null($source)) : $candidates[] = $source['title']; endif;
2128 + if (!is_null($source)) : $candidates[] = $source['title']; endif;
2199 2129 $candidates[] = $this->link->name(/*fromFeed=*/ true);
2200 2130 $candidates[] = $this->link->name(/*fromFeed=*/ false);
2201 2131 if (strlen($this->link->homepage()) > 0) : $candidates[] = feedwordpress_display_url($this->link->homepage()); endif;
2202 2132 $candidates[] = feedwordpress_display_url($this->link->uri());
@@ -2205,16 +2135,15 @@
2205 2135 // Pick the first one that works from the list, screening against empty
2206 2136 // or forbidden names.
2207 2137
2208 2138 $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;
2139 + while (is_null($author) and ($candidate = each($candidates))) :
2140 + if (!is_null($candidate['value'])
2141 + and (strlen(trim($candidate['value'])) > 0)
2142 + and !in_array(strtolower(trim($candidate['value'])), $forbidden)) :
2143 + $author = $candidate['value'];
2215 2144 endif;
2216 - }
2145 + endwhile;
2217 2146
2218 2147 $email = (isset($a['email']) ? $a['email'] : NULL);
2219 2148 $authorUrl = (isset($a['uri']) ? $a['uri'] : NULL);
2220 2149
@@ -2257,9 +2186,9 @@
2257 2186
2258 2187 $nice_author = sanitize_title($author);
2259 2188 $nice_author = apply_filters('pre_user_nicename', $nice_author);
2260 2189
2261 - $reg_author = esc_sql(preg_quote($author ?: "(unknown)"));
2190 + $reg_author = esc_sql(preg_quote($author));
2262 2191 $author = esc_sql($author);
2263 2192 $email = esc_sql($email);
2264 2193 $test_email = esc_sql($test_email);
2265 2194 $authorUrl = esc_sql($authorUrl);
@@ -2273,11 +2202,9 @@
2273 2202 $author_rule = NULL;
2274 2203 endif;
2275 2204
2276 2205 // 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)) :
2206 + if (is_numeric($author_rule) and get_userdata((int) $author_rule)) :
2280 2207 $id = (int) $author_rule;
2281 2208
2282 2209 // User name is filtered out
2283 2210 elseif ('filter' == $author_rule) :
@@ -2318,11 +2245,11 @@
2318 2245 if (is_null($id)) :
2319 2246 if ($unfamiliar_author === 'create') :
2320 2247 $userdata = array();
2321 2248
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.)
2249 + // WordPress 3 is going to pitch a fit if we attempt to register
2250 + // more than one user account with an empty e-mail address, so we
2251 + // need *something* here. Ugh.
2325 2252 if (strlen($email) == 0 or FeedWordPress::is_null_email($email)) :
2326 2253 $url = parse_url($hostUrl);
2327 2254 $email = $nice_author.'@'.$url['host'];
2328 2255 endif;
@@ -2334,20 +2261,15 @@
2334 2261 $userdata['user_pass'] = substr(md5(uniqid(microtime())), 0, 6); // just something random to lock it up
2335 2262 $userdata['user_email'] = $email;
2336 2263 $userdata['user_url'] = $authorUrl;
2337 2264 $userdata['nickname'] = $author;
2338 -
2339 2265 $parts = preg_split('/\s+/', trim($author), 2);
2340 2266 if (isset($parts[0])) : $userdata['first_name'] = $parts[0]; endif;
2341 2267 if (isset($parts[1])) : $userdata['last_name'] = $parts[1]; endif;
2342 -
2343 2268 $userdata['display_name'] = $author;
2344 2269 $userdata['role'] = 'contributor';
2345 2270
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 {
2271 + do { // Keep trying until you get it right. Or until PHP crashes, I guess.
2350 2272 $id = wp_insert_user($userdata);
2351 2273 if (is_wp_error($id)) :
2352 2274 $codes = $id->get_error_code();
2353 2275 switch ($codes) :
@@ -2352,23 +2274,17 @@
2352 2274 $codes = $id->get_error_code();
2353 2275 switch ($codes) :
2354 2276 case 'empty_user_login' :
2355 2277 case 'existing_user_login' :
2356 - case 'invalid_username' :
2357 2278 // Add a random disambiguator
2358 2279 $userdata['user_login'] .= substr(md5(uniqid(microtime())), 0, 6);
2359 2280 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 2281 case 'user_nicename_too_long' :
2366 - // Add a limited 50 characters user_nicename based on user_login
2282 + // Add a limited 50 caracters user_nicename based on user_login
2367 2283 $userdata['user_nicename'] = mb_substr( $userdata['user_login'], 0, 50 );
2368 2284 break;
2369 2285 case 'existing_user_email' :
2370 - // Disassemble email for username, host
2286 + // No disassemble!
2371 2287 $parts = explode('@', $userdata['user_email'], 2);
2372 2288
2373 2289 // Add a random disambiguator as a gmail-style username extension
2374 2290 $parts[0] .= '+'.substr(md5(uniqid(microtime())), 0, 6);
@@ -2375,39 +2291,14 @@
2375 2291
2376 2292 // Reassemble
2377 2293 $userdata['user_email'] = $parts[0].'@'.$parts[1];
2378 2294 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 2295 endswitch;
2392 2296 endif;
2393 - $insanity = $insanity + 1;
2394 2297 } 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 ) ) :
2298 + elseif (is_numeric($unfamiliar_author) and get_userdata((int) $unfamiliar_author)) :
2408 2299 $id = (int) $unfamiliar_author;
2409 - elseif ( $unfamiliar_author === 'default' ) :
2300 + elseif ($unfamiliar_author === 'default') :
2410 2301 $id = 1;
2411 2302 endif;
2412 2303 endif;
2413 2304 endif;