Skip to content

Commit 3777400

Browse files
committed
PHP 8.1 | wp_xmlrpc_server::mw_newPost(): fix passing null to non-nullable and more
The `wp_xmlrpc_server::mw_newPost()` method creates a new post via `wp_insert_post()`, but the default/fall-back values used in the function were not in line with the default/fall-back 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 would be `null`, this would overwrite the default value (see: https://3v4l.org/bfVlv ) and will lead to "passing null to non-nullable" deprecation notices on PHP 8.1 for certain arguments. For this commit, I've: * Ensured that all arguments are defined before they are `compact()`-ed together to the arguments array. * Verified that the default/fall-back value of the arguments as set within the `wp_xmlrpc_server::mw_newPost()` method are the same as the default/fall-back values used in the `wp_insert_post()` function. * Verified that arguments which do not have a default/fall-back 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. I've now documented the argument 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). * Remove 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 ```
1 parent 60dfe0b commit 3777400

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

src/wp-includes/class-wp-xmlrpc-server.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5399,30 +5399,27 @@ public function mw_newPost( $args ) {
53995399

54005400
// Let WordPress generate the 'post_name' (slug) unless
54015401
// one has been provided.
5402-
$post_name = '';
5402+
$post_name = null;
54035403
if ( isset( $content_struct['wp_slug'] ) ) {
54045404
$post_name = $content_struct['wp_slug'];
54055405
}
54065406

54075407
// Only use a password if one was given.
5408+
$post_password = '';
54085409
if ( isset( $content_struct['wp_password'] ) ) {
54095410
$post_password = $content_struct['wp_password'];
5410-
} else {
5411-
$post_password = '';
54125411
}
54135412

54145413
// Only set a post parent if one was given.
5414+
$post_parent = 0;
54155415
if ( isset( $content_struct['wp_page_parent_id'] ) ) {
54165416
$post_parent = $content_struct['wp_page_parent_id'];
5417-
} else {
5418-
$post_parent = 0;
54195417
}
54205418

54215419
// Only set the 'menu_order' if it was given.
5420+
$menu_order = 0;
54225421
if ( isset( $content_struct['wp_page_order'] ) ) {
54235422
$menu_order = $content_struct['wp_page_order'];
5424-
} else {
5425-
$menu_order = 0;
54265423
}
54275424

54285425
$post_author = $user->ID;
@@ -5450,8 +5447,8 @@ public function mw_newPost( $args ) {
54505447
$post_author = $content_struct['wp_author_id'];
54515448
}
54525449

5453-
$post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
5454-
$post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
5450+
$post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : '';
5451+
$post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : '';
54555452

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

@@ -5464,15 +5461,15 @@ public function mw_newPost( $args ) {
54645461
$post_status = $content_struct[ "{$post_type}_status" ];
54655462
break;
54665463
default:
5467-
$post_status = $publish ? 'publish' : 'draft';
5464+
// Deliberably left empty.
54685465
break;
54695466
}
54705467
}
54715468

5472-
$post_excerpt = isset( $content_struct['mt_excerpt'] ) ? $content_struct['mt_excerpt'] : null;
5473-
$post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
5469+
$post_excerpt = isset( $content_struct['mt_excerpt'] ) ? $content_struct['mt_excerpt'] : '';
5470+
$post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : '';
54745471

5475-
$tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
5472+
$tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : array();
54765473

54775474
if ( isset( $content_struct['mt_allow_comments'] ) ) {
54785475
if ( ! is_numeric( $content_struct['mt_allow_comments'] ) ) {
@@ -5536,10 +5533,10 @@ public function mw_newPost( $args ) {
55365533
}
55375534

55385535
if ( $post_more ) {
5539-
$post_content = $post_content . '<!--more-->' . $post_more;
5536+
$post_content .= '<!--more-->' . $post_more;
55405537
}
55415538

5542-
$to_ping = null;
5539+
$to_ping = '';
55435540
if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
55445541
$to_ping = $content_struct['mt_tb_ping_urls'];
55455542
if ( is_array( $to_ping ) ) {
@@ -5555,12 +5552,11 @@ public function mw_newPost( $args ) {
55555552
$dateCreated = $content_struct['dateCreated']->getIso();
55565553
}
55575554

5555+
$post_date = '';
5556+
$post_date_gmt = '';
55585557
if ( ! empty( $dateCreated ) ) {
55595558
$post_date = iso8601_to_datetime( $dateCreated );
55605559
$post_date_gmt = iso8601_to_datetime( $dateCreated, 'gmt' );
5561-
} else {
5562-
$post_date = '';
5563-
$post_date_gmt = '';
55645560
}
55655561

55665562
$post_category = array();

src/wp-includes/post.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4024,6 +4024,7 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
40244024
* child terms can have the same names with different parent terms,
40254025
* so the only way to connect them is using ID. Default empty.
40264026
* @type array $meta_input Array of post meta values keyed by their post meta key. Default empty.
4027+
* @type string $page_template Page template to use.
40274028
* }
40284029
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false.
40294030
* @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.

0 commit comments

Comments
 (0)