PluginProbe
FeedWordPress / 2017.0913
FeedWordPress v2017.0913
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 +361 -476 trunk2017.0913 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])) :
@@ -618,11 +598,10 @@
618 598 * @uses home_url()
619 599 * @uses apply_filters()
620 600 */
621 601 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);
602 + $url = trailingslashit(home_url(/*path=*/ '', /*scheme=*/ 'http'));
603 + return apply_filters('syndicated_item_guid_normalized_prefix', $url . '?guid=');
625 604 } /* SyndicatedPost::normalize_guid_prefix() */
626 605
627 606 static function normalize_guid ($guid) {
628 607 $guid = trim($guid);
@@ -631,16 +610,15 @@
631 610 elseif ((strlen(esc_url($guid)) == 0) or (esc_url($guid) != $guid)) :
632 611 $guid = SyndicatedPost::normalize_guid_prefix().md5($guid);
633 612 endif;
634 613 $guid = trim($guid);
635 -
614 +
636 615 return $guid;
637 616 } /* SyndicatedPost::normalize_guid() */
638 617
639 618 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);
619 + $url = trailingslashit(home_url(/*path=*/ '', /*scheme=*/ 'https'));
620 + return apply_filters('syndicated_item_guid_normalized_prefix', $url . '?guid=');
643 621 }
644 622 static function alternative_guid ($guid) {
645 623 $guid = trim($guid);
646 624 if (preg_match('/^[0-9a-z]{32}$/i', $guid)) : // MD5
@@ -648,13 +626,13 @@
648 626 elseif ((strlen(esc_url($guid)) == 0) or (esc_url($guid) != $guid)) :
649 627 $guid = SyndicatedPost::alternative_guid_prefix().md5($guid);
650 628 endif;
651 629 $guid = trim($guid);
652 -
630 +
653 631 return $guid;
654 632 } /* SyndicatedPost::normalize_guid() */
655 633
656 - public function guid () {
634 + function guid () {
657 635 $guid = null;
658 636 if (isset($this->item['id'])): // Atom 0.3 / 1.0
659 637 $guid = $this->item['id'];
660 638 elseif (isset($this->item['atom']['id'])) : // Namespaced Atom
@@ -684,9 +662,9 @@
684 662 // gets clipped by MySQL, uniqueness tests will fail
685 663 // forever after and the post will be endlessly
686 664 // reduplicated. So, instead, Guids Of A Certain Length
687 665 // are hashed down into a nice, manageable tag: URI.
688 - if ( !is_null($original_guid)) :
666 + if (!is_null($original_guid)) :
689 667 $guid .= ',2010-12-03:id.'.md5($original_guid);
690 668
691 669 // If we have a date of creation, then we can use that
692 670 // to uniquely identify the item. (On the other hand, if
@@ -692,9 +670,9 @@
692 670 // to uniquely identify the item. (On the other hand, if
693 671 // the feed producer was consicentious enough to
694 672 // generate dates of creation, she probably also was
695 673 // conscientious enough to generate unique identifiers.)
696 - elseif ( !is_null($this->created())) :
674 + elseif (!is_null($this->created())) :
697 675 $guid .= '://post.'.date('YmdHis', $this->created());
698 676
699 677 // Otherwise, use both the URI of the item, *and* the
700 678 // item's title. We have to use both because titles are
@@ -705,19 +683,19 @@
705 683 // this is about the best we can do.
706 684 else :
707 685 $link = $this->permalink();
708 686 if (is_null($link)) : $link = $this->link->uri(); endif;
709 - $guid .= '://'.md5($link.'/'.$this->title());
687 + $guid .= '://'.md5($link.'/'.$this->item['title']);
710 688 endif;
711 689 endif;
712 690 return $guid;
713 691 } /* SyndicatedPost::guid() */
714 692
715 - public function author () {
693 + function author () {
716 694 $author = array ();
717 695
718 696 $aa = $this->entry->get_authors();
719 - if (is_countable($aa) and count($aa) > 0) :
697 + if (count($aa) > 0) :
720 698 $a = reset($aa);
721 699
722 700 $author = array(
723 701 'name' => $a->get_name(),
@@ -745,9 +723,9 @@
745 723 // e-mail address, but lots of people don't use
746 724 // it that way. So let's make of it what we can.
747 725 $author = parse_email_with_realname($this->item['author']);
748 726
749 - if ( !isset($author['name'])) :
727 + if (!isset($author['name'])) :
750 728 if (isset($author['email'])) :
751 729 $author['name'] = $author['email'];
752 730 else :
753 731 $author['name'] = $this->feed->channel['title'];
@@ -755,9 +733,9 @@
755 733 endif;
756 734 endif;
757 735 endif;
758 736
759 - if ( !isset($author['name']) or is_null($author['name'])) :
737 + if (!isset($author['name']) or is_null($author['name'])) :
760 738 // Nothing found. Try some crappy defaults.
761 739 if ($this->link->name()) :
762 740 $author['name'] = $this->link->name();
763 741 else :
@@ -797,9 +775,9 @@
797 775 * @since 2016.0331
798 776 * @return array of lists, each element has the taxonomy for a key ('category', 'post_tag', etc.),
799 777 * and a list of term codes (either alphanumeric names, or ID numbers encoded in a format that
800 778 * SyndicatedLink::category_ids() can understand) within that taxonomy
801 - *
779 + *
802 780 */
803 781 public function get_terms_from_settings () {
804 782 // Categories: start with default categories, if any.
805 783 $cats = array();
@@ -836,9 +814,9 @@
836 814
837 815 foreach ($taxonomies as $tax) :
838 816 // category and tag settings have already previously been handled
839 817 // but if this is from another taxonomy, then...
840 - if ( !isset($specials[$tax])) :
818 + if (!isset($specials[$tax])) :
841 819 $terms = array();
842 820
843 821 // See if we should get the globals
844 822 if ('no' != $this->link->setting("add/$tax", NULL, 'yes')) :
@@ -855,12 +833,12 @@
855 833 // That's all, folks.
856 834 $preset_terms[$tax] = $terms;
857 835 endif;
858 836 endforeach;
859 -
837 +
860 838 return $preset_terms;
861 839 } /* SyndicatedPost::get_terms_from_settings () */
862 -
840 +
863 841 /**
864 842 * SyndicatedPost::get_terms_from_feeds(): Return an array of terms to associate with the incoming
865 843 * post based on the contents of the subscribed feed (atom:category and rss:category elements, dc:subject
866 844 * elements, tags embedded using microformats in the post content, etc.)
@@ -868,15 +846,15 @@
868 846 * @since 2016.0331
869 847 * @return array of lists, each element has the taxonomy for a key ('category', 'post_tag', etc.),
870 848 * and a list of alphanumeric term names
871 849 */
872 - public function get_terms_from_feeds () {
850 + public function get_terms_from_feeds () {
873 851 // Now add categories from the post, if we have 'em
874 852 $cats = array();
875 853 $post_cats = $this->entry->get_categories();
876 854 if (is_array($post_cats)) : foreach ($post_cats as $cat) :
877 855 $cat_name = $cat->get_term();
878 - if ( ! $cat_name) : $cat_name = $cat->get_label(); endif;
856 + if (!$cat_name) : $cat_name = $cat->get_label(); endif;
879 857
880 858 if ($this->link->setting('cat_split', NULL, NULL)) :
881 859 $pcre = "\007".$this->feedmeta['cat_split']."\007";
882 860 $cats = array_merge(
@@ -913,9 +891,9 @@
913 891 $tags = array();
914 892 $content = $this->content();
915 893 $pattern = FeedWordPressHTML::tagWithAttributeRegex('a', 'rel', 'tag');
916 894 preg_match_all($pattern, $content, $refs, PREG_SET_ORDER);
917 - if (is_countable($refs) and count($refs) > 0) :
895 + if (count($refs) > 0) :
918 896 foreach ($refs as $ref) :
919 897 $tag = FeedWordPressHTML::tagWithAttributeMatch($ref);
920 898 $tags[] = $tag['content'];
921 899 endforeach;
@@ -985,21 +963,21 @@
985 963 * that will be returned to only those with a MIME type which
986 964 * matches this regular expression.
987 965 * @return array
988 966 */
989 - function enclosures( $type = '/.*/' ) {
967 + function enclosures ($type = '/.*/') {
990 968 $enclosures = array();
991 969
992 - if ( isset( $this->item['enclosure#'] ) ) :
970 + if (isset($this->item['enclosure#'])) :
993 971 // 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)
972 + for ($i = 1; $i <= $this->item['enclosure#']; $i++) :
973 + $eid = (($i > 1) ? "#{$id}" : "");
996 974
997 975 // Does it match the type we want?
998 - if ( preg_match($type, $this->item["enclosure{$eid}@type"] ) ) :
976 + if (preg_match($type, $this->item["enclosure{$eid}@type"])) :
999 977 $enclosures[] = array(
1000 - "url" => $this->item["enclosure{$eid}@url"],
1001 - "type" => $this->item["enclosure{$eid}@type"],
978 + "url" => $this->item["enclosure{$eid}@url"],
979 + "type" => $this->item["enclosure{$eid}@type"],
1002 980 "length" => $this->item["enclosure{$eid}@length"],
1003 981 );
1004 982 endif;
1005 983 endfor;
@@ -1028,9 +1006,9 @@
1028 1006 $ret['id'] = $id_tags[0]['data'];
1029 1007 endif;
1030 1008 endif;
1031 1009
1032 - if ( !is_null($what) and is_scalar($what)) :
1010 + if (!is_null($what) and is_scalar($what)) :
1033 1011 $ret = $ret[$what];
1034 1012 endif;
1035 1013 return $ret;
1036 1014 }
@@ -1235,11 +1213,12 @@
1235 1213 * 1 = post already syndicated, but needs to be updated to latest
1236 1214 * 2 = post has not yet been syndicated; needs to be created
1237 1215 */
1238 1216 function freshness ($format = 'number') {
1217 + global $wpdb;
1239 1218
1240 1219 if ($this->filtered()) : // This should never happen.
1241 - FeedWordPressDiagnostic::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1220 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1242 1221 endif;
1243 1222
1244 1223 if (is_null($this->_freshness)) : // Not yet checked and cached.
1245 1224 $guid = $this->post['guid'];
@@ -1247,17 +1226,15 @@
1247 1226
1248 1227 $q = new WP_Query(array(
1249 1228 'fields' => '_synfresh', // id, guid, post_modified_gmt
1250 1229 'ignore_sticky_posts' => true,
1251 - 'guid' => $eguid, // it's always better to use the escaped version, right? (gwyneth 20230915)
1230 + 'guid' => $guid,
1252 1231 ));
1253 1232
1254 1233 $old_post = NULL;
1255 1234 if ($q->have_posts()) :
1256 1235 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;
1236 + $old_post = $q->post;
1260 1237 endwhile;
1261 1238 endif;
1262 1239
1263 1240 if (is_null($old_post)) : // No post with this guid
@@ -1268,23 +1245,23 @@
1268 1245 // Presume there is nothing new until we find
1269 1246 // something new.
1270 1247 $updated = false;
1271 1248 $live = false;
1272 -
1249 +
1273 1250 // Pull the list of existing revisions to get
1274 1251 // timestamps.
1275 1252 $revisions = wp_get_post_revisions($old_post->ID);
1276 1253 foreach ($revisions as $rev) :
1277 - $revisions_ts[] = mysql2date('G', $rev->post_modified_gmt);
1254 + $revisions_ts[] = mysql2date('G', $rev->post_modified_gmt);
1278 1255 endforeach;
1279 1256
1280 1257 $revisions_ts[] = mysql2date('G', $old_post->post_modified_gmt);
1281 1258 $last_rev_ts = end($revisions_ts);
1282 1259 $updated_ts = $this->updated(/*fallback=*/ true, /*default=*/ NULL);
1283 -
1260 +
1284 1261 // If we have an explicit updated timestamp,
1285 1262 // check that against existing stamps.
1286 - if ( !is_null($updated_ts)) :
1263 + if (!is_null($updated_ts)) :
1287 1264 $updated = !in_array($updated_ts, $revisions_ts);
1288 1265
1289 1266 // If this a newer revision, make it go
1290 1267 // live. If an older one, just record
@@ -1292,9 +1269,9 @@
1292 1269 $live = ($updated and ($updated_ts > $last_rev_ts));
1293 1270 endif;
1294 1271
1295 1272 // This is a revision we haven't seen before, judging by the date.
1296 -
1273 +
1297 1274 $updatedReason = NULL;
1298 1275 if ($updated) :
1299 1276 $updatedReason = preg_replace(
1300 1277 "/\s+/", " ",
@@ -1310,15 +1287,15 @@
1310 1287 else :
1311 1288 // Or the hash...
1312 1289 $hash = $this->update_hash();
1313 1290 $seen = $this->stored_hashes($old_post->ID);
1314 - if (is_countable($seen) and count($seen) > 0) :
1291 + if (count($seen) > 0) :
1315 1292 $updated = !in_array($hash, $seen); // Not seen yet?
1316 1293 else :
1317 1294 $updated = true; // Can't find syndication meta-data
1318 1295 endif;
1319 1296
1320 - if ($updated and FeedWordPressDiagnostic::is_on('feed_items:freshness:reasons')) :
1297 + if ($updated and FeedWordPress::diagnostic_on('feed_items:freshness:reasons')) :
1321 1298 // In the absence of definitive
1322 1299 // timestamp information, we
1323 1300 // just have to assume that a
1324 1301 // hash we haven't seen before
@@ -1336,11 +1313,11 @@
1336 1313
1337 1314 $frozen = false;
1338 1315 if ($updated) : // Ignore if the post is frozen
1339 1316 $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));
1317 + if (!$frozen) :
1318 + $frozen_values = get_post_custom_values('_syndication_freeze_updates', $old_post->ID);
1319 + $frozen = (count($frozen_values) > 0 and 'yes' == $frozen_values[0]);
1343 1320
1344 1321 if ($frozen) :
1345 1322 $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A UPDATE LOCK ON THIS POST, EVEN THOUGH IT '.$updatedReason;
1346 1323 endif;
@@ -1347,13 +1324,13 @@
1347 1324 else :
1348 1325 $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A FEEDWORDPRESS UPDATE LOCK, EVEN THOUGH IT '.$updatedReason;
1349 1326 endif;
1350 1327 endif;
1351 - $live = ($live and ! $frozen);
1328 + $live = ($live and !$frozen);
1352 1329
1353 1330 if ($updated) :
1354 1331 FeedWordPress::diagnostic('feed_items:freshness', 'Item ['.$guid.'] "'.$this->entry->get_title().'" is an update of an existing post.');
1355 - if ( !is_null($updatedReason)) :
1332 + if (!is_null($updatedReason)) :
1356 1333 $updatedReason = preg_replace('/\s+/', ' ', $updatedReason);
1357 1334 FeedWordPress::diagnostic('feed_items:freshness:reasons', 'Item ['.$guid.'] "'.$this->entry->get_title().'" '.$updatedReason);
1358 1335 endif;
1359 1336
@@ -1374,9 +1351,9 @@
1374 1351 $this->_wp_id = $old_post->ID;
1375 1352 endif;
1376 1353 endif;
1377 1354 endif;
1378 -
1355 +
1379 1356 switch ($format) :
1380 1357 case 'status' :
1381 1358 switch ($this->_freshness) :
1382 1359 case -1:
@@ -1397,17 +1374,17 @@
1397 1374 case 'number' :
1398 1375 default :
1399 1376 $ret = $this->_freshness;
1400 1377 endswitch;
1401 -
1402 -
1378 +
1379 +
1403 1380 return $ret;
1404 1381 } /* SyndicatedPost::freshness () */
1405 -
1382 +
1406 1383 function has_fresh_content () {
1407 1384 return ( ! $this->filtered() and $this->freshness() != 0 );
1408 1385 } /* SyndicatedPost::has_fresh_content () */
1409 -
1386 +
1410 1387 function this_revision_needs_original_post ($freshness = NULL) {
1411 1388 if (is_null($freshness)) :
1412 1389 $freshness = $this->freshness();
1413 1390 endif;
@@ -1412,9 +1389,9 @@
1412 1389 $freshness = $this->freshness();
1413 1390 endif;
1414 1391 return ( $freshness >= 2 );
1415 1392 }
1416 -
1393 +
1417 1394 function this_revision_is_current ($freshness = NULL) {
1418 1395 if (is_null($freshness)) :
1419 1396 $freshness = $this->freshness();
1420 1397 endif;
@@ -1419,13 +1396,13 @@
1419 1396 $freshness = $this->freshness();
1420 1397 endif;
1421 1398 return ( $freshness >= 1 );
1422 1399 } /* SyndicatedPost::this_revision_is_current () */
1423 -
1400 +
1424 1401 function fresh_content_is_update () {
1425 1402 return ($this->freshness() < 2);
1426 1403 } /* SyndicatedPost::fresh_content_is_update () */
1427 -
1404 +
1428 1405 function fresh_storage_diagnostic () {
1429 1406 $ret = NULL;
1430 1407 switch ($this->freshness()) :
1431 1408 case -1 :
@@ -1436,13 +1413,13 @@
1436 1413 break;
1437 1414 case 2 :
1438 1415 default :
1439 1416 $ret = 'Inserting new post "'.$this->post['post_title'].'"';
1440 - break;
1417 + break;
1441 1418 endswitch;
1442 1419 return $ret;
1443 1420 } /* SyndicatedPost::fresh_storage_diagnostic() */
1444 -
1421 +
1445 1422 function fresh_storage_hook () {
1446 1423 $ret = NULL;
1447 1424 switch ($this->freshness()) :
1448 1425 case -1 :
@@ -1455,9 +1432,9 @@
1455 1432 break;
1456 1433 endswitch;
1457 1434 return $ret;
1458 1435 } /* SyndicatedPost::fresh_storage_hook () */
1459 -
1436 +
1460 1437 #################################################
1461 1438 #### INTERNAL STORAGE AND MANAGEMENT METHODS ####
1462 1439 #################################################
1463 1440
@@ -1462,9 +1439,9 @@
1462 1439 #################################################
1463 1440
1464 1441 function wp_id () {
1465 1442 if ($this->filtered()) : // This should never happen.
1466 - FeedWordPressDiagnostic::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1443 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1467 1444 endif;
1468 1445
1469 1446 if (is_null($this->_wp_id) and is_null($this->_freshness)) :
1470 1447 $fresh = $this->freshness(); // sets WP DB id in the process
@@ -1471,165 +1448,124 @@
1471 1448 endif;
1472 1449 return $this->_wp_id;
1473 1450 }
1474 1451
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 - );
1452 + function store () {
1453 + global $wpdb;
1495 1454
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;
1455 + if ($this->filtered()) : // This should never happen.
1456 + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
1499 1457 endif;
1500 - } /* SyndicatedPost::secure_author_id() */
1501 1458
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);
1459 + $freshness = $this->freshness();
1460 + if ($this->has_fresh_content()) :
1461 + # -- Look up, or create, numeric ID for author
1462 + $this->post['post_author'] = $this->author_id (
1463 + $this->link->setting('unfamiliar author', 'unfamiliar_author', 'create')
1464 + );
1524 1465
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'];
1466 + if (is_null($this->post['post_author'])) :
1467 + FeedWordPress::diagnostic('feed_items:rejected', 'Filtered out item ['.$this->guid().'] without syndication: no author available');
1468 + $this->post = NULL;
1469 + endif;
1470 + endif;
1530 1471
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']);
1472 + // We have to check again in case post has been filtered during
1473 + // the author_id lookup
1474 + if ($this->has_fresh_content()) :
1475 + $mapping = apply_filters('syndicated_post_terms_mapping', array(
1476 + 'category' => array('abbr' => 'cats', 'unfamiliar' => 'category', 'domain' => array('category', 'post_tag')),
1477 + 'post_tag' => array('abbr' => 'tags', 'unfamiliar' => 'post_tag', 'domain' => array('post_tag')),
1478 + ), $this);
1534 1479
1535 - // Eliminate dummy variables
1536 - $taxonomies = array_filter( $taxonomies, 'remove_dummy_zero' );
1480 + $termSet = array(); $valid = null;
1481 + foreach ($this->feed_terms as $what => $anTerms) :
1482 + // Default to using the inclusive procedures (for cats) rather than exclusive (for inline tags)
1483 + $taxes = (isset($mapping[$what]) ? $mapping[$what] : $mapping['category']);
1484 + $unfamiliar = $taxes['unfamiliar'];
1485 +
1486 + if (!is_null($this->post)) : // Not filtered out yet
1487 + # -- Look up, or create, numeric ID for categories
1488 + $taxonomies = $this->link->setting("match/".$taxes['abbr'], 'match_'.$taxes['abbr'], $taxes['domain']);
1537 1489
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);
1490 + // Eliminate dummy variables
1491 + $taxonomies = array_filter($taxonomies, 'remove_dummy_zero');
1541 1492
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 - );
1493 + // Allow FWP add-on filters to control the taxonomies we use to search for a term
1494 + $taxonomies = apply_filters("syndicated_post_terms_match", $taxonomies, $what, $this);
1495 + $taxonomies = apply_filters("syndicated_post_terms_match_${what}", $taxonomies, $this);
1552 1496
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 - );
1497 + // Allow FWP add-on filters to control with greater precision what happens on unmatched
1498 + $unmatched = apply_filters("syndicated_post_terms_unfamiliar",
1499 + $this->link->setting(
1500 + "unfamiliar {$unfamiliar}",
1501 + "unfamiliar_{$unfamiliar}",
1502 + 'create:'.$unfamiliar
1503 + ),
1504 + $what,
1505 + $this
1506 + );
1562 1507
1563 - if (is_null($terms) or is_null($termSet)) :
1564 - // filtered out -- no matches
1565 - else :
1566 - $valid = true;
1508 + $terms = $this->category_ids (
1509 + $anTerms,
1510 + $unmatched,
1511 + /*taxonomies=*/ $taxonomies,
1512 + array(
1513 + 'singleton' => false, // I don't like surprises
1514 + 'filters' => true,
1515 + )
1516 + );
1567 1517
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;
1518 + if (is_null($terms) or is_null($termSet)) :
1519 + // filtered out -- no matches
1520 + else :
1521 + $valid = true;
1578 1522
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();
1523 + // filter mode off, or at least one match
1524 + foreach ($terms as $tax => $term_ids) :
1525 + if (!isset($termSet[$tax])) :
1526 + $termSet[$tax] = array();
1527 + endif;
1528 + $termSet[$tax] = array_merge($termSet[$tax], $term_ids);
1529 + endforeach;
1530 + endif;
1586 1531 endif;
1587 - $this->post['tax_input'][$tax] = array_merge(
1588 - $this->post['tax_input'][$tax],
1589 - $term_ids
1590 - );
1591 1532 endforeach;
1592 1533
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;
1534 + if (is_null($valid)) : // Plonked
1535 + $this->post = NULL;
1536 + else : // We can proceed
1537 + $this->post['tax_input'] = array();
1538 + foreach ($termSet as $tax => $term_ids) :
1539 + if (!isset($this->post['tax_input'][$tax])) :
1540 + $this->post['tax_input'][$tax] = array();
1541 + endif;
1542 + $this->post['tax_input'][$tax] = array_merge(
1543 + $this->post['tax_input'][$tax],
1544 + $term_ids
1545 + );
1546 + endforeach;
1598 1547
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() */
1548 + // Now let's add on the feed and global presets
1549 + foreach ($this->preset_terms as $tax => $term_ids) :
1550 + if (!isset($this->post['tax_input'][$tax])) :
1551 + $this->post['tax_input'][$tax] = array();
1552 + endif;
1612 1553
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__);
1554 + $this->post['tax_input'][$tax] = array_merge (
1555 + $this->post['tax_input'][$tax],
1556 + $this->category_ids (
1557 + /*terms=*/ $term_ids,
1558 + /*unfamiliar=*/ 'create:'.$tax, // These are presets; for those added in a tagbox editor, the tag may not yet exist
1559 + /*taxonomies=*/ array($tax),
1560 + array(
1561 + 'singleton' => true,
1562 + ))
1563 + );
1564 + endforeach;
1565 + endif;
1621 1566 endif;
1622 1567
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 1568 // We have to check again in case the post has been filtered
1633 1569 // during the category/tags/taxonomy terms lookup
1634 1570 if ($this->has_fresh_content()) :
1635 1571 // Filter some individual fields
@@ -1640,9 +1576,9 @@
1640 1576 $post_name = (is_null($this->_wp_post) ? NULL : $this->_wp_post->post_name);
1641 1577
1642 1578 // Allow filters to set post slug. Props niska.
1643 1579 $post_name = apply_filters('syndicated_post_slug', $post_name, $this);
1644 - if ( !empty($post_name)) :
1580 + if (!empty($post_name)) :
1645 1581 $this->post['post_name'] = $post_name;
1646 1582 endif;
1647 1583
1648 1584 $this->post = apply_filters('syndicated_post', $this->post, $this);
@@ -1661,29 +1597,29 @@
1661 1597 /*callback=*/ array($this, 'add_rss_meta'),
1662 1598 /*priority=*/ -10000, /* very early */
1663 1599 /*arguments=*/ 3
1664 1600 );
1665 -
1601 +
1666 1602 $ret = false;
1667 1603 if ($this->has_fresh_content()) :
1668 1604 $diag = $this->fresh_storage_diagnostic();
1669 - if ( !is_null($diag)) :
1605 + if (!is_null($diag)) :
1670 1606 FeedWordPress::diagnostic('syndicated_posts', $diag);
1671 1607 endif;
1672 -
1608 +
1673 1609 $this->insert_post(/*update=*/ $this->fresh_content_is_update(), $this->freshness());
1674 1610
1675 1611 $hook = $this->fresh_storage_hook();
1676 - if ( !is_null($hook)) :
1612 + if (!is_null($hook)) :
1677 1613 do_action($hook, $this->wp_id(), $this);
1678 1614 endif;
1679 -
1615 +
1680 1616 $ret = $this->freshness('status');
1681 1617 endif;
1682 1618
1683 1619 // If this is a legit, non-filtered post, tag it as found on the
1684 1620 // feed regardless of fresh or stale status
1685 - if ( ! $this->filtered()) :
1621 + if (!$this->filtered()) :
1686 1622 $key = '_feedwordpress_retire_me_' . $this->link->id;
1687 1623 delete_post_meta($this->wp_id(), $key);
1688 1624
1689 1625 $status = get_post_field('post_status', $this->wp_id());
@@ -1709,13 +1645,15 @@
1709 1645 return $ret;
1710 1646 } /* function SyndicatedPost::store () */
1711 1647
1712 1648 function insert_post ($update = false, $freshness = 2) {
1649 + global $wpdb;
1650 +
1713 1651 $dbpost = $this->normalize_post(/*new=*/ true);
1714 1652
1715 1653 $ret = null;
1716 1654
1717 - if ( !is_null($dbpost)) :
1655 + if (!is_null($dbpost)) :
1718 1656 $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks
1719 1657
1720 1658 // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data
1721 1659 add_action('_wp_put_post_revision', array($this, 'fix_revision_meta'));
@@ -1769,28 +1707,27 @@
1769 1707 // the basic post record before we do anything else.
1770 1708 if ($this->this_revision_needs_original_post()) :
1771 1709 // *sigh*, for handling inconsistent slash expectations < 3.6
1772 1710 $sdbpost = $this->db_sanitize_post($dbpost);
1773 -
1711 +
1774 1712 // Go ahead and insert the first post record to
1775 1713 // anchor the revision history.
1776 -
1777 1714 $this->_wp_id = wp_insert_post($sdbpost, /*return wp_error=*/ true);
1778 -
1715 +
1779 1716 $dbpost['ID'] = $this->_wp_id;
1780 1717 endif;
1781 -
1718 +
1782 1719 // Sanity check: if the attempt to insert post
1783 1720 // returned an error, then feeding that error
1784 1721 // object in to _wp_put_post_revision() would
1785 1722 // cause a fatal error. Better to break out.
1786 - if ( !is_wp_error($this->_wp_id)) :
1723 + if (!is_wp_error($this->_wp_id)) :
1787 1724 // Now that we've made sure the original exists, insert
1788 1725 // this version here as a revision.
1789 1726 $revision_id = _wp_put_post_revision($dbpost, /*autosave=*/ false);
1790 1727
1791 - if ( ! $this->this_revision_needs_original_post()) :
1792 -
1728 + if (!$this->this_revision_needs_original_post()) :
1729 +
1793 1730 if ($this->this_revision_is_current()) :
1794 1731
1795 1732 wp_restore_post_revision($revision_id);
1796 1733
@@ -1830,30 +1767,18 @@
1830 1767 add_filter('content_save_pre', $filter);
1831 1768 endforeach;
1832 1769
1833 1770 $this->validate_post_id($dbpost, $update, array(__CLASS__, __FUNCTION__));
1834 -
1771 +
1835 1772 $ret = $this->_wp_id;
1836 1773 endif;
1837 1774 return $ret;
1838 1775 } /* function SyndicatedPost::insert_post () */
1839 1776
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 1777 function insert_new () {
1847 1778 $this->insert_post(/*update=*/ false, 1);
1848 1779 } /* SyndicatedPost::insert_new() */
1849 1780
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 1781 function update_existing () {
1857 1782 $this->insert_post(/*update=*/ true, 2);
1858 1783 } /* SyndicatedPost::update_existing() */
1859 1784
@@ -1859,12 +1784,14 @@
1859 1784
1860 1785 /**
1861 1786 * SyndicatedPost::normalize_post()
1862 1787 *
1863 - * @param bool $new If true, this post is to be inserted anew. If false, it is an update of an existing post. (Unused)
1788 + * @param bool $new If true, this post is to be inserted anew. If false, it is an update of an existing post.
1864 1789 * @return array A normalized representation of the post ready to be inserted into the database or sent to the WordPress API functions
1865 1790 */
1866 - function normalize_post( $new = true ) {
1791 + function normalize_post ($new = true) {
1792 + global $wpdb;
1793 +
1867 1794 $out = $this->post;
1868 1795
1869 1796 $fullPost = $out['post_title'].$out['post_content'];
1870 1797 $fullPost .= (isset($out['post_excerpt']) ? $out['post_excerpt'] : '');
@@ -1898,19 +1825,19 @@
1898 1825 // that work for MySQL but not for PHP mb_check_encoding. So instead
1899 1826 // we must rely on WordPress setting blog_charset and hope that the user
1900 1827 // has got their database encoding set up to roughly match
1901 1828 $charset = get_option('blog_charset', 'utf8');
1902 -
1829 +
1903 1830 foreach ($out as $key => $value) :
1904 1831 if (is_string($value)) :
1905 -
1906 - if ( !function_exists('mb_check_encoding') or mb_check_encoding($value, $charset)) :
1832 +
1833 + if (!function_exists('mb_check_encoding') or mb_check_encoding($value, $charset)) :
1907 1834 $out[$key] = $value;
1908 1835 else :
1909 1836 $fromCharset = mb_detect_encoding($value, mb_detect_order(), /*strict=*/ true);
1910 1837 $out[$key] = mb_convert_encoding($value, $charset, $fromCharset);
1911 1838 endif;
1912 -
1839 +
1913 1840 elseif (is_array($value)) :
1914 1841 $out[$key] = $this->db_sanitize_post_check_encoding($value);
1915 1842
1916 1843 else :
@@ -1915,25 +1842,25 @@
1915 1842
1916 1843 else :
1917 1844 $out[$key] = $value;
1918 1845 endif;
1919 -
1846 +
1920 1847 endforeach;
1921 -
1848 +
1922 1849 return $out;
1923 1850 } /* SyndicatedPost::db_sanitize_post_check_encoding () */
1924 -
1851 +
1925 1852 function db_sanitize_post ($out) {
1926 1853 global $wp_db_version;
1927 -
1854 +
1928 1855 $out = $this->db_sanitize_post_check_encoding($out);
1929 -
1856 +
1930 1857 // < 3.6. Core API, including `wp_insert_post()`, expects
1931 1858 // properly slashed data. If `wp_slash()` exists, then
1932 1859 // this is after the big change-over in how data slashing
1933 1860 // was handled.
1934 - if ( !function_exists('wp_slash')) :
1935 -
1861 + if (!function_exists('wp_slash')) :
1862 +
1936 1863 foreach ($out as $key => $value) :
1937 1864 if (is_string($value)) :
1938 1865 $out[$key] = esc_sql($value);
1939 1866 else :
@@ -1939,28 +1866,28 @@
1939 1866 else :
1940 1867 $out[$key] = $value;
1941 1868 endif;
1942 1869 endforeach;
1943 -
1870 +
1944 1871 // For revisions [@23416,@23554), core API expects
1945 1872 // unslashed data. Cf. <https://core.trac.wordpress.org/browser/trunk/wp-includes/post.php?rev=23416>
1946 1873 // NOOP for those revisions.
1947 -
1874 +
1948 1875 // In revisions @23554 to present, `wp_insert_post()`
1949 1876 // expects slashed data once again.
1950 1877 // Cf. <https://core.trac.wordpress.org/changeset/23554/trunk/wp-includes/post.php?contextall=1>
1951 1878 // But at least now we can use the wp_slash API function to do that.
1952 1879 // Hooray.
1953 -
1880 +
1954 1881 elseif ($wp_db_version >= 23524) :
1955 -
1882 +
1956 1883 $out = wp_slash($out);
1957 -
1884 +
1958 1885 endif;
1959 1886
1960 1887 return $out;
1961 1888 }
1962 -
1889 +
1963 1890 /**
1964 1891 * SyndicatedPost::validate_post_id()
1965 1892 *
1966 1893 * @param array $dbpost An array representing the post we attempted to insert or update
@@ -1970,9 +1897,9 @@
1970 1897 if (is_array($ns)) : $ns = implode('::', $ns);
1971 1898 else : $ns = (string) $ns; endif;
1972 1899
1973 1900 // This should never happen.
1974 - if ( !is_numeric($this->_wp_id) or ($this->_wp_id == 0)) :
1901 + if (!is_numeric($this->_wp_id) or ($this->_wp_id == 0)) :
1975 1902 $verb = ($is_update ? 'update existing' : 'insert new');
1976 1903 $guid = $this->guid();
1977 1904 $url = $this->permalink();
1978 1905 $feed = $this->link->uri(array('add_params' => true));
@@ -1981,9 +1908,9 @@
1981 1908 // notice if we are in debug mode.
1982 1909 $mesg = "Failed to $verb item [$guid]. WordPress API returned no valid post ID.\n"
1983 1910 ."\t\tID = ".serialize($this->_wp_id)."\n"
1984 1911 ."\t\tURL = ".MyPHP::val($url)
1985 - ."\t\tFeed = ".MyPHP::val($feed);
1912 + ."\t\tFeed = ".MyPHP::val($feed);
1986 1913
1987 1914 FeedWordPress::diagnostic('updated_feeds:errors', "WordPress API error: $mesg");
1988 1915 FeedWordPress::diagnostic('feed_items:rejected', $mesg);
1989 1916
@@ -1994,9 +1921,9 @@
1994 1921 from the feed at $feed
1995 1922
1996 1923 $ns::_wp_id
1997 1924 EOM;
1998 - FeedWordPressDiagnostic::noncritical_bug(
1925 + FeedWordPress::noncritical_bug(
1999 1926 /*message=*/ $mesg,
2000 1927 /*var =*/ array(
2001 1928 "\$this->_wp_id" => $this->_wp_id,
2002 1929 "\$dbpost" => $dbpost,
@@ -2022,22 +1949,24 @@
2022 1949 * by _wp_put_post_revision.
2023 1950 *
2024 1951 * @param int $revision_id The revision ID to fix up meta-data
2025 1952 */
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)
1953 + function fix_revision_meta ($revision_id) {
1954 + global $wpdb;
2028 1955
1956 + $post_author = (int) $this->post['post_author'];
1957 +
2029 1958 $revision_id = (int) $revision_id;
2030 -
1959 +
2031 1960 // Let's fix the author.
2032 - set_post_field( 'post_author', $this->post['post_author'], $revision_id );
2033 -
1961 + set_post_field('post_author', $this->post['post_author'], $revision_id);
1962 +
2034 1963 // 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 );
1964 + set_post_field('guid', 'http://feedwordpress.radgeek.com/?rev='.$this->update_hash(), $revision_id);
2036 1965
2037 1966 // Let's fire an event for add-ons and filters
2038 - do_action( 'syndicated_post_fix_revision_meta', $revision_id, $this );
2039 -
1967 + do_action('syndicated_post_fix_revision_meta', $revision_id, $this);
1968 +
2040 1969 } /* SyndicatedPost::fix_revision_meta () */
2041 1970
2042 1971 /**
2043 1972 * SyndicatedPost::add_terms() -- if FeedWordPress is processing an
@@ -2125,9 +2054,9 @@
2125 2054 * @param string $new_status Unused action parameter.
2126 2055 * @param string $old_status Unused action parameter.
2127 2056 * @param object $post The database record for the post just inserted.
2128 2057 */
2129 - function add_rss_meta($new_status, $old_status, $post) {
2058 + function add_rss_meta ($new_status, $old_status, $post) {
2130 2059 global $wpdb;
2131 2060 if ($new_status!='inherit') : // Bail if we are creating a revision.
2132 2061 FeedWordPress::diagnostic('syndicated_posts:meta_data', 'Adding post meta-data: {'.implode(", ", array_keys($this->post['meta'])).'}');
2133 2062
@@ -2153,19 +2082,14 @@
2153 2082 WHERE post_id='$postId' AND meta_key='$eKey'
2154 2083 ");
2155 2084
2156 2085 // Allow for either a single value or an array
2157 - if ( !is_array($values)) $values = array($values);
2086 + if (!is_array($values)) $values = array($values);
2158 2087 foreach ( $values as $value ) :
2159 2088 FeedWordPress::diagnostic('syndicated_posts:meta_data', "Adding post meta-datum to post [$postId]: [$key] = ".MyPHP::val($value, /*no newlines=*/ true));
2160 2089 add_post_meta($postId, $key, $value, /*unique=*/ false);
2161 2090 endforeach;
2162 2091 endforeach;
2163 -
2164 - if ( $result === false)
2165 - {
2166 - error_log( "delete query failed: " . $wpdb->last_error );
2167 - }
2168 2092 endif;
2169 2093 endif;
2170 2094 } /* SyndicatedPost::add_rss_meta () */
2171 2095
@@ -2194,9 +2118,9 @@
2194 2118 // as last resort
2195 2119
2196 2120 $candidates = array();
2197 2121 $candidates[] = $a['name'];
2198 - if ( !is_null($source)) : $candidates[] = $source['title']; endif;
2122 + if (!is_null($source)) : $candidates[] = $source['title']; endif;
2199 2123 $candidates[] = $this->link->name(/*fromFeed=*/ true);
2200 2124 $candidates[] = $this->link->name(/*fromFeed=*/ false);
2201 2125 if (strlen($this->link->homepage()) > 0) : $candidates[] = feedwordpress_display_url($this->link->homepage()); endif;
2202 2126 $candidates[] = feedwordpress_display_url($this->link->uri());
@@ -2205,16 +2129,15 @@
2205 2129 // Pick the first one that works from the list, screening against empty
2206 2130 // or forbidden names.
2207 2131
2208 2132 $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;
2133 + while (is_null($author) and ($candidate = each($candidates))) :
2134 + if (!is_null($candidate['value'])
2135 + and (strlen(trim($candidate['value'])) > 0)
2136 + and !in_array(strtolower(trim($candidate['value'])), $forbidden)) :
2137 + $author = $candidate['value'];
2215 2138 endif;
2216 - }
2139 + endwhile;
2217 2140
2218 2141 $email = (isset($a['email']) ? $a['email'] : NULL);
2219 2142 $authorUrl = (isset($a['uri']) ? $a['uri'] : NULL);
2220 2143
@@ -2257,9 +2180,9 @@
2257 2180
2258 2181 $nice_author = sanitize_title($author);
2259 2182 $nice_author = apply_filters('pre_user_nicename', $nice_author);
2260 2183
2261 - $reg_author = esc_sql(preg_quote($author ?: "(unknown)"));
2184 + $reg_author = esc_sql(preg_quote($author));
2262 2185 $author = esc_sql($author);
2263 2186 $email = esc_sql($email);
2264 2187 $test_email = esc_sql($test_email);
2265 2188 $authorUrl = esc_sql($authorUrl);
@@ -2273,11 +2196,9 @@
2273 2196 $author_rule = NULL;
2274 2197 endif;
2275 2198
2276 2199 // 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)) :
2200 + if (is_numeric($author_rule) and get_userdata((int) $author_rule)) :
2280 2201 $id = (int) $author_rule;
2281 2202
2282 2203 // User name is filtered out
2283 2204 elseif ('filter' == $author_rule) :
@@ -2318,11 +2239,11 @@
2318 2239 if (is_null($id)) :
2319 2240 if ($unfamiliar_author === 'create') :
2320 2241 $userdata = array();
2321 2242
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.)
2243 + // WordPress 3 is going to pitch a fit if we attempt to register
2244 + // more than one user account with an empty e-mail address, so we
2245 + // need *something* here. Ugh.
2325 2246 if (strlen($email) == 0 or FeedWordPress::is_null_email($email)) :
2326 2247 $url = parse_url($hostUrl);
2327 2248 $email = $nice_author.'@'.$url['host'];
2328 2249 endif;
@@ -2334,20 +2255,15 @@
2334 2255 $userdata['user_pass'] = substr(md5(uniqid(microtime())), 0, 6); // just something random to lock it up
2335 2256 $userdata['user_email'] = $email;
2336 2257 $userdata['user_url'] = $authorUrl;
2337 2258 $userdata['nickname'] = $author;
2338 -
2339 2259 $parts = preg_split('/\s+/', trim($author), 2);
2340 2260 if (isset($parts[0])) : $userdata['first_name'] = $parts[0]; endif;
2341 2261 if (isset($parts[1])) : $userdata['last_name'] = $parts[1]; endif;
2342 -
2343 2262 $userdata['display_name'] = $author;
2344 2263 $userdata['role'] = 'contributor';
2345 2264
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 {
2265 + do { // Keep trying until you get it right. Or until PHP crashes, I guess.
2350 2266 $id = wp_insert_user($userdata);
2351 2267 if (is_wp_error($id)) :
2352 2268 $codes = $id->get_error_code();
2353 2269 switch ($codes) :
@@ -2352,23 +2268,17 @@
2352 2268 $codes = $id->get_error_code();
2353 2269 switch ($codes) :
2354 2270 case 'empty_user_login' :
2355 2271 case 'existing_user_login' :
2356 - case 'invalid_username' :
2357 2272 // Add a random disambiguator
2358 2273 $userdata['user_login'] .= substr(md5(uniqid(microtime())), 0, 6);
2359 2274 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 2275 case 'user_nicename_too_long' :
2366 - // Add a limited 50 characters user_nicename based on user_login
2276 + // Add a limited 50 caracters user_nicename based on user_login
2367 2277 $userdata['user_nicename'] = mb_substr( $userdata['user_login'], 0, 50 );
2368 2278 break;
2369 2279 case 'existing_user_email' :
2370 - // Disassemble email for username, host
2280 + // No disassemble!
2371 2281 $parts = explode('@', $userdata['user_email'], 2);
2372 2282
2373 2283 // Add a random disambiguator as a gmail-style username extension
2374 2284 $parts[0] .= '+'.substr(md5(uniqid(microtime())), 0, 6);
@@ -2375,39 +2285,14 @@
2375 2285
2376 2286 // Reassemble
2377 2287 $userdata['user_email'] = $parts[0].'@'.$parts[1];
2378 2288 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 2289 endswitch;
2392 2290 endif;
2393 - $insanity = $insanity + 1;
2394 2291 } 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 ) ) :
2292 + elseif (is_numeric($unfamiliar_author) and get_userdata((int) $unfamiliar_author)) :
2408 2293 $id = (int) $unfamiliar_author;
2409 - elseif ( $unfamiliar_author === 'default' ) :
2294 + elseif ($unfamiliar_author === 'default') :
2410 2295 $id = 1;
2411 2296 endif;
2412 2297 endif;
2413 2298 endif;