WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 10 months ago

Last modified 3 months ago

#32504 closed enhancement (fixed)

WP_Network_Query class

Reported by: jeremyfelt Owned by: jeremyfelt
Milestone: 4.6 Priority: normal
Severity: normal Version:
Component: Networks and Sites Keywords: has-patch has-unit-tests
Focuses: multisite Cc:

Description

Work on WP_Network is being done in #31985 and will soon split several methods into a WP_Network_Query class. While these two tickets exist very much in tandem, it makes sense to track logistical progress on each in separate tickets.

The WP_Network_Query class should be responsible for all of the database queries required to maintain and find WP_Network objects.

Attachments (8)

32504.diff (26.1 KB) - added by flixos90 12 months ago.
initial patch for the class
32504.2.diff (40.9 KB) - added by flixos90 12 months ago.
fixed double quotes, added unit tests
32504.3.diff (26.6 KB) - added by flixos90 11 months ago.
32504.4.diff (41.3 KB) - added by flixos90 10 months ago.
32504.5.diff (33.4 KB) - added by flixos90 10 months ago.
32504.6.diff (34.1 KB) - added by flixos90 10 months ago.
32504.7.diff (32.9 KB) - added by jeremyfelt 10 months ago.
32504-dontcache-max_num_pages.diff (1.3 KB) - added by spacedmonkey 10 months ago.

Download all attachments as: .zip

Change History (48)

This ticket was mentioned in Slack in #core-multisite by jeremyfelt. View the logs.


2 years ago

#2 follow-up: @spacedmonkey
23 months ago

I had two questions about this class.

  1. Will it have meta_query functionality, like other query classes? I think there is value in being able to query networks (sites) by sitemeta.
  1. How much will these queries be cached? I think that network queries should return quickly, as I worry that if these queries are slow, it could be a serious bottle neck in the multisite bootstrapping process.

#3 in reply to: ↑ 2 @jeremyfelt
22 months ago

  • Milestone changed from 4.3 to Future Release

Replying to spacedmonkey:

  1. Will it have meta_query functionality, like other query classes? I think there is value in being able to query networks (sites) by sitemeta.

Great question. I haven't given this area too much thought yet, but I think there is room for some meta query functionality.

  1. How much will these queries be cached? I think that network queries should return quickly, as I worry that if these queries are slow, it could be a serious bottle neck in the multisite bootstrapping process.

IMO, all of the queries should be cached, though some things will be super quick or unnecessary for single (or few) network installs.

I'm going to push this back to future release with the hope that progress will continue over the next few months for early inclusion in the 4.4 cycle.

This ticket was mentioned in Slack in #core by wonderboymusic. View the logs.


20 months ago

This ticket was mentioned in Slack in #core-multisite by flixos90. View the logs.


12 months ago

@flixos90
12 months ago

initial patch for the class

#6 @flixos90
12 months ago

  • Keywords has-patch added; needs-patch removed

32504.diff is an initial take on WP_Network_Query. I took an approach similar to the other recent query classes where only the ID is queried. These IDs are then (if necessary) transformed into objects through a new get_network() function. I also added related method to prime the network cache. Network meta (also known as network options or site options, stored in the sitemeta table) is also supported.

This is just a first patch that definitely needs to be enhanced and extended. Specifically I think it would be great to directly support a super_admin parameter which the class will automatically handle and transform it into a meta query for it. There are other use-cases as well, specifically I'm taking the use-cases from the WP Multi Network plugin into account.

The patch also introduces a new function get_networks() which makes use of WP_Network_Query, so this is a good way to test it.

This ticket was mentioned in Slack in #core-multisite by jeremyfelt. View the logs.


12 months ago

#8 @ocean90
12 months ago

  • Keywords needs-unit-tests added

This ticket was mentioned in Slack in #core-multisite by jeremyfelt. View the logs.


12 months ago

@flixos90
12 months ago

fixed double quotes, added unit tests

#10 @flixos90
12 months ago

  • Keywords has-unit-tests added; needs-unit-tests removed

In 32504.2.diff I added unit tests for the class. Maybe we can still get this rolling to aim for a 4.6 merge, so I would appreciate some feedback.

I think for the initial merge we should keep the class simple as it currently is. In the future we might consider adding features like querying networks by sites in the network, by super admins, maybe even by a specific user in the network - but let's not get into this just yet.

#11 @flixos90
12 months ago

  • Keywords needs-testing added

This ticket was mentioned in Slack in #core-multisite by flixos90. View the logs.


12 months ago

#13 @jeremyfelt
11 months ago

  • Milestone changed from Future Release to 4.6

This ticket was mentioned in Slack in #core-multisite by jeremyfelt. View the logs.


11 months ago

#15 @jeremyfelt
11 months ago

  • Owner set to jeremyfelt
  • Status changed from new to reviewing

This ticket was mentioned in Slack in #core-multisite by jeremyfelt. View the logs.


11 months ago

#17 @jeremyfelt
11 months ago

Leaving a reminder to remove the $object parameter from get_network(), similar to get_site().

@flixos90
11 months ago

#18 @flixos90
11 months ago

32504.3.diff removes the $output parameter from get_network(). It also uses get_network() in wp_get_network() which we will deprecate as discussed earlier. Since ms-deprecated.php is however not loaded when bootstrapping the multisite environment, we probably need to leave this function in ms-load.php. I didn’t add a deprecated notice just yet because of that.

Note that wp_get_network() is expected to return false if no network is found, therefore the type juggling in there.

#19 follow-up: @spacedmonkey
11 months ago

Where does the clean_network_cache function need to be applied? When you update network options? That would effect meta queries. There is no delete / add / update network. However there is populate_network.

