Make WordPress Core

Changeset 54320


Ignore:
Timestamp:
09/27/2022 01:58:43 AM (22 months ago)
Author:
SergeyBiryukov
Message:

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

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-xmlrpc-server.php

    r54133 r54320  
    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'];
     
    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
     
    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';
     
    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;
    5474 
    5475         $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : 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'] : '';
     5471
     5472        $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : array();
    54765473
    54775474        if ( isset( $content_struct['mt_allow_comments'] ) ) {
     
    55375534
    55385535        if ( $post_more ) {
    5539             $post_content = $post_content . '<!--more-->' . $post_more;
    5540         }
    5541 
    5542         $to_ping = null;
     5536            $post_content .= '<!--more-->' . $post_more;
     5537        }
     5538
     5539        $to_ping = '';
    55435540        if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
    55445541            $to_ping = $content_struct['mt_tb_ping_urls'];
     
    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
     
    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'];
     
    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';
     
    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 ) {
     
    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'];
  • trunk/src/wp-includes/post.php

    r54284 r54320  
    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.
Note: See TracChangeset for help on using the changeset viewer.