From 3e54a1fab513a891f11d33dcb9aca9e84450ad7d Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 15 May 2014 03:50:46 +0000 Subject: [PATCH] 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 --- src/wp-includes/taxonomy.php | 40 ++++++++++++++++++-------------- tests/phpunit/tests/taxonomy.php | 16 +++++++++++++ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 4b423b4606..f201b36c25 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -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[] = "$term->name"; - - if ( $links ) - $taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms); + foreach ( $terms as $term ) { + $links[] = "$term->name"; + } + if ( $links ) { + $taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms ); + } } return $taxonomies; } diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index 7e4986b62f..fbab63f2a4 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -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);