Also trying to find some uses in core for this class.

network_domain_check
get_main_network_id
get_admin_users_for_domain

#20 in reply to: ↑ 19 @flixos90
11 months ago

Replying to spacedmonkey:

Where does the clean_network_cache function need to be applied? When you update network options? That would effect meta queries.

I'm not sure if it can be used anywhere in Core since Multinetwork is mostly plugin-territory atm. Using the function when network options (sitemeta) is updated could indeed make sense, however as far as I can see other meta areas of Core don't clean the cache of their object either when updating - correct me if I'm wrong here.

There is no delete / add / update network. However there is populate_network.

It might make sense to use it there too. However I have never looked at that function in detail.

I think for now, we should work on the class itself and its surrounding functionality to hopefully get it into 4.6 - using it in Core could be done later (it would only be in very few areas anyway). What do you think @jeremyfelt? Also additional eyes from @johnjamesjacoby would be helpful since this is likely to be adopted in WP Multi Network.

Sorry for all the pings :)

Last edited 11 months ago by flixos90 (previous) (diff)

@flixos90
10 months ago

#21 @flixos90
10 months ago

The unit tests got lost in 32504.3.diff. They have now been added back in with 32504.4.diff.

@flixos90
10 months ago

#22 @flixos90
10 months ago

  • Keywords needs-testing removed

Due to the unclear nature of network options (which theoretically allow to do a meta query, but they aren't currently dealt with as meta) we'll aim for a 4.6 merge of the class without any meta query (which in our terminology is rather an option query) functionality. We'll deal with this in a later ticket as we have to figure out which way we wanna go with network options. Related: #37181

@flixos90
10 months ago

#23 @flixos90
10 months ago

32504.6.diff includes the fix for the found_posts query from WP_Site_Query that applies here as well.

@jeremyfelt
10 months ago

#24 @jeremyfelt
10 months ago

In 32504.7.diff, I made a couple doc tweaks and removed the to_array() method from WP_Network. I don't believe we need this because we don't have the same back-compat concerns here (or at least not yet).

This ticket was mentioned in Slack in #core by ocean90. View the logs.


10 months ago

#26 @jeremyfelt
10 months ago

In 37893:

Multisite: Introduce get_network().

Given a network ID or network object, get_network() retrieves network data in the same vein as get_site() or get_post(). This will allow for clean retrieval of networks from a primed cache when WP_Network_Query is implemented.

Props flixos90.
See #32504.

#27 @jeremyfelt
10 months ago

In 37894:

Multisite: Introduce WP_Network_Query.

Provides a consistent way to query $wpdb->site for WP_Network objects based on domain, path, network ID, and (main) site ID.

Introduces and uses update_network_cache() and _prime_network_caches() to maintain a cached list of WP_Network objects for use in multiple queries.

Props flixos90.
See #32504.

#28 @jeremyfelt
10 months ago

In 37895:

Multisite: Introduce get_networks().

get_networks() is a wrapper for WP_Network_Query.

Props flixos90.
See #32504.

#29 @jeremyfelt
10 months ago

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

In 37896:

Multisite: Replace wp_get_network() internals with get_network().

get_network() should be considered a replacement for wp_get_network().

Props flixos90.
Fixes #32504.

#30 follow-up: @geminorum
10 months ago

heads up, it will conflict with WP Multi Network v1.7.0:

Fatal error: Cannot redeclare get_networks() (previously declared in \wp-includes\ms-blogs.php:1084) in \wp-content\plugins\wp-multi-network\includes\functions-wp-ms-networks.php on line 51

#31 in reply to: ↑ 30 @jeremyfelt
10 months ago

Replying to geminorum:

heads up, it will conflict with WP Multi Network v1.7.0:

Fatal error: Cannot redeclare get_networks() (previously declared in \wp-includes\ms-blogs.php:1084) in \wp-content\plugins\wp-multi-network\includes\functions-wp-ms-networks.php on line 51

Hi @geminorum, thanks for the heads up. This should be addressed in WP Multi Network soon - https://github.com/stuttter/wp-multi-network/pull/75

#32 @spacedmonkey
10 months ago

32504-dontcache-max_num_pages.diff is an updated patch related to similar change in wp_site_query.

#33 @jeremyfelt
10 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Re-opening to address concerns from fixing the found rows issue in comment queries. See https://core.trac.wordpress.org/ticket/37184#comment:7

#34 @jeremyfelt
10 months ago

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

In 38003:

Multisite: Don't store max_num_pages in WP_Network_Query query cache.

This value can be easily calculated with available data.

Props spacedmonkey.
Fixes #32504.

#35 @jeremyfelt
10 months ago

In 38042:

Multisite: Set default $args to an empty array in get_networks().

The empty string was not incorrect. Using array() here instead makes things a bit more consistent by aligning with get_sites(), get_users(), and get_terms().

See #32504.

#36 @DrewAPicture
10 months ago

In 38055:

Docs: Update the default value for the optional $args parameter in get_networks() following [38042].

See #32504.

#37 @SergeyBiryukov
10 months ago

In 38102:

Multisite: Correct default value for orderby in WP_Network_Query::__construct().

Add a unit test.

See #32504.

#38 @SergeyBiryukov
10 months ago

In 38104:

Docs: Clarify the fields argument description in WP_Network_Query::__construct().

See #32504.

#39 @SergeyBiryukov
8 months ago

In 38595:

Docs: Correct description for domain and path arguments in WP_Network_Query::__construct().

See #32504.

This ticket was mentioned in Slack in #core-multisite by flixos90. View the logs.


3 months ago

Note: See TracTickets for help on using tickets.