<!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>[44546] trunk: General: Fix problematic string to array parsing.</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/44546">44546</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/44546","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-01-10 21:05:50 +0000 (Thu, 10 Jan 2019)</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'>General: Fix problematic string to array parsing.
WordPress has historically often used code like `preg_split( '/[\s,]+/', $var )` to parse a string of comma-separated values into an array. However, this approach was causing an empty string to not be parsed into an empty array as expected, but rather into an array with the empty string as its sole element.
This was among other areas causing problems in the REST API where passing an empty request parameter could cause that request to fail because, instead of it being ignored, that parameter would be compared against the valid values for it, which typically do not include an empty string.
Props david.binda, sstoqnov.
Fixes <a href="https://core.trac.wordpress.org/ticket/43977">#43977</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesbookmarkphp">trunk/src/wp-includes/bookmark.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcommentqueryphp">trunk/src/wp-includes/class-wp-comment-query.php</a></li>
<li><a href="#trunksrcwpincludesfunctionsphp">trunk/src/wp-includes/functions.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestcontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunktestsphpunittestsbookmarkgetBookmarksphp">trunk/tests/phpunit/tests/bookmark/getBookmarks.php</a></li>
<li><a href="#trunktestsphpunittestsfunctionsphp">trunk/tests/phpunit/tests/functions.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestcontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemasanitizationphp">trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemavalidationphp">trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesbookmarkphp"></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/bookmark.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/bookmark.php 2019-01-10 17:20:59 UTC (rev 44545)
+++ trunk/src/wp-includes/bookmark.php 2019-01-10 21:05:50 UTC (rev 44546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -171,13 +171,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $r['exclude'] = ''; //ignore exclude, category, and category_name params if using include
</span><span class="cx" style="display: block; padding: 0 10px"> $r['category'] = '';
</span><span class="cx" style="display: block; padding: 0 10px"> $r['category_name'] = '';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $inclinks = preg_split( '/[\s,]+/', $r['include'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $inclinks = wp_parse_id_list( $r['include'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( count( $inclinks ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $inclinks as $inclink ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $inclusions ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $inclusions = ' AND ( link_id = ' . intval( $inclink ) . ' ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $inclusions = ' AND ( link_id = ' . $inclink . ' ';
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $inclusions .= ' OR link_id = ' . intval( $inclink ) . ' ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $inclusions .= ' OR link_id = ' . $inclink . ' ';
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -188,13 +188,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $exclusions = '';
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $r['exclude'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $exlinks = preg_split( '/[\s,]+/', $r['exclude'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $exlinks = wp_parse_id_list( $r['exclude'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( count( $exlinks ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $exlinks as $exlink ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $exclusions ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $exclusions = ' AND ( link_id <> ' . intval( $exlink ) . ' ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $exclusions = ' AND ( link_id <> ' . $exlink . ' ';
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $exclusions .= ' AND link_id <> ' . intval( $exlink ) . ' ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $exclusions .= ' AND link_id <> ' . $exlink . ' ';
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -223,13 +223,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $category_query = '';
</span><span class="cx" style="display: block; padding: 0 10px"> $join = '';
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $r['category'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $incategories = preg_split( '/[\s,]+/', $r['category'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $incategories = wp_parse_id_list( $r['category'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( count( $incategories ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $incategories as $incat ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $category_query ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $category_query = ' AND ( tt.term_id = ' . intval( $incat ) . ' ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $category_query = ' AND ( tt.term_id = ' . $incat . ' ';
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $category_query .= ' OR tt.term_id = ' . intval( $incat ) . ' ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $category_query .= ' OR tt.term_id = ' . $incat . ' ';
</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="trunksrcwpincludesclasswpcommentqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-comment-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-comment-query.php 2019-01-10 17:20:59 UTC (rev 44545)
+++ trunk/src/wp-includes/class-wp-comment-query.php 2019-01-10 21:05:50 UTC (rev 44546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -482,9 +482,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // 'status' accepts an array or a comma-separated string.
</span><span class="cx" style="display: block; padding: 0 10px"> $status_clauses = array();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $statuses = $this->query_vars['status'];
- if ( ! is_array( $statuses ) ) {
- $statuses = preg_split( '/[\s,]+/', $statuses );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $statuses = wp_parse_list( $this->query_vars['status'] );
+
+ // Empty 'status' should be interpreted as 'all'.
+ if ( empty( $statuses ) ) {
+ $statuses = array( 'all' );
</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"> // 'any' overrides other statuses.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -517,14 +519,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // User IDs or emails whose unapproved comments are included, regardless of $status.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $this->query_vars['include_unapproved'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $include_unapproved = $this->query_vars['include_unapproved'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $include_unapproved = wp_parse_list( $this->query_vars['include_unapproved'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Accepts arrays or comma-separated strings.
- if ( ! is_array( $include_unapproved ) ) {
- $include_unapproved = preg_split( '/[\s,]+/', $include_unapproved );
- }
-
- $unapproved_ids = $unapproved_emails = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $unapproved_ids = array();
+ $unapproved_emails = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $include_unapproved as $unapproved_identifier ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // Numeric values are assumed to be user ids.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_numeric( $unapproved_identifier ) ) {
</span></span></pre></div>
<a id="trunksrcwpincludesfunctionsphp"></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/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/functions.php 2019-01-10 17:20:59 UTC (rev 44545)
+++ trunk/src/wp-includes/functions.php 2019-01-10 21:05:50 UTC (rev 44546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3823,6 +3823,22 @@
</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">+ * Cleans up an array, comma- or space-separated list of scalar values.
+ *
+ * @since 5.1.0
+ *
+ * @param array|string $list List of values.
+ * @return array Sanitized array of values.
+ */
+function wp_parse_list( $list ) {
+ if ( ! is_array( $list ) ) {
+ return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY );
+ }
+
+ return $list;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Clean up an array, comma- or space-separated list of IDs.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.0.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3831,9 +3847,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array Sanitized array of IDs.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_parse_id_list( $list ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! is_array( $list ) ) {
- $list = preg_split( '/[\s,]+/', $list );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $list = wp_parse_list( $list );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return array_unique( array_map( 'absint', $list ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3847,15 +3861,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array Sanitized array of slugs.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_parse_slug_list( $list ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! is_array( $list ) ) {
- $list = preg_split( '/[\s,]+/', $list );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $list = wp_parse_list( $list );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $list as $key => $value ) {
- $list[ $key ] = sanitize_title( $value );
- }
-
- return array_unique( $list );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return array_unique( array_map( 'sanitize_title', $list ) );
</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="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php 2019-01-10 17:20:59 UTC (rev 44545)
+++ trunk/src/wp-includes/post.php 2019-01-10 21:05:50 UTC (rev 44546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5088,7 +5088,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $author_query = '';
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $r['authors'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $post_authors = preg_split( '/[\s,]+/', $r['authors'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $post_authors = wp_parse_list( $r['authors'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $post_authors ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $post_authors as $post_author ) {
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestcontrollerphp"></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-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-controller.php 2019-01-10 17:20:59 UTC (rev 44545)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php 2019-01-10 21:05:50 UTC (rev 44546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -532,7 +532,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $request['_fields'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $fields;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $requested_fields = is_array( $request['_fields'] ) ? $request['_fields'] : preg_split( '/[\s,]+/', $request['_fields'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $requested_fields = wp_parse_list( $request['_fields'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( 0 === count( $requested_fields ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $fields;
</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 2019-01-10 17:20:59 UTC (rev 44545)
+++ trunk/src/wp-includes/rest-api.php 2019-01-10 21:05:50 UTC (rev 44546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -679,7 +679,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $fields = is_array( $request['_fields'] ) ? $request['_fields'] : preg_split( '/[\s,]+/', $request['_fields'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $fields = wp_parse_list( $request['_fields'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 0 === count( $fields ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $response;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1109,8 +1109,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function rest_validate_value_from_schema( $value, $args, $param = '' ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'array' === $args['type'] ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! is_array( $value ) ) {
- $value = preg_split( '/[\s,]+/', $value );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! is_null( $value ) ) {
+ $value = wp_parse_list( $value );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! wp_is_numeric_array( $value ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: 1: parameter, 2: type name */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1253,9 +1253,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $args['items'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return (array) $value;
</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 ( ! is_array( $value ) ) {
- $value = preg_split( '/[\s,]+/', $value );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $value = wp_parse_list( $value );
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $value as $index => $v ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsbookmarkgetBookmarksphp"></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/bookmark/getBookmarks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/bookmark/getBookmarks.php 2019-01-10 17:20:59 UTC (rev 44545)
+++ trunk/tests/phpunit/tests/bookmark/getBookmarks.php 2019-01-10 21:05:50 UTC (rev 44546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -84,4 +84,61 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEqualSets( $found1, $found2 );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( $num_queries < $wpdb->num_queries );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ public function test_exclude_param_gets_properly_parsed_as_list() {
+ $bookmarks = self::factory()->bookmark->create_many( 3 );
+
+ $found = get_bookmarks(
+ array(
+ 'exclude' => ',,',
+ )
+ );
+ $found_ids = array();
+ foreach( $found as $bookmark ) {
+ $found_ids[] = $bookmark->link_id;
+ }
+
+ // equal sets != same order.
+ $this->assertEqualSets( $bookmarks, $found_ids );
+ }
+
+ public function test_include_param_gets_properly_parsed_as_list() {
+ $bookmarks = self::factory()->bookmark->create_many( 3 );
+
+ $found = get_bookmarks(
+ array(
+ 'include' => ',,',
+ )
+ );
+ $found_ids = array();
+ foreach( $found as $bookmark ) {
+ $found_ids[] = $bookmark->link_id;
+ }
+
+ // equal sets != same order.
+ $this->assertEqualSets( $bookmarks, $found_ids );
+ }
+
+ public function test_category_param_propelry_gets_parsed_as_list() {
+ $bookmarks = self::factory()->bookmark->create_many( 3 );
+ $categories = self::factory()->term->create_many( 3, array(
+ 'taxonomy' => 'link_category',
+ ) );
+ $add = wp_add_object_terms( $bookmarks[0], $categories[0], 'link_category' );
+ $add = wp_add_object_terms( $bookmarks[1], $categories[1], 'link_category' );
+ $add = wp_add_object_terms( $bookmarks[2], $categories[2], 'link_category' );
+
+ $found = get_bookmarks(
+ array(
+ 'category' => ',,',
+ )
+ );
+ $found_ids = array();
+ foreach( $found as $bookmark ) {
+ $found_ids[] = $bookmark->link_id;
+ }
+
+ // equal sets != same order.
+ $this->assertEqualSets( $bookmarks, $found_ids );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsfunctionsphp"></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/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/functions.php 2019-01-10 17:20:59 UTC (rev 44545)
+++ trunk/tests/phpunit/tests/functions.php 2019-01-10 21:05:50 UTC (rev 44546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -535,6 +535,30 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 43977
+ * @dataProvider data_wp_parse_list
+ */
+ function test_wp_parse_list( $expected, $actual ) {
+ $this->assertSame( $expected, array_values( wp_parse_list( $actual ) ) );
+ }
+
+ function data_wp_parse_list() {
+ return array(
+ array( array( '1', '2', '3', '4' ), '1,2,3,4' ),
+ array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple,banana,carrot,dog' ),
+ array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,banana' ),
+ array( array( '1', '2', 'apple', 'banana' ), '1, 2,apple,banana' ),
+ array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,,banana' ),
+ array( array( '1', '2', 'apple', 'banana' ), ',1,2,apple,banana' ),
+ array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,banana,' ),
+ array( array( '1', '2', 'apple', 'banana' ), '1,2 ,apple,banana' ),
+ array( array(), '' ),
+ array( array(), ',' ),
+ array( array(), ',,' ),
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @dataProvider data_wp_parse_id_list
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function test_wp_parse_id_list( $expected, $actual ) {
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestcontrollerphp"></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-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-controller.php 2019-01-10 17:20:59 UTC (rev 44545)
+++ trunk/tests/phpunit/tests/rest-api/rest-controller.php 2019-01-10 21:05:50 UTC (rev 44546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -203,7 +203,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 'a', $args['somedefault']['default'] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_get_fields_for_response() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @dataProvider data_get_fields_for_response,
+ */
+ public function test_get_fields_for_response( $param, $expected ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $controller = new WP_REST_Test_Controller();
</span><span class="cx" style="display: block; padding: 0 10px"> $request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
</span><span class="cx" style="display: block; padding: 0 10px"> $fields = $controller->get_fields_for_response( $request );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -221,14 +224,34 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> $fields
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request->set_param( '_fields', 'somestring,someinteger' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request->set_param( '_fields', $param );
</ins><span class="cx" style="display: block; padding: 0 10px"> $fields = $controller->get_fields_for_response( $request );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( $expected, $fields );
+ }
+
+ public function data_get_fields_for_response() {
+ return array(
</ins><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'somestring',
- 'someinteger',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'somestring,someinteger',
+ array(
+ 'somestring',
+ 'someinteger',
+ ),
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $fields
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ array(
+ ',,',
+ array(
+ 'somestring',
+ 'someinteger',
+ 'someboolean',
+ 'someurl',
+ 'somedate',
+ 'someemail',
+ 'someenum',
+ 'someargoptions',
+ 'somedefault',
+ ),
+ ),
</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="trunktestsphpunittestsrestapirestschemasanitizationphp"></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-sanitization.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-sanitization.php 2019-01-10 17:20:59 UTC (rev 44545)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php 2019-01-10 21:05:50 UTC (rev 44546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -110,6 +110,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array( 1, 2 ), rest_sanitize_value_from_schema( '1,2', $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array( 1, 2, 0 ), rest_sanitize_value_from_schema( '1,2,a', $schema ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( array( 1, 2 ), rest_sanitize_value_from_schema( '1,2,', $schema ) );
</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"> public function test_type_array_with_enum() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -134,6 +135,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array( 'ribs', 'chicken' ), rest_sanitize_value_from_schema( 'ribs,chicken', $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array( 'chicken', 'coleslaw' ), rest_sanitize_value_from_schema( 'chicken,coleslaw', $schema ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( array( 'chicken', 'coleslaw' ), rest_sanitize_value_from_schema( 'chicken,coleslaw,', $schema ) );
</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"> public function test_type_array_is_associative() {
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestschemavalidationphp"></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-validation.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-validation.php 2019-01-10 17:20:59 UTC (rev 44545)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php 2019-01-10 21:05:50 UTC (rev 44546)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -120,6 +120,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( rest_validate_value_from_schema( array( 1 ), $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertWPError( rest_validate_value_from_schema( array( true ), $schema ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertWPError( rest_validate_value_from_schema( null, $schema ) );
</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"> public function test_type_array_nested() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -145,6 +146,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( rest_validate_value_from_schema( '1', $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( rest_validate_value_from_schema( '1,2,3', $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertWPError( rest_validate_value_from_schema( 'lol', $schema ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertTrue( rest_validate_value_from_schema( '1,,', $schema ) );
+ $this->assertTrue( rest_validate_value_from_schema( '', $schema ) );
</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"> public function test_type_array_with_enum() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -169,6 +172,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( rest_validate_value_from_schema( 'ribs,chicken', $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertWPError( rest_validate_value_from_schema( 'chicken,coleslaw', $schema ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertTrue( rest_validate_value_from_schema( 'ribs,chicken,', $schema ) );
+ $this->assertTrue( rest_validate_value_from_schema( '', $schema ) );
</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"> public function test_type_array_is_associative() {
</span></span></pre>
</div>
</div>
</body>
</html>