Completed
Push — woo/services-funnel ( 55dc75...caff50 )
by Jeff
22:39 queued 14:39
created

WC_Services_Installer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 0

6 Methods

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