Skip to content

Better init for addons. #1

Closed
Closed

Description

@danieliser

I think this is a better init for addons. It has less code, is more readable and uses existing WP core methods.

This is also how I do it for both Popup Maker and the new recipe plugin line Cooked.

add_action( 'plugins_loaded', 'wt_tpa_init' );
function wp_tpa_init() {
    if ( class_exists( 'Test_Plugin' ) ) {
        // If parent plugin already loaded, init add-on.
        test_plugin_addon();
    } else {
        /*
         * This part makes it fancy and notifies them with
         * a message in the admin that the base needs to be
         * installed/activated and includes links to do so.
         * This would replace your die method below with an elegant fallback.
        if ( ! class_exists( 'Plugin_Addon_Activation' ) ) {
            require_once 'includes/class.addon-activation.php';
        }

        $activation = new Plugin_Addon_Activation( plugin_dir_path( __FILE__ ), basename( __FILE__ ) );
        $activation->run();
         */

        // Parent plugin is not activated.
        //  1) Deactivate the add-on
        //  2) Show WP fata error message.

        deactivate_plugins( basename( __FILE__ ) );

        wp_die(
            'The Add On cannot run without the parent plugin. Please activate the parent plugin and then try again.',
            'Error',
            array( 'back_link' => true )
        );
    }
}

Also I would be happy to give you a copy of my Addon_Activation class which you could hack into a ready made include for this as well.

Activity

  1. vovafeldman commented on Oct 21, 2015

    @vovafeldman
    Contributor

    Thanks for the note Daniel. We don't hook to plugins_loaded intentionally. Why? Freemius leverages register_uninstall_hook for reporting abount 'uninstall' event (for users who opted-in). Connecting to register_uninstall_hook on plugins_loaded is too late since the uninstall hook is invoked before.

    Another note, Freemius already handles the error messaging when the parent plugin is missing, so you can remove that part from your Freemius powered add-ons.

  2. danieliser commented on Oct 21, 2015

    @danieliser
    ContributorAuthor

    @vovafeldman - About to test it so maybe I am missing the error message handling you mentioned for addons. But even in that case you are assuming that Freemius is loaded, as the addons doesn't include a copy of the SDK. So if the parent isn;t active then the addon can't have access to the SDK no?

    My method requires including the activation class with every addon, but no die messages. I simply add an admin notification to the top of every page saying Addon X requires Popup Maker, please install|activate it now. Where the install link will immediately install the free plugiin and the activate will activate it.

    I will look into the uninstall hook issue. What all needs to be available at that hook? I am assuming that you don't really need the plugin & addons initialized there, just freemius sdk to send the uninstall notice? In which case you could still use plugins_loaded for initializing plugins/addons but load the SDK immediately so it will always be available as soon as possible. Then the register_uninstall_hook would not be affected.

    Maybe I missed something. Just installed your test plugin & addons to experiment with.

  3. vovafeldman commented on Oct 22, 2015

    @vovafeldman
    Contributor

    Yes and no... :) We built the SDK to support the use-case of a core plugin without Freemius, which can work with a Freemius powered add-on. So:

    1. If the parent plugin is not active but has Freemius folder, the add-on will load the SDK from the parent's folder.
    2. Another option is to include the SDK in the add-on.

    With regards to the register_uninstall_hook, yes, it's for the uninstall notice. In any case, since you can't assure that your add-on's file will be included after the parent plugin's file, if you put the SDK init code-blocks simply as part of the file, when the add-on's SDK is initiated, Freemius will add an error message that the parent plugin is not active.

    Here's the problematic flow:

    • WordPress includes the add-on first (and it can happen, it's not some esoteric use-case)
    • Freemius SDK must be initiated, otherwise register_uninstall_hook callback won't be executed on deletion
    • As part of the init process, Freemius checks if the parent's plugin Freemius instance exist
    • Since the plugin was not yet included, Freemius instance was not yet generated for the plugin; therefore the add-on's instance will throw a corresponding message

    It's a tricky case, but hope it makes things clear.

  4. danieliser commented on Oct 22, 2015

    @danieliser
    ContributorAuthor

    @vovafeldman I see this all the time with Popup Maker. The official solution is to use the plugins_loaded hook for everything that isn't required before that point which excludes register_uninstall_hook and others like it.

    For those things I can fire my plugins init if needed but generally I don't need everything.

    I think the following would solve both problems.

    Init the SDK inline so it is initialized immediately so that it can be used by hooks.

    Run the plugin classes init function on plugins_loaded. I don't see any reason that they have to be married in that respect. Then all the addons can load the freemius SDK if its not loaded already, all will be registered properly. You can also have the SDK wait until plugins_loaded before checking for the parent plugin.

    In reality what I would like to see is all of this rolled into the SDK so that the addon doesn't have to worry about it. That is what we should aim to do here.

    What if we made a function in the SDK that does the foreach on active plugins and returns true or false. Then the developer simply does 1 short if check to load it.

    Hope that makes since.

  5. danieliser commented on Oct 22, 2015

    @danieliser
    ContributorAuthor

    PS I personally feel that those types of checks are really part of the product offering you seem to be going for, putting them in your code rather than the your users just makes for a better product. Let the addon code focus on addon functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions