-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10209 from google/issue/10169-plugin-detection-logic
Issue / 10169 Plugin Detection Logic
- Loading branch information
Showing
10 changed files
with
416 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
/** | ||
* Class Google\Site_Kit\Core\Util\Plugin_Status | ||
* | ||
* @package Google\Site_Kit | ||
* @copyright 2025 Google LLC | ||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 | ||
* @link https://sitekit.withgoogle.com | ||
*/ | ||
|
||
namespace Google\Site_Kit\Core\Util; | ||
|
||
/** | ||
* Plugin_Status class. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
class Plugin_Status { | ||
|
||
/** | ||
* Plugin installed identifier. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
const PLUGIN_STATUS_INSTALLED = 'installed'; | ||
|
||
/** | ||
* Plugin not active identifier. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
const PLUGIN_STATUS_ACTIVE = 'active'; | ||
|
||
/** | ||
* Plugin not installed identifier. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
const PLUGIN_STATUS_NOT_INSTALLED = 'not-installed'; | ||
|
||
/** | ||
* The plugin path of the plugin being checked. | ||
* | ||
* @var string The plugin path of the plugin being checked. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
public static $plugin_path; | ||
|
||
/** | ||
* Helper method to retrieve plugin installation/activation status. | ||
* | ||
* @param string $plugin_path The plugin path. | ||
* @param string $plugin_url The plugin URL. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @return string The status of the plugin. | ||
*/ | ||
public static function get_plugin_status( $plugin_path = '', $plugin_url = '' ) { | ||
static::$plugin_path = $plugin_path; | ||
|
||
if ( empty( $plugin_path ) && empty( $plugin_url ) ) { | ||
return static::PLUGIN_STATUS_NOT_INSTALLED; | ||
} | ||
|
||
if ( ! function_exists( 'get_plugins' ) ) { | ||
require_once ABSPATH . 'wp-admin/includes/plugin.php'; | ||
} | ||
|
||
if ( true === is_plugin_active( $plugin_path ) ) { | ||
return static::PLUGIN_STATUS_ACTIVE; | ||
} | ||
|
||
$plugins = get_plugins(); | ||
|
||
if ( array_key_exists( $plugin_path, $plugins ) ) { | ||
return static::PLUGIN_STATUS_INSTALLED; | ||
} else { | ||
foreach ( $plugins as $plugin_file => $installed_plugin ) { | ||
if ( $installed_plugin['PluginURI'] === $plugin_url ) { | ||
static::$plugin_path = $plugin_file; | ||
return static::PLUGIN_STATUS_INSTALLED; | ||
} | ||
} | ||
} | ||
|
||
return static::PLUGIN_STATUS_NOT_INSTALLED; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* PluginStatusTrait | ||
* | ||
* @package Google\Site_Kit\Tests | ||
* @copyright 2025 Google LLC | ||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 | ||
* @link https://sitekit.withgoogle.com | ||
*/ | ||
|
||
namespace Google\Site_Kit\Tests; | ||
|
||
trait PluginStatusTrait { | ||
|
||
protected $active_plugins = array(); | ||
|
||
protected $active_plugins_callback; | ||
|
||
public function activate_plugin( $plugin_path ) { | ||
if ( ! in_array( $plugin_path, $this->active_plugins, true ) ) { | ||
$this->active_plugins[] = $plugin_path; | ||
} | ||
|
||
if ( null === $this->active_plugins_callback ) { | ||
$this->active_plugins_callback = array( $this, 'filter_active_plugins' ); | ||
add_filter( 'pre_option_active_plugins', $this->active_plugins_callback ); | ||
} | ||
} | ||
|
||
public function filter_active_plugins( $plugins ) { | ||
return array_merge( $plugins, $this->active_plugins ); | ||
} | ||
|
||
public function deactivate_all_test_plugins() { | ||
if ( null !== $this->active_plugins_callback ) { | ||
remove_filter( 'pre_option_active_plugins', $this->active_plugins_callback ); | ||
$this->active_plugins_callback = null; | ||
} | ||
|
||
$this->active_plugins = array(); | ||
} | ||
} |
Oops, something went wrong.