| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription; |
| 4 |
|
| 5 |
/** |
| 6 |
* The Ajax class |
| 7 |
* |
| 8 |
* @package SpringDevs\Subscription |
| 9 |
*/ |
| 10 |
class Ajax { |
| 11 |
|
| 12 |
|
| 13 |
/** |
| 14 |
* Initialize the class |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
add_action( 'wp_ajax_subscrpt_install_woocommerce_plugin', array( $this, 'install_woocommerce_plugin' ) ); |
| 18 |
add_action( 'wp_ajax_subscrpt_activate_woocommerce_plugin', array( $this, 'wps_subscription_activate_woocommerce_plugin' ) ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Install the WooCommerce Plugin. |
| 23 |
*/ |
| 24 |
public function install_woocommerce_plugin() { |
| 25 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 26 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 27 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 28 |
include_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 29 |
|
| 30 |
if ( ! class_exists( 'Plugin_Upgrader' ) ) { |
| 31 |
require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php'; |
| 32 |
} |
| 33 |
if ( ! class_exists( 'Plugin_Installer_Skin' ) ) { |
| 34 |
require_once ABSPATH . 'wp-admin/includes/class-plugin-installer-skin.php'; |
| 35 |
} |
| 36 |
|
| 37 |
$plugin = 'woocommerce'; |
| 38 |
|
| 39 |
$api = plugins_api( |
| 40 |
'plugin_information', |
| 41 |
array( |
| 42 |
'slug' => $plugin, |
| 43 |
'fields' => array( |
| 44 |
'short_description' => false, |
| 45 |
'sections' => false, |
| 46 |
'requires' => false, |
| 47 |
'rating' => false, |
| 48 |
'ratings' => false, |
| 49 |
'downloaded' => false, |
| 50 |
'last_updated' => false, |
| 51 |
'added' => false, |
| 52 |
'tags' => false, |
| 53 |
'compatibility' => false, |
| 54 |
'homepage' => false, |
| 55 |
'donate_link' => false, |
| 56 |
), |
| 57 |
) |
| 58 |
); |
| 59 |
|
| 60 |
if ( is_wp_error( $api ) ) { |
| 61 |
wp_die( esc_html( $api->get_error_message() ) ); |
| 62 |
} |
| 63 |
|
| 64 |
$title = sprintf( |
| 65 |
// translators: Plugin name and version. |
| 66 |
__( 'Installing Plugin: %s', 'subscription' ), |
| 67 |
$api->name . ' ' . $api->version |
| 68 |
); |
| 69 |
$nonce = 'install-plugin_' . $plugin; |
| 70 |
$url = 'update.php?action=install-plugin&plugin=' . urlencode( $plugin ); |
| 71 |
|
| 72 |
$upgrader = new \Plugin_Upgrader( new \Plugin_Installer_Skin( compact( 'title', 'url', 'nonce', 'plugin', 'api' ) ) ); |
| 73 |
$upgrader->install( $api->download_link ); |
| 74 |
wp_send_json( |
| 75 |
array( |
| 76 |
'msg' => 'Installed successfully !!', |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Active WooComerce Plugin. |
| 83 |
*/ |
| 84 |
public function activate_woocommerce_plugin() { |
| 85 |
// add Deprecated notice |
| 86 |
_deprecated_function( 'Ajax::activate_woocommerce_plugin', '1.5.3', 'Ajax::wps_subscription_activate_woocommerce_plugin' ); |
| 87 |
return $this->wps_subscription_activate_woocommerce_plugin(); |
| 88 |
} |
| 89 |
|
| 90 |
public function wps_subscription_activate_woocommerce_plugin() { |
| 91 |
activate_plugin( 'woocommerce/woocommerce.php' ); |
| 92 |
wp_send_json( |
| 93 |
array( |
| 94 |
'msg' => 'Activated successfully !!', |
| 95 |
) |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
|