Add checks for WP_Error. Props filosofo. see #4809

git-svn-id: http://svn.automattic.com/wordpress/trunk@6125 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-09-18 16:32:22 +00:00
parent f30cd7acec
commit 9bfccd6286
23 changed files with 151 additions and 29 deletions

View File

@ -204,8 +204,11 @@ case 'add-meta' :
$now = current_time('timestamp', 1);
if ( $pid = wp_insert_post( array(
'post_title' => sprintf('Draft created on %s at %s', date(get_option('date_format'), $now), date(get_option('time_format'), $now))
) ) )
) ) ) {
if ( is_wp_error( $pid ) )
return $pid;
$mid = add_meta( $pid );
}
else
die('0');
} else if ( !$mid = add_meta( $id ) ) {

View File

@ -380,7 +380,9 @@ class Blogger_Import {
$entry = "<feed>$entry</feed>";
$AtomParser = new AtomParser();
$AtomParser->parse( $entry );
$this->import_post($AtomParser->entry);
$result = $this->import_post($AtomParser->entry);
if ( is_wp_error( $result ) )
return $result;
unset($AtomParser);
}
} else break;
@ -518,6 +520,8 @@ class Blogger_Import {
$post = compact('post_date', 'post_content', 'post_title', 'post_status');
$post_id = wp_insert_post($post);
if ( is_wp_error( $post_id ) )
return $post_id;
wp_create_categories( array_map( 'addslashes', $entry->categories ), $post_id );
@ -531,6 +535,7 @@ class Blogger_Import {
++$this->blogs[$importing_blog]['posts_done'];
}
$this->save_vars();
return;
}
function import_comment( $entry ) {
@ -767,7 +772,9 @@ class Blogger_Import {
if ( isset( $_REQUEST['blog'] ) ) {
$blog = is_array($_REQUEST['blog']) ? array_shift( array_keys( $_REQUEST['blog'] ) ) : $_REQUEST['blog'];
$blog = (int) $blog;
$this->import_blog( $blog );
$result = $this->import_blog( $blog );
if ( is_wp_error( $result ) )
echo $result->get_error_message();
} elseif ( isset($_GET['token']) )
$this->auth();
elseif ( $this->token && $this->token_is_valid() )

View File

@ -91,6 +91,9 @@ class BW_Import {
printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
$postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
$post_id = wp_insert_post($postdata);
if ( is_wp_error( $post_id ) ) {
return $post_id;
}
if (!$post_id) {
_e("Couldn't get post ID");
echo '</li>';
@ -155,7 +158,9 @@ class BW_Import {
}
$this->file = $file['file'];
$this->import_posts();
$result = $this->import_posts();
if ( is_wp_error( $result ) )
return $result;
wp_import_cleanup($file['id']);
echo '<h3>';
@ -176,7 +181,9 @@ class BW_Import {
$this->greet();
break;
case 1 :
$this->import();
$result = $this->import();
if ( is_wp_error( $result ) )
$result->get_error_message();
break;
}

View File

@ -364,6 +364,8 @@ class Dotclear_Import {
'ping_status' => $comment_status_map[$post_open_tb],
'comment_count' => $post_nb_comment + $post_nb_trackback)
);
if ( is_wp_error( $ret_id ) )
return $ret_id;
}
else
{
@ -382,6 +384,8 @@ class Dotclear_Import {
'ping_status' => $comment_status_map[$post_open_tb],
'comment_count' => $post_nb_comment + $post_nb_trackback)
);
if ( is_wp_error( $ret_id ) )
return $ret_id;
}
$dcposts2wpposts[$post_id] = $ret_id;
@ -562,7 +566,9 @@ class Dotclear_Import {
{
// Post Import
$posts = $this->get_dc_posts();
$this->posts2wp($posts);
$result = $this->posts2wp($posts);
if ( is_wp_error( $result ) )
return $result;
echo '<form action="admin.php?import=dotclear&amp;step=4" method="post">';
wp_nonce_field('import-dotclear');
@ -710,7 +716,9 @@ class Dotclear_Import {
$this->import_users();
break;
case 3 :
$this->import_posts();
$result = $this->import_posts();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
case 4 :
$this->import_comments();

View File

@ -233,6 +233,8 @@ class GM_Import {
$postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt');
$post_ID = wp_insert_post($postdata);
if ( is_wp_error( $post_ID ) )
return $post_ID;
}
$c=count($entry);
@ -287,6 +289,7 @@ class GM_Import {
<p><?php _e('Completed GreyMatter import!') ?></p>
<?php
$this->footer();
return;
}
function dispatch() {
@ -301,7 +304,9 @@ class GM_Import {
break;
case 1:
check_admin_referer('import-greymatter');
$this->import();
$result = $this->import();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
}
}

View File

@ -71,6 +71,8 @@ class LJ_Import {
printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
$postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
$post_id = wp_insert_post($postdata);
if ( is_wp_error( $post_id ) )
return $post_id;
if (!$post_id) {
_e("Couldn't get post ID");
echo '</li>';
@ -132,7 +134,9 @@ class LJ_Import {
}
$this->file = $file['file'];
$this->import_posts();
$result = $this->import_posts();
if ( is_wp_error( $result ) )
return $result;
wp_import_cleanup($file['id']);
echo '<h3>';
@ -154,7 +158,9 @@ class LJ_Import {
break;
case 1 :
check_admin_referer('import-upload');
$this->import();
$result = $this->import();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
}

View File

@ -215,6 +215,8 @@ class MT_Import {
$post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor
$post_id = wp_insert_post($post);
if ( is_wp_error( $post_id ) )
return $post_id;
// Add categories.
if ( 0 != count($post->categories) ) {
@ -291,7 +293,9 @@ class MT_Import {
} else if ( '--------' == $line ) {
// Finishing a post.
$context = '';
$this->save_post($post, $comments, $pings);
$result = $this->save_post($post, $comments, $pings);
if ( is_wp_error( $result ) )
return $result;
$post = new StdClass;
$comment = new StdClass();
$ping = new StdClass();
@ -415,7 +419,9 @@ class MT_Import {
else
$this->file = get_attached_file($this->id);
$this->get_authors_from_post();
$this->process_posts();
$result = $this->process_posts();
if ( is_wp_error( $result ) )
return $result;
}
function dispatch() {
@ -434,7 +440,9 @@ class MT_Import {
break;
case 2:
check_admin_referer('import-mt');
$this->import();
$result = $this->import();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
}
}

View File

@ -110,6 +110,8 @@ class RSS_Import {
_e('Post already imported');
} else {
$post_id = wp_insert_post($post);
if ( is_wp_error( $post_id ) )
return $post_id;
if (!$post_id) {
_e("Couldn't get post ID");
return;
@ -135,7 +137,9 @@ class RSS_Import {
$this->file = $file['file'];
$this->get_posts();
$this->import_posts();
$result = $this->import_posts();
if ( is_wp_error( $result ) )
return $result;
wp_import_cleanup($file['id']);
echo '<h3>';
@ -157,7 +161,9 @@ class RSS_Import {
break;
case 1 :
check_admin_referer('import-upload');
$this->import();
$result = $this->import();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
}

View File

@ -305,6 +305,8 @@ class Textpattern_Import {
'post_name' => $url_title,
'comment_count' => $comments_count)
);
if ( is_wp_error( $ret_id ) )
return $ret_id;
}
else
{
@ -321,6 +323,8 @@ class Textpattern_Import {
'post_name' => $url_title,
'comment_count' => $comments_count)
);
if ( is_wp_error( $ret_id ) )
return $ret_id;
}
$txpposts2wpposts[$ID] = $ret_id;
@ -498,7 +502,9 @@ class Textpattern_Import {
{
// Post Import
$posts = $this->get_txp_posts();
$this->posts2wp($posts);
$result = $this->posts2wp($posts);
if ( is_wp_error( $result ) )
return $result;
echo '<form action="admin.php?import=textpattern&amp;step=4" method="post">';
wp_nonce_field('import-textpattern');
@ -638,7 +644,9 @@ class Textpattern_Import {
$this->import_users();
break;
case 3 :
$this->import_posts();
$result = $this->import_posts();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
case 4 :
$this->import_comments();

View File

@ -250,8 +250,11 @@ class WP_Import {
$i = -1;
echo '<ol>';
foreach ($this->posts as $post)
$this->process_post($post);
foreach ($this->posts as $post) {
$result = $this->process_post($post);
if ( is_wp_error( $result ) )
return $result;
}
echo '</ol>';
@ -303,7 +306,11 @@ class WP_Import {
// If it has parent, process parent first.
$post_parent = (int) $post_parent;
if ($parent = $this->posts_processed[$post_parent]) {
if (!$parent[1]) $this->process_post($parent[0]); // If not yet, process the parent first.
if (!$parent[1]) {
$result = $this->process_post($parent[0]); // If not yet, process the parent first.
if ( is_wp_error( $result ) )
return $result;
}
$post_parent = $parent[1]; // New ID of the parent;
}
@ -314,6 +321,8 @@ class WP_Import {
$postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'post_name', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt', 'guid', 'post_parent', 'menu_order', 'post_type');
$comment_post_ID = $post_id = wp_insert_post($postdata);
if ( is_wp_error( $post_id ) )
return $post_id;
// Memorize old and new ID.
if ( $post_id && $post_ID && $this->posts_processed[$post_ID] )
@ -382,7 +391,9 @@ class WP_Import {
$this->get_authors_from_post();
$this->get_entries();
$this->process_categories();
$this->process_posts();
$result = $this->process_posts();
if ( is_wp_error( $result ) )
return $result;
}
function dispatch() {
@ -402,7 +413,9 @@ class WP_Import {
break;
case 2:
check_admin_referer('import-wordpress');
$this->import();
$result = $this->import();
if ( is_wp_error( $result ) )
echo $result->get_error_message();
break;
}
$this->footer();

View File

@ -8,7 +8,8 @@ function get_importers() {
function register_importer( $id, $name, $description, $callback ) {
global $wp_importers;
if ( is_wp_error( $callback ) )
return $callback;
$wp_importers[$id] = array ( $name, $description, $callback );
}

View File

@ -284,6 +284,8 @@ function wp_write_post() {
// Create the post.
$post_ID = wp_insert_post( $_POST );
if ( is_wp_error( $post_ID ) )
return $post_ID;
if ( empty($post_ID) )
return 0;

View File

@ -992,7 +992,7 @@ function dbDelta($queries, $execute = true) {
}
// Add the column list to the index create string
$index_string .= ' ('.$index_columns.')';
error_log("Index string: $index_string", 0);
if(!(($aindex = array_search($index_string, $indices)) === false)) {
unset($indices[$aindex]);
//echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";

View File

@ -159,6 +159,8 @@ if ( $links ) {
$cat_names = array();
foreach ($link->link_category as $category) {
$cat = get_term($category, 'link_category', OBJECT, 'display');
if ( is_wp_error( $cat ) )
echo $cat->get_error_message();
$cat_name = $cat->name;
if ( $cat_id != $category )
$cat_name = "<a href='link-manager.php?cat_id=$category'>$cat_name</a>";

View File

@ -262,6 +262,8 @@ EOD;
log_app('Inserting Post. Data:', print_r($post_data,true));
$postID = wp_insert_post($post_data);
if ( is_wp_error( $postID ) )
$this->internal_error($postID->get_error_message());
if (!$postID) {
$this->internal_error(__('Sorry, your entry could not be posted. Something wrong happened.'));

View File

@ -12,6 +12,8 @@ function get_category_children($id, $before = '/', $after = '') {
continue;
$category = get_category($cat_id);
if ( is_wp_error( $category ) )
return $category;
if ( $category->parent == $id ) {
$chain .= $before.$category->term_id.$after;
$chain .= get_category_children($category->term_id, $before, $after);
@ -29,6 +31,8 @@ function get_category_link($category_id) {
$catlink = $file . '?cat=' . $category_id;
} else {
$category = &get_category($category_id);
if ( is_wp_error( $category ) )
return $category;
$category_nicename = $category->slug;
if ( $parent = $category->parent )
@ -43,6 +47,8 @@ function get_category_link($category_id) {
function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
$chain = '';
$parent = &get_category($id);
if ( is_wp_error( $parent ) )
return $parent;
if ( $nicename )
$name = $parent->slug;
@ -98,6 +104,8 @@ function _usort_terms_by_ID($a, $b) {
function get_the_category_by_ID($cat_ID) {
$cat_ID = (int) $cat_ID;
$category = &get_category($cat_ID);
if ( is_wp_error( $category ) )
return $category;
return $category->name;
}
@ -313,6 +321,9 @@ function wp_tag_cloud( $args = '' ) {
return;
$return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
if ( is_wp_error( $return ) )
return false;
else
echo apply_filters( 'wp_tag_cloud', $return, $args );
}
@ -334,6 +345,8 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
foreach ( (array) $tags as $tag ) {
$counts[$tag->name] = $tag->count;
$tag_links[$tag->name] = get_tag_link( $tag->term_id );
if ( is_wp_error( $tag_links[$tag->name] ) )
return $tag_links[$tag->name];
$tag_ids[$tag->name] = $tag->term_id;
}
@ -410,6 +423,8 @@ function get_tag_link( $tag_id ) {
$taglink = $wp_rewrite->get_tag_permastruct();
$tag = &get_term($tag_id, 'post_tag');
if ( is_wp_error( $tag ) )
return $tag;
$slug = $tag->slug;
if ( empty($taglink) ) {
@ -450,8 +465,12 @@ function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
return false;
$tag_list = $before;
foreach ( $tags as $tag )
$tag_links[] = '<a href="' . get_tag_link($tag->term_id) . '" rel="tag">' . $tag->name . '</a>';
foreach ( $tags as $tag ) {
$link = get_tag_link($tag->term_id);
if ( is_wp_error( $link ) )
return $link;
$tag_links[] = '<a href="' . $link . '" rel="tag">' . $tag->name . '</a>';
}
$tag_links = join( $sep, $tag_links );
$tag_links = apply_filters( 'the_tags', $tag_links );
@ -463,7 +482,11 @@ function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
}
function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) {
echo get_the_tag_list($before, $sep, $after);
$return = get_the_tag_list($before, $sep, $after);
if ( is_wp_error( $return ) )
return false;
else
echo $return;
}
?>

View File

@ -30,6 +30,8 @@ function &get_categories($args = '') {
// Handles category caching.
function &get_category($category, $output = OBJECT, $filter = 'raw') {
$category = get_term($category, 'category', $output, $filter);
if ( is_wp_error( $category ) )
return $category;
_make_cat_compat($category);
@ -58,6 +60,8 @@ function get_category_by_path($category_path, $full_match = true, $output = OBJE
$curcategory = $category;
while ( ($curcategory->parent != 0) && ($curcategory->parent != $curcategory->term_id) ) {
$curcategory = get_term($curcategory->parent, 'category');
if ( is_wp_error( $curcategory ) )
return $curcategory;
$path = '/' . $curcategory->slug . $path;
}

View File

@ -12,6 +12,8 @@ function bloginfo_rss($show = '') {
function get_wp_title_rss($sep = '&#187;') {
$title = wp_title($sep, false);
if ( is_wp_error( $title ) )
return $title->get_error_message();
$title = apply_filters('get_wp_title_rss', $title);
return $title;
}

View File

@ -195,6 +195,8 @@ function wp_title($sep = '&raquo;', $display = true) {
if ( !empty($tag) ) {
$tag = get_term($tag, 'post_tag', OBJECT, 'display');
if ( is_wp_error( $tag ) )
return $tag;
if ( ! empty($tag->name) )
$title = apply_filters('single_tag_title', $tag->name);
}

View File

@ -940,6 +940,8 @@ class WP_Query {
if ( !empty($q['category__not_in']) ) {
$ids = get_objects_in_term($q['category__not_in'], 'category');
if ( is_wp_error( $ids ) )
return $ids;
if ( is_array($ids) && count($ids > 0) ) {
$out_posts = "'" . implode("', '", $ids) . "'";
$whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
@ -1415,6 +1417,8 @@ class WP_Query {
} else if ($this->is_tag) {
$tag_id = $this->get('tag_id');
$tag = &get_term($tag_id, 'post_tag');
if ( is_wp_error( $tag ) )
return $tag;
$this->queried_object = &$tag;
$this->queried_object_id = (int) $tag_id;
} else if ($this->is_posts_page) {

View File

@ -391,7 +391,6 @@ function get_term_children( $term, $taxonomy ) {
function get_term_field( $field, $term, $taxonomy, $context = 'display' ) {
$term = (int) $term;
$term = get_term( $term, $taxonomy );
if ( is_wp_error($term) )
return $term;
@ -801,6 +800,8 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) {
// Update children to point to new parent
if ( is_taxonomy_hierarchical($taxonomy) ) {
$term_obj = get_term($term, $taxonomy);
if ( is_wp_error( $term_obj ) )
return $term_obj;
$parent = $term_obj->parent;
$wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = '$parent' WHERE parent = '$term_obj->term_id' AND taxonomy = '$taxonomy'");
@ -1252,6 +1253,8 @@ function &_get_term_children($term_id, $terms, $taxonomy) {
$use_id = false;
if ( !is_object($term) ) {
$term = get_term($term, $taxonomy);
if ( is_wp_error( $term ) )
return $term;
$use_id = true;
}

View File

@ -148,6 +148,8 @@ for ($i=1; $i <= $count; $i++) :
$post_data = add_magic_quotes($post_data);
$post_ID = wp_insert_post($post_data);
if ( is_wp_error( $post_ID ) )
echo "\n" . $post_ID->get_error_message();
if (!$post_ID) {
// we couldn't post, for whatever reason. better move forward to the next email

View File

@ -800,6 +800,8 @@ class wp_xmlrpc_server extends IXR_Server {
$post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
$post_ID = wp_insert_post($post_data);
if ( is_wp_error( $post_ID ) )
return new IXR_Error(500, $post_ID->get_error_message());
if (!$post_ID) {
return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
@ -1088,6 +1090,8 @@ class wp_xmlrpc_server extends IXR_Server {
$postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'to_ping', 'post_type', 'post_name', 'post_password', 'post_parent', 'menu_order', 'tags_input');
$post_ID = wp_insert_post($postdata);
if ( is_wp_error( $post_ID ) )
return new IXR_Error(500, $post_ID->get_error_message());
if (!$post_ID) {
return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));