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

WC_Services_Installer::__construct()   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
	/**
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