Completed
Push — update/launch-price-ux ( af1100...50e28f )
by Marin
07:13 queued 27s
created

WC_Services_Installer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 1

7 Methods

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