Make WordPress Core

Opened 16 months ago

Last modified 6 days ago

#60052 assigned defect (bug)

Error when deleting media if default taxonomies have been removed

Reported by: imatoni's profile imatoni Owned by: adamsilverstein's profile adamsilverstein
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.4.2
Component: General Keywords: dev-feedback has-patch has-unit-tests
Focuses: Cc:

Description

Hello,

I recently had a problem with deleting media from the media library.

I received the following error message:

2151#0: *203114 FastCGI sent in stderr: "PHP message: UM_TEST_ERROR_LOG
PHP message: 63870
PHP message: "category"
PHP message: UM_TEST_ERROR_LOG
PHP message: 63870
PHP message: "post_tag"
PHP message: PHP Fatal error: Uncaught TypeError: array_map(): Argument #2 ($array) must be of type array, WP_Error given in /var/www/vhosts/fis-gmbh.de/stage.fis-gmbh.de/wp-includes/taxonomy.php:1964
Stack trace:
#0 /var/www/vhosts/fis-gmbh.de/stage.fis-gmbh.de/wp-includes/taxonomy.php(1964): array_map()
#1 /var/www/vhosts/fis-gmbh.de/stage.fis-gmbh.de/wp-includes/post.php(6304): wp_delete_object_term_relationships()
#2 /var/www/vhosts/fis-gmbh.de/stage.fis-gmbh.de/wp-includes/post.php(3416): wp_delete_attachment()
#3 /var/www/vhosts/fis-gmbh.de/stage.fis-gmbh.de/wp-admin/includes/ajax-actions.php(886): wp_delete_post()
#4 /var/www/vhosts/fis-gmbh.de/stage.fis-gmbh.de/wp-includes/class-wp-hook.php(324): wp_ajax_delete_post()
#5 /var/www/vhosts/fis-gmbh.de/stage.fis-gmbh.de/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#6 /var/www/vhosts/fis-gmbh.de/stage.fis-gmbh.de/wp-includes/plugin.php(517): WP_Hook->do_action()
#7 /var/www/vhosts/fis-gmbh.de/stage.fis-gmbh.de/wp-admin/admin-ajax.php(1" while reading response header from upstream

This happened because we had used the following code elsewhere:
add_action( 'init', 'deregister_tag' );
function deregister_tag() {
global $wp_taxonomies;
$tax = 'post_tag';
if ( taxonomy_exists( $tax ) ) {
unset( $wp_taxonomies[ $tax ] );
}
}

This code ensures that the standard WordPress taxonomy 'post_tag' is no longer present (this was not necessary for this project).

In wp-includes/post.php there is the following call:
wp_delete_object_term_relationships( $post_id, array( 'category', 'post_tag' ) );
This call causes an error to occur if the taxonomy 'post_tag' is not present.

I am of the opinion that this error should be caught in the WordPress core.

This issue is also being discussed here at WPML:
https://wpml.org/forums/topic/fatal-error-when-deleting-a-media/

Thank you.

Change History (12)

This ticket was mentioned in Slack in #core-test by ankit-k-gupta. View the logs.


16 months ago

#2 @Ankit K Gupta
16 months ago

  • Keywords dev-feedback added

#3 @imatoni
16 months ago

oops, the first lines in the error message came from my error log. Of course, they do not come from the core.

This comes from my error_log():

UM_TEST_ERROR_LOG
PHP message: 63870
PHP message: "category"
PHP message: UM_TEST_ERROR_LOG
PHP message: 63870
PHP message: "post_tag"

#4 @imatoni
16 months ago

  • Resolution set to maybelater
  • Status changed from new to closed

#5 @imatoni
16 months ago

  • Resolution maybelater deleted
  • Status changed from closed to reopened

This ticket was mentioned in PR #6230 on WordPress/wordpress-develop by JPrentice-thecode.


13 months ago
#6

  • Keywords has-patch added

https://core.trac.wordpress.org/ticket/60052#no0

that causes crit errors if default taxonomies catergory / post_tag have been removed or disabled.

The following line is a more generic check that would check category and post tag if they existed.

Deleted: wp_delete_object_term_relationships( $post_id, array( 'category', 'post_tag' ) );

This following line does the same thing but doesn't fail if empty, and covers any custom taxonomies.

Following line:

wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) );

#8 @jeremyprentice
5 weeks ago

  • Keywords needs-unit-tests added

https://github.com/WordPress/wordpress-develop/pull/6230

Instead of removing the line, checks added.

Needs unit tests

#9 @anthonythorne
5 weeks ago

  • Keywords has-testing-info added

Testing Criteria & Summary of Unit Test

Testing Criteria:
Ensure that an attachment can be deleted even when the post_tag taxonomy has been unregistered.
Verify that wp_delete_attachment() correctly handles the absence of post_tag without errors.
Simulate real-world conditions by unregistering both category and post_tag taxonomies before deleting the attachment.
Confirm that the function returns a valid WP_Post object upon successful deletion.

Unit Test Summary:
This unit test creates a test attachment, unregisters the category and post_tag taxonomies, and then attempts to delete the attachment using wp_delete_attachment(). In previous versions, deleting an attachment after unregistering post_tag could cause a failure (#60052). The test ensures that after the core patch, the function correctly handles missing taxonomies and successfully deletes the attachment. The test also explicitly removes taxonomy entries from $wp_taxonomies to fully replicate the missing taxonomy scenario.

Let me know if you'd like any refinements!

Last edited 5 weeks ago by anthonythorne (previous) (diff)

#10 @jeremyprentice
5 weeks ago

  • Keywords has-unit-tests added; needs-unit-tests has-testing-info removed

https://github.com/WordPress/wordpress-develop/pull/6230

@anthonythorne credit for completing the PHPUnit testing.

@jamespayne credit for removing the duplicate calls

#11 @adamsilverstein
5 weeks ago

  • Owner set to adamsilverstein
  • Status changed from reopened to assigned

This ticket was mentioned in Slack in #core by jopdop30. View the logs.


6 days ago

Note: See TracTickets for help on using tickets.