1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Installs and activates the WooCommerce Services plugin. |
9
|
|
|
*/ |
10
|
|
|
class WC_Services_Installer { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The instance of the Jetpack class. |
14
|
|
|
* |
15
|
|
|
* @var Jetpack |
16
|
|
|
*/ |
17
|
|
|
private $jetpack; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The singleton instance of this class. |
21
|
|
|
* |
22
|
|
|
* @var WC_Services_Installer |
23
|
|
|
*/ |
24
|
|
|
private static $instance = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns the singleton instance of this class. |
28
|
|
|
* |
29
|
|
|
* @return object The WC_Services_Installer object. |
30
|
|
|
*/ |
31
|
|
|
public static function init() { |
32
|
|
|
if ( is_null( self::$instance ) ) { |
33
|
|
|
self::$instance = new WC_Services_Installer(); |
34
|
|
|
} |
35
|
|
|
return self::$instance; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
*/ |
41
|
|
|
public function __construct() { |
42
|
|
|
add_action( 'jetpack_loaded', array( $this, 'on_jetpack_loaded' ) ); |
43
|
|
|
add_action( 'admin_init', array( $this, 'add_error_notice' ) ); |
44
|
|
|
add_action( 'admin_init', array( $this, 'try_install' ) ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Runs on Jetpack being ready to load its packages. |
49
|
|
|
* |
50
|
|
|
* @param Jetpack $jetpack object. |
51
|
|
|
*/ |
52
|
|
|
public function on_jetpack_loaded( $jetpack ) { |
53
|
|
|
$this->jetpack = $jetpack; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Verify the intent to install WooCommerce Services, and kick off installation. |
58
|
|
|
*/ |
59
|
|
|
public function try_install() { |
60
|
|
|
if ( ! isset( $_GET['wc-services-action'] ) ) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
check_admin_referer( 'wc-services-install' ); |
64
|
|
|
|
65
|
|
|
$result = false; |
66
|
|
|
|
67
|
|
|
switch ( $_GET['wc-services-action'] ) { |
68
|
|
|
case 'install': |
69
|
|
|
if ( current_user_can( 'install_plugins' ) ) { |
70
|
|
|
$this->jetpack->stat( 'jitm', 'wooservices-install-' . JETPACK__VERSION ); |
71
|
|
|
$result = $this->install(); |
72
|
|
|
if ( $result ) { |
73
|
|
|
$result = $this->activate(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
break; |
77
|
|
|
|
78
|
|
|
case 'activate': |
79
|
|
|
if ( current_user_can( 'activate_plugins' ) ) { |
80
|
|
|
$this->jetpack->stat( 'jitm', 'wooservices-activate-' . JETPACK__VERSION ); |
81
|
|
|
$result = $this->activate(); |
82
|
|
|
} |
83
|
|
|
break; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ( isset( $_GET['redirect'] ) ) { |
87
|
|
|
$redirect = home_url( esc_url_raw( wp_unslash( $_GET['redirect'] ) ) ); |
88
|
|
|
} else { |
89
|
|
|
$redirect = admin_url(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ( $result ) { |
93
|
|
|
$this->jetpack->stat( 'jitm', 'wooservices-activated-' . JETPACK__VERSION ); |
94
|
|
|
} else { |
95
|
|
|
$redirect = add_query_arg( 'wc-services-install-error', true, $redirect ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
wp_safe_redirect( $redirect ); |
99
|
|
|
|
100
|
|
|
exit; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set up installation error admin notice. |
105
|
|
|
*/ |
106
|
|
|
public function add_error_notice() { |
107
|
|
|
if ( ! empty( $_GET['wc-services-install-error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
108
|
|
|
add_action( 'admin_notices', array( $this, 'error_notice' ) ); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Notify the user that the installation of WooCommerce Services failed. |
114
|
|
|
*/ |
115
|
|
|
public function error_notice() { |
116
|
|
|
?> |
117
|
|
|
<div class="notice notice-error is-dismissible"> |
118
|
|
|
<p><?php esc_html_e( 'There was an error installing WooCommerce Services.', 'jetpack' ); ?></p> |
119
|
|
|
</div> |
120
|
|
|
<?php |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Download and install the WooCommerce Services plugin. |
125
|
|
|
* |
126
|
|
|
* @return bool result of installation |
127
|
|
|
*/ |
128
|
|
|
private function install() { |
129
|
|
|
include_once ABSPATH . '/wp-admin/includes/admin.php'; |
130
|
|
|
include_once ABSPATH . '/wp-admin/includes/plugin-install.php'; |
131
|
|
|
include_once ABSPATH . '/wp-admin/includes/plugin.php'; |
132
|
|
|
include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; |
133
|
|
|
include_once ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php'; |
134
|
|
|
|
135
|
|
|
$api = plugins_api( 'plugin_information', array( 'slug' => 'woocommerce-services' ) ); |
136
|
|
|
|
137
|
|
|
if ( is_wp_error( $api ) ) { |
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() ); |
142
|
|
|
$result = $upgrader->install( $api->download_link ); |
143
|
|
|
|
144
|
|
|
return true === $result; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Activate the WooCommerce Services plugin. |
149
|
|
|
* |
150
|
|
|
* @return bool result of activation |
151
|
|
|
*/ |
152
|
|
|
private function activate() { |
153
|
|
|
$result = activate_plugin( 'woocommerce-services/woocommerce-services.php' ); |
154
|
|
|
|
155
|
|
|
// Activate_plugin() returns null on success. |
156
|
|
|
return is_null( $result ); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
WC_Services_Installer::init(); |
161
|
|
|
|