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

Help Hub - Transient issue when installing new plugin #2331

Merged
merged 18 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/Tribe/PUE/Checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,35 @@ public function __construct( $pue_update_url, $slug = '', $options = [], $plugin
$this->set_options( $options );
$this->hooks();
$this->set_key_status_name();
$this->init( $slug );
// So we can reference our "registered" instances later.
self::$instances[ $slug ] ??= $this;
}

/**
* Initializes the PUE checker and fires the appropriate action if not already initialized.
*
* This method ensures that the `tec_pue_checker_init` action is fired only once per unique slug.
*
* @since TBD
*
* @param string $slug The unique slug for the plugin being initialized.
*/
public function init( $slug ) {
if ( isset( self::$instances[ $slug ] ) ) {
return;
}

/**
* Fires when initializing the PUE checker.
*
* @since TBD
*
* @param Tribe__PUE__Checker $checker An instance of the PUE Checker being initialized.
*/
do_action( 'tec_pue_checker_init', $this );
}

/**
* Gets whether the license key is valid or not.
*
Expand Down Expand Up @@ -447,6 +472,7 @@ public function hooks() {
add_filter( 'upgrader_pre_download', [ Tribe__PUE__Package_Handler::instance(), 'filter_upgrader_pre_download' ], 5, 3 );

add_action( 'admin_init', [ $this, 'monitor_uplink_actions' ], 1000 );
add_action( 'tec_pue_checker_init', [ __CLASS__, 'monitor_active_plugins' ] );
}


Expand Down Expand Up @@ -2158,5 +2184,28 @@ function ( $plugin ) {
},
);
}

/**
* Monitor active plugins and validate the transient for the given slug.
*
* @param Tribe__PUE__Checker $checker An instance of the PUE Checker.
*/
public static function monitor_active_plugins( Tribe__PUE__Checker $checker ) {
$slug = $checker->get_slug();

if ( empty( $slug ) ) {
return;
}

$transient_data = get_transient( self::IS_ANY_LICENSE_VALID_TRANSIENT_KEY );

$plugins = $transient_data['plugins'] ?? null;

if ( ! is_array( $plugins ) || array_key_exists( $slug, $plugins ) ) {
return;
}

delete_transient( self::IS_ANY_LICENSE_VALID_TRANSIENT_KEY );
redscar marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
125 changes: 0 additions & 125 deletions tests/integration/Tribe/PUE/Checker_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,129 +601,4 @@ function () {
'A transient marked invalid should return false.',
];
}

/**
* Mock we're inside the wp-admin dashboard and fire off the admin_init hook.
*
* @param bool $network Whether we're in the network dashboard.
*
* @return void
*/
protected function admin_init( bool $network = false ): void {
$screen = WP_Screen::get( $network ? 'dashboard-network' : 'dashboard' );
$GLOBALS['current_screen'] = $screen;

if ( $network ) {
$this->assertTrue( $screen->in_admin( 'network' ) );
}

$this->assertTrue( $screen->in_admin() );

// Fire off admin_init to run any of our events hooked into this action.
do_action( 'admin_init' );
}

/**
* Helper to register a new plugin for Uplink testing.
*
* @param string $slug The slug of the plugin.
* @param string $name The name of the plugin.
* @param string $version The version of the plugin.
*
* @return Resource
*/
private function register_new_uplink_plugin( string $slug, string $name = 'Sample Plugin', string $version = '1.0.10' ): Resource {
return Register::plugin(
$slug,
$name,
$version,
__DIR__,
tribe( Tribe__Main::class )
);
}

/**
* Creates a valid Uplink license for testing.
*
* @param string $slug The slug of the plugin to create a license for.
*
* @return string The generated token for the Uplink license.
*/
private function create_valid_uplink_license( string $slug ): string {
$plugin = $this->register_new_uplink_plugin( $slug );

// Set the current user to an admin.
wp_set_current_user( 1 );

// Initialize the token manager.
$this->token_manager = tribe( Token_Manager::class );

// Ensure no token exists for the plugin initially.
$this->assertNull( $this->token_manager->get( $plugin ) );

// Generate a nonce and a token.
$nonce = ( tribe( Nonce::class ) )->create();
$token = '53ca40ab-c6c7-4482-a1eb-14c56da31015';

// Mock these were passed via the query string.
global $_GET;
$_GET[ Connect_Controller::TOKEN ] = $token;
$_GET[ Connect_Controller::NONCE ] = $nonce;
$_GET[ Connect_Controller::SLUG ] = $slug;

// Mock we're an admin inside the dashboard.
$this->admin_init();

// Fire off the specification action tied to this slug.
do_action( tribe( Action_Manager::class )->get_hook_name( $slug ) );

// Verify that the token was assigned correctly.
$this->assertSame( $token, $this->token_manager->get( $plugin ) );

// Verify that the general 'connected' action fires.
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/connected' ) ,'Hook should only run once');

return $token;
}

/**
* Helper to disconnect an Uplink plugin.
*
* @param string $slug The slug of the plugin to disconnect.
*/
private function disconnect_uplink_plugin( string $slug ): void {
$plugin = get_resource( $slug );
if ( empty( $plugin ) ) {
return;
}

if ( empty( $this->token_manager ) ) {
return;
}

global $_GET;
wp_set_current_user( 1 );

$token = $this->token_manager->get( $plugin );
$this->assertNotNull( $token );

// Mock these were passed via the query string.
$_GET[ Disconnect_Controller::ARG ] = 1;
$_GET[ Disconnect_Controller::CACHE_KEY ] = 'nada';
$_GET[ Disconnect_Controller::SLUG ] = $slug;
$_GET['_wpnonce'] = wp_create_nonce( Disconnect_Controller::ARG );

// Mock we're an admin inside the dashboard.
$this->admin_init();

// Fire off the specification action tied to this slug.
do_action( tribe( Action_Manager::class )->get_hook_name( $slug ) );

// Assert that the token is removed.
$this->assertNull( $this->token_manager->get( $plugin ) );

// Verify that the disconnected action fires.
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/disconnected' ) );
}

}
Loading