WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 2 years ago

#37937 new enhancement

Support boolean strings for the 'public', 'archived', 'mature', 'spam' and 'deleted' attributes in WP_Site_Query

Reported by: birgire Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 4.6
Component: Networks and Sites Keywords: has-patch has-unit-tests
Focuses: multisite Cc:
PR Number:

Description

According to the documentation for the WP_Site_Query class:

*     @type int          $public           Limit results to public sites. Accepts '1' or '0'. Default empty. 
*     @type int          $archived         Limit results to archived sites. Accepts '1' or '0'. Default empty. 
*     @type int          $mature           Limit results to mature sites. Accepts '1' or '0'. Default empty. 
*     @type int          $spam             Limit results to spam sites. Accepts '1' or '0'. Default empty. 
*     @type int          $deleted          Limit results to deleted sites. Accepts '1' or '0'. Default empty. 

these attributes accepts values '1' and '0' (also 1 and 0 to be more specific).

I would suggest adding support for general boolean values so these attributes can also support e.g. true, false, 'true', 'false' .

Currently each attribute is handled like this:

if ( is_numeric( $this->query_vars['archived'] ) ) { 
	$archived                               = absint( $this->query_vars['archived'] ); 
	$this->sql_clauses['where']['archived'] = $wpdb->prepare( "archived = %d ", $archived ); 
}

Here we see why e.g. true doesn't work, because is_numeric( true ) is false and the SQL modification is skipped.

I want to suggest using e.g. wp_validate_boolean() for the validation.

Here's an example:

if ( isset( $this->query_vars['archived'] ) ) { 
    $archived                               = wp_validate_boolean( $this->query_vars['archived'] ); 
    $this->sql_clauses['where']['archived'] = $wpdb->prepare( "archived = %d ", $archived ); 
} 

or we could be more specific with:

if ( isset( $this->query_vars['archived'] ) ) { 
    $archived                               = wp_validate_boolean( $this->query_vars['archived'] ) ? 1 : 0; 
    $this->sql_clauses['where']['archived'] = $wpdb->prepare( "archived = %d ", $archived ); 
} 

etc.

I added a patch as a starting example. There I added @type mixed to the documentation because that's how the input of wp_validate_boolean() is documented (it can be a general boolean value like int, bool or string).

Attachments (4)

class-wp-site-query.diff (3.7 KB) - added by birgire 3 years ago.
v2.diff (3.8 KB) - added by birgire 3 years ago.
Version #2
test.diff (2.6 KB) - added by birgire 3 years ago.
Test
37937.4.patch (8.4 KB) - added by birgire 2 years ago.

Download all attachments as: .zip

Change History (16)

#1 @birgire
3 years ago

  • Keywords has-patch added

This ticket was mentioned in Slack in #core-multisite by flixos90. View the logs.


3 years ago

#3 @desrosj
3 years ago

  • Keywords needs-refresh needs-unit-tests added

Thanks for the ticket @birgire! This was discussed in a recent multisite ticket scrub. The consensus was adding support for booleans was a good move. true/false strings could also be acceptable. We want to make sure we keep the code simple though.

The patch looks like it needs to be refreshed. It failed to apply cleanly to trunk for me. Would you mind refreshing the patch? I think we would also want to include some unit tests as well.

#4 @desrosj
3 years ago

  • Version changed from 4.7 to 4.6

Related #35791

#5 @birgire
3 years ago

@desrosj I will look into it

@birgire
3 years ago

Version #2

@birgire
3 years ago

Test

#6 @birgire
3 years ago

  • Keywords needs-unit-tests removed

I updated the patch with a ! is_null() checks because the defaults are null.

Added tests as well, for the 'public' input argument with the values true, 'true', false and 'false' based on the existing siteQueyry.php tests.

#7 @DrewAPicture
3 years ago

  • Keywords has-unit-tests added; needs-refresh removed

This ticket was mentioned in Slack in #core-multisite by birgire. View the logs.


2 years ago

This ticket was mentioned in Slack in #core-multisite by desrosj. View the logs.


2 years ago

#10 @desrosj
2 years ago

Following up on the discussion in today's ticket scrub. @birgire will combine the tests and fix patches into one. Also, some feedback on the patch.

  • The empty line between the inline documentation and the function in the tests needs to be removed.
  • The tests only test the public parameter. The others should be tested as well.

@birgire
2 years ago

#11 @birgire
2 years ago

Thanks for the follow up @desrosj

In 37937.4.patch:

  • Added true, 'true', 1, '1' and false, 'false', 0, '0' tests for all the parameters, i.e. 'public', 'archived', 'mature', 'spam' and 'deleted'.
  • Removed the extra empty lines.
  • Combined patch and tests into a single file.
Last edited 2 years ago by birgire (previous) (diff)

#12 @jeremyfelt
2 years ago

  • Milestone changed from Awaiting Review to Future Release
Note: See TracTickets for help on using tickets.