[wp-trac] [WordPress Trac] #34525: Use wp_parse_id_list() in the WP_Query class
WordPress Trac
noreply at wordpress.org
Sat Oct 31 11:57:38 UTC 2015
#34525: Use wp_parse_id_list() in the WP_Query class
-------------------------+-----------------------------
Reporter: birgire | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Query | Version: 4.3.1
Severity: normal | Keywords:
Focuses: |
-------------------------+-----------------------------
We have this handy core function to clean up an array, comma- or space
separated list of IDs:
{{{
function wp_parse_id_list( $list ) {
if ( !is_array($list) )
$list = preg_split('/[\s,]+/', $list);
return array_unique(array_map('absint', $list));
}
}}}
Why not use it where we can?
== Examples: ==
Instead of the following lines, in the {{{WP_Query}}} class:
{{{
$q['category__in'] = array_map( 'absint', array_unique( (array)
$q['category__in'] ) );
$q['category__not_in'] = array_map( 'absint', array_unique(
(array) $q['category__not_in'] ) );
$q['category__and'] = array_map( 'absint', array_unique( (array)
$q['category__and'] ) );
$q['tag__in'] = array_map('absint', array_unique( (array)
$q['tag__in'] ) );
$q['tag__not_in'] = array_map('absint', array_unique( (array)
$q['tag__not_in'] ) );
$q['tag__and'] = array_map('absint', array_unique( (array)
$q['tag__and'] ) );
}}}
we could simplify it with:
{{{
$q['category__in'] = wp_parse_id_list( $q['category__in'] );
$q['category__not_in'] = wp_parse_id_list( $q['category__not_in']
);
$q['category__and'] = wp_parse_id_list( $q['category__and'] );
$q['tag__in'] = wp_parse_id_list( $q['tag__in'] );
$q['tag__not_in'] = wp_parse_id_list( $q['tag__not_in'] );
$q['tag__and'] = wp_parse_id_list( $q['tag__and'] );
}}}
Similarly for:
{{{
$author__not_in = implode( ',', array_map( 'absint', array_unique(
(array) $q['author__not_in'] ) ) );
$author__in = implode( ',', array_map( 'absint', array_unique(
(array) $q['author__in'] ) ) );
}}}
we could use:
{{{
$author__not_in = implode( ',', wp_parse_id_list(
$q['author__not_in'] ) );
$author__in = implode( ',', wp_parse_id_list( $q['author__in'] )
);
}}}
I will check it further and soon add the relevant patches.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/34525>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list