Skip to content

Commit

Permalink
Code Modernization: Fix null to non-nullable deprecations in `wp_xmlr…
Browse files Browse the repository at this point in the history
…pc_server::mw_newPost()`.

The `wp_xmlrpc_server::mw_newPost()` method creates a new post via `wp_insert_post()`, but 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.

This commit:
* Ensures that all arguments are defined before they are `compact()`'ed together to the arguments array.
* Verifies that the default/fallback value of the arguments as set within the `wp_xmlrpc_server::mw_newPost()` method are the same as the default/fallback values used in the `wp_insert_post()` function.
* Verifies that arguments which do not have a default/fallback value defined in the `wp_insert_post()` function are handled correctly.
 * This was not the case for `$post_name`, which would previously already get an empty string default value in the `wp_xmlrpc_server::mw_newPost()` function, but then in the `wp_insert_post()` function, this would prevent the slug generation from being activated. Fixed now by setting the default in the `wp_xmlrpc_server::mw_newPost()` function to `null`.
 * The `page_template` argument was handled, but not documented in the `wp_insert_post()` function. The argument is now documented in the `wp_insert_post()` function DocBlock. Note: There are more than likely several other potential arguments missing from that list, but verifying the whole list is outside the scope of this particular commit.

Includes minor simplifications, such as:
* Setting a default ahead of an `if`, instead of in an `else` clause (as long as no function call is needed to set the default).
* Removing the unnecessary logic duplication in the `$post_status` switch.
* Using a combined concatenation + assignment operator for adding `$post_more`.

Fixes various errors along the lines of:
{{{
1) Tests_XMLRPC_mw_editPost::test_draft_not_prematurely_published
strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated

/var/www/src/wp-includes/formatting.php:2497
/var/www/src/wp-includes/class-wp-hook.php:308
/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:5616
/var/www/tests/phpunit/tests/xmlrpc/mw/editPost.php:315

...

23) Tests_XMLRPC_mw_editPost::test_draft_not_prematurely_published
json_decode(): Passing null to parameter #1 ($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:5615
/var/www/tests/phpunit/tests/xmlrpc/mw/editPost.php:315
/var/www/vendor/bin/phpunit:123
}}}

Follow-up to [1563], [4793], [7900], [16824], [19848], [40677], [51968].

Props jrf.
See #55656.

git-svn-id: https://develop.svn.wordpress.org/trunk@54320 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Sep 27, 2022
1 parent 5eddc8f commit 1b9bbba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
40 changes: 18 additions & 22 deletions src/wp-includes/class-wp-xmlrpc-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5399,30 +5399,27 @@ public function mw_newPost( $args ) {

// Let WordPress generate the 'post_name' (slug) unless
// one has been provided.
$post_name = '';
$post_name = null;
if ( isset( $content_struct['wp_slug'] ) ) {
$post_name = $content_struct['wp_slug'];
}

// Only use a password if one was given.
$post_password = '';
if ( isset( $content_struct['wp_password'] ) ) {
$post_password = $content_struct['wp_password'];
} else {
$post_password = '';
}

// Only set a post parent if one was given.
$post_parent = 0;
if ( isset( $content_struct['wp_page_parent_id'] ) ) {
$post_parent = $content_struct['wp_page_parent_id'];
} else {
$post_parent = 0;
}

// Only set the 'menu_order' if it was given.
$menu_order = 0;
if ( isset( $content_struct['wp_page_order'] ) ) {
$menu_order = $content_struct['wp_page_order'];
} else {
$menu_order = 0;
}

$post_author = $user->ID;
Expand Down Expand Up @@ -5450,8 +5447,8 @@ public function mw_newPost( $args ) {
$post_author = $content_struct['wp_author_id'];
}

$post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
$post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
$post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : '';
$post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : '';

$post_status = $publish ? 'publish' : 'draft';

Expand All @@ -5464,15 +5461,15 @@ public function mw_newPost( $args ) {
$post_status = $content_struct[ "{$post_type}_status" ];
break;
default:
$post_status = $publish ? 'publish' : 'draft';
// Deliberably left empty.
break;
}
}

$post_excerpt = isset( $content_struct['mt_excerpt'] ) ? $content_struct['mt_excerpt'] : null;
$post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
$post_excerpt = isset( $content_struct['mt_excerpt'] ) ? $content_struct['mt_excerpt'] : '';
$post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : '';

$tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
$tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : array();

if ( isset( $content_struct['mt_allow_comments'] ) ) {
if ( ! is_numeric( $content_struct['mt_allow_comments'] ) ) {
Expand Down Expand Up @@ -5536,10 +5533,10 @@ public function mw_newPost( $args ) {
}

if ( $post_more ) {
$post_content = $post_content . '<!--more-->' . $post_more;
$post_content .= '<!--more-->' . $post_more;
}

$to_ping = null;
$to_ping = '';
if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
$to_ping = $content_struct['mt_tb_ping_urls'];
if ( is_array( $to_ping ) ) {
Expand All @@ -5555,12 +5552,11 @@ public function mw_newPost( $args ) {
$dateCreated = $content_struct['dateCreated']->getIso();
}

$post_date = '';
$post_date_gmt = '';
if ( ! empty( $dateCreated ) ) {
$post_date = iso8601_to_datetime( $dateCreated );
$post_date_gmt = iso8601_to_datetime( $dateCreated, 'gmt' );
} else {
$post_date = '';
$post_date_gmt = '';
}

$post_category = array();
Expand Down Expand Up @@ -5786,7 +5782,7 @@ public function mw_editPost( $args ) {
$menu_order = $content_struct['wp_page_order'];
}

$page_template = null;
$page_template = '';
if ( ! empty( $content_struct['wp_page_template'] ) && 'page' === $post_type ) {
$page_template = $content_struct['wp_page_template'];
}
Expand Down Expand Up @@ -5894,7 +5890,7 @@ public function mw_editPost( $args ) {
$post_excerpt = $content_struct['mt_excerpt'];
}

$post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
$post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : '';

$post_status = $publish ? 'publish' : 'draft';
if ( isset( $content_struct[ "{$post_type}_status" ] ) ) {
Expand All @@ -5911,7 +5907,7 @@ public function mw_editPost( $args ) {
}
}

$tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
$tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : array();

if ( 'publish' === $post_status || 'private' === $post_status ) {
if ( 'page' === $post_type && ! current_user_can( 'publish_pages' ) ) {
Expand All @@ -5925,7 +5921,7 @@ public function mw_editPost( $args ) {
$post_content = $post_content . '<!--more-->' . $post_more;
}

$to_ping = null;
$to_ping = '';
if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
$to_ping = $content_struct['mt_tb_ping_urls'];
if ( is_array( $to_ping ) ) {
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -4034,6 +4034,7 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
* child terms can have the same names with different parent terms,
* so the only way to connect them is using ID. Default empty.
* @type array $meta_input Array of post meta values keyed by their post meta key. Default empty.
* @type string $page_template Page template to use.
* }
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false.
* @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.
Expand Down

0 comments on commit 1b9bbba

Please sign in to comment.