Make WordPress Core

Ticket #40436: 40436.1.diff

File 40436.1.diff, 4.9 KB (added by soulseekah, 7 years ago)
  • src/wp-includes/class-wp-taxonomy.php

    diff --git src/wp-includes/class-wp-taxonomy.php src/wp-includes/class-wp-taxonomy.php
    index 94353d8..1836f2a 100644
    final class WP_Taxonomy { 
    152152        public $cap;
    153153
    154154        /**
     155         * Whether this taxonomy should remember the order in which terms are added to objects
     156         *
     157         * @var bool
     158         */
     159        public $sort;
     160
     161        /**
    155162         * Rewrites information for this taxonomy.
    156163         *
    157164         * @since 4.7.0
    final class WP_Taxonomy { 
    267274                        'meta_box_cb'           => null,
    268275                        'meta_box_sanitize_cb'  => null,
    269276                        'capabilities'          => array(),
     277                        'sort'                  => false,
    270278                        'rewrite'               => true,
    271279                        'query_var'             => $this->name,
    272280                        'update_count_callback' => '',
  • src/wp-includes/class-wp-term-query.php

    diff --git src/wp-includes/class-wp-term-query.php src/wp-includes/class-wp-term-query.php
    index b7c3b42..75b5566 100644
    class WP_Term_Query { 
    232232                $taxonomies = isset( $query['taxonomy'] ) ? (array) $query['taxonomy'] : null;
    233233
    234234                /**
     235                 * If all the requested taxonomies are sortable by term_order
     236                 * via the WP_Taxonomy::$sort flag we can set the default sort order
     237                 * to term_order.
     238                 */
     239                if ( is_array( $taxonomies ) ) {
     240                        $ts = array_map( 'get_taxonomy', $taxonomies );
     241                        if ( count( array_filter( wp_list_pluck( $ts, 'sort' ) ) ) == count( $taxonomies ) ) {
     242                                if ( ( $t = get_taxonomy( $taxonomies[0] ) ) && $t->sort ) {
     243                                        $this->query_var_defaults['orderby'] = 'term_order';
     244                                }
     245                        }
     246                }
     247
     248                /**
    235249                 * Filters the terms query default arguments.
    236250                 *
    237251                 * Use {@see 'get_terms_args'} to filter the passed arguments.
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index df828b0..7301c8e 100644
    function is_taxonomy_hierarchical( $taxonomy ) { 
    377377 *         @type string $delete_terms Default 'manage_categories'.
    378378 *         @type string $assign_terms Default 'edit_posts'.
    379379 *     }
     380 *     @type bool          $sort                  Whether this taxonomy should remember the order in which terms are
     381 *                                                added to objects. Default false.
    380382 *     @type bool|array    $rewrite {
    381383 *         Triggers the handling of rewrites for this taxonomy. Default true, using $taxonomy as slug. To prevent
    382384 *         rewrite, set to false. To specify rewrite rules, an array can be passed with any of these keys:
    function update_object_term_cache( $object_ids, $object_type ) { 
    33123314
    33133315        $object_ids = array_map( 'intval', $object_ids );
    33143316
     3317        $sorted_taxonomies = array();
    33153318        $taxonomies = get_object_taxonomies( $object_type );
    33163319
     3320        /**
     3321         * Split the sorted taxonomies out. They will
     3322         * be ordered by term_order.
     3323         */
     3324        foreach ( $taxonomies as $index => $taxonomy ) {
     3325                if ( ( $t = get_taxonomy( $taxonomy ) ) && $t->sort ) {
     3326                        $sorted_taxonomies[] = $taxonomy;
     3327                        unset( $taxonomies[ $index ] );
     3328                }
     3329        }
     3330
    33173331        $ids = array();
    33183332        foreach ( (array) $object_ids as $id ) {
    33193333                foreach ( $taxonomies as $taxonomy ) {
    function update_object_term_cache( $object_ids, $object_type ) { 
    33363350                )
    33373351        );
    33383352
     3353        $sorted_terms = wp_get_object_terms(
     3354                $ids, $sorted_taxonomies, array(
     3355                        'fields'                 => 'all_with_object_id',
     3356                        'orderby'                => 'term_order',
     3357                        'update_term_meta_cache' => false,
     3358                )
     3359        );
     3360
     3361        $terms = array_merge( $terms, $sorted_terms );
     3362
    33393363        $object_terms = array();
    33403364        foreach ( (array) $terms as $term ) {
    33413365                $object_terms[ $term->object_id ][ $term->taxonomy ][] = $term->term_id;
  • tests/phpunit/tests/term/query.php

    diff --git tests/phpunit/tests/term/query.php tests/phpunit/tests/term/query.php
    index b517417..440ba8f 100644
    class Tests_Term_Query extends WP_UnitTestCase { 
    614614                );
    615615                $this->assertSame( array(), $q->terms );
    616616        }
     617
     618        /**
     619         * @ticket 40436
     620         */
     621        public function test_taxonomy_sort_flag_orderby_term_order() {
     622                $post_id = self::factory()->post->create();
     623                register_taxonomy( 'wptests_sort_tax', array( 'post' ), array( 'sort' => true, 'hierarchical' => false ) );
     624
     625                $t = get_taxonomy( 'wptests_sort_tax' );
     626                $this->assertTrue( $t->sort );
     627
     628                $terms = array( 'three', 'and', 'four', 'five' );
     629                wp_set_object_terms( $post_id, $terms, 'wptests_sort_tax' );
     630
     631                // Make sure global term cache provides the correct order
     632                update_object_term_cache( $post_id, 'post' );
     633
     634                $q = new WP_Term_Query(
     635                        array(
     636                                'taxonomy'  => 'wptests_sort_tax',
     637                                'object_ids' => $post_id,
     638                        )
     639                );
     640                $this->assertSame( $terms, wp_list_pluck( $q->terms, 'slug' ) );
     641
     642                // Test metabox taxonomy (admin advanced edit)
     643                $terms_to_edit = get_terms_to_edit( $post_id, 'wptests_sort_tax' );
     644                $this->assertEquals( implode( ',', $terms ), $terms_to_edit );
     645        }
    617646}