| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | /* |
| 3 | 3 | Plugin Name: FeedWordPress |
| 4 | 4 | Plugin URI: http://projects.radgeek.com/feedwordpress |
| 5 | 5 | Description: simple and flexible Atom/RSS syndication for WordPress |
| 6 | -Version: 0.97 | |
| 6 | +Version: 0.98 | |
| 7 | 7 | Author: Charles Johnson |
| 8 | 8 | Author URI: http://radgeek.com/ |
| 9 | 9 | License: GPL |
| 10 | 10 | Last modified: 2005-09-28 4:40pm EDT |
| @@ -26,9 +26,9 @@ | ||
| 26 | 26 | # <http://www.zyx.com/blog/xmlrpc.php>), or see `update-feeds.php` |
| 27 | 27 | |
| 28 | 28 | # -- Don't change these unless you know what you're doing... |
| 29 | 29 | define ('RPC_MAGIC', 'tag:radgeek.com/projects/feedwordpress/'); |
| 30 | -define ('FEEDWORDPRESS_VERSION', '0.97'); | |
| 30 | +define ('FEEDWORDPRESS_VERSION', '0.98'); | |
| 31 | 31 | define ('DEFAULT_SYNDICATION_CATEGORY', 'Contributors'); |
| 32 | 32 | |
| 33 | 33 | define ('FEEDWORDPRESS_CAT_SEPARATOR_PATTERN', '/[:\n]/'); |
| 34 | 34 | define ('FEEDWORDPRESS_CAT_SEPARATOR', "\n"); |
| @@ -39,8 +39,12 @@ | ||
| 39 | 39 | // old & busted. For the new hotness, drop a copy of rss-functions.php from |
| 40 | 40 | // this archive into wp-includes/rss-functions.php |
| 41 | 41 | require_once (ABSPATH . WPINC . '/rss-functions.php'); |
| 42 | 42 | |
| 43 | +if (isset($wp_db_version) and $wp_db_version >= 2966) : | |
| 44 | + require_once (ABSPATH . WPINC . '/registration-functions.php'); | |
| 45 | +endif; | |
| 46 | + | |
| 43 | 47 | // Is this being loaded from within WordPress 1.5 or later? |
| 44 | 48 | if (isset($wp_version) and $wp_version >= 1.5): |
| 45 | 49 | |
| 46 | 50 | $fwp_db_version = get_settings('feedwordpress_version'); |
| @@ -2193,89 +2197,19 @@ | ||
| 2193 | 2197 | endif; |
| 2194 | 2198 | |
| 2195 | 2199 | if ($freshness == 2) : |
| 2196 | 2200 | // The item has not yet been added. So let's add it. |
| 2197 | - | |
| 2198 | - # The right way to do this would be to use: | |
| 2199 | - # | |
| 2200 | - # $postId = wp_insert_post($post); | |
| 2201 | - # $result = $wpdb->query(" | |
| 2202 | - # UPDATE $wpdb->posts | |
| 2203 | - # SET | |
| 2204 | - # guid='$guid' | |
| 2205 | - # WHERE post_id='$postId' | |
| 2206 | - # "); | |
| 2207 | - # | |
| 2208 | - # in place of everything in the cut below. Alas, | |
| 2209 | - # wp_insert_post seems to be a memory hog; using it | |
| 2210 | - # to insert several posts in one session makes php | |
| 2211 | - # segfault after inserting 50-100 posts. This can get | |
| 2212 | - # pretty annoying, especially if you are trying to | |
| 2213 | - # update your feeds for the first time. | |
| 2214 | - # | |
| 2215 | - # --- cut here --- | |
| 2216 | - $result = $wpdb->query(" | |
| 2217 | - INSERT INTO $wpdb->posts | |
| 2218 | - SET | |
| 2219 | - guid = '$guid', | |
| 2220 | - post_author = '".$post['post_author']."', | |
| 2221 | - post_date = '".$post['post_date']."', | |
| 2222 | - post_date_gmt = '".$post['post_date_gmt']."', | |
| 2223 | - post_content = '".$post['post_content']."'," | |
| 2224 | - .(isset($post['post_excerpt']) ? "post_excerpt = '".$post['post_excerpt']."'," : "")." | |
| 2225 | - post_title = '".$post['post_title']."', | |
| 2226 | - post_name = '".$post['post_name']."', | |
| 2227 | - post_modified = '".$post['post_modified']."', | |
| 2228 | - post_modified_gmt = '".$post['post_modified_gmt']."', | |
| 2229 | - comment_status = '".$post['comment_status']."', | |
| 2230 | - ping_status = '".$post['ping_status']."', | |
| 2231 | - post_status = '".$post['post_status']."' | |
| 2232 | - "); | |
| 2233 | - $postId = $wpdb->insert_id; | |
| 2234 | - $this->add_to_category($wpdb, $postId, $post['post_category']); | |
| 2235 | - | |
| 2236 | - // Since we are not going through official channels, we need to | |
| 2237 | - // manually tell WordPress that we've published a new post. | |
| 2238 | - // We need to make sure to do this in order for FeedWordPress | |
| 2239 | - // to play well with the staticize-reloaded plugin (something | |
| 2240 | - // that a large aggregator website is going to *want* to be | |
| 2241 | - // able to use). | |
| 2242 | - do_action('publish_post', $postId); | |
| 2243 | - # --- cut here --- | |
| 2244 | - | |
| 2201 | + $postId = $this->insert_new_post($post); | |
| 2245 | 2202 | $this->add_rss_meta($wpdb, $postId, $post); |
| 2246 | - | |
| 2247 | 2203 | do_action('post_syndicated_item', $postId); |
| 2248 | 2204 | |
| 2249 | 2205 | $ret = 'new'; |
| 2250 | 2206 | elseif ($freshness == 1) : |
| 2251 | - $postId = $result->id; $modified = $result->modified; | |
| 2207 | + $post['ID'] = $result->id; $modified = $result->modified; | |
| 2208 | + $this->update_existing_post($post); | |
| 2209 | + $this->add_rss_meta($wpdb, $post['ID'], $post); | |
| 2210 | + do_action('update_syndicated_item', $post['ID']); | |
| 2252 | 2211 | |
| 2253 | - $result = $wpdb->query(" | |
| 2254 | - UPDATE $wpdb->posts | |
| 2255 | - SET | |
| 2256 | - post_author = '".$post['post_author']."', | |
| 2257 | - post_content = '".$post['post_content']."', | |
| 2258 | - post_title = '".$post['post_title']."', | |
| 2259 | - post_name = '".$post['post_name']."', | |
| 2260 | - post_modified = '".$post['post_modified']."', | |
| 2261 | - post_modified_gmt = '".$post['post_modified_gmt']."' | |
| 2262 | - WHERE guid='$guid' | |
| 2263 | - "); | |
| 2264 | - $this->add_to_category($wpdb, $postId, $post['post_category']); | |
| 2265 | - | |
| 2266 | - // Since we are not going through official channels, we need to | |
| 2267 | - // manually tell WordPress that we've published a new post. | |
| 2268 | - // We need to make sure to do this in order for FeedWordPress | |
| 2269 | - // to play well with the staticize-reloaded plugin (something | |
| 2270 | - // that a large aggregator website is going to *want* to be | |
| 2271 | - // able to use). | |
| 2272 | - do_action('edit_post', $postId); | |
| 2273 | - | |
| 2274 | - $this->add_rss_meta($wpdb, $postId, $post); | |
| 2275 | - | |
| 2276 | - do_action('update_syndicated_item', $postId); | |
| 2277 | - | |
| 2278 | 2212 | $ret = 'updated'; |
| 2279 | 2213 | else : |
| 2280 | 2214 | $ret = false; |
| 2281 | 2215 | endif; |
| @@ -2281,9 +2215,92 @@ | ||
| 2281 | 2215 | endif; |
| 2282 | 2216 | |
| 2283 | 2217 | return $ret; |
| 2284 | 2218 | } // function FeedWordPress::add_post () |
| 2285 | - | |
| 2219 | + | |
| 2220 | + function insert_new_post ($post) { | |
| 2221 | + global $wpdb; | |
| 2222 | + | |
| 2223 | + $guid = $post['guid']; | |
| 2224 | + | |
| 2225 | + # The right way to do this would be to use: | |
| 2226 | + # | |
| 2227 | + # $postId = wp_insert_post($post); | |
| 2228 | + # $result = $wpdb->query(" | |
| 2229 | + # UPDATE $wpdb->posts | |
| 2230 | + # SET | |
| 2231 | + # guid='$guid' | |
| 2232 | + # WHERE post_id='$postId' | |
| 2233 | + # "); | |
| 2234 | + # | |
| 2235 | + # in place of everything in the cut below. Alas, | |
| 2236 | + # wp_insert_post seems to be a memory hog; using it | |
| 2237 | + # to insert several posts in one session makes php | |
| 2238 | + # segfault after inserting 50-100 posts. This can get | |
| 2239 | + # pretty annoying, especially if you are trying to | |
| 2240 | + # update your feeds for the first time. | |
| 2241 | + # | |
| 2242 | + # --- cut here --- | |
| 2243 | + $result = $wpdb->query(" | |
| 2244 | + INSERT INTO $wpdb->posts | |
| 2245 | + SET | |
| 2246 | + guid = '$guid', | |
| 2247 | + post_author = '".$post['post_author']."', | |
| 2248 | + post_date = '".$post['post_date']."', | |
| 2249 | + post_date_gmt = '".$post['post_date_gmt']."', | |
| 2250 | + post_content = '".$post['post_content']."'," | |
| 2251 | + .(isset($post['post_excerpt']) ? "post_excerpt = '".$post['post_excerpt']."'," : "")." | |
| 2252 | + post_title = '".$post['post_title']."', | |
| 2253 | + post_name = '".$post['post_name']."', | |
| 2254 | + post_modified = '".$post['post_modified']."', | |
| 2255 | + post_modified_gmt = '".$post['post_modified_gmt']."', | |
| 2256 | + comment_status = '".$post['comment_status']."', | |
| 2257 | + ping_status = '".$post['ping_status']."', | |
| 2258 | + post_status = '".$post['post_status']."' | |
| 2259 | + "); | |
| 2260 | + $postId = $wpdb->insert_id; | |
| 2261 | + $this->add_to_category($wpdb, $postId, $post['post_category']); | |
| 2262 | + | |
| 2263 | + // Since we are not going through official channels, we need to | |
| 2264 | + // manually tell WordPress that we've published a new post. | |
| 2265 | + // We need to make sure to do this in order for FeedWordPress | |
| 2266 | + // to play well with the staticize-reloaded plugin (something | |
| 2267 | + // that a large aggregator website is going to *want* to be | |
| 2268 | + // able to use). | |
| 2269 | + do_action('publish_post', $postId); | |
| 2270 | + # --- cut here --- | |
| 2271 | + | |
| 2272 | + return $postId; | |
| 2273 | + } /* FeedWordPress::insert_new_post() */ | |
| 2274 | + | |
| 2275 | + function update_existing_post ($post) { | |
| 2276 | + global $wpdb; | |
| 2277 | + | |
| 2278 | + $guid = $post['guid']; | |
| 2279 | + $postId = $post['ID']; | |
| 2280 | + | |
| 2281 | + $result = $wpdb->query(" | |
| 2282 | + UPDATE $wpdb->posts | |
| 2283 | + SET | |
| 2284 | + post_author = '".$post['post_author']."', | |
| 2285 | + post_content = '".$post['post_content']."', | |
| 2286 | + post_title = '".$post['post_title']."', | |
| 2287 | + post_name = '".$post['post_name']."', | |
| 2288 | + post_modified = '".$post['post_modified']."', | |
| 2289 | + post_modified_gmt = '".$post['post_modified_gmt']."' | |
| 2290 | + WHERE guid='$guid' | |
| 2291 | + "); | |
| 2292 | + $this->add_to_category($wpdb, $postId, $post['post_category']); | |
| 2293 | + | |
| 2294 | + // Since we are not going through official channels, we need to | |
| 2295 | + // manually tell WordPress that we've published a new post. | |
| 2296 | + // We need to make sure to do this in order for FeedWordPress | |
| 2297 | + // to play well with the staticize-reloaded plugin (something | |
| 2298 | + // that a large aggregator website is going to *want* to be | |
| 2299 | + // able to use). | |
| 2300 | + do_action('edit_post', $postId); | |
| 2301 | + } /* FeedWordPress::update_existing_post() */ | |
| 2302 | + | |
| 2286 | 2303 | # function FeedWordPress::add_to_category () |
| 2287 | 2304 | # |
| 2288 | 2305 | # If there is a way to properly hook in to wp_insert_post, then this |
| 2289 | 2306 | # function will no longer be needed. In the meantime, here it is. |
| @@ -2288,25 +2305,15 @@ | ||
| 2288 | 2305 | # If there is a way to properly hook in to wp_insert_post, then this |
| 2289 | 2306 | # function will no longer be needed. In the meantime, here it is. |
| 2290 | 2307 | # --- cut here --- |
| 2291 | 2308 | function add_to_category($wpdb, $postId, $post_categories) { |
| 2309 | + global $wp_db_version; // test for WordPress 2.0 database schema | |
| 2310 | + | |
| 2292 | 2311 | // Default to category 1 ("Uncategorized"), if nothing else |
| 2293 | 2312 | if (!$post_categories) $post_categories[] = 1; |
| 2294 | 2313 | |
| 2295 | - // Clean the slate (in case we're updating) | |
| 2296 | - $results = $wpdb->query(" | |
| 2297 | - DELETE FROM $wpdb->post2cat | |
| 2298 | - WHERE post_id = $postId | |
| 2299 | - "); | |
| 2300 | - | |
| 2301 | - foreach ($post_categories as $post_category): | |
| 2302 | - $results = $wpdb->query(" | |
| 2303 | - INSERT INTO $wpdb->post2cat | |
| 2304 | - SET | |
| 2305 | - post_id = $postId, | |
| 2306 | - category_id = $post_category | |
| 2307 | - "); | |
| 2308 | - endforeach; | |
| 2314 | + // Now pass the buck to the WordPress API... | |
| 2315 | + wp_set_post_cats('', $postId, $post_categories); | |
| 2309 | 2316 | } // function FeedWordPress::add_to_category () |
| 2310 | 2317 | # --- cut here --- |
| 2311 | 2318 | |
| 2312 | 2319 | // FeedWordPress::add_rss_meta: adds interesting meta-data to each entry |
| @@ -2349,8 +2356,10 @@ | ||
| 2349 | 2356 | |
| 2350 | 2357 | // FeedWordPress::author_to_id (): get the ID for an author name from |
| 2351 | 2358 | // the feed. Create the author if necessary. |
| 2352 | 2359 | function author_to_id ($wpdb, $author, $email, $url, $unfamiliar_author = 'create') { |
| 2360 | + global $wp_db_version; // test for WordPress 2.0 database schema | |
| 2361 | + | |
| 2353 | 2362 | // Never can be too careful... |
| 2354 | 2363 | $nice_author = sanitize_title($author); |
| 2355 | 2364 | $reg_author = $wpdb->escape(preg_quote($author)); |
| 2356 | 2365 | $author = $wpdb->escape($author); |
| @@ -2356,40 +2365,96 @@ | ||
| 2356 | 2365 | $author = $wpdb->escape($author); |
| 2357 | 2366 | $email = $wpdb->escape($email); |
| 2358 | 2367 | $url = $wpdb->escape($url); |
| 2359 | 2368 | |
| 2360 | - $id = $wpdb->get_var( | |
| 2361 | - "SELECT ID from $wpdb->users | |
| 2362 | - WHERE | |
| 2363 | - TRIM(LCASE(user_login)) = TRIM(LCASE('$author')) OR | |
| 2364 | - TRIM(LCASE(user_firstname)) = TRIM(LCASE('$author')) OR | |
| 2365 | - TRIM(LCASE(user_nickname)) = TRIM(LCASE('$author')) OR | |
| 2366 | - TRIM(LCASE(user_nicename)) = TRIM(LCASE('$nice_author')) OR | |
| 2367 | - TRIM(LCASE(user_description)) = TRIM(LCASE('$author')) OR | |
| 2368 | - ( | |
| 2369 | - LOWER(user_description) | |
| 2370 | - RLIKE CONCAT( | |
| 2371 | - '(^|\\n)a.k.a.( |\\t)*:?( |\\t)*', | |
| 2372 | - LCASE('$reg_author'), | |
| 2373 | - '( |\\t|\\r)*(\\n|\$)' | |
| 2369 | + // Look for an existing author record that fits... | |
| 2370 | + if (!isset($wp_db_version)) : | |
| 2371 | + #-- WordPress 1.5.x | |
| 2372 | + $id = $wpdb->get_var( | |
| 2373 | + "SELECT ID from $wpdb->users | |
| 2374 | + WHERE | |
| 2375 | + TRIM(LCASE(user_login)) = TRIM(LCASE('$author')) OR | |
| 2376 | + ( | |
| 2377 | + LENGTH(TRIM(LCASE(user_email))) > 0 | |
| 2378 | + AND TRIM(LCASE(user_email)) = TRIM(LCASE('$email')) | |
| 2379 | + ) OR | |
| 2380 | + TRIM(LCASE(user_firstname)) = TRIM(LCASE('$author')) OR | |
| 2381 | + TRIM(LCASE(user_nickname)) = TRIM(LCASE('$author')) OR | |
| 2382 | + TRIM(LCASE(user_nicename)) = TRIM(LCASE('$nice_author')) OR | |
| 2383 | + TRIM(LCASE(user_description)) = TRIM(LCASE('$author')) OR | |
| 2384 | + ( | |
| 2385 | + LOWER(user_description) | |
| 2386 | + RLIKE CONCAT( | |
| 2387 | + '(^|\\n)a.k.a.( |\\t)*:?( |\\t)*', | |
| 2388 | + LCASE('$reg_author'), | |
| 2389 | + '( |\\t|\\r)*(\\n|\$)' | |
| 2390 | + ) | |
| 2374 | 2391 | ) |
| 2375 | - ) | |
| 2376 | - "); | |
| 2392 | + "); | |
| 2393 | + elseif ($wp_db_version >= 2966) : | |
| 2394 | + #-- WordPress 2.0+ | |
| 2395 | + | |
| 2396 | + # First try the user core data table. | |
| 2397 | + $id = $wpdb->get_var( | |
| 2398 | + "SELECT ID FROM $wpdb->users | |
| 2399 | + WHERE | |
| 2400 | + TRIM(LCASE(user_login)) = TRIM(LCASE('$author')) | |
| 2401 | + OR ( | |
| 2402 | + LENGTH(TRIM(LCASE(user_email))) > 0 | |
| 2403 | + AND TRIM(LCASE(user_email)) = TRIM(LCASE('$email')) | |
| 2404 | + ) | |
| 2405 | + OR TRIM(LCASE(user_nicename)) = TRIM(LCASE('$nice_author')) | |
| 2406 | + "); | |
| 2377 | 2407 | |
| 2408 | + if (is_null($id)) : # Then look for aliases in the user meta data table | |
| 2409 | + $id = $wpdb->get_var( | |
| 2410 | + "SELECT user_id FROM $wpdb->usermeta | |
| 2411 | + WHERE | |
| 2412 | + (meta_key = 'description' AND TRIM(LCASE(meta_value)) = TRIM(LCASE('$author'))) | |
| 2413 | + OR ( | |
| 2414 | + meta_key = 'description' | |
| 2415 | + AND LCASE(meta_value) | |
| 2416 | + RLIKE CONCAT( | |
| 2417 | + '(^|\\n)a.k.a.( |\\t)*:?( |\\t)*', | |
| 2418 | + LCASE('$reg_author'), | |
| 2419 | + '( |\\t|\\r)*(\\n|\$)' | |
| 2420 | + ) | |
| 2421 | + ) | |
| 2422 | + "); | |
| 2423 | + endif; | |
| 2424 | + endif; | |
| 2425 | + | |
| 2426 | + // ... if you don't find one, then do what you need to do | |
| 2378 | 2427 | if (is_null($id)) : |
| 2379 | 2428 | if ($unfamiliar_author === 'create') : |
| 2380 | - $wpdb->query ( | |
| 2381 | - "INSERT INTO $wpdb->users | |
| 2382 | - SET | |
| 2383 | - ID='0', | |
| 2384 | - user_login='$author', | |
| 2385 | - user_firstname='$author', | |
| 2386 | - user_nickname='$author', | |
| 2387 | - user_nicename='$nice_author', | |
| 2388 | - user_description='$author', | |
| 2389 | - user_email='$email', | |
| 2390 | - user_url='$url'"); | |
| 2391 | - $id = $wpdb->insert_id; | |
| 2429 | + if (!isset($wp_db_version)) : | |
| 2430 | + #-- WordPress 1.5.x | |
| 2431 | + $wpdb->query ( | |
| 2432 | + "INSERT INTO $wpdb->users | |
| 2433 | + SET | |
| 2434 | + ID='0', | |
| 2435 | + user_login='$author', | |
| 2436 | + user_firstname='$author', | |
| 2437 | + user_nickname='$author', | |
| 2438 | + user_nicename='$nice_author', | |
| 2439 | + user_description='$author', | |
| 2440 | + user_email='$email', | |
| 2441 | + user_url='$url'"); | |
| 2442 | + $id = $wpdb->insert_id; | |
| 2443 | + elseif ($wp_db_version >= 2966) : | |
| 2444 | + #-- WordPress 2.0+ | |
| 2445 | + $userdata = array(); | |
| 2446 | + | |
| 2447 | + #-- user table data | |
| 2448 | + $userdata['ID'] = NULL; // new user | |
| 2449 | + $userdata['user_login'] = $author; | |
| 2450 | + $userdata['user_pass'] = FeedWordPress::rpc_secret(); | |
| 2451 | + $userdata['user_email'] = $email; | |
| 2452 | + $userdata['user_url'] = $url; | |
| 2453 | + $userdata['display_name'] = $author; | |
| 2454 | + | |
| 2455 | + $id = wp_insert_user($userdata); | |
| 2456 | + endif; | |
| 2392 | 2457 | elseif ($unfamiliar_author === 'default') : |
| 2393 | 2458 | $id = 1; |
| 2394 | 2459 | endif; |
| 2395 | 2460 | endif; |
| @@ -2563,9 +2628,9 @@ | ||
| 2563 | 2628 | if (is_null($from) or $from <= 0.96) : $from = 0.96; endif; |
| 2564 | 2629 | |
| 2565 | 2630 | switch ($from) : |
| 2566 | 2631 | case 0.96: // account for changes to syndication custom values and guid |
| 2567 | - echo "<p>Upgrading database from {$from} to 0.97...</p>\n"; | |
| 2632 | + echo "<p>Upgrading database from {$from} to 0.97+...</p>\n"; | |
| 2568 | 2633 | |
| 2569 | 2634 | $cat_id = FeedWordPress::link_category_id(); |
| 2570 | 2635 | |
| 2571 | 2636 | // Avoid duplicates |