Completed
Push — add/jetpack-plan-support ( c36d23...95100b )
by
unknown
14:33 queued 05:53
created

WC_Services_Installer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A __construct() 0 6 1
B try_install() 0 22 5
A add_error_notice() 0 5 2
A error_notice() 0 7 1
B install() 0 27 3
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'] ) && ( 'install' === $_GET['wc-services-action'] ) ) {
38
			check_admin_referer( 'wc-services-install' );
39
40
			if ( ! current_user_can( 'install_plugins' ) ) {
41
				return;
42
			}
43
44
			$result   = $this->install();
45
			$redirect = wp_get_referer();
46
47
			if ( false === $result ) {
48
				$redirect = add_query_arg( 'wc-services-install-error', true, $redirect );
49
			} else {
50
				$this->jetpack->stat( 'jitm', 'wooservices-activated-' . JETPACK__VERSION );
51
			}
52
53
			wp_safe_redirect( $redirect );
54
55
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method try_install() contains an exit expression.

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 exit expression 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.

Loading history...
56
		}
57
	}
58
59
	/**
60
	 * Set up installation error admin notice.
61
	 */
62
	public function add_error_notice() {
63
		if ( ! empty( $_GET['wc-services-install-error'] ) ) {
64
			add_action( 'admin_notices', array( $this, 'error_notice' ) );
65
		}
66
	}
67
68
	/**
69
	 * Notify the user that the installation of WooCommerce Services failed.
70
	 */
71
	public function error_notice() {
72
		?>
73
		<div class="notice notice-error is-dismissible">
74
			<p><?php _e( 'There was an error installing WooCommerce Services.', 'jetpack' ); ?></p>
75
		</div>
76
		<?php
77
	}
78
79
	/**
80
	 * Download, install, and activate the WooCommerce Services plugin.
81
	 *
82
	 * @return bool result of installation/activation
83
	 */
84
	private function install() {
85
		$this->jetpack->stat( 'jitm', 'wooservices-install-' . JETPACK__VERSION );
86
87
		include_once( ABSPATH . '/wp-admin/includes/admin.php' );
88
		include_once( ABSPATH . '/wp-admin/includes/plugin-install.php' );
89
		include_once( ABSPATH . '/wp-admin/includes/plugin.php' );
90
		include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
91
		include_once( ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php' );
92
93
		$api = plugins_api( 'plugin_information', array( 'slug' => 'woocommerce-services' ) );
94
95
		if ( is_wp_error( $api ) ) {
96
			return false;
97
		}
98
99
		$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
100
		$result   = $upgrader->install( $api->download_link );
101
102
		if ( true !== $result ) {
103
			return false;
104
		}
105
106
		$result = activate_plugin( 'woocommerce-services/woocommerce-services.php' );
107
108
		// activate_plugin() returns null on success
109
		return is_null( $result );
110
	}
111
}
112
113
WC_Services_Installer::init();
114