Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PluginStatus
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 check
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 check_jetpack_modules
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2namespace NewfoldLabs\WP\Module\Patterns\Data;
3
4/**
5 * Class PluginStatus
6 *
7 * Provides utility methods to check the installation and activation status of WordPress plugins.
8 *
9 * @package NewfoldLabs\WP\Module\Patterns\Data
10 */
11class PluginStatus {
12
13    /**
14     * Check the status of a plugin.
15     *
16     * @param string $slug Plugin slug, e.g., 'jetpack/jetpack.php'.
17     * @return string Plugin status: 'installed', 'active', 'inactive', or 'not_installed'.
18     */
19    public static function check( $slug ) {
20
21        // Check if the plugin is installed
22        if ( ! array_key_exists( $slug, get_plugins() ) ) {
23            return 'not_installed';
24        }
25
26        // Check if the plugin is active
27        if ( is_plugin_active( $slug ) ) {
28            // Special handling for Jetpack - check required modules
29            if ( 'jetpack/jetpack.php' === $slug ) {
30                return self::check_jetpack_modules() ? 'active' : 'inactive';
31            }
32
33            return 'active';
34        }
35
36        // The plugin is installed but inactive
37        return 'inactive';
38    }
39
40    /**
41     * Check if required Jetpack modules are active.
42     *
43     * @return bool True if blocks and contact-form modules are active, false otherwise.
44     */
45    private static function check_jetpack_modules() {
46        // Return false if Jetpack class doesn't exist
47        if ( ! class_exists( 'Jetpack' ) ) {
48            return false;
49        }
50
51        // Check if both required modules are active
52        $required_modules = array( 'blocks', 'contact-form' );
53
54        foreach ( $required_modules as $module ) {
55            if ( ! \Jetpack::is_module_active( $module ) ) {
56                return false;
57            }
58        }
59
60        return true;
61    }
62}