@@ -175,69 +175,99 @@ public static function install_premium_plugin( $plugin, $provider, $activate ) {
175175 // Provision a license for the premium plugin, this returns basename and download URL
176176 $ license_response = $ pls_utility ->provision_license ( $ plugin , $ provider );
177177 if ( is_wp_error ( $ license_response ) ) {
178+ $ license_response ->add (
179+ 'nfd_installer_error ' ,
180+ __ ( 'Failed to provision license ' , 'wp-module-installer ' ),
181+ array (
182+ 'plugin ' => $ plugin ,
183+ 'provider ' => $ provider ,
184+ )
185+ );
178186 return $ license_response ;
179187 }
180188
181189 // Get the plugin basename from the license response
182190 $ plugin_basename = ! empty ( $ license_response ['basename ' ] ) ? $ license_response ['basename ' ] : false ;
183191
184- // Check if the plugin is already installed
185- if ( $ plugin_basename && self ::is_plugin_installed ( $ plugin_basename ) ) {
186- // Check if the plugin is active
187- if ( is_plugin_active ( $ plugin_basename ) ) {
188- // If plugin is already installed and active, return success
189- return new \WP_REST_Response (
192+ // Bail if no basename
193+ if ( ! $ plugin_basename ) {
194+ return new \WP_Error (
195+ 'nfd_installer_error ' ,
196+ __ ( 'Plugin basename is missing. ' , 'wp-module-installer ' ),
197+ array (
198+ 'plugin ' => $ plugin ,
199+ 'provider ' => $ provider ,
200+ )
201+ );
202+ }
203+
204+ // If NOT installed, install plugin
205+ if ( ! self ::is_plugin_installed ( $ plugin_basename ) ) {
206+ // Check if the download URL is present in the license response
207+ if ( empty ( $ license_response ['downloadUrl ' ] ) ) {
208+ return new \WP_Error (
209+ 'nfd_installer_error ' ,
210+ __ ( 'Download URL is missing for premium plugin ' , 'wp-module-installer ' ),
190211 array (
191- 'message ' => __ ( 'Plugin is already installed and active: ' , 'wp-module-installer ' ) . $ plugin ,
192- ),
193- 200
212+ 'plugin ' => $ plugin ,
213+ 'provider ' => $ provider ,
214+ 'basename ' => $ plugin_basename ,
215+ )
194216 );
195217 }
218+ $ install_status = self ::install_from_zip ( $ license_response ['downloadUrl ' ], $ activate );
219+ if ( is_wp_error ( $ install_status ) ) {
220+ $ install_status ->add (
221+ 'nfd_installer_error ' ,
222+ __ ( 'Failed to install the plugin ' , 'wp-module-installer ' ),
223+ array (
224+ 'plugin ' => $ plugin ,
225+ 'provider ' => $ provider ,
226+ 'basename ' => $ plugin_basename ,
227+ 'download_url ' => $ license_response ['downloadUrl ' ],
228+ )
229+ );
230+ return $ install_status ;
231+ }
232+ }
196233
197- // Activate the plugin if it's installed but not active
234+ // If $activate is true, activate the plugin via \activate_plugin
235+ if ( $ activate && ! is_plugin_active ( $ plugin_basename ) ) {
198236 $ activate_plugin_response = activate_plugin ( $ plugin_basename );
199237 if ( is_wp_error ( $ activate_plugin_response ) ) {
200- return new \WP_Error ( 'nfd_installer_error ' , __ ( 'Failed to activate the plugin: ' , 'wp-module-installer ' ) . $ plugin );
201- }
202-
203- // Activate the license
204- $ activation_response = $ pls_utility ->activate_license ( $ plugin );
205- if ( is_wp_error ( $ activation_response ) ) {
206- return new \WP_Error ( 'nfd_installer_error ' , __ ( 'Failed to activate the license for the premium plugin: ' , 'wp-module-installer ' ) . $ plugin );
238+ $ activate_plugin_response ->add (
239+ 'nfd_installer_error ' ,
240+ __ ( 'Failed to activate the plugin ' , 'wp-module-installer ' ),
241+ array (
242+ 'plugin ' => $ plugin ,
243+ 'provider ' => $ provider ,
244+ 'basename ' => $ plugin_basename ,
245+ )
246+ );
247+ return $ activate_plugin_response ;
207248 }
249+ }
208250
209- // Return success response
210- return new \WP_REST_Response (
251+ // Activate the license
252+ // Should we do this here or let the activation hook handle it - see WPAdmin/Listeners/InstallerListener.php
253+ $ activation_response = $ pls_utility ->activate_license ( $ plugin );
254+ if ( is_wp_error ( $ activation_response ) ) {
255+ $ activation_response ->add (
256+ 'nfd_installer_error ' ,
257+ __ ( 'Failed to activate the license ' , 'wp-module-installer ' ),
211258 array (
212- 'message ' => __ ( 'Successfully provisioned and installed: ' , 'wp-module-installer ' ) . $ plugin ,
213- ),
214- 200
259+ 'plugin ' => $ plugin ,
260+ 'provider ' => $ provider ,
261+ 'basename ' => $ plugin_basename ,
262+ )
215263 );
216- }
217-
218- // Check if the download URL is present in the license response
219- if ( empty ( $ license_response ['downloadUrl ' ] ) ) {
220- return new \WP_Error ( 'nfd_installer_error ' , __ ( 'Download URL is missing for premium plugin: ' , 'wp-module-installer ' ) . $ plugin );
221- }
222-
223- // Plugin is not installed, proceed with installation
224- $ install_status = self ::install_from_zip ( $ license_response ['downloadUrl ' ], $ activate );
225- if ( is_wp_error ( $ install_status ) ) {
226- return new \WP_Error ( 'nfd_installer_error ' , __ ( 'Failed to install or activate the premium plugin: ' , 'wp-module-installer ' ) . $ plugin );
227- }
228-
229- // If activation is requested, activate the license
230- if ( $ activate ) {
231- $ activation_response = $ pls_utility ->activate_license ( $ plugin );
232- if ( is_wp_error ( $ activation_response ) ) {
233- return new \WP_Error ( 'nfd_installer_error ' , __ ( 'Failed to activate the license for the premium plugin: ' , 'wp-module-installer ' ) . $ plugin );
234- }
264+ return $ activation_response ;
235265 }
236266
237267 // Return success response
238268 return new \WP_REST_Response (
239269 array (
240- 'message ' => __ ( 'Successfully provisioned and installed: ' , 'wp-module-installer ' ) . $ plugin ,
270+ 'message ' => __ ( 'Successfully provisioned and installed the plugin ' , 'wp-module-installer ' ),
241271 ),
242272 200
243273 );
0 commit comments