Make WordPress Core

Opened 12 years ago

Closed 11 years ago

#21119 closed enhancement (fixed)

Make WP_User_Query::prepare_query() public

Reported by: scribu's profile scribu Owned by: wonderboymusic's profile wonderboymusic
Milestone: 3.9 Priority: normal
Severity: normal Version:
Component: Users Keywords: has-patch commit dev-reviewed
Focuses: Cc:

Description

WP_User_Query tries to follow the pattern from WP_Query, where if you don't pass any args, it won't run the query.

The problem is that you can't do anything with that instance afterwards; both prepare_query() and query() accept no parameters.

Attachments (4)

21119.diff (2.4 KB) - added by scribu 12 years ago.
21119.2.diff (2.2 KB) - added by wonderboymusic 11 years ago.
21119.3.diff (3.6 KB) - added by wonderboymusic 11 years ago.
21119.4.diff (930 bytes) - added by nacin 11 years ago.

Download all attachments as: .zip

Change History (15)

@scribu
12 years ago

#1 @scribu
12 years ago

With the patch, it's now possible to do this:

// construct $args somehow

$uq = new WP_User_Query;

$uq->prepare_query( $args );

var_dump( $uq->query_where );

#2 @nacin
11 years ago

  • Component changed from General to Users
  • Milestone changed from Awaiting Review to 3.9

Looks good.

#3 @wonderboymusic
11 years ago

  • Keywords needs-unit-tests added

21119.2.diff updates the patch, which was blowing up against trunk. I also made changes so that it passes all unit tests. This class is awkward, may need more unit tests.

#4 @wonderboymusic
11 years ago

21119.3.diff adds unit tests for WP_User_Query::prepare_query() and demonstrates how it works. It only does something if WP_User_Query::query_vars is empty or an array of any options is passed. Otherwise, it rebuilds the object properties from the previously set query vars.

#5 @wonderboymusic
11 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 27185:

Make WP_User_Query::prepare_query() public by allowing it to be passed an array of args. Previously, if the WP_User_Query constructor was not passed args, the object was basically unusable. Adds unit tests, all other tests pass.

Props scribu, for the initial patch.
Fixes #21119.

#6 @nacin
11 years ago

#27741 was marked as a duplicate.

@nacin
11 years ago

#7 @nacin
11 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

As noted in #27741, these unit tests fail under multisite. Multisite does indeed have a quirk here as it relates to WP_User_Query::prepare_query(), and I don't think it's the test that's the problem, but rather, this new public method.

The final $query->prepare_query() call re-does all of the logic in prepare_query(), re-adding another meta query for is_multisite(). This makes this function not safe to be called a second time, and I believe the intention was that it should.

[meta_query] => Array(
	[0] => Array( [key] => wptests_capabilities )
	[1] => Array( [key] => wptests_capabilities )
)

On the bright side, this doesn't *appear* to break anything. We should just be able to just look for an existing duplicate meta query and discard adding a new one. Patch attached, 21119.4.diff. As it doesn't seem to break anything as-is, I could spin this into a new ticket for post-3.9 if necessary.

This ticket was mentioned in IRC in #wordpress-dev by nacin. View the logs.


11 years ago

#9 @scribu
11 years ago

+1 for 21119.4.diff. It was an oversight in the initial patch.

#10 @nacin
11 years ago

  • Keywords commit dev-reviewed added; needs-unit-tests removed

Reviewed by markjaquith. Good to go.

#11 @nacin
11 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 28087:

User Query: Don't blindly re-append new meta queries for capabilities.

fixes #21119.

Note: See TracTickets for help on using tickets.