Completed
Push — branch-4.6-built ( 825187 )
by
unknown
70:28 queued 61:31
created

WC_Services_Installer::install()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
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;
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...
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