Fix table creation issue on multisite.#679
Conversation
PR Summary
In summary, these changes improve the clarity, modifiability, and efficiency of the code, and introduce more control over the multisite network handling. |
|
To get a clue of what is and what was going on before, I did the following tests at first on the 0.29.0 tagged version just to confirm and understand what was not working. With 0.29.0✅ 1. Plugin gets activated on an existing subsite of a network, tables are created. Going on with the same tests using the code of this PR with PR Fix 679✅ 1. Plugin gets activated on an existing subsite of a network, tables are created. [09-Jun-2024 21:06:02 UTC] PHP Fatal error: Uncaught TypeError: GatherPress\Core\Setup::on_site_create(): Argument #1 ($site_id) must be of type int, WP_Site given, called in /shared/httpd/gatherpress/htdocs/wp-includes/class-wp-hook.php on line 326 and defined in /shared/httpd/gatherpress/repositories/gatherpress/includes/core/classes/class-setup.php:339
Stack trace:
#0 /shared/httpd/gatherpress/htdocs/wp-includes/class-wp-hook.php(326): GatherPress\Core\Setup->on_site_create(Object(WP_Site))
#1 /shared/httpd/gatherpress/htdocs/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(true, Array)
#2 /shared/httpd/gatherpress/htdocs/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#3 /shared/httpd/gatherpress/htdocs/wp-includes/ms-site.php(105): do_action('wp_initialize_s...', Object(WP_Site), Array)
#4 /shared/httpd/gatherpress/htdocs/wp-includes/ms-functions.php(1401): wp_insert_site(Array)
#5 /shared/httpd/gatherpress/htdocs/wp-admin/network/site-new.php(144): wpmu_create_blog('fix.gatherpress...', '/', 'fix 3', 1, Array, 1)
#6 {main}
thrown in /shared/httpd/gatherpress/repositories/gatherpress/includes/core/classes/class-setup.php on line 339I'm going on testing ... |
| * @param int $site_id ID of the newly created site. | ||
| * @return void | ||
| */ | ||
| public function on_site_create( int $site_id ): void { |
There was a problem hiding this comment.
This function is hooked onto wp_initialize_site, which provides the WP_Site object.
* @param \WP_Site $site New site object of the created site.
* @return void
*/
public function on_site_create( \WP_Site $site ): void {| */ | ||
| public function on_site_create( int $site_id ): void { | ||
| if ( is_plugin_active_for_network( 'gatherpress/gatherpress.php' ) ) { | ||
| switch_to_blog( $site_id ); |
There was a problem hiding this comment.
switch_to_blog( $site->blog_id );| // Get all blogs in the network and activate plugin on each one. | ||
| $blog_ids = $wpdb->get_col( $wpdb->prepare( 'SELECT blog_id FROM %i', $wpdb->blogs ) ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
There was a problem hiding this comment.
Wouldn't it be easier to just use https://developer.wordpress.org/reference/functions/get_sites/
$args = array(
'fields' => 'ids',
'network_id' => get_current_site()->id,
);
$blog_ids = get_sites( $args );There was a problem hiding this comment.
There is no error, just the wish for core functions.
|
With the suggested changes in place, I can confirm, that all my individual tests ran successfully. ✅ 1. Plugin gets activated on an existing subsite of a network, tables are created. |
|
Last missing piece, I would love to see, is a new FAQ section in the
Could be much better, but I'm not creative enough in the moment. |
…herpress into fix-multisite-activation
Description of the Change
Installing GatherPress on a WordPress multisite was not activating correctly.
This PR fixes both of those issues and refactors code a little bit.
How to test the Change
Activate the plugin in a multisite network and GatherPress custom tables for events and rsvp should be created for each site. Add a new site to the network and tables should be created.
Changelog Entry
Credits
Props @mauteri @patriciabt
Checklist: