Eliminate use of `extract()` in `get_the_taxonomies()`. Adds unit test.

See #22400.


git-svn-id: https://develop.svn.wordpress.org/trunk@28415 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2014-05-15 03:50:46 +00:00
parent e135cc2076
commit 3e54a1fab5
2 changed files with 38 additions and 18 deletions

View File

@ -3712,39 +3712,43 @@ function the_taxonomies($args = array()) {
* @param array $args Override the defaults. * @param array $args Override the defaults.
* @return array * @return array
*/ */
function get_the_taxonomies($post = 0, $args = array() ) { function get_the_taxonomies( $post = 0, $args = array() ) {
$post = get_post( $post ); $post = get_post( $post );
$args = wp_parse_args( $args, array( $args = wp_parse_args( $args, array(
'template' => '%s: %l.', 'template' => '%s: %l.',
) ); ) );
extract( $args, EXTR_SKIP );
$taxonomies = array(); $taxonomies = array();
if ( !$post ) if ( ! $post ) {
return $taxonomies; return $taxonomies;
}
foreach ( get_object_taxonomies($post) as $taxonomy ) { foreach ( get_object_taxonomies( $post ) as $taxonomy ) {
$t = (array) get_taxonomy($taxonomy); $t = (array) get_taxonomy( $taxonomy );
if ( empty($t['label']) ) if ( empty( $t['label'] ) ) {
$t['label'] = $taxonomy; $t['label'] = $taxonomy;
if ( empty($t['args']) ) }
if ( empty( $t['args'] ) ) {
$t['args'] = array(); $t['args'] = array();
if ( empty($t['template']) ) }
$t['template'] = $template; if ( empty( $t['template'] ) ) {
$t['template'] = $args['template'];
$terms = get_object_term_cache($post->ID, $taxonomy); }
if ( false === $terms )
$terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
$terms = get_object_term_cache( $post->ID, $taxonomy );
if ( false === $terms ) {
$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
}
$links = array(); $links = array();
foreach ( $terms as $term ) foreach ( $terms as $term ) {
$links[] = "<a href='" . esc_attr( get_term_link($term) ) . "'>$term->name</a>"; $links[] = "<a href='" . esc_attr( get_term_link( $term ) ) . "'>$term->name</a>";
}
if ( $links ) if ( $links ) {
$taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms); $taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
}
} }
return $taxonomies; return $taxonomies;
} }

View File

@ -33,6 +33,22 @@ class Tests_Taxonomy extends WP_UnitTestCase {
} }
} }
function test_get_the_taxonomies() {
$post_id = $this->factory->post->create();
$taxes = get_the_taxonomies( $post_id );
$this->assertNotEmpty( $taxes );
$this->assertEquals( array( 'category' ), array_keys( $taxes ) );
$id = $this->factory->tag->create();
wp_set_post_tags( $post_id, array( $id ) );
$taxes = get_the_taxonomies( $post_id );
$this->assertNotEmpty( $taxes );
$this->assertCount( 2, $taxes );
$this->assertEquals( array( 'category', 'post_tag' ), array_keys( $taxes ) );
}
function test_get_link_taxonomy() { function test_get_link_taxonomy() {
foreach ( get_object_taxonomies('link') as $taxonomy ) { foreach ( get_object_taxonomies('link') as $taxonomy ) {
$tax = get_taxonomy($taxonomy); $tax = get_taxonomy($taxonomy);