Opened 8 years ago
Last modified 7 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: |
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)
Change History (16)
This ticket was mentioned in Slack in #core-multisite by flixos90. View the logs.
7 years ago
#6
@
7 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.
This ticket was mentioned in Slack in #core-multisite by birgire. View the logs.
7 years ago
This ticket was mentioned in Slack in #core-multisite by desrosj. View the logs.
7 years ago
#10
@
7 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.
#11
@
7 years ago
Thanks for the follow up @desrosj
In 37937.4.patch:
- Added
true, 'true', 1, '1'
andfalse, '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.
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.