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.
* @return array
*/
function get_the_taxonomies($post = 0, $args = array() ) {
function get_the_taxonomies( $post = 0, $args = array() ) {
$post = get_post( $post );
$args = wp_parse_args( $args, array(
'template' => '%s: %l.',
) );
extract( $args, EXTR_SKIP );
$taxonomies = array();
if ( !$post )
if ( ! $post ) {
return $taxonomies;
}
foreach ( get_object_taxonomies($post) as $taxonomy ) {
$t = (array) get_taxonomy($taxonomy);
if ( empty($t['label']) )
foreach ( get_object_taxonomies( $post ) as $taxonomy ) {
$t = (array) get_taxonomy( $taxonomy );
if ( empty( $t['label'] ) ) {
$t['label'] = $taxonomy;
if ( empty($t['args']) )
}
if ( empty( $t['args'] ) ) {
$t['args'] = array();
if ( empty($t['template']) )
$t['template'] = $template;
$terms = get_object_term_cache($post->ID, $taxonomy);
if ( false === $terms )
$terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
}
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'] );
}
$links = array();
foreach ( $terms as $term )
$links[] = "<a href='" . esc_attr( get_term_link($term) ) . "'>$term->name</a>";
if ( $links )
$taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms);
foreach ( $terms as $term ) {
$links[] = "<a href='" . esc_attr( get_term_link( $term ) ) . "'>$term->name</a>";
}
if ( $links ) {
$taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
}
}
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() {
foreach ( get_object_taxonomies('link') as $taxonomy ) {
$tax = get_taxonomy($taxonomy);