<!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>[51964] trunk: REST API: Support custom namespaces for taxonomies.</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 { white-space: pre-line; 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/51964">51964</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/51964","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>TimothyBlynJacobs</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-11-01 03:26:06 +0000 (Mon, 01 Nov 2021)</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'>REST API: Support custom namespaces for taxonomies.

While a taxonomy can define a custom route by using the rest_base argument, a namespace of wp/v2 was assumed. This commit introduces support for a rest_namespace argument.

A new rest_get_route_for_taxonomy_items function has been introduced and the rest_get_route_for_term function updated to facilitate getting the correct route for taxonomies.

For maximum compatibility sticking with the default wp/v2 namespace is recommended until the API functions see wider use.

Props spacedmonkey.
Fixes <a href="https://core.trac.wordpress.org/ticket/54267">#54267</a>.
See <a href="https://core.trac.wordpress.org/changeset/51962">[51962]</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswptaxonomyphp">trunk/src/wp-includes/class-wp-taxonomy.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswpresttaxonomiescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswpresttermscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunksrcwpincludestaxonomyphp">trunk/src/wp-includes/taxonomy.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiresttaxonomiescontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiphp">trunk/tests/phpunit/tests/rest-api.php</a></li>
<li><a href="#trunktestsqunitfixtureswpapigeneratedjs">trunk/tests/qunit/fixtures/wp-api-generated.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswptaxonomyphp"></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/class-wp-taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-taxonomy.php       2021-11-01 02:12:09 UTC (rev 51963)
+++ trunk/src/wp-includes/class-wp-taxonomy.php 2021-11-01 03:26:06 UTC (rev 51964)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -200,6 +200,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public $rest_base;
</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">+         * The namespace for this taxonomy's REST API endpoints.
+        *
+        * @since 5.9
+        * @var string|bool $rest_namespace
+        */
+       public $rest_namespace;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * The controller for this taxonomy's REST API endpoints.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * Custom controllers must extend WP_REST_Controller.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -319,6 +327,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'update_count_callback' => '',
</span><span class="cx" style="display: block; padding: 0 10px">                        'show_in_rest'          => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        'rest_base'             => false,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'rest_namespace'        => false,
</ins><span class="cx" style="display: block; padding: 0 10px">                         'rest_controller_class' => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        'default_term'          => null,
</span><span class="cx" style="display: block; padding: 0 10px">                        'sort'                  => null,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -384,6 +393,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $args['show_in_quick_edit'] = $args['show_ui'];
</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">+                // If not set, default rest_namespace to wp/v2 if show_in_rest is true.
+               if ( false === $args['rest_namespace'] && ! empty( $args['show_in_rest'] ) ) {
+                       $args['rest_namespace'] = 'wp/v2';
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $default_caps = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'manage_terms' => 'manage_categories',
</span><span class="cx" style="display: block; padding: 0 10px">                        'edit_terms'   => 'manage_categories',
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp"></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/rest-api/endpoints/class-wp-rest-posts-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php       2021-11-01 02:12:09 UTC (rev 51963)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2021-11-01 03:26:06 UTC (rev 51964)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2060,19 +2060,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $links['https://api.w.org/term'] = array();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $taxonomies as $tax ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $taxonomy_obj = get_taxonomy( $tax );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $taxonomy_route = rest_get_route_for_taxonomy_items( $tax );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                // Skip taxonomies that are not public.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( empty( $taxonomy_obj->show_in_rest ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( empty( $taxonomy_route ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                         continue;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                               $tax_base = ! empty( $taxonomy_obj->rest_base ) ? $taxonomy_obj->rest_base : $tax;
-
</del><span class="cx" style="display: block; padding: 0 10px">                                 $terms_url = add_query_arg(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'post',
</span><span class="cx" style="display: block; padding: 0 10px">                                        $post->ID,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        rest_url( 'wp/v2/' . $tax_base )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 rest_url( $taxonomy_route )
</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">                                $links['https://api.w.org/term'][] = array(
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswpresttaxonomiescontrollerphp"></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/rest-api/endpoints/class-wp-rest-taxonomies-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php  2021-11-01 02:12:09 UTC (rev 51963)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php    2021-11-01 03:26:06 UTC (rev 51964)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -250,6 +250,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $data['rest_base'] = $base;
</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">+                if ( in_array( 'rest_namespace', $fields, true ) ) {
+                       $data['rest_namespace'] = $taxonomy->rest_namespace;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( in_array( 'visibility', $fields, true ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $data['visibility'] = array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'public'             => (bool) $taxonomy->public,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -274,7 +278,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><span class="cx" style="display: block; padding: 0 10px">                                'https://api.w.org/items' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'href' => rest_url( rest_get_route_for_taxonomy_items( $taxonomy->name ) ),
</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">@@ -310,49 +314,49 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'title'      => 'taxonomy',
</span><span class="cx" style="display: block; padding: 0 10px">                        'type'       => 'object',
</span><span class="cx" style="display: block; padding: 0 10px">                        'properties' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'capabilities' => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'capabilities'   => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'description' => __( 'All capabilities used by the taxonomy.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'object',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'edit' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'readonly'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'description'  => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'description'    => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'description' => __( 'A human-readable description of the taxonomy.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'string',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'view', 'edit' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'readonly'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'hierarchical' => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'hierarchical'   => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'description' => __( 'Whether or not the taxonomy should have children.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'boolean',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'view', 'edit' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'readonly'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'labels'       => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'labels'         => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'object',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'edit' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'readonly'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'name'         => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'name'           => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'description' => __( 'The title for the taxonomy.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'string',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'view', 'edit', 'embed' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'readonly'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'slug'         => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'slug'           => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'string',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'view', 'edit', 'embed' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'readonly'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'show_cloud'   => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'show_cloud'     => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'description' => __( 'Whether or not the term cloud should be displayed.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'boolean',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'edit' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'readonly'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'types'        => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'types'          => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'description' => __( 'Types associated with the taxonomy.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'array',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'items'       => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -361,13 +365,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'view', 'edit' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'readonly'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'rest_base'    => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'rest_base'      => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'description' => __( 'REST base route for the taxonomy.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'string',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'view', 'edit', 'embed' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'readonly'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'visibility'   => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'rest_namespace' => array(
+                                       'description' => __( 'REST namespace route for the taxonomy.' ),
+                                       'type'        => 'string',
+                                       'context'     => array( 'view', 'edit', 'embed' ),
+                                       'readonly'    => true,
+                               ),
+                               'visibility'     => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'description' => __( 'The visibility settings for the taxonomy.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'object',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'edit' ),
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswpresttermscontrollerphp"></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/rest-api/endpoints/class-wp-rest-terms-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php       2021-11-01 02:12:09 UTC (rev 51963)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php 2021-11-01 03:26:06 UTC (rev 51964)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -57,9 +57,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function __construct( $taxonomy ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $this->taxonomy  = $taxonomy;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->namespace = 'wp/v2';
</del><span class="cx" style="display: block; padding: 0 10px">                 $tax_obj         = get_taxonomy( $taxonomy );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->rest_base = ! empty( $tax_obj->rest_base ) ? $tax_obj->rest_base : $tax_obj->name;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->namespace = ! empty( $tax_obj->rest_namespace ) ? $tax_obj->rest_namespace : 'wp/v2';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->meta = new WP_REST_Term_Meta_Fields( $taxonomy );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiphp"></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/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api.php        2021-11-01 02:12:09 UTC (rev 51963)
+++ trunk/src/wp-includes/rest-api.php  2021-11-01 03:26:06 UTC (rev 51964)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3115,25 +3115,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return '';
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $taxonomy = get_taxonomy( $term->taxonomy );
-       if ( ! $taxonomy ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $taxonomy_route = rest_get_route_for_taxonomy_items( $term->taxonomy );
+       if ( ! $taxonomy_route ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return '';
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $controller = $taxonomy->get_rest_controller();
-       if ( ! $controller ) {
-               return '';
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $route = sprintf( '%s/%d', $taxonomy_route, $term->term_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $route = '';
-
-       // The only controller that works is the Terms controller.
-       if ( $controller instanceof WP_REST_Terms_Controller ) {
-               $namespace = 'wp/v2';
-               $rest_base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
-               $route     = sprintf( '/%s/%s/%d', $namespace, $rest_base, $term->term_id );
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Filters the REST API route for a term.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3146,6 +3134,39 @@
</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">+ * Gets the REST API route for a taxonomy.
+ *
+ * @since 5.9.0
+ *
+ * @param string $taxonomy Name of taxonomy.
+ * @return string The route path with a leading slash for the given taxonomy.
+ */
+function rest_get_route_for_taxonomy_items( $taxonomy ) {
+       $taxonomy = get_taxonomy( $taxonomy );
+       if ( ! $taxonomy ) {
+               return '';
+       }
+
+       if ( ! $taxonomy->show_in_rest ) {
+               return '';
+       }
+
+       $namespace = ! empty( $taxonomy->rest_namespace ) ? $taxonomy->rest_namespace : 'wp/v2';
+       $rest_base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
+       $route     = sprintf( '/%s/%s', $namespace, $rest_base );
+
+       /**
+        * Filters the REST API route for a taxonomy.
+        *
+        * @since 5.9.0
+        *
+        * @param string      $route    The route path.
+        * @param WP_Taxonomy $taxonomy The taxonomy object.
+        */
+       return apply_filters( 'rest_route_for_taxonomy_items', $route, $taxonomy );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Gets the REST route for the currently queried object.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.5.0
</span></span></pre></div>
<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        2021-11-01 02:12:09 UTC (rev 51963)
+++ trunk/src/wp-includes/taxonomy.php  2021-11-01 03:26:06 UTC (rev 51964)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -387,6 +387,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type bool          $show_in_rest          Whether to include the taxonomy in the REST API. Set this to true
</span><span class="cx" style="display: block; padding: 0 10px">  *                                                for the taxonomy to be available in the block editor.
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type string        $rest_base             To change the base url of REST API route. Default is $taxonomy.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *     @type string        $rest_namespace        To change the namespace URL of REST API route. Default is wp/v2.
</ins><span class="cx" style="display: block; padding: 0 10px">  *     @type string        $rest_controller_class REST API Controller class name. Default is 'WP_REST_Terms_Controller'.
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type bool          $show_tagcloud         Whether to list the taxonomy in the Tag Cloud Widget controls. If not set,
</span><span class="cx" style="display: block; padding: 0 10px">  *                                                the default is inherited from `$show_ui` (default true).
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiresttaxonomiescontrollerphp"></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/rest-api/rest-taxonomies-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php 2021-11-01 02:12:09 UTC (rev 51963)
+++ trunk/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php   2021-11-01 03:26:06 UTC (rev 51964)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -219,7 +219,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $response   = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px">                $data       = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px">                $properties = $data['schema']['properties'];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertCount( 10, $properties );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertCount( 11, $properties );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertArrayHasKey( 'capabilities', $properties );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertArrayHasKey( 'description', $properties );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertArrayHasKey( 'hierarchical', $properties );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -230,6 +230,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertArrayHasKey( 'types', $properties );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertArrayHasKey( 'visibility', $properties );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertArrayHasKey( 'rest_base', $properties );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->assertArrayHasKey( 'rest_namespace', $properties );
</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">@@ -252,6 +253,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( $tax_obj->description, $data['description'] );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( $tax_obj->hierarchical, $data['hierarchical'] );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( $tax_obj->rest_base, $data['rest_base'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->assertSame( $tax_obj->rest_namespace, $data['rest_namespace'] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertSame( rest_url( 'wp/v2/taxonomies' ), $links['collection'][0]['href'] );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertArrayHasKey( 'https://api.w.org/items', $links );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'edit' === $context ) {
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiphp"></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/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api.php    2021-11-01 02:12:09 UTC (rev 51963)
+++ trunk/tests/phpunit/tests/rest-api.php      2021-11-01 03:26:06 UTC (rev 51964)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1963,6 +1963,50 @@
</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 54267
+        */
+       public function test_rest_get_route_for_taxonomy_custom_namespace() {
+               register_taxonomy(
+                       'ct',
+                       'post',
+                       array(
+                               'show_in_rest'   => true,
+                               'rest_base'      => 'ct',
+                               'rest_namespace' => 'wordpress/v1',
+                       )
+               );
+               $term = self::factory()->term->create_and_get( array( 'taxonomy' => 'ct' ) );
+
+               $this->assertSame( '/wordpress/v1/ct/' . $term->term_id, rest_get_route_for_term( $term ) );
+               unregister_taxonomy( 'ct' );
+       }
+
+       /**
+        * @ticket 54267
+        */
+       public function test_rest_get_route_for_taxonomy_items() {
+               $this->assertSame( '/wp/v2/categories', rest_get_route_for_taxonomy_items( 'category' ) );
+       }
+
+       /**
+        * @ticket 54267
+        */
+       public function test_rest_get_route_for_taxonomy_items_custom_namespace() {
+               register_taxonomy(
+                       'ct',
+                       'post',
+                       array(
+                               'show_in_rest'   => true,
+                               'rest_base'      => 'ct',
+                               'rest_namespace' => 'wordpress/v1',
+                       )
+               );
+
+               $this->assertSame( '/wordpress/v1/ct', rest_get_route_for_taxonomy_items( 'ct' ) );
+               unregister_post_type( 'ct' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 50300
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @dataProvider _dp_rest_is_object
</span></span></pre></div>
<a id="trunktestsqunitfixtureswpapigeneratedjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/fixtures/wp-api-generated.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/fixtures/wp-api-generated.js    2021-11-01 02:12:09 UTC (rev 51963)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js      2021-11-01 03:26:06 UTC (rev 51964)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -8589,6 +8589,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         ],
</span><span class="cx" style="display: block; padding: 0 10px">         "hierarchical": true,
</span><span class="cx" style="display: block; padding: 0 10px">         "rest_base": "categories",
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "rest_namespace": "wp/v2",
</ins><span class="cx" style="display: block; padding: 0 10px">         "_links": {
</span><span class="cx" style="display: block; padding: 0 10px">             "collection": [
</span><span class="cx" style="display: block; padding: 0 10px">                 {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -8618,6 +8619,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         ],
</span><span class="cx" style="display: block; padding: 0 10px">         "hierarchical": false,
</span><span class="cx" style="display: block; padding: 0 10px">         "rest_base": "tags",
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "rest_namespace": "wp/v2",
</ins><span class="cx" style="display: block; padding: 0 10px">         "_links": {
</span><span class="cx" style="display: block; padding: 0 10px">             "collection": [
</span><span class="cx" style="display: block; padding: 0 10px">                 {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -8648,7 +8650,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         "post"
</span><span class="cx" style="display: block; padding: 0 10px">     ],
</span><span class="cx" style="display: block; padding: 0 10px">     "hierarchical": true,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-    "rest_base": "categories"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+    "rest_base": "categories",
+    "rest_namespace": "wp/v2"
</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"> mockedApiResponse.CategoriesCollection = [
</span></span></pre>
</div>
</div>

</body>
</html>