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