[wp-trac] [WordPress Trac] #37937: Support boolean strings for the 'public', 'archived', 'mature', 'spam' and 'deleted' attributes in WP_Site_Query

WordPress Trac noreply at wordpress.org
Sat Sep 3 16:29:53 UTC 2016


#37937: Support boolean strings for the 'public', 'archived', 'mature', 'spam' and
'deleted' attributes in WP_Site_Query
--------------------------------+-----------------------------
 Reporter:  birgire             |      Owner:
     Type:  enhancement         |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  Networks and Sites  |    Version:  trunk
 Severity:  normal              |   Keywords:
  Focuses:  multisite           |
--------------------------------+-----------------------------
 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}}}).

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37937>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list