From 22b0cb202991d66dfa611da11e26ebf2b36abe0d Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 17 Dec 2015 16:26:06 +0000 Subject: [PATCH] Introduce 'tag-link-position-x' class to tag cloud links. The new class describes the cardinal position of a link in the cloud, allowing more fine-grained CSS and JS targeting. Props Mte90, chmac. Fixes #5172. git-svn-id: https://develop.svn.wordpress.org/trunk@35984 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/category-template.php | 3 +- .../phpunit/tests/term/wpGenerateTagCloud.php | 42 ++++++++++++++----- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/category-template.php b/src/wp-includes/category-template.php index 68cd74c8e5..971434baf2 100644 --- a/src/wp-includes/category-template.php +++ b/src/wp-includes/category-template.php @@ -904,7 +904,8 @@ function wp_generate_tag_cloud( $tags, $args = '' ) { // generate the output links array foreach ( $tags_data as $key => $tag_data ) { - $a[] = "" . esc_html( $tag_data['name'] ) . ""; + $class = $tag_data['class'] . ' tag-link-position-' . ( $key + 1 ); + $a[] = "" . esc_html( $tag_data['name'] ) . ""; } switch ( $args['format'] ) { diff --git a/tests/phpunit/tests/term/wpGenerateTagCloud.php b/tests/phpunit/tests/term/wpGenerateTagCloud.php index 742b3210c1..47a1283657 100644 --- a/tests/phpunit/tests/term/wpGenerateTagCloud.php +++ b/tests/phpunit/tests/term/wpGenerateTagCloud.php @@ -78,7 +78,7 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase { 'number' => 1, 'hide_empty' => false, ) ); - $expected = "{$term->name}"; + $expected = "{$term->name}"; $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, array( 'hide_empty' => false, ) ) ); @@ -94,7 +94,7 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase { 'format' => 'array', ) ); - $expected = "{$term->name}"; + $expected = "{$term->name}"; $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, array( 'hide_empty' => false, ) ) ); @@ -109,7 +109,7 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase { 'hide_empty' => false, ) ); - $expected = "\n"; + $expected = "\n"; $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, array( 'hide_empty' => false, 'format' => 'list', @@ -129,10 +129,10 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase { 'hide_empty' => false, ) ); - $expected = "{$terms[0]->name}\n". - "{$terms[1]->name}\n". - "{$terms[2]->name}\n". - "{$terms[3]->name}"; + $expected = "{$terms[0]->name}\n". + "{$terms[1]->name}\n". + "{$terms[2]->name}\n". + "{$terms[3]->name}"; $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, array( 'hide_empty' => false, ) ) ); @@ -152,10 +152,10 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase { ) ); $expected = "\n"; $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, array( @@ -219,6 +219,26 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase { $this->assertContains( "title='2 foo'", $actual[1] ); } + /** + * @ticket 5172 + */ + public function test_should_include_tag_link_position_class() { + register_taxonomy( 'wptests_tax', 'post' ); + $term_ids = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); + + $p = self::factory()->post->create(); + wp_set_post_terms( $p, $term_ids, 'wptests_tax' ); + + $term_objects = get_terms( 'wptests_tax', array( + 'include' => $term_ids, + ) ); + + $cloud = wp_generate_tag_cloud( $term_objects ); + preg_match_all( '|tag\-link\-position-([0-9]+)|', $cloud, $matches ); + + $this->assertSame( array( 1, 2, 3 ), array_map( 'intval', $matches[1] ) ); + } + /** * Helper method retrieve the created terms. *