| 1 |
<?php |
| 2 |
if ( ! class_exists( 'BetterDocs_Core_Installer' ) ) : |
| 3 |
/** |
| 4 |
* WPDeveloper Core Install |
| 5 |
*/ |
| 6 |
class BetterDocs_Core_Installer { |
| 7 |
/** |
| 8 |
* Plugin Base Name |
| 9 |
* |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
private $plugin_basename; |
| 13 |
/** |
| 14 |
* Instantiate the class |
| 15 |
* |
| 16 |
* @param string $affiliate |
| 17 |
*/ |
| 18 |
function __construct( $plugin_basename = '' ) { |
| 19 |
$this->plugin_basename = $plugin_basename; |
| 20 |
add_action( 'init', array( $this, 'init_hooks' ) ); |
| 21 |
} |
| 22 |
/** |
| 23 |
* Initialize the hooks |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function init_hooks() { |
| 28 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
add_action( 'wp_ajax_wpdeveloper_upsale_core_install_' . $this->plugin_basename, array( $this, 'core_install' ) ); |
| 32 |
} |
| 33 |
/** |
| 34 |
* Fail if plugin installtion/activation fails |
| 35 |
* |
| 36 |
* @param Object $thing |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function fail_on_error( $thing ) { |
| 41 |
if ( is_wp_error( $thing ) ) { |
| 42 |
wp_send_json_error( $thing->get_error_message() ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Install Upsale Plugin |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function core_install() { |
| 52 |
check_ajax_referer( 'wpdeveloper_upsale_core_install_' . $this->plugin_basename ); |
| 53 |
|
| 54 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 55 |
wp_send_json_error( __( 'You don\'t have permission to install the plugins' ) ); |
| 56 |
} |
| 57 |
|
| 58 |
$plugin_slug = ( isset( $_POST['slug'] ) ) ? $_POST['slug'] : ''; |
| 59 |
$plugin_file = ( isset( $_POST['file'] ) ) ? $_POST['file'] : ''; |
| 60 |
|
| 61 |
if( empty( $plugin_file ) || empty( $plugin_slug ) ) { |
| 62 |
wp_send_json_error( __( 'You don\'t have set any slug and file name to install the plugins' ) ); |
| 63 |
} |
| 64 |
|
| 65 |
$plugin_status = $this->install_plugin( $plugin_slug, $plugin_file ); |
| 66 |
$this->fail_on_error( $plugin_status ); |
| 67 |
|
| 68 |
wp_send_json_success(); |
| 69 |
} |
| 70 |
/** |
| 71 |
* Install and activate a plugin |
| 72 |
* |
| 73 |
* @param string $slug |
| 74 |
* @param string $file |
| 75 |
* |
| 76 |
* @return WP_Error|null |
| 77 |
*/ |
| 78 |
public function install_plugin( $slug, $file ) { |
| 79 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 80 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 81 |
|
| 82 |
$plugin_basename = $slug . '/' . $file; |
| 83 |
|
| 84 |
// if exists and not activated |
| 85 |
if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_basename ) ) { |
| 86 |
return activate_plugin( $plugin_basename ); |
| 87 |
} |
| 88 |
|
| 89 |
// seems like the plugin doesn't exists. Download and activate it |
| 90 |
$upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() ); |
| 91 |
|
| 92 |
$api = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) ); |
| 93 |
$result = $upgrader->install( $api->download_link ); |
| 94 |
|
| 95 |
if ( is_wp_error( $result ) ) { |
| 96 |
return $result; |
| 97 |
} |
| 98 |
|
| 99 |
return activate_plugin( $plugin_basename ); |
| 100 |
} |
| 101 |
} |
| 102 |
endif; |
| 103 |
|
| 104 |
|