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:
parent
f653932157
commit
0fdf0d4566
|
@ -51,10 +51,16 @@ function get_categories( $args = '' ) {
|
||||||
$taxonomy = $args['taxonomy'] = 'link_category';
|
$taxonomy = $args['taxonomy'] = 'link_category';
|
||||||
}
|
}
|
||||||
|
|
||||||
$categories = (array) get_terms( $taxonomy, $args );
|
$categories = get_terms( $taxonomy, $args );
|
||||||
|
|
||||||
foreach ( array_keys( $categories ) as $k )
|
if ( is_wp_error( $categories ) ) {
|
||||||
_make_cat_compat( $categories[$k] );
|
$categories = array();
|
||||||
|
} else {
|
||||||
|
$categories = (array) $categories;
|
||||||
|
foreach ( array_keys( $categories ) as $k ) {
|
||||||
|
_make_cat_compat( $categories[ $k ] );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $categories;
|
return $categories;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue