| 1 |
<?php |
| 2 |
|
| 3 |
namespace NotificationX; |
| 4 |
|
| 5 |
/** |
| 6 |
* WPDeveloper Core Install |
| 7 |
* @method static CoreInstaller get_instance($args = null) |
| 8 |
*/ |
| 9 |
class CoreInstaller { |
| 10 |
/** |
| 11 |
* Instance of CoreInstallers |
| 12 |
* |
| 13 |
* @var CoreInstaller |
| 14 |
*/ |
| 15 |
use GetInstance; |
| 16 |
|
| 17 |
/** |
| 18 |
* Plugin Base Name |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $plugin_basename; |
| 23 |
|
| 24 |
/** |
| 25 |
* Instantiate the class |
| 26 |
* |
| 27 |
* @param string $affiliate |
| 28 |
*/ |
| 29 |
function __construct($plugin_basename = '') { |
| 30 |
$this->plugin_basename = $plugin_basename; |
| 31 |
add_action('init', array($this, 'init_hooks')); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Initialize the hooks |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function init_hooks() { |
| 40 |
if (!current_user_can('manage_options')) { |
| 41 |
return; |
| 42 |
} |
| 43 |
add_action('wp_ajax_wpdeveloper_upsale_core_install_' . $this->plugin_basename, array($this, 'core_install')); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Fail if plugin installtion/activation fails |
| 48 |
* |
| 49 |
* @param Object $thing |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function fail_on_error($thing) { |
| 54 |
if (is_wp_error($thing)) { |
| 55 |
wp_send_json_error($thing->get_error_message()); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Install Upsale Plugin |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function core_install() { |
| 65 |
check_ajax_referer('wpdeveloper_upsale_core_install_' . $this->plugin_basename); |
| 66 |
if (!current_user_can('manage_options')) { |
| 67 |
wp_send_json_error(__('You don\'t have permission to install the plugins', 'notificationx')); |
| 68 |
} |
| 69 |
|
| 70 |
$this->raise_limits(); |
| 71 |
|
| 72 |
$plugin_slug = (isset($_POST['slug'])) ? sanitize_text_field( $_POST['slug'] ) : ''; |
| 73 |
$plugin_file = (isset($_POST['file'])) ? sanitize_file_name( $_POST['file'] ) : ''; |
| 74 |
|
| 75 |
if (empty($plugin_file) || empty($plugin_slug)) { |
| 76 |
wp_send_json_error(__('You don\'t have set any slug and file name to install the plugins', 'notificationx')); |
| 77 |
} |
| 78 |
|
| 79 |
$plugin_status = $this->install_plugin($plugin_slug, $plugin_file); |
| 80 |
$this->fail_on_error($plugin_status); |
| 81 |
|
| 82 |
wp_send_json_success(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Install and activate a plugin |
| 87 |
* |
| 88 |
* @param string $slug |
| 89 |
* @param string $file |
| 90 |
* |
| 91 |
* @return WP_Error|null |
| 92 |
*/ |
| 93 |
public function install_plugin($slug, $file) { |
| 94 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 95 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 96 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 97 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 98 |
|
| 99 |
$this->raise_limits(); |
| 100 |
|
| 101 |
$plugin_basename = $slug . '/' . $file; |
| 102 |
|
| 103 |
// if exists and not activated |
| 104 |
if (file_exists(WP_PLUGIN_DIR . '/' . $plugin_basename)) { |
| 105 |
return \activate_plugin($plugin_basename); |
| 106 |
} |
| 107 |
|
| 108 |
// seems like the plugin doesn't exists. Download and activate it |
| 109 |
$upgrader = new \Plugin_Upgrader(new \WP_Ajax_Upgrader_Skin()); |
| 110 |
|
| 111 |
$api = plugins_api('plugin_information', array('slug' => $slug, 'fields' => array('sections' => false))); |
| 112 |
$result = $upgrader->install($api->download_link); |
| 113 |
|
| 114 |
if (is_wp_error($result)) { |
| 115 |
return $result; |
| 116 |
} |
| 117 |
|
| 118 |
return activate_plugin($plugin_basename); |
| 119 |
} |
| 120 |
/** |
| 121 |
* Some process take long time to execute |
| 122 |
* for that need to raise the limit. |
| 123 |
*/ |
| 124 |
public function raise_limits() { |
| 125 |
wp_raise_memory_limit('admin'); |
| 126 |
if (wp_is_ini_value_changeable('max_execution_time')) { |
| 127 |
ini_set('max_execution_time', 0); |
| 128 |
} |
| 129 |
@set_time_limit(0); |
| 130 |
} |
| 131 |
} |
| 132 |
|