WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 months ago

#38825 closed defect (bug) (worksforme)

wp_get_post_revisions default $args value

Reported by: natostanco Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.6
Component: Revisions Keywords: reporter-feedback close
Focuses: Cc:

Description

function wp_get_post_revisions( $post_id = 0, $args = null ) {

wp-includes/revision.php
$args should be array() or [], not null, otherwise array merge returns null every time $args is null.

Change History (4)

#1 follow-up: @ocean90
4 years ago

  • Keywords reporter-feedback added
  • Version changed from trunk to 2.6

Hello @natostanco, do you have some code which is currently failing for you? Because of the wp_parse_args() call $args should never be null.

<?php
$args     = null;
$defaults = [ 'foo' => 'bar' ];
$args     = wp_parse_args( $args, $defaults );
var_dump( $args ); // array(1) { ["foo"]=> string(3) "bar" }

Introduced in [7987].

#2 @adamsilverstein
4 years ago

  • Keywords close added

I tested wp_get_post_revisions passing null (or no $args) and everything worked as expected.

https://cl.ly/3W3s2j38163T/1._adamsilversteinAdams-MacBook-Pro-2_develop.svn.wordpress.orgtrunksrc_zsh_2016-11-16_10-53-38.jpg

#3 in reply to: ↑ 1 @natostanco
4 years ago

Replying to ocean90:

Hello @natostanco, do you have some code which is currently failing for you? Because of the wp_parse_args() call $args should never be null.

<?php
$args     = null;
$defaults = [ 'foo' => 'bar' ];
$args     = wp_parse_args( $args, $defaults );
var_dump( $args ); // array(1) { ["foo"]=> string(3) "bar" }

Introduced in [7987].

yes wp_parse_args() should return not null, this is another case of null =/= ''. If $args is null parse_str() does not touch $r which is then left undefined/null, then array_merge fails. For pars_str() to work and set $r to empty array $args must be empty string '', not null

#4 @hellofromTonya
3 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

I was unable to reproduce the reported issue. As it's marked as a close candidate, I'm closing it. Why?

Let's talk about what's happening in the code.

When invoking wp_get_post_revisions without passing the 2nd argument, $args is set to null within the function. This is its initial state.

function wp_get_post_revisions( $post_id = 0, $args = null ) {

When null is passed to wp_parse_args, the $defaults array is returned as demonstrated here.

$defaults = array(
        'order'         => 'DESC',
        'orderby'       => 'date ID',
        'check_enabled' => true,
);
$args     = wp_parse_args( $args, $defaults );

After the above code, $args is set to the default array and is no longer null.

When the code gets to the array_merge, $args is an array and not null.

What could be happening?

There's a filter in wp_parse_str():

function wp_parse_str( $string, &$array ) {
        parse_str( $string, $array );

        /**
         * Filters the array of variables derived from a parsed string.
         *
         * @since 2.3.0
         *
         * @param array $array The array populated with variables.
         */
        $array = apply_filters( 'wp_parse_str', $array );
}

A callback may be incorrectly running null instead of an array. Searching core, there is not a callback registered to this hook.

I suspect there's a callback in a plugin, must use script, or theme that is returning null to cause the problem @natostanco is reporting.

Note: See TracTickets for help on using tickets.