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
This commit is contained in:
Boone Gorges 2016-03-14 13:52:14 +00:00
parent f653932157
commit 0fdf0d4566
2 changed files with 24 additions and 3 deletions

View File

@ -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;
}

View File

@ -0,0 +1,15 @@
<?php
/**
* @group taxonomy
* @group category.php
*/
class Tests_Category_GetCategories extends WP_UnitTestCase {
/**
* @ticket 36227
*/
public function test_wp_error_should_return_an_empty_array() {
$found = get_categories( array( 'taxonomy' => 'foo' ) );
$this->assertSame( array(), $found );
}
}