| 1 |
<?php |
| 2 |
namespace WPDeveloper\BetterDocs\Core; |
| 3 |
|
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} // Exit if accessed directly. |
| 7 |
|
| 8 |
use \WP_Error; |
| 9 |
|
| 10 |
class PluginInstaller |
| 11 |
{ |
| 12 |
public function __construct() { |
| 13 |
add_action( 'wp_ajax_wpdeveloper_auto_active_even_not_installed', [ $this, 'ajax_auto_active_even_not_installed' ] ); |
| 14 |
add_action( 'wp_ajax_wpdeveloper_install_plugin', [ $this, 'ajax_install_plugin' ] ); |
| 15 |
add_action( 'wp_ajax_wpdeveloper_upgrade_plugin', [ $this, 'ajax_upgrade_plugin' ] ); |
| 16 |
add_action( 'wp_ajax_wpdeveloper_activate_plugin', [ $this, 'ajax_activate_plugin' ] ); |
| 17 |
add_action( 'wp_ajax_wpdeveloper_deactivate_plugin', [ $this, 'ajax_deactivate_plugin' ] ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* get_local_plugin_data |
| 22 |
* |
| 23 |
* @param mixed $basename |
| 24 |
* @return array|false |
| 25 |
*/ |
| 26 |
public function get_local_plugin_data($basename = '') |
| 27 |
{ |
| 28 |
if (empty($basename)) { |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
if (!function_exists('get_plugins')) { |
| 33 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 34 |
} |
| 35 |
|
| 36 |
$plugins = get_plugins(); |
| 37 |
|
| 38 |
if (!isset($plugins[$basename])) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
return $plugins[$basename]; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* get_remote_plugin_data |
| 47 |
* |
| 48 |
* @param mixed $slug |
| 49 |
* @return mixed array|WP_Error |
| 50 |
*/ |
| 51 |
public function get_remote_plugin_data($slug = '') |
| 52 |
{ |
| 53 |
if (empty($slug)) { |
| 54 |
return new WP_Error('empty_arg', __('Argument should not be empty.', 'betterdocs')); |
| 55 |
} |
| 56 |
|
| 57 |
$response = wp_remote_post( |
| 58 |
'http://api.wordpress.org/plugins/info/1.0/', |
| 59 |
[ |
| 60 |
'body' => [ |
| 61 |
'action' => 'plugin_information', |
| 62 |
'request' => serialize((object) [ |
| 63 |
'slug' => $slug, |
| 64 |
'fields' => [ |
| 65 |
'version' => false, |
| 66 |
], |
| 67 |
]), |
| 68 |
], |
| 69 |
] |
| 70 |
); |
| 71 |
|
| 72 |
if (is_wp_error($response)) { |
| 73 |
return $response; |
| 74 |
} |
| 75 |
|
| 76 |
return unserialize(wp_remote_retrieve_body($response)); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* install_plugin |
| 81 |
* |
| 82 |
* @param mixed $slug |
| 83 |
* @param bool $active |
| 84 |
* @return mixed bool|WP_Error |
| 85 |
*/ |
| 86 |
public function install_plugin($slug = '', $active = true) |
| 87 |
{ |
| 88 |
if (empty($slug)) { |
| 89 |
return new WP_Error('empty_arg', __('Argument should not be empty.', 'betterdocs')); |
| 90 |
} |
| 91 |
|
| 92 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 93 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 94 |
include_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php'; |
| 95 |
|
| 96 |
$plugin_data = $this->get_remote_plugin_data($slug); |
| 97 |
|
| 98 |
if (is_wp_error($plugin_data)) { |
| 99 |
return $plugin_data; |
| 100 |
} |
| 101 |
|
| 102 |
$upgrader = new \Plugin_Upgrader(new \Automatic_Upgrader_Skin()); |
| 103 |
|
| 104 |
// install plugin |
| 105 |
$install = $upgrader->install($plugin_data->download_link); |
| 106 |
|
| 107 |
if (is_wp_error($install)) { |
| 108 |
return $install; |
| 109 |
} |
| 110 |
|
| 111 |
// activate plugin |
| 112 |
if ($install === true && $active) { |
| 113 |
$active = activate_plugin($upgrader->plugin_info(), '', false, true); |
| 114 |
|
| 115 |
if (is_wp_error($active)) { |
| 116 |
return $active; |
| 117 |
} |
| 118 |
|
| 119 |
return $active === null; |
| 120 |
} |
| 121 |
|
| 122 |
return $install; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* upgrade_plugin |
| 127 |
* |
| 128 |
* @param mixed $basename |
| 129 |
* @return mixed bool|WP_Error |
| 130 |
*/ |
| 131 |
public function upgrade_plugin($basename = '') |
| 132 |
{ |
| 133 |
if (empty($slug)) { |
| 134 |
return new WP_Error('empty_arg', __('Argument should not be empty.', 'betterdocs')); |
| 135 |
} |
| 136 |
|
| 137 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 138 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 139 |
include_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php'; |
| 140 |
|
| 141 |
$upgrader = new \Plugin_Upgrader(new \Automatic_Upgrader_Skin()); |
| 142 |
|
| 143 |
// upgrade plugin |
| 144 |
return $upgrader->upgrade($basename); |
| 145 |
} |
| 146 |
|
| 147 |
public function ajax_install_plugin() |
| 148 |
{ |
| 149 |
check_ajax_referer('betterdocs-wpdeveloper-plugins', 'security'); |
| 150 |
|
| 151 |
if(!current_user_can( 'install_plugins' )) { |
| 152 |
wp_send_json_error(__('you are not allowed to do this action', 'betterdocs')); |
| 153 |
} |
| 154 |
|
| 155 |
$slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : ''; |
| 156 |
$result = $this->install_plugin( $slug ); |
| 157 |
|
| 158 |
$promotype = isset( $_POST['promotype'] ) ? sanitize_text_field( wp_unslash( $_POST['promotype'] ) ) : ''; |
| 159 |
if ( 'eb-banner' === $promotype ) { |
| 160 |
wp_remote_get( 'https://essential-addons.com/essential-blocks-install-gutenberg' ); |
| 161 |
} |
| 162 |
|
| 163 |
if ( is_wp_error( $result ) ) { |
| 164 |
wp_send_json_error( $result->get_error_message() ); |
| 165 |
} |
| 166 |
|
| 167 |
wp_send_json_success(__('Plugin is installed successfully!', 'betterdocs')); |
| 168 |
} |
| 169 |
|
| 170 |
public function ajax_upgrade_plugin() |
| 171 |
{ |
| 172 |
check_ajax_referer('betterdocs-wpdeveloper-plugins', 'security'); |
| 173 |
//check user capabilities |
| 174 |
if(!current_user_can( 'update_plugins' )) { |
| 175 |
wp_send_json_error(__('you are not allowed to do this action', 'betterdocs')); |
| 176 |
} |
| 177 |
|
| 178 |
$basename = isset( $_POST['basename'] ) ? sanitize_text_field( wp_unslash( $_POST['basename'] ) ) : ''; |
| 179 |
$result = $this->upgrade_plugin( $basename ); |
| 180 |
|
| 181 |
if (is_wp_error($result)) { |
| 182 |
wp_send_json_error($result->get_error_message()); |
| 183 |
} |
| 184 |
|
| 185 |
wp_send_json_success(__('Plugin is updated successfully!', 'betterdocs')); |
| 186 |
} |
| 187 |
|
| 188 |
public function ajax_activate_plugin() |
| 189 |
{ |
| 190 |
check_ajax_referer('betterdocs-wpdeveloper-plugins', 'security'); |
| 191 |
|
| 192 |
//check user capabilities |
| 193 |
if(!current_user_can( 'activate_plugins' )) { |
| 194 |
wp_send_json_error(__('you are not allowed to do this action', 'betterdocs')); |
| 195 |
} |
| 196 |
|
| 197 |
$basename = isset( $_POST['basename'] ) ? sanitize_text_field( wp_unslash( $_POST['basename'] ) ) : ''; |
| 198 |
$result = activate_plugin( $basename, '', false, true ); |
| 199 |
|
| 200 |
if ( is_wp_error( $result ) ) { |
| 201 |
wp_send_json_error( $result->get_error_message() ); |
| 202 |
} |
| 203 |
|
| 204 |
if ($result === false) { |
| 205 |
wp_send_json_error(__('Plugin couldn\'t be activated.', 'betterdocs')); |
| 206 |
} |
| 207 |
wp_send_json_success(__('Plugin is activated successfully!', 'betterdocs')); |
| 208 |
} |
| 209 |
|
| 210 |
public function ajax_deactivate_plugin() { |
| 211 |
check_ajax_referer( 'betterdocs-wpdeveloper-plugins', 'security' ); |
| 212 |
|
| 213 |
//check user capabilities |
| 214 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 215 |
wp_send_json_error( __( 'you are not allowed to do this action', 'betterdocs' ) ); |
| 216 |
} |
| 217 |
|
| 218 |
$basename = isset( $_POST['basename'] ) ? sanitize_text_field( wp_unslash( $_POST['basename'] ) ) : ''; |
| 219 |
deactivate_plugins( $basename, true ); |
| 220 |
|
| 221 |
wp_send_json_success( __( 'Plugin is deactivated successfully!', 'betterdocs' ) ); |
| 222 |
} |
| 223 |
|
| 224 |
public function ajax_auto_active_even_not_installed() { |
| 225 |
check_ajax_referer( 'betterdocs-wpdeveloper-plugins', 'security' ); |
| 226 |
|
| 227 |
$basename = isset( $_POST['basename'] ) ? sanitize_text_field( wp_unslash( $_POST['basename'] ) ) : ''; |
| 228 |
if ( $this->get_local_plugin_data( $basename ) === false ) { |
| 229 |
$this->ajax_install_plugin(); |
| 230 |
} else { |
| 231 |
$this->ajax_activate_plugin(); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|