| @@ -1,103 +1,150 @@ | ||
| 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'); | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * class SyndicatedPost: FeedWordPress uses to manage the conversion of | |
| 8 | + * incoming items from the feed parser into posts for the WordPress | |
| 9 | + * database. It contains several internal management methods primarily | |
| 10 | + * of interest to someone working on the FeedWordPress source, as well | |
| 11 | + * as some utility methods for extracting useful data from many | |
| 12 | + * different feed formats, which may be useful to FeedWordPress users | |
| 13 | + * who make use of feed data in PHP add-ons and filters. | |
| 14 | + * | |
| 15 | + * @version 2017.1004 | |
| 16 | + */ | |
| 2 | 17 | class SyndicatedPost { |
| 3 | - var $item = null; | |
| 4 | - | |
| 18 | + var $item = null; // MagpieRSS representation | |
| 19 | + var $entry = null; // SimplePie_Item representation | |
| 20 | + | |
| 5 | 21 | var $link = null; |
| 6 | 22 | var $feed = null; |
| 7 | 23 | var $feedmeta = null; |
| 8 | - | |
| 24 | + | |
| 25 | + var $xmlns = array (); | |
| 26 | + | |
| 9 | 27 | var $post = array (); |
| 10 | 28 | |
| 29 | + var $named = array (); | |
| 30 | + var $preset_terms = array (); | |
| 31 | + var $feed_terms = array (); | |
| 32 | + | |
| 11 | 33 | var $_freshness = null; |
| 12 | 34 | var $_wp_id = null; |
| 35 | + var $_wp_post = null; | |
| 13 | 36 | |
| 14 | - function SyndicatedPost ($item, $link) { | |
| 37 | + /** | |
| 38 | + * SyndicatedPost constructor: Given a feed item and the source from | |
| 39 | + * which it was taken, prepare a post that can be inserted into the | |
| 40 | + * WordPress database on request, or updated in place if it has already | |
| 41 | + * been syndicated. | |
| 42 | + * | |
| 43 | + * @param array $item The item syndicated from the feed. | |
| 44 | + * @param SyndicatedLink $source The feed it was syndicated from. | |
| 45 | + */ | |
| 46 | + function __construct ($item, &$source) { | |
| 15 | 47 | global $wpdb; |
| 16 | 48 | |
| 17 | - $this->link = $link; | |
| 18 | - $feedmeta = $link->settings; | |
| 19 | - $feed = $link->magpie; | |
| 49 | + if ( empty($item) && empty($source) ) | |
| 50 | + return; | |
| 20 | 51 | |
| 21 | - # This is ugly as all hell. I'd like to use apply_filters()'s | |
| 22 | - # alleged support for a variable argument count, but this seems | |
| 23 | - # to have been broken in WordPress 1.5. It'll be fixed somehow | |
| 24 | - # in WP 1.5.1, but I'm aiming at WP 1.5 compatibility across | |
| 25 | - # the board here. | |
| 26 | - # | |
| 27 | - # Cf.: <http://mosquito.wordpress.org/view.php?id=901> | |
| 28 | - global $fwp_channel, $fwp_feedmeta; | |
| 29 | - $fwp_channel = $feed; $fwp_feedmeta = $feedmeta; | |
| 52 | + if (is_array($item) | |
| 53 | + and isset($item['simplepie']) | |
| 54 | + and isset($item['magpie'])) : | |
| 55 | + $this->entry = $item['simplepie']; | |
| 56 | + $this->item = $item['magpie']; | |
| 57 | + $item = $item['magpie']; | |
| 58 | + elseif (is_a($item, 'SimplePie_Item')) : | |
| 59 | + $this->entry = $item; | |
| 30 | 60 | |
| 31 | - $this->feed = $feed; | |
| 32 | - $this->feedmeta = $feedmeta; | |
| 61 | + // convert to Magpie for compat purposes | |
| 62 | + $mp = new MagpieFromSimplePie($source->simplepie, $this->entry); | |
| 63 | + $this->item = $mp->get_item(); | |
| 33 | 64 | |
| 34 | - $this->item = $item; | |
| 35 | - $this->item = apply_filters('syndicated_item', $this->item, $this); | |
| 65 | + // done with conversion object | |
| 66 | + $mp = NULL; unset($mp); | |
| 67 | + else : | |
| 68 | + $this->item = $item; | |
| 69 | + endif; | |
| 36 | 70 | |
| 71 | + $this->link =& $source; | |
| 72 | + $this->feed = $source->magpie; | |
| 73 | + $this->feedmeta = $source->settings; | |
| 74 | + | |
| 75 | + FeedWordPress::diagnostic('feed_items', 'Considering item ['.$this->guid().'] "'.$this->entry->get_title().'"'); | |
| 76 | + | |
| 77 | + # Dealing with namespaces can get so fucking fucked. | |
| 78 | + $this->xmlns['forward'] = $source->magpie->_XMLNS_FAMILIAR; | |
| 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(); | |
| 83 | + endif; | |
| 84 | + $this->xmlns['reverse'][$ns][] = $url; | |
| 85 | + endforeach; | |
| 86 | + | |
| 87 | + // Fucking SimplePie. | |
| 88 | + $this->xmlns['reverse']['rss'][] = ''; | |
| 89 | + | |
| 90 | + // Trigger global syndicated_item filter. | |
| 91 | + $changed = apply_filters('syndicated_item', $this->item, $this); | |
| 92 | + $this->item = $changed; | |
| 93 | + | |
| 94 | + // Allow for feed-specific syndicated_item filters. | |
| 95 | + $changed = apply_filters( | |
| 96 | + "syndicated_item_".$source->uri(), | |
| 97 | + $this->item, | |
| 98 | + $this | |
| 99 | + ); | |
| 100 | + $this->item = $changed; | |
| 101 | + | |
| 37 | 102 | # Filters can halt further processing by returning NULL |
| 38 | 103 | if (is_null($this->item)) : |
| 39 | 104 | $this->post = NULL; |
| 40 | 105 | else : |
| 41 | - # Note that nothing is run through $wpdb->escape() here. | |
| 106 | + | |
| 107 | + # Note that nothing is run through esc_sql() here. | |
| 42 | 108 | # That's deliberate. The escaping is done at the point |
| 43 | 109 | # of insertion, not here, to avoid double-escaping and |
| 44 | 110 | # to avoid screwing with syndicated_post filters |
| 45 | 111 | |
| 46 | - $this->post['post_title'] = apply_filters('syndicated_item_title', $this->item['title'], $this); | |
| 112 | + $this->post['post_title'] = apply_filters( | |
| 113 | + 'syndicated_item_title', | |
| 114 | + $this->entry->get_title(), $this | |
| 115 | + ); | |
| 47 | 116 | |
| 48 | - // This just gives us an alphanumeric representation of | |
| 49 | - // the author. We will look up (or create) the numeric | |
| 50 | - // ID for the author in SyndicatedPost::add() | |
| 51 | - $this->post['named']['author'] = apply_filters('syndicated_item_author', $this->author(), $this); | |
| 52 | 117 | |
| 53 | - # Identify content and sanitize it. | |
| 54 | - # --------------------------------- | |
| 55 | - if (isset($this->item['atom_content'])) : | |
| 56 | - $content = $this->item['atom_content']; | |
| 57 | - elseif (isset($this->item['xhtml']['body'])) : | |
| 58 | - $content = $this->item['xhtml']['body']; | |
| 59 | - elseif (isset($this->item['xhtml']['div'])) : | |
| 60 | - $content = $this->item['xhtml']['div']; | |
| 61 | - elseif (isset($this->item['content']['encoded']) and $this->item['content']['encoded']): | |
| 62 | - $content = $this->item['content']['encoded']; | |
| 63 | - else: | |
| 64 | - $content = $this->item['description']; | |
| 65 | - endif; | |
| 66 | - $this->post['post_content'] = apply_filters('syndicated_item_content', $content, $this); | |
| 118 | + $this->named['author'] = apply_filters( | |
| 119 | + 'syndicated_item_author', | |
| 120 | + $this->author(), $this | |
| 121 | + ); | |
| 122 | + // This just gives us an alphanumeric name for the author. | |
| 123 | + // We look up (or create) the numeric ID for the author | |
| 124 | + // in SyndicatedPost::add(). | |
| 67 | 125 | |
| 68 | - # Identify and sanitize excerpt | |
| 69 | - $excerpt = NULL; | |
| 70 | - if ( isset($this->item['description']) and $this->item['description'] ) : | |
| 71 | - $excerpt = $this->item['description']; | |
| 72 | - elseif ( isset($content) and $content ) : | |
| 73 | - $excerpt = strip_tags($content); | |
| 74 | - if (strlen($excerpt) > 255) : | |
| 75 | - $excerpt = substr($excerpt,0,252).'...'; | |
| 76 | - endif; | |
| 77 | - endif; | |
| 78 | - $excerpt = apply_filters('syndicated_item_excerpt', $excerpt, $this); | |
| 126 | + $this->post['post_content'] = apply_filters( | |
| 127 | + 'syndicated_item_content', | |
| 128 | + $this->content(), $this | |
| 129 | + ); | |
| 130 | + | |
| 131 | + $excerpt = apply_filters('syndicated_item_excerpt', $this->excerpt(), $this); | |
| 79 | 132 | |
| 80 | - if (!is_null($excerpt)): | |
| 133 | + if (!empty($excerpt)): | |
| 81 | 134 | $this->post['post_excerpt'] = $excerpt; |
| 82 | 135 | endif; |
| 83 | - | |
| 84 | - // This is unnecessary if we use wp_insert_post | |
| 85 | - if (!$this->use_api('wp_insert_post')) : | |
| 86 | - $this->post['post_name'] = sanitize_title($this->post['post_title']); | |
| 87 | - endif; | |
| 88 | 136 | |
| 89 | - $this->post['epoch']['issued'] = apply_filters('syndicated_item_published', $this->published(), $this); | |
| 90 | - $this->post['epoch']['created'] = apply_filters('syndicated_item_created', $this->created(), $this); | |
| 91 | - $this->post['epoch']['modified'] = apply_filters('syndicated_item_updated', $this->updated(), $this); | |
| 92 | - | |
| 93 | 137 | // Dealing with timestamps in WordPress is so fucking fucked. |
| 94 | 138 | $offset = (int) get_option('gmt_offset') * 60 * 60; |
| 95 | - $this->post['post_date'] = gmdate('Y-m-d H:i:s', $this->published() + $offset); | |
| 96 | - $this->post['post_modified'] = gmdate('Y-m-d H:i:s', $this->updated() + $offset); | |
| 97 | - $this->post['post_date_gmt'] = gmdate('Y-m-d H:i:s', $this->published()); | |
| 98 | - $this->post['post_modified_gmt'] = gmdate('Y-m-d H:i:s', $this->updated()); | |
| 139 | + $post_date_gmt = $this->published(array('default' => -1)); | |
| 140 | + $post_modified_gmt = $this->updated(array('default' => -1)); | |
| 99 | 141 | |
| 142 | + $this->post['post_date_gmt'] = gmdate('Y-m-d H:i:s', $post_date_gmt); | |
| 143 | + $this->post['post_date'] = gmdate('Y-m-d H:i:s', $post_date_gmt + $offset); | |
| 144 | + $this->post['post_modified_gmt'] = gmdate('Y-m-d H:i:s', $post_modified_gmt); | |
| 145 | + $this->post['post_modified'] = gmdate('Y-m-d H:i:s', $post_modified_gmt + $offset); | |
| 146 | + | |
| 100 | 147 | // Use feed-level preferences or the global default. |
| 101 | 148 | $this->post['post_status'] = $this->link->syndicated_status('post', 'publish'); |
| 102 | 149 | $this->post['comment_status'] = $this->link->syndicated_status('comment', 'closed'); |
| 103 | 150 | $this->post['ping_status'] = $this->link->syndicated_status('ping', 'closed'); |
| @@ -104,210 +151,1304 @@ | ||
| 104 | 151 | |
| 105 | 152 | // Unique ID (hopefully a unique tag: URI); failing that, the permalink |
| 106 | 153 | $this->post['guid'] = apply_filters('syndicated_item_guid', $this->guid(), $this); |
| 107 | 154 | |
| 108 | - // User-supplied custom settings to apply to each post. Do first so that FWP-generated custom settings will overwrite if necessary; thus preventing any munging | |
| 109 | - $default_custom_settings = get_option('feedwordpress_custom_settings'); | |
| 110 | - if ($default_custom_settings and !is_array($default_custom_settings)) : | |
| 111 | - $default_custom_settings = unserialize($default_custom_settings); | |
| 155 | + // User-supplied custom settings to apply to each post. | |
| 156 | + // Do first so that FWP-generated custom settings will | |
| 157 | + // overwrite if necessary; thus preventing any munging. | |
| 158 | + $postMetaIn = $this->link->postmeta(array("parsed" => true)); | |
| 159 | + $postMetaOut = array(); | |
| 160 | + | |
| 161 | + foreach ($postMetaIn as $key => $meta) : | |
| 162 | + $postMetaOut[$key] = $meta->do_substitutions($this); | |
| 163 | + endforeach; | |
| 164 | + | |
| 165 | + foreach ($postMetaOut as $key => $values) : | |
| 166 | + $this->post['meta'][$key] = array(); | |
| 167 | + foreach ($values as $value) : | |
| 168 | + $this->post['meta'][$key][] = apply_filters("syndicated_post_meta_{$key}", $value, $this); | |
| 169 | + endforeach; | |
| 170 | + endforeach; | |
| 171 | + | |
| 172 | + // RSS 2.0 / Atom 1.0 enclosure support | |
| 173 | + $enclosures = $this->entry->get_enclosures(); | |
| 174 | + if (is_array($enclosures)) : foreach ($enclosures as $enclosure) : | |
| 175 | + $this->post['meta']['enclosure'][] = | |
| 176 | + apply_filters('syndicated_item_enclosure_url', $enclosure->get_link(), $this)."\n". | |
| 177 | + apply_filters('syndicated_item_enclosure_length', $enclosure->get_length(), $this)."\n". | |
| 178 | + apply_filters('syndicated_item_enclosure_type', $enclosure->get_type(), $this); | |
| 179 | + endforeach; endif; | |
| 180 | + | |
| 181 | + // In case you want to point back to the blog this was | |
| 182 | + // syndicated from. | |
| 183 | + | |
| 184 | + $sourcemeta['syndication_source'] = apply_filters( | |
| 185 | + 'syndicated_item_source_title', | |
| 186 | + $this->link->name(), | |
| 187 | + $this | |
| 188 | + ); | |
| 189 | + $sourcemeta['syndication_source_uri'] = apply_filters( | |
| 190 | + 'syndicated_item_source_link', | |
| 191 | + $this->link->homepage(), | |
| 192 | + $this | |
| 193 | + ); | |
| 194 | + $sourcemeta['syndication_source_id'] = apply_filters( | |
| 195 | + 'syndicated_item_source_id', | |
| 196 | + $this->link->guid(), | |
| 197 | + $this | |
| 198 | + ); | |
| 199 | + | |
| 200 | + // Make use of atom:source data, if present in an aggregated feed | |
| 201 | + $entry_source = $this->source(); | |
| 202 | + if (!is_null($entry_source)) : | |
| 203 | + foreach ($entry_source as $what => $value) : | |
| 204 | + if (!is_null($value)) : | |
| 205 | + if ($what=='title') : $key = 'syndication_source'; | |
| 206 | + elseif ($what=='feed') : $key = 'syndication_feed'; | |
| 207 | + else : $key = "syndication_source_${what}"; | |
| 208 | + endif; | |
| 209 | + | |
| 210 | + $sourcemeta["${key}_original"] = apply_filters( | |
| 211 | + 'syndicated_item_original_source_'.$what, | |
| 212 | + $value, | |
| 213 | + $this | |
| 214 | + ); | |
| 215 | + endif; | |
| 216 | + endforeach; | |
| 112 | 217 | endif; |
| 113 | - if (!is_array($default_custom_settings)) : | |
| 114 | - $default_custom_settings = array(); | |
| 218 | + | |
| 219 | + foreach ($sourcemeta as $meta_key => $value) : | |
| 220 | + if (!is_null($value)) : | |
| 221 | + $this->post['meta'][$meta_key] = $value; | |
| 222 | + endif; | |
| 223 | + endforeach; | |
| 224 | + | |
| 225 | + // Store information on human-readable and machine-readable comment URIs | |
| 226 | + | |
| 227 | + // Human-readable comment URI | |
| 228 | + $commentLink = apply_filters('syndicated_item_comments', $this->comment_link(), $this); | |
| 229 | + if (!is_null($commentLink)) : $this->post['meta']['rss:comments'] = $commentLink; endif; | |
| 230 | + | |
| 231 | + // Machine-readable content feed URI | |
| 232 | + $commentFeed = apply_filters('syndicated_item_commentrss', $this->comment_feed(), $this); | |
| 233 | + if (!is_null($commentFeed)) : $this->post['meta']['wfw:commentRSS'] = $commentFeed; endif; | |
| 234 | + // Yeah, yeah, now I know that it's supposed to be | |
| 235 | + // wfw:commentRss. Oh well. Path dependence, sucka. | |
| 236 | + | |
| 237 | + // Store information to identify the feed that this came from | |
| 238 | + if (isset($this->feedmeta['link/uri'])) : | |
| 239 | + $this->post['meta']['syndication_feed'] = $this->feedmeta['link/uri']; | |
| 115 | 240 | endif; |
| 116 | - | |
| 117 | - $custom_settings = (isset($this->link->settings['postmeta']) ? $this->link->settings['postmeta'] : null); | |
| 118 | - if ($custom_settings and !is_array($custom_settings)) : | |
| 119 | - $custom_settings = unserialize($custom_settings); | |
| 241 | + if (isset($this->feedmeta['link/id'])) : | |
| 242 | + $this->post['meta']['syndication_feed_id'] = $this->feedmeta['link/id']; | |
| 120 | 243 | endif; |
| 121 | - if (!is_array($custom_settings)) : | |
| 122 | - $custom_settings = array(); | |
| 244 | + | |
| 245 | + if (isset($this->item['source_link_self'])) : | |
| 246 | + $this->post['meta']['syndication_feed_original'] = $this->item['source_link_self']; | |
| 123 | 247 | endif; |
| 124 | - $this->post['meta'] = array_merge($default_custom_settings, $custom_settings); | |
| 125 | 248 | |
| 126 | - // RSS 2.0 / Atom 1.0 enclosure support | |
| 127 | - if ( isset($this->item['enclosure#']) ) : | |
| 128 | - for ($i = 1; $i <= $this->item['enclosure#']; $i++) : | |
| 129 | - $eid = (($i > 1) ? "#{$id}" : ""); | |
| 130 | - $this->post['meta']['enclosure'][] = | |
| 131 | - apply_filters('syndicated_item_enclosure_url', $this->item["enclosure{$eid}@url"], $this)."\n". | |
| 132 | - apply_filters('syndicated_item_enclosure_length', $this->item["enclosure{$eid}@length"], $this)."\n". | |
| 133 | - apply_filters('syndicated_item_enclosure_type', $this->item["enclosure{$eid}@type"], $this); | |
| 134 | - endfor; | |
| 249 | + // In case you want to know the external permalink... | |
| 250 | + $this->post['meta']['syndication_permalink'] = apply_filters('syndicated_item_link', $this->permalink()); | |
| 251 | + | |
| 252 | + // Store a hash of the post content for checking whether something needs to be updated | |
| 253 | + $this->post['meta']['syndication_item_hash'] = $this->update_hash(); | |
| 254 | + | |
| 255 | + // Categories, Tags, and other Terms: from settings assignments (global settings, subscription settings), | |
| 256 | + // and from feed assignments (item metadata, post content) | |
| 257 | + $this->preset_terms = apply_filters('syndicated_item_preset_terms', $this->get_terms_from_settings(), $this); | |
| 258 | + $this->feed_terms = apply_filters('syndicated_item_feed_terms', $this->get_terms_from_feeds(), $this); | |
| 259 | + | |
| 260 | + $this->post['post_type'] = apply_filters('syndicated_post_type', $this->link->setting('syndicated post type', 'syndicated_post_type', 'post'), $this); | |
| 261 | + endif; | |
| 262 | + } /* SyndicatedPost::__construct() */ | |
| 263 | + | |
| 264 | + ##################################### | |
| 265 | + #### EXTRACT DATA FROM FEED ITEM #### | |
| 266 | + ##################################### | |
| 267 | + | |
| 268 | + function substitution_function ($name) { | |
| 269 | + $ret = NULL; | |
| 270 | + | |
| 271 | + switch ($name) : | |
| 272 | + // Allowed PHP string functions | |
| 273 | + case 'trim': | |
| 274 | + case 'ltrim': | |
| 275 | + case 'rtrim': | |
| 276 | + case 'strtoupper': | |
| 277 | + case 'strtolower': | |
| 278 | + case 'urlencode': | |
| 279 | + case 'urldecode': | |
| 280 | + $ret = $name; | |
| 281 | + endswitch; | |
| 282 | + return $ret; | |
| 283 | + } | |
| 284 | + | |
| 285 | + /** | |
| 286 | + * SyndicatedPost::query uses an XPath-like syntax to query arbitrary | |
| 287 | + * elements within the syndicated item. | |
| 288 | + * | |
| 289 | + * @param string $path | |
| 290 | + * @returns array of string values representing contents of matching | |
| 291 | + * elements or attributes | |
| 292 | + */ | |
| 293 | + function query ($path) { | |
| 294 | + $xq = new SyndicatedPostXPathQuery(array("path" => $path)); | |
| 295 | + | |
| 296 | + $feedChannel = array_merge( | |
| 297 | + $this->get_feed_root_element(), | |
| 298 | + $this->get_feed_channel_elements() | |
| 299 | + ); | |
| 300 | + | |
| 301 | + $matches = $xq->match(array( | |
| 302 | + "type" => $this->link->simplepie->get_type(), | |
| 303 | + "xmlns" => $this->xmlns, | |
| 304 | + "map" => array( | |
| 305 | + "/" => array($this->entry->data), | |
| 306 | + "item" => array($this->entry->data), | |
| 307 | + "feed" => $feedChannel, | |
| 308 | + "channel" => $feedChannel | |
| 309 | + ), | |
| 310 | + "context" => $this->entry->data, | |
| 311 | + "parent" => $feedChannel, | |
| 312 | + )); | |
| 313 | + | |
| 314 | + return $matches; | |
| 315 | + } /* SyndicatedPost::query() */ | |
| 316 | + | |
| 317 | + function get_feed_root_element () { | |
| 318 | + $matches = array(); | |
| 319 | + foreach ($this->link->simplepie->data['child'] as $ns => $root) : | |
| 320 | + foreach ($root as $element => $data) : | |
| 321 | + $matches = array_merge($matches, $data); | |
| 322 | + endforeach; | |
| 323 | + endforeach; | |
| 324 | + return $matches; | |
| 325 | + } /* SyndicatedPost::get_feed_root_element() */ | |
| 326 | + | |
| 327 | + function get_feed_channel_elements () { | |
| 328 | + $rss = array( | |
| 329 | + SIMPLEPIE_NAMESPACE_RSS_090, | |
| 330 | + SIMPLEPIE_NAMESPACE_RSS_10, | |
| 331 | + 'http://backend.userland.com/RSS2', | |
| 332 | + SIMPLEPIE_NAMESPACE_RSS_20, | |
| 333 | + ); | |
| 334 | + | |
| 335 | + $matches = array(); | |
| 336 | + foreach ($rss as $ns) : | |
| 337 | + $data = $this->link->simplepie->get_feed_tags($ns, 'channel'); | |
| 338 | + if (!is_null($data)) : | |
| 339 | + $matches = array_merge($matches, $data); | |
| 135 | 340 | endif; |
| 341 | + endforeach; | |
| 342 | + return $matches; | |
| 343 | + } /* SyndicatedPost::get_feed_channel_elements() */ | |
| 136 | 344 | |
| 137 | - // In case you want to point back to the blog this was syndicated from | |
| 138 | - if (isset($this->feed->channel['title'])) : | |
| 139 | - $this->post['meta']['syndication_source'] = apply_filters('syndicated_item_source_title', $this->feed->channel['title'], $this); | |
| 140 | - endif; | |
| 345 | + function get_categories ($params = array()) { | |
| 346 | + return $this->entry->get_categories(); | |
| 347 | + } | |
| 348 | + | |
| 349 | + function title ($params = array()) { | |
| 350 | + return $this->entry->get_title(); | |
| 351 | + } /* SyndicatedPost::title () */ | |
| 352 | + | |
| 353 | + function content ($params = array()) { | |
| 141 | 354 | |
| 142 | - if (isset($this->feed->channel['link'])) : | |
| 143 | - $this->post['meta']['syndication_source_uri'] = apply_filters('syndicated_item_source_link', $this->feed->channel['link'], $this); | |
| 355 | + $params = wp_parse_args($params, array( | |
| 356 | + "full only" => false, | |
| 357 | + )); | |
| 358 | + | |
| 359 | + $content = NULL; | |
| 360 | + | |
| 361 | + // FIXME: This is one of the main places in the code still using | |
| 362 | + // the outmoded SimplePie - to - Magpie construction. We could | |
| 363 | + // replace using SimplePie_Item::get_tags() here. (Or if really | |
| 364 | + // ambitious we could attempt to just use | |
| 365 | + // SimplePie_Item::get_content() with content-only set to TRUE | |
| 366 | + // and some sanitization in effect. -CJ 1jul14 | |
| 367 | + | |
| 368 | + // atom:content, standard method of providing full HTML content | |
| 369 | + // in Atom feeds. | |
| 370 | + if (isset($this->item['atom_content'])) : | |
| 371 | + $content = $this->item['atom_content']; | |
| 372 | + elseif (isset($this->item['atom']['atom_content'])) : | |
| 373 | + $content = $this->item['atom']['atom_content']; | |
| 374 | + | |
| 375 | + // Some exotics: back in the day, before widespread convergence | |
| 376 | + // on content:encoding, some RSS feeds took advantage of XML | |
| 377 | + // namespaces to use an inline xhtml:body or xhtml:div element | |
| 378 | + // for full-content HTML. (E.g. Sam Ruby's feed, IIRC.) | |
| 379 | + elseif (isset($this->item['xhtml']['body'])) : | |
| 380 | + $content = $this->item['xhtml']['body']; | |
| 381 | + elseif (isset($this->item['xhtml']['div'])) : | |
| 382 | + $content = $this->item['xhtml']['div']; | |
| 383 | + | |
| 384 | + // content:encoded, most common method of providing full HTML in | |
| 385 | + // RSS 2.0 feeds. | |
| 386 | + elseif (isset($this->item['content']['encoded']) and $this->item['content']['encoded']): | |
| 387 | + $content = $this->item['content']['encoded']; | |
| 388 | + | |
| 389 | + // Fall back on elements that sometimes may contain full HTML | |
| 390 | + // but sometimes not. | |
| 391 | + elseif (!$params['full only']) : | |
| 392 | + | |
| 393 | + // description element is sometimes used for full HTML | |
| 394 | + // sometimes for summary text in RSS. (By the letter of | |
| 395 | + // the standard, it should just be for summary text.) | |
| 396 | + if (isset($this->item['description'])) : | |
| 397 | + $content = $this->item['description']; | |
| 144 | 398 | endif; |
| 145 | 399 | |
| 146 | - // Make use of atom:source data, if present in an aggregated feed | |
| 147 | - if (isset($this->item['source_title'])) : | |
| 148 | - $this->post['meta']['syndication_source_original'] = $this->item['source_title']; | |
| 400 | + endif; | |
| 401 | + | |
| 402 | + return $content; | |
| 403 | + } /* SyndicatedPost::content() */ | |
| 404 | + | |
| 405 | + function excerpt () { | |
| 406 | + # Identify and sanitize excerpt: atom:summary, or rss:description | |
| 407 | + $excerpt = $this->entry->get_description(); | |
| 408 | + | |
| 409 | + # Many RSS feeds use rss:description, inadvisably, to | |
| 410 | + # carry the entire post (typically with escaped HTML). | |
| 411 | + # If that's what happened, we don't want the full | |
| 412 | + # content for the excerpt. | |
| 413 | + $content = $this->content(); | |
| 414 | + | |
| 415 | + // Ignore whitespace, case, and tag cruft. | |
| 416 | + $theExcerpt = preg_replace('/\s+/', '', strtolower(strip_tags(html_entity_decode($excerpt)))); | |
| 417 | + $theContent = preg_replace('/\s+/', '', strtolower(strip_tags(html_entity_decode($content)))); | |
| 418 | + if ( empty($excerpt) or $theExcerpt == $theContent ) : | |
| 419 | + # If content is available, generate an excerpt. | |
| 420 | + if ( strlen(trim($content)) > 0 ) : | |
| 421 | + $excerpt = strip_tags($content); | |
| 422 | + if (strlen($excerpt) > 255) : | |
| 423 | + if (is_object($this->link) and is_object($this->link->simplepie)) : | |
| 424 | + $encoding = $this->link->simplepie->get_encoding(); | |
| 425 | + else : | |
| 426 | + $encoding = get_option('blog_charset', 'utf8'); | |
| 427 | + endif; | |
| 428 | + $excerpt = mb_substr($excerpt,0,252,$encoding).'...'; | |
| 429 | + endif; | |
| 149 | 430 | endif; |
| 431 | + endif; | |
| 150 | 432 | |
| 151 | - if (isset($this->item['source_link'])) : | |
| 152 | - $this->post['meta']['syndication_source_uri_original'] = $this->item['source_link']; | |
| 433 | + return $excerpt; | |
| 434 | + } /* SyndicatedPost::excerpt() */ | |
| 435 | + | |
| 436 | + function permalink () { | |
| 437 | + // Handles explicit <link> elements and also RSS 2.0 cases with | |
| 438 | + // <guid isPermaLink="true">, etc. Hooray! | |
| 439 | + $permalink = $this->entry->get_link(); | |
| 440 | + return $permalink; | |
| 441 | + } | |
| 442 | + | |
| 443 | + function created ($params = array()) { | |
| 444 | + $unfiltered = false; $default = NULL; | |
| 445 | + extract($params); | |
| 446 | + | |
| 447 | + $date = ''; | |
| 448 | + if (isset($this->item['dc']['created'])) : | |
| 449 | + $date = $this->item['dc']['created']; | |
| 450 | + elseif (isset($this->item['dcterms']['created'])) : | |
| 451 | + $date = $this->item['dcterms']['created']; | |
| 452 | + elseif (isset($this->item['created'])): // Atom 0.3 | |
| 453 | + $date = $this->item['created']; | |
| 454 | + endif; | |
| 455 | + | |
| 456 | + $time = new FeedTime($date); | |
| 457 | + $ts = $time->timestamp(); | |
| 458 | + if (!$unfiltered) : | |
| 459 | + apply_filters('syndicated_item_created', $ts, $this); | |
| 460 | + endif; | |
| 461 | + return $ts; | |
| 462 | + } /* SyndicatedPost::created() */ | |
| 463 | + | |
| 464 | + function published ($params = array(), $default = NULL) { | |
| 465 | + $fallback = true; $unfiltered = false; | |
| 466 | + if (!is_array($params)) : // Old style | |
| 467 | + $fallback = $params; | |
| 468 | + else : // New style | |
| 469 | + extract($params); | |
| 470 | + endif; | |
| 471 | + | |
| 472 | + $date = ''; | |
| 473 | + $ts = null; | |
| 474 | + | |
| 475 | + # RSS is a fucking mess. Figure out whether we have a date in | |
| 476 | + # <dc:date>, <issued>, <pubDate>, etc., and get it into Unix | |
| 477 | + # epoch format for reformatting. If we can't find anything, | |
| 478 | + # we'll use the last-updated time. | |
| 479 | + if (isset($this->item['dc']['date'])): // Dublin Core | |
| 480 | + $date = $this->item['dc']['date']; | |
| 481 | + elseif (isset($this->item['dcterms']['issued'])) : // Dublin Core extensions | |
| 482 | + $date = $this->item['dcterms']['issued']; | |
| 483 | + elseif (isset($this->item['published'])) : // Atom 1.0 | |
| 484 | + $date = $this->item['published']; | |
| 485 | + elseif (isset($this->item['issued'])): // Atom 0.3 | |
| 486 | + $date = $this->item['issued']; | |
| 487 | + elseif (isset($this->item['pubdate'])): // RSS 2.0 | |
| 488 | + $date = $this->item['pubdate']; | |
| 489 | + endif; | |
| 490 | + | |
| 491 | + if (strlen($date) > 0) : | |
| 492 | + $time = new FeedTime($date); | |
| 493 | + $ts = $time->timestamp(); | |
| 494 | + elseif ($fallback) : // Fall back to <updated> / <modified> if present | |
| 495 | + $ts = $this->updated(/*fallback=*/ false, /*default=*/ $default); | |
| 496 | + endif; | |
| 497 | + | |
| 498 | + # If everything failed, then default to the current time. | |
| 499 | + if (is_null($ts)) : | |
| 500 | + if (-1 == $default) : | |
| 501 | + $ts = time(); | |
| 502 | + else : | |
| 503 | + $ts = $default; | |
| 153 | 504 | endif; |
| 154 | - | |
| 155 | - if (isset($this->item['source_id'])) : | |
| 156 | - $this->post['meta']['syndication_source_id_original'] = $this->item['source_id']; | |
| 505 | + endif; | |
| 506 | + | |
| 507 | + if (!$unfiltered) : | |
| 508 | + $ts = apply_filters('syndicated_item_published', $ts, $this); | |
| 509 | + endif; | |
| 510 | + return $ts; | |
| 511 | + } /* SyndicatedPost::published() */ | |
| 512 | + | |
| 513 | + function updated ($params = array(), $default = -1) { | |
| 514 | + $fallback = true; $unfiltered = false; | |
| 515 | + if (!is_array($params)) : // Old style | |
| 516 | + $fallback = $params; | |
| 517 | + else : // New style | |
| 518 | + extract($params); | |
| 519 | + endif; | |
| 520 | + | |
| 521 | + $date = ''; | |
| 522 | + $ts = null; | |
| 523 | + | |
| 524 | + # As far as I know, only dcterms and Atom have reliable ways to | |
| 525 | + # specify when something was *modified* last. If neither is | |
| 526 | + # available, then we'll try to get the time of publication. | |
| 527 | + if (isset($this->item['dc']['modified'])) : // Not really correct | |
| 528 | + $date = $this->item['dc']['modified']; | |
| 529 | + elseif (isset($this->item['dcterms']['modified'])) : // Dublin Core extensions | |
| 530 | + $date = $this->item['dcterms']['modified']; | |
| 531 | + elseif (isset($this->item['modified'])): // Atom 0.3 | |
| 532 | + $date = $this->item['modified']; | |
| 533 | + elseif (isset($this->item['updated'])): // Atom 1.0 | |
| 534 | + $date = $this->item['updated']; | |
| 535 | + endif; | |
| 536 | + | |
| 537 | + if (strlen($date) > 0) : | |
| 538 | + $time = new FeedTime($date); | |
| 539 | + $ts = $time->timestamp(); | |
| 540 | + elseif ($fallback) : // Fall back to issued / dc:date | |
| 541 | + $ts = $this->published(/*fallback=*/ false, /*default=*/ $default); | |
| 542 | + endif; | |
| 543 | + | |
| 544 | + # If everything failed, then default to the current time. | |
| 545 | + if (is_null($ts)) : | |
| 546 | + if (-1 == $default) : | |
| 547 | + $ts = time(); | |
| 548 | + else : | |
| 549 | + $ts = $default; | |
| 157 | 550 | endif; |
| 551 | + endif; | |
| 158 | 552 | |
| 159 | - // Store information on human-readable and machine-readable comment URIs | |
| 160 | - if (isset($this->item['comments'])) : | |
| 161 | - $this->post['meta']['rss:comments'] = apply_filters('syndicated_item_comments', $this->item['comments']); | |
| 553 | + if (!$unfiltered) : | |
| 554 | + apply_filters('syndicated_item_updated', $ts, $this); | |
| 555 | + endif; | |
| 556 | + return $ts; | |
| 557 | + } /* SyndicatedPost::updated() */ | |
| 558 | + | |
| 559 | + var $_hashes = array(); | |
| 560 | + function stored_hashes ($id = NULL) { | |
| 561 | + if (is_null($id)) : | |
| 562 | + $id = $this->wp_id(); | |
| 563 | + endif; | |
| 564 | + | |
| 565 | + if (!isset($this->_hashes[$id])) : | |
| 566 | + $this->_hashes[$id] = get_post_custom_values( | |
| 567 | + 'syndication_item_hash', $id | |
| 568 | + ); | |
| 569 | + if (is_null($this->_hashes[$id])) : | |
| 570 | + $this->_hashes[$id] = array(); | |
| 162 | 571 | endif; |
| 163 | - | |
| 164 | - // RSS 2.0 comment feeds extension | |
| 165 | - if (isset($this->item['wfw']['commentrss'])) : | |
| 166 | - $this->post['meta']['wfw:commentRSS'] = apply_filters('syndicated_item_commentrss', $this->item['wfw']['commentrss']); | |
| 572 | + endif; | |
| 573 | + return $this->_hashes[$id]; | |
| 574 | + } | |
| 575 | + | |
| 576 | + function update_hash ($hashed = true) { | |
| 577 | + // Basis for tracking possible changes to item. | |
| 578 | + $hash = array( | |
| 579 | + "title" => $this->entry->get_title(), | |
| 580 | + "link" => $this->permalink(), | |
| 581 | + "content" => $this->content(), | |
| 582 | + "excerpt" => $this->excerpt(), | |
| 583 | + ); | |
| 584 | + | |
| 585 | + if ($hashed) : | |
| 586 | + $hash = md5(serialize($hash)); | |
| 587 | + endif; | |
| 588 | + | |
| 589 | + return $hash; | |
| 590 | + } /* SyndicatedPost::update_hash() */ | |
| 591 | + | |
| 592 | + /** | |
| 593 | + * SyndicatedPost::normalize_guid_prefix(): generates a normalized URL | |
| 594 | + * prefix (including scheme, authority, full path, and the beginning of | |
| 595 | + * a query string) for creating guids that conform to WordPress's | |
| 596 | + * internal constraints on the URL space for valid guids. To create a | |
| 597 | + * normalized guid, just concatenate a valid URL query parameter value | |
| 598 | + * to the returned URL. | |
| 599 | + * | |
| 600 | + * @return string The URL prefix generated. | |
| 601 | + * | |
| 602 | + * @uses trailingslashit() | |
| 603 | + * @uses home_url() | |
| 604 | + * @uses apply_filters() | |
| 605 | + */ | |
| 606 | + static function normalize_guid_prefix () { | |
| 607 | + $url = trailingslashit(home_url(/*path=*/ '', /*scheme=*/ 'http')); | |
| 608 | + return apply_filters('syndicated_item_guid_normalized_prefix', $url . '?guid='); | |
| 609 | + } /* SyndicatedPost::normalize_guid_prefix() */ | |
| 610 | + | |
| 611 | + static function normalize_guid ($guid) { | |
| 612 | + $guid = trim($guid); | |
| 613 | + if (preg_match('/^[0-9a-z]{32}$/i', $guid)) : // MD5 | |
| 614 | + $guid = SyndicatedPost::normalize_guid_prefix().strtolower($guid); | |
| 615 | + elseif ((strlen(esc_url($guid)) == 0) or (esc_url($guid) != $guid)) : | |
| 616 | + $guid = SyndicatedPost::normalize_guid_prefix().md5($guid); | |
| 617 | + endif; | |
| 618 | + $guid = trim($guid); | |
| 619 | + | |
| 620 | + return $guid; | |
| 621 | + } /* SyndicatedPost::normalize_guid() */ | |
| 622 | + | |
| 623 | + static function alternative_guid_prefix () { | |
| 624 | + $url = trailingslashit(home_url(/*path=*/ '', /*scheme=*/ 'https')); | |
| 625 | + return apply_filters('syndicated_item_guid_normalized_prefix', $url . '?guid='); | |
| 626 | + } | |
| 627 | + static function alternative_guid ($guid) { | |
| 628 | + $guid = trim($guid); | |
| 629 | + if (preg_match('/^[0-9a-z]{32}$/i', $guid)) : // MD5 | |
| 630 | + $guid = SyndicatedPost::alternative_guid_prefix().strtolower($guid); | |
| 631 | + elseif ((strlen(esc_url($guid)) == 0) or (esc_url($guid) != $guid)) : | |
| 632 | + $guid = SyndicatedPost::alternative_guid_prefix().md5($guid); | |
| 633 | + endif; | |
| 634 | + $guid = trim($guid); | |
| 635 | + | |
| 636 | + return $guid; | |
| 637 | + } /* SyndicatedPost::normalize_guid() */ | |
| 638 | + | |
| 639 | + function guid () { | |
| 640 | + $guid = null; | |
| 641 | + if (isset($this->item['id'])): // Atom 0.3 / 1.0 | |
| 642 | + $guid = $this->item['id']; | |
| 643 | + elseif (isset($this->item['atom']['id'])) : // Namespaced Atom | |
| 644 | + $guid = $this->item['atom']['id']; | |
| 645 | + elseif (isset($this->item['guid'])) : // RSS 2.0 | |
| 646 | + $guid = $this->item['guid']; | |
| 647 | + elseif (isset($this->item['dc']['identifier'])) : // yeah, right | |
| 648 | + $guid = $this->item['dc']['identifier']; | |
| 649 | + endif; | |
| 650 | + | |
| 651 | + // Un-set or too long to use as-is. Generate a tag: URI. | |
| 652 | + if (is_null($guid) or strlen($guid) > 250) : | |
| 653 | + // In case we need to check this again | |
| 654 | + $original_guid = $guid; | |
| 655 | + | |
| 656 | + // The feed does not seem to have provided us with a | |
| 657 | + // usable unique identifier, so we'll have to cobble | |
| 658 | + // together a tag: URI that might work for us. The base | |
| 659 | + // of the URI will be the host name of the feed source ... | |
| 660 | + $bits = parse_url($this->link->uri()); | |
| 661 | + $guid = 'tag:'.$bits['host']; | |
| 662 | + | |
| 663 | + // Some ill-mannered feeds (for example, certain feeds | |
| 664 | + // coming from Google Calendar) have extraordinarily long | |
| 665 | + // guids -- so long that they exceed the 255 character | |
| 666 | + // width of the WordPress guid field. But if the string | |
| 667 | + // gets clipped by MySQL, uniqueness tests will fail | |
| 668 | + // forever after and the post will be endlessly | |
| 669 | + // reduplicated. So, instead, Guids Of A Certain Length | |
| 670 | + // are hashed down into a nice, manageable tag: URI. | |
| 671 | + if (!is_null($original_guid)) : | |
| 672 | + $guid .= ',2010-12-03:id.'.md5($original_guid); | |
| 673 | + | |
| 674 | + // If we have a date of creation, then we can use that | |
| 675 | + // to uniquely identify the item. (On the other hand, if | |
| 676 | + // the feed producer was consicentious enough to | |
| 677 | + // generate dates of creation, she probably also was | |
| 678 | + // conscientious enough to generate unique identifiers.) | |
| 679 | + elseif (!is_null($this->created())) : | |
| 680 | + $guid .= '://post.'.date('YmdHis', $this->created()); | |
| 681 | + | |
| 682 | + // Otherwise, use both the URI of the item, *and* the | |
| 683 | + // item's title. We have to use both because titles are | |
| 684 | + // often not unique, and sometimes links aren't unique | |
| 685 | + // either (e.g. Bitch (S)HITLIST, Mozilla Dot Org news, | |
| 686 | + // some podcasts). But it's rare to have *both* the same | |
| 687 | + // title *and* the same link for two different items. So | |
| 688 | + // this is about the best we can do. | |
| 689 | + else : | |
| 690 | + $link = $this->permalink(); | |
| 691 | + if (is_null($link)) : $link = $this->link->uri(); endif; | |
| 692 | + $guid .= '://'.md5($link.'/'.$this->item['title']); | |
| 167 | 693 | endif; |
| 694 | + endif; | |
| 695 | + return $guid; | |
| 696 | + } /* SyndicatedPost::guid() */ | |
| 168 | 697 | |
| 169 | - // Atom 1.0 comment feeds link-rel | |
| 170 | - if (isset($this->item['link_replies'])) : | |
| 171 | - // There may be multiple <link rel="replies"> elements; feeds have a feed MIME type | |
| 172 | - $N = isset($this->item['link_replies#']) ? $this->item['link_replies#'] : 1; | |
| 173 | - for ($i = 1; $i <= $N; $i++) : | |
| 174 | - $currentElement = 'link_replies'.(($i > 1) ? '#'.$i : ''); | |
| 175 | - if (isset($this->item[$currentElement.'@type']) | |
| 176 | - and preg_match("\007application/(atom|rss|rdf)\+xml\007i", $this->item[$currentElement.'@type'])) : | |
| 177 | - $this->post['meta']['wfw:commentRSS'] = apply_filters('syndicated_item_commentrss', $this->item[$currentElement]); | |
| 178 | - endif; | |
| 179 | - endfor; | |
| 180 | - endif; | |
| 698 | + function author () { | |
| 699 | + $author = array (); | |
| 181 | 700 | |
| 182 | - // Store information to identify the feed that this came from | |
| 183 | - $this->post['meta']['syndication_feed'] = $this->feedmeta['link/uri']; | |
| 184 | - $this->post['meta']['syndication_feed_id'] = $this->feedmeta['link/id']; | |
| 701 | + $aa = $this->entry->get_authors(); | |
| 702 | + if (count($aa) > 0) : | |
| 703 | + $a = reset($aa); | |
| 185 | 704 | |
| 186 | - if (isset($this->item['source_link_self'])) : | |
| 187 | - $this->post['meta']['syndication_feed_original'] = $this->item['source_link_self']; | |
| 188 | - endif; | |
| 705 | + $author = array( | |
| 706 | + 'name' => $a->get_name(), | |
| 707 | + 'email' => $a->get_email(), | |
| 708 | + 'uri' => $a->get_link(), | |
| 709 | + ); | |
| 710 | + endif; | |
| 189 | 711 | |
| 190 | - // In case you want to know the external permalink... | |
| 191 | - if (isset($this->item['link'])) : | |
| 192 | - $permalink = $this->item['link']; | |
| 712 | + if (FEEDWORDPRESS_COMPATIBILITY) : | |
| 713 | + // Search through the MagpieRSS elements: Atom, Dublin Core, RSS | |
| 714 | + if (isset($this->item['author_name'])): | |
| 715 | + $author['name'] = $this->item['author_name']; | |
| 716 | + elseif (isset($this->item['dc']['creator'])): | |
| 717 | + $author['name'] = $this->item['dc']['creator']; | |
| 718 | + elseif (isset($this->item['dc']['contributor'])): | |
| 719 | + $author['name'] = $this->item['dc']['contributor']; | |
| 720 | + elseif (isset($this->feed->channel['dc']['creator'])) : | |
| 721 | + $author['name'] = $this->feed->channel['dc']['creator']; | |
| 722 | + elseif (isset($this->feed->channel['dc']['contributor'])) : | |
| 723 | + $author['name'] = $this->feed->channel['dc']['contributor']; | |
| 724 | + elseif (isset($this->feed->channel['author_name'])) : | |
| 725 | + $author['name'] = $this->feed->channel['author_name']; | |
| 726 | + elseif ($this->feed->is_rss() and isset($this->item['author'])) : | |
| 727 | + // The author element in RSS is allegedly an | |
| 728 | + // e-mail address, but lots of people don't use | |
| 729 | + // it that way. So let's make of it what we can. | |
| 730 | + $author = parse_email_with_realname($this->item['author']); | |
| 193 | 731 | |
| 194 | - // No <link> element. See if this feed has <guid isPermalink="true"> .... | |
| 195 | - elseif (isset($this->item['guid'])) : | |
| 196 | - if (isset($this->item['guid@ispermalink']) and strtolower(trim($this->item['guid@ispermalink'])) != 'false') : | |
| 197 | - $permalink = $this->item['guid']; | |
| 732 | + if (!isset($author['name'])) : | |
| 733 | + if (isset($author['email'])) : | |
| 734 | + $author['name'] = $author['email']; | |
| 735 | + else : | |
| 736 | + $author['name'] = $this->feed->channel['title']; | |
| 737 | + endif; | |
| 198 | 738 | endif; |
| 199 | 739 | endif; |
| 740 | + endif; | |
| 200 | 741 | |
| 201 | - $this->post['meta']['syndication_permalink'] = apply_filters('syndicated_item_link', $permalink); | |
| 742 | + if (!isset($author['name']) or is_null($author['name'])) : | |
| 743 | + // Nothing found. Try some crappy defaults. | |
| 744 | + if ($this->link->name()) : | |
| 745 | + $author['name'] = $this->link->name(); | |
| 746 | + else : | |
| 747 | + $url = parse_url($this->link->uri()); | |
| 748 | + $author['name'] = $url['host']; | |
| 749 | + endif; | |
| 750 | + endif; | |
| 202 | 751 | |
| 203 | - // Store a hash of the post content for checking whether something needs to be updated | |
| 204 | - $this->post['meta']['syndication_item_hash'] = $this->update_hash(); | |
| 752 | + if (FEEDWORDPRESS_COMPATIBILITY) : | |
| 753 | + if (isset($this->item['author_email'])): | |
| 754 | + $author['email'] = $this->item['author_email']; | |
| 755 | + elseif (isset($this->feed->channel['author_email'])) : | |
| 756 | + $author['email'] = $this->feed->channel['author_email']; | |
| 757 | + endif; | |
| 205 | 758 | |
| 206 | - // Feed-by-feed options for author and category creation | |
| 207 | - $this->post['named']['unfamiliar']['author'] = (isset($this->feedmeta['unfamiliar author']) ? $this->feedmeta['unfamiliar author'] : null); | |
| 208 | - $this->post['named']['unfamiliar']['category'] = (isset($this->feedmeta['unfamiliar category']) ? $this->feedmeta['unfamiliar category'] : null); | |
| 759 | + if (isset($this->item['author_uri'])): | |
| 760 | + $author['uri'] = $this->item['author_uri']; | |
| 761 | + elseif (isset($this->item['author_url'])): | |
| 762 | + $author['uri'] = $this->item['author_url']; | |
| 763 | + elseif (isset($this->feed->channel['author_uri'])) : | |
| 764 | + $author['uri'] = $this->item['author_uri']; | |
| 765 | + elseif (isset($this->feed->channel['author_url'])) : | |
| 766 | + $author['uri'] = $this->item['author_url']; | |
| 767 | + elseif (isset($this->feed->channel['link'])) : | |
| 768 | + $author['uri'] = $this->feed->channel['link']; | |
| 769 | + endif; | |
| 770 | + endif; | |
| 209 | 771 | |
| 210 | - // Categories: start with default categories, if any | |
| 772 | + return $author; | |
| 773 | + } /* SyndicatedPost::author() */ | |
| 774 | + | |
| 775 | + /** | |
| 776 | + * SyndicatedPost::get_terms_from_settings(): Return an array of terms to associate with the incoming | |
| 777 | + * post based on the Categories, Tags, and other terms associated with each new post by the user's | |
| 778 | + * settings (global and feed-specific). | |
| 779 | + * | |
| 780 | + * @since 2016.0331 | |
| 781 | + * @return array of lists, each element has the taxonomy for a key ('category', 'post_tag', etc.), | |
| 782 | + * and a list of term codes (either alphanumeric names, or ID numbers encoded in a format that | |
| 783 | + * SyndicatedLink::category_ids() can understand) within that taxonomy | |
| 784 | + * | |
| 785 | + */ | |
| 786 | + public function get_terms_from_settings () { | |
| 787 | + // Categories: start with default categories, if any. | |
| 788 | + $cats = array(); | |
| 789 | + if ('no' != $this->link->setting('add/category', NULL, 'yes')) : | |
| 211 | 790 | $fc = get_option("feedwordpress_syndication_cats"); |
| 212 | 791 | if ($fc) : |
| 213 | - $this->post['named']['preset/category'] = explode("\n", $fc); | |
| 214 | - else : | |
| 215 | - $this->post['named']['preset/category'] = array(); | |
| 792 | + $cats = array_merge($cats, explode("\n", $fc)); | |
| 216 | 793 | endif; |
| 794 | + endif; | |
| 217 | 795 | |
| 218 | - if (isset($this->feedmeta['cats']) and is_array($this->feedmeta['cats'])) : | |
| 219 | - $this->post['named']['preset/category'] = array_merge($this->post['named']['preset/category'], $this->feedmeta['cats']); | |
| 220 | - endif; | |
| 796 | + $fc = $this->link->setting('cats',NULL, array()); | |
| 797 | + if (is_array($fc)) : | |
| 798 | + $cats = array_merge($cats, $fc); | |
| 799 | + endif; | |
| 800 | + $preset_terms['category'] = $cats; | |
| 221 | 801 | |
| 222 | - // Now add categories from the post, if we have 'em | |
| 223 | - $this->post['named']['category'] = array(); | |
| 224 | - if ( isset($this->item['category#']) ) : | |
| 225 | - for ($i = 1; $i <= $this->item['category#']; $i++) : | |
| 226 | - $cat_idx = (($i > 1) ? "#{$i}" : ""); | |
| 227 | - $cat = $this->item["category{$cat_idx}"]; | |
| 802 | + // Tags: start with default tags, if any | |
| 803 | + $tags = array(); | |
| 804 | + if ('no' != $this->link->setting('add/post_tag', NULL, 'yes')) : | |
| 805 | + $ft = get_option("feedwordpress_syndication_tags", NULL); | |
| 806 | + $tags = (is_null($ft) ? array() : explode(FEEDWORDPRESS_CAT_SEPARATOR, $ft)); | |
| 807 | + endif; | |
| 228 | 808 | |
| 229 | - if ( isset($this->feedmeta['cat_split']) and strlen($this->feedmeta['cat_split']) > 0) : | |
| 230 | - $pcre = "\007".$this->feedmeta['cat_split']."\007"; | |
| 231 | - $this->post['named']['category'] = array_merge($this->post['named']['category'], preg_split($pcre, $cat, -1 /*=no limit*/, PREG_SPLIT_NO_EMPTY)); | |
| 232 | - else : | |
| 233 | - $this->post['named']['category'][] = $cat; | |
| 809 | + $ft = $this->link->setting('tags', NULL, array()); | |
| 810 | + if (is_array($ft)) : | |
| 811 | + $tags = array_merge($tags, $ft); | |
| 812 | + endif; | |
| 813 | + $preset_terms['post_tag'] = $tags; | |
| 814 | + | |
| 815 | + $taxonomies = $this->link->taxonomies(); | |
| 816 | + $feedTerms = $this->link->setting('terms', NULL, array()); | |
| 817 | + $globalTerms = get_option('feedwordpress_syndication_terms', array()); | |
| 818 | + $specials = array('category' => 'cats', 'post_tag' => 'tags'); | |
| 819 | + | |
| 820 | + foreach ($taxonomies as $tax) : | |
| 821 | + // category and tag settings have already previously been handled | |
| 822 | + // but if this is from another taxonomy, then... | |
| 823 | + if (!isset($specials[$tax])) : | |
| 824 | + $terms = array(); | |
| 825 | + | |
| 826 | + // See if we should get the globals | |
| 827 | + if ('no' != $this->link->setting("add/$tax", NULL, 'yes')) : | |
| 828 | + if (isset($globalTerms[$tax])) : | |
| 829 | + $terms = $globalTerms[$tax]; | |
| 234 | 830 | endif; |
| 235 | - endfor; | |
| 831 | + endif; | |
| 832 | + | |
| 833 | + // Now merge in the locals | |
| 834 | + if (isset($feedTerms[$tax])) : | |
| 835 | + $terms = array_merge($terms, $feedTerms[$tax]); | |
| 836 | + endif; | |
| 837 | + | |
| 838 | + // That's all, folks. | |
| 839 | + $preset_terms[$tax] = $terms; | |
| 236 | 840 | endif; |
| 237 | - $this->post['named']['category'] = apply_filters('syndicated_item_categories', $this->post['named']['category'], $this); | |
| 238 | - | |
| 239 | - // Tags: start with default tags, if any | |
| 240 | - $ft = get_option("feedwordpress_syndication_tags"); | |
| 241 | - if ($ft) : | |
| 242 | - $this->post['tags_input'] = explode(FEEDWORDPRESS_CAT_SEPARATOR, $ft); | |
| 841 | + endforeach; | |
| 842 | + | |
| 843 | + return $preset_terms; | |
| 844 | + } /* SyndicatedPost::get_terms_from_settings () */ | |
| 845 | + | |
| 846 | + /** | |
| 847 | + * SyndicatedPost::get_terms_from_feeds(): Return an array of terms to associate with the incoming | |
| 848 | + * post based on the contents of the subscribed feed (atom:category and rss:category elements, dc:subject | |
| 849 | + * elements, tags embedded using microformats in the post content, etc.) | |
| 850 | + * | |
| 851 | + * @since 2016.0331 | |
| 852 | + * @return array of lists, each element has the taxonomy for a key ('category', 'post_tag', etc.), | |
| 853 | + * and a list of alphanumeric term names | |
| 854 | + */ | |
| 855 | + public function get_terms_from_feeds () { | |
| 856 | + // Now add categories from the post, if we have 'em | |
| 857 | + $cats = array(); | |
| 858 | + $post_cats = $this->entry->get_categories(); | |
| 859 | + if (is_array($post_cats)) : foreach ($post_cats as $cat) : | |
| 860 | + $cat_name = $cat->get_term(); | |
| 861 | + if (!$cat_name) : $cat_name = $cat->get_label(); endif; | |
| 862 | + | |
| 863 | + if ($this->link->setting('cat_split', NULL, NULL)) : | |
| 864 | + $pcre = "\007".$this->feedmeta['cat_split']."\007"; | |
| 865 | + $cats = array_merge( | |
| 866 | + $cats, | |
| 867 | + preg_split( | |
| 868 | + $pcre, | |
| 869 | + $cat_name, | |
| 870 | + -1 /*=no limit*/, | |
| 871 | + PREG_SPLIT_NO_EMPTY | |
| 872 | + ) | |
| 873 | + ); | |
| 243 | 874 | else : |
| 244 | - $this->post['tags_input'] = array(); | |
| 875 | + $cats[] = $cat_name; | |
| 245 | 876 | endif; |
| 246 | - | |
| 247 | - if (isset($this->feedmeta['tags']) and is_array($this->feedmeta['tags'])) : | |
| 248 | - $this->post['tags_input'] = array_merge($this->post['tags_input'], $this->feedmeta['tags']); | |
| 877 | + endforeach; endif; | |
| 878 | + | |
| 879 | + $feed_terms['category'] = apply_filters('syndicated_item_categories', $cats, $this); | |
| 880 | + | |
| 881 | + // Scan post for /a[@rel='tag'] and use as tags if present | |
| 882 | + $tags = $this->inline_tags(); | |
| 883 | + $feed_terms['post_tag'] = apply_filters('syndicated_item_tags', $tags, $this); | |
| 884 | + | |
| 885 | + return $feed_terms; | |
| 886 | + } /* SyndicatedPost::get_terms_from_feeds () */ | |
| 887 | + | |
| 888 | + /** | |
| 889 | + * SyndicatedPost::inline_tags: Return a list of all the tags embedded | |
| 890 | + * in post content using the a[@rel="tag"] microformat. | |
| 891 | + * | |
| 892 | + * @since 2010.0630 | |
| 893 | + * @return array of string values containing the name of each tag | |
| 894 | + */ | |
| 895 | + function inline_tags () { | |
| 896 | + $tags = array(); | |
| 897 | + $content = $this->content(); | |
| 898 | + $pattern = FeedWordPressHTML::tagWithAttributeRegex('a', 'rel', 'tag'); | |
| 899 | + preg_match_all($pattern, $content, $refs, PREG_SET_ORDER); | |
| 900 | + if (count($refs) > 0) : | |
| 901 | + foreach ($refs as $ref) : | |
| 902 | + $tag = FeedWordPressHTML::tagWithAttributeMatch($ref); | |
| 903 | + $tags[] = $tag['content']; | |
| 904 | + endforeach; | |
| 905 | + endif; | |
| 906 | + return $tags; | |
| 907 | + } | |
| 908 | + | |
| 909 | + /** | |
| 910 | + * SyndicatedPost::isTaggedAs: Test whether a feed item is | |
| 911 | + * tagged / categorized with a given string. Case and leading and | |
| 912 | + * trailing whitespace are ignored. | |
| 913 | + * | |
| 914 | + * @param string $tag Tag to check for | |
| 915 | + * | |
| 916 | + * @return bool Whether or not at least one of the categories / tags on | |
| 917 | + * $this->item is set to $tag (modulo case and leading and trailing | |
| 918 | + * whitespace) | |
| 919 | + */ | |
| 920 | + function isTaggedAs ($tag) { | |
| 921 | + $desiredTag = strtolower(trim($tag)); // Normalize case and whitespace | |
| 922 | + | |
| 923 | + // Check to see if this is tagged with $tag | |
| 924 | + $currentCategory = 'category'; | |
| 925 | + $currentCategoryNumber = 1; | |
| 926 | + | |
| 927 | + // If we have the new MagpieRSS, the number of category elements | |
| 928 | + // on this item is stored under index "category#". | |
| 929 | + if (isset($this->item['category#'])) : | |
| 930 | + $numberOfCategories = (int) $this->item['category#']; | |
| 931 | + | |
| 932 | + // We REALLY shouldn't have the old and busted MagpieRSS, but in | |
| 933 | + // case we do, it doesn't support multiple categories, but there | |
| 934 | + // might still be a single value under the "category" index. | |
| 935 | + elseif (isset($this->item['category'])) : | |
| 936 | + $numberOfCategories = 1; | |
| 937 | + | |
| 938 | + // No standard category or tag elements on this feed item. | |
| 939 | + else : | |
| 940 | + $numberOfCategories = 0; | |
| 941 | + | |
| 942 | + endif; | |
| 943 | + | |
| 944 | + $isSoTagged = false; // Innocent until proven guilty | |
| 945 | + | |
| 946 | + // Loop through category elements; if there are multiple | |
| 947 | + // elements, they are indexed as category, category#2, | |
| 948 | + // category#3, ... category#N | |
| 949 | + while ($currentCategoryNumber <= $numberOfCategories) : | |
| 950 | + if ($desiredTag == strtolower(trim($this->item[$currentCategory]))) : | |
| 951 | + $isSoTagged = true; // Got it! | |
| 952 | + break; | |
| 249 | 953 | endif; |
| 250 | - $this->post['tags_input'] = apply_filters('syndicated_item_tags', $this->post['tags_input'], $this); | |
| 954 | + | |
| 955 | + $currentCategoryNumber += 1; | |
| 956 | + $currentCategory = 'category#'.$currentCategoryNumber; | |
| 957 | + endwhile; | |
| 958 | + | |
| 959 | + return $isSoTagged; | |
| 960 | + } /* SyndicatedPost::isTaggedAs() */ | |
| 961 | + | |
| 962 | + /** | |
| 963 | + * SyndicatedPost::enclosures: returns an array with any enclosures | |
| 964 | + * that may be attached to this syndicated item. | |
| 965 | + * | |
| 966 | + * @param string $type If you only want enclosures that match a certain | |
| 967 | + * MIME type or group of MIME types, you can limit the enclosures | |
| 968 | + * that will be returned to only those with a MIME type which | |
| 969 | + * matches this regular expression. | |
| 970 | + * @return array | |
| 971 | + */ | |
| 972 | + function enclosures ($type = '/.*/') { | |
| 973 | + $enclosures = array(); | |
| 974 | + | |
| 975 | + if (isset($this->item['enclosure#'])) : | |
| 976 | + // Loop through enclosure, enclosure#2, enclosure#3, .... | |
| 977 | + for ($i = 1; $i <= $this->item['enclosure#']; $i++) : | |
| 978 | + $eid = (($i > 1) ? "#{$id}" : ""); | |
| 979 | + | |
| 980 | + // Does it match the type we want? | |
| 981 | + if (preg_match($type, $this->item["enclosure{$eid}@type"])) : | |
| 982 | + $enclosures[] = array( | |
| 983 | + "url" => $this->item["enclosure{$eid}@url"], | |
| 984 | + "type" => $this->item["enclosure{$eid}@type"], | |
| 985 | + "length" => $this->item["enclosure{$eid}@length"], | |
| 986 | + ); | |
| 987 | + endif; | |
| 988 | + endfor; | |
| 251 | 989 | endif; |
| 252 | - } // SyndicatedPost::SyndicatedPost() | |
| 990 | + return $enclosures; | |
| 991 | + } /* SyndicatedPost::enclosures() */ | |
| 253 | 992 | |
| 993 | + function source ($what = NULL) { | |
| 994 | + $ret = NULL; | |
| 995 | + $source = $this->entry->get_source(); | |
| 996 | + if ($source) : | |
| 997 | + $ret = array(); | |
| 998 | + $ret['title'] = $source->get_title(); | |
| 999 | + $ret['uri'] = $source->get_link(); | |
| 1000 | + $ret['feed'] = $source->get_link(0, 'self'); | |
| 1001 | + | |
| 1002 | + if ($id_tags = $source->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'id')) : | |
| 1003 | + $ret['id'] = $id_tags[0]['data']; | |
| 1004 | + elseif ($id_tags = $source->get_source_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'id')) : | |
| 1005 | + $ret['id'] = $id_tags[0]['data']; | |
| 1006 | + elseif ($id_tags = $source->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'guid')) : | |
| 1007 | + $ret['id'] = $id_tags[0]['data']; | |
| 1008 | + elseif ($id_tags = $source->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'guid')) : | |
| 1009 | + $ret['id'] = $id_tags[0]['data']; | |
| 1010 | + elseif ($id_tags = $source->get_source_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'guid')) : | |
| 1011 | + $ret['id'] = $id_tags[0]['data']; | |
| 1012 | + endif; | |
| 1013 | + endif; | |
| 1014 | + | |
| 1015 | + if (!is_null($what) and is_scalar($what)) : | |
| 1016 | + $ret = $ret[$what]; | |
| 1017 | + endif; | |
| 1018 | + return $ret; | |
| 1019 | + } | |
| 1020 | + | |
| 1021 | + function comment_link () { | |
| 1022 | + $url = null; | |
| 1023 | + | |
| 1024 | + // RSS 2.0 has a standard <comments> element: | |
| 1025 | + // "<comments> is an optional sub-element of <item>. If present, | |
| 1026 | + // it is the url of the comments page for the item." | |
| 1027 | + // <http://cyber.law.harvard.edu/rss/rss.html#ltcommentsgtSubelementOfLtitemgt> | |
| 1028 | + if (isset($this->item['comments'])) : | |
| 1029 | + $url = $this->item['comments']; | |
| 1030 | + endif; | |
| 1031 | + | |
| 1032 | + // The convention in Atom feeds is to use a standard <link> | |
| 1033 | + // element with @rel="replies" and @type="text/html". | |
| 1034 | + // Unfortunately, SimplePie_Item::get_links() allows us to filter | |
| 1035 | + // by the value of @rel, but not by the value of @type. *sigh* | |
| 1036 | + | |
| 1037 | + // Try Atom 1.0 first | |
| 1038 | + $linkElements = $this->entry->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'link'); | |
| 1039 | + | |
| 1040 | + // Fall back and try Atom 0.3 | |
| 1041 | + if (is_null($linkElements)) : $linkElements = $this->entry->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'link'); endif; | |
| 1042 | + | |
| 1043 | + // Now loop through the elements, screening by @rel and @type | |
| 1044 | + if (is_array($linkElements)) : foreach ($linkElements as $link) : | |
| 1045 | + $rel = (isset($link['attribs']['']['rel']) ? $link['attribs']['']['rel'] : 'alternate'); | |
| 1046 | + $type = (isset($link['attribs']['']['type']) ? $link['attribs']['']['type'] : NULL); | |
| 1047 | + $href = (isset($link['attribs']['']['href']) ? $link['attribs']['']['href'] : NULL); | |
| 1048 | + | |
| 1049 | + if (strtolower($rel)=='replies' and $type=='text/html' and !is_null($href)) : | |
| 1050 | + $url = $href; | |
| 1051 | + endif; | |
| 1052 | + endforeach; endif; | |
| 1053 | + | |
| 1054 | + return $url; | |
| 1055 | + } | |
| 1056 | + | |
| 1057 | + function comment_feed () { | |
| 1058 | + $feed = null; | |
| 1059 | + | |
| 1060 | + // Well Formed Web comment feeds extension for RSS 2.0 | |
| 1061 | + // <http://www.sellsbrothers.com/spout/default.aspx?content=archive.htm#exposingRssComments> | |
| 1062 | + // | |
| 1063 | + // N.B.: Correct capitalization is wfw:commentRss, but | |
| 1064 | + // wfw:commentRSS is common in the wild (partly due to a typo in | |
| 1065 | + // the original spec). In any case, our item array is normalized | |
| 1066 | + // to all lowercase anyways. | |
| 1067 | + if (isset($this->item['wfw']['commentrss'])) : | |
| 1068 | + $feed = $this->item['wfw']['commentrss']; | |
| 1069 | + endif; | |
| 1070 | + | |
| 1071 | + // In Atom 1.0, the convention is to use a standard link element | |
| 1072 | + // with @rel="replies". Sometimes this is also used to pass a | |
| 1073 | + // link to the human-readable comments page, so we also need to | |
| 1074 | + // check link/@type for a feed MIME type. | |
| 1075 | + // | |
| 1076 | + // Which is why I'm not using the SimplePie_Item::get_links() | |
| 1077 | + // method here, incidentally: it doesn't allow you to filter by | |
| 1078 | + // @type. *sigh* | |
| 1079 | + if (isset($this->item['link_replies'])) : | |
| 1080 | + // There may be multiple <link rel="replies"> elements; feeds have a feed MIME type | |
| 1081 | + $N = isset($this->item['link_replies#']) ? $this->item['link_replies#'] : 1; | |
| 1082 | + for ($i = 1; $i <= $N; $i++) : | |
| 1083 | + $currentElement = 'link_replies'.(($i > 1) ? '#'.$i : ''); | |
| 1084 | + if (isset($this->item[$currentElement.'@type']) | |
| 1085 | + and preg_match("\007application/(atom|rss|rdf)\+xml\007i", $this->item[$currentElement.'@type'])) : | |
| 1086 | + $feed = $this->item[$currentElement]; | |
| 1087 | + endif; | |
| 1088 | + endfor; | |
| 1089 | + endif; | |
| 1090 | + return $feed; | |
| 1091 | + } /* SyndicatedPost::comment_feed() */ | |
| 1092 | + | |
| 1093 | + ################################## | |
| 1094 | + #### BUILT-IN CONTENT FILTERS #### | |
| 1095 | + ################################## | |
| 1096 | + | |
| 1097 | + var $uri_attrs = array ( | |
| 1098 | + array('a', 'href'), | |
| 1099 | + array('applet', 'codebase'), | |
| 1100 | + array('area', 'href'), | |
| 1101 | + array('blockquote', 'cite'), | |
| 1102 | + array('body', 'background'), | |
| 1103 | + array('del', 'cite'), | |
| 1104 | + array('form', 'action'), | |
| 1105 | + array('frame', 'longdesc'), | |
| 1106 | + array('frame', 'src'), | |
| 1107 | + array('iframe', 'longdesc'), | |
| 1108 | + array('iframe', 'src'), | |
| 1109 | + array('head', 'profile'), | |
| 1110 | + array('img', 'longdesc'), | |
| 1111 | + array('img', 'src'), | |
| 1112 | + array('img', 'usemap'), | |
| 1113 | + array('input', 'src'), | |
| 1114 | + array('input', 'usemap'), | |
| 1115 | + array('ins', 'cite'), | |
| 1116 | + array('link', 'href'), | |
| 1117 | + array('object', 'classid'), | |
| 1118 | + array('object', 'codebase'), | |
| 1119 | + array('object', 'data'), | |
| 1120 | + array('object', 'usemap'), | |
| 1121 | + array('q', 'cite'), | |
| 1122 | + array('script', 'src') | |
| 1123 | + ); /* var SyndicatedPost::$uri_attrs */ | |
| 1124 | + | |
| 1125 | + var $_base = null; | |
| 1126 | + | |
| 1127 | + function resolve_single_relative_uri ($refs) { | |
| 1128 | + $tag = FeedWordPressHTML::attributeMatch($refs); | |
| 1129 | + $url = SimplePie_Misc::absolutize_url($tag['value'], $this->_base); | |
| 1130 | + | |
| 1131 | + return $tag['prefix'] . $url . $tag['suffix']; | |
| 1132 | + } /* function SyndicatedPost::resolve_single_relative_uri() */ | |
| 1133 | + | |
| 1134 | + static function resolve_relative_uris ($content, $obj) { | |
| 1135 | + $set = $obj->link->setting('resolve relative', 'resolve_relative', 'yes'); | |
| 1136 | + if ($set and $set != 'no') : | |
| 1137 | + // Fallback: if we don't have anything better, use the | |
| 1138 | + // item link from the feed | |
| 1139 | + $obj->_base = $obj->permalink(); // Reset the base for resolving relative URIs | |
| 1140 | + | |
| 1141 | + // What we should do here, properly, is to use | |
| 1142 | + // SimplePie_Item::get_base() -- but that method is | |
| 1143 | + // currently broken. Or getting down and dirty in the | |
| 1144 | + // SimplePie representation of the content tags and | |
| 1145 | + // grabbing the xml_base member for the content element. | |
| 1146 | + // Maybe someday... | |
| 1147 | + | |
| 1148 | + foreach ($obj->uri_attrs as $pair) : | |
| 1149 | + list($tag, $attr) = $pair; | |
| 1150 | + $pattern = FeedWordPressHTML::attributeRegex($tag, $attr); | |
| 1151 | + | |
| 1152 | + // FIXME: Encountered issue while testing an extremely long (= 88827 characters) item | |
| 1153 | + // Relying on preg_replace_callback() here can cause a PHP seg fault on my development | |
| 1154 | + // server. preg_match_all() causes a similar problem. Apparently this is a PCRE issue | |
| 1155 | + // Cf. discussion of similar issue <https://bugs.php.net/bug.php?id=65009> | |
| 1156 | + $content = preg_replace_callback ( | |
| 1157 | + $pattern, | |
| 1158 | + array($obj, 'resolve_single_relative_uri'), | |
| 1159 | + $content | |
| 1160 | + ); | |
| 1161 | + | |
| 1162 | + endforeach; | |
| 1163 | + endif; | |
| 1164 | + | |
| 1165 | + return $content; | |
| 1166 | + } /* function SyndicatedPost::resolve_relative_uris () */ | |
| 1167 | + | |
| 1168 | + var $strip_attrs = array ( | |
| 1169 | + array('[a-z]+', 'target'), | |
| 1170 | +// array('[a-z]+', 'style'), | |
| 1171 | +// array('[a-z]+', 'on[a-z]+'), | |
| 1172 | + ); | |
| 1173 | + | |
| 1174 | + function strip_attribute_from_tag ($refs) { | |
| 1175 | + $tag = FeedWordPressHTML::attributeMatch($refs); | |
| 1176 | + return $tag['before_attribute'].$tag['after_attribute']; | |
| 1177 | + } | |
| 1178 | + | |
| 1179 | + static function sanitize_content ($content, $obj) { | |
| 1180 | + # This kind of sucks. I intend to replace it with | |
| 1181 | + # lib_filter sometime soon. | |
| 1182 | + foreach ($obj->strip_attrs as $pair): | |
| 1183 | + list($tag,$attr) = $pair; | |
| 1184 | + $pattern = FeedWordPressHTML::attributeRegex($tag, $attr); | |
| 1185 | + | |
| 1186 | + $content = preg_replace_callback ( | |
| 1187 | + $pattern, | |
| 1188 | + array($obj, 'strip_attribute_from_tag'), | |
| 1189 | + $content | |
| 1190 | + ); | |
| 1191 | + endforeach; | |
| 1192 | + return $content; | |
| 1193 | + } /* SyndicatedPost::sanitize() */ | |
| 1194 | + | |
| 1195 | + ##################### | |
| 1196 | + #### POST STATUS #### | |
| 1197 | + ##################### | |
| 1198 | + | |
| 1199 | + /** | |
| 1200 | + * SyndicatedPost::filtered: check whether or not this post has been | |
| 1201 | + * screened out by a registered filter. | |
| 1202 | + * | |
| 1203 | + * @return bool TRUE iff post has been filtered out by a previous filter | |
| 1204 | + */ | |
| 254 | 1205 | function filtered () { |
| 255 | 1206 | return is_null($this->post); |
| 256 | - } | |
| 1207 | + } /* SyndicatedPost::filtered() */ | |
| 257 | 1208 | |
| 258 | - function freshness () { | |
| 1209 | + /** | |
| 1210 | + * SyndicatedPost::freshness: check whether post is a new post to be | |
| 1211 | + * inserted, a previously syndicated post that needs to be updated to | |
| 1212 | + * match the latest revision, or a previously syndicated post that is | |
| 1213 | + * still up-to-date. | |
| 1214 | + * | |
| 1215 | + * @return int A status code representing the freshness of the post | |
| 1216 | + * -1 = post already syndicated; has a revision that needs to be stored, but not updated to | |
| 1217 | + * 0 = post already syndicated; no update needed | |
| 1218 | + * 1 = post already syndicated, but needs to be updated to latest | |
| 1219 | + * 2 = post has not yet been syndicated; needs to be created | |
| 1220 | + */ | |
| 1221 | + function freshness ($format = 'number') { | |
| 259 | 1222 | global $wpdb; |
| 260 | 1223 | |
| 261 | 1224 | if ($this->filtered()) : // This should never happen. |
| 262 | - FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__); | |
| 1225 | + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__); | |
| 263 | 1226 | endif; |
| 264 | - | |
| 265 | - if (is_null($this->_freshness)) : | |
| 266 | - $guid = $wpdb->escape($this->guid()); | |
| 267 | 1227 | |
| 268 | - $result = $wpdb->get_row(" | |
| 269 | - SELECT id, guid, post_modified_gmt | |
| 270 | - FROM $wpdb->posts WHERE guid='$guid' | |
| 271 | - "); | |
| 1228 | + if (is_null($this->_freshness)) : // Not yet checked and cached. | |
| 1229 | + $guid = $this->post['guid']; | |
| 1230 | + $eguid = esc_sql($this->post['guid']); | |
| 272 | 1231 | |
| 273 | - if (!$result) : | |
| 1232 | + $q = new WP_Query(array( | |
| 1233 | + 'fields' => '_synfresh', // id, guid, post_modified_gmt | |
| 1234 | + 'ignore_sticky_posts' => true, | |
| 1235 | + 'guid' => $guid, | |
| 1236 | + )); | |
| 1237 | + | |
| 1238 | + $old_post = NULL; | |
| 1239 | + if ($q->have_posts()) : | |
| 1240 | + while ($q->have_posts()) : $q->the_post(); | |
| 1241 | + $old_post = $q->post; | |
| 1242 | + endwhile; | |
| 1243 | + endif; | |
| 1244 | + | |
| 1245 | + if (is_null($old_post)) : // No post with this guid | |
| 1246 | + FeedWordPress::diagnostic('feed_items:freshness', 'Item ['.$guid.'] "'.$this->entry->get_title().'" is a NEW POST.'); | |
| 1247 | + $this->_wp_id = NULL; | |
| 274 | 1248 | $this->_freshness = 2; // New content |
| 275 | - else: | |
| 276 | - $stored_update_hashes = get_post_custom_values('syndication_item_hash', $result->id); | |
| 277 | - if (count($stored_update_hashes) > 0) : | |
| 278 | - $stored_update_hash = $stored_update_hashes[0]; | |
| 279 | - $update_hash_changed = ($stored_update_hash != $this->update_hash()); | |
| 1249 | + else : | |
| 1250 | + // Presume there is nothing new until we find | |
| 1251 | + // something new. | |
| 1252 | + $updated = false; | |
| 1253 | + $live = false; | |
| 1254 | + | |
| 1255 | + // Pull the list of existing revisions to get | |
| 1256 | + // timestamps. | |
| 1257 | + $revisions = wp_get_post_revisions($old_post->ID); | |
| 1258 | + foreach ($revisions as $rev) : | |
| 1259 | + $revisions_ts[] = mysql2date('G', $rev->post_modified_gmt); | |
| 1260 | + endforeach; | |
| 1261 | + | |
| 1262 | + $revisions_ts[] = mysql2date('G', $old_post->post_modified_gmt); | |
| 1263 | + $last_rev_ts = end($revisions_ts); | |
| 1264 | + $updated_ts = $this->updated(/*fallback=*/ true, /*default=*/ NULL); | |
| 1265 | + | |
| 1266 | + // If we have an explicit updated timestamp, | |
| 1267 | + // check that against existing stamps. | |
| 1268 | + if (!is_null($updated_ts)) : | |
| 1269 | + $updated = !in_array($updated_ts, $revisions_ts); | |
| 1270 | + | |
| 1271 | + // If this a newer revision, make it go | |
| 1272 | + // live. If an older one, just record | |
| 1273 | + // the contents. | |
| 1274 | + $live = ($updated and ($updated_ts > $last_rev_ts)); | |
| 1275 | + endif; | |
| 1276 | + | |
| 1277 | + // This is a revision we haven't seen before, judging by the date. | |
| 1278 | + | |
| 1279 | + $updatedReason = NULL; | |
| 1280 | + if ($updated) : | |
| 1281 | + $updatedReason = preg_replace( | |
| 1282 | + "/\s+/", " ", | |
| 1283 | + 'has been marked with a new timestamp (' | |
| 1284 | + .date('Y-m-d H:i:s', $updated_ts) | |
| 1285 | + ." > " | |
| 1286 | + .date('Y-m-d H:i:s', $last_rev_ts) | |
| 1287 | + .')' | |
| 1288 | + ); | |
| 1289 | + | |
| 1290 | + // The date does not indicate a new revision, so | |
| 1291 | + // let's check the hash. | |
| 280 | 1292 | else : |
| 281 | - $update_hash_changed = false; | |
| 1293 | + // Or the hash... | |
| 1294 | + $hash = $this->update_hash(); | |
| 1295 | + $seen = $this->stored_hashes($old_post->ID); | |
| 1296 | + if (count($seen) > 0) : | |
| 1297 | + $updated = !in_array($hash, $seen); // Not seen yet? | |
| 1298 | + else : | |
| 1299 | + $updated = true; // Can't find syndication meta-data | |
| 1300 | + endif; | |
| 1301 | + | |
| 1302 | + if ($updated and FeedWordPress::diagnostic_on('feed_items:freshness:reasons')) : | |
| 1303 | + // In the absence of definitive | |
| 1304 | + // timestamp information, we | |
| 1305 | + // just have to assume that a | |
| 1306 | + // hash we haven't seen before | |
| 1307 | + // is a newer version. | |
| 1308 | + $live = true; | |
| 1309 | + | |
| 1310 | + $updatedReason = ' has a not-yet-seen update hash: ' | |
| 1311 | + .MyPHP::val($hash) | |
| 1312 | + .' not in {' | |
| 1313 | + .implode(", ", array_map(array('FeedWordPress', 'val'), $seen)) | |
| 1314 | + .'}. Basis: ' | |
| 1315 | + .MyPHP::val(array_keys($this->update_hash(false))); | |
| 1316 | + endif; | |
| 282 | 1317 | endif; |
| 283 | 1318 | |
| 284 | - preg_match('/([0-9]+)-([0-9]+)-([0-9]+) ([0-9]+):([0-9]+):([0-9]+)/', $result->post_modified_gmt, $backref); | |
| 1319 | + $frozen = false; | |
| 1320 | + if ($updated) : // Ignore if the post is frozen | |
| 1321 | + $frozen = ('yes' == $this->link->setting('freeze updates', 'freeze_updates', NULL)); | |
| 1322 | + if (!$frozen) : | |
| 1323 | + $frozen_values = get_post_custom_values('_syndication_freeze_updates', $old_post->ID); | |
| 1324 | + $frozen = (count($frozen_values) > 0 and 'yes' == $frozen_values[0]); | |
| 285 | 1325 | |
| 286 | - $last_rev_ts = gmmktime($backref[4], $backref[5], $backref[6], $backref[2], $backref[3], $backref[1]); | |
| 287 | - $updated_ts = $this->updated(/*fallback=*/ true, /*default=*/ NULL); | |
| 288 | - $updated = (( | |
| 289 | - !is_null($updated_ts) | |
| 290 | - and ($updated_ts > $last_rev_ts) | |
| 291 | - ) or $update_hash_changed); | |
| 1326 | + if ($frozen) : | |
| 1327 | + $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A UPDATE LOCK ON THIS POST, EVEN THOUGH IT '.$updatedReason; | |
| 1328 | + endif; | |
| 1329 | + else : | |
| 1330 | + $updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A FEEDWORDPRESS UPDATE LOCK, EVEN THOUGH IT '.$updatedReason; | |
| 1331 | + endif; | |
| 1332 | + endif; | |
| 1333 | + $live = ($live and !$frozen); | |
| 292 | 1334 | |
| 293 | 1335 | if ($updated) : |
| 294 | - $this->_freshness = 1; // Updated content | |
| 295 | - $this->_wp_id = $result->id; | |
| 1336 | + FeedWordPress::diagnostic('feed_items:freshness', 'Item ['.$guid.'] "'.$this->entry->get_title().'" is an update of an existing post.'); | |
| 1337 | + if (!is_null($updatedReason)) : | |
| 1338 | + $updatedReason = preg_replace('/\s+/', ' ', $updatedReason); | |
| 1339 | + FeedWordPress::diagnostic('feed_items:freshness:reasons', 'Item ['.$guid.'] "'.$this->entry->get_title().'" '.$updatedReason); | |
| 1340 | + endif; | |
| 1341 | + | |
| 1342 | + $this->_freshness = apply_filters('syndicated_item_freshness', ($live ? 1 : -1), $updated, $frozen, $updated_ts, $last_rev_ts, $this); | |
| 1343 | + | |
| 1344 | + $this->_wp_id = $old_post->ID; | |
| 1345 | + $this->_wp_post = $old_post; | |
| 1346 | + | |
| 1347 | + // We want this to keep a running list of all the | |
| 1348 | + // processed update hashes. | |
| 1349 | + $this->post['meta']['syndication_item_hash'] = array_merge( | |
| 1350 | + $this->stored_hashes(), | |
| 1351 | + array($this->update_hash()) | |
| 1352 | + ); | |
| 296 | 1353 | else : |
| 1354 | + FeedWordPress::diagnostic('feed_items:freshness', 'Item ['.$guid.'] "'.$this->entry->get_title().'" is a duplicate of an existing post.'); | |
| 297 | 1355 | $this->_freshness = 0; // Same old, same old |
| 298 | - $this->_wp_id = $result->id; | |
| 1356 | + $this->_wp_id = $old_post->ID; | |
| 299 | 1357 | endif; |
| 300 | 1358 | endif; |
| 301 | 1359 | endif; |
| 302 | - return $this->_freshness; | |
| 1360 | + | |
| 1361 | + switch ($format) : | |
| 1362 | + case 'status' : | |
| 1363 | + switch ($this->_freshness) : | |
| 1364 | + case -1: | |
| 1365 | + $ret = 'stored'; | |
| 1366 | + break; | |
| 1367 | + case 0: | |
| 1368 | + $ret = NULL; | |
| 1369 | + break; | |
| 1370 | + case 1: | |
| 1371 | + $ret = 'updated'; | |
| 1372 | + break; | |
| 1373 | + case 2: | |
| 1374 | + default: | |
| 1375 | + $ret = 'new'; | |
| 1376 | + break; | |
| 1377 | + endswitch; | |
| 1378 | + break; | |
| 1379 | + case 'number' : | |
| 1380 | + default : | |
| 1381 | + $ret = $this->_freshness; | |
| 1382 | + endswitch; | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + return $ret; | |
| 1386 | + } /* SyndicatedPost::freshness () */ | |
| 1387 | + | |
| 1388 | + function has_fresh_content () { | |
| 1389 | + return ( ! $this->filtered() and $this->freshness() != 0 ); | |
| 1390 | + } /* SyndicatedPost::has_fresh_content () */ | |
| 1391 | + | |
| 1392 | + function this_revision_needs_original_post ($freshness = NULL) { | |
| 1393 | + if (is_null($freshness)) : | |
| 1394 | + $freshness = $this->freshness(); | |
| 1395 | + endif; | |
| 1396 | + return ( $freshness >= 2 ); | |
| 303 | 1397 | } |
| 1398 | + | |
| 1399 | + function this_revision_is_current ($freshness = NULL) { | |
| 1400 | + if (is_null($freshness)) : | |
| 1401 | + $freshness = $this->freshness(); | |
| 1402 | + endif; | |
| 1403 | + return ( $freshness >= 1 ); | |
| 1404 | + } /* SyndicatedPost::this_revision_is_current () */ | |
| 1405 | + | |
| 1406 | + function fresh_content_is_update () { | |
| 1407 | + return ($this->freshness() < 2); | |
| 1408 | + } /* SyndicatedPost::fresh_content_is_update () */ | |
| 1409 | + | |
| 1410 | + function fresh_storage_diagnostic () { | |
| 1411 | + $ret = NULL; | |
| 1412 | + switch ($this->freshness()) : | |
| 1413 | + case -1 : | |
| 1414 | + $ret = 'Storing alternate revision of existing post # '.$this->wp_id().', "'.$this->post['post_title'].'"'; | |
| 1415 | + break; | |
| 1416 | + case 1 : | |
| 1417 | + $ret = 'Updating existing post # '.$this->wp_id().', "'.$this->post['post_title'].'"'; | |
| 1418 | + break; | |
| 1419 | + case 2 : | |
| 1420 | + default : | |
| 1421 | + $ret = 'Inserting new post "'.$this->post['post_title'].'"'; | |
| 1422 | + break; | |
| 1423 | + endswitch; | |
| 1424 | + return $ret; | |
| 1425 | + } /* SyndicatedPost::fresh_storage_diagnostic() */ | |
| 1426 | + | |
| 1427 | + function fresh_storage_hook () { | |
| 1428 | + $ret = NULL; | |
| 1429 | + switch ($this->freshness()) : | |
| 1430 | + case -1 : | |
| 1431 | + case 1 : | |
| 1432 | + $ret = 'update_syndicated_item'; | |
| 1433 | + break; | |
| 1434 | + case 2 : | |
| 1435 | + default : | |
| 1436 | + $ret = 'post_syndicated_item'; | |
| 1437 | + break; | |
| 1438 | + endswitch; | |
| 1439 | + return $ret; | |
| 1440 | + } /* SyndicatedPost::fresh_storage_hook () */ | |
| 1441 | + | |
| 1442 | + ################################################# | |
| 1443 | + #### INTERNAL STORAGE AND MANAGEMENT METHODS #### | |
| 1444 | + ################################################# | |
| 304 | 1445 | |
| 305 | 1446 | function wp_id () { |
| 306 | 1447 | if ($this->filtered()) : // This should never happen. |
| 307 | - FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__); | |
| 1448 | + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__); | |
| 308 | 1449 | endif; |
| 309 | - | |
| 1450 | + | |
| 310 | 1451 | if (is_null($this->_wp_id) and is_null($this->_freshness)) : |
| 311 | 1452 | $fresh = $this->freshness(); // sets WP DB id in the process |
| 312 | 1453 | endif; |
| 313 | 1454 | return $this->_wp_id; |
| @@ -312,240 +1453,340 @@ | ||
| 312 | 1453 | endif; |
| 313 | 1454 | return $this->_wp_id; |
| 314 | 1455 | } |
| 315 | 1456 | |
| 316 | - function store () { | |
| 1457 | + public function store () { | |
| 317 | 1458 | global $wpdb; |
| 318 | 1459 | |
| 319 | 1460 | if ($this->filtered()) : // This should never happen. |
| 320 | - FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__); | |
| 1461 | + FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__); | |
| 321 | 1462 | endif; |
| 322 | - | |
| 1463 | + | |
| 323 | 1464 | $freshness = $this->freshness(); |
| 324 | - if ($freshness > 0) : | |
| 1465 | + if ($this->has_fresh_content()) : | |
| 325 | 1466 | # -- Look up, or create, numeric ID for author |
| 326 | 1467 | $this->post['post_author'] = $this->author_id ( |
| 327 | - FeedWordPress::on_unfamiliar('author', $this->post['named']['unfamiliar']['author']) | |
| 1468 | + $this->link->setting('unfamiliar author', 'unfamiliar_author', 'create') | |
| 328 | 1469 | ); |
| 329 | 1470 | |
| 330 | 1471 | if (is_null($this->post['post_author'])) : |
| 1472 | + FeedWordPress::diagnostic('feed_items:rejected', 'Filtered out item ['.$this->guid().'] without syndication: no author available'); | |
| 331 | 1473 | $this->post = NULL; |
| 332 | 1474 | endif; |
| 333 | 1475 | endif; |
| 334 | - | |
| 335 | - if (!$this->filtered() and $freshness > 0) : | |
| 336 | - # -- Look up, or create, numeric ID for categories | |
| 337 | - list($pcats, $ptags) = $this->category_ids ( | |
| 338 | - $this->post['named']['category'], | |
| 339 | - FeedWordPress::on_unfamiliar('category', $this->post['named']['unfamiliar']['category']), | |
| 340 | - /*tags_too=*/ true | |
| 341 | - ); | |
| 342 | 1476 | |
| 343 | - $this->post['post_category'] = $pcats; | |
| 344 | - $this->post['tags_input'] = array_merge($this->post['tags_input'], $ptags); | |
| 1477 | + // We have to check again in case post has been filtered during | |
| 1478 | + // the author_id lookup | |
| 1479 | + if ($this->has_fresh_content()) : | |
| 1480 | + $mapping = apply_filters('syndicated_post_terms_mapping', array( | |
| 1481 | + 'category' => array('abbr' => 'cats', 'unfamiliar' => 'category', 'domain' => array('category', 'post_tag')), | |
| 1482 | + 'post_tag' => array('abbr' => 'tags', 'unfamiliar' => 'post_tag', 'domain' => array('post_tag')), | |
| 1483 | + ), $this); | |
| 345 | 1484 | |
| 346 | - if (is_null($this->post['post_category'])) : | |
| 347 | - // filter mode on, no matching categories; drop the post | |
| 1485 | + $termSet = array(); $valid = null; | |
| 1486 | + foreach ($this->feed_terms as $what => $anTerms) : | |
| 1487 | + // Default to using the inclusive procedures (for cats) rather than exclusive (for inline tags) | |
| 1488 | + $taxes = (isset($mapping[$what]) ? $mapping[$what] : $mapping['category']); | |
| 1489 | + $unfamiliar = $taxes['unfamiliar']; | |
| 1490 | + | |
| 1491 | + if (!is_null($this->post)) : // Not filtered out yet | |
| 1492 | + # -- Look up, or create, numeric ID for categories | |
| 1493 | + $taxonomies = $this->link->setting("match/".$taxes['abbr'], 'match_'.$taxes['abbr'], $taxes['domain']); | |
| 1494 | + | |
| 1495 | + // Eliminate dummy variables | |
| 1496 | + $taxonomies = array_filter($taxonomies, 'remove_dummy_zero'); | |
| 1497 | + | |
| 1498 | + // Allow FWP add-on filters to control the taxonomies we use to search for a term | |
| 1499 | + $taxonomies = apply_filters("syndicated_post_terms_match", $taxonomies, $what, $this); | |
| 1500 | + $taxonomies = apply_filters("syndicated_post_terms_match_${what}", $taxonomies, $this); | |
| 1501 | + | |
| 1502 | + // Allow FWP add-on filters to control with greater precision what happens on unmatched | |
| 1503 | + $unmatched = apply_filters("syndicated_post_terms_unfamiliar", | |
| 1504 | + $this->link->setting( | |
| 1505 | + "unfamiliar {$unfamiliar}", | |
| 1506 | + "unfamiliar_{$unfamiliar}", | |
| 1507 | + 'create:'.$unfamiliar | |
| 1508 | + ), | |
| 1509 | + $what, | |
| 1510 | + $this | |
| 1511 | + ); | |
| 1512 | + | |
| 1513 | + $terms = $this->category_ids ( | |
| 1514 | + $anTerms, | |
| 1515 | + $unmatched, | |
| 1516 | + /*taxonomies=*/ $taxonomies, | |
| 1517 | + array( | |
| 1518 | + 'singleton' => false, // I don't like surprises | |
| 1519 | + 'filters' => true, | |
| 1520 | + ) | |
| 1521 | + ); | |
| 1522 | + | |
| 1523 | + if (is_null($terms) or is_null($termSet)) : | |
| 1524 | + // filtered out -- no matches | |
| 1525 | + else : | |
| 1526 | + $valid = true; | |
| 1527 | + | |
| 1528 | + // filter mode off, or at least one match | |
| 1529 | + foreach ($terms as $tax => $term_ids) : | |
| 1530 | + if (!isset($termSet[$tax])) : | |
| 1531 | + $termSet[$tax] = array(); | |
| 1532 | + endif; | |
| 1533 | + $termSet[$tax] = array_merge($termSet[$tax], $term_ids); | |
| 1534 | + endforeach; | |
| 1535 | + endif; | |
| 1536 | + endif; | |
| 1537 | + endforeach; | |
| 1538 | + | |
| 1539 | + if (is_null($valid)) : // Plonked | |
| 348 | 1540 | $this->post = NULL; |
| 349 | - else : | |
| 350 | - // filter mode off or at least one match; now add on the feed and global presets | |
| 351 | - $this->post['post_category'] = array_merge ( | |
| 352 | - $this->post['post_category'], | |
| 353 | - $this->category_ids ( | |
| 354 | - $this->post['named']['preset/category'], | |
| 355 | - 'default' | |
| 356 | - ) | |
| 357 | - ); | |
| 1541 | + else : // We can proceed | |
| 1542 | + $this->post['tax_input'] = array(); | |
| 1543 | + foreach ($termSet as $tax => $term_ids) : | |
| 1544 | + if (!isset($this->post['tax_input'][$tax])) : | |
| 1545 | + $this->post['tax_input'][$tax] = array(); | |
| 1546 | + endif; | |
| 1547 | + $this->post['tax_input'][$tax] = array_merge( | |
| 1548 | + $this->post['tax_input'][$tax], | |
| 1549 | + $term_ids | |
| 1550 | + ); | |
| 1551 | + endforeach; | |
| 358 | 1552 | |
| 359 | - if (count($this->post['post_category']) < 1) : | |
| 360 | - $this->post['post_category'][] = 1; // Default to category 1 ("Uncategorized" / "General") if nothing else | |
| 361 | - endif; | |
| 1553 | + // Now let's add on the feed and global presets | |
| 1554 | + foreach ($this->preset_terms as $tax => $term_ids) : | |
| 1555 | + if (!isset($this->post['tax_input'][$tax])) : | |
| 1556 | + $this->post['tax_input'][$tax] = array(); | |
| 1557 | + endif; | |
| 1558 | + | |
| 1559 | + $this->post['tax_input'][$tax] = array_merge ( | |
| 1560 | + $this->post['tax_input'][$tax], | |
| 1561 | + $this->category_ids ( | |
| 1562 | + /*terms=*/ $term_ids, | |
| 1563 | + /*unfamiliar=*/ 'create:'.$tax, // These are presets; for those added in a tagbox editor, the tag may not yet exist | |
| 1564 | + /*taxonomies=*/ array($tax), | |
| 1565 | + array( | |
| 1566 | + 'singleton' => true, | |
| 1567 | + )) | |
| 1568 | + ); | |
| 1569 | + endforeach; | |
| 362 | 1570 | endif; |
| 363 | 1571 | endif; |
| 364 | - | |
| 365 | - if (!$this->filtered() and $freshness > 0) : | |
| 366 | - unset($this->post['named']); | |
| 1572 | + | |
| 1573 | + // We have to check again in case the post has been filtered | |
| 1574 | + // during the category/tags/taxonomy terms lookup | |
| 1575 | + if ($this->has_fresh_content()) : | |
| 1576 | + // Filter some individual fields | |
| 1577 | + | |
| 1578 | + // If there already is a post slug (from syndication or by manual | |
| 1579 | + // editing) don't cause WP to overwrite it by sending in a NULL | |
| 1580 | + // post_name. Props Chris Fritz 2012-11-28. | |
| 1581 | + $post_name = (is_null($this->_wp_post) ? NULL : $this->_wp_post->post_name); | |
| 1582 | + | |
| 1583 | + // Allow filters to set post slug. Props niska. | |
| 1584 | + $post_name = apply_filters('syndicated_post_slug', $post_name, $this); | |
| 1585 | + if (!empty($post_name)) : | |
| 1586 | + $this->post['post_name'] = $post_name; | |
| 1587 | + endif; | |
| 1588 | + | |
| 367 | 1589 | $this->post = apply_filters('syndicated_post', $this->post, $this); |
| 1590 | + | |
| 1591 | + // Allow for feed-specific syndicated_post filters. | |
| 1592 | + $this->post = apply_filters( | |
| 1593 | + "syndicated_post_".$this->link->uri(), | |
| 1594 | + $this->post, | |
| 1595 | + $this | |
| 1596 | + ); | |
| 368 | 1597 | endif; |
| 1598 | + | |
| 1599 | + // Hook in early to make sure these get inserted if at all possible | |
| 1600 | + add_action( | |
| 1601 | + /*hook=*/ 'transition_post_status', | |
| 1602 | + /*callback=*/ array($this, 'add_rss_meta'), | |
| 1603 | + /*priority=*/ -10000, /* very early */ | |
| 1604 | + /*arguments=*/ 3 | |
| 1605 | + ); | |
| 369 | 1606 | |
| 370 | - if (!$this->filtered() and $freshness == 2) : | |
| 371 | - // The item has not yet been added. So let's add it. | |
| 372 | - $this->insert_new(); | |
| 373 | - $this->add_rss_meta(); | |
| 374 | - do_action('post_syndicated_item', $this->wp_id()); | |
| 1607 | + $ret = false; | |
| 1608 | + if ($this->has_fresh_content()) : | |
| 1609 | + $diag = $this->fresh_storage_diagnostic(); | |
| 1610 | + if (!is_null($diag)) : | |
| 1611 | + FeedWordPress::diagnostic('syndicated_posts', $diag); | |
| 1612 | + endif; | |
| 1613 | + | |
| 1614 | + $this->insert_post(/*update=*/ $this->fresh_content_is_update(), $this->freshness()); | |
| 375 | 1615 | |
| 376 | - $ret = 'new'; | |
| 377 | - elseif (!$this->filtered() and $freshness == 1) : | |
| 378 | - $this->post['ID'] = $this->wp_id(); | |
| 379 | - $this->update_existing(); | |
| 380 | - $this->add_rss_meta(); | |
| 381 | - do_action('update_syndicated_item', $this->wp_id()); | |
| 1616 | + $hook = $this->fresh_storage_hook(); | |
| 1617 | + if (!is_null($hook)) : | |
| 1618 | + do_action($hook, $this->wp_id(), $this); | |
| 1619 | + endif; | |
| 1620 | + | |
| 1621 | + $ret = $this->freshness('status'); | |
| 1622 | + endif; | |
| 382 | 1623 | |
| 383 | - $ret = 'updated'; | |
| 384 | - else : | |
| 385 | - $ret = false; | |
| 1624 | + // If this is a legit, non-filtered post, tag it as found on the | |
| 1625 | + // feed regardless of fresh or stale status | |
| 1626 | + if (!$this->filtered()) : | |
| 1627 | + $key = '_feedwordpress_retire_me_' . $this->link->id; | |
| 1628 | + delete_post_meta($this->wp_id(), $key); | |
| 1629 | + | |
| 1630 | + $status = get_post_field('post_status', $this->wp_id()); | |
| 1631 | + if ('fwpretired'==$status and $this->link->is_non_incremental()) : | |
| 1632 | + FeedWordPress::diagnostic('syndicated_posts', "Un-retiring previously retired post # ".$this->wp_id()." due to re-appearance on non-incremental feed."); | |
| 1633 | + set_post_field('post_status', $this->post['post_status'], $this->wp_id()); | |
| 1634 | + wp_transition_post_status($this->post['post_status'], $status, $this->post); | |
| 1635 | + elseif ('fwpzapped'==$status) : | |
| 1636 | + // Set this new revision up to be | |
| 1637 | + // blanked on the next update. | |
| 1638 | + add_post_meta($this->wp_id(), '_feedwordpress_zapped_blank_me', 2, /*single=*/ true); | |
| 1639 | + endif; | |
| 386 | 1640 | endif; |
| 387 | - | |
| 1641 | + | |
| 1642 | + // Remove add_rss_meta hook | |
| 1643 | + remove_action( | |
| 1644 | + /*hook=*/ 'transition_post_status', | |
| 1645 | + /*callback=*/ array($this, 'add_rss_meta'), | |
| 1646 | + /*priority=*/ -10000, /* very early */ | |
| 1647 | + /*arguments=*/ 3 | |
| 1648 | + ); | |
| 1649 | + | |
| 388 | 1650 | return $ret; |
| 389 | - } // function SyndicatedPost::store () | |
| 390 | - | |
| 391 | - function insert_new () { | |
| 392 | - global $wpdb, $wp_db_version; | |
| 1651 | + } /* function SyndicatedPost::store () */ | |
| 393 | 1652 | |
| 1653 | + function insert_post ($update = false, $freshness = 2) { | |
| 1654 | + global $wpdb; | |
| 1655 | + | |
| 394 | 1656 | $dbpost = $this->normalize_post(/*new=*/ true); |
| 1657 | + | |
| 1658 | + $ret = null; | |
| 1659 | + | |
| 395 | 1660 | if (!is_null($dbpost)) : |
| 396 | - if ($this->use_api('wp_insert_post')) : | |
| 397 | - $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks | |
| 398 | - | |
| 399 | - // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data | |
| 400 | - add_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 1661 | + $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks | |
| 1662 | + | |
| 1663 | + // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data | |
| 1664 | + add_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 1665 | + | |
| 1666 | + // Kludge to prevent kses filters from stripping the | |
| 1667 | + // content of posts when updating without a logged in | |
| 1668 | + // user who has `unfiltered_html` capability. | |
| 1669 | + $mungers = array('wp_filter_kses', 'wp_filter_post_kses'); | |
| 1670 | + $removed = array(); | |
| 1671 | + foreach ($mungers as $munger) : | |
| 1672 | + if (has_filter('content_save_pre', $munger)) : | |
| 1673 | + remove_filter('content_save_pre', $munger); | |
| 1674 | + $removed[] = $munger; | |
| 1675 | + endif; | |
| 1676 | + endforeach; | |
| 1677 | + | |
| 1678 | + if ($update and function_exists('get_post_field')) : | |
| 1679 | + // Don't munge status fields that the user may | |
| 1680 | + // have reset manually | |
| 1681 | + $doNotMunge = array('post_status', 'comment_status', 'ping_status'); | |
| 1682 | + | |
| 1683 | + foreach ($doNotMunge as $field) : | |
| 1684 | + $dbpost[$field] = get_post_field($field, $this->wp_id()); | |
| 1685 | + endforeach; | |
| 1686 | + endif; | |
| 1687 | + | |
| 1688 | + // WP3's wp_insert_post scans current_user_can() for the | |
| 1689 | + // tax_input, with no apparent way to override. Ugh. | |
| 1690 | + add_action( | |
| 1691 | + /*hook=*/ 'transition_post_status', | |
| 1692 | + /*callback=*/ array($this, 'add_terms'), | |
| 1693 | + /*priority=*/ -10001, /* very early */ | |
| 1694 | + /*arguments=*/ 3 | |
| 1695 | + ); | |
| 1696 | + | |
| 1697 | + // WP3 appears to override whatever you give it for | |
| 1698 | + // post_modified. Ugh. | |
| 1699 | + add_action( | |
| 1700 | + /*hook=*/ 'transition_post_status', | |
| 1701 | + /*callback=*/ array($this, 'fix_post_modified_ts'), | |
| 1702 | + /*priority=*/ -10000, /* very early */ | |
| 1703 | + /*arguments=*/ 3 | |
| 1704 | + ); | |
| 1705 | + | |
| 1706 | + if ($update) : | |
| 1707 | + $this->post['ID'] = $this->wp_id(); | |
| 1708 | + $dbpost['ID'] = $this->post['ID']; | |
| 1709 | + endif; | |
| 1710 | + | |
| 1711 | + // O.K., is this a new post? If so, we need to create | |
| 1712 | + // the basic post record before we do anything else. | |
| 1713 | + if ($this->this_revision_needs_original_post()) : | |
| 1714 | + // *sigh*, for handling inconsistent slash expectations < 3.6 | |
| 1715 | + $sdbpost = $this->db_sanitize_post($dbpost); | |
| 401 | 1716 | |
| 402 | - // Kludge to prevent kses filters from stripping the | |
| 403 | - // content of posts when updating without a logged in | |
| 404 | - // user who has `unfiltered_html` capability. | |
| 405 | - add_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 1717 | + // Go ahead and insert the first post record to | |
| 1718 | + // anchor the revision history. | |
| 1719 | + | |
| 1720 | + $this->_wp_id = wp_insert_post($sdbpost, /*return wp_error=*/ true); | |
| 406 | 1721 | |
| 407 | - $this->_wp_id = wp_insert_post($dbpost); | |
| 408 | - | |
| 409 | - // Turn off ridiculous fucking kludges #1 and #2 | |
| 410 | - remove_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 411 | - remove_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 412 | - | |
| 413 | - $this->validate_post_id($dbpost, array(__CLASS__, __FUNCTION__)); | |
| 414 | - | |
| 415 | - // Unfortunately, as of WordPress 2.3, wp_insert_post() | |
| 416 | - // *still* offers no way to use a guid of your choice, | |
| 417 | - // and munges your post modified timestamp, too. | |
| 418 | - $result = $wpdb->query(" | |
| 419 | - UPDATE $wpdb->posts | |
| 420 | - SET | |
| 421 | - guid='{$dbpost['guid']}', | |
| 422 | - post_modified='{$dbpost['post_modified']}', | |
| 423 | - post_modified_gmt='{$dbpost['post_modified_gmt']}' | |
| 424 | - WHERE ID='{$this->_wp_id}' | |
| 425 | - "); | |
| 426 | - else : | |
| 427 | - # The right way to do this is the above. But, alas, | |
| 428 | - # in earlier versions of WordPress, wp_insert_post has | |
| 429 | - # too much behavior (mainly related to pings) that can't | |
| 430 | - # be overridden. In WordPress 1.5, it's enough of a | |
| 431 | - # resource hog to make PHP segfault after inserting | |
| 432 | - # 50-100 posts. This can get pretty annoying, especially | |
| 433 | - # if you are trying to update your feeds for the first | |
| 434 | - # time. | |
| 435 | - | |
| 436 | - $result = $wpdb->query(" | |
| 437 | - INSERT INTO $wpdb->posts | |
| 438 | - SET | |
| 439 | - guid = '{$dbpost['guid']}', | |
| 440 | - post_author = '{$dbpost['post_author']}', | |
| 441 | - post_date = '{$dbpost['post_date']}', | |
| 442 | - post_date_gmt = '{$dbpost['post_date_gmt']}', | |
| 443 | - post_content = '{$dbpost['post_content']}'," | |
| 444 | - .(isset($dbpost['post_excerpt']) ? "post_excerpt = '{$dbpost['post_excerpt']}'," : "")." | |
| 445 | - post_title = '{$dbpost['post_title']}', | |
| 446 | - post_name = '{$dbpost['post_name']}', | |
| 447 | - post_modified = '{$dbpost['post_modified']}', | |
| 448 | - post_modified_gmt = '{$dbpost['post_modified_gmt']}', | |
| 449 | - comment_status = '{$dbpost['comment_status']}', | |
| 450 | - ping_status = '{$dbpost['ping_status']}', | |
| 451 | - post_status = '{$dbpost['post_status']}' | |
| 452 | - "); | |
| 453 | - $this->_wp_id = $wpdb->insert_id; | |
| 454 | - | |
| 455 | - $this->validate_post_id($dbpost, array(__CLASS__, __FUNCTION__)); | |
| 456 | - | |
| 457 | - // WordPress 1.5.x - 2.0.x | |
| 458 | - wp_set_post_cats('1', $this->wp_id(), $this->post['post_category']); | |
| 459 | - | |
| 460 | - // Since we are not going through official channels, we need to | |
| 461 | - // manually tell WordPress that we've published a new post. | |
| 462 | - // We need to make sure to do this in order for FeedWordPress | |
| 463 | - // to play well with the staticize-reloaded plugin (something | |
| 464 | - // that a large aggregator website is going to *want* to be | |
| 465 | - // able to use). | |
| 466 | - do_action('publish_post', $this->_wp_id); | |
| 1722 | + $dbpost['ID'] = $this->_wp_id; | |
| 467 | 1723 | endif; |
| 468 | - endif; | |
| 469 | - } /* SyndicatedPost::insert_new() */ | |
| 1724 | + | |
| 1725 | + // Sanity check: if the attempt to insert post | |
| 1726 | + // returned an error, then feeding that error | |
| 1727 | + // object in to _wp_put_post_revision() would | |
| 1728 | + // cause a fatal error. Better to break out. | |
| 1729 | + if (!is_wp_error($this->_wp_id)) : | |
| 1730 | + // Now that we've made sure the original exists, insert | |
| 1731 | + // this version here as a revision. | |
| 1732 | + $revision_id = _wp_put_post_revision($dbpost, /*autosave=*/ false); | |
| 470 | 1733 | |
| 471 | - function update_existing () { | |
| 472 | - global $wpdb; | |
| 1734 | + if (!$this->this_revision_needs_original_post()) : | |
| 1735 | + | |
| 1736 | + if ($this->this_revision_is_current()) : | |
| 473 | 1737 | |
| 474 | - // Why the fuck doesn't wp_insert_post already do this? | |
| 475 | - $dbpost = $this->normalize_post(/*new=*/ false); | |
| 476 | - if (!is_null($dbpost)) : | |
| 477 | - if ($this->use_api('wp_insert_post')) : | |
| 478 | - $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks | |
| 479 | - | |
| 480 | - // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data | |
| 481 | - add_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 482 | - | |
| 483 | - // Kludge to prevent kses filters from stripping the | |
| 484 | - // content of posts when updating without a logged in | |
| 485 | - // user who has `unfiltered_html` capability. | |
| 486 | - add_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 1738 | + wp_restore_post_revision($revision_id); | |
| 487 | 1739 | |
| 488 | - // Don't munge status fields that the user may have reset manually | |
| 489 | - if (function_exists('get_post_field')) : | |
| 490 | - $doNotMunge = array('post_status', 'comment_status', 'ping_status'); | |
| 491 | - foreach ($doNotMunge as $field) : | |
| 492 | - $dbpost[$field] = get_post_field($field, $this->wp_id()); | |
| 493 | - endforeach; | |
| 494 | - endif; | |
| 1740 | + else : | |
| 495 | 1741 | |
| 496 | - $this->_wp_id = wp_insert_post($dbpost); | |
| 497 | - | |
| 498 | - // Turn off ridiculous fucking kludges #1 and #2 | |
| 499 | - remove_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 500 | - remove_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 501 | - | |
| 502 | - $this->validate_post_id($dbpost, array(__CLASS__, __FUNCTION__)); | |
| 503 | - | |
| 504 | - // Unfortunately, as of WordPress 2.3, wp_insert_post() | |
| 505 | - // munges your post modified timestamp. | |
| 506 | - $result = $wpdb->query(" | |
| 507 | - UPDATE $wpdb->posts | |
| 508 | - SET | |
| 509 | - post_modified='{$dbpost['post_modified']}', | |
| 510 | - post_modified_gmt='{$dbpost['post_modified_gmt']}' | |
| 511 | - WHERE ID='{$this->_wp_id}' | |
| 512 | - "); | |
| 513 | - else : | |
| 514 | - | |
| 515 | - $result = $wpdb->query(" | |
| 516 | - UPDATE $wpdb->posts | |
| 517 | - SET | |
| 518 | - post_author = '{$dbpost['post_author']}', | |
| 519 | - post_content = '{$dbpost['post_content']}'," | |
| 520 | - .(isset($dbpost['post_excerpt']) ? "post_excerpt = '{$dbpost['post_excerpt']}'," : "")." | |
| 521 | - post_title = '{$dbpost['post_title']}', | |
| 522 | - post_name = '{$dbpost['post_name']}', | |
| 523 | - post_modified = '{$dbpost['post_modified']}', | |
| 524 | - post_modified_gmt = '{$dbpost['post_modified_gmt']}' | |
| 525 | - WHERE guid='{$dbpost['guid']}' | |
| 526 | - "); | |
| 527 | - | |
| 528 | - // WordPress 2.1.x and up | |
| 529 | - if (function_exists('wp_set_post_categories')) : | |
| 530 | - wp_set_post_categories($this->wp_id(), $this->post['post_category']); | |
| 531 | - // WordPress 1.5.x - 2.0.x | |
| 532 | - elseif (function_exists('wp_set_post_cats')) : | |
| 533 | - wp_set_post_cats('1', $this->wp_id(), $this->post['post_category']); | |
| 534 | - // This should never happen. | |
| 535 | - else : | |
| 536 | - FeedWordPress::critical_bug(__CLASS__.'::'.__FUNCTION.'(): no post categorizing function', array("dbpost" => $dbpost, "this" => $this), __LINE__); | |
| 1742 | + // If we do not activate this revision, then the | |
| 1743 | + // add_rss_meta will not be called, which is | |
| 1744 | + // more or less as it should be, but that means | |
| 1745 | + // we have to actively record this revision's | |
| 1746 | + // update hash from here. | |
| 1747 | + $postId = $this->post['ID']; | |
| 1748 | + $key = 'syndication_item_hash'; | |
| 1749 | + $hash = $this->update_hash(); | |
| 1750 | + FeedWordPress::diagnostic('syndicated_posts:meta_data', "Adding post meta-datum to post [$postId]: [$key] = ".FeedWordPress::val($hash, /*no newlines=*/ true)); | |
| 1751 | + add_post_meta( $postId, $key, $hash, /*unique=*/ false ); | |
| 1752 | + endif; | |
| 537 | 1753 | endif; |
| 538 | - | |
| 539 | - // Since we are not going through official channels, we need to | |
| 540 | - // manually tell WordPress that we've published a new post. | |
| 541 | - // We need to make sure to do this in order for FeedWordPress | |
| 542 | - // to play well with the staticize-reloaded plugin (something | |
| 543 | - // that a large aggregator website is going to *want* to be | |
| 544 | - // able to use). | |
| 545 | - do_action('edit_post', $this->post['ID']); | |
| 546 | 1754 | endif; |
| 1755 | + | |
| 1756 | + remove_action( | |
| 1757 | + /*hook=*/ 'transition_post_status', | |
| 1758 | + /*callback=*/ array($this, 'add_terms'), | |
| 1759 | + /*priority=*/ -10001, /* very early */ | |
| 1760 | + /*arguments=*/ 3 | |
| 1761 | + ); | |
| 1762 | + | |
| 1763 | + remove_action( | |
| 1764 | + /*hook=*/ 'transition_post_status', | |
| 1765 | + /*callback=*/ array($this, 'fix_post_modified_ts'), | |
| 1766 | + /*priority=*/ -10000, /* very early */ | |
| 1767 | + /*arguments=*/ 3 | |
| 1768 | + ); | |
| 1769 | + | |
| 1770 | + // Turn off ridiculous fucking kludges #1 and #2 | |
| 1771 | + remove_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 1772 | + foreach ($removed as $filter) : | |
| 1773 | + add_filter('content_save_pre', $filter); | |
| 1774 | + endforeach; | |
| 1775 | + | |
| 1776 | + $this->validate_post_id($dbpost, $update, array(__CLASS__, __FUNCTION__)); | |
| 1777 | + | |
| 1778 | + $ret = $this->_wp_id; | |
| 547 | 1779 | endif; |
| 1780 | + return $ret; | |
| 1781 | + } /* function SyndicatedPost::insert_post () */ | |
| 1782 | + | |
| 1783 | + function insert_new () { | |
| 1784 | + $this->insert_post(/*update=*/ false, 1); | |
| 1785 | + } /* SyndicatedPost::insert_new() */ | |
| 1786 | + | |
| 1787 | + function update_existing () { | |
| 1788 | + $this->insert_post(/*update=*/ true, 2); | |
| 548 | 1789 | } /* SyndicatedPost::update_existing() */ |
| 549 | 1790 | |
| 550 | 1791 | /** |
| 551 | 1792 | * SyndicatedPost::normalize_post() |
| @@ -555,33 +1796,104 @@ | ||
| 555 | 1796 | */ |
| 556 | 1797 | function normalize_post ($new = true) { |
| 557 | 1798 | global $wpdb; |
| 558 | 1799 | |
| 559 | - $out = array(); | |
| 1800 | + $out = $this->post; | |
| 560 | 1801 | |
| 561 | - // Why the fuck doesn't wp_insert_post already do this? | |
| 562 | - foreach ($this->post as $key => $value) : | |
| 563 | - if (is_string($value)) : | |
| 564 | - $out[$key] = $wpdb->escape($value); | |
| 565 | - else : | |
| 566 | - $out[$key] = $value; | |
| 567 | - endif; | |
| 568 | - endforeach; | |
| 569 | - | |
| 570 | - if (strlen($out['post_title'].$out['post_content'].$out['post_excerpt']) == 0) : | |
| 1802 | + $fullPost = $out['post_title'].$out['post_content']; | |
| 1803 | + $fullPost .= (isset($out['post_excerpt']) ? $out['post_excerpt'] : ''); | |
| 1804 | + if (strlen($fullPost) < 1) : | |
| 571 | 1805 | // FIXME: Option for filtering out empty posts |
| 572 | 1806 | endif; |
| 573 | 1807 | if (strlen($out['post_title'])==0) : |
| 574 | 1808 | $offset = (int) get_option('gmt_offset') * 60 * 60; |
| 575 | - $out['post_title'] = | |
| 576 | - $this->post['meta']['syndication_source'] | |
| 1809 | + if (isset($this->post['meta']['syndication_source'])) : | |
| 1810 | + $source_title = $this->post['meta']['syndication_source']; | |
| 1811 | + else : | |
| 1812 | + $feed_url = parse_url($this->post['meta']['syndication_feed']); | |
| 1813 | + $source_title = $feed_url['host']; | |
| 1814 | + endif; | |
| 1815 | + | |
| 1816 | + $out['post_title'] = $source_title | |
| 577 | 1817 | .' '.gmdate('Y-m-d H:i:s', $this->published() + $offset); |
| 578 | 1818 | // FIXME: Option for what to fill a blank title with... |
| 579 | 1819 | endif; |
| 580 | 1820 | |
| 1821 | + // Normalize the guid if necessary. | |
| 1822 | + $out['guid'] = SyndicatedPost::normalize_guid($out['guid']); | |
| 1823 | + | |
| 581 | 1824 | return $out; |
| 582 | 1825 | } |
| 583 | 1826 | |
| 1827 | + public function db_sanitize_post_check_encoding ($out) { | |
| 1828 | + // Check encoding recursively: every string field needs to be checked | |
| 1829 | + // for character encoding issues. This is a bit problematic because we | |
| 1830 | + // *should* be using DB_CHARSET, but DB_CHARSET sometimes has values | |
| 1831 | + // that work for MySQL but not for PHP mb_check_encoding. So instead | |
| 1832 | + // we must rely on WordPress setting blog_charset and hope that the user | |
| 1833 | + // has got their database encoding set up to roughly match | |
| 1834 | + $charset = get_option('blog_charset', 'utf8'); | |
| 1835 | + | |
| 1836 | + foreach ($out as $key => $value) : | |
| 1837 | + if (is_string($value)) : | |
| 1838 | + | |
| 1839 | + if (!function_exists('mb_check_encoding') or mb_check_encoding($value, $charset)) : | |
| 1840 | + $out[$key] = $value; | |
| 1841 | + else : | |
| 1842 | + $fromCharset = mb_detect_encoding($value, mb_detect_order(), /*strict=*/ true); | |
| 1843 | + $out[$key] = mb_convert_encoding($value, $charset, $fromCharset); | |
| 1844 | + endif; | |
| 1845 | + | |
| 1846 | + elseif (is_array($value)) : | |
| 1847 | + $out[$key] = $this->db_sanitize_post_check_encoding($value); | |
| 1848 | + | |
| 1849 | + else : | |
| 1850 | + $out[$key] = $value; | |
| 1851 | + endif; | |
| 1852 | + | |
| 1853 | + endforeach; | |
| 1854 | + | |
| 1855 | + return $out; | |
| 1856 | + } /* SyndicatedPost::db_sanitize_post_check_encoding () */ | |
| 1857 | + | |
| 1858 | + function db_sanitize_post ($out) { | |
| 1859 | + global $wp_db_version; | |
| 1860 | + | |
| 1861 | + $out = $this->db_sanitize_post_check_encoding($out); | |
| 1862 | + | |
| 1863 | + // < 3.6. Core API, including `wp_insert_post()`, expects | |
| 1864 | + // properly slashed data. If `wp_slash()` exists, then | |
| 1865 | + // this is after the big change-over in how data slashing | |
| 1866 | + // was handled. | |
| 1867 | + if (!function_exists('wp_slash')) : | |
| 1868 | + | |
| 1869 | + foreach ($out as $key => $value) : | |
| 1870 | + if (is_string($value)) : | |
| 1871 | + $out[$key] = esc_sql($value); | |
| 1872 | + else : | |
| 1873 | + $out[$key] = $value; | |
| 1874 | + endif; | |
| 1875 | + endforeach; | |
| 1876 | + | |
| 1877 | + // For revisions [@23416,@23554), core API expects | |
| 1878 | + // unslashed data. Cf. <https://core.trac.wordpress.org/browser/trunk/wp-includes/post.php?rev=23416> | |
| 1879 | + // NOOP for those revisions. | |
| 1880 | + | |
| 1881 | + // In revisions @23554 to present, `wp_insert_post()` | |
| 1882 | + // expects slashed data once again. | |
| 1883 | + // Cf. <https://core.trac.wordpress.org/changeset/23554/trunk/wp-includes/post.php?contextall=1> | |
| 1884 | + // But at least now we can use the wp_slash API function to do that. | |
| 1885 | + // Hooray. | |
| 1886 | + | |
| 1887 | + elseif ($wp_db_version >= 23524) : | |
| 1888 | + | |
| 1889 | + $out = wp_slash($out); | |
| 1890 | + | |
| 1891 | + endif; | |
| 1892 | + | |
| 1893 | + return $out; | |
| 1894 | + } | |
| 1895 | + | |
| 584 | 1896 | /** |
| 585 | 1897 | * SyndicatedPost::validate_post_id() |
| 586 | 1898 | * |
| 587 | 1899 | * @param array $dbpost An array representing the post we attempted to insert or update |
| @@ -586,35 +1898,57 @@ | ||
| 586 | 1898 | * |
| 587 | 1899 | * @param array $dbpost An array representing the post we attempted to insert or update |
| 588 | 1900 | * @param mixed $ns A string or array representing the namespace (class, method) whence this method was called. |
| 589 | 1901 | */ |
| 590 | - function validate_post_id ($dbpost, $ns) { | |
| 1902 | + function validate_post_id ($dbpost, $is_update, $ns) { | |
| 591 | 1903 | if (is_array($ns)) : $ns = implode('::', $ns); |
| 592 | 1904 | else : $ns = (string) $ns; endif; |
| 593 | - | |
| 1905 | + | |
| 594 | 1906 | // This should never happen. |
| 595 | 1907 | if (!is_numeric($this->_wp_id) or ($this->_wp_id == 0)) : |
| 596 | - FeedWordPress::critical_bug( | |
| 597 | - /*name=*/ $ns.'::_wp_id', | |
| 1908 | + $verb = ($is_update ? 'update existing' : 'insert new'); | |
| 1909 | + $guid = $this->guid(); | |
| 1910 | + $url = $this->permalink(); | |
| 1911 | + $feed = $this->link->uri(array('add_params' => true)); | |
| 1912 | + | |
| 1913 | + // wp_insert_post failed. Diagnostics, or barf up a critical bug | |
| 1914 | + // notice if we are in debug mode. | |
| 1915 | + $mesg = "Failed to $verb item [$guid]. WordPress API returned no valid post ID.\n" | |
| 1916 | + ."\t\tID = ".serialize($this->_wp_id)."\n" | |
| 1917 | + ."\t\tURL = ".MyPHP::val($url) | |
| 1918 | + ."\t\tFeed = ".MyPHP::val($feed); | |
| 1919 | + | |
| 1920 | + FeedWordPress::diagnostic('updated_feeds:errors', "WordPress API error: $mesg"); | |
| 1921 | + FeedWordPress::diagnostic('feed_items:rejected', $mesg); | |
| 1922 | + | |
| 1923 | + $mesg = <<<EOM | |
| 1924 | +The WordPress API returned an invalid post ID | |
| 1925 | + when FeedWordPress tried to $verb item $guid | |
| 1926 | + [URL: $url] | |
| 1927 | + from the feed at $feed | |
| 1928 | + | |
| 1929 | +$ns::_wp_id | |
| 1930 | +EOM; | |
| 1931 | + FeedWordPress::noncritical_bug( | |
| 1932 | + /*message=*/ $mesg, | |
| 598 | 1933 | /*var =*/ array( |
| 599 | 1934 | "\$this->_wp_id" => $this->_wp_id, |
| 600 | 1935 | "\$dbpost" => $dbpost, |
| 601 | - "\$this" => $this | |
| 602 | 1936 | ), |
| 603 | - /*line # =*/ __LINE__ | |
| 1937 | + /*line # =*/ __LINE__, /*filename=*/ __FILE__ | |
| 604 | 1938 | ); |
| 605 | 1939 | endif; |
| 606 | 1940 | } /* SyndicatedPost::validate_post_id() */ |
| 607 | - | |
| 1941 | + | |
| 608 | 1942 | /** |
| 609 | - * SyndicatedPost::fix_revision_meta() - Fixes the way WP 2.6+ fucks up | |
| 610 | - * meta-data (authorship, etc.) when storing revisions of an updated | |
| 611 | - * syndicated post. | |
| 1943 | + * SyndicatedPost::fix_revision_meta() - Ensures that we get the meta | |
| 1944 | + * data (authorship, guid, etc.) that we want when storing revisions of | |
| 1945 | + * a syndicated post. | |
| 612 | 1946 | * |
| 613 | - * In their infinite wisdom, the WordPress coders have made it completely | |
| 614 | - * impossible for a plugin that uses wp_insert_post() to set certain | |
| 615 | - * meta-data (such as the author) when you store an old revision of an | |
| 616 | - * updated post. Instead, it uses the WordPress defaults (= currently | |
| 1947 | + * In their infinite wisdom, the WordPress coders seem to have made it | |
| 1948 | + * completely impossible for a plugin that uses wp_insert_post() to set | |
| 1949 | + * certain meta-data (such as the author) when you store an old revision | |
| 1950 | + * of an updated post. Instead, it uses the WordPress defaults (= cur. | |
| 617 | 1951 | * active user ID if the process is running with a user logged in, or |
| 618 | 1952 | * = #0 if there is no user logged in). This results in bogus authorship |
| 619 | 1953 | * data for revisions that are syndicated from off the feed, unless we |
| 620 | 1954 | * use a ridiculous kludge like this to end-run the munging of meta-data |
| @@ -623,110 +1957,203 @@ | ||
| 623 | 1957 | * @param int $revision_id The revision ID to fix up meta-data |
| 624 | 1958 | */ |
| 625 | 1959 | function fix_revision_meta ($revision_id) { |
| 626 | 1960 | global $wpdb; |
| 1961 | + | |
| 1962 | + $post_author = (int) $this->post['post_author']; | |
| 1963 | + | |
| 1964 | + $revision_id = (int) $revision_id; | |
| 627 | 1965 | |
| 628 | - $post_author = (int) $this->post['post_author']; | |
| 1966 | + // Let's fix the author. | |
| 1967 | + set_post_field('post_author', $this->post['post_author'], $revision_id); | |
| 629 | 1968 | |
| 630 | - $revision_id = (int) $revision_id; | |
| 631 | - $wpdb->query(" | |
| 632 | - UPDATE $wpdb->posts | |
| 633 | - SET post_author={$this->post['post_author']} | |
| 634 | - WHERE post_type = 'revision' AND ID='$revision_id' | |
| 635 | - "); | |
| 1969 | + // Let's fix the GUID to a dummy URL with the update hash. | |
| 1970 | + set_post_field('guid', 'http://feedwordpress.radgeek.com/?rev='.$this->update_hash(), $revision_id); | |
| 1971 | + | |
| 1972 | + // Let's fire an event for add-ons and filters | |
| 1973 | + do_action('syndicated_post_fix_revision_meta', $revision_id, $this); | |
| 1974 | + | |
| 636 | 1975 | } /* SyndicatedPost::fix_revision_meta () */ |
| 637 | 1976 | |
| 638 | 1977 | /** |
| 639 | - * SyndicatedPost::avoid_kses_munge() -- If FeedWordPress is processing | |
| 640 | - * an automatic update, that generally means that wp_insert_post() is | |
| 641 | - * being called under the user credentials of whoever is viewing the | |
| 642 | - * blog at the time -- usually meaning no user at all. But if WordPress | |
| 643 | - * gets a wp_insert_post() when current_user_can('unfiltered_html') is | |
| 644 | - * false, it will run the content of the post through a kses function | |
| 645 | - * that strips out lots of HTML tags -- notably <object> and some others. | |
| 646 | - * This causes problems for syndicating (for example) feeds that contain | |
| 647 | - * YouTube videos. It also produces an unexpected asymmetry between | |
| 648 | - * automatically-initiated updates and updates initiated manually from | |
| 649 | - * the WordPress Dashboard (which are usually initiated under the | |
| 650 | - * credentials of a logged-in admin, and so don't get run through the | |
| 651 | - * kses function). So, to avoid the whole mess, what we do here is | |
| 652 | - * just forcibly disable the kses munging for a single syndicated post, | |
| 653 | - * by restoring the contents of the `post_content` field. | |
| 1978 | + * SyndicatedPost::add_terms() -- if FeedWordPress is processing an | |
| 1979 | + * automatic update, that generally means that wp_insert_post() is being | |
| 1980 | + * called under the user credentials of whoever is viewing the blog at | |
| 1981 | + * the time -- which usually means no user at all. But wp_insert_post() | |
| 1982 | + * checks current_user_can() before assigning any of the terms in a | |
| 1983 | + * post's tax_input structure -- which is unfortunate, since | |
| 1984 | + * current_user_can() always returns FALSE when there is no current user | |
| 1985 | + * logged in. Meaning that automatic updates get no terms assigned. | |
| 654 | 1986 | * |
| 655 | - * @param string $content The content of the post, after other filters have gotten to it | |
| 656 | - * @return string The original content of the post, before other filters had a chance to munge it. | |
| 1987 | + * So, wp_insert_post() is not going to do the term assignments for us. | |
| 1988 | + * If you want something done right.... | |
| 1989 | + * | |
| 1990 | + * @param string $new_status Unused action parameter. | |
| 1991 | + * @param string $old_status Unused action parameter. | |
| 1992 | + * @param object $post The database record for the post just inserted. | |
| 657 | 1993 | */ |
| 658 | - function avoid_kses_munge ($content) { | |
| 659 | - global $wpdb; | |
| 660 | - return $wpdb->escape($this->post['post_content']); | |
| 661 | - } | |
| 662 | - | |
| 663 | - // SyndicatedPost::add_rss_meta: adds interesting meta-data to each entry | |
| 664 | - // using the space for custom keys. The set of keys and values to add is | |
| 665 | - // specified by the keys and values of $post['meta']. This is used to | |
| 666 | - // store anything that the WordPress user might want to access from a | |
| 667 | - // template concerning the post's original source that isn't provided | |
| 668 | - // for by standard WP meta-data (i.e., any interesting data about the | |
| 669 | - // syndicated post other than author, title, timestamp, categories, and | |
| 670 | - // guid). It's also used to hook into WordPress's support for | |
| 671 | - // enclosures. | |
| 672 | - function add_rss_meta () { | |
| 1994 | + function add_terms ($new_status, $old_status, $post) { | |
| 1995 | + | |
| 1996 | + if ($new_status!='inherit') : // Bail if we are creating a revision. | |
| 1997 | + if ( is_array($this->post) and isset($this->post['tax_input']) and is_array($this->post['tax_input']) ) : | |
| 1998 | + foreach ($this->post['tax_input'] as $taxonomy => $terms) : | |
| 1999 | + if (is_array($terms)) : | |
| 2000 | + $terms = array_filter($terms); // strip out empties | |
| 2001 | + endif; | |
| 2002 | + | |
| 2003 | + $res = wp_set_post_terms( | |
| 2004 | + /*post_id=*/ $post->ID, | |
| 2005 | + /*terms=*/ $terms, | |
| 2006 | + /*taxonomy=*/ $taxonomy | |
| 2007 | + ); | |
| 2008 | + | |
| 2009 | + FeedWordPress::diagnostic( | |
| 2010 | + 'syndicated_posts:categories', | |
| 2011 | + 'Category: post('.json_encode($post->ID).') '.$taxonomy | |
| 2012 | + .' := ' | |
| 2013 | + .json_encode($terms) | |
| 2014 | + .' / result: ' | |
| 2015 | + .json_encode($res) | |
| 2016 | + ); | |
| 2017 | + | |
| 2018 | + endforeach; | |
| 2019 | + endif; | |
| 2020 | + endif; | |
| 2021 | + | |
| 2022 | + } /* SyndicatedPost::add_terms () */ | |
| 2023 | + | |
| 2024 | + /** | |
| 2025 | + * SyndicatedPost::fix_post_modified_ts() -- We would like to set | |
| 2026 | + * post_modified and post_modified_gmt to reflect the value of | |
| 2027 | + * <atom:updated> or equivalent elements on the feed. Unfortunately, | |
| 2028 | + * wp_insert_post() refuses to acknowledge explicitly-set post_modified | |
| 2029 | + * fields and overwrites them, either with the post_date (if new) or the | |
| 2030 | + * current timestamp (if updated). | |
| 2031 | + * | |
| 2032 | + * So, wp_insert_post() is not going to do the last-modified assignments | |
| 2033 | + * for us. If you want something done right.... | |
| 2034 | + * | |
| 2035 | + * @param string $new_status Unused action parameter. | |
| 2036 | + * @param string $old_status Unused action parameter. | |
| 2037 | + * @param object $post The database record for the post just inserted. | |
| 2038 | + */ | |
| 2039 | + function fix_post_modified_ts ($new_status, $old_status, $post) { | |
| 673 | 2040 | global $wpdb; |
| 674 | - if ( is_array($this->post) and isset($this->post['meta']) and is_array($this->post['meta']) ) : | |
| 675 | - $postId = $this->wp_id(); | |
| 676 | - | |
| 677 | - // Aggregated posts should NOT send out pingbacks. | |
| 678 | - // WordPress 2.1-2.2 claim you can tell them not to | |
| 679 | - // using $post_pingback, but they don't listen, so we | |
| 680 | - // make sure here. | |
| 681 | - $result = $wpdb->query(" | |
| 682 | - DELETE FROM $wpdb->postmeta | |
| 683 | - WHERE post_id='$postId' AND meta_key='_pingme' | |
| 684 | - "); | |
| 2041 | + if ($new_status!='inherit') : // Bail if we are creating a revision. | |
| 2042 | + $wpdb->update( $wpdb->posts, /*data=*/ array( | |
| 2043 | + 'post_modified' => $this->post['post_modified'], | |
| 2044 | + 'post_modified_gmt' => $this->post['post_modified_gmt'], | |
| 2045 | + ), /*where=*/ array('ID' => $post->ID) ); | |
| 2046 | + endif; | |
| 2047 | + } /* SyndicatedPost::fix_post_modified_ts () */ | |
| 685 | 2048 | |
| 686 | - foreach ( $this->post['meta'] as $key => $values ) : | |
| 2049 | + /** | |
| 2050 | + * SyndicatedPost::add_rss_meta: adds interesting meta-data to each entry | |
| 2051 | + * using the space for custom keys. The set of keys and values to add is | |
| 2052 | + * specified by the keys and values of $post['meta']. This is used to | |
| 2053 | + * store anything that the WordPress user might want to access from a | |
| 2054 | + * template concerning the post's original source that isn't provided | |
| 2055 | + * for by standard WP meta-data (i.e., any interesting data about the | |
| 2056 | + * syndicated post other than author, title, timestamp, categories, and | |
| 2057 | + * guid). It's also used to hook into WordPress's support for | |
| 2058 | + * enclosures. | |
| 2059 | + * | |
| 2060 | + * @param string $new_status Unused action parameter. | |
| 2061 | + * @param string $old_status Unused action parameter. | |
| 2062 | + * @param object $post The database record for the post just inserted. | |
| 2063 | + */ | |
| 2064 | + function add_rss_meta ($new_status, $old_status, $post) { | |
| 2065 | + global $wpdb; | |
| 2066 | + if ($new_status!='inherit') : // Bail if we are creating a revision. | |
| 2067 | + FeedWordPress::diagnostic('syndicated_posts:meta_data', 'Adding post meta-data: {'.implode(", ", array_keys($this->post['meta'])).'}'); | |
| 687 | 2068 | |
| 688 | - $key = $wpdb->escape($key); | |
| 2069 | + if ( is_array($this->post) and isset($this->post['meta']) and is_array($this->post['meta']) ) : | |
| 2070 | + $postId = $post->ID; | |
| 689 | 2071 | |
| 690 | - // If this is an update, clear out the old | |
| 691 | - // values to avoid duplication. | |
| 2072 | + // Aggregated posts should NOT send out pingbacks. | |
| 2073 | + // WordPress 2.1-2.2 claim you can tell them not to | |
| 2074 | + // using $post_pingback, but they don't listen, so we | |
| 2075 | + // make sure here. | |
| 692 | 2076 | $result = $wpdb->query(" |
| 693 | 2077 | DELETE FROM $wpdb->postmeta |
| 694 | - WHERE post_id='$postId' AND meta_key='$key' | |
| 2078 | + WHERE post_id='$postId' AND meta_key='_pingme' | |
| 695 | 2079 | "); |
| 696 | 2080 | |
| 697 | - // Allow for either a single value or an array | |
| 698 | - if (!is_array($values)) $values = array($values); | |
| 699 | - foreach ( $values as $value ) : | |
| 700 | - $value = $wpdb->escape($value); | |
| 2081 | + foreach ( $this->post['meta'] as $key => $values ) : | |
| 2082 | + $eKey = esc_sql($key); | |
| 2083 | + | |
| 2084 | + // If this is an update, clear out the old | |
| 2085 | + // values to avoid duplication. | |
| 701 | 2086 | $result = $wpdb->query(" |
| 702 | - INSERT INTO $wpdb->postmeta | |
| 703 | - SET | |
| 704 | - post_id='$postId', | |
| 705 | - meta_key='$key', | |
| 706 | - meta_value='$value' | |
| 2087 | + DELETE FROM $wpdb->postmeta | |
| 2088 | + WHERE post_id='$postId' AND meta_key='$eKey' | |
| 707 | 2089 | "); |
| 708 | - if (!$result) : | |
| 709 | - $err = mysql_error(); | |
| 710 | - if (FEEDWORDPRESS_DEBUG) : | |
| 711 | - echo "[DEBUG:".date('Y-m-d H:i:S')."][feedwordpress]: post metadata insertion FAILED for field '$key' := '$value': [$err]"; | |
| 712 | - endif; | |
| 713 | - endif; | |
| 2090 | + | |
| 2091 | + // Allow for either a single value or an array | |
| 2092 | + if (!is_array($values)) $values = array($values); | |
| 2093 | + foreach ( $values as $value ) : | |
| 2094 | + FeedWordPress::diagnostic('syndicated_posts:meta_data', "Adding post meta-datum to post [$postId]: [$key] = ".MyPHP::val($value, /*no newlines=*/ true)); | |
| 2095 | + add_post_meta($postId, $key, $value, /*unique=*/ false); | |
| 2096 | + endforeach; | |
| 714 | 2097 | endforeach; |
| 715 | - endforeach; | |
| 2098 | + endif; | |
| 716 | 2099 | endif; |
| 717 | 2100 | } /* SyndicatedPost::add_rss_meta () */ |
| 718 | 2101 | |
| 719 | - // SyndicatedPost::author_id (): get the ID for an author name from | |
| 720 | - // the feed. Create the author if necessary. | |
| 2102 | + /** | |
| 2103 | + * SyndicatedPost::author_id (): get the ID for an author name from | |
| 2104 | + * the feed. Create the author if necessary. | |
| 2105 | + * | |
| 2106 | + * @param string $unfamiliar_author | |
| 2107 | + * | |
| 2108 | + * @return NULL|int The numeric ID of the author to attribute the post to | |
| 2109 | + * NULL if the post should be filtered out. | |
| 2110 | + */ | |
| 721 | 2111 | function author_id ($unfamiliar_author = 'create') { |
| 722 | 2112 | global $wpdb; |
| 723 | 2113 | |
| 724 | - $a = $this->author(); | |
| 725 | - $author = $a['name']; | |
| 726 | - $email = $a['email']; | |
| 727 | - $url = $a['uri']; | |
| 2114 | + $a = $this->named['author']; | |
| 728 | 2115 | |
| 2116 | + $source = $this->source(); | |
| 2117 | + $forbidden = apply_filters('feedwordpress_forbidden_author_names', | |
| 2118 | + array('admin', 'administrator', 'www', 'root')); | |
| 2119 | + | |
| 2120 | + // Prepare the list of candidates to try for author name: name from | |
| 2121 | + // feed, original source title (if any), immediate source title live | |
| 2122 | + // from feed, subscription title, prettied version of feed homepage URL, | |
| 2123 | + // prettied version of feed URL, or, failing all, use "unknown author" | |
| 2124 | + // as last resort | |
| 2125 | + | |
| 2126 | + $candidates = array(); | |
| 2127 | + $candidates[] = $a['name']; | |
| 2128 | + if (!is_null($source)) : $candidates[] = $source['title']; endif; | |
| 2129 | + $candidates[] = $this->link->name(/*fromFeed=*/ true); | |
| 2130 | + $candidates[] = $this->link->name(/*fromFeed=*/ false); | |
| 2131 | + if (strlen($this->link->homepage()) > 0) : $candidates[] = feedwordpress_display_url($this->link->homepage()); endif; | |
| 2132 | + $candidates[] = feedwordpress_display_url($this->link->uri()); | |
| 2133 | + $candidates[] = 'unknown author'; | |
| 2134 | + | |
| 2135 | + // Pick the first one that works from the list, screening against empty | |
| 2136 | + // or forbidden names. | |
| 2137 | + | |
| 2138 | + $author = NULL; | |
| 2139 | + while (is_null($author) and ($candidate = each($candidates))) : | |
| 2140 | + if (!is_null($candidate['value']) | |
| 2141 | + and (strlen(trim($candidate['value'])) > 0) | |
| 2142 | + and !in_array(strtolower(trim($candidate['value'])), $forbidden)) : | |
| 2143 | + $author = $candidate['value']; | |
| 2144 | + endif; | |
| 2145 | + endwhile; | |
| 2146 | + | |
| 2147 | + $email = (isset($a['email']) ? $a['email'] : NULL); | |
| 2148 | + $authorUrl = (isset($a['uri']) ? $a['uri'] : NULL); | |
| 2149 | + | |
| 2150 | + | |
| 2151 | + $hostUrl = $this->link->homepage(); | |
| 2152 | + if (is_null($hostUrl) or (strlen($hostUrl) < 0)) : | |
| 2153 | + $hostUrl = $this->link->uri(); | |
| 2154 | + endif; | |
| 2155 | + | |
| 729 | 2156 | $match_author_by_email = !('yes' == get_option("feedwordpress_do_not_match_author_by_email")); |
| 730 | 2157 | if ($match_author_by_email and !FeedWordPress::is_null_email($email)) : |
| 731 | 2158 | $test_email = $email; |
| 732 | 2159 | else : |
| @@ -734,21 +2161,43 @@ | ||
| 734 | 2161 | endif; |
| 735 | 2162 | |
| 736 | 2163 | // Never can be too careful... |
| 737 | 2164 | $login = sanitize_user($author, /*strict=*/ true); |
| 2165 | + | |
| 2166 | + // Possible for, e.g., foreign script author names | |
| 2167 | + if (strlen($login) < 1) : | |
| 2168 | + // No usable characters in author name for a login. | |
| 2169 | + // (Sometimes results from, e.g., foreign scripts.) | |
| 2170 | + // | |
| 2171 | + // We just need *something* in Western alphanumerics, | |
| 2172 | + // so let's try the domain name. | |
| 2173 | + // | |
| 2174 | + // Uniqueness will be guaranteed below if necessary. | |
| 2175 | + | |
| 2176 | + $url = parse_url($hostUrl); | |
| 2177 | + | |
| 2178 | + $login = sanitize_user($url['host'], /*strict=*/ true); | |
| 2179 | + if (strlen($login) < 1) : | |
| 2180 | + // This isn't working. Frak it. | |
| 2181 | + $login = 'syndicated'; | |
| 2182 | + endif; | |
| 2183 | + endif; | |
| 2184 | + | |
| 738 | 2185 | $login = apply_filters('pre_user_login', $login); |
| 739 | 2186 | |
| 740 | 2187 | $nice_author = sanitize_title($author); |
| 741 | 2188 | $nice_author = apply_filters('pre_user_nicename', $nice_author); |
| 742 | 2189 | |
| 743 | - $reg_author = $wpdb->escape(preg_quote($author)); | |
| 744 | - $author = $wpdb->escape($author); | |
| 745 | - $email = $wpdb->escape($email); | |
| 746 | - $test_email = $wpdb->escape($test_email); | |
| 747 | - $url = $wpdb->escape($url); | |
| 2190 | + $reg_author = esc_sql(preg_quote($author)); | |
| 2191 | + $author = esc_sql($author); | |
| 2192 | + $email = esc_sql($email); | |
| 2193 | + $test_email = esc_sql($test_email); | |
| 2194 | + $authorUrl = esc_sql($authorUrl); | |
| 748 | 2195 | |
| 749 | 2196 | // Check for an existing author rule.... |
| 750 | - if (isset($this->link->settings['map authors']['name'][strtolower(trim($author))])) : | |
| 2197 | + if (isset($this->link->settings['map authors']['name']['*'])) : | |
| 2198 | + $author_rule = $this->link->settings['map authors']['name']['*']; | |
| 2199 | + elseif (isset($this->link->settings['map authors']['name'][strtolower(trim($author))])) : | |
| 751 | 2200 | $author_rule = $this->link->settings['map authors']['name'][strtolower(trim($author))]; |
| 752 | 2201 | else : |
| 753 | 2202 | $author_rule = NULL; |
| 754 | 2203 | endif; |
| @@ -759,69 +2208,38 @@ | ||
| 759 | 2208 | |
| 760 | 2209 | // User name is filtered out |
| 761 | 2210 | elseif ('filter' == $author_rule) : |
| 762 | 2211 | $id = NULL; |
| 763 | - | |
| 2212 | + | |
| 764 | 2213 | else : |
| 765 | 2214 | // Check the database for an existing author record that might fit |
| 766 | 2215 | |
| 767 | - #-- WordPress 2.0+ | |
| 768 | - if (fwp_test_wp_version(FWP_SCHEMA_HAS_USERMETA)) : | |
| 2216 | + // First try the user core data table. | |
| 2217 | + $id = $wpdb->get_var( | |
| 2218 | + "SELECT ID FROM $wpdb->users | |
| 2219 | + WHERE TRIM(LCASE(display_name)) = TRIM(LCASE('$author')) | |
| 2220 | + OR TRIM(LCASE(user_login)) = TRIM(LCASE('$author')) | |
| 2221 | + OR ( | |
| 2222 | + LENGTH(TRIM(LCASE(user_email))) > 0 | |
| 2223 | + AND TRIM(LCASE(user_email)) = TRIM(LCASE('$test_email')) | |
| 2224 | + )"); | |
| 769 | 2225 | |
| 770 | - // First try the user core data table. | |
| 2226 | + // If that fails, look for aliases in the user meta data table | |
| 2227 | + if (is_null($id)) : | |
| 771 | 2228 | $id = $wpdb->get_var( |
| 772 | - "SELECT ID FROM $wpdb->users | |
| 2229 | + "SELECT user_id FROM $wpdb->usermeta | |
| 773 | 2230 | WHERE |
| 774 | - TRIM(LCASE(user_login)) = TRIM(LCASE('$login')) | |
| 2231 | + (meta_key = 'description' AND TRIM(LCASE(meta_value)) = TRIM(LCASE('$author'))) | |
| 775 | 2232 | OR ( |
| 776 | - LENGTH(TRIM(LCASE(user_email))) > 0 | |
| 777 | - AND TRIM(LCASE(user_email)) = TRIM(LCASE('$test_email')) | |
| 778 | - ) | |
| 779 | - OR TRIM(LCASE(user_nicename)) = TRIM(LCASE('$nice_author')) | |
| 780 | - "); | |
| 781 | - | |
| 782 | - // If that fails, look for aliases in the user meta data table | |
| 783 | - if (is_null($id)) : | |
| 784 | - $id = $wpdb->get_var( | |
| 785 | - "SELECT user_id FROM $wpdb->usermeta | |
| 786 | - WHERE | |
| 787 | - (meta_key = 'description' AND TRIM(LCASE(meta_value)) = TRIM(LCASE('$author'))) | |
| 788 | - OR ( | |
| 789 | - meta_key = 'description' | |
| 790 | - AND TRIM(LCASE(meta_value)) | |
| 791 | - RLIKE CONCAT( | |
| 792 | - '(^|\\n)a\\.?k\\.?a\\.?( |\\t)*:?( |\\t)*', | |
| 793 | - TRIM(LCASE('$reg_author')), | |
| 794 | - '( |\\t|\\r)*(\\n|\$)' | |
| 795 | - ) | |
| 796 | - ) | |
| 797 | - "); | |
| 798 | - endif; | |
| 799 | - | |
| 800 | - #-- WordPress 1.5.x | |
| 801 | - else : | |
| 802 | - $id = $wpdb->get_var( | |
| 803 | - "SELECT ID from $wpdb->users | |
| 804 | - WHERE | |
| 805 | - TRIM(LCASE(user_login)) = TRIM(LCASE('$login')) OR | |
| 806 | - ( | |
| 807 | - LENGTH(TRIM(LCASE(user_email))) > 0 | |
| 808 | - AND TRIM(LCASE(user_email)) = TRIM(LCASE('$test_email')) | |
| 809 | - ) OR | |
| 810 | - TRIM(LCASE(user_firstname)) = TRIM(LCASE('$author')) OR | |
| 811 | - TRIM(LCASE(user_nickname)) = TRIM(LCASE('$author')) OR | |
| 812 | - TRIM(LCASE(user_nicename)) = TRIM(LCASE('$nice_author')) OR | |
| 813 | - TRIM(LCASE(user_description)) = TRIM(LCASE('$author')) OR | |
| 814 | - ( | |
| 815 | - LOWER(user_description) | |
| 2233 | + meta_key = 'description' | |
| 2234 | + AND TRIM(LCASE(meta_value)) | |
| 816 | 2235 | RLIKE CONCAT( |
| 817 | 2236 | '(^|\\n)a\\.?k\\.?a\\.?( |\\t)*:?( |\\t)*', |
| 818 | - LCASE('$reg_author'), | |
| 2237 | + TRIM(LCASE('$reg_author')), | |
| 819 | 2238 | '( |\\t|\\r)*(\\n|\$)' |
| 820 | 2239 | ) |
| 821 | 2240 | ) |
| 822 | 2241 | "); |
| 823 | - | |
| 824 | 2242 | endif; |
| 825 | 2243 | |
| 826 | 2244 | // ... if you don't find one, then do what you need to do |
| 827 | 2245 | if (is_null($id)) : |
| @@ -827,8 +2245,16 @@ | ||
| 827 | 2245 | if (is_null($id)) : |
| 828 | 2246 | if ($unfamiliar_author === 'create') : |
| 829 | 2247 | $userdata = array(); |
| 830 | 2248 | |
| 2249 | + // WordPress 3 is going to pitch a fit if we attempt to register | |
| 2250 | + // more than one user account with an empty e-mail address, so we | |
| 2251 | + // need *something* here. Ugh. | |
| 2252 | + if (strlen($email) == 0 or FeedWordPress::is_null_email($email)) : | |
| 2253 | + $url = parse_url($hostUrl); | |
| 2254 | + $email = $nice_author.'@'.$url['host']; | |
| 2255 | + endif; | |
| 2256 | + | |
| 831 | 2257 | #-- user table data |
| 832 | 2258 | $userdata['ID'] = NULL; // new user |
| 833 | 2259 | $userdata['user_login'] = $login; |
| 834 | 2260 | $userdata['user_nicename'] = $nice_author; |
| @@ -833,12 +2259,43 @@ | ||
| 833 | 2259 | $userdata['user_login'] = $login; |
| 834 | 2260 | $userdata['user_nicename'] = $nice_author; |
| 835 | 2261 | $userdata['user_pass'] = substr(md5(uniqid(microtime())), 0, 6); // just something random to lock it up |
| 836 | 2262 | $userdata['user_email'] = $email; |
| 837 | - $userdata['user_url'] = $url; | |
| 2263 | + $userdata['user_url'] = $authorUrl; | |
| 2264 | + $userdata['nickname'] = $author; | |
| 2265 | + $parts = preg_split('/\s+/', trim($author), 2); | |
| 2266 | + if (isset($parts[0])) : $userdata['first_name'] = $parts[0]; endif; | |
| 2267 | + if (isset($parts[1])) : $userdata['last_name'] = $parts[1]; endif; | |
| 838 | 2268 | $userdata['display_name'] = $author; |
| 839 | - | |
| 840 | - $id = wp_insert_user($userdata); | |
| 2269 | + $userdata['role'] = 'contributor'; | |
| 2270 | + | |
| 2271 | + do { // Keep trying until you get it right. Or until PHP crashes, I guess. | |
| 2272 | + $id = wp_insert_user($userdata); | |
| 2273 | + if (is_wp_error($id)) : | |
| 2274 | + $codes = $id->get_error_code(); | |
| 2275 | + switch ($codes) : | |
| 2276 | + case 'empty_user_login' : | |
| 2277 | + case 'existing_user_login' : | |
| 2278 | + // Add a random disambiguator | |
| 2279 | + $userdata['user_login'] .= substr(md5(uniqid(microtime())), 0, 6); | |
| 2280 | + break; | |
| 2281 | + case 'user_nicename_too_long' : | |
| 2282 | + // Add a limited 50 caracters user_nicename based on user_login | |
| 2283 | + $userdata['user_nicename'] = mb_substr( $userdata['user_login'], 0, 50 ); | |
| 2284 | + break; | |
| 2285 | + case 'existing_user_email' : | |
| 2286 | + // No disassemble! | |
| 2287 | + $parts = explode('@', $userdata['user_email'], 2); | |
| 2288 | + | |
| 2289 | + // Add a random disambiguator as a gmail-style username extension | |
| 2290 | + $parts[0] .= '+'.substr(md5(uniqid(microtime())), 0, 6); | |
| 2291 | + | |
| 2292 | + // Reassemble | |
| 2293 | + $userdata['user_email'] = $parts[0].'@'.$parts[1]; | |
| 2294 | + break; | |
| 2295 | + endswitch; | |
| 2296 | + endif; | |
| 2297 | + } while (is_wp_error($id)); | |
| 841 | 2298 | elseif (is_numeric($unfamiliar_author) and get_userdata((int) $unfamiliar_author)) : |
| 842 | 2299 | $id = (int) $unfamiliar_author; |
| 843 | 2300 | elseif ($unfamiliar_author === 'default') : |
| 844 | 2301 | $id = 1; |
| @@ -847,432 +2304,30 @@ | ||
| 847 | 2304 | endif; |
| 848 | 2305 | |
| 849 | 2306 | if ($id) : |
| 850 | 2307 | $this->link->settings['map authors']['name'][strtolower(trim($author))] = $id; |
| 851 | - endif; | |
| 852 | - return $id; | |
| 853 | - } // function SyndicatedPost::author_id () | |
| 854 | 2308 | |
| 855 | - // look up (and create) category ids from a list of categories | |
| 856 | - function category_ids ($cats, $unfamiliar_category = 'create', $tags_too = false) { | |
| 857 | - global $wpdb; | |
| 858 | - | |
| 859 | - // We need to normalize whitespace because (1) trailing | |
| 860 | - // whitespace can cause PHP and MySQL not to see eye to eye on | |
| 861 | - // VARCHAR comparisons for some versions of MySQL (cf. | |
| 862 | - // <http://dev.mysql.com/doc/mysql/en/char.html>), and (2) | |
| 863 | - // because I doubt most people want to make a semantic | |
| 864 | - // distinction between 'Computers' and 'Computers ' | |
| 865 | - $cats = array_map('trim', $cats); | |
| 866 | - | |
| 867 | - $tags = array(); | |
| 868 | - | |
| 869 | - $cat_ids = array (); | |
| 870 | - foreach ($cats as $cat_name) : | |
| 871 | - if (preg_match('/^{#([0-9]+)}$/', $cat_name, $backref)) : | |
| 872 | - $cat_id = (int) $backref[1]; | |
| 873 | - if (function_exists('is_term') and is_term($cat_id, 'category')) : | |
| 874 | - $cat_ids[] = $cat_id; | |
| 875 | - elseif (get_category($cat_id)) : | |
| 876 | - $cat_ids[] = $cat_id; | |
| 877 | - endif; | |
| 878 | - elseif (strlen($cat_name) > 0) : | |
| 879 | - $esc = $wpdb->escape($cat_name); | |
| 880 | - $resc = $wpdb->escape(preg_quote($cat_name)); | |
| 881 | - | |
| 882 | - // WordPress 2.3+ | |
| 883 | - if (function_exists('is_term')) : | |
| 884 | - $cat_id = is_term($cat_name, 'category'); | |
| 885 | - if ($cat_id) : | |
| 886 | - $cat_ids[] = $cat_id['term_id']; | |
| 887 | - // There must be a better way to do this... | |
| 888 | - elseif ($results = $wpdb->get_results( | |
| 889 | - "SELECT term_id | |
| 890 | - FROM $wpdb->term_taxonomy | |
| 891 | - WHERE | |
| 892 | - LOWER(description) RLIKE | |
| 893 | - CONCAT('(^|\\n)a\\.?k\\.?a\\.?( |\\t)*:?( |\\t)*', LOWER('{$resc}'), '( |\\t|\\r)*(\\n|\$)')" | |
| 894 | - )) : | |
| 895 | - foreach ($results AS $term) : | |
| 896 | - $cat_ids[] = (int) $term->term_id; | |
| 897 | - endforeach; | |
| 898 | - elseif ('tag'==$unfamiliar_category) : | |
| 899 | - $tags[] = $cat_name; | |
| 900 | - elseif ('create'===$unfamiliar_category) : | |
| 901 | - $term = wp_insert_term($cat_name, 'category'); | |
| 902 | - if (is_wp_error($term)) : | |
| 903 | - FeedWordPress::noncritical_bug('term insertion problem', array('cat_name' => $cat_name, 'term' => $term, 'this' => $this), __LINE__); | |
| 904 | - else : | |
| 905 | - $cat_ids[] = $term['term_id']; | |
| 906 | - endif; | |
| 907 | - endif; | |
| 908 | - | |
| 909 | - // WordPress 1.5.x - 2.2.x | |
| 910 | - else : | |
| 911 | - $results = $wpdb->get_results( | |
| 912 | - "SELECT cat_ID | |
| 913 | - FROM $wpdb->categories | |
| 914 | - WHERE | |
| 915 | - (LOWER(cat_name) = LOWER('$esc')) | |
| 916 | - OR (LOWER(category_description) | |
| 917 | - RLIKE CONCAT('(^|\\n)a\\.?k\\.?a\\.?( |\\t)*:?( |\\t)*', LOWER('{$resc}'), '( |\\t|\\r)*(\\n|\$)')) | |
| 918 | - "); | |
| 919 | - if ($results) : | |
| 920 | - foreach ($results as $term) : | |
| 921 | - $cat_ids[] = (int) $term->cat_ID; | |
| 922 | - endforeach; | |
| 923 | - elseif ('create'===$unfamiliar_category) : | |
| 924 | - if (function_exists('wp_insert_category')) : | |
| 925 | - $cat_id = wp_insert_category(array('cat_name' => $esc)); | |
| 926 | - // And into the database we go. | |
| 927 | - else : | |
| 928 | - $nice_kitty = sanitize_title($cat_name); | |
| 929 | - $wpdb->query(sprintf(" | |
| 930 | - INSERT INTO $wpdb->categories | |
| 931 | - SET | |
| 932 | - cat_name='%s', | |
| 933 | - category_nicename='%s' | |
| 934 | - ", $esc, $nice_kitty | |
| 935 | - )); | |
| 936 | - $cat_id = $wpdb->insert_id; | |
| 937 | - endif; | |
| 938 | - $cat_ids[] = $cat_id; | |
| 939 | - endif; | |
| 940 | - endif; | |
| 2309 | + // Multisite: Check whether the author has been recorded | |
| 2310 | + // on *this* blog before. If not, put her down as a | |
| 2311 | + // Contributor for *this* blog. | |
| 2312 | + $user = new WP_User((int) $id); | |
| 2313 | + if (empty($user->roles)) : | |
| 2314 | + $user->add_role('contributor'); | |
| 941 | 2315 | endif; |
| 942 | - endforeach; | |
| 943 | - | |
| 944 | - if ((count($cat_ids) == 0) and ($unfamiliar_category === 'filter')) : | |
| 945 | - $cat_ids = NULL; // Drop the post | |
| 946 | - else : | |
| 947 | - $cat_ids = array_unique($cat_ids); | |
| 948 | 2316 | endif; |
| 949 | - | |
| 950 | - if ($tags_too) : $ret = array($cat_ids, $tags); | |
| 951 | - else : $ret = $cat_ids; | |
| 952 | - endif; | |
| 2317 | + return $id; | |
| 2318 | + } /* function SyndicatedPost::author_id () */ | |
| 953 | 2319 | |
| 954 | - return $ret; | |
| 955 | - } // function SyndicatedPost::category_ids () | |
| 956 | - | |
| 957 | - function use_api ($tag) { | |
| 958 | - global $wp_db_version; | |
| 959 | - switch ($tag) : | |
| 960 | - case 'wp_insert_post': | |
| 961 | - // Before 2.2, wp_insert_post does too much of the wrong stuff to use it | |
| 962 | - // In 1.5 it was such a resource hog it would make PHP segfault on big updates | |
| 963 | - $ret = (isset($wp_db_version) and $wp_db_version > FWP_SCHEMA_21); | |
| 964 | - break; | |
| 965 | - case 'post_status_pending': | |
| 966 | - $ret = (isset($wp_db_version) and $wp_db_version > FWP_SCHEMA_23); | |
| 967 | - break; | |
| 968 | - endswitch; | |
| 969 | - return $ret; | |
| 970 | - } // function SyndicatedPost::use_api () | |
| 971 | - | |
| 972 | - #### EXTRACT DATA FROM FEED ITEM #### | |
| 973 | - | |
| 974 | - function created () { | |
| 975 | - $epoch = null; | |
| 976 | - if (isset($this->item['dc']['created'])) : | |
| 977 | - $epoch = @parse_w3cdtf($this->item['dc']['created']); | |
| 978 | - elseif (isset($this->item['dcterms']['created'])) : | |
| 979 | - $epoch = @parse_w3cdtf($this->item['dcterms']['created']); | |
| 980 | - elseif (isset($this->item['created'])): // Atom 0.3 | |
| 981 | - $epoch = @parse_w3cdtf($this->item['created']); | |
| 982 | - endif; | |
| 983 | - return $epoch; | |
| 984 | - } | |
| 985 | - function published ($fallback = true) { | |
| 986 | - $epoch = null; | |
| 987 | - | |
| 988 | - # RSS is a fucking mess. Figure out whether we have a date in | |
| 989 | - # <dc:date>, <issued>, <pubDate>, etc., and get it into Unix | |
| 990 | - # epoch format for reformatting. If we can't find anything, | |
| 991 | - # we'll use the last-updated time. | |
| 992 | - if (isset($this->item['dc']['date'])): // Dublin Core | |
| 993 | - $epoch = @parse_w3cdtf($this->item['dc']['date']); | |
| 994 | - elseif (isset($this->item['dcterms']['issued'])) : // Dublin Core extensions | |
| 995 | - $epoch = @parse_w3cdtf($this->item['dcterms']['issued']); | |
| 996 | - elseif (isset($this->item['published'])) : // Atom 1.0 | |
| 997 | - $epoch = @parse_w3cdtf($this->item['published']); | |
| 998 | - elseif (isset($this->item['issued'])): // Atom 0.3 | |
| 999 | - $epoch = @parse_w3cdtf($this->item['issued']); | |
| 1000 | - elseif (isset($this->item['pubdate'])): // RSS 2.0 | |
| 1001 | - $epoch = strtotime($this->item['pubdate']); | |
| 1002 | - elseif ($fallback) : // Fall back to <updated> / <modified> if present | |
| 1003 | - $epoch = $this->updated(/*fallback=*/ false); | |
| 1004 | - endif; | |
| 1005 | - | |
| 1006 | - # If everything failed, then default to the current time. | |
| 1007 | - if (is_null($epoch)) : | |
| 1008 | - if (-1 == $default) : | |
| 1009 | - $epoch = time(); | |
| 1010 | - else : | |
| 1011 | - $epoch = $default; | |
| 1012 | - endif; | |
| 1013 | - endif; | |
| 1014 | - | |
| 1015 | - return $epoch; | |
| 1016 | - } | |
| 1017 | - function updated ($fallback = true, $default = -1) { | |
| 1018 | - $epoch = null; | |
| 1019 | - | |
| 1020 | - # As far as I know, only dcterms and Atom have reliable ways to | |
| 1021 | - # specify when something was *modified* last. If neither is | |
| 1022 | - # available, then we'll try to get the time of publication. | |
| 1023 | - if (isset($this->item['dc']['modified'])) : // Not really correct | |
| 1024 | - $epoch = @parse_w3cdtf($this->item['dc']['modified']); | |
| 1025 | - elseif (isset($this->item['dcterms']['modified'])) : // Dublin Core extensions | |
| 1026 | - $epoch = @parse_w3cdtf($this->item['dcterms']['modified']); | |
| 1027 | - elseif (isset($this->item['modified'])): // Atom 0.3 | |
| 1028 | - $epoch = @parse_w3cdtf($this->item['modified']); | |
| 1029 | - elseif (isset($this->item['updated'])): // Atom 1.0 | |
| 1030 | - $epoch = @parse_w3cdtf($this->item['updated']); | |
| 1031 | - elseif ($fallback) : // Fall back to issued / dc:date | |
| 1032 | - $epoch = $this->published(/*fallback=*/ false, /*default=*/ $default); | |
| 1033 | - endif; | |
| 1034 | - | |
| 1035 | - # If everything failed, then default to the current time. | |
| 1036 | - if (is_null($epoch)) : | |
| 1037 | - if (-1 == $default) : | |
| 1038 | - $epoch = time(); | |
| 1039 | - else : | |
| 1040 | - $epoch = $default; | |
| 1041 | - endif; | |
| 1042 | - endif; | |
| 1043 | - | |
| 1044 | - return $epoch; | |
| 1045 | - } | |
| 1046 | - | |
| 1047 | - function update_hash () { | |
| 1048 | - return md5(serialize($this->item)); | |
| 1049 | - } | |
| 1050 | - | |
| 1051 | - function guid () { | |
| 1052 | - $guid = null; | |
| 1053 | - if (isset($this->item['id'])): // Atom 0.3 / 1.0 | |
| 1054 | - $guid = $this->item['id']; | |
| 1055 | - elseif (isset($this->item['atom']['id'])) : // Namespaced Atom | |
| 1056 | - $guid = $this->item['atom']['id']; | |
| 1057 | - elseif (isset($this->item['guid'])) : // RSS 2.0 | |
| 1058 | - $guid = $this->item['guid']; | |
| 1059 | - elseif (isset($this->item['dc']['identifier'])) :// yeah, right | |
| 1060 | - $guid = $this->item['dc']['identifier']; | |
| 1061 | - else : | |
| 1062 | - // The feed does not seem to have provided us with a | |
| 1063 | - // unique identifier, so we'll have to cobble together | |
| 1064 | - // a tag: URI that might work for us. The base of the | |
| 1065 | - // URI will be the host name of the feed source ... | |
| 1066 | - $bits = parse_url($this->feedmeta['link/uri']); | |
| 1067 | - $guid = 'tag:'.$bits['host']; | |
| 1068 | - | |
| 1069 | - // If we have a date of creation, then we can use that | |
| 1070 | - // to uniquely identify the item. (On the other hand, if | |
| 1071 | - // the feed producer was consicentious enough to | |
| 1072 | - // generate dates of creation, she probably also was | |
| 1073 | - // conscientious enough to generate unique identifiers.) | |
| 1074 | - if (!is_null($this->created())) : | |
| 1075 | - $guid .= '://post.'.date('YmdHis', $this->created()); | |
| 1076 | - | |
| 1077 | - // Otherwise, use both the URI of the item, *and* the | |
| 1078 | - // item's title. We have to use both because titles are | |
| 1079 | - // often not unique, and sometimes links aren't unique | |
| 1080 | - // either (e.g. Bitch (S)HITLIST, Mozilla Dot Org news, | |
| 1081 | - // some podcasts). But it's rare to have *both* the same | |
| 1082 | - // title *and* the same link for two different items. So | |
| 1083 | - // this is about the best we can do. | |
| 1084 | - else : | |
| 1085 | - $guid .= '://'.md5($this->item['link'].'/'.$this->item['title']); | |
| 1086 | - endif; | |
| 1087 | - endif; | |
| 1088 | - return $guid; | |
| 1089 | - } | |
| 1090 | - | |
| 1091 | - function author () { | |
| 1092 | - $author = array (); | |
| 1093 | - | |
| 1094 | - if (isset($this->item['author_name'])): | |
| 1095 | - $author['name'] = $this->item['author_name']; | |
| 1096 | - elseif (isset($this->item['dc']['creator'])): | |
| 1097 | - $author['name'] = $this->item['dc']['creator']; | |
| 1098 | - elseif (isset($this->item['dc']['contributor'])): | |
| 1099 | - $author['name'] = $this->item['dc']['contributor']; | |
| 1100 | - elseif (isset($this->feed->channel['dc']['creator'])) : | |
| 1101 | - $author['name'] = $this->feed->channel['dc']['creator']; | |
| 1102 | - elseif (isset($this->feed->channel['dc']['contributor'])) : | |
| 1103 | - $author['name'] = $this->feed->channel['dc']['contributor']; | |
| 1104 | - elseif (isset($this->feed->channel['author_name'])) : | |
| 1105 | - $author['name'] = $this->feed->channel['author_name']; | |
| 1106 | - elseif ($this->feed->is_rss() and isset($this->item['author'])) : | |
| 1107 | - // The author element in RSS is allegedly an | |
| 1108 | - // e-mail address, but lots of people don't use | |
| 1109 | - // it that way. So let's make of it what we can. | |
| 1110 | - $author = parse_email_with_realname($this->item['author']); | |
| 1111 | - | |
| 1112 | - if (!isset($author['name'])) : | |
| 1113 | - if (isset($author['email'])) : | |
| 1114 | - $author['name'] = $author['email']; | |
| 1115 | - else : | |
| 1116 | - $author['name'] = $this->feed->channel['title']; | |
| 1117 | - endif; | |
| 1118 | - endif; | |
| 1119 | - else : | |
| 1120 | - $author['name'] = $this->feed->channel['title']; | |
| 1121 | - endif; | |
| 1122 | - | |
| 1123 | - if (isset($this->item['author_email'])): | |
| 1124 | - $author['email'] = $this->item['author_email']; | |
| 1125 | - elseif (isset($this->feed->channel['author_email'])) : | |
| 1126 | - $author['email'] = $this->feed->channel['author_email']; | |
| 1127 | - endif; | |
| 1128 | - | |
| 1129 | - if (isset($this->item['author_url'])): | |
| 1130 | - $author['uri'] = $this->item['author_url']; | |
| 1131 | - elseif (isset($this->feed->channel['author_url'])) : | |
| 1132 | - $author['uri'] = $this->item['author_url']; | |
| 1133 | - else: | |
| 1134 | - $author['uri'] = $this->feed->channel['link']; | |
| 1135 | - endif; | |
| 1136 | - | |
| 1137 | - return $author; | |
| 1138 | - } // SyndicatedPost::author() | |
| 1139 | - | |
| 1140 | 2320 | /** |
| 1141 | - * SyndicatedPost::isTaggedAs: Test whether a feed item is | |
| 1142 | - * tagged / categorized with a given string. Case and leading and | |
| 1143 | - * trailing whitespace are ignored. | |
| 2321 | + * category_ids: look up (and create) category ids from a list of | |
| 2322 | + * categories | |
| 1144 | 2323 | * |
| 1145 | - * @param string $tag Tag to check for | |
| 1146 | - * | |
| 1147 | - * @return bool Whether or not at least one of the categories / tags on | |
| 1148 | - * $this->item is set to $tag (modulo case and leading and trailing | |
| 1149 | - * whitespace) | |
| 2324 | + * @param array $cats | |
| 2325 | + * @param string $unfamiliar_category | |
| 2326 | + * @param array|null $taxonomies | |
| 2327 | + * @return array | |
| 1150 | 2328 | */ |
| 1151 | - function isTaggedAs ($tag) { | |
| 1152 | - $desiredTag = strtolower(trim($tag)); // Normalize case and whitespace | |
| 2329 | + function category_ids ($cats, $unfamiliar_category = 'create', $taxonomies = NULL, $params = array()) { | |
| 2330 | + return $this->link->category_ids($this, $cats, $unfamiliar_category, $taxonomies, $params); | |
| 2331 | + } /* SyndicatedPost::category_ids () */ | |
| 1153 | 2332 | |
| 1154 | - // Check to see if this is tagged with $tag | |
| 1155 | - $currentCategory = 'category'; | |
| 1156 | - $currentCategoryNumber = 1; | |
| 1157 | - | |
| 1158 | - // If we have the new MagpieRSS, the number of category elements | |
| 1159 | - // on this item is stored under index "category#". | |
| 1160 | - if (isset($this->item['category#'])) : | |
| 1161 | - $numberOfCategories = (int) $this->item['category#']; | |
| 1162 | - | |
| 1163 | - // We REALLY shouldn't have the old and busted MagpieRSS, but in | |
| 1164 | - // case we do, it doesn't support multiple categories, but there | |
| 1165 | - // might still be a single value under the "category" index. | |
| 1166 | - elseif (isset($this->item['category'])) : | |
| 1167 | - $numberOfCategories = 1; | |
| 1168 | - | |
| 1169 | - // No standard category or tag elements on this feed item. | |
| 1170 | - else : | |
| 1171 | - $numberOfCategories = 0; | |
| 1172 | - | |
| 1173 | - endif; | |
| 1174 | - | |
| 1175 | - $isSoTagged = false; // Innocent until proven guilty | |
| 1176 | - | |
| 1177 | - // Loop through category elements; if there are multiple | |
| 1178 | - // elements, they are indexed as category, category#2, | |
| 1179 | - // category#3, ... category#N | |
| 1180 | - while ($currentCategoryNumber <= $numberOfCategories) : | |
| 1181 | - if ($desiredTag == strtolower(trim($this->item[$currentCategory]))) : | |
| 1182 | - $isSoTagged = true; // Got it! | |
| 1183 | - break; | |
| 1184 | - endif; | |
| 1185 | - | |
| 1186 | - $currentCategoryNumber += 1; | |
| 1187 | - $currentCategory = 'category#'.$currentCategoryNumber; | |
| 1188 | - endwhile; | |
| 1189 | - | |
| 1190 | - return $isSoTagged; | |
| 1191 | - } /* SyndicatedPost::isTaggedAs() */ | |
| 1192 | - | |
| 1193 | - var $uri_attrs = array ( | |
| 1194 | - array('a', 'href'), | |
| 1195 | - array('applet', 'codebase'), | |
| 1196 | - array('area', 'href'), | |
| 1197 | - array('blockquote', 'cite'), | |
| 1198 | - array('body', 'background'), | |
| 1199 | - array('del', 'cite'), | |
| 1200 | - array('form', 'action'), | |
| 1201 | - array('frame', 'longdesc'), | |
| 1202 | - array('frame', 'src'), | |
| 1203 | - array('iframe', 'longdesc'), | |
| 1204 | - array('iframe', 'src'), | |
| 1205 | - array('head', 'profile'), | |
| 1206 | - array('img', 'longdesc'), | |
| 1207 | - array('img', 'src'), | |
| 1208 | - array('img', 'usemap'), | |
| 1209 | - array('input', 'src'), | |
| 1210 | - array('input', 'usemap'), | |
| 1211 | - array('ins', 'cite'), | |
| 1212 | - array('link', 'href'), | |
| 1213 | - array('object', 'classid'), | |
| 1214 | - array('object', 'codebase'), | |
| 1215 | - array('object', 'data'), | |
| 1216 | - array('object', 'usemap'), | |
| 1217 | - array('q', 'cite'), | |
| 1218 | - array('script', 'src') | |
| 1219 | - ); /* var SyndicatedPost::$uri_attrs */ | |
| 1220 | - | |
| 1221 | - var $_base = null; | |
| 1222 | - | |
| 1223 | - function resolve_single_relative_uri ($refs) { | |
| 1224 | - $tag = FeedWordPressHTML::attributeMatch($refs); | |
| 1225 | - $url = Relative_URI::resolve($tag['value'], $this->_base); | |
| 1226 | - return $tag['prefix'] . $url . $tag['suffix']; | |
| 1227 | - } /* function SyndicatedPost::resolve_single_relative_uri() */ | |
| 1228 | - | |
| 1229 | - function resolve_relative_uris ($content, $obj) { | |
| 1230 | - # The MagpieRSS upgrade has some `xml:base` support baked in. | |
| 1231 | - # However, sometimes people do silly things, like putting | |
| 1232 | - # relative URIs out on a production RSS 2.0 feed or other feeds | |
| 1233 | - # with no good support for `xml:base`. So we'll do our best to | |
| 1234 | - # try to catch any remaining relative URIs and resolve them as | |
| 1235 | - # best we can. | |
| 1236 | - $obj->_base = $obj->item['link']; // Reset the base for resolving relative URIs | |
| 1237 | - | |
| 1238 | - foreach ($obj->uri_attrs as $pair) : | |
| 1239 | - list($tag, $attr) = $pair; | |
| 1240 | - $pattern = FeedWordPressHTML::attributeRegex($tag, $attr); | |
| 1241 | - $content = preg_replace_callback ( | |
| 1242 | - $pattern, | |
| 1243 | - array(&$obj, 'resolve_single_relative_uri'), | |
| 1244 | - $content | |
| 1245 | - ); | |
| 1246 | - endforeach; | |
| 1247 | - | |
| 1248 | - return $content; | |
| 1249 | - } /* function SyndicatedPost::resolve_relative_uris () */ | |
| 1250 | - | |
| 1251 | - var $strip_attrs = array ( | |
| 1252 | - array('[a-z]+', 'target'), | |
| 1253 | -// array('[a-z]+', 'style'), | |
| 1254 | -// array('[a-z]+', 'on[a-z]+'), | |
| 1255 | - ); | |
| 1256 | - | |
| 1257 | - function strip_attribute_from_tag ($refs) { | |
| 1258 | - $tag = FeedWordPressHTML::attributeMatch($refs); | |
| 1259 | - return $tag['before_attribute'].$tag['after_attribute']; | |
| 1260 | - } | |
| 1261 | - | |
| 1262 | - function sanitize_content ($content, $obj) { | |
| 1263 | - # This kind of sucks. I intend to replace it with | |
| 1264 | - # lib_filter sometime soon. | |
| 1265 | - foreach ($obj->strip_attrs as $pair): | |
| 1266 | - list($tag,$attr) = $pair; | |
| 1267 | - $pattern = FeedWordPressHTML::attributeRegex($tag, $attr); | |
| 1268 | - | |
| 1269 | - $content = preg_replace_callback ( | |
| 1270 | - $pattern, | |
| 1271 | - array(&$obj, 'strip_attribute_from_tag'), | |
| 1272 | - $content | |
| 1273 | - ); | |
| 1274 | - endforeach; | |
| 1275 | - return $content; | |
| 1276 | - } | |
| 1277 | -} // class SyndicatedPost | |
| 1278 | - | |
| 2333 | +} /* class SyndicatedPost */ | |