<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[36614] trunk: Allow `get_terms()` to fetch terms regardless of taxonomy.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/36614">36614</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/36614","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-02-22 22:16:37 +0000 (Mon, 22 Feb 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Allow `get_terms()` to fetch terms regardless of taxonomy.
`get_terms()` has historically required that a taxonomy be specified when
querying terms. This requirement is related to the fact that terms could
formerly be shared between taxonomies, making `$taxonomies` critical for
disambiguation. Since terms can no longer be shared as of 4.4, it'
s desirable to be able to query for terms regardless of what taxonomy they're in.
Because it's now optional to pass taxonomies, it's no longer necessary to have
`$taxonomies` as the first (and required) parameter for `get_terms()`. The new
function signature is `get_terms( $args )`, where 'taxonomy' can (optionally) be
passed as part of the `$args` array. This syntax is more consistent with
functions like `get_users()` and `get_posts()`.
We've maintained backward compatibility by always giving precedence to the old
argument format. If a second parameter is detected, or if it's detected that
the first parameter is a list of taxonomy names rather than an `$args` array,
`get_terms()` will parse the function arguments in the legacy fashion.
Props flixos90, swissspidy, DrewAPicture, boonebgorges.
Fixes <a href="https://core.trac.wordpress.org/ticket/35495">#35495</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludestaxonomyphp">trunk/src/wp-includes/taxonomy.php</a></li>
<li><a href="#trunktestsphpunitteststermgetTermsphp">trunk/tests/phpunit/tests/term/getTerms.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludestaxonomyphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy.php 2016-02-22 19:54:39 UTC (rev 36613)
+++ trunk/src/wp-includes/taxonomy.php 2016-02-22 22:16:37 UTC (rev 36614)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1072,20 +1072,37 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * The {@see 'get_terms_orderby'} filter passes the `ORDER BY` clause for the query
</span><span class="cx" style="display: block; padding: 0 10px"> * along with the $args array.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Prior to 4.5.0, the first parameter of `get_terms()` was a taxonomy or list of taxonomies:
+ *
+ * $terms = get_terms( 'post_tag', array(
+ * 'hide_empty' => false,
+ * ) );
+ *
+ * Since 4.5.0, taxonomies should be passed via the 'taxonomy' argument in the `$args` array:
+ *
+ * $terms = get_terms( array(
+ * 'taxonomy' => 'post_tag',
+ * 'hide_empty' => false,
+ * ) );
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 2.3.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.2.0 Introduced 'name' and 'childless' parameters.
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.4.0 Introduced the ability to pass 'term_id' as an alias of 'id' for the `orderby` parameter.
</span><span class="cx" style="display: block; padding: 0 10px"> * Introduced the 'meta_query' and 'update_term_meta_cache' parameters. Converted to return
</span><span class="cx" style="display: block; padding: 0 10px"> * a list of WP_Term objects.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 4.5.0 Introduced 'meta_key' and 'meta_value' parameters. Introduced the ability to order results by metadata.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.5.0 Changed the function signature so that the `$args` array can be provided as the first parameter.
+ * Introduced 'meta_key' and 'meta_value' parameters. Introduced the ability to order results by metadata.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @internal The `$deprecated` parameter is parsed for backward compatibility only.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @global wpdb $wpdb WordPress database abstraction object.
</span><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_filter
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string|array $taxonomies Taxonomy name or list of Taxonomy names.
</del><span class="cx" style="display: block; padding: 0 10px"> * @param array|string $args {
</span><span class="cx" style="display: block; padding: 0 10px"> * Optional. Array or string of arguments to get terms.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type string|array $taxonomy Taxonomy name, or array of taxonomies, to which results should
+ * be limited.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @type string $orderby Field(s) to order terms by. Accepts term fields ('name', 'slug',
</span><span class="cx" style="display: block; padding: 0 10px"> * 'term_group', 'term_id', 'id', 'description'), 'count' for term
</span><span class="cx" style="display: block; padding: 0 10px"> * taxonomy count, 'include' to match the 'order' of the $include param,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1144,24 +1161,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @type string $meta_value Limit terms to those matching a specific metadata value. Usually used
</span><span class="cx" style="display: block; padding: 0 10px"> * in conjunction with `$meta_key`.
</span><span class="cx" style="display: block; padding: 0 10px"> * }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $deprecated Argument array, when using the legacy function parameter format. If present, this
+ * parameter will be interpreted as `$args`, and the first function parameter will
+ * be parsed as a taxonomy or array of taxonomies.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies
</span><span class="cx" style="display: block; padding: 0 10px"> * do not exist.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function get_terms( $taxonomies, $args = '' ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function get_terms( $args = array(), $deprecated = '' ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> global $wpdb;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $empty_array = array();
</del><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! is_array( $taxonomies ) ) {
- $taxonomies = array( $taxonomies );
- }
-
- foreach ( $taxonomies as $taxonomy ) {
- if ( ! taxonomy_exists($taxonomy) ) {
- return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
- }
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> $defaults = array(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'taxonomy' => null,
</ins><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => 'name',
</span><span class="cx" style="display: block; padding: 0 10px"> 'order' => 'ASC',
</span><span class="cx" style="display: block; padding: 0 10px"> 'hide_empty' => true,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1187,6 +1197,35 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'meta_query' => ''
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * Legacy argument format ($taxonomy, $args) takes precedence.
+ *
+ * We detect legacy argument format by checking if
+ * (a) a second non-empty parameter is passed, or
+ * (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies)
+ */
+ $key_intersect = array_intersect_key( $defaults, (array) $args );
+ $do_legacy_args = $deprecated || empty( $key_intersect );
+
+ $taxonomies = null;
+ if ( $do_legacy_args ) {
+ $taxonomies = (array) $args;
+ $args = $deprecated;
+ } elseif ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) {
+ $taxonomies = (array) $args['taxonomy'];
+ unset( $args['taxonomy'] );
+ }
+
+ $empty_array = array();
+
+ if ( $taxonomies ) {
+ foreach ( $taxonomies as $taxonomy ) {
+ if ( ! taxonomy_exists($taxonomy) ) {
+ return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
+ }
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filter the terms query default arguments.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1204,9 +1243,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Save queries by not crawling the tree in the case of multiple taxes or a flat tax.
</span><span class="cx" style="display: block; padding: 0 10px"> $has_hierarchical_tax = false;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $taxonomies as $_tax ) {
- if ( is_taxonomy_hierarchical( $_tax ) ) {
- $has_hierarchical_tax = true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $taxonomies ) {
+ foreach ( $taxonomies as $_tax ) {
+ if ( is_taxonomy_hierarchical( $_tax ) ) {
+ $has_hierarchical_tax = true;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1311,7 +1352,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $where_conditions = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $where_conditions[] = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $taxonomies ) {
+ $where_conditions[] = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $exclude = $args['exclude'];
</span><span class="cx" style="display: block; padding: 0 10px"> $exclude_tree = $args['exclude_tree'];
</span></span></pre></div>
<a id="trunktestsphpunitteststermgetTermsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/term/getTerms.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/getTerms.php 2016-02-22 19:54:39 UTC (rev 36613)
+++ trunk/tests/phpunit/tests/term/getTerms.php 2016-02-22 22:16:37 UTC (rev 36614)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,6 +12,43 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 35495
+ */
+ public function test_should_accept_an_args_array_containing_taxonomy_for_first_parameter() {
+ register_taxonomy( 'wptests_tax', 'post' );
+ $term = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
+
+ $found = get_terms( array(
+ 'taxonomy' => 'wptests_tax',
+ 'hide_empty' => false,
+ 'fields' => 'ids',
+ 'update_term_meta_cache' => false,
+ ) );
+
+ $this->assertEqualSets( array( $term ), $found );
+ }
+
+ /**
+ * @ticket 35495
+ */
+ public function test_excluding_taxonomy_arg_should_return_terms_from_all_taxonomies() {
+ register_taxonomy( 'wptests_tax1', 'post' );
+ register_taxonomy( 'wptests_tax2', 'post' );
+ $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
+ $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
+
+ $found = get_terms( array(
+ 'hide_empty' => false,
+ 'fields' => 'ids',
+ 'update_term_meta_cache' => false,
+ ) );
+
+ // There may be other terms lying around, so don't do an exact match.
+ $this->assertContains( $t1, $found );
+ $this->assertContains( $t2, $found );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 23326
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_get_terms_cache() {
</span></span></pre>
</div>
</div>
</body>
</html>