Skip to content

Commit 1b9bbba

Browse files
Code Modernization: Fix null to non-nullable deprecations in wp_xmlrpc_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
1 parent 5eddc8f commit 1b9bbba

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

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

Lines changed: 18 additions & 22 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();
@@ -5786,7 +5782,7 @@ public function mw_editPost( $args ) {
57865782
$menu_order = $content_struct['wp_page_order'];
57875783
}
57885784

5789-
$page_template = null;
5785+
$page_template = '';
57905786
if ( ! empty( $content_struct['wp_page_template'] ) && 'page' === $post_type ) {
57915787
$page_template = $content_struct['wp_page_template'];
57925788
}
@@ -5894,7 +5890,7 @@ public function mw_editPost( $args ) {
58945890
$post_excerpt = $content_struct['mt_excerpt'];
58955891
}
58965892

5897-
$post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
5893+
$post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : '';
58985894

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

5914-
$tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
5910+
$tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : array();
59155911

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

5928-
$to_ping = null;
5924+
$to_ping = '';
59295925
if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
59305926
$to_ping = $content_struct['mt_tb_ping_urls'];
59315927
if ( is_array( $to_ping ) ) {

src/wp-includes/post.php

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

0 commit comments

Comments
 (0)