[wp-trac] [WordPress Trac] #41333: Implement `wp_install_site()` and `wp_uninstall_site()`

WordPress Trac noreply at wordpress.org
Tue Aug 7 12:33:16 UTC 2018


#41333: Implement `wp_install_site()` and `wp_uninstall_site()`
-------------------------------------------------+-------------------------
 Reporter:  flixos90                             |       Owner:  flixos90
     Type:  enhancement                          |      Status:  assigned
 Priority:  normal                               |   Milestone:  5.0
Component:  Networks and Sites                   |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  ms-roadmap has-patch needs-unit-     |     Focuses:  multisite
  tests needs-testing dev-feedback               |
-------------------------------------------------+-------------------------
Changes (by flixos90):

 * keywords:  2nd-opinion ms-roadmap => ms-roadmap has-patch needs-unit-
     tests needs-testing dev-feedback


Comment:

 [attachment:"41333.diff"] is a first take on this:

 * A `wp_install_site` action is introduced and fired after the site's DB
 row has been inserted and after any other related functionality has been
 executed (via the `wp_insert_site` hook).
 * A `wp_uninstall_site` action is introduced and fired before the site's
 DB row will be deleted and before any other related functionality will be
 executed (via the `wp_delete_site` hook).
 * A new `wp_install_site()` function is hooked into `wp_install_site`. It
 receives the site object and any arguments passed to `wp_insert_site()`
 that are not part of the site's DB row fields.
 * A new `wp_uninstall_site()` function is hooked into `wp_uninstall_site`.
 It receives the site object.
 * The temporary removal of the action
 `wp_update_blog_public_option_on_site_update()` from the
 `update_blog_public` hook has been moved into `wp_insert_site()`. This is
 necessary since, when the `wp_insert_site` hook fires, the database tables
 won't exist yet.
 * The removal of site metadata (`blogmeta`) is now part of
 `wp_delete_site()`. This data is in network scope, so it should be covered
 by this function, while `wp_uninstall_site()` covers the site scope data.
 * `wp_install_site()` is arguably the most complex part of the patch. It
 contains logic that was previously part of `wpmu_create_blog()`. Moreover,
 `install_blog()` has been deprecated, and its logic is now also part of
 `wp_install_site()`, in a more flexible way. `populate_options()` now
 accepts an optional `$options` array parameter that allows to override
 some of the initial options with custom values. This parameter is used in
 `wp_install_site()`, which allows to get rid of some unnecessary
 `update_option()` calls later on.
 * `wp_uninstall_site()` contains logic that was previously part of
 `wpmu_delete_blog()`. It removes users from the site, drops the DB tables
 and removes the uploads directory. It explicitly does not support the
 `$drop` parameter, as uninstalling a site ''is'' equal to dropping
 everything. It's thus not a direct replacement for `wpmu_delete_blog()`
 because it makes more sense to deal with setting the `deleted` flag and
 actually deleting separately.
 * `wpmu_create_blog()` now simply calls `wp_insert_site()`, passing the
 `$title`, `$user_id`, and `$meta` arguments to it as necessary.
 * `wpmu_delete_blog()` now simply calls `wp_delete_site()` if `$drop` is
 true. Since `wpmu_delete_blog()` removes all users from the site even if
 it only sets the `deleted` flag, that code may be executed twice
 (`wp_uninstall_site()` needs to remove users as well). This is not an
 issue though, since after the first run those users will already have been
 removed.
 * Any default option that relies on some network value now uses the
 network that is specified for the new site. It previously would look up
 the current network, which may not always be the right one (usually it
 would, but not always).
 * The unrelated function `install_blog_defaults()` has been moved to `ms-
 deprecated.php`. It has been deprecated for a long time, but for some
 reason was still in `ms-functions.php`.

 Future follow-up stuff (that should not be part of this ticket):
 * `wpmu_create_blog()` and `wpmu_delete_blog()` should no longer be used
 by core. However I think it's best to make these changes in a release
 after 5.0, because we wanna make sure the code we have is really backward-
 compatible first.
 * The two functions should then probably be deprecated too.

 Open questions from my end:
 * In `wp_install_site()`, passing an empty user ID currently results in a
 `WP_Error`. I wonder whether we should use a default, for example the
 network administrator of the site's network. That would be a good fallback
 IMO, since that user has access anyway.
 * Is the order of the actions the right choice?
     * For inserting a site: DB transaction -> `wp_insert_site` hook ->
 `wp_install_site` hook
     * For deleting a site: `wp_uninstall_site` hook -> DB transaction ->
 `wp_delete_site` hook

 This whole change needs quite a bit of testing, so please apply the patch
 and play around with it. It's not that big actually, a lot has been copy-
 paste only. The most important thing we need to ensure is that site
 installation and uninstallation (DB tables, correct initial options and
 content, uploads directory) work correctly.

 Unit tests will be provided after some initial feedback and discussion on
 whether this is on the right track.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/41333#comment:12>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list