Commit c86189f6 authored by John James Jacoby's avatar John James Jacoby
Browse files

Simplify attributes_query idea. Needs work, though.

parent eff63821
......@@ -608,7 +608,6 @@ final class WP_User_Attributes {
// Make `attribute_query` arguments work
add_filter( 'pre_user_query', array( $this, 'pre_user_query' ), 10, 3 );
add_filter( 'get_users_args', array( $this, 'get_users_args' ), -999 );
// Filter user-insert metas
add_filter( 'insert_user_meta', array( $this, 'insert_user_meta' ), 10, 2 );
......@@ -906,62 +905,39 @@ final class WP_User_Attributes {
/** User Attributes Query *************************************************/
/**
* Filter `pre_get_users` and add an empty `attribute_query` argument.
*
* This is mostly a dumb hack to ensure that `attribute_query` starts as an
* available argument in the `$args` array, to get developers familiar with
* it eventually maybe possibly being available all of the time.
*
* If we're being honest with each other, this method isn't even really
* necessary. It's just me being pedantic about environment variables, and
* hoping that if someone else comes along and sees how much care I took to
* be this thorough, they'll say "hey good job" or "that was really cool
* that you did that thing that no one else would have thought to do."
*
* @since 0.1.0
*
* @param array $args An array of arguments.
*
* @return array Array of arguments with `attribute_query` parameter added
*/
public function get_users_args( $args = array() ) {
return wp_parse_args( $args, array(
'attribute_query' => ''
) );
}
/**
* Filter `pre_user_query` and add support for an `attribute_query` argument
*
* @since 0.1.0
*
* @param array $pieces Users query SQL clauses
* @param array $query Users query SQL clauses
*
* @return Array of query pieces, maybe modifed
*/
public function pre_user_query( $pieces = array(), $args = array() ) {
public function pre_user_query( $query = array() ) {
// Maybe attribute query
if ( ! empty( $args['attribute_query'] ) ) {
if ( ! empty( $query->query_vars['attribute_query'] ) ) {
// Make doubly sure global database object is prepared
$this->add_table_to_db_object();
// Get the query parts
$attr_query = new WP_Meta_Query( $args['attribute_query'] );
$attr_query->parse_query_vars( $args );
$attr_query = new WP_Meta_Query( $query->query_vars['attribute_query'] );
$attr_query->meta_table = $this->db->user_attributes;
$attr_query->meta_id_column = 'attributes_id';
$attr_query->parse_query_vars( $query->query_vars['attribute_query'] );
// Combine pieces & query clauses
if ( ! empty( $attr_query->queries ) ) {
$attr_clauses = $attr_query->get_sql( 'user', 'user_attributes', 'user_id', $pieces );
$pieces['join'] .= $attr_clauses['join'];
$pieces['where'] .= $attr_clauses['where'];
$attr_clauses = $attr_query->get_sql( 'user', 'user_attributes', 'user_id', $query );
$query['join'] .= $attr_clauses['join'];
$query['where'] .= $attr_clauses['where'];
}
}
// Return possibly modified pieces array
return $pieces;
return $query;
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment