PluginProbe
FeedWordPress / 2016.1211
FeedWordPress v2016.1211
trunk 0.8 0.9 0.91 0.95 0.96 0.97 0.98 0.981 0.99 0.991 0.992 0.993 2008.1030 2008.1101 2008.1105 2008.1214 2009.0612 2009.0613 2009.0618 2009.0707 2009.1111 2009.1112 2010.0127 2010.0528 All 65 releases
← All changes | syndicatedpost.class.php +357 -504 trunk2016.1211 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 2013.0525
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,58 @@
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 );
132 -
133 - $excerpt = apply_filters( 'syndicated_item_excerpt', $this->excerpt(), $this );
134 -
135 - if ( !empty( $excerpt ) ):
130 +
131 + $excerpt = apply_filters('syndicated_item_excerpt', $this->excerpt(), $this);
132 + if (!empty($excerpt)):
136 133 $this->post['post_excerpt'] = $excerpt;
137 134 endif;
138 135
139 136 // 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 ) );
137 + $offset = (int) get_option('gmt_offset') * 60 * 60;
138 + $post_date_gmt = $this->published(array('default' => -1));
139 + $post_modified_gmt = $this->updated(array('default' => -1));
143 140
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 );
141 + $this->post['post_date_gmt'] = gmdate('Y-m-d H:i:s', $post_date_gmt);
142 + $this->post['post_date'] = gmdate('Y-m-d H:i:s', $post_date_gmt + $offset);
143 + $this->post['post_modified_gmt'] = gmdate('Y-m-d H:i:s', $post_modified_gmt);
144 + $this->post['post_modified'] = gmdate('Y-m-d H:i:s', $post_modified_gmt + $offset);
148 145
149 146 // 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' );
147 + $this->post['post_status'] = $this->link->syndicated_status('post', 'publish');
148 + $this->post['comment_status'] = $this->link->syndicated_status('comment', 'closed');
149 + $this->post['ping_status'] = $this->link->syndicated_status('ping', 'closed');
153 150
154 151 // Unique ID (hopefully a unique tag: URI); failing that, the permalink
155 - $this->post['guid'] = apply_filters( 'syndicated_item_guid', $this->guid(), $this );
152 + $this->post['guid'] = apply_filters('syndicated_item_guid', $this->guid(), $this);
156 153
157 154 // User-supplied custom settings to apply to each post.
158 155 // Do first so that FWP-generated custom settings will
159 156 // overwrite if necessary; thus preventing any munging.
160 - $postMetaIn = $this->link->postmeta( array( "parsed" => true ) );
157 + $postMetaIn = $this->link->postmeta(array("parsed" => true));
161 158 $postMetaOut = array();
162 159
163 - foreach ( $postMetaIn as $key => $meta ) :
164 - $postMetaOut[ $key ] = $meta->do_substitutions( $this );
160 + foreach ($postMetaIn as $key => $meta) :
161 + $postMetaOut[$key] = $meta->do_substitutions($this);
165 162 endforeach;
166 163
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 );
164 + foreach ($postMetaOut as $key => $values) :
165 + $this->post['meta'][$key] = array();
166 + foreach ($values as $value) :
167 + $this->post['meta'][$key][] = apply_filters("syndicated_post_meta_{$key}", $value, $this);
174 168 endforeach;
175 169 endforeach;
176 170
177 171 // RSS 2.0 / Atom 1.0 enclosure support
178 172 $enclosures = $this->entry->get_enclosures();
179 - if ( is_array( $enclosures ) ) : foreach ( $enclosures as $enclosure ) :
173 + if (is_array($enclosures)) : foreach ($enclosures as $enclosure) :
180 174 $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 );
175 + apply_filters('syndicated_item_enclosure_url', $enclosure->get_link(), $this)."\n".
176 + apply_filters('syndicated_item_enclosure_length', $enclosure->get_length(), $this)."\n".
177 + apply_filters('syndicated_item_enclosure_type', $enclosure->get_type(), $this);
184 178 endforeach; endif;
185 179
186 180 // In case you want to point back to the blog this was
187 181 // syndicated from.
@@ -203,18 +197,18 @@
203 197 );
204 198
205 199 // Make use of atom:source data, if present in an aggregated feed
206 200 $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}";
201 + if (!is_null($entry_source)) :
202 + foreach ($entry_source as $what => $value) :
203 + if (!is_null($value)) :
204 + if ($what=='title') : $key = 'syndication_source';
205 + elseif ($what=='feed') : $key = 'syndication_feed';
206 + else : $key = "syndication_source_${what}";
213 207 endif;
214 208
215 - $sourcemeta["{$key}_original"] = apply_filters(
216 - 'syndicated_item_original_source_' . $what,
209 + $sourcemeta["${key}_original"] = apply_filters(
210 + 'syndicated_item_original_source_'.$what,
217 211 $value,
218 212 $this
219 213 );
220 214 endif;
@@ -220,11 +214,11 @@
220 214 endif;
221 215 endforeach;
222 216 endif;
223 217
224 - foreach ( $sourcemeta as $meta_key => $value ) :
225 - if ( !is_null( $value ) ) :
226 - $this->post['meta'][ $meta_key ] = $value;
218 + foreach ($sourcemeta as $meta_key => $value) :
219 + if (!is_null($value)) :
220 + $this->post['meta'][$meta_key] = $value;
227 221 endif;
228 222 endforeach;
229 223
230 224 // Store information on human-readable and machine-readable comment URIs
@@ -229,31 +223,31 @@
229 223
230 224 // Store information on human-readable and machine-readable comment URIs
231 225
232 226 // 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;
227 + $commentLink = apply_filters('syndicated_item_comments', $this->comment_link(), $this);
228 + if (!is_null($commentLink)) : $this->post['meta']['rss:comments'] = $commentLink; endif;
235 229
236 230 // 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;
231 + $commentFeed = apply_filters('syndicated_item_commentrss', $this->comment_feed(), $this);
232 + if (!is_null($commentFeed)) : $this->post['meta']['wfw:commentRSS'] = $commentFeed; endif;
239 233 // Yeah, yeah, now I know that it's supposed to be
240 234 // wfw:commentRss. Oh well. Path dependence, sucka.
241 235
242 236 // Store information to identify the feed that this came from
243 - if ( isset( $this->feedmeta['link/uri'] ) ) :
237 + if (isset($this->feedmeta['link/uri'])) :
244 238 $this->post['meta']['syndication_feed'] = $this->feedmeta['link/uri'];
245 239 endif;
246 - if ( isset( $this->feedmeta['link/id'] ) ) :
240 + if (isset($this->feedmeta['link/id'])) :
247 241 $this->post['meta']['syndication_feed_id'] = $this->feedmeta['link/id'];
248 242 endif;
249 243
250 - if ( isset( $this->item['source_link_self'] ) ) :
244 + if (isset($this->item['source_link_self'])) :
251 245 $this->post['meta']['syndication_feed_original'] = $this->item['source_link_self'];
252 246 endif;
253 247
254 248 // In case you want to know the external permalink...
255 - $this->post['meta']['syndication_permalink'] = apply_filters( 'syndicated_item_link', $this->permalink() );
249 + $this->post['meta']['syndication_permalink'] = apply_filters('syndicated_item_link', $this->permalink());
256 250
257 251 // Store a hash of the post content for checking whether something needs to be updated
258 252 $this->post['meta']['syndication_item_hash'] = $this->update_hash();
259 253
@@ -258,17 +252,14 @@
258 252 $this->post['meta']['syndication_item_hash'] = $this->update_hash();
259 253
260 254 // Categories, Tags, and other Terms: from settings assignments (global settings, subscription settings),
261 255 // 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 );
256 + $this->preset_terms = apply_filters('syndicated_item_preset_terms', $this->get_terms_from_settings(), $this);
257 + $this->feed_terms = apply_filters('syndicated_item_feed_terms', $this->get_terms_from_feeds(), $this);
264 258
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 - );
259 + $this->post['post_type'] = apply_filters('syndicated_post_type', $this->link->setting('syndicated post type', 'syndicated_post_type', 'post'), $this);
270 260 endif;
261 +
271 262 } /* SyndicatedPost::__construct() */
272 263
273 264 #####################################
274 265 #### EXTRACT DATA FROM FEED ITEM ####
@@ -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
@@ -421,43 +412,32 @@
421 412 # content for the excerpt.
422 413 $content = $this->content();
423 414
424 415 // Ignore whitespace, case, and tag cruft.
425 - $theExcerpt = preg_replace('/\s+/', '', strtolower(strip_tags(html_entity_decode($excerpt))));
426 - $theContent = preg_replace('/\s+/', '', strtolower(strip_tags(html_entity_decode($content))));
416 + $theExcerpt = preg_replace('/\s+/', '', strtolower(strip_tags($excerpt)));
417 + $theContent = preg_replace('/\s+/', '', strtolower(strip_tags($content)));
418 +
427 419 if ( empty($excerpt) or $theExcerpt == $theContent ) :
428 420 # If content is available, generate an excerpt.
429 421 if ( strlen(trim($content)) > 0 ) :
430 422 $excerpt = strip_tags($content);
431 423 if (strlen($excerpt) > 255) :
432 - if (is_object($this->link) and is_object($this->link->simplepie)) :
433 - $encoding = $this->link->simplepie->get_encoding();
434 - else :
435 - $encoding = get_option('blog_charset', 'utf8');
436 - endif;
437 - $excerpt = mb_substr($excerpt,0,252,$encoding).'...';
424 + $excerpt = substr($excerpt,0,252).'...';
438 425 endif;
439 426 endif;
440 427 endif;
441 -
442 428 return $excerpt;
443 429 } /* SyndicatedPost::excerpt() */
444 430
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 () {
431 + function permalink () {
452 432 // Handles explicit <link> elements and also RSS 2.0 cases with
453 433 // <guid isPermaLink="true">, etc. Hooray!
454 434 $permalink = $this->entry->get_link();
455 435 return $permalink;
456 - } /* SyndicatedPost::permalink () */
436 + }
457 437
458 - public function created ($params = array()) {
459 - $unfiltered = false; // $default = NULL; // seems to be unused on this function (gwyneth 20230916)
438 + function created ($params = array()) {
439 + $unfiltered = false; $default = NULL;
460 440 extract($params);
461 441
462 442 $date = '';
463 443 if (isset($this->item['dc']['created'])) :
@@ -468,18 +448,18 @@
468 448 $date = $this->item['created'];
469 449 endif;
470 450
471 451 $time = new FeedTime($date);
472 - $tstamp = $time->timestamp();
473 - if ( ! $unfiltered) :
474 - apply_filters('syndicated_item_created', $tstamp, $this);
452 + $ts = $time->timestamp();
453 + if (!$unfiltered) :
454 + apply_filters('syndicated_item_created', $ts, $this);
475 455 endif;
476 - return $tstamp;
456 + return $ts;
477 457 } /* SyndicatedPost::created() */
478 458
479 - public function published ($params = array(), $default = NULL) {
459 + function published ($params = array(), $default = NULL) {
480 460 $fallback = true; $unfiltered = false;
481 - if ( !is_array($params)) : // Old style
461 + if (!is_array($params)) : // Old style
482 462 $fallback = $params;
483 463 else : // New style
484 464 extract($params);
485 465 endif;
@@ -484,9 +464,9 @@
484 464 extract($params);
485 465 endif;
486 466
487 467 $date = '';
488 - $tstamp = null;
468 + $ts = null;
489 469
490 470 # RSS is a fucking mess. Figure out whether we have a date in
491 471 # <dc:date>, <issued>, <pubDate>, etc., and get it into Unix
492 472 # epoch format for reformatting. If we can't find anything,
@@ -504,31 +484,31 @@
504 484 endif;
505 485
506 486 if (strlen($date) > 0) :
507 487 $time = new FeedTime($date);
508 - $tstamp = $time->timestamp();
488 + $ts = $time->timestamp();
509 489 elseif ($fallback) : // Fall back to <updated> / <modified> if present
510 - $tstamp = $this->updated(/*fallback=*/ false, /*default=*/ $default);
490 + $ts = $this->updated(/*fallback=*/ false, /*default=*/ $default);
511 491 endif;
512 492
513 493 # If everything failed, then default to the current time.
514 - if (is_null($tstamp)) :
494 + if (is_null($ts)) :
515 495 if (-1 == $default) :
516 - $tstamp = time();
496 + $ts = time();
517 497 else :
518 - $tstamp = $default;
498 + $ts = $default;
519 499 endif;
520 500 endif;
521 501
522 - if ( ! $unfiltered) :
523 - $tstamp = apply_filters('syndicated_item_published', $tstamp, $this);
502 + if (!$unfiltered) :
503 + $ts = apply_filters('syndicated_item_published', $ts, $this);
524 504 endif;
525 - return $tstamp;
505 + return $ts;
526 506 } /* SyndicatedPost::published() */
527 507
528 - public function updated ($params = array(), $default = -1) {
508 + function updated ($params = array(), $default = -1) {
529 509 $fallback = true; $unfiltered = false;
530 - if ( !is_array($params)) : // Old style
510 + if (!is_array($params)) : // Old style
531 511 $fallback = $params;
532 512 else : // New style
533 513 extract($params);
534 514 endif;
@@ -533,9 +513,9 @@
533 513 extract($params);
534 514 endif;
535 515
536 516 $date = '';
537 - $tstamp = null;
517 + $ts = null;
538 518
539 519 # As far as I know, only dcterms and Atom have reliable ways to
540 520 # specify when something was *modified* last. If neither is
541 521 # available, then we'll try to get the time of publication.
@@ -550,26 +530,26 @@
550 530 endif;
551 531
552 532 if (strlen($date) > 0) :
553 533 $time = new FeedTime($date);
554 - $tstamp = $time->timestamp();
534 + $ts = $time->timestamp();
555 535 elseif ($fallback) : // Fall back to issued / dc:date
556 - $tstamp = $this->published(/*fallback=*/ false, /*default=*/ $default);
536 + $ts = $this->published(/*fallback=*/ false, /*default=*/ $default);
557 537 endif;
558 538
559 539 # If everything failed, then default to the current time.
560 - if (is_null($tstamp)) :
540 + if (is_null($ts)) :
561 541 if (-1 == $default) :
562 - $tstamp = time();
542 + $ts = time();
563 543 else :
564 - $tstamp = $default;
544 + $ts = $default;
565 545 endif;
566 546 endif;
567 547
568 - if ( ! $unfiltered) :
569 - $tstamp = apply_filters('syndicated_item_updated', $tstamp, $this);
548 + if (!$unfiltered) :
549 + apply_filters('syndicated_item_updated', $ts, $this);
570 550 endif;
571 - return $tstamp;
551 + return $ts;
572 552 } /* SyndicatedPost::updated() */
573 553
574 554 var $_hashes = array();
575 555 function stored_hashes ($id = NULL) {
@@ -576,9 +556,9 @@
576 556 if (is_null($id)) :
577 557 $id = $this->wp_id();
578 558 endif;
579 559
580 - if ( !isset($this->_hashes[$id])) :
560 + if (!isset($this->_hashes[$id])) :
581 561 $this->_hashes[$id] = get_post_custom_values(
582 562 'syndication_item_hash', $id
583 563 );
584 564 if (is_null($this->_hashes[$id])) :
@@ -603,27 +583,11 @@
603 583
604 584 return $hash;
605 585 } /* SyndicatedPost::update_hash() */
606 586
607 - /**
608 - * SyndicatedPost::normalize_guid_prefix(): generates a normalized URL
609 - * prefix (including scheme, authority, full path, and the beginning of
610 - * a query string) for creating guids that conform to WordPress's
611 - * internal constraints on the URL space for valid guids. To create a
612 - * normalized guid, just concatenate a valid URL query parameter value
613 - * to the returned URL.
614 - *
615 - * @return string The URL prefix generated.
616 - *
617 - * @uses trailingslashit()
618 - * @uses home_url()
619 - * @uses apply_filters()
620 - */
621 587 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);
625 - } /* SyndicatedPost::normalize_guid_prefix() */
588 + return trailingslashit(get_bloginfo('url')).'?guid=';
589 + }
626 590
627 591 static function normalize_guid ($guid) {
628 592 $guid = trim($guid);
629 593 if (preg_match('/^[0-9a-z]{32}$/i', $guid)) : // MD5
@@ -631,30 +595,12 @@
631 595 elseif ((strlen(esc_url($guid)) == 0) or (esc_url($guid) != $guid)) :
632 596 $guid = SyndicatedPost::normalize_guid_prefix().md5($guid);
633 597 endif;
634 598 $guid = trim($guid);
635 -
636 599 return $guid;
637 600 } /* SyndicatedPost::normalize_guid() */
638 601
639 - 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);
643 - }
644 - static function alternative_guid ($guid) {
645 - $guid = trim($guid);
646 - if (preg_match('/^[0-9a-z]{32}$/i', $guid)) : // MD5
647 - $guid = SyndicatedPost::alternative_guid_prefix().strtolower($guid);
648 - elseif ((strlen(esc_url($guid)) == 0) or (esc_url($guid) != $guid)) :
649 - $guid = SyndicatedPost::alternative_guid_prefix().md5($guid);
650 - endif;
651 - $guid = trim($guid);
652 -
653 - return $guid;
654 - } /* SyndicatedPost::normalize_guid() */
655 -
656 - public function guid () {
602 + function guid () {
657 603 $guid = null;
658 604 if (isset($this->item['id'])): // Atom 0.3 / 1.0
659 605 $guid = $this->item['id'];
660 606 elseif (isset($this->item['atom']['id'])) : // Namespaced Atom
@@ -684,9 +630,9 @@
684 630 // gets clipped by MySQL, uniqueness tests will fail
685 631 // forever after and the post will be endlessly
686 632 // reduplicated. So, instead, Guids Of A Certain Length
687 633 // are hashed down into a nice, manageable tag: URI.
688 - if ( !is_null($original_guid)) :
634 + if (!is_null($original_guid)) :
689 635 $guid .= ',2010-12-03:id.'.md5($original_guid);
690 636
691 637 // If we have a date of creation, then we can use that
692 638 // to uniquely identify the item. (On the other hand, if
@@ -692,9 +638,9 @@
692 638 // to uniquely identify the item. (On the other hand, if
693 639 // the feed producer was consicentious enough to
694 640 // generate dates of creation, she probably also was
695 641 // conscientious enough to generate unique identifiers.)
696 - elseif ( !is_null($this->created())) :
642 + elseif (!is_null($this->created())) :
697 643 $guid .= '://post.'.date('YmdHis', $this->created());
698 644
699 645 // Otherwise, use both the URI of the item, *and* the
700 646 // item's title. We have to use both because titles are
@@ -705,19 +651,19 @@
705 651 // this is about the best we can do.
706 652 else :
707 653 $link = $this->permalink();
708 654 if (is_null($link)) : $link = $this->link->uri(); endif;
709 - $guid .= '://'.md5($link.'/'.$this->title());
655 + $guid .= '://'.md5($link.'/'.$this->item['title']);
710 656 endif;
711 657 endif;
712 658 return $guid;
713 659 } /* SyndicatedPost::guid() */
714 660
715 - public function author () {
661 + function author () {
716 662 $author = array ();
717 663
718 664 $aa = $this->entry->get_authors();
719 - if (is_countable($aa) and count($aa) > 0) :
665 + if (count($aa) > 0) :
720 666 $a = reset($aa);
721 667
722 668 $author = array(
723 669 'name' => $a->get_name(),
@@ -745,9 +691,9 @@
745 691 // e-mail address, but lots of people don't use
746 692 // it that way. So let's make of it what we can.
747 693 $author = parse_email_with_realname($this->item['author']);
748 694
749 - if ( !isset($author['name'])) :
695 + if (!isset($author['name'])) :
750 696 if (isset($author['email'])) :
751 697 $author['name'] = $author['email'];
752 698 else :
753 699 $author['name'] = $this->feed->channel['title'];
@@ -755,9 +701,9 @@
755 701 endif;
756 702 endif;
757 703 endif;
758 704
759 - if ( !isset($author['name']) or is_null($author['name'])) :
705 + if (!isset($author['name']) or is_null($author['name'])) :
760 706 // Nothing found. Try some crappy defaults.
761 707 if ($this->link->name()) :
762 708 $author['name'] = $this->link->name();
763 709 else :
@@ -797,9 +743,9 @@
797 743 * @since 2016.0331
798 744 * @return array of lists, each element has the taxonomy for a key ('category', 'post_tag', etc.),
799 745 * and a list of term codes (either alphanumeric names, or ID numbers encoded in a format that
800 746 * SyndicatedLink::category_ids() can understand) within that taxonomy
801 - *
747 + *
802 748 */
803 749 public function get_terms_from_settings () {
804 750 // Categories: start with default categories, if any.
805 751 $cats = array();
@@ -836,9 +782,9 @@
836 782
837 783 foreach ($taxonomies as $tax) :
838 784 // category and tag settings have already previously been handled
839 785 // but if this is from another taxonomy, then...
840 - if ( !isset($specials[$tax])) :
786 + if (!isset($specials[$tax])) :
841 787 $terms = array();
842 788
843 789 // See if we should get the globals
844 790 if ('no' != $this->link->setting("add/$tax", NULL, 'yes')) :
@@ -855,12 +801,12 @@
855 801 // That's all, folks.
856 802 $preset_terms[$tax] = $terms;
857 803 endif;
858 804 endforeach;
859 -
805 +
860 806 return $preset_terms;
861 807 } /* SyndicatedPost::get_terms_from_settings () */
862 -
808 +
863 809 /**
864 810 * SyndicatedPost::get_terms_from_feeds(): Return an array of terms to associate with the incoming
865 811 * post based on the contents of the subscribed feed (atom:category and rss:category elements, dc:subject
866 812 * elements, tags embedded using microformats in the post content, etc.)
@@ -868,15 +814,15 @@
868 814 * @since 2016.0331
869 815 * @return array of lists, each element has the taxonomy for a key ('category', 'post_tag', etc.),
870 816 * and a list of alphanumeric term names
871 817 */
872 - public function get_terms_from_feeds () {
818 + public function get_terms_from_feeds () {
873 819 // Now add categories from the post, if we have 'em
874 820 $cats = array();
875 821 $post_cats = $this->entry->get_categories();
876 822 if (is_array($post_cats)) : foreach ($post_cats as $cat) :
877 823 $cat_name = $cat->get_term();
878 - if ( ! $cat_name) : $cat_name = $cat->get_label(); endif;
824 + if (!$cat_name) : $cat_name = $cat->get_label(); endif;
879 825
880 826 if ($this->link->setting('cat_split', NULL, NULL)) :
881 827 $pcre = "\007".$this->feedmeta['cat_split']."\007";
882 828 $cats = array_merge(
@@ -913,9 +859,9 @@
913 859 $tags = array();
914 860 $content = $this->content();
915 861 $pattern = FeedWordPressHTML::tagWithAttributeRegex('a', 'rel', 'tag');
916 862 preg_match_all($pattern, $content, $refs, PREG_SET_ORDER);
917 - if (is_countable($refs) and count($refs) > 0) :
863 + if (count($refs) > 0) :
918 864 foreach ($refs as $ref) :
919 865 $tag = FeedWordPressHTML::tagWithAttributeMatch($ref);
920 866 $tags[] = $tag['content'];
921 867 endforeach;
@@ -985,21 +931,21 @@
985 931 * that will be returned to only those with a MIME type which
986 932 * matches this regular expression.
987 933 * @return array
988 934 */
989 - function enclosures( $type = '/.*/' ) {
935 + function enclosures ($type = '/.*/') {
990 936 $enclosures = array();
991 937
992 - if ( isset( $this->item['enclosure#'] ) ) :
938 + if (isset($this->item['enclosure#'])) :
993 939 // 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)
940 + for ($i = 1; $i <= $this->item['enclosure#']; $i++) :
941 + $eid = (($i > 1) ? "#{$id}" : "");
996 942
997 943 // Does it match the type we want?
998 - if ( preg_match($type, $this->item["enclosure{$eid}@type"] ) ) :
944 + if (preg_match($type, $this->item["enclosure{$eid}@type"])) :
999 945 $enclosures[] = array(
1000 - "url" => $this->item["enclosure{$eid}@url"],
1001 - "type" => $this->item["enclosure{$eid}@type"],
946 + "url" => $this->item["enclosure{$eid}@url"],
947 + "type" => $this->item["enclosure{$eid}@type"],
1002 948 "length" => $this->item["enclosure{$eid}@length"],
1003 949 );
1004 950 endif;
1005 951 endfor;
@@ -1028,9 +974,9 @@
1028 974 $ret['id'] = $id_tags[0]['data'];
1029 975 endif;
1030 976 endif;
1031 977
1032 - if ( !is_null($what) and is_scalar($what)) :
978 + if (!is_null($what) and is_scalar($what)) :
1033 979 $ret = $ret[$what];
1034 980 endif;
1035 981 return $ret;
1036 982 }
@@ -1235,11 +1181,12 @@
1235 1181 * 1 = post already syndicated, but needs to be updated to latest
1236 1182 * 2 = post has not yet been syndicated; needs to be created
1237 1183 */
1238 1184 function freshness ($format = 'number') {
1185 + global $wpdb;
1239 1186
1240 1187 if ($this->filtered()) : // This should never happen.
1241 - FeedWordPressDiagnostic::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1188 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1242 1189 endif;
1243 1190
1244 1191 if (is_null($this->_freshness)) : // Not yet checked and cached.
1245 1192 $guid = $this->post['guid'];
@@ -1247,17 +1194,15 @@
1247 1194
1248 1195 $q = new WP_Query(array(
1249 1196 'fields' => '_synfresh', // id, guid, post_modified_gmt
1250 1197 'ignore_sticky_posts' => true,
1251 - 'guid' => $eguid, // it's always better to use the escaped version, right? (gwyneth 20230915)
1198 + 'guid' => $guid,
1252 1199 ));
1253 1200
1254 1201 $old_post = NULL;
1255 1202 if ($q->have_posts()) :
1256 1203 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;
1204 + $old_post = $q->post;
1260 1205 endwhile;
1261 1206 endif;
1262 1207
1263 1208 if (is_null($old_post)) : // No post with this guid
@@ -1268,23 +1213,23 @@
1268 1213 // Presume there is nothing new until we find
1269 1214 // something new.
1270 1215 $updated = false;
1271 1216 $live = false;
1272 -
1217 +
1273 1218 // Pull the list of existing revisions to get
1274 1219 // timestamps.
1275 1220 $revisions = wp_get_post_revisions($old_post->ID);
1276 1221 foreach ($revisions as $rev) :
1277 - $revisions_ts[] = mysql2date('G', $rev->post_modified_gmt);
1222 + $revisions_ts[] = mysql2date('G', $rev->post_modified_gmt);
1278 1223 endforeach;
1279 1224
1280 1225 $revisions_ts[] = mysql2date('G', $old_post->post_modified_gmt);
1281 1226 $last_rev_ts = end($revisions_ts);
1282 1227 $updated_ts = $this->updated(/*fallback=*/ true, /*default=*/ NULL);
1283 -
1228 +
1284 1229 // If we have an explicit updated timestamp,
1285 1230 // check that against existing stamps.
1286 - if ( !is_null($updated_ts)) :
1231 + if (!is_null($updated_ts)) :
1287 1232 $updated = !in_array($updated_ts, $revisions_ts);
1288 1233
1289 1234 // If this a newer revision, make it go
1290 1235 // live. If an older one, just record
@@ -1292,9 +1237,9 @@
1292 1237 $live = ($updated and ($updated_ts > $last_rev_ts));
1293 1238 endif;
1294 1239
1295 1240 // This is a revision we haven't seen before, judging by the date.
1296 -
1241 +
1297 1242 $updatedReason = NULL;
1298 1243 if ($updated) :
1299 1244 $updatedReason = preg_replace(
1300 1245 "/\s+/", " ",
@@ -1310,15 +1255,15 @@
1310 1255 else :
1311 1256 // Or the hash...
1312 1257 $hash = $this->update_hash();
1313 1258 $seen = $this->stored_hashes($old_post->ID);
1314 - if (is_countable($seen) and count($seen) > 0) :
1259 + if (count($seen) > 0) :
1315 1260 $updated = !in_array($hash, $seen); // Not seen yet?
1316 1261 else :
1317 1262 $updated = true; // Can't find syndication meta-data
1318 1263 endif;
1319 1264
1320 - if ($updated and FeedWordPressDiagnostic::is_on('feed_items:freshness:reasons')) :
1265 + if ($updated and FeedWordPress::diagnostic_on('feed_items:freshness:reasons')) :
1321 1266 // In the absence of definitive
1322 1267 // timestamp information, we
1323 1268 // just have to assume that a
1324 1269 // hash we haven't seen before
@@ -1336,11 +1281,11 @@
1336 1281
1337 1282 $frozen = false;
1338 1283 if ($updated) : // Ignore if the post is frozen
1339 1284 $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));
1285 + if (!$frozen) :
1286 + $frozen_values = get_post_custom_values('_syndication_freeze_updates', $old_post->ID);
1287 + $frozen = (count($frozen_values) > 0 and 'yes' == $frozen_values[0]);
1343 1288
1344 1289 if ($frozen) :
1345 1290 $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A UPDATE LOCK ON THIS POST, EVEN THOUGH IT '.$updatedReason;
1346 1291 endif;
@@ -1347,13 +1292,13 @@
1347 1292 else :
1348 1293 $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A FEEDWORDPRESS UPDATE LOCK, EVEN THOUGH IT '.$updatedReason;
1349 1294 endif;
1350 1295 endif;
1351 - $live = ($live and ! $frozen);
1296 + $live = ($live and !$frozen);
1352 1297
1353 1298 if ($updated) :
1354 1299 FeedWordPress::diagnostic('feed_items:freshness', 'Item ['.$guid.'] "'.$this->entry->get_title().'" is an update of an existing post.');
1355 - if ( !is_null($updatedReason)) :
1300 + if (!is_null($updatedReason)) :
1356 1301 $updatedReason = preg_replace('/\s+/', ' ', $updatedReason);
1357 1302 FeedWordPress::diagnostic('feed_items:freshness:reasons', 'Item ['.$guid.'] "'.$this->entry->get_title().'" '.$updatedReason);
1358 1303 endif;
1359 1304
@@ -1374,9 +1319,9 @@
1374 1319 $this->_wp_id = $old_post->ID;
1375 1320 endif;
1376 1321 endif;
1377 1322 endif;
1378 -
1323 +
1379 1324 switch ($format) :
1380 1325 case 'status' :
1381 1326 switch ($this->_freshness) :
1382 1327 case -1:
@@ -1397,17 +1342,17 @@
1397 1342 case 'number' :
1398 1343 default :
1399 1344 $ret = $this->_freshness;
1400 1345 endswitch;
1401 -
1402 -
1346 +
1347 +
1403 1348 return $ret;
1404 1349 } /* SyndicatedPost::freshness () */
1405 -
1350 +
1406 1351 function has_fresh_content () {
1407 1352 return ( ! $this->filtered() and $this->freshness() != 0 );
1408 1353 } /* SyndicatedPost::has_fresh_content () */
1409 -
1354 +
1410 1355 function this_revision_needs_original_post ($freshness = NULL) {
1411 1356 if (is_null($freshness)) :
1412 1357 $freshness = $this->freshness();
1413 1358 endif;
@@ -1412,9 +1357,9 @@
1412 1357 $freshness = $this->freshness();
1413 1358 endif;
1414 1359 return ( $freshness >= 2 );
1415 1360 }
1416 -
1361 +
1417 1362 function this_revision_is_current ($freshness = NULL) {
1418 1363 if (is_null($freshness)) :
1419 1364 $freshness = $this->freshness();
1420 1365 endif;
@@ -1419,13 +1364,13 @@
1419 1364 $freshness = $this->freshness();
1420 1365 endif;
1421 1366 return ( $freshness >= 1 );
1422 1367 } /* SyndicatedPost::this_revision_is_current () */
1423 -
1368 +
1424 1369 function fresh_content_is_update () {
1425 1370 return ($this->freshness() < 2);
1426 1371 } /* SyndicatedPost::fresh_content_is_update () */
1427 -
1372 +
1428 1373 function fresh_storage_diagnostic () {
1429 1374 $ret = NULL;
1430 1375 switch ($this->freshness()) :
1431 1376 case -1 :
@@ -1436,13 +1381,13 @@
1436 1381 break;
1437 1382 case 2 :
1438 1383 default :
1439 1384 $ret = 'Inserting new post "'.$this->post['post_title'].'"';
1440 - break;
1385 + break;
1441 1386 endswitch;
1442 1387 return $ret;
1443 1388 } /* SyndicatedPost::fresh_storage_diagnostic() */
1444 -
1389 +
1445 1390 function fresh_storage_hook () {
1446 1391 $ret = NULL;
1447 1392 switch ($this->freshness()) :
1448 1393 case -1 :
@@ -1455,9 +1400,9 @@
1455 1400 break;
1456 1401 endswitch;
1457 1402 return $ret;
1458 1403 } /* SyndicatedPost::fresh_storage_hook () */
1459 -
1404 +
1460 1405 #################################################
1461 1406 #### INTERNAL STORAGE AND MANAGEMENT METHODS ####
1462 1407 #################################################
1463 1408
@@ -1462,9 +1407,9 @@
1462 1407 #################################################
1463 1408
1464 1409 function wp_id () {
1465 1410 if ($this->filtered()) : // This should never happen.
1466 - FeedWordPressDiagnostic::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1411 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1467 1412 endif;
1468 1413
1469 1414 if (is_null($this->_wp_id) and is_null($this->_freshness)) :
1470 1415 $fresh = $this->freshness(); // sets WP DB id in the process
@@ -1471,165 +1416,124 @@
1471 1416 endif;
1472 1417 return $this->_wp_id;
1473 1418 }
1474 1419
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 - );
1420 + function store () {
1421 + global $wpdb;
1495 1422
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;
1423 + if ($this->filtered()) : // This should never happen.
1424 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1499 1425 endif;
1500 - } /* SyndicatedPost::secure_author_id() */
1501 1426
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);
1427 + $freshness = $this->freshness();
1428 + if ($this->has_fresh_content()) :
1429 + # -- Look up, or create, numeric ID for author
1430 + $this->post['post_author'] = $this->author_id (
1431 + $this->link->setting('unfamiliar author', 'unfamiliar_author', 'create')
1432 + );
1524 1433
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'];
1434 + if (is_null($this->post['post_author'])) :
1435 + FeedWordPress::diagnostic('feed_items:rejected', 'Filtered out item ['.$this->guid().'] without syndication: no author available');
1436 + $this->post = NULL;
1437 + endif;
1438 + endif;
1530 1439
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']);
1440 + // We have to check again in case post has been filtered during
1441 + // the author_id lookup
1442 + if ($this->has_fresh_content()) :
1443 + $mapping = apply_filters('syndicated_post_terms_mapping', array(
1444 + 'category' => array('abbr' => 'cats', 'unfamiliar' => 'category', 'domain' => array('category', 'post_tag')),
1445 + 'post_tag' => array('abbr' => 'tags', 'unfamiliar' => 'post_tag', 'domain' => array('post_tag')),
1446 + ), $this);
1534 1447
1535 - // Eliminate dummy variables
1536 - $taxonomies = array_filter( $taxonomies, 'remove_dummy_zero' );
1448 + $termSet = array(); $valid = null;
1449 + foreach ($this->feed_terms as $what => $anTerms) :
1450 + // Default to using the inclusive procedures (for cats) rather than exclusive (for inline tags)
1451 + $taxes = (isset($mapping[$what]) ? $mapping[$what] : $mapping['category']);
1452 + $unfamiliar = $taxes['unfamiliar'];
1453 +
1454 + if (!is_null($this->post)) : // Not filtered out yet
1455 + # -- Look up, or create, numeric ID for categories
1456 + $taxonomies = $this->link->setting("match/".$taxes['abbr'], 'match_'.$taxes['abbr'], $taxes['domain']);
1537 1457
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);
1458 + // Eliminate dummy variables
1459 + $taxonomies = array_filter($taxonomies, 'remove_dummy_zero');
1541 1460
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 - );
1461 + // Allow FWP add-on filters to control the taxonomies we use to search for a term
1462 + $taxonomies = apply_filters("syndicated_post_terms_match", $taxonomies, $what, $this);
1463 + $taxonomies = apply_filters("syndicated_post_terms_match_${what}", $taxonomies, $this);
1552 1464
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 - );
1465 + // Allow FWP add-on filters to control with greater precision what happens on unmatched
1466 + $unmatched = apply_filters("syndicated_post_terms_unfamiliar",
1467 + $this->link->setting(
1468 + "unfamiliar {$unfamiliar}",
1469 + "unfamiliar_{$unfamiliar}",
1470 + 'create:'.$unfamiliar
1471 + ),
1472 + $what,
1473 + $this
1474 + );
1562 1475
1563 - if (is_null($terms) or is_null($termSet)) :
1564 - // filtered out -- no matches
1565 - else :
1566 - $valid = true;
1476 + $terms = $this->category_ids (
1477 + $anTerms,
1478 + $unmatched,
1479 + /*taxonomies=*/ $taxonomies,
1480 + array(
1481 + 'singleton' => false, // I don't like surprises
1482 + 'filters' => true,
1483 + )
1484 + );
1567 1485
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;
1486 + if (is_null($terms) or is_null($termSet)) :
1487 + // filtered out -- no matches
1488 + else :
1489 + $valid = true;
1578 1490
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();
1491 + // filter mode off, or at least one match
1492 + foreach ($terms as $tax => $term_ids) :
1493 + if (!isset($termSet[$tax])) :
1494 + $termSet[$tax] = array();
1495 + endif;
1496 + $termSet[$tax] = array_merge($termSet[$tax], $term_ids);
1497 + endforeach;
1498 + endif;
1586 1499 endif;
1587 - $this->post['tax_input'][$tax] = array_merge(
1588 - $this->post['tax_input'][$tax],
1589 - $term_ids
1590 - );
1591 1500 endforeach;
1592 1501
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;
1502 + if (is_null($valid)) : // Plonked
1503 + $this->post = NULL;
1504 + else : // We can proceed
1505 + $this->post['tax_input'] = array();
1506 + foreach ($termSet as $tax => $term_ids) :
1507 + if (!isset($this->post['tax_input'][$tax])) :
1508 + $this->post['tax_input'][$tax] = array();
1509 + endif;
1510 + $this->post['tax_input'][$tax] = array_merge(
1511 + $this->post['tax_input'][$tax],
1512 + $term_ids
1513 + );
1514 + endforeach;
1598 1515
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() */
1516 + // Now let's add on the feed and global presets
1517 + foreach ($this->preset_terms as $tax => $term_ids) :
1518 + if (!isset($this->post['tax_input'][$tax])) :
1519 + $this->post['tax_input'][$tax] = array();
1520 + endif;
1612 1521
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__);
1522 + $this->post['tax_input'][$tax] = array_merge (
1523 + $this->post['tax_input'][$tax],
1524 + $this->category_ids (
1525 + /*terms=*/ $term_ids,
1526 + /*unfamiliar=*/ 'create:'.$tax, // These are presets; for those added in a tagbox editor, the tag may not yet exist
1527 + /*taxonomies=*/ array($tax),
1528 + array(
1529 + 'singleton' => true,
1530 + ))
1531 + );
1532 + endforeach;
1533 + endif;
1621 1534 endif;
1622 1535
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 1536 // We have to check again in case the post has been filtered
1633 1537 // during the category/tags/taxonomy terms lookup
1634 1538 if ($this->has_fresh_content()) :
1635 1539 // Filter some individual fields
@@ -1640,9 +1544,9 @@
1640 1544 $post_name = (is_null($this->_wp_post) ? NULL : $this->_wp_post->post_name);
1641 1545
1642 1546 // Allow filters to set post slug. Props niska.
1643 1547 $post_name = apply_filters('syndicated_post_slug', $post_name, $this);
1644 - if ( !empty($post_name)) :
1548 + if (!empty($post_name)) :
1645 1549 $this->post['post_name'] = $post_name;
1646 1550 endif;
1647 1551
1648 1552 $this->post = apply_filters('syndicated_post', $this->post, $this);
@@ -1661,29 +1565,29 @@
1661 1565 /*callback=*/ array($this, 'add_rss_meta'),
1662 1566 /*priority=*/ -10000, /* very early */
1663 1567 /*arguments=*/ 3
1664 1568 );
1665 -
1569 +
1666 1570 $ret = false;
1667 1571 if ($this->has_fresh_content()) :
1668 1572 $diag = $this->fresh_storage_diagnostic();
1669 - if ( !is_null($diag)) :
1573 + if (!is_null($diag)) :
1670 1574 FeedWordPress::diagnostic('syndicated_posts', $diag);
1671 1575 endif;
1672 -
1576 +
1673 1577 $this->insert_post(/*update=*/ $this->fresh_content_is_update(), $this->freshness());
1674 1578
1675 1579 $hook = $this->fresh_storage_hook();
1676 - if ( !is_null($hook)) :
1580 + if (!is_null($hook)) :
1677 1581 do_action($hook, $this->wp_id(), $this);
1678 1582 endif;
1679 -
1583 +
1680 1584 $ret = $this->freshness('status');
1681 1585 endif;
1682 1586
1683 1587 // If this is a legit, non-filtered post, tag it as found on the
1684 1588 // feed regardless of fresh or stale status
1685 - if ( ! $this->filtered()) :
1589 + if (!$this->filtered()) :
1686 1590 $key = '_feedwordpress_retire_me_' . $this->link->id;
1687 1591 delete_post_meta($this->wp_id(), $key);
1688 1592
1689 1593 $status = get_post_field('post_status', $this->wp_id());
@@ -1709,13 +1613,15 @@
1709 1613 return $ret;
1710 1614 } /* function SyndicatedPost::store () */
1711 1615
1712 1616 function insert_post ($update = false, $freshness = 2) {
1617 + global $wpdb;
1618 +
1713 1619 $dbpost = $this->normalize_post(/*new=*/ true);
1714 1620
1715 1621 $ret = null;
1716 1622
1717 - if ( !is_null($dbpost)) :
1623 + if (!is_null($dbpost)) :
1718 1624 $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks
1719 1625
1720 1626 // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data
1721 1627 add_action('_wp_put_post_revision', array($this, 'fix_revision_meta'));
@@ -1769,28 +1675,27 @@
1769 1675 // the basic post record before we do anything else.
1770 1676 if ($this->this_revision_needs_original_post()) :
1771 1677 // *sigh*, for handling inconsistent slash expectations < 3.6
1772 1678 $sdbpost = $this->db_sanitize_post($dbpost);
1773 -
1679 +
1774 1680 // Go ahead and insert the first post record to
1775 1681 // anchor the revision history.
1776 -
1777 1682 $this->_wp_id = wp_insert_post($sdbpost, /*return wp_error=*/ true);
1778 -
1683 +
1779 1684 $dbpost['ID'] = $this->_wp_id;
1780 1685 endif;
1781 -
1686 +
1782 1687 // Sanity check: if the attempt to insert post
1783 1688 // returned an error, then feeding that error
1784 1689 // object in to _wp_put_post_revision() would
1785 1690 // cause a fatal error. Better to break out.
1786 - if ( !is_wp_error($this->_wp_id)) :
1691 + if (!is_wp_error($this->_wp_id)) :
1787 1692 // Now that we've made sure the original exists, insert
1788 1693 // this version here as a revision.
1789 1694 $revision_id = _wp_put_post_revision($dbpost, /*autosave=*/ false);
1790 1695
1791 - if ( ! $this->this_revision_needs_original_post()) :
1792 -
1696 + if (!$this->this_revision_needs_original_post()) :
1697 +
1793 1698 if ($this->this_revision_is_current()) :
1794 1699
1795 1700 wp_restore_post_revision($revision_id);
1796 1701
@@ -1830,30 +1735,18 @@
1830 1735 add_filter('content_save_pre', $filter);
1831 1736 endforeach;
1832 1737
1833 1738 $this->validate_post_id($dbpost, $update, array(__CLASS__, __FUNCTION__));
1834 -
1739 +
1835 1740 $ret = $this->_wp_id;
1836 1741 endif;
1837 1742 return $ret;
1838 1743 } /* function SyndicatedPost::insert_post () */
1839 1744
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 1745 function insert_new () {
1847 1746 $this->insert_post(/*update=*/ false, 1);
1848 1747 } /* SyndicatedPost::insert_new() */
1849 1748
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 1749 function update_existing () {
1857 1750 $this->insert_post(/*update=*/ true, 2);
1858 1751 } /* SyndicatedPost::update_existing() */
1859 1752
@@ -1859,12 +1752,14 @@
1859 1752
1860 1753 /**
1861 1754 * SyndicatedPost::normalize_post()
1862 1755 *
1863 - * @param bool $new If true, this post is to be inserted anew. If false, it is an update of an existing post. (Unused)
1756 + * @param bool $new If true, this post is to be inserted anew. If false, it is an update of an existing post.
1864 1757 * @return array A normalized representation of the post ready to be inserted into the database or sent to the WordPress API functions
1865 1758 */
1866 - function normalize_post( $new = true ) {
1759 + function normalize_post ($new = true) {
1760 + global $wpdb;
1761 +
1867 1762 $out = $this->post;
1868 1763
1869 1764 $fullPost = $out['post_title'].$out['post_content'];
1870 1765 $fullPost .= (isset($out['post_excerpt']) ? $out['post_excerpt'] : '');
@@ -1898,19 +1793,19 @@
1898 1793 // that work for MySQL but not for PHP mb_check_encoding. So instead
1899 1794 // we must rely on WordPress setting blog_charset and hope that the user
1900 1795 // has got their database encoding set up to roughly match
1901 1796 $charset = get_option('blog_charset', 'utf8');
1902 -
1797 +
1903 1798 foreach ($out as $key => $value) :
1904 1799 if (is_string($value)) :
1905 -
1906 - if ( !function_exists('mb_check_encoding') or mb_check_encoding($value, $charset)) :
1800 +
1801 + if (!function_exists('mb_check_encoding') or mb_check_encoding($value, $charset)) :
1907 1802 $out[$key] = $value;
1908 1803 else :
1909 1804 $fromCharset = mb_detect_encoding($value, mb_detect_order(), /*strict=*/ true);
1910 1805 $out[$key] = mb_convert_encoding($value, $charset, $fromCharset);
1911 1806 endif;
1912 -
1807 +
1913 1808 elseif (is_array($value)) :
1914 1809 $out[$key] = $this->db_sanitize_post_check_encoding($value);
1915 1810
1916 1811 else :
@@ -1915,25 +1810,25 @@
1915 1810
1916 1811 else :
1917 1812 $out[$key] = $value;
1918 1813 endif;
1919 -
1814 +
1920 1815 endforeach;
1921 -
1816 +
1922 1817 return $out;
1923 1818 } /* SyndicatedPost::db_sanitize_post_check_encoding () */
1924 -
1819 +
1925 1820 function db_sanitize_post ($out) {
1926 1821 global $wp_db_version;
1927 -
1822 +
1928 1823 $out = $this->db_sanitize_post_check_encoding($out);
1929 -
1824 +
1930 1825 // < 3.6. Core API, including `wp_insert_post()`, expects
1931 1826 // properly slashed data. If `wp_slash()` exists, then
1932 1827 // this is after the big change-over in how data slashing
1933 1828 // was handled.
1934 - if ( !function_exists('wp_slash')) :
1935 -
1829 + if (!function_exists('wp_slash')) :
1830 +
1936 1831 foreach ($out as $key => $value) :
1937 1832 if (is_string($value)) :
1938 1833 $out[$key] = esc_sql($value);
1939 1834 else :
@@ -1939,28 +1834,28 @@
1939 1834 else :
1940 1835 $out[$key] = $value;
1941 1836 endif;
1942 1837 endforeach;
1943 -
1838 +
1944 1839 // For revisions [@23416,@23554), core API expects
1945 1840 // unslashed data. Cf. <https://core.trac.wordpress.org/browser/trunk/wp-includes/post.php?rev=23416>
1946 1841 // NOOP for those revisions.
1947 -
1842 +
1948 1843 // In revisions @23554 to present, `wp_insert_post()`
1949 1844 // expects slashed data once again.
1950 1845 // Cf. <https://core.trac.wordpress.org/changeset/23554/trunk/wp-includes/post.php?contextall=1>
1951 1846 // But at least now we can use the wp_slash API function to do that.
1952 1847 // Hooray.
1953 -
1848 +
1954 1849 elseif ($wp_db_version >= 23524) :
1955 -
1850 +
1956 1851 $out = wp_slash($out);
1957 -
1852 +
1958 1853 endif;
1959 1854
1960 1855 return $out;
1961 1856 }
1962 -
1857 +
1963 1858 /**
1964 1859 * SyndicatedPost::validate_post_id()
1965 1860 *
1966 1861 * @param array $dbpost An array representing the post we attempted to insert or update
@@ -1970,9 +1865,9 @@
1970 1865 if (is_array($ns)) : $ns = implode('::', $ns);
1971 1866 else : $ns = (string) $ns; endif;
1972 1867
1973 1868 // This should never happen.
1974 - if ( !is_numeric($this->_wp_id) or ($this->_wp_id == 0)) :
1869 + if (!is_numeric($this->_wp_id) or ($this->_wp_id == 0)) :
1975 1870 $verb = ($is_update ? 'update existing' : 'insert new');
1976 1871 $guid = $this->guid();
1977 1872 $url = $this->permalink();
1978 1873 $feed = $this->link->uri(array('add_params' => true));
@@ -1981,9 +1876,9 @@
1981 1876 // notice if we are in debug mode.
1982 1877 $mesg = "Failed to $verb item [$guid]. WordPress API returned no valid post ID.\n"
1983 1878 ."\t\tID = ".serialize($this->_wp_id)."\n"
1984 1879 ."\t\tURL = ".MyPHP::val($url)
1985 - ."\t\tFeed = ".MyPHP::val($feed);
1880 + ."\t\tFeed = ".MyPHP::val($feed);
1986 1881
1987 1882 FeedWordPress::diagnostic('updated_feeds:errors', "WordPress API error: $mesg");
1988 1883 FeedWordPress::diagnostic('feed_items:rejected', $mesg);
1989 1884
@@ -1994,9 +1889,9 @@
1994 1889 from the feed at $feed
1995 1890
1996 1891 $ns::_wp_id
1997 1892 EOM;
1998 - FeedWordPressDiagnostic::noncritical_bug(
1893 + FeedWordPress::noncritical_bug(
1999 1894 /*message=*/ $mesg,
2000 1895 /*var =*/ array(
2001 1896 "\$this->_wp_id" => $this->_wp_id,
2002 1897 "\$dbpost" => $dbpost,
@@ -2022,22 +1917,24 @@
2022 1917 * by _wp_put_post_revision.
2023 1918 *
2024 1919 * @param int $revision_id The revision ID to fix up meta-data
2025 1920 */
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)
1921 + function fix_revision_meta ($revision_id) {
1922 + global $wpdb;
2028 1923
1924 + $post_author = (int) $this->post['post_author'];
1925 +
2029 1926 $revision_id = (int) $revision_id;
2030 -
1927 +
2031 1928 // Let's fix the author.
2032 - set_post_field( 'post_author', $this->post['post_author'], $revision_id );
2033 -
1929 + set_post_field('post_author', $this->post['post_author'], $revision_id);
1930 +
2034 1931 // 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 );
1932 + set_post_field('guid', 'http://feedwordpress.radgeek.com/?rev='.$this->update_hash(), $revision_id);
2036 1933
2037 1934 // Let's fire an event for add-ons and filters
2038 - do_action( 'syndicated_post_fix_revision_meta', $revision_id, $this );
2039 -
1935 + do_action('syndicated_post_fix_revision_meta', $revision_id, $this);
1936 +
2040 1937 } /* SyndicatedPost::fix_revision_meta () */
2041 1938
2042 1939 /**
2043 1940 * SyndicatedPost::add_terms() -- if FeedWordPress is processing an
@@ -2125,9 +2022,9 @@
2125 2022 * @param string $new_status Unused action parameter.
2126 2023 * @param string $old_status Unused action parameter.
2127 2024 * @param object $post The database record for the post just inserted.
2128 2025 */
2129 - function add_rss_meta($new_status, $old_status, $post) {
2026 + function add_rss_meta ($new_status, $old_status, $post) {
2130 2027 global $wpdb;
2131 2028 if ($new_status!='inherit') : // Bail if we are creating a revision.
2132 2029 FeedWordPress::diagnostic('syndicated_posts:meta_data', 'Adding post meta-data: {'.implode(", ", array_keys($this->post['meta'])).'}');
2133 2030
@@ -2153,19 +2050,14 @@
2153 2050 WHERE post_id='$postId' AND meta_key='$eKey'
2154 2051 ");
2155 2052
2156 2053 // Allow for either a single value or an array
2157 - if ( !is_array($values)) $values = array($values);
2054 + if (!is_array($values)) $values = array($values);
2158 2055 foreach ( $values as $value ) :
2159 2056 FeedWordPress::diagnostic('syndicated_posts:meta_data', "Adding post meta-datum to post [$postId]: [$key] = ".MyPHP::val($value, /*no newlines=*/ true));
2160 2057 add_post_meta($postId, $key, $value, /*unique=*/ false);
2161 2058 endforeach;
2162 2059 endforeach;
2163 -
2164 - if ( $result === false)
2165 - {
2166 - error_log( "delete query failed: " . $wpdb->last_error );
2167 - }
2168 2060 endif;
2169 2061 endif;
2170 2062 } /* SyndicatedPost::add_rss_meta () */
2171 2063
@@ -2194,9 +2086,9 @@
2194 2086 // as last resort
2195 2087
2196 2088 $candidates = array();
2197 2089 $candidates[] = $a['name'];
2198 - if ( !is_null($source)) : $candidates[] = $source['title']; endif;
2090 + if (!is_null($source)) : $candidates[] = $source['title']; endif;
2199 2091 $candidates[] = $this->link->name(/*fromFeed=*/ true);
2200 2092 $candidates[] = $this->link->name(/*fromFeed=*/ false);
2201 2093 if (strlen($this->link->homepage()) > 0) : $candidates[] = feedwordpress_display_url($this->link->homepage()); endif;
2202 2094 $candidates[] = feedwordpress_display_url($this->link->uri());
@@ -2205,16 +2097,15 @@
2205 2097 // Pick the first one that works from the list, screening against empty
2206 2098 // or forbidden names.
2207 2099
2208 2100 $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;
2101 + while (is_null($author) and ($candidate = each($candidates))) :
2102 + if (!is_null($candidate['value'])
2103 + and (strlen(trim($candidate['value'])) > 0)
2104 + and !in_array(strtolower(trim($candidate['value'])), $forbidden)) :
2105 + $author = $candidate['value'];
2215 2106 endif;
2216 - }
2107 + endwhile;
2217 2108
2218 2109 $email = (isset($a['email']) ? $a['email'] : NULL);
2219 2110 $authorUrl = (isset($a['uri']) ? $a['uri'] : NULL);
2220 2111
@@ -2257,9 +2148,9 @@
2257 2148
2258 2149 $nice_author = sanitize_title($author);
2259 2150 $nice_author = apply_filters('pre_user_nicename', $nice_author);
2260 2151
2261 - $reg_author = esc_sql(preg_quote($author ?: "(unknown)"));
2152 + $reg_author = esc_sql(preg_quote($author));
2262 2153 $author = esc_sql($author);
2263 2154 $email = esc_sql($email);
2264 2155 $test_email = esc_sql($test_email);
2265 2156 $authorUrl = esc_sql($authorUrl);
@@ -2273,11 +2164,9 @@
2273 2164 $author_rule = NULL;
2274 2165 endif;
2275 2166
2276 2167 // 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)) :
2168 + if (is_numeric($author_rule) and get_userdata((int) $author_rule)) :
2280 2169 $id = (int) $author_rule;
2281 2170
2282 2171 // User name is filtered out
2283 2172 elseif ('filter' == $author_rule) :
@@ -2318,11 +2207,11 @@
2318 2207 if (is_null($id)) :
2319 2208 if ($unfamiliar_author === 'create') :
2320 2209 $userdata = array();
2321 2210
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.)
2211 + // WordPress 3 is going to pitch a fit if we attempt to register
2212 + // more than one user account with an empty e-mail address, so we
2213 + // need *something* here. Ugh.
2325 2214 if (strlen($email) == 0 or FeedWordPress::is_null_email($email)) :
2326 2215 $url = parse_url($hostUrl);
2327 2216 $email = $nice_author.'@'.$url['host'];
2328 2217 endif;
@@ -2334,20 +2223,15 @@
2334 2223 $userdata['user_pass'] = substr(md5(uniqid(microtime())), 0, 6); // just something random to lock it up
2335 2224 $userdata['user_email'] = $email;
2336 2225 $userdata['user_url'] = $authorUrl;
2337 2226 $userdata['nickname'] = $author;
2338 -
2339 2227 $parts = preg_split('/\s+/', trim($author), 2);
2340 2228 if (isset($parts[0])) : $userdata['first_name'] = $parts[0]; endif;
2341 2229 if (isset($parts[1])) : $userdata['last_name'] = $parts[1]; endif;
2342 -
2343 2230 $userdata['display_name'] = $author;
2344 2231 $userdata['role'] = 'contributor';
2345 2232
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 {
2233 + do { // Keep trying until you get it right. Or until PHP crashes, I guess.
2350 2234 $id = wp_insert_user($userdata);
2351 2235 if (is_wp_error($id)) :
2352 2236 $codes = $id->get_error_code();
2353 2237 switch ($codes) :
@@ -2352,23 +2236,17 @@
2352 2236 $codes = $id->get_error_code();
2353 2237 switch ($codes) :
2354 2238 case 'empty_user_login' :
2355 2239 case 'existing_user_login' :
2356 - case 'invalid_username' :
2357 2240 // Add a random disambiguator
2358 2241 $userdata['user_login'] .= substr(md5(uniqid(microtime())), 0, 6);
2359 2242 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 2243 case 'user_nicename_too_long' :
2366 - // Add a limited 50 characters user_nicename based on user_login
2244 + // Add a limited 50 caracters user_nicename based on user_login
2367 2245 $userdata['user_nicename'] = mb_substr( $userdata['user_login'], 0, 50 );
2368 2246 break;
2369 2247 case 'existing_user_email' :
2370 - // Disassemble email for username, host
2248 + // No disassemble!
2371 2249 $parts = explode('@', $userdata['user_email'], 2);
2372 2250
2373 2251 // Add a random disambiguator as a gmail-style username extension
2374 2252 $parts[0] .= '+'.substr(md5(uniqid(microtime())), 0, 6);
@@ -2375,39 +2253,14 @@
2375 2253
2376 2254 // Reassemble
2377 2255 $userdata['user_email'] = $parts[0].'@'.$parts[1];
2378 2256 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 2257 endswitch;
2392 2258 endif;
2393 - $insanity = $insanity + 1;
2394 2259 } 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 ) ) :
2260 + elseif (is_numeric($unfamiliar_author) and get_userdata((int) $unfamiliar_author)) :
2408 2261 $id = (int) $unfamiliar_author;
2409 - elseif ( $unfamiliar_author === 'default' ) :
2262 + elseif ($unfamiliar_author === 'default') :
2410 2263 $id = 1;
2411 2264 endif;
2412 2265 endif;
2413 2266 endif;