<!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>[49132] trunk: REST API: Introduce search post format handler.</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/49132">49132</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/49132","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>2020-10-12 19:52:57 +0000 (Mon, 12 Oct 2020)</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: Introduce search post format handler.
This allows for clients to search the used post formats via the /wp/v2/search endpoint by using a type=post-format query parameter.
Fixes <a href="https://core.trac.wordpress.org/ticket/51459">#51459</a>.
Props andraganescu, zieladam, noisysocks, TimothyBlynJacobs.</pre>
<h3>Modified Paths</h3>
<ul>
<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="#trunktestsphpunittestsrestapirestsearchcontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-search-controller.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="#trunksrcwpincludesrestapisearchclasswprestpostformatsearchhandlerphp">trunk/src/wp-includes/rest-api/search/class-wp-rest-post-format-search-handler.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapisearchclasswprestpostformatsearchhandlerphp"></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/search/class-wp-rest-post-format-search-handler.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/search/class-wp-rest-post-format-search-handler.php (rev 0)
+++ trunk/src/wp-includes/rest-api/search/class-wp-rest-post-format-search-handler.php 2020-10-12 19:52:57 UTC (rev 49132)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,128 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Post_Format_Search_Handler class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 5.6.0
+ */
+
+/**
+ * Core class representing a search handler for post formats in the REST API.
+ *
+ * @since 5.6.0
+ *
+ * @see WP_REST_Search_Handler
+ */
+class WP_REST_Post_Format_Search_Handler extends WP_REST_Search_Handler {
+
+ /**
+ * Constructor.
+ *
+ * @since 5.6.0
+ */
+ public function __construct() {
+ $this->type = 'post-format';
+ }
+
+ /**
+ * Searches the object type content for a given search request.
+ *
+ * @since 5.6.0
+ *
+ * @param WP_REST_Request $request Full REST request.
+ * @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
+ * an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
+ * total count for the matching search results.
+ */
+ public function search_items( WP_REST_Request $request ) {
+ $format_strings = get_post_format_strings();
+ $format_slugs = array_keys( $format_strings );
+
+ $query_args = array();
+
+ if ( ! empty( $request['search'] ) ) {
+ $query_args['search'] = $request['search'];
+ }
+
+ /**
+ * Filters the query arguments for a search request.
+ *
+ * Enables adding extra arguments or setting defaults for a post format search request.
+ *
+ * @since 5.6.0
+ *
+ * @param array $query_args Key value array of query var to query value.
+ * @param WP_REST_Request $request The request used.
+ */
+ $query_args = apply_filters( 'rest_post_format_search_query', $query_args, $request );
+
+ $found_ids = array();
+ foreach ( $format_slugs as $index => $format_slug ) {
+ if ( ! empty( $query_args['search'] ) ) {
+ $format_string = get_post_format_string( $format_slug );
+ $format_slug_match = stripos( $format_slug, $query_args['search'] ) !== false;
+ $format_string_match = stripos( $format_string, $query_args['search'] ) !== false;
+ if ( ! $format_slug_match && ! $format_string_match ) {
+ continue;
+ }
+ }
+
+ $format_link = get_post_format_link( $format_slug );
+ if ( $format_link ) {
+ $found_ids[] = $format_slug;
+ }
+ }
+
+ $page = (int) $request['page'];
+ $per_page = (int) $request['per_page'];
+
+ return array(
+ self::RESULT_IDS => array_slice( $found_ids, ( $page - 1 ) * $per_page, $per_page ),
+ self::RESULT_TOTAL => count( $found_ids ),
+ );
+ }
+
+ /**
+ * Prepares the search result for a given ID.
+ *
+ * @since 5.6.0
+ *
+ * @param string $id Item ID, the post format slug.
+ * @param array $fields Fields to include for the item.
+ * @return array Associative array containing all fields for the item.
+ */
+ public function prepare_item( $id, array $fields ) {
+ $data = array();
+
+ if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
+ $data[ WP_REST_Search_Controller::PROP_ID ] = $id;
+ }
+
+ if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
+ $data[ WP_REST_Search_Controller::PROP_TITLE ] = get_post_format_string( $id );
+ }
+
+ if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
+ $data[ WP_REST_Search_Controller::PROP_URL ] = get_post_format_link( $id );
+ }
+
+ if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
+ $data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
+ }
+
+ return $data;
+ }
+
+ /**
+ * Prepares links for the search result.
+ *
+ * @since 5.6.0
+ *
+ * @param string $id Item ID, the post format slug.
+ * @return array Links for the given item.
+ */
+ public function prepare_item_links( $id ) {
+ return array();
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/rest-api/search/class-wp-rest-post-format-search-handler.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="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 2020-10-12 19:08:24 UTC (rev 49131)
+++ trunk/src/wp-includes/rest-api.php 2020-10-12 19:52:57 UTC (rev 49132)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -276,6 +276,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $search_handlers = array(
</span><span class="cx" style="display: block; padding: 0 10px"> new WP_REST_Post_Search_Handler(),
</span><span class="cx" style="display: block; padding: 0 10px"> new WP_REST_Term_Search_Handler(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ new WP_REST_Post_Format_Search_Handler(),
</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="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 2020-10-12 19:08:24 UTC (rev 49131)
+++ trunk/src/wp-settings.php 2020-10-12 19:52:57 UTC (rev 49132)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -269,6 +269,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/search/class-wp-rest-search-handler.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/search/class-wp-rest-post-search-handler.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/search/class-wp-rest-term-search-handler.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require ABSPATH . WPINC . '/rest-api/search/class-wp-rest-post-format-search-handler.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/sitemaps.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/sitemaps/class-wp-sitemaps.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/sitemaps/class-wp-sitemaps-index.php';
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestsearchcontrollerphp"></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-search-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-search-controller.php 2020-10-12 19:08:24 UTC (rev 49131)
+++ trunk/tests/phpunit/tests/rest-api/rest-search-controller.php 2020-10-12 19:52:57 UTC (rev 49132)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -54,6 +54,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpSetUpBeforeClass( $factory ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ add_theme_support( 'post-formats' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> self::$my_title_post_ids = $factory->post->create_many(
</span><span class="cx" style="display: block; padding: 0 10px"> 4,
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -77,6 +79,8 @@
</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">+ set_post_format( self::$my_title_post_ids[0], 'aside' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> self::$my_category_id = $factory->term->create(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'taxonomy' => 'category',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -96,6 +100,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Delete our fake data after our tests run.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpTearDownAfterClass() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ remove_theme_support( 'post-formats' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $post_ids = array_merge(
</span><span class="cx" style="display: block; padding: 0 10px"> self::$my_title_post_ids,
</span><span class="cx" style="display: block; padding: 0 10px"> self::$my_title_page_ids,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -704,6 +710,65 @@
</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">+ * Search through post formats of any type.
+ *
+ * @ticket 51459
+ */
+ public function test_get_items_search_type_post_format() {
+ $response = $this->do_request_with_params(
+ array(
+ 'per_page' => 100,
+ 'type' => 'post-format',
+ )
+ );
+ $this->assertEquals( 200, $response->get_status() );
+ $this->assertContains(
+ 'Aside',
+ wp_list_pluck( $response->get_data(), 'title' )
+ );
+ }
+
+ /**
+ * Search through all that matches a 'Aside' search.
+ *
+ * @ticket 51459
+ */
+ public function test_get_items_search_for_test_post_format() {
+ $response = $this->do_request_with_params(
+ array(
+ 'per_page' => 100,
+ 'search' => 'Aside',
+ 'type' => 'post-format',
+ )
+ );
+
+ $this->assertEquals( 200, $response->get_status() );
+ $this->assertContains(
+ 'Aside',
+ wp_list_pluck( $response->get_data(), 'title' )
+ );
+ }
+
+ /**
+ * Searching for a post format that doesn't exist should return an empty
+ * result.
+ *
+ * @ticket 51459
+ */
+ public function test_get_items_search_for_missing_post_format() {
+ $response = $this->do_request_with_params(
+ array(
+ 'per_page' => 100,
+ 'search' => 'Doesn\'t exist',
+ 'type' => 'post-format',
+ )
+ );
+
+ $this->assertEquals( 200, $response->get_status() );
+ $this->assertEmpty( $response->get_data() );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Perform a REST request to our search endpoint with given parameters.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> private function do_request_with_params( $params = array(), $method = 'GET' ) {
</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 2020-10-12 19:08:24 UTC (rev 49131)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js 2020-10-12 19:52:57 UTC (rev 49132)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4510,7 +4510,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "default": "post",
</span><span class="cx" style="display: block; padding: 0 10px"> "enum": [
</span><span class="cx" style="display: block; padding: 0 10px"> "post",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- "term"
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "term",
+ "post-format"
</ins><span class="cx" style="display: block; padding: 0 10px"> ],
</span><span class="cx" style="display: block; padding: 0 10px"> "description": "Limit results to items of an object type.",
</span><span class="cx" style="display: block; padding: 0 10px"> "type": "string"
</span></span></pre>
</div>
</div>
</body>
</html>