Completed
Push — woo/services-funnel ( 68e5e7...8c09a9 )
by Jeff
57:06 queued 49:39
created

WC_Services_Installer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A __construct() 0 4 1
B try_install() 0 20 5
A add_error_notice() 0 5 2
A error_notice() 0 7 1
B install() 0 25 3
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
	public function try_install() {
27
		if ( isset( $_GET['wc-services-action'] ) && ( 'install' === $_GET['wc-services-action'] ) ) {
28
			check_admin_referer( 'wc-services-install' );
29
30
			if ( ! current_user_can( 'install_plugins' ) ) {
31
				return;
32
			}
33
34
			$result   = $this->install();
35
			$redirect = wp_get_referer();
36
37
			if ( false === $result ) {
38
				$redirect = add_query_arg( 'wc-services-install-error', true, $redirect );
39
			}
40
41
			wp_safe_redirect( $redirect );
42
43
			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...
44
		}
45
	}
46
47
	public function add_error_notice() {
48
		if ( ! empty( $_GET['wc-services-install-error'] ) ) {
49
			add_action( 'admin_notices', array( $this, 'error_notice' ) );
50
		}
51
	}
52
53
	public function error_notice() {
54
		?>
55
		<div class="notice notice-error is-dismissible">
56
			<p><?php _e( 'There was an error installing WooCommerce Services.', 'jetpack' ); ?></p>
57
		</div>
58
		<?php
59
	}
60
61
	private function install() {
62
		include_once( ABSPATH . '/wp-admin/includes/admin.php' );
63
		include_once( ABSPATH . '/wp-admin/includes/plugin-install.php' );
64
		include_once( ABSPATH . '/wp-admin/includes/plugin.php' );
65
		include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
66
		include_once( ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php' );
67
68
		$api = plugins_api( 'plugin_information', array( 'slug' => 'connect-for-woocommerce' ) );
69
70
		if ( is_wp_error( $api ) ) {
71
			return false;
72
		}
73
74
		$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
75
		$result   = $upgrader->install( $api->download_link );
76
77
		if ( true !== $result ) {
78
			return false;
79
		}
80
81
		$result = activate_plugin( 'connect-for-woocommerce/woocommerce-connect-client.php' );
82
83
		// activate_plugin() returns null on success
84
		return is_null( $result );
85
	}
86
}
87
88
WC_Services_Installer::init();
89