Completed
Push — woo/services-funnel ( 752dfd...f7c1aa )
by Jeff
08:16
created

WC_Services_Installer::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
class WC_Services_Installer {
8
9
	public function init() {
10
		$this->add_error_notice();
11
		$this->try_install();
12
	}
13
14
	public function try_install() {
15
		$action = filter_input( INPUT_GET, 'wc-services-action' );
16
17
		if ( 'install' === $action ) {
18
			check_admin_referer( 'wc-services-install' );
19
20
			$result = $this->install();
21
22
			wp_safe_redirect( add_query_arg( array(
23
				'wc-services-action'        => false,
24
				'_wpnonce'                  => false,
25
				'wc-services-install-error' => ( false === $result ),
26
			) ) );
27
28
			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...
29
		}
30
	}
31
32
	public function add_error_notice() {
33
		$error = filter_input( INPUT_GET, 'wc-services-install-error' );
34
35
		if ( $error ) {
36
			add_action( 'admin_notices', array( $this, 'error_notice' ) );
37
		}
38
	}
39
40
	public function error_notice() {
41
		?>
42
		<div class="notice notice-error is-dismissible">
43
			<p><?php _e( 'There was an error installing WooCommerce Services.', 'jetpack' ); ?></p>
44
		</div>
45
		<?php
46
	}
47
48
	private function install() {
49
		include_once( ABSPATH . '/wp-admin/includes/admin.php' );
50
		include_once( ABSPATH . '/wp-admin/includes/plugin-install.php' );
51
		include_once( ABSPATH . '/wp-admin/includes/plugin.php' );
52
		include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
53
		include_once( ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php' );
54
55
		$api = plugins_api( 'plugin_information', array( 'slug' => 'connect-for-woocommerce' ) );
56
57
		if ( is_wp_error( $api ) ) {
58
			return false;
59
		}
60
61
		$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
62
		$result   = $upgrader->install( $api->download_link );
63
64
		if ( true !== $result ) {
65
			return false;
66
		}
67
68
		$result = activate_plugin( 'connect-for-woocommerce/woocommerce-connect-client.php' );
69
70
		if ( is_wp_error( $result ) ) {
71
			return false;
72
		}
73
	}
74
}
75
76
$wc_services_installer = new WC_Services_Installer();
77
78
add_action( 'admin_init', array( $wc_services_installer, 'init' ) );
79