From 0fdf0d45664c2caa398f535829076f23e5987f84 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 14 Mar 2016 13:52:14 +0000 Subject: [PATCH] Improve error handling in `get_categories()`. When passed an invalid `'taxonomy'`, `get_terms()` will return a `WP_Error` object. This object should not be blindly cast to an array. Instead, an empty array should be returned, to indicate that no matching terms have been found. Props virgodesign, sebastian.pisula. Fixes #36227. git-svn-id: https://develop.svn.wordpress.org/trunk@36988 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/category.php | 12 +++++++++--- tests/phpunit/tests/category/getCategories.php | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/tests/category/getCategories.php diff --git a/src/wp-includes/category.php b/src/wp-includes/category.php index cbef474490..a89b41065f 100644 --- a/src/wp-includes/category.php +++ b/src/wp-includes/category.php @@ -51,10 +51,16 @@ function get_categories( $args = '' ) { $taxonomy = $args['taxonomy'] = 'link_category'; } - $categories = (array) get_terms( $taxonomy, $args ); + $categories = get_terms( $taxonomy, $args ); - foreach ( array_keys( $categories ) as $k ) - _make_cat_compat( $categories[$k] ); + if ( is_wp_error( $categories ) ) { + $categories = array(); + } else { + $categories = (array) $categories; + foreach ( array_keys( $categories ) as $k ) { + _make_cat_compat( $categories[ $k ] ); + } + } return $categories; } diff --git a/tests/phpunit/tests/category/getCategories.php b/tests/phpunit/tests/category/getCategories.php new file mode 100644 index 0000000000..3f2140dd24 --- /dev/null +++ b/tests/phpunit/tests/category/getCategories.php @@ -0,0 +1,15 @@ + 'foo' ) ); + $this->assertSame( array(), $found ); + } +}