<!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>[51962] trunk: REST API: Support custom namespaces for custom post types.</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/51962">51962</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/51962","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-10-31 23:15:10 +0000 (Sun, 31 Oct 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 custom post types.
While a custom post type 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_post_type_items` function has been introduced and the `rest_get_route_for_post` function updated to facilitate getting the correct route for custom post types.
While the WordPress Core Block Editor bootstrap code has been updated to use these API functions, for maximum compatibility sticking with the default `wp/v2` namespace is recommended until the API functions see wider use.
Props spacedmonkey, swissspidy.
Fixes <a href="https://core.trac.wordpress.org/ticket/53656">#53656</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadmineditformblocksphp">trunk/src/wp-admin/edit-form-blocks.php</a></li>
<li><a href="#trunksrcwpadminwidgetsformblocksphp">trunk/src/wp-admin/widgets-form-blocks.php</a></li>
<li><a href="#trunksrcwpincludesclasswpposttypephp">trunk/src/wp-includes/class-wp-post-type.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunksrcwpincludesrestapiclasswprestserverphp">trunk/src/wp-includes/rest-api/class-wp-rest-server.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestautosavescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestpoststatusescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestposttypescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.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="#trunksrcwpincludesrestapiendpointsclasswprestrevisionscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswpresttemplatescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-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="#trunktestsphpunittestsrestapirestposttypescontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-post-types-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="trunksrcwpadmineditformblocksphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/edit-form-blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/edit-form-blocks.php 2021-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-admin/edit-form-blocks.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -49,7 +49,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> wp_enqueue_script( 'heartbeat' );
</span><span class="cx" style="display: block; padding: 0 10px"> wp_enqueue_script( 'wp-edit-post' );
</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 = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+$rest_path = rest_get_route_for_post( $post );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Preload common data.
</span><span class="cx" style="display: block; padding: 0 10px"> $preload_paths = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -57,12 +57,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/types?context=edit',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/taxonomies?per_page=-1&context=edit',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/themes?status=active',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ add_query_arg( 'context', 'edit', $rest_path ),
</ins><span class="cx" style="display: block; padding: 0 10px"> sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
</span><span class="cx" style="display: block; padding: 0 10px"> sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( '/wp/v2/media', 'OPTIONS' ),
- array( '/wp/v2/blocks', 'OPTIONS' ),
- sprintf( '/wp/v2/%s/%d/autosaves?context=edit', $rest_base, $post->ID ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
+ array( rest_get_route_for_post_type_items( 'wp_block' ), 'OPTIONS' ),
+ sprintf( '%s/autosaves?context=edit', $rest_path ),
</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"> block_editor_rest_api_preload( $preload_paths, $block_editor_context );
</span></span></pre></div>
<a id="trunksrcwpadminwidgetsformblocksphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/widgets-form-blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/widgets-form-blocks.php 2021-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-admin/widgets-form-blocks.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -18,7 +18,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $block_editor_context = new WP_Block_Editor_Context();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $preload_paths = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( '/wp/v2/media', 'OPTIONS' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/sidebars?context=edit&per_page=-1',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/widgets?context=edit&per_page=-1&_embed=about',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpposttypephp"></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-post-type.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-post-type.php 2021-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-includes/class-wp-post-type.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -359,6 +359,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 post type'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 post type'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">@@ -452,6 +460,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'delete_with_user' => null,
</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"> 'template' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> 'template_lock' => false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -473,6 +482,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $args['show_ui'] = $args['public'];
</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"> // If not set, default to the setting for 'show_ui'.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $args['show_in_menu'] = $args['show_ui'];
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></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/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php 2021-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-includes/post.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1418,6 +1418,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @type bool $show_in_rest Whether to include the post type in the REST API. Set this to true
</span><span class="cx" style="display: block; padding: 0 10px"> * for the post type 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 $post_type.
</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_Posts_Controller'.
</span><span class="cx" style="display: block; padding: 0 10px"> * @type int $menu_position The position in the menu order the post type should appear. To work,
</span><span class="cx" style="display: block; padding: 0 10px"> * $show_in_menu must be true. Default null (at the bottom).
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiclasswprestserverphp"></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/class-wp-rest-server.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/class-wp-rest-server.php 2021-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-includes/rest-api/class-wp-rest-server.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1288,7 +1288,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $site_logo_id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $response->add_link(
</span><span class="cx" style="display: block; padding: 0 10px"> 'https://api.w.org/featuredmedia',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- rest_url( 'wp/v2/media/' . $site_logo_id ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ rest_url( rest_get_route_for_post( $site_logo_id ) ),
</ins><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'embeddable' => true,
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestautosavescontrollerphp"></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-autosaves-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-autosaves-controller.php 2021-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -67,8 +67,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->parent_controller = $parent_controller;
</span><span class="cx" style="display: block; padding: 0 10px"> $this->revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type );
</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"> $this->rest_base = 'autosaves';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestpoststatusescontrollerphp"></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-post-statuses-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-post-statuses-controller.php 2021-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -263,10 +263,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_ensure_response( $data );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $rest_url = rest_url( rest_get_route_for_post_type_items( 'post' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( 'publish' === $status->name ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $response->add_link( 'archives', rest_url( 'wp/v2/posts' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $response->add_link( 'archives', $rest_url );
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $response->add_link( 'archives', add_query_arg( 'status', $status->name, rest_url( 'wp/v2/posts' ) ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $response->add_link( 'archives', add_query_arg( 'status', $status->name, $rest_url ) );
</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></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestposttypescontrollerphp"></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-post-types-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-post-types-controller.php 2021-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -180,6 +180,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $taxonomies = wp_list_pluck( $taxonomies, 'name' );
</span><span class="cx" style="display: block; padding: 0 10px"> $base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $namespace = ! empty( $post_type->rest_namespace ) ? $post_type->rest_namespace : 'wp/v2';
</ins><span class="cx" style="display: block; padding: 0 10px"> $supports = get_all_post_type_supports( $post_type->name );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $fields = $this->get_fields_for_response( $request );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -232,6 +233,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'] = $namespace;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $this->add_additional_fields_to_object( $data, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $this->filter_response_by_context( $data, $context );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -245,7 +250,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_post_type_items( $post_type->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">@@ -269,7 +274,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.8.0 The `supports` property was added.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 5.9.0 The `visibility` property was added.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.9.0 The `visibility` and `rest_namespace` properties were added.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array Item schema data.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -283,55 +288,55 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'title' => 'type',
</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 post type.' ),
</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 post type.' ),
</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 post type 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">- 'viewable' => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'viewable' => array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Whether or not the post type can be viewed.' ),
</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">- '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 post type 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 post type.' ),
</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 post type.' ),
</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">- 'supports' => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'supports' => array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'All features, supported by the post type.' ),
</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">- 'taxonomies' => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'taxonomies' => array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Taxonomies associated with post type.' ),
</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">@@ -340,13 +345,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 post type.' ),
</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 route\'s namespace for the post type.' ),
+ '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 post type.' ),
</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="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-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -48,9 +48,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function __construct( $post_type ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->post_type = $post_type;
</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"> $obj = get_post_type_object( $post_type );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->namespace = ! empty( $obj->rest_namespace ) ? $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_Post_Meta_Fields( $this->post_type );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2037,7 +2037,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // If we have a featured media, add that.
</span><span class="cx" style="display: block; padding: 0 10px"> $featured_media = get_post_thumbnail_id( $post->ID );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $featured_media ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $image_url = rest_url( 'wp/v2/media/' . $featured_media );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $image_url = rest_url( rest_get_route_for_post( $featured_media ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $links['https://api.w.org/featuredmedia'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'href' => $image_url,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2046,7 +2046,7 @@
</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"> if ( ! in_array( $post->post_type, array( 'attachment', 'nav_menu_item', 'revision' ), true ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $attachments_url = rest_url( 'wp/v2/media' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $attachments_url = rest_url( rest_get_route_for_post_type_items( 'attachment' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $attachments_url = add_query_arg( 'parent', $post->ID, $attachments_url );
</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/attachment'] = array(
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestrevisionscontrollerphp"></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-revisions-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-revisions-controller.php 2021-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -49,10 +49,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function __construct( $parent_post_type ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->parent_post_type = $parent_post_type;
</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"> $this->rest_base = 'revisions';
</span><span class="cx" style="display: block; padding: 0 10px"> $post_type_object = get_post_type_object( $parent_post_type );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->parent_controller = $post_type_object->get_rest_controller();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $this->parent_controller ) {
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswpresttemplatescontrollerphp"></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-templates-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-templates-controller.php 2021-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -33,9 +33,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function __construct( $post_type ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->post_type = $post_type;
</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"> $obj = get_post_type_object( $post_type );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->namespace = ! empty( $obj->rest_namespace ) ? $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">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</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-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -914,15 +914,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $post_type_links = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $taxonomy_obj->object_type as $type ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $post_type_object = get_post_type_object( $type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $rest_path = rest_get_route_for_post_type_items( $type );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( empty( $post_type_object->show_in_rest ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $rest_path ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> continue;
</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">- $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
</del><span class="cx" style="display: block; padding: 0 10px"> $post_type_links[] = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'href' => add_query_arg( $this->rest_base, $term->term_id, rest_url( sprintf( 'wp/v2/%s', $rest_base ) ) ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'href' => add_query_arg( $this->rest_base, $term->term_id, rest_url( $rest_path ) ),
</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></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-10-31 20:26:07 UTC (rev 51961)
+++ trunk/src/wp-includes/rest-api.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3049,25 +3049,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">- $post_type = get_post_type_object( $post->post_type );
- if ( ! $post_type ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $post_type_route = rest_get_route_for_post_type_items( $post->post_type );
+ if ( ! $post_type_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 = $post_type->get_rest_controller();
- if ( ! $controller ) {
- return '';
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $route = sprintf( '%s/%d', $post_type_route, $post->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 two controllers that we can detect are the Attachments and Posts controllers.
- if ( in_array( get_class( $controller ), array( 'WP_REST_Attachments_Controller', 'WP_REST_Posts_Controller' ), true ) ) {
- $namespace = 'wp/v2';
- $rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
- $route = sprintf( '/%s/%s/%d', $namespace, $rest_base, $post->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 post.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3080,6 +3068,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 post type.
+ *
+ * @since 5.9.0
+ *
+ * @param string $post_type The name of a registered post type.
+ * @return string The route path with a leading slash for the given post type, or an empty string if there is not a route.
+ */
+function rest_get_route_for_post_type_items( $post_type ) {
+ $post_type = get_post_type_object( $post_type );
+ if ( ! $post_type ) {
+ return '';
+ }
+
+ if ( ! $post_type->show_in_rest ) {
+ return '';
+ }
+
+ $namespace = ! empty( $post_type->rest_namespace ) ? $post_type->rest_namespace : 'wp/v2';
+ $rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
+ $route = sprintf( '/%s/%s', $namespace, $rest_base );
+
+ /**
+ * Filters the REST API route for a post type.
+ *
+ * @since 5.9.0
+ *
+ * @param string $route The route path.
+ * @param WP_Post_Type $post_type The post type object.
+ */
+ return apply_filters( 'rest_route_for_post_type_items', $route, $post_type );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Gets the REST API route for a term.
</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="trunktestsphpunittestsrestapirestposttypescontrollerphp"></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-post-types-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-post-types-controller.php 2021-10-31 20:26:07 UTC (rev 51961)
+++ trunk/tests/phpunit/tests/rest-api/rest-post-types-controller.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -62,6 +62,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( array( 'category', 'post_tag' ), $data['taxonomies'] );
</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 53656
+ */
+ public function test_get_item_cpt() {
+ register_post_type(
+ 'cpt',
+ array(
+ 'show_in_rest' => true,
+ 'rest_base' => 'cpt',
+ 'rest_namespace' => 'wordpress/v1',
+ )
+ );
+ $request = new WP_REST_Request( 'GET', '/wp/v2/types/cpt' );
+ $response = rest_get_server()->dispatch( $request );
+ $this->check_post_type_object_response( 'view', $response, 'cpt' );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_get_item_page() {
</span><span class="cx" style="display: block; padding: 0 10px"> $request = new WP_REST_Request( 'GET', '/wp/v2/types/page' );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -144,7 +161,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( 11, $properties );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertCount( 12, $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">@@ -155,6 +172,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'supports', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'taxonomies', $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"> $this->assertArrayHasKey( 'visibility', $properties );
</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">@@ -204,6 +222,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $post_type_obj->description, $data['description'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $post_type_obj->hierarchical, $data['hierarchical'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $post_type_obj->rest_base, $data['rest_base'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( $post_type_obj->rest_namespace, $data['rest_namespace'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $links = test_rest_expand_compact_links( $links );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( rest_url( 'wp/v2/types' ), $links['collection'][0]['href'] );
</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-10-31 20:26:07 UTC (rev 51961)
+++ trunk/tests/phpunit/tests/rest-api.php 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1830,6 +1830,48 @@
</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 53656
+ */
+ public function test_rest_get_route_for_post_custom_namespace() {
+ register_post_type(
+ 'cpt',
+ array(
+ 'show_in_rest' => true,
+ 'rest_base' => 'cpt',
+ 'rest_namespace' => 'wordpress/v1',
+ )
+ );
+ $post = self::factory()->post->create_and_get( array( 'post_type' => 'cpt' ) );
+
+ $this->assertSame( '/wordpress/v1/cpt/' . $post->ID, rest_get_route_for_post( $post ) );
+ unregister_post_type( 'cpt' );
+ }
+
+ /**
+ * @ticket 53656
+ */
+ public function test_rest_get_route_for_post_type_items() {
+ $this->assertSame( '/wp/v2/posts', rest_get_route_for_post_type_items( 'post' ) );
+ }
+
+ /**
+ * @ticket 53656
+ */
+ public function test_rest_get_route_for_post_type_items_custom_namespace() {
+ register_post_type(
+ 'cpt',
+ array(
+ 'show_in_rest' => true,
+ 'rest_base' => 'cpt',
+ 'rest_namespace' => 'wordpress/v1',
+ )
+ );
+
+ $this->assertSame( '/wordpress/v1/cpt', rest_get_route_for_post_type_items( 'cpt' ) );
+ unregister_post_type( 'cpt' );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 49116
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_rest_get_route_for_post_non_rest() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1839,10 +1881,11 @@
</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"> * @ticket 49116
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 53656
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_rest_get_route_for_post_custom_controller() {
</span><span class="cx" style="display: block; padding: 0 10px"> $post = self::factory()->post->create_and_get( array( 'post_type' => 'wp_block' ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( '', rest_get_route_for_post( $post ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( '/wp/v2/blocks/' . $post->ID, rest_get_route_for_post( $post ) );
</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></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-10-31 20:26:07 UTC (rev 51961)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js 2021-10-31 23:15:10 UTC (rev 51962)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -8336,6 +8336,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "post_tag"
</span><span class="cx" style="display: block; padding: 0 10px"> ],
</span><span class="cx" style="display: block; padding: 0 10px"> "rest_base": "posts",
</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">@@ -8363,6 +8364,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "slug": "page",
</span><span class="cx" style="display: block; padding: 0 10px"> "taxonomies": [],
</span><span class="cx" style="display: block; padding: 0 10px"> "rest_base": "pages",
</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">@@ -8390,6 +8392,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "slug": "attachment",
</span><span class="cx" style="display: block; padding: 0 10px"> "taxonomies": [],
</span><span class="cx" style="display: block; padding: 0 10px"> "rest_base": "media",
</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">@@ -8417,6 +8420,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "slug": "wp_block",
</span><span class="cx" style="display: block; padding: 0 10px"> "taxonomies": [],
</span><span class="cx" style="display: block; padding: 0 10px"> "rest_base": "blocks",
</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">@@ -8444,6 +8448,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "slug": "wp_template",
</span><span class="cx" style="display: block; padding: 0 10px"> "taxonomies": [],
</span><span class="cx" style="display: block; padding: 0 10px"> "rest_base": "templates",
</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">@@ -8475,7 +8480,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "category",
</span><span class="cx" style="display: block; padding: 0 10px"> "post_tag"
</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": "posts"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "rest_base": "posts",
+ "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.StatusesCollection = {
</span></span></pre>
</div>
</div>
</body>
</html>