TGMPA / TGM-Plugin-Activation

TGM Plugin Activation is a PHP library that allows you to easily require or recommend plugins for your WordPress themes (and plugins). It allows your users to install, update and even automatically activate plugins in singular or bulk fashion using native WordPress classes, functions and interfaces. You can reference bundled plugins, plugins from the WordPress Plugin Repository or even plugins hosted elsewhere on the internet.
http://tgmpluginactivation.com/
GNU General Public License v2.0
1.75k stars 431 forks source link

Can't pull plugins from the global $GLOBAL['tgmpa']? #743

Closed redlagoon closed 6 years ago

redlagoon commented 6 years ago

I got TGMPA to work, it displays everything correctly, yet, when I try to pull data from the instance, it just doesn't come. Please note that I've used the generator for TGMPA using themeforest settings, as such:

http://prntscr.com/jqorge

Here's my input / output, used in multiple places:

$tgmpa = isset( $GLOBALS['tgmpa'] ) ? $GLOBALS['tgmpa'] : TGM_Plugin_Activation::get_instance();

var_dump( $tgmpa );

Which results in:

object(TGM_Plugin_Activation)#1100 (17) { ["plugins"]=> array(0) { } ["sort_order":protected]=> array(0) { } ["has_forced_activation":protected]=> bool(false) ["has_forced_deactivation":protected]=> bool(false) ["id"]=> string(5) "tgmpa" ["menu":protected]=> string(21) "tgmpa-install-plugins" ["parent_slug"]=> string(10) "themes.php" ["capability"]=> string(18) "edit_theme_options" ["default_path"]=> string(0) "" ["has_notices"]=> bool(true) ["dismissable"]=> bool(true) ["dismiss_msg"]=> string(0) "" ["is_automatic"]=> bool(false) ["message"]=> string(0) "" ["strings"]=> array(0) { } ["wp_version"]=> string(5) "4.9.6" ["page_hook"]=> NULL }

There's no registered plugins.

But as you can see, the plugin clearly recognizes the plugins that need to be installed:

http://prntscr.com/jqoqid

The whole process of installing / updating works.

Any ideas?

Edit 1: As I thought. I lost the race and didn't catch tgmpa_register soon enough. Here's what I did:

add_action( 'tgmpa_register', '_amnth_register_required_plugins' );
add_action( 'tgmpa_register', function() {
    $tgmpa = isset( $GLOBALS['tgmpa'] ) ? $GLOBALS['tgmpa'] : TGM_Plugin_Activation::get_instance();

    var_dump( $tgmpa->plugins );
});

It seems TGMPA loses the plugin list as soon as that one hook is over, or rather, when it's "done" with them? What am I missing? Trying to make use of the list outside the plugin.

jrfnl commented 6 years ago

You may be running the call to get_instance() too early. Try running it on action init, prio 11.

redlagoon commented 6 years ago

@jrfnl Works if I hook it to init as expected!

But I'm thinking this could be problematic if I try to use it on other plugins / front-end. Any ideas on how to transport the data?

jrfnl commented 6 years ago

I'm not sure what you are asking.

redlagoon commented 6 years ago

@jrfnl

I'd like to improve upon TGMPA and allow a front-end screen for it (together with other options), so, a setup on its own, but without the need to go to the back-end. But I don't have access to TGMPA's registered instance, just its clean instance, so, it seems that if I call TGMPA on the front-end, it's empty, I just see its skeleton, while, if I call it on init as you suggested, it works.

It seems that this won't ever work, so perhaps I should create an admin page and redirect the user to it?

jrfnl commented 6 years ago

Correct. TGMPA is designed not to work to front-end as that could be considered a security risk.

redlagoon commented 6 years ago

Got it. Can mark as solved.