| @@ -3,8 +3,10 @@ | ||
| 3 | 3 | namespace SpringDevs\Subscription; |
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | 6 | * The Ajax class |
| 7 | + * | |
| 8 | + * @package SpringDevs\Subscription | |
| 7 | 9 | */ |
| 8 | 10 | class Ajax { |
| 9 | 11 | |
| 10 | 12 | |
| @@ -11,10 +13,11 @@ | ||
| 11 | 13 | /** |
| 12 | 14 | * Initialize the class |
| 13 | 15 | */ |
| 14 | 16 | public function __construct() { |
| 15 | - add_action( 'wp_ajax_install_woocommerce_plugin', array( $this, 'install_woocommerce_plugin' ) ); | |
| 16 | - add_action( 'wp_ajax_activate_woocommerce_plugin', array( $this, 'activate_woocommerce_plugin' ) ); | |
| 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 | + add_action( 'wp_ajax_subscrpt_install_integration_plugin', array( $this, 'install_integration_plugin' ) ); | |
| 17 | 20 | } |
| 18 | 21 | |
| 19 | 22 | /** |
| 20 | 23 | * Install the WooCommerce Plugin. |
| @@ -25,12 +28,12 @@ | ||
| 25 | 28 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 26 | 29 | include_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 27 | 30 | |
| 28 | 31 | if ( ! class_exists( 'Plugin_Upgrader' ) ) { |
| 29 | - include ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php'; | |
| 32 | + require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php'; | |
| 30 | 33 | } |
| 31 | 34 | if ( ! class_exists( 'Plugin_Installer_Skin' ) ) { |
| 32 | - include ABSPATH . 'wp-admin/includes/class-plugin-installer-skin.php'; | |
| 35 | + require_once ABSPATH . 'wp-admin/includes/class-plugin-installer-skin.php'; | |
| 33 | 36 | } |
| 34 | 37 | |
| 35 | 38 | $plugin = 'woocommerce'; |
| 36 | 39 | |
| @@ -55,12 +58,16 @@ | ||
| 55 | 58 | ) |
| 56 | 59 | ); |
| 57 | 60 | |
| 58 | 61 | if ( is_wp_error( $api ) ) { |
| 59 | - wp_die( $api ); | |
| 62 | + wp_die( esc_html( $api->get_error_message() ) ); | |
| 60 | 63 | } |
| 61 | 64 | |
| 62 | - $title = sprintf( __( 'Installing Plugin: %s', 'sdevs_subscrpt' ), $api->name . ' ' . $api->version ); | |
| 65 | + $title = sprintf( | |
| 66 | + // translators: Plugin name and version. | |
| 67 | + __( 'Installing Plugin: %s', 'subscription' ), | |
| 68 | + $api->name . ' ' . $api->version | |
| 69 | + ); | |
| 63 | 70 | $nonce = 'install-plugin_' . $plugin; |
| 64 | 71 | $url = 'update.php?action=install-plugin&plugin=' . urlencode( $plugin ); |
| 65 | 72 | |
| 66 | 73 | $upgrader = new \Plugin_Upgrader( new \Plugin_Installer_Skin( compact( 'title', 'url', 'nonce', 'plugin', 'api' ) ) ); |
| @@ -75,8 +82,14 @@ | ||
| 75 | 82 | /** |
| 76 | 83 | * Active WooComerce Plugin. |
| 77 | 84 | */ |
| 78 | 85 | public function activate_woocommerce_plugin() { |
| 86 | + // add Deprecated notice | |
| 87 | + _deprecated_function( 'Ajax::activate_woocommerce_plugin', '1.5.3', 'Ajax::wps_subscription_activate_woocommerce_plugin' ); | |
| 88 | + return $this->wps_subscription_activate_woocommerce_plugin(); | |
| 89 | + } | |
| 90 | + | |
| 91 | + public function wps_subscription_activate_woocommerce_plugin() { | |
| 79 | 92 | activate_plugin( 'woocommerce/woocommerce.php' ); |
| 80 | 93 | wp_send_json( |
| 81 | 94 | array( |
| 82 | 95 | 'msg' => 'Activated successfully !!', |
| @@ -81,6 +94,92 @@ | ||
| 81 | 94 | array( |
| 82 | 95 | 'msg' => 'Activated successfully !!', |
| 83 | 96 | ) |
| 84 | 97 | ); |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Install and activate a payment gateway plugin from the integrations page. | |
| 102 | + */ | |
| 103 | + public function install_integration_plugin() { | |
| 104 | + check_ajax_referer( 'subscrpt_integration_install_nonce', 'nonce' ); | |
| 105 | + | |
| 106 | + if ( ! current_user_can( 'install_plugins' ) ) { | |
| 107 | + wp_send_json_error( array( 'message' => __( 'Permission denied.', 'subscription' ) ), 403 ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + $plugin_slug = isset( $_POST['plugin_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_slug'] ) ) : ''; | |
| 111 | + | |
| 112 | + if ( empty( $plugin_slug ) ) { | |
| 113 | + wp_send_json_error( array( 'message' => __( 'Plugin slug is required.', 'subscription' ) ), 400 ); | |
| 114 | + } | |
| 115 | + | |
| 116 | + include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; | |
| 117 | + include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
| 118 | + include_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 119 | + include_once ABSPATH . 'wp-admin/includes/misc.php'; | |
| 120 | + | |
| 121 | + $cache_key = 'subscrpt_plugin_api_' . sanitize_key( $plugin_slug ); | |
| 122 | + $api = get_transient( $cache_key ); | |
| 123 | + | |
| 124 | + if ( false === $api ) { | |
| 125 | + // Buffer output so any PHP warnings from the API call don't corrupt the JSON response. | |
| 126 | + ob_start(); | |
| 127 | + | |
| 128 | + $api = plugins_api( | |
| 129 | + 'plugin_information', | |
| 130 | + array( | |
| 131 | + 'slug' => $plugin_slug, | |
| 132 | + 'fields' => array( | |
| 133 | + 'short_description' => false, | |
| 134 | + 'sections' => false, | |
| 135 | + 'requires' => false, | |
| 136 | + 'rating' => false, | |
| 137 | + 'ratings' => false, | |
| 138 | + 'downloaded' => false, | |
| 139 | + 'last_updated' => false, | |
| 140 | + 'added' => false, | |
| 141 | + 'tags' => false, | |
| 142 | + 'compatibility' => false, | |
| 143 | + 'homepage' => false, | |
| 144 | + 'donate_link' => false, | |
| 145 | + ), | |
| 146 | + ) | |
| 147 | + ); | |
| 148 | + | |
| 149 | + ob_end_clean(); | |
| 150 | + | |
| 151 | + if ( is_wp_error( $api ) ) { | |
| 152 | + wp_send_json_error( array( 'message' => $api->get_error_message() ), 400 ); | |
| 153 | + } | |
| 154 | + | |
| 155 | + set_transient( $cache_key, $api, 12 * HOUR_IN_SECONDS ); | |
| 156 | + } | |
| 157 | + | |
| 158 | + $upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() ); | |
| 159 | + $result = $upgrader->install( $api->download_link ); | |
| 160 | + | |
| 161 | + ob_end_clean(); | |
| 162 | + | |
| 163 | + if ( is_wp_error( $result ) ) { | |
| 164 | + wp_send_json_error( array( 'message' => $result->get_error_message() ), 500 ); | |
| 165 | + } | |
| 166 | + | |
| 167 | + if ( ! $result ) { | |
| 168 | + wp_send_json_error( array( 'message' => __( 'Plugin installation failed.', 'subscription' ) ), 500 ); | |
| 169 | + } | |
| 170 | + | |
| 171 | + $plugin_file = $upgrader->plugin_info(); | |
| 172 | + | |
| 173 | + if ( ! $plugin_file ) { | |
| 174 | + wp_send_json_success( array( 'warning' => __( 'Plugin installed but could not be activated automatically.', 'subscription' ) ) ); | |
| 175 | + } | |
| 176 | + | |
| 177 | + $activate = activate_plugin( $plugin_file ); | |
| 178 | + | |
| 179 | + if ( is_wp_error( $activate ) ) { | |
| 180 | + wp_send_json_success( array( 'warning' => __( 'Plugin installed but could not be activated automatically.', 'subscription' ) ) ); | |
| 181 | + } | |
| 182 | + | |
| 183 | + wp_send_json_success(); | |
| 85 | 184 | } |
| 86 | 185 | } |