Skip to content

Commit

Permalink
Handle pro/free plugin activation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaydsouza committed Sep 3, 2024
1 parent bd1ccd8 commit d7aa5b0
Showing 1 changed file with 84 additions and 4 deletions.
88 changes: 84 additions & 4 deletions includes/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

namespace WebberZone\Top_Ten;

use WebberZone\Top_Ten\Admin\Activator;
use WebberZone\Top_Ten\Admin\Cron;

if ( ! defined( 'WPINC' ) ) {
exit;
}

/**
* Main plugin class.
*
Expand Down Expand Up @@ -126,6 +129,7 @@ public static function get_instance() {
self::$instance = new self();
self::$instance->init();
}

return self::$instance;
}

Expand All @@ -152,13 +156,21 @@ private function init() {
$this->blocks = new Frontend\Blocks\Blocks();
$this->feed = new Frontend\Feed();
$this->cron = new Cron();

$this->hooks();

if ( is_admin() ) {
$this->admin = new Admin\Admin();
if ( is_multisite() ) {
$this->admin = new Admin\Network\Admin();
}
}

if ( tptn_freemius()->is__premium_only() ) {
if ( tptn_freemius()->can_use_premium_code() ) {
$this->pro = new Pro\Pro();
}
}
}

/**
Expand All @@ -171,6 +183,9 @@ public function hooks() {
add_action( 'widgets_init', array( $this, 'register_widgets' ) );
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
add_action( 'parse_query', array( $this, 'parse_query' ) );

add_action( 'activated_plugin', array( $this, 'activated_plugin' ), 10, 2 );
add_action( 'pre_current_active_plugins', array( $this, 'plugin_deactivated_notice' ) );
}

/**
Expand All @@ -188,10 +203,9 @@ public function initiate_plugin() {
* @since 3.3.0
*/
public function register_widgets() {
register_widget( '\\WebberZone\\Top_Ten\\Frontend\\Widgets\\Posts_Widget' );
register_widget( '\\WebberZone\\Top_Ten\\Frontend\\Widgets\\Count_Widget' );
register_widget( '\WebberZone\Top_Ten\Frontend\Widgets\Posts_Widget' );
register_widget( '\WebberZone\Top_Ten\Frontend\Widgets\Count_Widget' );
}

/**
* Function to register our new routes from the controller.
*
Expand All @@ -203,7 +217,7 @@ public function register_rest_routes() {
}

/**
* Hook into WP_Query to check if crp_query is set and is true. If so, we load the CRP query.
* Hook into WP_Query to check if tptn_query is set and is true. If so, we load the Top 10 query.
*
* @since 3.5.0
*
Expand All @@ -214,4 +228,70 @@ public function parse_query( $query ) {
new Top_Ten_Core_Query( $query->query_vars );
}
}

/**
* Checks if another version of Top 10/Top 10 Pro is active and deactivates it.
* Hooked on `activated_plugin` so other plugin is deactivated when current plugin is activated.
*
* @since 3.5.0
*
* @param string $plugin The plugin being activated.
* @param bool $network_wide Whether the plugin is being activated network-wide.
*/
public function activated_plugin( $plugin, $network_wide ) {
if ( ! in_array( $plugin, array( 'top-10/top-10.php', 'top-10-pro/top-10.php' ), true ) ) {
return;
}

Activator::activation_hook( $network_wide );

$plugin_to_deactivate = 'top-10/top-10.php';
$deactivated_notice_id = '1';

// If we just activated the free version, deactivate the pro version.
if ( $plugin === $plugin_to_deactivate ) {
$plugin_to_deactivate = 'top-10-pro/top-10.php';
$deactivated_notice_id = '2';
}

if ( is_multisite() && is_network_admin() ) {
$active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
$active_plugins = array_keys( $active_plugins );
} else {
$active_plugins = (array) get_option( 'active_plugins', array() );
}

foreach ( $active_plugins as $plugin_basename ) {
if ( $plugin_to_deactivate === $plugin_basename ) {
set_transient( 'tptn_deactivated_notice_id', $deactivated_notice_id, 1 * HOUR_IN_SECONDS );
deactivate_plugins( $plugin_basename );
return;
}
}
}

/**
* Displays a notice when either Top 10 or Top 10 Pro is automatically deactivated.
*
* @since 3.5.0
*/
public function plugin_deactivated_notice() {
$deactivated_notice_id = (int) get_transient( 'tptn_deactivated_notice_id' );
if ( ! in_array( $deactivated_notice_id, array( 1, 2 ), true ) ) {
return;
}

$message = __( "Top 10 and Top 10 Pro should not be active at the same time. We've automatically deactivated Top 10.", 'top-10' );
if ( 2 === $deactivated_notice_id ) {
$message = __( "Top 10 and Top 10 Pro should not be active at the same time. We've automatically deactivated Top 10 Pro.", 'top-10' );
}

?>
<div class="updated" style="border-left: 4px solid #ffba00;">
<p><?php echo esc_html( $message ); ?></p>
</div>
<?php

delete_transient( 'tptn_deactivated_notice_id' );
}
}

0 comments on commit d7aa5b0

Please sign in to comment.