Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
PluginService
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 8
1722
0.00% covered (danger)
0.00%
0 / 1
 is_installed
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 is_active
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
 install
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
42
 activate_plugin
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
 setup_plugin
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 activate
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 setup
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 enable_jetpack_forms_module
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2namespace NewfoldLabs\WP\Module\Patterns\Services;
3
4use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller;
5use NewfoldLabs\WP\Module\Installer\Tasks\PluginActivationTask;
6use NewfoldLabs\WP\Module\Installer\Tasks\PluginInstallTask;
7
8/**
9 * Class PluginService
10 *
11 * Provides utility methods for managing WordPress plugins within the Patterns module.
12 * This service handles plugin installation, activation, and integration setup for
13 * plugins required by various patterns. It offers both individual and bulk operations
14 * for plugin management with special handling for premium plugins.
15 */
16class PluginService {
17
18    /**
19     * Check if a single plugin is installed.
20     *
21     * @param string|array $plugin Plugin slug or plugin array with 'slug' key.
22     * @return boolean True if installed, false otherwise.
23     */
24    public static function is_installed( $plugin ) {
25        $slug        = is_array( $plugin ) ? $plugin['slug'] : $plugin;
26        $plugin_type = PluginInstaller::get_plugin_type( $slug );
27        $plugin_path = isset( $plugin['path'] ) ? $plugin['path'] : '';
28
29        if ( ! $plugin_path ) {
30            if ( isset( $plugin['basename'] ) ) {
31                $plugin_path = $plugin['basename'];
32            } else {
33                return false;
34            }
35        }
36
37        return PluginInstaller::is_plugin_installed( $plugin_path );
38    }
39
40    /**
41     * Check if a single plugin is active.
42     *
43     * @param string|array $plugin Plugin slug or plugin array with 'slug' key.
44     * @return boolean True if active, false otherwise.
45     */
46    public static function is_active( $plugin ) {
47        $slug        = is_array( $plugin ) ? $plugin['slug'] : $plugin;
48        $plugin_type = PluginInstaller::get_plugin_type( $slug );
49        $plugin_path = isset( $plugin['path'] ) ? $plugin['path'] : '';
50
51        if ( ! $plugin_path ) {
52            if ( isset( $plugin['basename'] ) ) {
53                $plugin_path = $plugin['basename'];
54            } else {
55                return false;
56            }
57        }
58
59        if ( ! PluginInstaller::is_plugin_installed( $plugin_path ) ) {
60            return false;
61        }
62
63        return is_plugin_active( $plugin_path );
64    }
65
66    /**
67     * Install a single plugin.
68     *
69     * @param string|array $plugin Plugin slug or plugin array with 'slug' key.
70     * @param boolean      $activate Whether to activate the plugin after installation.
71     * @return boolean True on success, false on failure.
72     */
73    public static function install( $plugin, $activate = true ) {
74        if ( is_string( $plugin ) ) {
75            $plugin = array( 'slug' => $plugin );
76        }
77
78        if ( empty( $plugin['slug'] ) ) {
79            return false;
80        }
81
82        if ( isset( $plugin['isPremium'] ) && $plugin['isPremium'] ) {
83            return PluginInstaller::install_premium_plugin( $plugin['plsSlug'], $plugin['plsProviderName'], $activate );
84        }
85
86        $plugin_installation_task = new PluginInstallTask(
87            $plugin['slug'],
88            $activate,
89            isset( $plugin['priority'] ) ? $plugin['priority'] : 0
90        );
91        return $plugin_installation_task->execute();
92    }
93
94    /**
95     * Activate a single plugin.
96     *
97     * @param string|array $plugin Plugin slug or plugin array with 'slug' key.
98     * @return boolean True on success, false on failure.
99     */
100    public static function activate_plugin( $plugin ) {
101
102        if ( ! self::is_installed( $plugin ) ) {
103            return false;
104        }
105
106        if ( isset( $plugin['isPremium'] ) && $plugin['isPremium'] ) {
107            return PluginInstaller::install_premium_plugin( $plugin['plsSlug'], $plugin['plsProviderName'], true );
108        }
109
110        $slug                   = is_array( $plugin ) ? $plugin['slug'] : $plugin;
111        $plugin_activation_task = new PluginActivationTask( $slug );
112        $result                 = $plugin_activation_task->execute();
113
114        if ( is_wp_error( $result ) ) {
115            return false;
116        }
117
118        return true;
119    }
120
121    /**
122     * Setup a single plugin integration.
123     *
124     * @param string|array $plugin Plugin slug or plugin array with 'slug' key.
125     * @return boolean True if setup was performed, false otherwise.
126     */
127    public static function setup_plugin( $plugin ) {
128        $slug = is_array( $plugin ) ? $plugin['slug'] : $plugin;
129
130        // Currently we only have special setup for Jetpack
131        if ( 'jetpack' === $slug ) {
132            self::enable_jetpack_forms_module();
133            return true;
134        }
135
136        return false;
137    }
138
139    /**
140     * Activate or install the specified plugins.
141     *
142     * @param array $plugins Array of plugins to be activated or installed.
143     * @return array Results with status for each plugin operation
144     */
145    public static function activate( $plugins ) {
146        $results = array();
147
148        if ( ! is_array( $plugins ) || empty( $plugins ) ) {
149            return $results;
150        }
151
152        foreach ( $plugins as $plugin ) {
153            if ( self::is_installed( $plugin ) ) {
154                $results[ $plugin['slug'] ] = self::activate_plugin( $plugin );
155            } else {
156                $results[ $plugin['slug'] ] = self::install( $plugin, true );
157            }
158        }
159
160        return $results;
161    }
162
163    /**
164     * Setup the integration for specific plugins.
165     *
166     * This method loops through the provided list of plugins and enables
167     * specific functionality or modules based on the plugin slug.
168     *
169     * @param array $plugins Array of plugins to be integrated. Each element is expected to have a 'slug' key.
170     * @return array Results of setup operations
171     */
172    public static function setup( $plugins ) {
173        $results = array();
174
175        if ( ! is_array( $plugins ) || empty( $plugins ) ) {
176            return $results;
177        }
178
179        foreach ( $plugins as $plugin ) {
180            if ( isset( $plugin['slug'] ) ) {
181                $results[ $plugin['slug'] ] = self::setup_plugin( $plugin );
182            }
183        }
184
185        return $results;
186    }
187
188    /**
189     * Enable the Jetpack Forms module.
190     *
191     * @return boolean True if module was activated or was already active
192     */
193    public static function enable_jetpack_forms_module() {
194        if ( class_exists( 'Jetpack' ) && ! \Jetpack::is_module_active( 'contact-form' ) ) {
195            if ( ! \Jetpack::is_module_active( 'blocks' ) ) {
196                \Jetpack::activate_module( 'blocks', false, false );
197            }
198
199            return \Jetpack::activate_module( 'contact-form', false, false );
200        }
201
202        // Return true if module is already active
203        return class_exists( 'Jetpack' ) && \Jetpack::is_module_active( 'contact-form' );
204    }
205}