Restore support for taxonomy 'args' override when querying object terms.

[7520] introduced an undocumented feature whereby developers could
register a custom taxonomy with an 'args' parameter, consisting of
an array of config params that, when present, override corresponding
params in the `$args` array passed to `wp_get_object_terms()` when
using that function to query for terms in the specified taxonomy.

The `wp_get_object_terms()` refactor in [38667] failed to respect
this secret covenant, and the current changeset atones for the
transgression.

Ports [40513] to the 4.7 branch.

Props danielbachhuber.
Fixes #40496.


git-svn-id: https://develop.svn.wordpress.org/branches/4.7@40514 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2017-04-21 19:18:00 +00:00
parent 2c0ecb234a
commit a82fe2b598
2 changed files with 67 additions and 1 deletions

View File

@ -1873,10 +1873,30 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
$args = wp_parse_args( $args );
/*
* When one or more queried taxonomies is registered with an 'args' array,
* those params override the `$args` passed to this function.
*/
$terms = array();
if ( count( $taxonomies ) > 1 ) {
foreach ( $taxonomies as $index => $taxonomy ) {
$t = get_taxonomy( $taxonomy );
if ( isset( $t->args ) && is_array( $t->args ) && $args != array_merge( $args, $t->args ) ) {
unset( $taxonomies[ $index ] );
$terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) );
}
}
} else {
$t = get_taxonomy( $taxonomies[0] );
if ( isset( $t->args ) && is_array( $t->args ) ) {
$args = array_merge( $args, $t->args );
}
}
$args['taxonomy'] = $taxonomies;
$args['object_ids'] = $object_ids;
$terms = get_terms( $args );
$terms = array_merge( $terms, get_terms( $args ) );
/**
* Filters the terms for a given object or objects.

View File

@ -381,4 +381,50 @@ class Tests_Term_Query extends WP_UnitTestCase {
$count = $query->get_terms();
$this->assertEquals( 1, $count );
}
/**
* @ticket 40496
*/
public function test_get_the_terms_should_respect_taxonomy_orderby() {
register_taxonomy( 'wptests_tax', 'post', array(
'sort' => true,
'args' => array(
'orderby' => 'term_order',
),
) );
$term_ids = self::factory()->term->create_many( 2, array(
'taxonomy' => 'wptests_tax',
) );
$post_id = self::factory()->post->create();
wp_set_object_terms( $post_id, array( $term_ids[0], $term_ids[1] ), 'wptests_tax' );
$terms = get_the_terms( $post_id, 'wptests_tax' );
$this->assertEquals( array( $term_ids[0], $term_ids[1] ), wp_list_pluck( $terms, 'term_id' ) );
// Flip the order
wp_set_object_terms( $post_id, array( $term_ids[1], $term_ids[0] ), 'wptests_tax' );
$terms = get_the_terms( $post_id, 'wptests_tax' );
$this->assertEquals( array( $term_ids[1], $term_ids[0] ), wp_list_pluck( $terms, 'term_id' ) );
}
/**
* @ticket 40496
*/
public function test_wp_get_object_terms_should_respect_taxonomy_orderby() {
register_taxonomy( 'wptests_tax', 'post', array(
'sort' => true,
'args' => array(
'orderby' => 'term_order',
),
) );
$term_ids = self::factory()->term->create_many( 2, array(
'taxonomy' => 'wptests_tax',
) );
$post_id = self::factory()->post->create();
wp_set_object_terms( $post_id, array( $term_ids[0], $term_ids[1] ), 'wptests_tax' );
$terms = wp_get_object_terms( $post_id, array( 'category', 'wptests_tax' ) );
$this->assertEquals( array( $term_ids[0], $term_ids[1], 1 ), wp_list_pluck( $terms, 'term_id' ) );
// Flip the order
wp_set_object_terms( $post_id, array( $term_ids[1], $term_ids[0] ), 'wptests_tax' );
$terms = wp_get_object_terms( $post_id, array( 'category', 'wptests_tax' ) );
$this->assertEquals( array( $term_ids[1], $term_ids[0], 1 ), wp_list_pluck( $terms, 'term_id' ) );
}
}