| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin: Addon/Plugin installer |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @copyright Copyright (c) 2022, Sandhills Development, LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 4.4.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SimplePay\Core\Admin\Addon; |
| 13 |
|
| 14 |
use Plugin_Upgrader; |
| 15 |
use SimplePay\Core\EventManagement\SubscriberInterface; |
| 16 |
use WP_Ajax_Upgrader_Skin; |
| 17 |
|
| 18 |
/** |
| 19 |
* AddonInstaller class. |
| 20 |
* |
| 21 |
* @since 4.4.0 |
| 22 |
*/ |
| 23 |
class AddonInstaller implements SubscriberInterface { |
| 24 |
|
| 25 |
/** |
| 26 |
* {@inheritdoc} |
| 27 |
*/ |
| 28 |
public function get_subscribed_events() { |
| 29 |
return array( |
| 30 |
'wp_ajax_simpay_activate_addon' => 'activate_addon', |
| 31 |
'wp_ajax_simpay_deactivate_addon' => 'deactivate_addon', |
| 32 |
'wp_ajax_simpay_install_addon' => 'install_addon', |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Listens for addon/plugin AJAX activation requests. |
| 38 |
* |
| 39 |
* @since 4.4.0 |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function activate_addon() { |
| 44 |
// Run a security check. |
| 45 |
check_ajax_referer( 'simpay-admin', 'nonce' ); |
| 46 |
|
| 47 |
// Check for permissions. |
| 48 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 49 |
wp_send_json_error( |
| 50 |
esc_html__( |
| 51 |
'Plugin activation is disabled for you on this site.', |
| 52 |
'stripe' |
| 53 |
) |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
$type = 'addon'; |
| 58 |
|
| 59 |
if ( isset( $_POST['plugin'] ) ) { |
| 60 |
if ( ! empty( $_POST['type'] ) ) { |
| 61 |
$type = sanitize_key( $_POST['type'] ); |
| 62 |
} |
| 63 |
|
| 64 |
/** @var string $plugin */ |
| 65 |
$plugin = wp_unslash( $_POST['plugin'] ); |
| 66 |
$plugin = sanitize_text_field( $plugin ); |
| 67 |
$activate = activate_plugins( $plugin ); |
| 68 |
|
| 69 |
if ( ! is_wp_error( $activate ) ) { |
| 70 |
if ( $type === 'plugin' ) { |
| 71 |
wp_send_json_success( |
| 72 |
esc_html__( 'Plugin activated.', 'stripe' ) |
| 73 |
); |
| 74 |
} else { |
| 75 |
wp_send_json_success( |
| 76 |
esc_html__( 'Addon activated.', 'stripe' ) |
| 77 |
); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if ( $type === 'plugin' ) { |
| 83 |
wp_send_json_error( |
| 84 |
esc_html__( |
| 85 |
'Could not activate the plugin. Please activate it on the Plugins page.', |
| 86 |
'stripe' |
| 87 |
) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
wp_send_json_error( |
| 92 |
esc_html__( |
| 93 |
'Could not activate the addon. Please activate it on the Plugins page.', |
| 94 |
'stripe' |
| 95 |
) |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Listens for addon/plugin AJAX deactivation requests. |
| 101 |
* |
| 102 |
* @since 4.4.0 |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function deactivate_addon() { |
| 107 |
// Run a security check. |
| 108 |
check_ajax_referer( 'simpay-admin', 'nonce' ); |
| 109 |
|
| 110 |
// Check for permissions. |
| 111 |
if ( ! current_user_can( 'deactivate_plugins' ) ) { |
| 112 |
wp_send_json_error( |
| 113 |
esc_html__( |
| 114 |
'Plugin deactivation is disabled for you on this site.', |
| 115 |
'stripe' |
| 116 |
) |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
$type = empty( $_POST['type'] ) |
| 121 |
? 'addon' |
| 122 |
: sanitize_key( $_POST['type'] ); |
| 123 |
|
| 124 |
if ( isset( $_POST['plugin'] ) ) { |
| 125 |
/** @var string $plugin */ |
| 126 |
$plugin = wp_unslash( $_POST['plugin'] ); |
| 127 |
$plugin = sanitize_text_field( $plugin ); |
| 128 |
|
| 129 |
deactivate_plugins( $plugin ); |
| 130 |
|
| 131 |
if ( $type === 'plugin' ) { |
| 132 |
wp_send_json_success( |
| 133 |
esc_html__( 'Plugin deactivated.', 'stripe' ) |
| 134 |
); |
| 135 |
} else { |
| 136 |
wp_send_json_success( |
| 137 |
esc_html__( 'Addon deactivated.', 'stripe' ) |
| 138 |
); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
wp_send_json_error( |
| 143 |
esc_html__( |
| 144 |
'Could not deactivate the addon. Please deactivate from the Plugins page.', |
| 145 |
'stripe' |
| 146 |
) |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Listens for addon/plugin AJAX installation requests. |
| 152 |
* |
| 153 |
* @since 4.4.0 |
| 154 |
* |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
public function install_addon() { |
| 158 |
// Run a security check. |
| 159 |
check_ajax_referer( 'simpay-admin', 'nonce' ); |
| 160 |
|
| 161 |
$generic_error = esc_html__( |
| 162 |
'There was an error while performing your request.', |
| 163 |
'stripe' |
| 164 |
); |
| 165 |
|
| 166 |
$type = ! empty( $_POST['type'] ) |
| 167 |
? sanitize_key( $_POST['type'] ) |
| 168 |
: 'addon'; |
| 169 |
|
| 170 |
// Check if new installations are allowed. |
| 171 |
if ( ! current_user_can( 'install_plugins' ) ) { |
| 172 |
wp_send_json_error( $generic_error ); |
| 173 |
} |
| 174 |
|
| 175 |
$error = $type === 'plugin' |
| 176 |
? esc_html__( |
| 177 |
'Could not install the plugin. Please download and install it manually.', |
| 178 |
'stripe' |
| 179 |
) |
| 180 |
: esc_html__( |
| 181 |
'Could not install the addon. Please download it from wpforms.com and install it manually.', |
| 182 |
'stripe' |
| 183 |
); |
| 184 |
|
| 185 |
/** @var string $plugin */ |
| 186 |
$plugin = ! empty( $_POST['plugin'] ) |
| 187 |
? wp_unslash( $_POST['plugin'] ) |
| 188 |
: ''; |
| 189 |
|
| 190 |
$plugin_url = ! empty( $plugin ) |
| 191 |
? esc_url_raw( $plugin ) |
| 192 |
: ''; |
| 193 |
|
| 194 |
if ( empty( $plugin_url ) ) { |
| 195 |
wp_send_json_error( $error ); |
| 196 |
} |
| 197 |
|
| 198 |
// Set the current screen to avoid undefined notices. |
| 199 |
set_current_screen( 'simpay_page_simpay-settings' ); |
| 200 |
|
| 201 |
// Prepare variables. |
| 202 |
$url = esc_url_raw( |
| 203 |
add_query_arg( |
| 204 |
array( |
| 205 |
'post_type' => 'simple-pay', |
| 206 |
'page' => 'simpay-about-us', |
| 207 |
), |
| 208 |
admin_url( 'edit.php' ) |
| 209 |
) |
| 210 |
); |
| 211 |
|
| 212 |
ob_start(); |
| 213 |
$creds = request_filesystem_credentials( $url, '', false, '', null ); |
| 214 |
|
| 215 |
// Hide the filesystem credentials form. |
| 216 |
ob_end_clean(); |
| 217 |
|
| 218 |
// Check for file system permissions. |
| 219 |
if ( $creds === false ) { |
| 220 |
wp_send_json_error( $error ); |
| 221 |
} |
| 222 |
|
| 223 |
/** @var array<string, string> $creds Filesystem credentials. */ |
| 224 |
if ( ! WP_Filesystem( $creds ) ) { |
| 225 |
wp_send_json_error( $error ); |
| 226 |
} |
| 227 |
|
| 228 |
// Do not allow WordPress to search/download translations, as this will break JS output. |
| 229 |
remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); |
| 230 |
|
| 231 |
/** \Plugin_Upgrader class */ |
| 232 |
$upgrader = ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 233 |
|
| 234 |
if ( ! file_exists( $upgrader ) ) { |
| 235 |
wp_send_json_error( $error ); |
| 236 |
} |
| 237 |
|
| 238 |
require_once $upgrader; |
| 239 |
|
| 240 |
$installer = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() ); |
| 241 |
|
| 242 |
// Error check. |
| 243 |
if ( ! method_exists( $installer, 'install' ) || empty( $_POST['plugin'] ) ) { |
| 244 |
wp_send_json_error( $error ); |
| 245 |
} |
| 246 |
|
| 247 |
$installer->install( $_POST['plugin'] ); // phpcs:ignore |
| 248 |
|
| 249 |
// Flush the cache and return the newly installed plugin basename. |
| 250 |
wp_cache_flush(); |
| 251 |
|
| 252 |
$plugin_basename = $installer->plugin_info(); |
| 253 |
|
| 254 |
if ( empty( $plugin_basename ) ) { |
| 255 |
wp_send_json_error( $error ); |
| 256 |
} |
| 257 |
|
| 258 |
$result = array( |
| 259 |
'msg' => $generic_error, |
| 260 |
'is_activated' => false, |
| 261 |
'basename' => $plugin_basename, |
| 262 |
); |
| 263 |
|
| 264 |
// Check for permissions. |
| 265 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 266 |
$result['msg'] = $type === 'plugin' |
| 267 |
? esc_html__( 'Plugin installed.', 'stripe' ) |
| 268 |
: esc_html__( 'Addon installed.', 'stripe' ); |
| 269 |
|
| 270 |
wp_send_json_success( $result ); |
| 271 |
} |
| 272 |
|
| 273 |
// Activate the plugin silently. |
| 274 |
/** @var string $plugin_basename */ |
| 275 |
$activated = activate_plugin( $plugin_basename ); |
| 276 |
|
| 277 |
if ( ! is_wp_error( $activated ) ) { |
| 278 |
$result['is_activated'] = true; |
| 279 |
$result['msg'] = $type === 'plugin' |
| 280 |
? esc_html__( 'Plugin installed & activated.', 'stripe' ) |
| 281 |
: esc_html__( 'Addon installed & activated.', 'stripe' ); |
| 282 |
|
| 283 |
wp_send_json_success( $result ); |
| 284 |
} |
| 285 |
|
| 286 |
// Fallback error just in case. |
| 287 |
wp_send_json_error( $result ); |
| 288 |
} |
| 289 |
|
| 290 |
} |
| 291 |
|