Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 85
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
PluginService
0.00% covered (danger)
0.00%
0 / 85
0.00% covered (danger)
0.00%
0 / 9
2256
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 / 23
0.00% covered (danger)
0.00%
0 / 1
56
 setup_plugin
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 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 / 7
0.00% covered (danger)
0.00%
0 / 1
56
 enable_yoast_seo_premium
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
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            $basename = $plugin['basename'] ?? $plugin['path'] ?? '';
108            $activate_plugin_response = activate_plugin( $basename );
109            if ( is_wp_error( $activate_plugin_response ) ) {
110                $activate_plugin_response->add(
111                    'nfd_installer_error',
112                    __( 'Failed to activate the plugin: ', 'nfd-wonder-blocks' ) . $plugin['slug'],
113                    array(
114                        'plugin'   => $plugin['slug'],
115                        'provider' => $plugin['provider'],
116                        'basename' => $basename,
117                    )
118                );
119                return $activate_plugin_response;
120            }
121            return true;
122        }
123
124        $slug                   = is_array( $plugin ) ? $plugin['slug'] : $plugin;
125        $plugin_activation_task = new PluginActivationTask( $slug );
126        $result                 = $plugin_activation_task->execute();
127
128        if ( is_wp_error( $result ) ) {
129            return false;
130        }
131
132        return true;
133    }
134
135    /**
136     * Setup a single plugin integration.
137     *
138     * @param string|array $plugin Plugin slug or plugin array with 'slug' key.
139     * @return boolean True if setup was performed, false otherwise.
140     */
141    public static function setup_plugin( $plugin ) {
142        $slug = is_array( $plugin ) ? $plugin['slug'] : $plugin;
143
144        // Currently we only have special setup for Jetpack
145        if ( 'jetpack' === $slug ) {
146            self::enable_jetpack_forms_module();
147            return true;
148        }
149
150        if ( 'wordpress-seo-premium' === $slug ) {
151            return self::enable_yoast_seo_premium();
152        }
153
154        return false;
155    }
156
157    /**
158     * Activate or install the specified plugins.
159     *
160     * @param array $plugins Array of plugins to be activated or installed.
161     * @return array Results with status for each plugin operation
162     */
163    public static function activate( $plugins ) {
164        $results = array();
165
166        if ( ! is_array( $plugins ) || empty( $plugins ) ) {
167            return $results;
168        }
169
170        foreach ( $plugins as $plugin ) {
171            if ( self::is_installed( $plugin ) ) {
172                $results[ $plugin['slug'] ] = self::activate_plugin( $plugin );
173            } else {
174                $results[ $plugin['slug'] ] = self::install( $plugin, true );
175            }
176        }
177
178        return $results;
179    }
180
181    /**
182     * Setup the integration for specific plugins.
183     *
184     * This method loops through the provided list of plugins and enables
185     * specific functionality or modules based on the plugin slug.
186     *
187     * @param array $plugins Array of plugins to be integrated. Each element is expected to have a 'slug' key.
188     * @return array Results of setup operations
189     */
190    public static function setup( $plugins ) {
191        $results = array();
192
193        if ( ! is_array( $plugins ) || empty( $plugins ) ) {
194            return $results;
195        }
196
197        foreach ( $plugins as $plugin ) {
198            if ( isset( $plugin['slug'] ) ) {
199                $results[ $plugin['slug'] ] = self::setup_plugin( $plugin );
200            }
201        }
202
203        return $results;
204    }
205
206    /**
207     * Enable the Jetpack Forms module.
208     *
209     * @return boolean True if module was activated or was already active
210     */
211    public static function enable_jetpack_forms_module() {
212        if ( class_exists( 'Jetpack' ) ) {
213            if ( ! \Jetpack::is_module_active( 'contact-form' ) ) {
214                \Jetpack::activate_module( 'contact-form', false, false );
215            }
216
217            if ( ! \Jetpack::is_module_active( 'blocks' ) ) {
218                \Jetpack::activate_module( 'blocks', false, false );
219            }
220
221            return \Jetpack::is_module_active( 'contact-form' ) && \Jetpack::is_module_active( 'blocks' );
222        }
223
224        // Return true if module is already active
225        return class_exists( 'Jetpack' ) && \Jetpack::is_module_active( 'contact-form' ) && \Jetpack::is_module_active( 'blocks' );
226    }
227
228    /**
229     * Enable the Yoast SEO Premium module.
230     *
231     * @return boolean True if module was activated or was already active
232     */
233    public static function enable_yoast_seo_premium() {
234        if ( class_exists( 'WPSEO_Options' ) ) {
235            // Disable redirect to Yoast onboarding.
236            \WPSEO_Options::set( 'should_redirect_after_install', false );
237        }
238
239        return true;
240    }
241}