Make WordPress Core

09/27/2022 02:16:28 AM (20 months ago)

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 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.


  • 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 #1 ($json) of type string is deprecated


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

Props jrf.
See #55656.

1 edited


  • trunk/tests/phpunit/tests/date/xmlrpc.php

    r54217 r54321  
    2525     */
    2626    public function test_date_new_post() {
    27         if ( PHP_VERSION_ID >= 80100 ) {
    28             /*
    29              * For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
    30              * via hooked in filter functions until a more structural solution to the
    31              * "missing input validation" conundrum has been architected and implemented.
    32              */
    33             $this->expectDeprecation();
    34             $this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
    35         }
    3727        $timezone = 'Europe/Helsinki';
    3828        update_option( 'timezone_string', $timezone );
Note: See TracChangeset for help on using the changeset viewer.