| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class WpxpoPlugins |
| 9 |
*/ |
| 10 |
class WpxpoPlugins { |
| 11 |
|
| 12 |
|
| 13 |
/** |
| 14 |
* Constructor. Hooks into various WordPress actions. |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
// Our Plugin Activation Hooks. |
| 18 |
add_action( 'wp_ajax_optn_install_plugin', array( $this, 'install_plugin_callback' ) ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Handles plugin installation and activation via AJAX. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function install_plugin_callback() { |
| 27 |
|
| 28 |
$nonce = isset( $_POST['wpnonce'] ) ? sanitize_key( wp_unslash( $_POST['wpnonce'] ) ) : ''; |
| 29 |
|
| 30 |
if ( ! isset( $nonce ) ) { |
| 31 |
wp_send_json_error( esc_html__( 'Nonce is missing.', 'optin' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
if ( wp_verify_nonce( $nonce, 'optin-nonce' ) === false ) { |
| 35 |
wp_send_json_error( esc_html__( 'Invalid nonce.', 'optin' ) ); |
| 36 |
} |
| 37 |
|
| 38 |
if ( ! current_user_can( 'install_plugins' ) ) { |
| 39 |
wp_send_json_error( esc_html__( 'Insufficient permissions.', 'optin' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
$plugin = isset( $_POST['plugin'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) : ''; |
| 43 |
|
| 44 |
if ( empty( $plugin ) ) { |
| 45 |
wp_send_json_error( array( 'message' => 'No plugin specified' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
$res = array( 'message' => 'false' ); |
| 49 |
|
| 50 |
if ( $plugin ) { |
| 51 |
$res = Xpo::install_and_active_plugin( $plugin ); |
| 52 |
} |
| 53 |
|
| 54 |
wp_send_json_success( array( 'message' => $res ) ); |
| 55 |
|
| 56 |
die(); |
| 57 |
} |
| 58 |
} |
| 59 |
|