| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | if ( ! defined( 'ABSPATH' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 	exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class WC_Services_Installer { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	 * @var Jetpack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	 **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	private $jetpack; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 * @var WC_Services_Installer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	private static $instance = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	static function init() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		if ( is_null( self::$instance ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 			self::$instance = new WC_Services_Installer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		return self::$instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$this->jetpack = Jetpack::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		add_action( 'admin_init', array( $this, 'add_error_notice' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		add_action( 'admin_init', array( $this, 'try_install' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * Verify the intent to install WooCommerce Services, and kick off installation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	public function try_install() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		if ( ! isset( $_GET['wc-services-action'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		check_admin_referer( 'wc-services-install' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		$result = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		switch ( $_GET['wc-services-action'] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 			case 'install': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 				if ( current_user_can( 'install_plugins' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 					$this->jetpack->stat( 'jitm', 'wooservices-install-' . JETPACK__VERSION ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 					$result = $this->install(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 					if ( $result ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 						$result = $this->activate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 				break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			case 'activate': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 				if ( current_user_can( 'activate_plugins' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 					$this->jetpack->stat( 'jitm', 'wooservices-activate-' . JETPACK__VERSION ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 					$result = $this->activate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 				break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$redirect = wp_get_referer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		if ( $result ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			$this->jetpack->stat( 'jitm', 'wooservices-activated-' . JETPACK__VERSION ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			$redirect = add_query_arg( 'wc-services-install-error', true, $redirect ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		wp_safe_redirect( $redirect ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		exit; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * Set up installation error admin notice. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 	public function add_error_notice() { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 		if ( ! empty( $_GET['wc-services-install-error'] ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 			add_action( 'admin_notices', array( $this, 'error_notice' ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * Notify the user that the installation of WooCommerce Services failed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	public function error_notice() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		?> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		<div class="notice notice-error is-dismissible"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			<p><?php _e( 'There was an error installing WooCommerce Services.', 'jetpack' ); ?></p> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		</div> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		<?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * Download and install the WooCommerce Services plugin. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * @return bool result of installation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	private function install() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		include_once( ABSPATH . '/wp-admin/includes/admin.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		include_once( ABSPATH . '/wp-admin/includes/plugin-install.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		include_once( ABSPATH . '/wp-admin/includes/plugin.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		include_once( ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		$api = plugins_api( 'plugin_information', array( 'slug' => 'woocommerce-services' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		if ( is_wp_error( $api ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		$result   = $upgrader->install( $api->download_link ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		return true === $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 * Activate the WooCommerce Services plugin. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * @return bool result of activation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	private function activate() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		$result = activate_plugin( 'woocommerce-services/woocommerce-services.php' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		// activate_plugin() returns null on success | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		return is_null( $result ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  | WC_Services_Installer::init(); | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  | 
            
                        
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.