Skip to content

Fix table creation issue on multisite.#679

Merged
mauteri merged 9 commits intoversion-0.29.1from
fix-multisite-activation
Jun 11, 2024
Merged

Fix table creation issue on multisite.#679
mauteri merged 9 commits intoversion-0.29.1from
fix-multisite-activation

Conversation

@mauteri
Copy link
Copy Markdown
Contributor

@mauteri mauteri commented Jun 9, 2024

Description of the Change

Installing GatherPress on a WordPress multisite was not activating correctly.

  • Tables were not being created when the plugin activated.
  • Tables were not created when a new site was added to the network.

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

Fixed - Bug fix

Credits

Props @mauteri @patriciabt

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@what-the-diff
Copy link
Copy Markdown

what-the-diff bot commented Jun 9, 2024

PR Summary

  • Added Action Hook for Site Initialization
    A new action hook, wp_initialize_site, has been incorporated which allows other developers to have their code executed when the site is initialized.

  • Method Renaming for Better Clarity
    The method previously named maybe_flush_gatherpress_rewrite_rules has been renamed to maybe_flush_rewrite_rules for consistency and easy understanding.

  • Addition of Method to Potentially Create Flush Rule Flag
    A new method maybe_create_flush_rewrite_rules_flag has been added, which checks conditions and possibly creates a flag to flush the rewrite rules.

  • Updated Method for Plugin Activation
    The activate_gatherpress_plugin method now also accepts an optional variable, bool $network_wide, to distinguish whether the activation needs to be applied across the network in a multisite setup. It has also been updated to incorporate new logic that handles network-wide activation.

  • Added Method for New Site Creation in Multisite Network
    The on_site_create method has been added to handle certain actions whenever a new site is created within a multisite network.

  • Consolidation of Table Creation Methods
    The maybe_create_custom_table method has been eliminated, its logic has been merged into the create_tables method to streamline the table creation process.

  • Replacement of Unnecessary Import Statement
    An unnecessary import statement in the create_tables method has been removed for code cleanliness.

In summary, these changes improve the clarity, modifiability, and efficiency of the code, and introduce more control over the multisite network handling.

@mauteri mauteri changed the title Fix table issue creation on multisite. Fix table creation issue on multisite. Jun 9, 2024
@mauteri mauteri marked this pull request as ready for review June 9, 2024 16:00
@mauteri mauteri requested review from carstingaxion and pbrocks June 9, 2024 16:05
@mauteri mauteri changed the base branch from main to version-0.29.1 June 9, 2024 16:12
@carstingaxion
Copy link
Copy Markdown
Collaborator

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.
❌ 2. Plugin gets network-activated on an existing network, all sites should get the tables created. Only the main site gets new tables.
❌ 3. Plugin is network-activated on a network, new added sites should get the tables created. No 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.
✅ 2. Plugin gets network-activated on an existing network, all sites get the tables created.
❌ 3. Plugin is network-activated on a network, new added sites should get the tables created. Throws a fatal error in php.

[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 339

I'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 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 );
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch_to_blog( $site->blog_id );

Comment on lines +188 to +189
// 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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no error, just the wish for core functions.

@carstingaxion
Copy link
Copy Markdown
Collaborator

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.
✅ 2. Plugin gets network-activated on an existing network, all sites get the tables created.
✅ 3. Plugin is network-activated on a network, new added sites get the tables created.

@carstingaxion
Copy link
Copy Markdown
Collaborator

Last missing piece, I would love to see, is a new FAQ section in the readme.md. Something like

Is it multisite compatible?

Yes. GatherPress can be run on a network of sites. The additional database tables it needs, will be created automatically for each new site if the plugin is network-activated.

GatherPress can also be activated per site.

Could be much better, but I'm not creative enough in the moment.

@mauteri mauteri merged commit bf516c7 into version-0.29.1 Jun 11, 2024
@mauteri mauteri deleted the fix-multisite-activation branch June 11, 2024 01:35
@carstingaxion carstingaxion mentioned this pull request Jun 14, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants