Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 96 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
PluginService | |
0.00% |
0 / 96 |
|
0.00% |
0 / 3 |
870 | |
0.00% |
0 / 1 |
initialize | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
132 | |||
activate_init_plugins | |
0.00% |
0 / 44 |
|
0.00% |
0 / 1 |
90 | |||
configure_activation_transient | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
90 |
1 | <?php |
2 | namespace NewfoldLabs\WP\Module\Onboarding\Services; |
3 | |
4 | use NewfoldLabs\WP\Module\Onboarding\WP_Admin; |
5 | use NewfoldLabs\WP\Module\Onboarding\Data\Options; |
6 | use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller; |
7 | use NewfoldLabs\WP\Module\Installer\TaskManagers\PluginActivationTaskManager; |
8 | use NewfoldLabs\WP\Module\Installer\TaskManagers\PluginInstallTaskManager; |
9 | use NewfoldLabs\WP\Module\Installer\TaskManagers\PluginDeactivationTaskManager; |
10 | use NewfoldLabs\WP\Module\Installer\Tasks\PluginActivationTask; |
11 | use NewfoldLabs\WP\Module\Installer\Tasks\PluginDeactivationTask; |
12 | use NewfoldLabs\WP\Module\Installer\Tasks\PluginInstallTask; |
13 | use NewfoldLabs\WP\Module\Onboarding\Data\Data; |
14 | use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService; |
15 | use NewfoldLabs\WP\Module\Onboarding\Data\Plugins; |
16 | use NewfoldLabs\WP\Module\Onboarding\Data\SiteFeatures; |
17 | |
18 | /** |
19 | * Class for providing plugin related services. |
20 | */ |
21 | class PluginService { |
22 | /** |
23 | * Queues the initial list of Plugin Installs for a flow. |
24 | * |
25 | * @return boolean |
26 | */ |
27 | public static function initialize() { |
28 | |
29 | $init_plugins = array(); |
30 | |
31 | $flow = Data::current_flow(); |
32 | |
33 | if ( 'sitegen' === $flow && SiteGenService::is_enabled() ) { |
34 | $init_plugins = Plugins::get_init(); |
35 | |
36 | /** |
37 | * We are disabling AI SiteGen plugin recommendations until we have a better way to handle it. |
38 | * TODO: Evaluate and re-enable AI SiteGen plugin recommendations. |
39 | */ |
40 | // Convert { slug->slug } to hash for faster search |
41 | // As Php uses array as { [0] -> slug_name } and that won't work with array_key_exists |
42 | // $plugin_slugs = array_column( $init_plugins, 'slug', 'slug' ); |
43 | // Get AI SiteGen plugin recommendations |
44 | // $default_plugins = SiteGenService::get_plugin_recommendations(); |
45 | // if ( is_wp_error( $default_plugins ) ) { |
46 | // return $default_plugins; |
47 | // } |
48 | // Iterate and ensure no duplicates are added |
49 | // foreach ( $default_plugins as $default_plugin ) { |
50 | // if ( ! array_key_exists( $default_plugin['slug'], $plugin_slugs ) ) { |
51 | // $init_plugins[] = $default_plugin; |
52 | // } |
53 | // } |
54 | } else { |
55 | // Get the initial list of plugins to be installed based on the plan. |
56 | $init_plugins = array_merge( Plugins::get_init(), SiteFeatures::get_init() ); |
57 | } |
58 | |
59 | foreach ( $init_plugins as $init_plugin ) { |
60 | $init_plugin_type = PluginInstaller::get_plugin_type( $init_plugin['slug'] ); |
61 | $init_plugin_path = PluginInstaller::get_plugin_path( $init_plugin['slug'], $init_plugin_type ); |
62 | if ( ! $init_plugin_path ) { |
63 | continue; |
64 | } |
65 | // Checks if a plugin with the given slug and activation criteria already exists. |
66 | if ( ! PluginInstaller::is_plugin_installed( $init_plugin_path ) ) { |
67 | // Add a new PluginInstallTask to the Plugin install queue. |
68 | PluginInstallTaskManager::add_to_queue( |
69 | new PluginInstallTask( |
70 | $init_plugin['slug'], |
71 | $init_plugin['activate'], |
72 | isset( $init_plugin['priority'] ) ? $init_plugin['priority'] : 0 |
73 | ) |
74 | ); |
75 | continue; |
76 | } |
77 | |
78 | if ( ! $init_plugin['activate'] && PluginInstaller::is_active( $init_plugin_path ) ) { |
79 | PluginDeactivationTaskManager::add_to_queue( |
80 | new PluginDeactivationTask( |
81 | $init_plugin['slug'] |
82 | ) |
83 | ); |
84 | } |
85 | |
86 | if ( $init_plugin['activate'] && ! PluginInstaller::is_active( $init_plugin_path ) ) { |
87 | PluginActivationTaskManager::add_to_queue( |
88 | new PluginActivationTask( |
89 | $init_plugin['slug'] |
90 | ) |
91 | ); |
92 | } |
93 | } |
94 | |
95 | return true; |
96 | } |
97 | |
98 | /** |
99 | * Activates the initial list of plugins, filtering out non-selected site features. |
100 | * |
101 | * @return boolean |
102 | */ |
103 | public static function activate_init_plugins() { |
104 | $init_plugins = Plugins::get_init(); |
105 | $filtered_init_plugins = SiteFeatures::filter( $init_plugins, true ); |
106 | $site_features_selected = SiteFeatures::get_selected(); |
107 | $site_features_unselected = SiteFeatures::get_unselected(); |
108 | $final_init_plugins = array_merge( $filtered_init_plugins, $site_features_selected ); |
109 | |
110 | foreach ( $final_init_plugins as $init_plugin ) { |
111 | $init_plugin_type = PluginInstaller::get_plugin_type( $init_plugin['slug'] ); |
112 | $init_plugin_path = PluginInstaller::get_plugin_path( $init_plugin['slug'], $init_plugin_type ); |
113 | if ( ! $init_plugin_path ) { |
114 | continue; |
115 | } |
116 | // Checks if a plugin with the given slug and activation criteria already exists. |
117 | if ( PluginInstaller::is_plugin_installed( $init_plugin_path ) ) { |
118 | // Add a new PluginInstallTask to the Plugin install queue. |
119 | PluginActivationTaskManager::add_to_queue( |
120 | new PluginActivationTask( |
121 | $init_plugin['slug'] |
122 | ) |
123 | ); |
124 | continue; |
125 | } |
126 | |
127 | PluginInstallTaskManager::add_to_queue( |
128 | new PluginInstallTask( |
129 | $init_plugin['slug'], |
130 | true, |
131 | isset( $init_plugin['priority'] ) ? $init_plugin['priority'] : 0 |
132 | ) |
133 | ); |
134 | } |
135 | |
136 | foreach ( $site_features_unselected as $init_plugin ) { |
137 | $init_plugin_type = PluginInstaller::get_plugin_type( $init_plugin['slug'] ); |
138 | $init_plugin_path = PluginInstaller::get_plugin_path( $init_plugin['slug'], $init_plugin_type ); |
139 | if ( ! $init_plugin_path ) { |
140 | continue; |
141 | } |
142 | // Checks if a plugin with the given slug and activation criteria already exists. |
143 | if ( PluginInstaller::is_plugin_installed( $init_plugin_path ) ) { |
144 | // Add a new PluginDeactivationTask to the Plugin Deactivation queue. |
145 | PluginDeactivationTaskManager::add_to_queue( |
146 | new PluginDeactivationTask( |
147 | $init_plugin['slug'] |
148 | ) |
149 | ); |
150 | continue; |
151 | } |
152 | |
153 | PluginInstallTaskManager::add_to_queue( |
154 | new PluginInstallTask( |
155 | $init_plugin['slug'], |
156 | false, |
157 | isset( $init_plugin['priority'] ) ? $init_plugin['priority'] : 0 |
158 | ) |
159 | ); |
160 | } |
161 | |
162 | return true; |
163 | } |
164 | |
165 | /** |
166 | * Sets up a Transient to activate plugins and filter_active_plugins |
167 | * |
168 | * @return void |
169 | */ |
170 | public static function configure_activation_transient() { |
171 | global $pagenow; |
172 | |
173 | switch ( $pagenow ) { |
174 | case 'index.php': |
175 | // If the page is nfd-onboarding. Ignore lint since WP_Admin::$slug is a static string. |
176 | // phpcs:ignore |
177 | if ( isset( $_GET['page'] ) && ( WP_Admin::$slug === \sanitize_text_field( $_GET['page'] ) ) ) { |
178 | if ( '1' !== get_transient( Options::get_option_name( 'filter_active_plugins' ) ) ) { |
179 | set_transient( Options::get_option_name( 'filter_active_plugins' ), '1', 20 * MINUTE_IN_SECONDS ); |
180 | } |
181 | } |
182 | break; |
183 | default: |
184 | if ( '1' === get_transient( Options::get_option_name( 'filter_active_plugins' ) ) ) { |
185 | delete_transient( Options::get_option_name( 'filter_active_plugins' ) ); |
186 | $flow = Data::current_flow(); |
187 | if ( 'sitegen' !== $flow ) { |
188 | self::activate_init_plugins(); |
189 | } |
190 | } |
191 | break; |
192 | } |
193 | |
194 | // Add hook to activate plugins after transient is deleted |
195 | add_filter( |
196 | 'option_active_plugins', |
197 | function ( $plugins ) { |
198 | if ( '1' === get_transient( Options::get_option_name( 'filter_active_plugins' ) ) ) { |
199 | return Plugins::get_active_plugins_list(); |
200 | } |
201 | return $plugins; |
202 | } |
203 | ); |
204 | } |
205 | } |