Completed
Push — woo/services-funnel ( 3e7ed4 )
by Nabeel
09:59
created

WC_Services_Installer::install()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 8
nop 0
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
if ( ! class_exists( 'WC_Connect_Admin_Setup_Wizard' ) ) :
8
9
class WC_Services_Installer {
10
11
	public function notice() {
12
		$this->try_install();
13
		$link = add_query_arg( array( 'wc-services-action' => 'install' ) );
14
?>
15
		<div class="notice notice-warning">
16
			<p>
17
				Hello World. <a href="<?php echo $link; ?>">install</a>
18
			</p>
19
		</div>
20
<?php
21
	}
22
23
	public function try_install() {
24
		$install = filter_input( INPUT_GET, 'wc-services-action' );
25
		if ( 'install' === $install ) {
26
			$this->install();
27
			wp_redirect( esc_url_raw( remove_query_arg( array( 'wc-services-action' ) ) ) );
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
	private function install() {
33
		include_once( ABSPATH . '/wp-admin/includes/admin.php' );
34
		include_once( ABSPATH . '/wp-admin/includes/plugin-install.php' );
35
		include_once( ABSPATH . '/wp-admin/includes/plugin.php' );
36
		include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
37
		include_once( ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php' );
38
39
		$api = plugins_api( 'plugin_information', array(
40
			'slug'   => 'connect-for-woocommerce',
41
		) );
42
43
		if ( is_wp_error( $api ) ) {
44
			wp_die( $api );
45
		}
46
47
		$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
48
		$result   = $upgrader->install( $api->download_link );
49
50
		if ( is_wp_error( $result ) ) {
51
			wp_die( $result );
52
		}
53
54
		$result = activate_plugin( 'connect-for-woocommerce/woocommerce-connect-client.php' );
55
56
		if ( is_wp_error( $result ) ) {
57
			wp_die( $result );
58
		}
59
	}
60
}
61
62
$wc_services_installer = new WC_Services_Installer();
63
64
add_action( 'init', array( $wc_services_installer, 'try_install' ) );
65
add_action( 'admin_notices', array( $wc_services_installer, 'notice' ) );
66
67
endif;
68