| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Libs; |
| 4 |
|
| 5 |
// No, Direct access Sir !!! |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/* |
| 11 |
* Addons global class |
| 12 |
*/ |
| 13 |
|
| 14 |
if (!class_exists('Addons')) { |
| 15 |
|
| 16 |
/** |
| 17 |
* Addons Class |
| 18 |
* |
| 19 |
* Jewel Theme <support@jeweltheme.com> |
| 20 |
*/ |
| 21 |
class Addons |
| 22 |
{ |
| 23 |
public $menu_items = []; |
| 24 |
public $plugins_list = []; |
| 25 |
public $sub_menu; |
| 26 |
public $menu_order; |
| 27 |
|
| 28 |
public $server_url = 'https://coupon.wpadminify.com/'; |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor method |
| 33 |
* |
| 34 |
* @param integer $menu_order . |
| 35 |
* @author Jewel Theme <support@jeweltheme.com> |
| 36 |
*/ |
| 37 |
public function __construct($menu_order = 70) |
| 38 |
{ |
| 39 |
$this->menu_order = $menu_order; |
| 40 |
$this->menu_items = $this->menu_items(); |
| 41 |
$this->plugins_list = $this->plugins_list(); |
| 42 |
|
| 43 |
$this->includes(); |
| 44 |
|
| 45 |
add_action('admin_menu', array($this, 'admin_menu'), 1000); |
| 46 |
// add_action('network_admin_menu', array($this, 'admin_menu'), $this->menu_order); |
| 47 |
add_action('wp_ajax_jltwp_adminify_addons_upgrade_plugin', array($this, 'jltwp_adminify_addons_upgrade_plugin')); |
| 48 |
add_action('wp_ajax_jltwp_adminify_addons_activate_plugin', array($this, 'jltwp_adminify_addons_activate_plugin')); |
| 49 |
add_action('plugins_loaded', array($this, 'maybe_replace_addons_path'), 1000); // 1000 is important |
| 50 |
add_action( 'rest_api_init', array( $this , 'jltwp_adminify_addons_rest_routes') ); |
| 51 |
} |
| 52 |
|
| 53 |
public function jltwp_adminify_addons_rest_routes() { |
| 54 |
register_rest_route('adminify/v1', '/get-addons-list', array( |
| 55 |
'methods' => 'GET', |
| 56 |
'callback' => [$this, 'jltwp_adminify_get_addons_plugins_list'], |
| 57 |
'permission_callback' => [$this, 'adminify_is_admin_user'], |
| 58 |
)); |
| 59 |
|
| 60 |
register_rest_route('adminify/v1', '/install-addons', array( |
| 61 |
'methods' => 'POST', |
| 62 |
'callback' => [$this, 'jltwp_adminify_install_addons'], |
| 63 |
'permission_callback' => [$this, 'adminify_verify_nonce_and_permissions'], |
| 64 |
)); |
| 65 |
} |
| 66 |
|
| 67 |
public function adminify_is_admin_user() { |
| 68 |
return current_user_can('manage_options'); |
| 69 |
} |
| 70 |
|
| 71 |
public function adminify_verify_nonce_and_permissions() { |
| 72 |
// Check user |
| 73 |
if ( ! current_user_can('manage_options') ) { |
| 74 |
return new WP_Error('forbidden', 'You are not allowed to do this.', array('status' => 403)); |
| 75 |
} |
| 76 |
|
| 77 |
// Check nonce from header |
| 78 |
$nonce = $_SERVER['HTTP_X_WP_NONCE'] ?? ''; |
| 79 |
if ( ! wp_verify_nonce($nonce, 'wp_rest') ) { |
| 80 |
return new WP_Error('rest_cookie_invalid_nonce', __('Invalid nonce.'), array('status' => 403)); |
| 81 |
} |
| 82 |
if ( is_multisite() && ! is_super_admin() ) { |
| 83 |
return new WP_Error('not_allowed', 'Super admin only on multisite.', array('status' => 403)); |
| 84 |
} |
| 85 |
|
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
public function jltwp_adminify_get_addons_plugins_list() { |
| 91 |
$plugins = $this->plugins_list; |
| 92 |
unset($plugins['master-addons']); |
| 93 |
$all_plugins = get_plugins(); |
| 94 |
$active_plugins = get_option('active_plugins'); |
| 95 |
foreach( $plugins as $slug => $plugin){ |
| 96 |
foreach ($all_plugins as $plugin_file => $plugin_data) { |
| 97 |
if (strpos($plugin_file, $slug) !== false) { |
| 98 |
$plugins[$slug]["status"] = 'installed'; |
| 99 |
|
| 100 |
if (in_array($plugin_file, $active_plugins)) { |
| 101 |
$plugins[$slug]["status"] = 'activated'; |
| 102 |
} |
| 103 |
break; |
| 104 |
} |
| 105 |
} |
| 106 |
if( !isset($plugins[$slug]["status"])) $plugins[$slug]["status"] = 'not-installed'; |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
return rest_ensure_response($plugins); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
public function jltwp_adminify_install_addons( $request ) { |
| 116 |
$addons = $request->get_param('addons'); |
| 117 |
if ( empty($addons) || !is_array($addons) ) { |
| 118 |
return new WP_Error('no_addons', 'No addons were selected.', array('status' => 400)); |
| 119 |
} |
| 120 |
|
| 121 |
$plugins_list = $this->jltwp_adminify_get_addons_plugins_list()->data; |
| 122 |
foreach( $addons as $key => $plugin ){ |
| 123 |
if($plugins_list[$plugin]['status'] == "activated") continue; |
| 124 |
if($plugins_list[$plugin]['status'] == "installed") { |
| 125 |
$this->jltwp_adminify_activate_plugin_by_slug($plugin); |
| 126 |
continue; |
| 127 |
} |
| 128 |
$params = [ |
| 129 |
'request_type' => 'rest', |
| 130 |
'plugin' => $plugins_list[$plugin]['download_link'], |
| 131 |
]; |
| 132 |
|
| 133 |
$this->jltwp_adminify_addons_upgrade_plugin($params); |
| 134 |
} |
| 135 |
|
| 136 |
return rest_ensure_response(['message' => 'Addons installed', 'addons' => $addons]); |
| 137 |
} |
| 138 |
|
| 139 |
function jltwp_adminify_activate_plugin_by_slug($slug) { |
| 140 |
$plugin_path = WP_PLUGIN_DIR . '/' . $slug; |
| 141 |
|
| 142 |
if (!is_dir($plugin_path)) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
$plugin_files = glob("$plugin_path/*.php"); |
| 147 |
if (!$plugin_files || empty($plugin_files)) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
$main_plugin_file = basename($plugin_files[0]); |
| 152 |
$plugin_relative_path = $slug . '/' . $main_plugin_file; |
| 153 |
|
| 154 |
if (is_plugin_active($plugin_relative_path)) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
activate_plugin($plugin_relative_path); |
| 159 |
} |
| 160 |
|
| 161 |
public function maybe_replace_addons_path() { |
| 162 |
|
| 163 |
$addons = [ |
| 164 |
'sidebar-generator/adminify-sidebar-generator.php' => 'adminify-sidebar-generator/adminify-sidebar-generator.php' |
| 165 |
]; |
| 166 |
|
| 167 |
foreach ($addons as $old_plugin => $new_plugin) { |
| 168 |
|
| 169 |
$old_plugin_path = WP_PLUGIN_DIR . '/' . $old_plugin; |
| 170 |
$new_plugin_path = WP_PLUGIN_DIR . '/' . $new_plugin; |
| 171 |
|
| 172 |
// Both files exist, delete the old one |
| 173 |
if ( file_exists($old_plugin_path) && file_exists($new_plugin_path) ) { |
| 174 |
unlink(dirname($old_plugin_path)); |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
// If the old file exists and the new file doesn't exist, rename the old file to the new file |
| 179 |
if ( file_exists($old_plugin_path) && !file_exists($new_plugin_path) ) { |
| 180 |
|
| 181 |
// check if the old plugin is active |
| 182 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 183 |
|
| 184 |
$is_active = is_plugin_active( $old_plugin ); |
| 185 |
|
| 186 |
if ( $is_active ) { |
| 187 |
// Deactivate the old plugin |
| 188 |
deactivate_plugins( $old_plugin ); |
| 189 |
// Rename the old plugin to the new plugin |
| 190 |
rename( dirname($old_plugin_path), dirname($new_plugin_path) ); |
| 191 |
|
| 192 |
if ( file_exists($new_plugin_path) ) { |
| 193 |
// Clear the plugin cache |
| 194 |
wp_cache_delete( 'plugins', 'plugins' ); |
| 195 |
|
| 196 |
// Activate the new plugin |
| 197 |
activate_plugin( $new_plugin ); |
| 198 |
} |
| 199 |
|
| 200 |
} else { |
| 201 |
// Rename the old plugin to the new plugin |
| 202 |
rename( dirname($old_plugin_path), dirname($new_plugin_path) ); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Includes |
| 212 |
* |
| 213 |
* @author Jewel Theme <support@jeweltheme.com> |
| 214 |
*/ |
| 215 |
public function includes() |
| 216 |
{ |
| 217 |
// if (!function_exists('install_plugin_install_status')) { |
| 218 |
// require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 219 |
require_once(ABSPATH . '/wp-load.php'); |
| 220 |
require_once(ABSPATH . 'wp-admin/includes/plugin-install.php'); |
| 221 |
require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 222 |
require_once(ABSPATH . 'wp-admin/includes/misc.php'); |
| 223 |
require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
| 224 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
| 225 |
// } |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Menu Items |
| 230 |
* |
| 231 |
* @author Jewel Theme <support@jeweltheme.com> |
| 232 |
*/ |
| 233 |
public function menu_items() |
| 234 |
{ |
| 235 |
return array(); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Plugins list |
| 240 |
* |
| 241 |
* @author Jewel Theme <support@jeweltheme.com> |
| 242 |
*/ |
| 243 |
public function plugins_list() |
| 244 |
{ |
| 245 |
return array(); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Admin submenu |
| 250 |
*/ |
| 251 |
public function admin_menu() |
| 252 |
{ |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Render addons plugins body |
| 257 |
*/ |
| 258 |
public function render_addons_plugins() |
| 259 |
{ |
| 260 |
?> |
| 261 |
<div class='wp-adminify-addons-wrapper'> |
| 262 |
<?php $this->header(); ?> |
| 263 |
<?php $this->body(); ?> |
| 264 |
</div> |
| 265 |
<?php |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
/** |
| 270 |
* Addons License Header Check |
| 271 |
* |
| 272 |
* @return void |
| 273 |
*/ |
| 274 |
|
| 275 |
public function jltwp_adminify_addons_check() |
| 276 |
{ |
| 277 |
|
| 278 |
$license = jltwp_adminify()->_get_license(); |
| 279 |
|
| 280 |
if (!is_object($license) || !$license->is_valid() || !$license->is_active()) return; |
| 281 |
|
| 282 |
if ( $this->is_eligible_for_coupon() ) { |
| 283 |
// Get the coupon |
| 284 |
$coupon = $this->maybe_create_and_get_coupon(); |
| 285 |
if (!empty($coupon) && !empty($coupon['code'])) { |
| 286 |
echo sprintf( |
| 287 |
__('<h3>Coupon Code: <strong style="color: red">%s</strong> Redeem this coupon code to get free access to all our premium addons (Except Admin Bar Editor, RoleMaster Suite and Master Addons). Learn how to <a href="https://wpadminify.com/redeem-addons-using-coupon-code/" target="_blank">redeem coupon code?</a></h3> ', 'adminify'), |
| 288 |
esc_attr($coupon['code']) |
| 289 |
); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
echo '<style> |
| 294 |
#fs_addons .fs-cards-list{ display: flex; } |
| 295 |
#fs_addons .fs-cards-list .fs-card .fs-inner .fs-cta .button{ |
| 296 |
top: 112px; |
| 297 |
right: 12px; |
| 298 |
line-height: 26px !important; |
| 299 |
border-radius: 3px !important; |
| 300 |
}</style>'; |
| 301 |
} |
| 302 |
|
| 303 |
public function is_eligible_for_coupon() { |
| 304 |
|
| 305 |
$is_eligible = get_option('wp_adminify_addon__is_eligible_for_coupon', null); |
| 306 |
|
| 307 |
if ( $is_eligible !== null ) return wp_validate_boolean($is_eligible); |
| 308 |
$args = [ |
| 309 |
'license' => base64_encode(json_encode(jltwp_adminify()->_get_license())), |
| 310 |
'action' => 'check_eligibility' |
| 311 |
]; |
| 312 |
|
| 313 |
$request_uri = add_query_arg($args, $this->server_url); |
| 314 |
|
| 315 |
$response = wp_remote_get($request_uri); |
| 316 |
|
| 317 |
if (!is_wp_error($response) && $response['response']['code'] === 200) { |
| 318 |
$file_contents = wp_remote_retrieve_body($response); |
| 319 |
$is_eligible = json_decode($file_contents, true); |
| 320 |
update_option('wp_adminify_addon__is_eligible_for_coupon', wp_validate_boolean($is_eligible)); |
| 321 |
return $is_eligible; |
| 322 |
} |
| 323 |
|
| 324 |
return false; |
| 325 |
} |
| 326 |
|
| 327 |
public function maybe_delete_corrupted_coupon(){ |
| 328 |
$coupon_delete_check = get_option('wp_adminify_addon__coupon_is_deleted', false); |
| 329 |
if($coupon_delete_check != true){ |
| 330 |
delete_option('wp_adminify_addon__coupon'); |
| 331 |
update_option('wp_adminify_addon__coupon_is_deleted', true); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
public function maybe_create_and_get_coupon() |
| 336 |
{ |
| 337 |
$this->maybe_delete_corrupted_coupon(); |
| 338 |
$coupon = get_option('wp_adminify_addon__coupon'); |
| 339 |
|
| 340 |
if (!empty($coupon)) return $coupon; |
| 341 |
|
| 342 |
// communicate hit hserver get coupon |
| 343 |
$args = [ |
| 344 |
'license' => base64_encode(json_encode(jltwp_adminify()->_get_license())), |
| 345 |
'action' => 'get_coupon' |
| 346 |
]; |
| 347 |
|
| 348 |
$response = wp_remote_get(add_query_arg($args, $this->server_url)); |
| 349 |
|
| 350 |
if (!is_wp_error($response) && $response['response']['code'] === 200) { |
| 351 |
|
| 352 |
$file_contents = wp_remote_retrieve_body($response); |
| 353 |
$response_data = json_decode($file_contents, true); |
| 354 |
|
| 355 |
if (!empty($response_data) && is_array($response_data) && !empty($response_data['id']) && !empty($response_data['code']) ) { |
| 356 |
$coupon = [ |
| 357 |
'id' => $response_data['id'], |
| 358 |
'code' => $response_data['code'] |
| 359 |
]; |
| 360 |
update_option('wp_adminify_addon__coupon', $coupon); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
return $coupon; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
/** |
| 369 |
* Header |
| 370 |
*/ |
| 371 |
public function header() |
| 372 |
{ |
| 373 |
?> |
| 374 |
<div class='wp-adminify-addons-header'> |
| 375 |
<div class='wp-adminify-addons-title'> |
| 376 |
<h2> |
| 377 |
<?php echo esc_html__('Add Ons for WP Adminify', 'adminify'); ?> |
| 378 |
</h2> |
| 379 |
<?php $this->jltwp_adminify_addons_check(); ?> |
| 380 |
</div> |
| 381 |
<div class='wp-adminify-addons-menu'> |
| 382 |
<div class="wp-filter"> |
| 383 |
<ul class="filter-links"> |
| 384 |
<?php |
| 385 |
$i = 0; |
| 386 |
|
| 387 |
foreach ($this->menu_items as $menu) { |
| 388 |
$class = str_replace(' ', '-', strtolower($menu['key'])); |
| 389 |
?> |
| 390 |
<li class="plugin-install-<?php echo esc_attr($class); ?>"> |
| 391 |
<a href="#" class="<?php echo esc_attr(0 === $i ? 'current' : ''); ?>" data-type="<?php echo esc_attr($menu['key']); ?>"><?php echo esc_html($menu['label']); ?></a> |
| 392 |
</li> |
| 393 |
<?php |
| 394 |
++$i; |
| 395 |
} |
| 396 |
?> |
| 397 |
</ul> |
| 398 |
|
| 399 |
<form class="search-form wp-adminify-search-plugins mr-0" method="get"> |
| 400 |
<input type="hidden" name="tab" value="search"> |
| 401 |
<label class="screen-reader-text" for="search-plugins"> |
| 402 |
<?php echo esc_html__('Search Plugins', 'adminify'); ?> |
| 403 |
</label> |
| 404 |
<input type="search" name="s" id="search-plugins" value="" class="wp-filter-search" placeholder="<?php echo esc_html__('Search plugins...', 'adminify'); ?>"> |
| 405 |
<input type="submit" id="search-submit" class="button hide-if-js" value="<?php echo esc_html__('Search Plugins', 'adminify'); ?>"> |
| 406 |
</form> |
| 407 |
</div> |
| 408 |
</div> |
| 409 |
</div> |
| 410 |
<?php |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Body |
| 415 |
*/ |
| 416 |
public function body() |
| 417 |
{ |
| 418 |
?> |
| 419 |
<div class="wp-list-table widefat plugin-install"> |
| 420 |
<div id="the-list"> |
| 421 |
<?php |
| 422 |
$this->plugins(); |
| 423 |
?> |
| 424 |
</div> |
| 425 |
</div> |
| 426 |
<?php |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Body |
| 431 |
*/ |
| 432 |
public function plugins() |
| 433 |
{ |
| 434 |
|
| 435 |
foreach ($this->plugins_list as $key => $plugin) { |
| 436 |
$install_status = \install_plugin_install_status($plugin); |
| 437 |
$classes = implode(' ', $plugin['type']); |
| 438 |
|
| 439 |
$more_details = self_admin_url( |
| 440 |
'plugin-install.php?tab=plugin-information&plugin=' . esc_attr($plugin['slug']) . |
| 441 |
'&TB_iframe=true&width=600&height=550' |
| 442 |
); |
| 443 |
|
| 444 |
?> |
| 445 |
<div class="plugin-card plugin-card-<?php echo esc_attr($key); ?> <?php echo esc_attr($classes); ?>"> |
| 446 |
<div class="plugin-card-top"> |
| 447 |
<div class="name column-name"> |
| 448 |
<h3> |
| 449 |
<a href="<?php echo esc_url($more_details); ?>" class="thickbox open-plugin-details-modal"> |
| 450 |
<?php echo esc_html($plugin['name']); ?> |
| 451 |
<img src="<?php echo esc_url($plugin['icon']); ?>" class="plugin-icon" alt=""> |
| 452 |
</a> |
| 453 |
</h3> |
| 454 |
</div> |
| 455 |
<div class="desc column-description"> |
| 456 |
<p><?php echo wp_kses_post($plugin['short_description']); ?></p> |
| 457 |
</div> |
| 458 |
<!-- Hover Popup --> |
| 459 |
<?php if( !empty($plugin['pricing_url']) || !empty($plugin['view_details']) ) { ?> |
| 460 |
<div class="adminify-plugin-details"> |
| 461 |
<?php if( !empty($plugin['view_details']) ) { ?> |
| 462 |
<a href="<?php echo esc_url($plugin['view_details']); ?>" target="_blank" class="adminify-view-details"><?php echo esc_html__('View Details', 'adminify'); ?></a> |
| 463 |
<?php } ?> |
| 464 |
|
| 465 |
<?php if( !empty($plugin['pricing_url']) ) { ?> |
| 466 |
<a href="<?php echo esc_url($plugin['pricing_url']); ?>" target="_blank"><?php echo esc_html__('Buy Now', 'adminify'); ?></a> |
| 467 |
<?php } ?> |
| 468 |
</div> |
| 469 |
<?php } ?> |
| 470 |
</div> |
| 471 |
<div class="plugin-card-bottom"> |
| 472 |
<div class="column-downloaded"> |
| 473 |
<span class="plugin-status"> |
| 474 |
<?php |
| 475 |
echo esc_html__('Status:', 'adminify'); |
| 476 |
|
| 477 |
if ('install' === $install_status['status']) { |
| 478 |
?> |
| 479 |
<span class="plugin-status-not-install" data-plugin-url="<?php echo esc_attr($plugin['download_link']); ?>"><?php echo esc_html__('No Installed', 'adminify'); ?></span> |
| 480 |
<?php |
| 481 |
} elseif ('update_available' === $install_status['status']) { |
| 482 |
if (is_plugin_active($install_status['file'])) { |
| 483 |
?> |
| 484 |
<span class="plugin-status-active"> |
| 485 |
<?php echo esc_html__('Active', 'adminify'); ?> |
| 486 |
</span> |
| 487 |
<?php |
| 488 |
} else { |
| 489 |
?> |
| 490 |
<span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr(esc_attr($install_status['file'])); ?>"> |
| 491 |
<?php echo esc_html__('Inactive', 'adminify'); ?> |
| 492 |
</span> |
| 493 |
<?php |
| 494 |
} |
| 495 |
} elseif (('latest_installed' === $install_status['status']) || ('newer_installed' === $install_status['status'])) { |
| 496 |
if (is_plugin_active($install_status['file'])) { |
| 497 |
?> |
| 498 |
<span class="plugin-status-active"> |
| 499 |
<?php echo esc_html__('Active', 'adminify'); ?> |
| 500 |
</span> |
| 501 |
<?php |
| 502 |
} elseif (current_user_can('activate_plugin', $install_status['file'])) { |
| 503 |
?> |
| 504 |
<span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>"> |
| 505 |
<?php echo esc_html__('Inactive', 'adminify'); ?> |
| 506 |
</span> |
| 507 |
<?php |
| 508 |
} else { |
| 509 |
?> |
| 510 |
<span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>"> |
| 511 |
<?php echo esc_html__('Inactive', 'adminify'); ?> |
| 512 |
</span> |
| 513 |
<?php |
| 514 |
} |
| 515 |
} |
| 516 |
?> |
| 517 |
</span> |
| 518 |
</div> |
| 519 |
<div class="column-compatibility"> |
| 520 |
<ul class="plugin-action-buttons"> |
| 521 |
<?php |
| 522 |
if ('install' === $install_status['status']) { |
| 523 |
?> |
| 524 |
<li> |
| 525 |
<button class="install-now adminify-btn adminify-btn-outline-primary" data-install-url="<?php echo esc_attr($plugin['download_link']); ?>"> |
| 526 |
<?php echo esc_html__('Install Now', 'adminify'); ?> |
| 527 |
</button> |
| 528 |
</li> |
| 529 |
<?php |
| 530 |
} elseif ('update_available' === $install_status['status']) { |
| 531 |
?> |
| 532 |
<li class="mr-0"> |
| 533 |
<button class="update-now button" data-plugin="<?php echo esc_attr($install_status['file']); ?>" data-slug="<?php echo esc_attr($plugin['slug']); ?>" data-update-url="<?php echo esc_attr($install_status['url']); ?>"> |
| 534 |
<?php echo esc_html__('Update Now', 'adminify'); ?> |
| 535 |
</button> |
| 536 |
</li> |
| 537 |
<?php |
| 538 |
} elseif (('latest_installed' === $install_status['status']) || ('newer_installed' === $install_status['status'])) { |
| 539 |
if (is_plugin_active($install_status['file'])) { |
| 540 |
?> |
| 541 |
<li class="mr-0"> |
| 542 |
<button type="button" class="adminify-btn adminify-btn-success" disabled="disabled"> |
| 543 |
<?php echo esc_html__('Activated', 'adminify'); ?> |
| 544 |
</button> |
| 545 |
</li> |
| 546 |
<?php |
| 547 |
} elseif (current_user_can('activate_plugin', $install_status['file'])) { |
| 548 |
?> |
| 549 |
<button class="button activate-now" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>"> |
| 550 |
<?php echo esc_html__('Activate Now', 'adminify'); ?> |
| 551 |
</button> |
| 552 |
<?php |
| 553 |
} else { |
| 554 |
?> |
| 555 |
<li class="mr-0"> |
| 556 |
<button type="button" class="button button-disabled" disabled="disabled"> |
| 557 |
<?php echo esc_html__('Installed', 'adminify'); ?> |
| 558 |
</button> |
| 559 |
</li> |
| 560 |
<?php |
| 561 |
} |
| 562 |
} |
| 563 |
?> |
| 564 |
</ul> |
| 565 |
</div> |
| 566 |
</div> |
| 567 |
</div> |
| 568 |
<?php |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* Activate Plugins |
| 574 |
* |
| 575 |
* @author Jewel Theme <support@jeweltheme.com> |
| 576 |
*/ |
| 577 |
public function jltwp_adminify_addons_activate_plugin() |
| 578 |
{ |
| 579 |
if (empty($_POST['plugin'])) { |
| 580 |
return; |
| 581 |
} |
| 582 |
try { |
| 583 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; |
| 584 |
|
| 585 |
if (!wp_verify_nonce($nonce, 'jltwp_adminify_addons_nonce')) { |
| 586 |
wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify'))); |
| 587 |
} |
| 588 |
|
| 589 |
// Security check - only administrators can activate plugins |
| 590 |
if (!current_user_can('activate_plugins')) { |
| 591 |
wp_send_json_error(array('mess' => __('You do not have permission to perform this action.', 'adminify'))); |
| 592 |
} |
| 593 |
|
| 594 |
$plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); |
| 595 |
$plugin_links = array_values(wp_list_pluck($this->plugins_list, 'slug')); |
| 596 |
|
| 597 |
if (!in_array(dirname($plugin), $plugin_links)) { |
| 598 |
wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify'))); |
| 599 |
} |
| 600 |
|
| 601 |
$result = activate_plugin($plugin); |
| 602 |
|
| 603 |
if (is_wp_error($result)) { |
| 604 |
wp_send_json_error( |
| 605 |
array( |
| 606 |
'mess' => $result->get_error_message(), |
| 607 |
) |
| 608 |
); |
| 609 |
} |
| 610 |
wp_send_json_success( |
| 611 |
array( |
| 612 |
'mess' => __('Activate success', 'adminify'), |
| 613 |
) |
| 614 |
); |
| 615 |
} catch (\Exception $ex) { |
| 616 |
wp_send_json_error( |
| 617 |
array( |
| 618 |
'mess' => __('Error exception.', 'adminify'), |
| 619 |
array( |
| 620 |
'error' => $ex, |
| 621 |
), |
| 622 |
) |
| 623 |
); |
| 624 |
} catch (\Error $ex) { |
| 625 |
wp_send_json_error( |
| 626 |
array( |
| 627 |
'mess' => __('Error.', 'adminify'), |
| 628 |
array( |
| 629 |
'error' => $ex, |
| 630 |
), |
| 631 |
) |
| 632 |
); |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
public function get_the_plugin_slug( $plugin ) { |
| 637 |
|
| 638 |
// If the plugin is like myplugin/myplugin.php |
| 639 |
if ( ! filter_var($plugin, FILTER_VALIDATE_URL) ) { |
| 640 |
return dirname($plugin); |
| 641 |
} |
| 642 |
|
| 643 |
// If the plugin is from wordpress.org |
| 644 |
if ( false !== strpos( $plugin, 'https://downloads.wordpress.org/plugin/' ) ) { |
| 645 |
$plugin = str_replace( 'https://downloads.wordpress.org/plugin/', '', $plugin ); |
| 646 |
return str_replace( '.zip', '', $plugin ); |
| 647 |
} |
| 648 |
|
| 649 |
// If the plugin is from local store |
| 650 |
$plugin = explode( 'plugin_slug=', $plugin ); |
| 651 |
$plugin = explode( '&', $plugin[1] ); |
| 652 |
return $plugin[0]; |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* Upgrade Plugins required Libraries |
| 657 |
* |
| 658 |
* @author Jewel Theme <support@jeweltheme.com> |
| 659 |
*/ |
| 660 |
public function jltwp_adminify_addons_upgrade_plugin( $params = null ) |
| 661 |
{ |
| 662 |
if ($params == null && empty($_POST['plugin'])) { |
| 663 |
return; |
| 664 |
} |
| 665 |
|
| 666 |
try { |
| 667 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 668 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 669 |
require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; |
| 670 |
require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php'; |
| 671 |
|
| 672 |
if($params == null){ |
| 673 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; |
| 674 |
|
| 675 |
if (!wp_verify_nonce($nonce, 'jltwp_adminify_addons_nonce')) { |
| 676 |
wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify'))); |
| 677 |
} |
| 678 |
$plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); |
| 679 |
}else{ |
| 680 |
$plugin = $params['plugin']; |
| 681 |
} |
| 682 |
|
| 683 |
// Security check - only administrators can install plugins |
| 684 |
if (!current_user_can('install_plugins')) { |
| 685 |
wp_send_json_error(array('mess' => __('You do not have permission to perform this action.', 'adminify'))); |
| 686 |
} |
| 687 |
|
| 688 |
$plugin_slug = $this->get_the_plugin_slug( $plugin ); |
| 689 |
|
| 690 |
if ( ! array_key_exists( $plugin_slug, $this->plugins_list) ) { |
| 691 |
wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify'))); |
| 692 |
} |
| 693 |
|
| 694 |
if($params == null){ |
| 695 |
$type = isset($_POST['type']) ? sanitize_text_field(wp_unslash($_POST['type'])) : 'install'; |
| 696 |
}else{ |
| 697 |
$type = 'install'; |
| 698 |
} |
| 699 |
$skin = new \WP_Ajax_Upgrader_Skin(); |
| 700 |
$upgrader = new \Plugin_Upgrader($skin); |
| 701 |
|
| 702 |
if ('install' === $type) { |
| 703 |
|
| 704 |
$result = $upgrader->install($plugin); |
| 705 |
if ($params == null){ |
| 706 |
if (empty($result) || empty($upgrader->result)) { |
| 707 |
wp_send_json_error( |
| 708 |
array( |
| 709 |
'mess' => 'Something is wrong', |
| 710 |
) |
| 711 |
); |
| 712 |
} |
| 713 |
|
| 714 |
if (is_wp_error($result)) { |
| 715 |
wp_send_json_error( |
| 716 |
array( |
| 717 |
'mess' => $result->get_error_message(), |
| 718 |
) |
| 719 |
); |
| 720 |
} |
| 721 |
}else{ |
| 722 |
if(empty($result) || empty($upgrader->result)){ |
| 723 |
return; |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
$plugins = get_plugins('/' . $upgrader->result['destination_name']); |
| 728 |
$plugin_data = end($plugins); |
| 729 |
$plugin_data['slug'] = $upgrader->result['destination_name']; |
| 730 |
$plugin_data['version'] = $plugin_data['Version']; |
| 731 |
|
| 732 |
if (!empty($plugin_data) && !is_wp_error($plugin_data)) { |
| 733 |
|
| 734 |
$install_status = \install_plugin_install_status($plugin_data); |
| 735 |
|
| 736 |
$active_plugin = activate_plugin($install_status['file']); |
| 737 |
|
| 738 |
if ($params == null){ |
| 739 |
if (is_wp_error($active_plugin)) { |
| 740 |
wp_send_json_error( |
| 741 |
array( |
| 742 |
'mess' => $active_plugin->get_error_message(), |
| 743 |
) |
| 744 |
); |
| 745 |
} else { |
| 746 |
wp_send_json_success( |
| 747 |
array( |
| 748 |
'mess' => __('Install success', 'adminify'), |
| 749 |
) |
| 750 |
); |
| 751 |
} |
| 752 |
} |
| 753 |
} else { |
| 754 |
if ($params == null){ |
| 755 |
wp_send_json_error( |
| 756 |
array( |
| 757 |
'mess' => 'Error', |
| 758 |
) |
| 759 |
); |
| 760 |
|
| 761 |
} |
| 762 |
} |
| 763 |
} else { |
| 764 |
|
| 765 |
$is_active = is_plugin_active($plugin); |
| 766 |
$result = $upgrader->upgrade($plugin); |
| 767 |
|
| 768 |
if ($params == null){ |
| 769 |
if ( empty($result) || is_wp_error($result) ) { |
| 770 |
wp_send_json_error( |
| 771 |
array( |
| 772 |
'mess' => is_wp_error($result) ? $result->get_error_message() : __('Couldn\'t upgrade', 'adminify') |
| 773 |
) |
| 774 |
); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
$active_status = activate_plugin($plugin); |
| 779 |
|
| 780 |
if ($params == null){ |
| 781 |
if ( empty($active_status) || is_wp_error($active_status) ) { |
| 782 |
wp_send_json_error( |
| 783 |
array( |
| 784 |
'mess' => is_wp_error($result) ? $result->get_error_message() : __('Activation Failed', 'adminify') |
| 785 |
) |
| 786 |
); |
| 787 |
} |
| 788 |
|
| 789 |
wp_send_json_success( |
| 790 |
array( |
| 791 |
'mess' => __('Update success', 'adminify'), |
| 792 |
'active' => true, |
| 793 |
) |
| 794 |
); |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
} catch (\Exception $ex) { |
| 799 |
if ($params == null){ |
| 800 |
wp_send_json_error( |
| 801 |
array( |
| 802 |
'mess' => __('Error exception.', 'adminify'), |
| 803 |
array( |
| 804 |
'error' => $ex, |
| 805 |
), |
| 806 |
) |
| 807 |
); |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
} |
| 813 |
} |
| 814 |
|