<!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>[53152] trunk: REST API: Bring new endpoints for Block Patterns from Gutenberg plugin</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/53152">53152</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/53152","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>gziolo</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-04-12 09:24:51 +0000 (Tue, 12 Apr 2022)</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: Bring new endpoints for Block Patterns from Gutenberg plugin
Related Gutenberg issue: https://github.com/WordPress/gutenberg/issues/39889.
Backporting changes from the Gutenberg plugin:
- new Block Patterns REST API endpoint
- new Block Pattern Categories REST API endpoint
- updates to Query Loop related patterns
- support for custom taxonomies in Query Loop block
Props hellofromtonya, peterwilsoncc, ntsekouras, zieladam, ironprogrammer, spacedmonkey, timothyblynjacobs, antonvlasenko, jsnajdr.
See <a href="https://core.trac.wordpress.org/ticket/55505">#55505</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblockpatternsquerygridpostsphp">trunk/src/wp-includes/block-patterns/query-grid-posts.php</a></li>
<li><a href="#trunksrcwpincludesblockpatternsquerylargetitlepostsphp">trunk/src/wp-includes/block-patterns/query-large-title-posts.php</a></li>
<li><a href="#trunksrcwpincludesblockpatternsquerymediumpostsphp">trunk/src/wp-includes/block-patterns/query-medium-posts.php</a></li>
<li><a href="#trunksrcwpincludesblockpatternsqueryoffsetpostsphp">trunk/src/wp-includes/block-patterns/query-offset-posts.php</a></li>
<li><a href="#trunksrcwpincludesblockpatternsquerysmallpostsphp">trunk/src/wp-includes/block-patterns/query-small-posts.php</a></li>
<li><a href="#trunksrcwpincludesblockpatternsquerystandardpostsphp">trunk/src/wp-includes/block-patterns/query-standard-posts.php</a></li>
<li><a href="#trunksrcwpincludesblockpatternsphp">trunk/src/wp-includes/block-patterns.php</a></li>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestpatterndirectorycontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
<li><a href="#trunktestsphpunittestsblockswpBlockphp">trunk/tests/phpunit/tests/blocks/wpBlock.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestpatterndirectorycontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemasetupphp">trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php</a></li>
<li><a href="#trunktestsqunitfixtureswpapigeneratedjs">trunk/tests/qunit/fixtures/wp-api-generated.js</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestblockpatterncategoriescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestblockpatternscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiwpRestBlockPatternCategoriesControllerphp">trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiwpRestBlockPatternsControllerphp">trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesblockpatternsquerygridpostsphp"></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/block-patterns/query-grid-posts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-patterns/query-grid-posts.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/src/wp-includes/block-patterns/query-grid-posts.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -9,7 +9,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'title' => _x( 'Grid', 'Block pattern title' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'blockTypes' => array( 'core/query' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'categories' => array( 'query' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'content' => '<!-- wp:query {"query":{"perPage":6,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"exclude","inherit":false},"displayLayout":{"type":"flex","columns":3}} -->
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'content' => '<!-- wp:query {"query":{"perPage":6,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"exclude","inherit":false},"displayLayout":{"type":"flex","columns":3}} -->
</ins><span class="cx" style="display: block; padding: 0 10px"> <div class="wp-block-query">
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:post-template -->
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:group {"style":{"spacing":{"padding":{"top":"30px","right":"30px","bottom":"30px","left":"30px"}}},"layout":{"inherit":false}} -->
</span></span></pre></div>
<a id="trunksrcwpincludesblockpatternsquerylargetitlepostsphp"></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/block-patterns/query-large-title-posts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-patterns/query-large-title-posts.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/src/wp-includes/block-patterns/query-large-title-posts.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,7 +10,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'blockTypes' => array( 'core/query' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'categories' => array( 'query' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'content' => '<!-- wp:group {"align":"full","style":{"spacing":{"padding":{"top":"100px","right":"100px","bottom":"100px","left":"100px"}},"color":{"text":"#ffffff","background":"#000000"}}} -->
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- <div class="wp-block-group alignfull has-text-color has-background" style="background-color:#000000;color:#ffffff;padding-top:100px;padding-right:100px;padding-bottom:100px;padding-left:100px"><!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <div class="wp-block-group alignfull has-text-color has-background" style="background-color:#000000;color:#ffffff;padding-top:100px;padding-right:100px;padding-bottom:100px;padding-left:100px"><!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
</ins><span class="cx" style="display: block; padding: 0 10px"> <div class="wp-block-query"><!-- wp:post-template -->
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:separator {"customColor":"#ffffff","align":"wide","className":"is-style-wide"} -->
</span><span class="cx" style="display: block; padding: 0 10px"> <hr class="wp-block-separator alignwide has-text-color has-background is-style-wide" style="background-color:#ffffff;color:#ffffff"/>
</span></span></pre></div>
<a id="trunksrcwpincludesblockpatternsquerymediumpostsphp"></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/block-patterns/query-medium-posts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-patterns/query-medium-posts.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/src/wp-includes/block-patterns/query-medium-posts.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -9,7 +9,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'title' => _x( 'Image at left', 'Block pattern title' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'blockTypes' => array( 'core/query' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'categories' => array( 'query' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
</ins><span class="cx" style="display: block; padding: 0 10px"> <div class="wp-block-query">
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:post-template -->
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:columns {"align":"wide"} -->
</span></span></pre></div>
<a id="trunksrcwpincludesblockpatternsqueryoffsetpostsphp"></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/block-patterns/query-offset-posts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-patterns/query-offset-posts.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/src/wp-includes/block-patterns/query-offset-posts.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,7 +12,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'content' => '<!-- wp:group {"style":{"spacing":{"padding":{"top":"30px","right":"30px","bottom":"30px","left":"30px"}}},"layout":{"inherit":false}} -->
</span><span class="cx" style="display: block; padding: 0 10px"> <div class="wp-block-group" style="padding-top:30px;padding-right:30px;padding-bottom:30px;padding-left:30px"><!-- wp:columns -->
</span><span class="cx" style="display: block; padding: 0 10px"> <div class="wp-block-columns"><!-- wp:column {"width":"50%"} -->
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- <div class="wp-block-column" style="flex-basis:50%"><!-- wp:query {"query":{"perPage":2,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"exclude","inherit":false},"displayLayout":{"type":"list"}} -->
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <div class="wp-block-column" style="flex-basis:50%"><!-- wp:query {"query":{"perPage":2,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"exclude","inherit":false},"displayLayout":{"type":"list"}} -->
</ins><span class="cx" style="display: block; padding: 0 10px"> <div class="wp-block-query"><!-- wp:post-template -->
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:post-featured-image /-->
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:post-title /-->
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -24,7 +24,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- /wp:query --></div>
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- /wp:column -->
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:column {"width":"50%"} -->
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- <div class="wp-block-column" style="flex-basis:50%"><!-- wp:query {"query":{"perPage":2,"pages":0,"offset":2,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"exclude","inherit":false},"displayLayout":{"type":"list"}} -->
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <div class="wp-block-column" style="flex-basis:50%"><!-- wp:query {"query":{"perPage":2,"pages":0,"offset":2,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"exclude","inherit":false},"displayLayout":{"type":"list"}} -->
</ins><span class="cx" style="display: block; padding: 0 10px"> <div class="wp-block-query"><!-- wp:post-template -->
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:spacer {"height":200} -->
</span><span class="cx" style="display: block; padding: 0 10px"> <div style="height:200px" aria-hidden="true" class="wp-block-spacer"></div>
</span></span></pre></div>
<a id="trunksrcwpincludesblockpatternsquerysmallpostsphp"></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/block-patterns/query-small-posts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-patterns/query-small-posts.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/src/wp-includes/block-patterns/query-small-posts.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -9,7 +9,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'title' => _x( 'Small image and title', 'Block pattern title' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'blockTypes' => array( 'core/query' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'categories' => array( 'query' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
</ins><span class="cx" style="display: block; padding: 0 10px"> <div class="wp-block-query">
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:post-template -->
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:columns {"verticalAlignment":"center"} -->
</span></span></pre></div>
<a id="trunksrcwpincludesblockpatternsquerystandardpostsphp"></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/block-patterns/query-standard-posts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-patterns/query-standard-posts.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/src/wp-includes/block-patterns/query-standard-posts.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -9,7 +9,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'title' => _x( 'Standard', 'Block pattern title' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'blockTypes' => array( 'core/query' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'categories' => array( 'query' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
</ins><span class="cx" style="display: block; padding: 0 10px"> <div class="wp-block-query">
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:post-template -->
</span><span class="cx" style="display: block; padding: 0 10px"> <!-- wp:post-title {"isLink":true} /-->
</span></span></pre></div>
<a id="trunksrcwpincludesblockpatternsphp"></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/block-patterns.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-patterns.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/src/wp-includes/block-patterns.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,7 +12,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Registers the core block patterns and categories.
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @private
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @access private
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function _register_core_block_patterns_and_categories() {
</span><span class="cx" style="display: block; padding: 0 10px"> $should_register_core_patterns = get_theme_support( 'core-block-patterns' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -127,3 +127,215 @@
</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">+
+/**
+ * Registers patterns from Pattern Directory provided by a theme's
+ * `theme.json` file.
+ *
+ * @since 6.0.0
+ * @access private
+ */
+function _register_remote_theme_patterns() {
+ if ( ! get_theme_support( 'core-block-patterns' ) ) {
+ return;
+ }
+
+ /** This filter is documented in wp-includes/block-patterns.php */
+ if ( ! apply_filters( 'should_load_remote_block_patterns', true ) ) {
+ return;
+ }
+
+ if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
+ return;
+ }
+
+ $pattern_settings = WP_Theme_JSON_Resolver::get_theme_data()->get_patterns();
+ if ( empty( $pattern_settings ) ) {
+ return;
+ }
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
+ $request['slug'] = implode( ',', $pattern_settings );
+ $response = rest_do_request( $request );
+ if ( $response->is_error() ) {
+ return;
+ }
+ $patterns = $response->get_data();
+ $patterns_registry = WP_Block_Patterns_Registry::get_instance();
+ foreach ( $patterns as $pattern ) {
+ $pattern_name = sanitize_title( $pattern['title'] );
+ // Some patterns might be already registered as core patterns with the `core` prefix.
+ $is_registered = $patterns_registry->is_registered( $pattern_name ) || $patterns_registry->is_registered( "core/$pattern_name" );
+ if ( ! $is_registered ) {
+ register_block_pattern( $pattern_name, (array) $pattern );
+ }
+ }
+}
+
+/**
+ * Register any patterns that the active theme may provide under its
+ * `./patterns/` directory. Each pattern is defined as a PHP file and defines
+ * its metadata using plugin-style headers. The minimum required definition is:
+ *
+ * /**
+ * * Title: My Pattern
+ * * Slug: my-theme/my-pattern
+ * *
+ *
+ * The output of the PHP source corresponds to the content of the pattern, e.g.:
+ *
+ * <main><p><?php echo "Hello"; ?></p></main>
+ *
+ * If applicable, this will collect from both parent and child theme.
+ *
+ * Other settable fields include:
+ *
+ * - Description
+ * - Viewport Width
+ * - Categories (comma-separated values)
+ * - Keywords (comma-separated values)
+ * - Block Types (comma-separated values)
+ * - Inserter (yes/no)
+ *
+ * @since 6.0.0
+ * @access private
+ */
+function _register_theme_block_patterns() {
+ $default_headers = array(
+ 'title' => 'Title',
+ 'slug' => 'Slug',
+ 'description' => 'Description',
+ 'viewportWidth' => 'Viewport Width',
+ 'categories' => 'Categories',
+ 'keywords' => 'Keywords',
+ 'blockTypes' => 'Block Types',
+ 'inserter' => 'Inserter',
+ );
+
+ /*
+ * Register patterns for the active theme. If the theme is a child theme,
+ * let it override any patterns from the parent theme that shares the same slug.
+ */
+ $themes = array();
+ $stylesheet = get_stylesheet();
+ $template = get_template();
+ if ( $stylesheet !== $template ) {
+ $themes[] = wp_get_theme( $stylesheet );
+ }
+ $themes[] = wp_get_theme( $template );
+
+ foreach ( $themes as $theme ) {
+ $dirpath = $theme->get_stylesheet_directory() . '/patterns/';
+ if ( ! is_dir( $dirpath ) || ! is_readable( $dirpath ) ) {
+ continue;
+ }
+ if ( file_exists( $dirpath ) ) {
+ $files = glob( $dirpath . '*.php' );
+ if ( $files ) {
+ foreach ( $files as $file ) {
+ $pattern_data = get_file_data( $file, $default_headers );
+
+ if ( empty( $pattern_data['slug'] ) ) {
+ _doing_it_wrong(
+ '_register_theme_block_patterns',
+ sprintf(
+ /* translators: %s: file name. */
+ __( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ),
+ $file
+ ),
+ '6.0.0'
+ );
+ continue;
+ }
+
+ if ( ! preg_match( '/^[A-z0-9\/_-]+$/', $pattern_data['slug'] ) ) {
+ _doing_it_wrong(
+ '_register_theme_block_patterns',
+ sprintf(
+ /* translators: %1s: file name; %2s: slug value found. */
+ __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")' ),
+ $file,
+ $pattern_data['slug']
+ ),
+ '6.0.0'
+ );
+ }
+
+ if ( WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_data['slug'] ) ) {
+ continue;
+ }
+
+ // Title is a required property.
+ if ( ! $pattern_data['title'] ) {
+ _doing_it_wrong(
+ '_register_theme_block_patterns',
+ sprintf(
+ /* translators: %1s: file name; %2s: slug value found. */
+ __( 'Could not register file "%s" as a block pattern ("Title" field missing)' ),
+ $file
+ ),
+ '6.0.0'
+ );
+ continue;
+ }
+
+ // For properties of type array, parse data as comma-separated.
+ foreach ( array( 'categories', 'keywords', 'blockTypes' ) as $property ) {
+ if ( ! empty( $pattern_data[ $property ] ) ) {
+ $pattern_data[ $property ] = array_filter(
+ preg_split(
+ '/[\s,]+/',
+ (string) $pattern_data[ $property ]
+ )
+ );
+ } else {
+ unset( $pattern_data[ $property ] );
+ }
+ }
+
+ // Parse properties of type int.
+ foreach ( array( 'viewportWidth' ) as $property ) {
+ if ( ! empty( $pattern_data[ $property ] ) ) {
+ $pattern_data[ $property ] = (int) $pattern_data[ $property ];
+ } else {
+ unset( $pattern_data[ $property ] );
+ }
+ }
+
+ // Parse properties of type bool.
+ foreach ( array( 'inserter' ) as $property ) {
+ if ( ! empty( $pattern_data[ $property ] ) ) {
+ $pattern_data[ $property ] = in_array(
+ strtolower( $pattern_data[ $property ] ),
+ array( 'yes', 'true' ),
+ true
+ );
+ } else {
+ unset( $pattern_data[ $property ] );
+ }
+ }
+
+ // Translate the pattern metadata.
+ $text_domain = $theme->get( 'TextDomain' );
+ //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.NonSingularStringLiteralContext, WordPress.WP.I18n.NonSingularStringLiteralDomain, WordPress.WP.I18n.LowLevelTranslationFunction
+ $pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', $text_domain );
+ if ( ! empty( $pattern_data['description'] ) ) {
+ //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.NonSingularStringLiteralContext, WordPress.WP.I18n.NonSingularStringLiteralDomain, WordPress.WP.I18n.LowLevelTranslationFunction
+ $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', $text_domain );
+ }
+
+ // The actual pattern content is the output of the file.
+ ob_start();
+ include $file;
+ $pattern_data['content'] = ob_get_clean();
+ if ( ! $pattern_data['content'] ) {
+ continue;
+ }
+
+ register_block_pattern( $pattern_data['slug'], $pattern_data );
+ }
+ }
+ }
+ }
+}
+add_action( 'init', '_register_theme_block_patterns' );
</ins></span></pre></div>
<a id="trunksrcwpincludesblocksphp"></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/blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/src/wp-includes/blocks.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -955,10 +955,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * If do_blocks() needs to remove wpautop() from the `the_content` filter, this re-adds it afterwards,
</span><span class="cx" style="display: block; padding: 0 10px"> * for subsequent `the_content` usage.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.0.0
</ins><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 5.0.0
- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @param string $content The post content running through this filter.
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string The unmodified content.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1142,15 +1141,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset;
</span><span class="cx" style="display: block; padding: 0 10px"> $query['posts_per_page'] = $per_page;
</span><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( $block->context['query']['categoryIds'] ) ) {
- $term_ids = array_map( 'intval', $block->context['query']['categoryIds'] );
- $term_ids = array_filter( $term_ids );
- $query['category__in'] = $term_ids;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility.
+ if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) {
+ $tax_query = array();
+ if ( ! empty( $block->context['query']['categoryIds'] ) ) {
+ $tax_query[] = array(
+ 'taxonomy' => 'category',
+ 'terms' => array_filter( array_map( 'intval', $block->context['query']['categoryIds'] ) ),
+ 'include_children' => false,
+ );
+ }
+ if ( ! empty( $block->context['query']['tagIds'] ) ) {
+ $tax_query[] = array(
+ 'taxonomy' => 'post_tag',
+ 'terms' => array_filter( array_map( 'intval', $block->context['query']['tagIds'] ) ),
+ 'include_children' => false,
+ );
+ }
+ $query['tax_query'] = $tax_query;
</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( $block->context['query']['tagIds'] ) ) {
- $term_ids = array_map( 'intval', $block->context['query']['tagIds'] );
- $term_ids = array_filter( $term_ids );
- $query['tag__in'] = $term_ids;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $block->context['query']['taxQuery'] ) ) {
+ $query['tax_query'] = array();
+ foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) {
+ if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) {
+ $query['tax_query'][] = array(
+ 'taxonomy' => $taxonomy,
+ 'terms' => array_filter( array_map( 'intval', $terms ) ),
+ 'include_children' => false,
+ );
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> if (
</span><span class="cx" style="display: block; padding: 0 10px"> isset( $block->context['query']['order'] ) &&
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestblockpatterncategoriescontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-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-block-pattern-categories-controller.php (rev 0)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,150 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Block_Pattern_Catergories_Controller class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 6.0.0
+ */
+
+/**
+ * Core class used to access block pattern categories via the REST API.
+ *
+ * @since 6.0.0
+ *
+ * @see WP_REST_Controller
+ */
+class WP_REST_Block_Pattern_Categories_Controller extends WP_REST_Controller {
+
+ /**
+ * Constructs the controller.
+ *
+ * @since 6.0.0
+ */
+ public function __construct() {
+ $this->namespace = 'wp/v2';
+ $this->rest_base = 'block-patterns/categories';
+ }
+
+ /**
+ * Registers the routes for the objects of the controller.
+ *
+ * @since 6.0.0
+ */
+ public function register_routes() {
+ register_rest_route(
+ $this->namespace,
+ '/' . $this->rest_base,
+ array(
+ array(
+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => array( $this, 'get_items' ),
+ 'permission_callback' => array( $this, 'get_items_permissions_check' ),
+ ),
+ 'schema' => array( $this, 'get_public_item_schema' ),
+ )
+ );
+ }
+
+ /**
+ * Checks whether a given request has permission to read block patterns.
+ *
+ * @since 6.0.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
+ */
+ public function get_items_permissions_check( $request ) {
+ if ( current_user_can( 'edit_posts' ) ) {
+ return true;
+ }
+
+ foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
+ if ( current_user_can( $post_type->cap->edit_posts ) ) {
+ return true;
+ }
+ }
+
+ return new WP_Error(
+ 'rest_cannot_view',
+ __( 'Sorry, you are not allowed to view the registered block pattern categories.' ),
+ array( 'status' => rest_authorization_required_code() )
+ );
+ }
+
+ /**
+ * Retrieves all block pattern categories.
+ *
+ * @since 6.0.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
+ */
+ public function get_items( $request ) {
+ $response = array();
+ $categories = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered();
+ foreach ( $categories as $category ) {
+ $prepared_category = $this->prepare_item_for_response( $category, $request );
+ $response[] = $this->prepare_response_for_collection( $prepared_category );
+ }
+
+ return rest_ensure_response( $response );
+ }
+
+ /**
+ * Prepare a raw block pattern category before it gets output in a REST API response.
+ *
+ * @since 6.0.0
+ *
+ * @param object $item Raw category as registered, before any changes.
+ * @param WP_REST_Request $request Request object.
+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
+ */
+ public function prepare_item_for_response( $item, $request ) {
+ $fields = $this->get_fields_for_response( $request );
+ $keys = array( 'name', 'label' );
+ $data = array();
+ foreach ( $keys as $key ) {
+ if ( rest_is_field_included( $key, $fields ) ) {
+ $data[ $key ] = $item[ $key ];
+ }
+ }
+
+ $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
+ $data = $this->add_additional_fields_to_object( $data, $request );
+ $data = $this->filter_response_by_context( $data, $context );
+
+ return rest_ensure_response( $data );
+ }
+
+ /**
+ * Retrieves the block pattern category schema, conforming to JSON Schema.
+ *
+ * @since 6.0.0
+ *
+ * @return array Item schema data.
+ */
+ public function get_item_schema() {
+ $schema = array(
+ '$schema' => 'http://json-schema.org/draft-04/schema#',
+ 'title' => 'block-pattern-category',
+ 'type' => 'object',
+ 'properties' => array(
+ 'name' => array(
+ 'description' => __( 'The category name.' ),
+ 'type' => 'string',
+ 'readonly' => true,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ 'label' => array(
+ 'description' => __( 'The category label, in human readable format.' ),
+ 'type' => 'string',
+ 'readonly' => true,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ ),
+ );
+
+ return $this->add_additional_fields_schema( $schema );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesrestapiendpointsclasswprestblockpatternscontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-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-block-patterns-controller.php (rev 0)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,198 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Block_Patterns_Controller class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 6.0.0
+ */
+
+/**
+ * Core class used to access block patterns via the REST API.
+ *
+ * @since 6.0.0
+ *
+ * @see WP_REST_Controller
+ */
+class WP_REST_Block_Patterns_Controller extends WP_REST_Controller {
+
+ /**
+ * Constructs the controller.
+ *
+ * @since 6.0.0
+ */
+ public function __construct() {
+ $this->namespace = 'wp/v2';
+ $this->rest_base = 'block-patterns/patterns';
+ }
+
+ /**
+ * Registers the routes for the objects of the controller.
+ *
+ * @since 6.0.0
+ */
+ public function register_routes() {
+ register_rest_route(
+ $this->namespace,
+ '/' . $this->rest_base,
+ array(
+ array(
+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => array( $this, 'get_items' ),
+ 'permission_callback' => array( $this, 'get_items_permissions_check' ),
+ ),
+ 'schema' => array( $this, 'get_public_item_schema' ),
+ )
+ );
+ }
+
+ /**
+ * Checks whether a given request has permission to read block patterns.
+ *
+ * @since 6.0.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
+ */
+ public function get_items_permissions_check( $request ) {
+ if ( current_user_can( 'edit_posts' ) ) {
+ return true;
+ }
+
+ foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
+ if ( current_user_can( $post_type->cap->edit_posts ) ) {
+ return true;
+ }
+ }
+
+ return new WP_Error(
+ 'rest_cannot_view',
+ __( 'Sorry, you are not allowed to view the registered block patterns.' ),
+ array( 'status' => rest_authorization_required_code() )
+ );
+ }
+
+ /**
+ * Retrieves all block patterns.
+ *
+ * @since 6.0.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
+ */
+ public function get_items( $request ) {
+ // Load block patterns from w.org.
+ _load_remote_block_patterns(); // Patterns with the `core` keyword.
+ _load_remote_featured_patterns(); // Patterns in the `featured` category.
+ _register_remote_theme_patterns(); // Patterns requested by current theme.
+
+ $response = array();
+ $patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered();
+ foreach ( $patterns as $pattern ) {
+ $prepared_pattern = $this->prepare_item_for_response( $pattern, $request );
+ $response[] = $this->prepare_response_for_collection( $prepared_pattern );
+ }
+ return rest_ensure_response( $response );
+ }
+
+ /**
+ * Prepare a raw block pattern before it gets output in a REST API response.
+ *
+ * @since 6.0.0
+ *
+ * @param object $item Raw pattern as registered, before any changes.
+ * @param WP_REST_Request $request Request object.
+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
+ */
+ public function prepare_item_for_response( $item, $request ) {
+ $fields = $this->get_fields_for_response( $request );
+ $keys = array(
+ 'name',
+ 'title',
+ 'description',
+ 'viewportWidth',
+ 'blockTypes',
+ 'categories',
+ 'keywords',
+ 'content',
+ );
+ $data = array();
+ foreach ( $keys as $key ) {
+ if ( isset( $item[ $key ] ) && rest_is_field_included( $key, $fields ) ) {
+ $data[ $key ] = $item[ $key ];
+ }
+ }
+
+ $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
+ $data = $this->add_additional_fields_to_object( $data, $request );
+ $data = $this->filter_response_by_context( $data, $context );
+ return rest_ensure_response( $data );
+ }
+
+ /**
+ * Retrieves the block pattern schema, conforming to JSON Schema.
+ *
+ * @since 6.0.0
+ *
+ * @return array Item schema data.
+ */
+ public function get_item_schema() {
+ $schema = array(
+ '$schema' => 'http://json-schema.org/draft-04/schema#',
+ 'title' => 'block-pattern',
+ 'type' => 'object',
+ 'properties' => array(
+ 'name' => array(
+ 'description' => __( 'The pattern name.' ),
+ 'type' => 'string',
+ 'readonly' => true,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ 'title' => array(
+ 'description' => __( 'The pattern title, in human readable format.' ),
+ 'type' => 'string',
+ 'readonly' => true,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ 'description' => array(
+ 'description' => __( 'The pattern detailed description.' ),
+ 'type' => 'string',
+ 'readonly' => true,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ 'viewportWidth' => array(
+ 'description' => __( 'The pattern viewport width for inserter preview.' ),
+ 'type' => 'number',
+ 'readonly' => true,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ 'blockTypes' => array(
+ 'description' => __( 'Block types that the pattern is intended to be used with.' ),
+ 'type' => 'array',
+ 'readonly' => true,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ 'categories' => array(
+ 'description' => __( 'The pattern category slugs.' ),
+ 'type' => 'array',
+ 'readonly' => true,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ 'keywords' => array(
+ 'description' => __( 'The pattern keywords.' ),
+ 'type' => 'array',
+ 'readonly' => true,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ 'content' => array(
+ 'description' => __( 'The pattern content.' ),
+ 'type' => 'string',
+ 'readonly' => true,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ ),
+ );
+
+ return $this->add_additional_fields_schema( $schema );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesrestapiendpointsclasswprestpatterndirectorycontrollerphp"></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-pattern-directory-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-pattern-directory-controller.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -80,6 +80,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Search and retrieve block patterns metadata
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.8.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.0.0 Added 'slug' to request.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Full details about the request.
</span><span class="cx" style="display: block; padding: 0 10px"> * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -100,6 +101,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $category_id = $request['category'];
</span><span class="cx" style="display: block; padding: 0 10px"> $keyword_id = $request['keyword'];
</span><span class="cx" style="display: block; padding: 0 10px"> $search_term = $request['search'];
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $slug = $request['slug'];
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $category_id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $query_args['pattern-categories'] = $category_id;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -113,6 +115,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $query_args['search'] = $search_term;
</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 ( $slug ) {
+ $query_args['slug'] = $slug;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * Include a hash of the query args, so that different requests are stored in
</span><span class="cx" style="display: block; padding: 0 10px"> * separate caches.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -159,7 +165,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $raw_patterns = new WP_Error(
</span><span class="cx" style="display: block; padding: 0 10px"> 'pattern_api_failed',
</span><span class="cx" style="display: block; padding: 0 10px"> sprintf(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /* translators: %s: Support forums URL. */
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /* translators: %s: Support forums URL. */
</ins><span class="cx" style="display: block; padding: 0 10px"> __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> __( 'https://wordpress.org/support/forums/' )
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -255,7 +261,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'The pattern ID.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'integer',
</span><span class="cx" style="display: block; padding: 0 10px"> 'minimum' => 1,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'context' => array( 'view', 'embed' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'context' => array( 'view', 'edit', 'embed' ),
</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"> 'title' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -262,7 +268,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'The pattern title, in human readable format.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="cx" style="display: block; padding: 0 10px"> 'minLength' => 1,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'context' => array( 'view', 'embed' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'context' => array( 'view', 'edit', 'embed' ),
</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"> 'content' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -269,7 +275,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'The pattern content.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="cx" style="display: block; padding: 0 10px"> 'minLength' => 1,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'context' => array( 'view', 'embed' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'context' => array( 'view', 'edit', 'embed' ),
</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"> 'categories' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -277,7 +283,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'array',
</span><span class="cx" style="display: block; padding: 0 10px"> 'uniqueItems' => true,
</span><span class="cx" style="display: block; padding: 0 10px"> 'items' => array( 'type' => 'string' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'context' => array( 'view', 'embed' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'context' => array( 'view', 'edit', 'embed' ),
</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"> 'keywords' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -285,7 +291,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'array',
</span><span class="cx" style="display: block; padding: 0 10px"> 'uniqueItems' => true,
</span><span class="cx" style="display: block; padding: 0 10px"> 'items' => array( 'type' => 'string' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'context' => array( 'view', 'embed' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'context' => array( 'view', 'edit', 'embed' ),
</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"> 'description' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -292,13 +298,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'A description of the pattern.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="cx" style="display: block; padding: 0 10px"> 'minLength' => 1,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'context' => array( 'view', 'embed' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'context' => array( 'view', 'edit', 'embed' ),
</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"> 'viewport_width' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'The preferred width of the viewport when previewing a pattern, in pixels.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'integer',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'context' => array( 'view', 'embed' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'context' => array( 'view', 'edit', 'embed' ),
</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 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/src/wp-includes/rest-api.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -337,6 +337,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $controller = new WP_REST_Pattern_Directory_Controller();
</span><span class="cx" style="display: block; padding: 0 10px"> $controller->register_routes();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Block Patterns.
+ $controller = new WP_REST_Block_Patterns_Controller();
+ $controller->register_routes();
+
+ // Block Pattern Categories.
+ $controller = new WP_REST_Block_Pattern_Categories_Controller();
+ $controller->register_routes();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Site Health.
</span><span class="cx" style="display: block; padding: 0 10px"> $site_health = WP_Site_Health::get_instance();
</span><span class="cx" style="display: block; padding: 0 10px"> $controller = new WP_REST_Site_Health_Controller( $site_health );
</span></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-settings.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/src/wp-settings.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -277,6 +277,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-directory-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-patterns-controller.php';
+require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-application-passwords-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-site-health-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-sidebars-controller.php';
</span></span></pre></div>
<a id="trunktestsphpunittestsblockswpBlockphp"></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/blocks/wpBlock.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/wpBlock.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/tests/phpunit/tests/blocks/wpBlock.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -443,8 +443,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'order' => 'DESC',
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => 'title',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post__not_in' => array( 1, 2 ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'category__in' => array( 56 ),
- 'tag__in' => array( 3, 11, 10 ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'tax_query' => array(
+ array(
+ 'taxonomy' => 'category',
+ 'terms' => array( 56 ),
+ 'include_children' => false,
+ ),
+ array(
+ 'taxonomy' => 'post_tag',
+ 'terms' => array( 3, 11, 10 ),
+ 'include_children' => false,
+ ),
+ ),
</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="trunktestsphpunittestsrestapirestpatterndirectorycontrollerphp"></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-pattern-directory-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-pattern-directory-controller.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -79,7 +79,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $patterns = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'view', $patterns['endpoints'][0]['args']['context']['default'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( array( 'view', 'embed' ), $patterns['endpoints'][0]['args']['context']['enum'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( array( 'view', 'embed', 'edit' ), $patterns['endpoints'][0]['args']['context']['enum'] );
</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="trunktestsphpunittestsrestapirestschemasetupphp"></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-schema-setup.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -161,6 +161,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/plugins',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/plugins/(?P<plugin>[^.\/]+(?:\/[^.\/]+)?)',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/block-directory/search',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '/wp/v2/block-patterns/categories',
+ '/wp/v2/block-patterns/patterns',
</ins><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/sidebars',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/sidebars/(?P<id>[\w-]+)',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/widget-types',
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiwpRestBlockPatternCategoriesControllerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,175 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Unit tests covering WP_Block_Pattern_Categories_Registry functionality.
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 6.0.0
+ */
+
+/**
+ * Tests for REST API for Block Pattern Categories Registry.
+ *
+ * @since 6.0.0
+ *
+ * @ticket 55505
+ *
+ * @covers WP_REST_Block_Pattern_Categories_Controller
+ *
+ * @group restapi
+ */
+class Tests_REST_WpRestBlockPatternCategoriesController extends WP_Test_REST_Controller_Testcase {
+
+ /**
+ * Admin user ID.
+ *
+ * @since 6.0.0
+ *
+ * @var int
+ */
+ protected static $admin_id;
+
+ /**
+ * Original instance of WP_Block_Patterns_Registry.
+ *
+ * @since 6.0.0
+ *
+ * @var WP_Block_Patterns_Registry
+ */
+ protected static $orig_registry;
+
+ /**
+ * Instance of the reflected `instance` property.
+ *
+ * @since 6.0.0
+ *
+ * @var ReflectionProperty
+ */
+ private static $registry_instance_property;
+
+ /**
+ * The REST API route.
+ *
+ * @since 6.0.0
+ *
+ * @var string
+ */
+ const REQUEST_ROUTE = '/wp/v2/block-patterns/categories';
+
+ /**
+ * Set up class test fixtures.
+ *
+ * @since 6.0.0
+ *
+ * @param WP_UnitTest_Factory $factory WordPress unit test factory.
+ */
+ public static function wpSetupBeforeClass( $factory ) {
+ self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
+
+ // Setup an empty testing instance of `WP_Block_Pattern_Categories_Registry` and save the original.
+ self::$orig_registry = WP_Block_Pattern_Categories_Registry::get_instance();
+ self::$registry_instance_property = new ReflectionProperty( 'WP_Block_Pattern_Categories_Registry', 'instance' );
+ self::$registry_instance_property->setAccessible( true );
+ $test_registry = new WP_Block_Pattern_Categories_Registry();
+ self::$registry_instance_property->setValue( $test_registry );
+
+ // Register some categories in the test registry.
+ $test_registry->register( 'test', array( 'label' => 'Test' ) );
+ $test_registry->register( 'query', array( 'label' => 'Query' ) );
+ }
+
+ public static function wpTearDownAfterClass() {
+ self::delete_user( self::$admin_id );
+
+ // Restore the original registry instance.
+ self::$registry_instance_property->setValue( self::$orig_registry );
+ self::$registry_instance_property->setAccessible( false );
+ self::$registry_instance_property = null;
+ self::$orig_registry = null;
+ }
+
+ public function set_up() {
+ parent::set_up();
+
+ switch_theme( 'emptytheme' );
+ }
+
+ public function test_register_routes() {
+ $routes = rest_get_server()->get_routes();
+ $this->assertArrayHasKey( static::REQUEST_ROUTE, $routes );
+ }
+
+ public function test_get_items() {
+ wp_set_current_user( self::$admin_id );
+
+ $expected_names = array( 'test', 'query' );
+ $expected_fields = array( 'name', 'label' );
+
+ $request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
+ $request['_fields'] = 'name,label';
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertCount( count( $expected_names ), $data );
+ foreach ( $data as $idx => $item ) {
+ $this->assertSame( $expected_names[ $idx ], $item['name'] );
+ $this->assertSame( $expected_fields, array_keys( $item ) );
+ }
+ }
+
+ /**
+ * Verify capability check for unauthorized request (not logged in).
+ */
+ public function test_get_items_unauthorized() {
+ // Ensure current user is logged out.
+ wp_logout();
+
+ $request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
+ $response = rest_do_request( $request );
+
+ $this->assertWPError( $response->as_error() );
+ $this->assertSame( 401, $response->get_status() );
+ }
+
+ /**
+ * Verify capability check for forbidden request (insufficient capability).
+ */
+ public function test_get_items_forbidden() {
+ // Set current user without `edit_posts` capability.
+ wp_set_current_user( $this->factory()->user->create( array( 'role' => 'subscriber' ) ) );
+
+ $request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
+ $response = rest_do_request( $request );
+
+ $this->assertWPError( $response->as_error() );
+ $this->assertSame( 403, $response->get_status() );
+ }
+
+ public function test_context_param() {
+ $this->markTestSkipped( 'Controller does not use context_param.' );
+ }
+
+ public function test_get_item() {
+ $this->markTestSkipped( 'Controller does not have get_item route.' );
+ }
+
+ public function test_create_item() {
+ $this->markTestSkipped( 'Controller does not have create_item route.' );
+ }
+
+ public function test_update_item() {
+ $this->markTestSkipped( 'Controller does not have update_item route.' );
+ }
+
+ public function test_delete_item() {
+ $this->markTestSkipped( 'Controller does not have delete_item route.' );
+ }
+
+ public function test_prepare_item() {
+ $this->markTestSkipped( 'Controller does not have prepare_item route.' );
+ }
+
+ public function test_get_item_schema() {
+ $this->markTestSkipped( 'Controller does not have get_item_schema route.' );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsrestapiwpRestBlockPatternsControllerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,201 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Unit tests covering WP_REST_Block_Patterns_Controller functionality.
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 6.0.0
+ */
+
+/**
+ * Tests for REST API for Block Patterns.
+ *
+ * @since 6.0.0
+ *
+ * @ticket 55505
+ *
+ * @covers WP_REST_Block_Patterns_Controller
+ *
+ * @group restapi
+ */
+class Tests_REST_WpRestBlockPatternsController extends WP_Test_REST_Controller_Testcase {
+
+ /**
+ * Admin user ID.
+ *
+ * @since 6.0.0
+ *
+ * @var int
+ */
+ protected static $admin_id;
+
+ /**
+ * Original instance of WP_Block_Patterns_Registry.
+ *
+ * @since 6.0.0
+ *
+ * @var WP_Block_Patterns_Registry
+ */
+ protected static $orig_registry;
+
+ /**
+ * Instance of the reflected `instance` property.
+ *
+ * @since 6.0.0
+ *
+ * @var ReflectionProperty
+ */
+ private static $registry_instance_property;
+
+ /**
+ * The REST API route.
+ *
+ * @since 6.0.0
+ *
+ * @var string
+ */
+ const REQUEST_ROUTE = '/wp/v2/block-patterns/patterns';
+
+ /**
+ * Set up class test fixtures.
+ *
+ * @since 6.0.0
+ *
+ * @param WP_UnitTest_Factory $factory WordPress unit test factory.
+ */
+ public static function wpSetUpBeforeClass( $factory ) {
+ self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
+
+ // Setup an empty testing instance of `WP_Block_Patterns_Registry` and save the original.
+ self::$orig_registry = WP_Block_Patterns_Registry::get_instance();
+ self::$registry_instance_property = new ReflectionProperty( 'WP_Block_Patterns_Registry', 'instance' );
+ self::$registry_instance_property->setAccessible( true );
+ $test_registry = new WP_Block_Pattern_Categories_Registry();
+ self::$registry_instance_property->setValue( $test_registry );
+
+ // Register some patterns in the test registry.
+ $test_registry->register(
+ 'test/one',
+ array(
+ 'title' => 'Pattern One',
+ 'categories' => array( 'test' ),
+ 'viewportWidth' => 1440,
+ 'content' => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
+ )
+ );
+
+ $test_registry->register(
+ 'test/two',
+ array(
+ 'title' => 'Pattern Two',
+ 'categories' => array( 'test' ),
+ 'content' => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
+ )
+ );
+ }
+
+ public static function wpTearDownAfterClass() {
+ self::delete_user( self::$admin_id );
+
+ // Restore the original registry instance.
+ self::$registry_instance_property->setValue( self::$orig_registry );
+ self::$registry_instance_property->setAccessible( false );
+ self::$registry_instance_property = null;
+ self::$orig_registry = null;
+ }
+
+ public function set_up() {
+ parent::set_up();
+
+ switch_theme( 'emptytheme' );
+ }
+
+ public function test_register_routes() {
+ $routes = rest_get_server()->get_routes();
+ $this->assertArrayHasKey( static::REQUEST_ROUTE, $routes );
+ }
+
+ public function test_get_items() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
+ $request['_fields'] = 'name,content';
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertIsArray( $data, 'WP_REST_Block_Patterns_Controller::get_items() should return an array' );
+ $this->assertGreaterThanOrEqual( 2, count( $data ), 'WP_REST_Block_Patterns_Controller::get_items() should return at least 2 items' );
+ $this->assertSame(
+ array(
+ 'name' => 'test/one',
+ 'content' => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
+ ),
+ $data[0],
+ 'WP_REST_Block_Patterns_Controller::get_items() should return test/one'
+ );
+ $this->assertSame(
+ array(
+ 'name' => 'test/two',
+ 'content' => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
+ ),
+ $data[1],
+ 'WP_REST_Block_Patterns_Controller::get_items() should return test/two'
+ );
+ }
+
+ /**
+ * Verify capability check for unauthorized request (not logged in).
+ */
+ public function test_get_items_unauthorized() {
+ // Ensure current user is logged out.
+ wp_logout();
+
+ $request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
+ $response = rest_do_request( $request );
+
+ $this->assertWPError( $response->as_error() );
+ $this->assertSame( 401, $response->get_status() );
+ }
+
+ /**
+ * Verify capability check for forbidden request (insufficient capability).
+ */
+ public function test_get_items_forbidden() {
+ // Set current user without `edit_posts` capability.
+ wp_set_current_user( $this->factory()->user->create( array( 'role' => 'subscriber' ) ) );
+
+ $request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
+ $response = rest_do_request( $request );
+
+ $this->assertWPError( $response->as_error() );
+ $this->assertSame( 403, $response->get_status() );
+ }
+
+ public function test_context_param() {
+ $this->markTestSkipped( 'Controller does not use context_param.' );
+ }
+
+ public function test_get_item() {
+ $this->markTestSkipped( 'Controller does not have get_item route.' );
+ }
+
+ public function test_create_item() {
+ $this->markTestSkipped( 'Controller does not have create_item route.' );
+ }
+
+ public function test_update_item() {
+ $this->markTestSkipped( 'Controller does not have update_item route.' );
+ }
+
+ public function test_delete_item() {
+ $this->markTestSkipped( 'Controller does not have delete_item route.' );
+ }
+
+ public function test_prepare_item() {
+ $this->markTestSkipped( 'Controller does not have prepare_item route.' );
+ }
+
+ public function test_get_item_schema() {
+ $this->markTestSkipped( 'Controller does not have get_item_schema route.' );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><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 2022-04-12 06:05:45 UTC (rev 53151)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js 2022-04-12 09:24:51 UTC (rev 53152)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10362,7 +10362,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "type": "string",
</span><span class="cx" style="display: block; padding: 0 10px"> "enum": [
</span><span class="cx" style="display: block; padding: 0 10px"> "view",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- "embed"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "embed",
+ "edit"
</ins><span class="cx" style="display: block; padding: 0 10px"> ],
</span><span class="cx" style="display: block; padding: 0 10px"> "default": "view",
</span><span class="cx" style="display: block; padding: 0 10px"> "required": false
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10396,6 +10397,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">+ "/wp/v2/block-patterns/patterns": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": []
+ }
+ ],
+ "_links": {
+ "self": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/block-patterns/patterns"
+ }
+ ]
+ }
+ },
+ "/wp/v2/block-patterns/categories": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": []
+ }
+ ],
+ "_links": {
+ "self": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/block-patterns/categories"
+ }
+ ]
+ }
+ },
</ins><span class="cx" style="display: block; padding: 0 10px"> "/wp-site-health/v1": {
</span><span class="cx" style="display: block; padding: 0 10px"> "namespace": "wp-site-health/v1",
</span><span class="cx" style="display: block; padding: 0 10px"> "methods": [
</span></span></pre>
</div>
</div>
</body>
</html>