From c8065409430e7cdfbe8a5b969096d66a6571cff4 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 4 Jul 2017 19:58:35 +0000 Subject: [PATCH] Avoid PHP notices when checking termmeta capabilities against a non-existent term. Previously, checks like `current_user_can( 'edit_term_meta', $term_id )` returned the proper value, but generated a PHP notice due to the fact that `get_term( $term_id )` could, in certain instances, return `WP_Error` objects. Props caercam. Fixes #40891. git-svn-id: https://develop.svn.wordpress.org/trunk@40999 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/capabilities.php | 2 +- tests/phpunit/tests/user/capabilities.php | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/capabilities.php b/src/wp-includes/capabilities.php index f40345deb9..13851cc2e1 100644 --- a/src/wp-includes/capabilities.php +++ b/src/wp-includes/capabilities.php @@ -280,7 +280,7 @@ function map_meta_cap( $cap, $user_id ) { case 'term': $term = get_term( $object_id ); - if ( ! $term ) { + if ( ! $term instanceof WP_Term ) { break; } diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php index 2070a64fa4..2dffd25c9f 100644 --- a/tests/phpunit/tests/user/capabilities.php +++ b/tests/phpunit/tests/user/capabilities.php @@ -1353,6 +1353,27 @@ class Tests_User_Capabilities extends WP_UnitTestCase { } } + /** + * @ticket 40891 + */ + public function test_taxonomy_meta_capabilities_with_non_existent_terms() { + $caps = array( + 'add_term_meta', + 'delete_term_meta', + 'edit_term_meta', + ); + + $taxonomy = 'wptests_tax'; + register_taxonomy( $taxonomy, 'post' ); + + $editor = self::$users['editor']; + + foreach ( $caps as $cap ) { + // `null` represents a non-existent term ID. + $this->assertFalse( user_can( $editor->ID, $cap, null ) ); + } + } + /** * @ticket 21786 */