Make WordPress Core

Ticket #9547: 9547.3.diff

File 9547.3.diff, 11.3 KB (added by simonwheatley, 11 years ago)

Refresh the patch, add in the tests patch, fix typos

  • src/wp-includes/category-template.php

     
    12381238 *
    12391239 * @since 2.5.0
    12401240 *
     1241 * @see wp_get_object_terms() for reference on the $args param
     1242 *
    12411243 * @param int|object $post Post ID or object.
    12421244 * @param string $taxonomy Taxonomy name.
     1245 * @param array $args Accepts the same arguments as the $args param for wp_get_object_terms.
    12431246 * @return array|bool|WP_Error Array of term objects on success, false or WP_Error on failure.
    12441247 */
    1245 function get_the_terms( $post, $taxonomy ) {
     1248function get_the_terms( $post, $taxonomy, $args = array() ) {
    12461249        if ( ! $post = get_post( $post ) )
    12471250                return false;
    12481251
    1249         $terms = get_object_term_cache( $post->ID, $taxonomy );
     1252        $key = "{$post->ID}:$taxonomy:" . md5( serialize( $args ) );
     1253        $group = "{$taxonomy}_relationships";
     1254        $terms = wp_cache_get( $key, $group );
     1255
    12501256        if ( false === $terms ) {
    1251                 $terms = wp_get_object_terms( $post->ID, $taxonomy );
    1252                 wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
     1257                $terms = wp_get_object_terms( $post->ID, $taxonomy, $args );
     1258                wp_cache_add( $key, $terms, $group );
    12531259        }
    12541260
    12551261        /**
     
    12741280 *
    12751281 * @since 2.5.0
    12761282 *
     1283 * @see wp_get_object_terms() for reference on the $args param
     1284 *
    12771285 * @param int $id Post ID.
    12781286 * @param string $taxonomy Taxonomy name.
    12791287 * @param string $before Optional. Before list.
    12801288 * @param string $sep Optional. Separate items using this.
    12811289 * @param string $after Optional. After list.
     1290 * @param array $args Accepts the same arguments as the $args param for wp_get_object_terms.
    12821291 * @return string|bool|WP_Error A list of terms on success, false or WP_Error on failure.
    12831292 */
    1284 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
    1285         $terms = get_the_terms( $id, $taxonomy );
     1293function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '', $args = array() ) {
     1294        $terms = get_the_terms( $id, $taxonomy, $args );
    12861295
    12871296        if ( is_wp_error( $terms ) )
    12881297                return $terms;
     
    13221331 * @param string $before Optional. Before list.
    13231332 * @param string $sep Optional. Separate items using this.
    13241333 * @param string $after Optional. After list.
     1334 * @param array $args Accepts the same arguments as the $args param for wp_get_object_terms.
    13251335 * @return null|bool False on WordPress error. Returns null when displaying.
    13261336 */
    1327 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
    1328         $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
     1337function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '', $args = array() ) {
     1338        $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after, $args );
    13291339
    13301340        if ( is_wp_error( $term_list ) )
    13311341                return false;
  • tests/phpunit/tests/term.php

     
    6969                $this->assertNull( term_exists(rand_str()) );
    7070                $this->assertEquals( 0, term_exists(0) );
    7171                $this->assertEquals( 0, term_exists('') );
    72                 $this->assertEquals( 0, term_exists(NULL) );
     72                $this->assertEquals( 0, term_exists(null) );
    7373        }
    7474
    7575        /**
     
    220220
    221221                // get_the_terms() does prime the cache.
    222222                $terms = get_the_terms( $post_id, $this->taxonomy );
    223                 $cache = wp_cache_get( $post_id, $this->taxonomy . '_relationships');
    224                 $this->assertInternalType( 'array', $cache );
     223                $cache1 = wp_cache_get( $post_id . ':' . $this->taxonomy . ':' . md5( serialize( array() ) ), $this->taxonomy . '_relationships');
     224                $this->assertInternalType( 'array', $cache1 );
    225225
     226                // get_the_terms() does prime the cache differently when the args parameter has a value.
     227                $terms = get_the_terms( $post_id, $this->taxonomy, array( 'order' => 'DESC' ) );
     228                $cache2 = wp_cache_get( $post_id . ':' . $this->taxonomy . ':' . md5( serialize( array( 'order' => 'DESC' ) ) ), $this->taxonomy . '_relationships');
     229                $this->assertInternalType( 'array', $cache2 );
     230
     231                // Verify that the two cache gets returned different results
     232                $this->assertNotEquals( $cache1, $cache2 );
     233
    226234                // Cache should be empty after a set.
    227235                $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
    228236                $this->assertEquals( 2, count($tt_2) );
     
    590598                $this->assertCount( 3, $terms2 );
    591599                $this->assertEquals( array( $cat_id, $cat_id, $cat_id2 ), wp_list_pluck( $terms2, 'term_id' ) );
    592600        }
     601
     602        function test_get_the_terms_by_order() {
     603                // Set up a sortable taxonomy. The only way to add a value to the term_order column is via a sortable tax.
     604                register_taxonomy( 'sortable-taxonomy', 'post', array(
     605                        'sort' => true,
     606                ) );
     607
     608                $post_id = $this->factory->post->create();
     609
     610                // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering
     611                $term1_id = $this->factory->term->create( array( 'taxonomy' => 'sortable-taxonomy', 'name' => 'M Term' ) );
     612                $term2_id = $this->factory->term->create( array( 'taxonomy' => 'sortable-taxonomy', 'name' => 'A Term' ) );
     613                $term3_id = $this->factory->term->create( array( 'taxonomy' => 'sortable-taxonomy', 'name' => 'Z Term' ) );
     614
     615                // Attach the terms to the post
     616                $tt_ids = wp_set_object_terms(
     617                        $post_id,
     618                        array( $term1_id, $term2_id, $term3_id ),
     619                        'sortable-taxonomy'
     620                );
     621
     622                // Verify get by term order
     623                $post_terms_by_term_order = wp_get_post_terms( $post_id, 'sortable-taxonomy', array( 'orderby' => 'term_order' ) );
     624                $this->assertEquals( array( $term1_id, $term2_id, $term3_id ), wp_list_pluck( $post_terms_by_term_order, 'term_id' ) );
     625
     626                // Verify get by term slug
     627                $post_terms_by_term_slug = wp_get_post_terms( $post_id, 'sortable-taxonomy', array( 'orderby' => 'slug' ) );
     628                $this->assertEquals( array( $term2_id, $term1_id, $term3_id ), wp_list_pluck( $post_terms_by_term_slug, 'term_id' ) );
     629
     630                // Verify get by term name
     631                $post_terms_by_term_name = wp_get_post_terms( $post_id, 'sortable-taxonomy', array( 'orderby' => 'name' ) );
     632                $this->assertEquals( array( $term2_id, $term1_id, $term3_id ), wp_list_pluck( $post_terms_by_term_name, 'term_id' ) );
     633        }
     634
     635        /**
     636         * @ticket 9547
     637         */
     638        function test_get_the_terms_with_args() {
     639                $post_id = $this->factory->post->create();
     640
     641                // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering
     642                $term1_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'M Tag', ) );
     643                $term2_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'A Tag', ) );
     644                $term3_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'Z Tag', ) );
     645
     646                // Attach the terms to the post
     647                $tt_ids = wp_set_object_terms(
     648                        $post_id,
     649                        array( $term1_id, $term2_id, $term3_id ),
     650                        'post_tag'
     651                );
     652
     653                // Make sure that the terms added are available when the args parameter is used
     654                $terms = get_the_terms( $post_id, 'post_tag', array( 'orderby' => 'name' ) );
     655                $this->assertEquals( array( $term2_id, $term1_id, $term3_id ), wp_list_pluck( $terms, 'term_id' ) );
     656
     657                /**
     658                 * Verify that using the args parameter produces two different results when the args parameter is different. This
     659                 * suggests that the cache key is working correctly by taking the args into account.
     660                 */
     661                $orderby_asc  = get_the_terms( $post_id, 'post_tag', array( 'order' => 'ASC' ) );
     662                $orderby_desc = get_the_terms( $post_id, 'post_tag', array( 'order' => 'DESC' ) );
     663                $this->assertFalse( in_array( $orderby_asc, $orderby_desc ) );
     664        }
     665
     666        /**
     667         * @ticket 9547
     668         */
     669        function test_get_the_term_list_with_args() {
     670                $post_id = $this->factory->post->create();
     671
     672                // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering
     673                $term1_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'M Tag', ) );
     674                $term2_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'A Tag', ) );
     675                $term3_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'Z Tag', ) );
     676
     677                // Attach the terms to the post
     678                $tt_ids = wp_set_object_terms(
     679                        $post_id,
     680                        array( $term1_id, $term2_id, $term3_id ),
     681                        'post_tag'
     682                );
     683
     684                // Verify that the term list returns its expected result
     685                $links = array();
     686                foreach ( array( $term2_id, $term1_id, $term3_id ) as $term_id ) {
     687                        $term      = get_term( $term_id, 'post_tag' );
     688                        $term_link = get_term_link( $term_id, 'post_tag' );
     689                        $links[]   = '<a href="' . esc_url( $term_link ) . '" rel="tag">' . $term->name . '</a>';
     690                }
     691                $expected = join( '', $links );
     692
     693                $this->assertEquals( $expected, get_the_term_list( $post_id, 'post_tag', '', '', '', array( 'order' => 'ASC', 'orderby' => 'name' ) ) );
     694
     695                /**
     696                 * Verify that using the args parameter produces two different results when the args parameter is different. This
     697                 * suggests that the cache key is working correctly by taking the args into account.
     698                 */
     699                $orderby_asc  = get_the_term_list( $post_id, 'post_tag', '', '', '', array( 'order' => 'ASC' ) );
     700                $orderby_desc = get_the_term_list( $post_id, 'post_tag', '', '', '', array( 'order' => 'DESC' ) );
     701                $this->assertNotEquals( $orderby_asc, $orderby_desc );
     702        }
     703
     704        /**
     705         * @ticket 9547
     706         */
     707        function test_the_terms_with_args() {
     708                $post_id = $this->factory->post->create();
     709
     710                // Create three tags to test ordering. Purposefully creating tags out of order to be able to easily verify ordering
     711                $term1_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'M Tag', ) );
     712                $term2_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'A Tag', ) );
     713                $term3_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'Z Tag', ) );
     714
     715                // Attach the terms to the post
     716                $tt_ids = wp_set_object_terms(
     717                        $post_id,
     718                        array( $term1_id, $term2_id, $term3_id ),
     719                        'post_tag'
     720                );
     721
     722                // Prepare a string that represents the output of the_terms when setting orderby to name and order to ASC
     723                $links = array();
     724                foreach ( array( $term2_id, $term1_id, $term3_id ) as $term_id ) {
     725                        $term      = get_term( $term_id, 'post_tag' );
     726                        $term_link = get_term_link( $term_id, 'post_tag' );
     727                        $links[] = '<a href="' . esc_url( $term_link ) . '" rel="tag">' . $term->name . '</a>';
     728                }
     729                $expected1 = join( ', ', $links );
     730
     731                /**
     732                 * Prepare a string that represents the output of the_terms when setting order to DESC; It is important to test
     733                 * two variants to ensure that the cache keys are working properly.
     734                 */
     735                $links = array();
     736                foreach ( array( $term3_id, $term1_id, $term2_id ) as $term_id ) {
     737                        $term      = get_term( $term_id, 'post_tag' );
     738                        $term_link = get_term_link( $term_id, 'post_tag' );
     739                        $links[] = '<a href="' . esc_url( $term_link ) . '" rel="tag">' . $term->name . '</a>';
     740                }
     741                $expected2 = join( ', ', $links );
     742
     743                /**
     744                 * At the end of the test, the_terms will run twice producing output. The output should be the concatenation of
     745                 * the first and second test string.
     746                 */
     747                $this->expectOutputString( $expected1 . $expected2 );
     748
     749                the_terms( $post_id, 'post_tag', '', ', ', '', array( 'order' => 'ASC', 'orderby' => 'name' ) );
     750                the_terms( $post_id, 'post_tag', '', ', ', '', array( 'order' => 'DESC' ) );
     751        }
    593752}