WordPress.org

Make WordPress Core

Changeset 48882


Ignore:
Timestamp:
08/27/2020 03:06:22 AM (16 months ago)
Author:
SergeyBiryukov
Message:

Taxonomy: Make sure wp_terms_checklist() and Walker_Category_Checklist::start_el() properly handle an array of strings as selected_cats or popular_cats values.

Even with these values documented as an array of integers, they can technically also accept an array of strings, e.g. as form results.

Add a unit test.

Props brianhogg, TimothyBlynJacobs, SergeyBiryukov.
Merges [48880] to the 5.5 branch.
Fixes #51137.

Location:
branches/5.5
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/5.5

  • branches/5.5/src/wp-admin/includes/class-walker-category-checklist.php

    r47557 r48882  
    8282        }
    8383
    84         $args['popular_cats'] = empty( $args['popular_cats'] ) ? array() : $args['popular_cats'];
     84        $args['popular_cats'] = ! empty( $args['popular_cats'] ) ? array_map( 'intval', $args['popular_cats'] ) : array();
    8585
    8686        $class = in_array( $category->term_id, $args['popular_cats'], true ) ? ' class="popular-category"' : '';
    8787
    88         $args['selected_cats'] = empty( $args['selected_cats'] ) ? array() : $args['selected_cats'];
     88        $args['selected_cats'] = ! empty( $args['selected_cats'] ) ? array_map( 'intval', $args['selected_cats'] ) : array();
    8989
    9090        if ( ! empty( $args['list_only'] ) ) {
  • branches/5.5/src/wp-admin/includes/template.php

    r48620 r48882  
    121121
    122122    if ( is_array( $parsed_args['selected_cats'] ) ) {
    123         $args['selected_cats'] = $parsed_args['selected_cats'];
     123        $args['selected_cats'] = array_map( 'intval', $parsed_args['selected_cats'] );
    124124    } elseif ( $post_id ) {
    125125        $args['selected_cats'] = wp_get_object_terms( $post_id, $taxonomy, array_merge( $args, array( 'fields' => 'ids' ) ) );
     
    129129
    130130    if ( is_array( $parsed_args['popular_cats'] ) ) {
    131         $args['popular_cats'] = $parsed_args['popular_cats'];
     131        $args['popular_cats'] = array_map( 'intval', $parsed_args['popular_cats'] );
    132132    } else {
    133133        $args['popular_cats'] = get_terms(
  • branches/5.5/tests/phpunit/tests/admin/includesTemplate.php

    r47777 r48882  
    44 */
    55class Tests_Admin_includesTemplate extends WP_UnitTestCase {
     6
    67    function test_equal() {
    78        $this->assertEquals( ' selected=\'selected\'', selected( 'foo', 'foo', false ) );
     
    4546        $this->assertEquals( '', selected( 0, false, false ) );
    4647        $this->assertEquals( '', checked( 0, false, false ) );
     48    }
     49
     50    /**
     51     * @ticket 51147
     52     * @dataProvider data_wp_terms_checklist_with_selected_cats
     53     */
     54    public function test_wp_terms_checklist_with_selected_cats( $term_id ) {
     55        $output = wp_terms_checklist(
     56            0,
     57            array(
     58                'selected_cats' => array( $term_id ),
     59                'echo'          => false,
     60            )
     61        );
     62
     63        $this->assertContains( "checked='checked'", $output );
     64    }
     65
     66    /**
     67     * @ticket 51147
     68     * @dataProvider data_wp_terms_checklist_with_selected_cats
     69     */
     70    public function test_wp_terms_checklist_with_popular_cats( $term_id ) {
     71        $output = wp_terms_checklist(
     72            0,
     73            array(
     74                'popular_cats' => array( $term_id ),
     75                'echo'         => false,
     76            )
     77        );
     78
     79        $this->assertContains( 'class="popular-category"', $output );
     80    }
     81
     82    public function data_wp_terms_checklist_with_selected_cats() {
     83        return array(
     84            array( '1' ),
     85            array( 1 ),
     86        );
    4787    }
    4888
Note: See TracChangeset for help on using the changeset viewer.