<!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>[54321] trunk: Code Modernization: Fix null to non-nullable deprecations in `wp_xmlrpc_server::_insert_post()`.</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/54321">54321</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/54321","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-09-27 02:16:28 +0000 (Tue, 27 Sep 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'>Code Modernization: Fix null to non-nullable deprecations in `wp_xmlrpc_server::_insert_post()`.
The `wp_xmlrpc_server::_insert_post()` method creates a new post via `wp_insert_post()` or updates an existing one via `wp_update_post()`, which subsequently calls `wp_insert_post()`. However, the default/fallback values used in the function were not in line with the default/fallback values used in the `wp_insert_post()` function.
The `wp_insert_post()` function does a `wp_parse_args()` (array merge) of the received arguments with the defaults. If any of the received arguments are `null`, this would overwrite the default value, as seen in [https://3v4l.org/bfVlv array_merge() example], and lead to "passing null to non-nullable" deprecation notices on PHP 8.1 for certain arguments.
Unfortunately, the conditional logic within the `wp_xmlrpc_server::_insert_post()` function itself often uses an `isset()` to trigger certain code blocks, so syncing the defaults with those used in the `wp_insert_post()` function was not an option.
This commit:
* Updates the default/fallback values in the `$defaults` array only for those values where this would not lead to a change in the behavior of the function.
* Adds a safeguard function, filtering out all remaining `null` values from the `$post_data` array before it is passed on to the `wp_insert_post()` or `wp_update_post()` functions. Removing those values is safe as this means that these array keys will now:
* either be set to the default/fallback value as defined in `wp_insert_post()`.
* or not be set and for those values which don't have a default/fallback value in `wp_insert_post()`, the function does an `! empty()` or `isset()` check anyway and those array keys not being defined means that the result of those checks will remain the same.
Includes
* Removing a couple of conditions which are now redundant.
* Removing an `expectDeprecation()` in the `Tests_Date_XMLRPC` test class, which is now no longer needed.
Fixes various errors along the lines of:
{{{
36) Tests_XMLRPC_wp_newPost::test_no_content
json_decode(): Passing null to parameter <a href="https://core.trac.wordpress.org/ticket/1">#1</a> ($json) of type string is deprecated
/var/www/src/wp-includes/kses.php:2074
/var/www/src/wp-includes/class-wp-hook.php:307
/var/www/src/wp-includes/plugin.php:205
/var/www/src/wp-includes/post.php:2835
/var/www/src/wp-includes/post.php:2720
/var/www/src/wp-includes/post.php:4066
/var/www/src/wp-includes/class-wp-xmlrpc-server.php:1683
/var/www/src/wp-includes/class-wp-xmlrpc-server.php:1347
/var/www/tests/phpunit/tests/xmlrpc/wp/newPost.php:25
/var/www/vendor/bin/phpunit:123
}}}
Follow-up to <a href="https://core.trac.wordpress.org/changeset/1563">[1563]</a>, <a href="https://core.trac.wordpress.org/changeset/4793">[4793]</a>, <a href="https://core.trac.wordpress.org/changeset/7900">[7900]</a>, <a href="https://core.trac.wordpress.org/changeset/16824">[16824]</a>, <a href="https://core.trac.wordpress.org/changeset/19848">[19848]</a>, <a href="https://core.trac.wordpress.org/changeset/19873">[19873]</a>, <a href="https://core.trac.wordpress.org/changeset/20632">[20632]</a>, <a href="https://core.trac.wordpress.org/changeset/40677">[40677]</a>, <a href="https://core.trac.wordpress.org/changeset/51968">[51968]</a>, <a href="https://core.trac.wordpress.org/changeset/54320">[54320]</a>.
Props jrf.
See <a href="https://core.trac.wordpress.org/ticket/55656">#55656</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpxmlrpcserverphp">trunk/src/wp-includes/class-wp-xmlrpc-server.php</a></li>
<li><a href="#trunktestsphpunittestsdatexmlrpcphp">trunk/tests/phpunit/tests/date/xmlrpc.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpxmlrpcserverphp"></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-xmlrpc-server.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-xmlrpc-server.php 2022-09-27 01:58:43 UTC (rev 54320)
+++ trunk/src/wp-includes/class-wp-xmlrpc-server.php 2022-09-27 02:16:28 UTC (rev 54321)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1412,19 +1412,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $defaults = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_status' => 'draft',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_type' => 'post',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_author' => null,
- 'post_password' => null,
- 'post_excerpt' => null,
- 'post_content' => null,
- 'post_title' => null,
- 'post_date' => null,
- 'post_date_gmt' => null,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_author' => 0,
+ 'post_password' => '',
+ 'post_excerpt' => '',
+ 'post_content' => '',
+ 'post_title' => '',
+ 'post_date' => '',
+ 'post_date_gmt' => '',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'post_format' => null,
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_name' => null,
</span><span class="cx" style="display: block; padding: 0 10px"> 'post_thumbnail' => null,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'post_parent' => null,
- 'ping_status' => null,
- 'comment_status' => null,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'post_parent' => 0,
+ 'ping_status' => '',
+ 'comment_status' => '',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'custom_fields' => null,
</span><span class="cx" style="display: block; padding: 0 10px"> 'terms_names' => null,
</span><span class="cx" style="display: block; padding: 0 10px"> 'terms' => null,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1499,11 +1499,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $post_data['post_author'] = $user->ID;
</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">- if ( isset( $post_data['comment_status'] ) && 'open' !== $post_data['comment_status'] && 'closed' !== $post_data['comment_status'] ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'open' !== $post_data['comment_status'] && 'closed' !== $post_data['comment_status'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> unset( $post_data['comment_status'] );
</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">- if ( isset( $post_data['ping_status'] ) && 'open' !== $post_data['ping_status'] && 'closed' !== $post_data['ping_status'] ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'open' !== $post_data['ping_status'] && 'closed' !== $post_data['ping_status'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> unset( $post_data['ping_status'] );
</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">@@ -1681,6 +1681,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Remove all null values to allow for using the insert/update post default values for those keys instead.
+ $post_data = array_filter(
+ $post_data,
+ static function ( $value ) {
+ return null !== $value;
+ }
+ );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $post_ID ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new IXR_Error( 500, $post_ID->get_error_message() );
</span></span></pre></div>
<a id="trunktestsphpunittestsdatexmlrpcphp"></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/date/xmlrpc.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/date/xmlrpc.php 2022-09-27 01:58:43 UTC (rev 54320)
+++ trunk/tests/phpunit/tests/date/xmlrpc.php 2022-09-27 02:16:28 UTC (rev 54321)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -24,16 +24,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers wp_xmlrpc_server::mw_newPost
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_date_new_post() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( PHP_VERSION_ID >= 80100 ) {
- /*
- * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
- * via hooked in filter functions until a more structural solution to the
- * "missing input validation" conundrum has been architected and implemented.
- */
- $this->expectDeprecation();
- $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> $timezone = 'Europe/Helsinki';
</span><span class="cx" style="display: block; padding: 0 10px"> update_option( 'timezone_string', $timezone );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre>
</div>
</div>
</body>
</html>