| @@ -15,8 +15,9 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | public function __construct() { |
| 17 | 17 | add_action( 'wp_ajax_subscrpt_install_woocommerce_plugin', array( $this, 'install_woocommerce_plugin' ) ); |
| 18 | 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' ) ); | |
| 19 | 20 | } |
| 20 | 21 | |
| 21 | 22 | /** |
| 22 | 23 | * Install the WooCommerce Plugin. |
| @@ -93,6 +94,92 @@ | ||
| 93 | 94 | array( |
| 94 | 95 | 'msg' => 'Activated successfully !!', |
| 95 | 96 | ) |
| 96 | 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(); | |
| 97 | 184 | } |
| 98 | 185 | } |