| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | if ( ! defined( 'ABSPATH' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 	exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class WC_Services_Installer { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	 * @var WC_Services_Installer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	 **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	private static $instance = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	static function init() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 		if ( is_null( self::$instance ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 			self::$instance = new WC_Services_Installer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		return self::$instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		add_action( 'admin_init', array( $this, 'add_error_notice' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		add_action( 'admin_init', array( $this, 'try_install' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * Verify the intent to install WooCommerce Services, and kick off installation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	public function try_install() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		if ( isset( $_GET['wc-services-action'] ) && ( 'install' === $_GET['wc-services-action'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 			check_admin_referer( 'wc-services-install' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 			if ( ! current_user_can( 'install_plugins' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 				return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			$result   = $this->install(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			$redirect = wp_get_referer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			if ( false === $result ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 				$redirect = add_query_arg( 'wc-services-install-error', true, $redirect ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			wp_safe_redirect( $redirect ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			exit; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * Set up installation error admin notice. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	public function add_error_notice() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		if ( ! empty( $_GET['wc-services-install-error'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			add_action( 'admin_notices', array( $this, 'error_notice' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 * Notify the user that the installation of WooCommerce Services failed. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 	public function error_notice() { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 		?> | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 		<div class="notice notice-error is-dismissible"> | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 			<p><?php _e( 'There was an error installing WooCommerce Services.', 'jetpack' ); ?></p> | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 		</div> | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 		<?php | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * Download, install, and activate the WooCommerce Services plugin. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @return bool result of installation/activation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	private function install() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		include_once( ABSPATH . '/wp-admin/includes/admin.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		include_once( ABSPATH . '/wp-admin/includes/plugin-install.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		include_once( ABSPATH . '/wp-admin/includes/plugin.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		include_once( ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		$api = plugins_api( 'plugin_information', array( 'slug' => 'woocommerce-services' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		if ( is_wp_error( $api ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		$result   = $upgrader->install( $api->download_link ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		if ( true !== $result ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$result = activate_plugin( 'woocommerce-services/woocommerce-services.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		// activate_plugin() returns null on success | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		return is_null( $result ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  | WC_Services_Installer::init(); | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  | 
            
                        
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.