Make WordPress Core


Ignore:
Timestamp:
06/29/2015 02:15:59 PM (10 years ago)
Author:
boonebgorges
Message:

Introduce 'wp_generate_tag_cloud_data' filter.

This filter allows developers to modify the data that is used to create tag
clouds, without having to manipulate the tag cloud markup directly.

As part of the refactor, this changeset also adds a few unit tests for the way
wp_generate_tag_cloud() generates the 'title' attribute, as well as
improvements to output escaping.

Props flixos90, ysalame.
Fixes #24656.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/wpGenerateTagCloud.php

    r32995 r32996  
    165165    }
    166166
    167 
     167    public function test_topic_count_text() {
     168        register_taxonomy( 'wptests_tax', 'post' );
     169        $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     170        $posts = $this->factory->post->create_many( 2 );
     171
     172        wp_set_post_terms( $posts[0], $terms, 'wptests_tax' );
     173        wp_set_post_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
     174
     175        $term_objects = $this->retrieve_terms( array(
     176            'include' => $terms,
     177        ), 'wptests_tax' );
     178
     179        $actual = wp_generate_tag_cloud( $term_objects, array(
     180            'format' => 'array',
     181            'topic_count_text' => array(
     182                'singular' => 'Term has %s post',
     183                'plural' => 'Term has %s posts',
     184                'domain' => 'foo',
     185                'context' => 'bar',
     186            ),
     187        ) );
     188
     189        $this->assertContains( "title='Term has 1 post'", $actual[0] );
     190        $this->assertContains( "title='Term has 2 posts'", $actual[1] );
     191    }
     192
     193    public function test_topic_count_text_callback() {
     194        register_taxonomy( 'wptests_tax', 'post' );
     195        $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     196        $posts = $this->factory->post->create_many( 2 );
     197
     198        wp_set_post_terms( $posts[0], $terms, 'wptests_tax' );
     199        wp_set_post_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
     200
     201        $term_objects = $this->retrieve_terms( array(
     202            'include' => $terms,
     203        ), 'wptests_tax' );
     204
     205        $actual = wp_generate_tag_cloud( $term_objects, array(
     206            'format' => 'array',
     207            'topic_count_text_callback' => array( $this, 'topic_count_text_callback' ),
     208        ) );
     209
     210        $this->assertContains( "title='1 foo'", $actual[0] );
     211        $this->assertContains( "title='2 foo'", $actual[1] );
     212    }
    168213
    169214    /**
     
    176221     * @return array
    177222     */
    178     protected function retrieve_terms( $get_terms_args ) {
    179 
    180         $terms = get_terms( array( 'post_tag' ), $get_terms_args );
     223    protected function retrieve_terms( $get_terms_args, $taxonomy = 'post_tag' ) {
     224        $terms = get_terms( array( $taxonomy ), $get_terms_args );
    181225
    182226        $tags = array();
     
    190234        return $tags;
    191235    }
     236
     237    public function topic_count_text_callback( $real_count, $tag, $args ) {
     238        return sprintf( '%s foo', $real_count );
     239    }
    192240}
Note: See TracChangeset for help on using the changeset viewer.