[wp-trac] [WordPress Trac] #54973: Failed wp_insert_category() when IMPORT_DEBUG is set to true produces a fatal error.
WordPress Trac
noreply at wordpress.org
Fri Jan 28 12:46:38 UTC 2022
#54973: Failed wp_insert_category() when IMPORT_DEBUG is set to true produces a
fatal error.
--------------------------+-----------------------------
Reporter: enshrined | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Import | Version:
Severity: normal | Keywords: needs-patch
Focuses: |
--------------------------+-----------------------------
When calling `wp_insert_category()`, setting the second parameter to true
lets us return a `WP_Error`. In this case, we don't, so `0` is returned in
the case of a failure.
If a `WP_Error` or `0` value is returned, it will echo that it failed to
import the category.
If we also have `IMPORT_DEBUG` set to true, it will try and fetch the
error message from the returned value. This is fine, if we're returning a
`WP_Error`, but in the case that it returns `0`, it causes a fatal error.
See: https://github.com/WordPress/wordpress-
importer/blob/99462ec2d3390bc0de3bc5ffbe817f2160e5ef54/src/class-wp-
import.php#L426-L438
`Fatal error: Uncaught Error: Call to a member function
get_error_message() on int`
I suggest that we add an additional check, to see if we have a `WP_Error`,
before trying to render the error message:
{{{#!php
$id = wp_insert_category( $data );
if ( ! is_wp_error( $id ) && $id > 0 ) {
if ( isset( $cat['term_id'] ) ) {
$this->processed_terms[ intval( $cat['term_id'] ) ] = $id;
}
} else {
printf( __( 'Failed to import category %s', 'wordpress-importer'
), esc_html( $cat['category_nicename'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG && is_wp_error( $id
) ) {
echo ': ' . $id->get_error_message();
}
echo '<br />';
continue;
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/54973>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list