Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin database tables not being created for all instances of a WP network installation? #4

Open
winnewoerp opened this issue Dec 18, 2020 · 10 comments

Comments

@winnewoerp
Copy link

Installing the plugin with a WP network installation worked, but errors where logged when trying to save connection data, saying that the respective database tables are not existing. As a workaround, we now temporarily added the 'CREATE TABLE...' part of the plugin into our theme functions.

@AHowiller
Copy link

We had the same issue and your workaround did work for us, too. Maybe anyone else finds a reason why the CMRF plugin itself fails here? Anyway: Thanks for finding that out and also for sharing your workaround as an add-on at https://codeberg.org/winnewoerp/wpcmrf-lda

@winnewoerp
Copy link
Author

winnewoerp commented Sep 9, 2021

I found the error but unfortunately I don't have the time to fork this plugin and integrate the solution. The is_multisite() part of the following code needs to be adapted and added to the respective function where the databases tables are being created

function my_plugin_activate($network_wide) {

    if ( is_multisite() && $network_wide ) { 

        foreach (get_sites(['fields'=>'ids']) as $blog_id) {
            switch_to_blog($blog_id);
            //do your specific thing here...
            restore_current_blog();
        } 

    } else {
        //run in single site context
    }

}

register_activation_hook( __FILE__, 'my_plugin_activate' );

Source: How to run an activation function when plugin is network activated on multisite?

PLEASE NOTE: Both this plugin and my add-on (Link in the previous comment) should work correctly in a WordPress network when not activated network-wide but for single sites only. If you want this plugin to be activated network-wide, then you have to use my add-on and activate it for single sites. But: Now that I found the error I don't see any reason for using my add-on anymore. Just use this plugin and activate it per site (where needed) and not network-wide—until the solution has been implemented here.

But please be aware that this plugin adds custom database tables to all of the sites in your network (once the fix is implemented) when activated network-wide. So you should consider to only activate it for sites where connections to a CiviCRM instance are actually needed!

@winnewoerp
Copy link
Author

An alternative solution regarding my previous comment: Maybe the better solution is then (after all) to prevent network-wide activation of the plugin. Like this:

function activate($networkwide) {
    if (is_multisite() && $networkwide) 
       die('This plugin can\'t be activated networkwide');
}

register_activation_hook('your-plugin/index.php','activate');

Source: Prevent network activation of plugin

@AHowiller
Copy link

Thanks again @winnewoerp – we decided to fund a fix and will give feedback.

@winnewoerp
Copy link
Author

Thank you! Here's my approach to the solution (just prevent network-wide plugin activation) for my plugin, that should be adaptable for the main plugin:
[Branch: dev] wpcmrf-lda/wpcmrf-lda.php
It is working (the plugin can't be activated on network plugin admin page). But the error message called via no_network_activation() doesn't appear. It seems that the 'admin_init' filter is not working for network admin pages.
Here's the discussion about the issue at wordpress.stackexchange.com

@jaapjansma
Copy link
Contributor

I have updated the code and added code to create the database table on all active sites.

@christianwach
Copy link

@jaapjansma Just a word of warning... I know the code you've added is easy to find on the interwebs, but it has its issues - particularly for large Multisite installs. Your code will time out on larger Multisite installs and potentially cause problems.

I would recommend (at minimum) adding a check for wp_is_large_network( 'sites' ) in your install code. This will return true for networks with more than 10,000 sites, but that value may itself be filtered. Docs here. (The wpmu_new_blog callback can stay as-is since that's an infrequent operation.)

Better still, I would recommend providing an admin page for this plugin with an AJAX-driven UI that adds the tables to sub-sites in batches so that timeouts do not happen. See CiviCRM Profile Sync, see CiviCRM Event Organiser or the CiviCRM-WordPress plugin itself for implementations of AJAX in WordPress admin pages.

Just sayin' :)

@jaapjansma
Copy link
Contributor

@christianwach thanks for pointing out. How would I create such an AJAX driven UI? I am still not familiar with word press although I have developed a couple of plugins.

@christianwach
Copy link

How would I create such an AJAX driven UI? I am still not familiar with word press

@jaapjansma All I can really suggest is digging through some of the repos that I pointed to. I'm ridiculously over-zealous about commenting my code, so you should be able to get an idea of how things work fairly quickly.

CiviCRM Member Sync, for example, uses a combination of an admin page declared here (though you'd want to do this in response to the Network Admin action), this JS file enqueued here, an AJAX callback method declared here and implemented here. Those links should give you a fair idea of how to use the built-in jQuery progress bar script to achieve this kind of UI. Your use case is way simpler than what I'm pointing to so you'd be able to strip a whole lot of code out for your use case.

Of course most Multisite installs are not large networks - so I doubt there's a great hurry. I just wanted to leave this here for reference :)

@jaapjansma
Copy link
Contributor

Thanks @christianwach I had a short look into whether I could implement this quickly but it requires a bit of work from my side as I have to learn/understand wordpress and multi site. And also a test setup with a fair amount of multi sites. So I will leave it here till I have funding for this issue.
I agree that most sites are probably not large multi sites.

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

No branches or pull requests

4 participants