| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Libs; |
| 4 |
|
| 5 |
// No, Direct access Sir !!! |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/* |
| 11 |
* Recommended global class |
| 12 |
*/ |
| 13 |
|
| 14 |
if (!class_exists('Recommended')) { |
| 15 |
|
| 16 |
/** |
| 17 |
* Recommended Class |
| 18 |
* |
| 19 |
* Jewel Theme <support@jeweltheme.com> |
| 20 |
*/ |
| 21 |
class Recommended |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
public $menu_items; |
| 26 |
public $plugins_list; |
| 27 |
public $sub_menu; |
| 28 |
public $menu_order; |
| 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 = 99) |
| 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'), $this->menu_order); |
| 46 |
add_action('wp_ajax_jltwp_adminify_recommended_upgrade_plugin', array($this, 'jltwp_adminify_recommended_upgrade_plugin')); |
| 47 |
add_action('wp_ajax_jltwp_adminify_recommended_activate_plugin', array($this, 'jltwp_adminify_recommended_activate_plugin')); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Includes |
| 52 |
* |
| 53 |
* @author Jewel Theme <support@jeweltheme.com> |
| 54 |
*/ |
| 55 |
public function includes() |
| 56 |
{ |
| 57 |
if (!function_exists('install_plugin_install_status')) { |
| 58 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Menu Items |
| 64 |
* |
| 65 |
* @author Jewel Theme <support@jeweltheme.com> |
| 66 |
*/ |
| 67 |
public function menu_items() |
| 68 |
{ |
| 69 |
return array(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Plugins list |
| 74 |
* |
| 75 |
* @author Jewel Theme <support@jeweltheme.com> |
| 76 |
*/ |
| 77 |
public function plugins_list() |
| 78 |
{ |
| 79 |
return array(); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Admin submenu |
| 84 |
*/ |
| 85 |
public function admin_menu() |
| 86 |
{ |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Render recommended plugins body |
| 91 |
*/ |
| 92 |
public function render_recommended_plugins() |
| 93 |
{ |
| 94 |
?> |
| 95 |
<div class='wp-adminify-recommended-wrapper'> |
| 96 |
<?php $this->header(); ?> |
| 97 |
<?php $this->body(); ?> |
| 98 |
</div> |
| 99 |
<?php |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Header |
| 104 |
*/ |
| 105 |
public function header() |
| 106 |
{ |
| 107 |
?> |
| 108 |
<div class='wp-adminify-recommended-header'> |
| 109 |
<div class='wp-adminify-recommended-title'> |
| 110 |
<h2> |
| 111 |
<?php echo esc_html__('Recommended Plugins', 'adminify'); ?> |
| 112 |
</h2> |
| 113 |
</div> |
| 114 |
<div class='wp-adminify-recommended-menu'> |
| 115 |
<div class="wp-filter"> |
| 116 |
<ul class="filter-links"> |
| 117 |
<?php |
| 118 |
$i = 0; |
| 119 |
|
| 120 |
foreach ($this->menu_items as $menu) { |
| 121 |
$class = str_replace(' ', '-', strtolower($menu['key'])); |
| 122 |
?> |
| 123 |
<li class="plugin-install-<?php echo esc_attr($class); ?>"> |
| 124 |
<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> |
| 125 |
</li> |
| 126 |
<?php |
| 127 |
++$i; |
| 128 |
} |
| 129 |
?> |
| 130 |
</ul> |
| 131 |
|
| 132 |
<form class="search-form wp-adminify-search-plugins mr-0" method="get"> |
| 133 |
<input type="hidden" name="tab" value="search"> |
| 134 |
<label class="screen-reader-text" for="search-plugins"> |
| 135 |
<?php echo esc_html__('Search Plugins', 'adminify'); ?> |
| 136 |
</label> |
| 137 |
<input type="search" name="s" id="search-plugins" value="" class="wp-filter-search" placeholder="<?php echo esc_html__('Search plugins...', 'adminify'); ?>"> |
| 138 |
<input type="submit" id="search-submit" class="button hide-if-js" value="<?php echo esc_html__('Search Plugins', 'adminify'); ?>"> |
| 139 |
</form> |
| 140 |
</div> |
| 141 |
</div> |
| 142 |
</div> |
| 143 |
<?php |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Body |
| 148 |
*/ |
| 149 |
public function body() |
| 150 |
{ |
| 151 |
?> |
| 152 |
<div class="wp-list-table widefat plugin-install"> |
| 153 |
<div id="the-list"> |
| 154 |
<?php |
| 155 |
$this->plugins(); |
| 156 |
?> |
| 157 |
</div> |
| 158 |
</div> |
| 159 |
<?php |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Body |
| 164 |
*/ |
| 165 |
public function plugins() |
| 166 |
{ |
| 167 |
foreach ($this->plugins_list as $key => $plugin) { |
| 168 |
$install_status = \install_plugin_install_status($plugin); |
| 169 |
$classes = implode(' ', $plugin['type']); |
| 170 |
|
| 171 |
$more_details = self_admin_url( |
| 172 |
'plugin-install.php?tab=plugin-information&plugin=' . esc_attr($plugin['slug']) . |
| 173 |
'&TB_iframe=true&width=600&height=550' |
| 174 |
); |
| 175 |
?> |
| 176 |
<div class="plugin-card plugin-card-<?php echo esc_attr($key); ?> <?php echo esc_attr($classes); ?>"> |
| 177 |
<div class="plugin-card-top"> |
| 178 |
<div class="name column-name"> |
| 179 |
<h3> |
| 180 |
<a href="<?php echo esc_url($more_details); ?>" class="thickbox open-plugin-details-modal"> |
| 181 |
<?php echo esc_html($plugin['name']); ?> |
| 182 |
<img src="<?php echo esc_url($plugin['icon']); ?>" class="plugin-icon" alt=""> |
| 183 |
</a> |
| 184 |
</h3> |
| 185 |
</div> |
| 186 |
<div class="desc column-description"> |
| 187 |
<p><?php echo wp_kses_post($plugin['short_description']); ?></p> |
| 188 |
</div> |
| 189 |
</div> |
| 190 |
<div class="plugin-card-bottom"> |
| 191 |
<div class="column-downloaded"> |
| 192 |
<span class="plugin-status"> |
| 193 |
<?php |
| 194 |
echo esc_html__('Status:', 'adminify'); |
| 195 |
|
| 196 |
if ('install' === $install_status['status']) { |
| 197 |
?> |
| 198 |
<span class="plugin-status-not-install" data-plugin-url="<?php echo esc_attr($plugin['download_link']); ?>"><?php echo esc_html__('No Installed', 'adminify'); ?></span> |
| 199 |
<?php |
| 200 |
} elseif ('update_available' === $install_status['status']) { |
| 201 |
if (is_plugin_active($install_status['file'])) { |
| 202 |
?> |
| 203 |
<span class="plugin-status-active"> |
| 204 |
<?php echo esc_html__('Active', 'adminify'); ?> |
| 205 |
</span> |
| 206 |
<?php |
| 207 |
} else { |
| 208 |
?> |
| 209 |
<span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr(esc_attr($install_status['file'])); ?>"> |
| 210 |
<?php echo esc_html__('Inactive', 'adminify'); ?> |
| 211 |
</span> |
| 212 |
<?php |
| 213 |
} |
| 214 |
} elseif (('latest_installed' === $install_status['status']) || ('newer_installed' === $install_status['status'])) { |
| 215 |
if (is_plugin_active($install_status['file'])) { |
| 216 |
?> |
| 217 |
<span class="plugin-status-active"> |
| 218 |
<?php echo esc_html__('Active', 'adminify'); ?> |
| 219 |
</span> |
| 220 |
<?php |
| 221 |
} elseif (current_user_can('activate_plugin', $install_status['file'])) { |
| 222 |
?> |
| 223 |
<span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>"> |
| 224 |
<?php echo esc_html__('Inactive', 'adminify'); ?> |
| 225 |
</span> |
| 226 |
<?php |
| 227 |
} else { |
| 228 |
?> |
| 229 |
<span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>"> |
| 230 |
<?php echo esc_html__('Inactive', 'adminify'); ?> |
| 231 |
</span> |
| 232 |
<?php |
| 233 |
} |
| 234 |
} |
| 235 |
?> |
| 236 |
</span> |
| 237 |
</div> |
| 238 |
<div class="column-compatibility"> |
| 239 |
<ul class="plugin-action-buttons mr-0"> |
| 240 |
<?php |
| 241 |
if ('install' === $install_status['status']) { |
| 242 |
?> |
| 243 |
<li class="mr-0"> |
| 244 |
<button class="install-now button button-primary" data-install-url="<?php echo esc_attr($plugin['download_link']); ?>"> |
| 245 |
<?php echo esc_html__('Install Now', 'adminify'); ?> |
| 246 |
</button> |
| 247 |
</li> |
| 248 |
<?php |
| 249 |
} elseif ('update_available' === $install_status['status']) { |
| 250 |
?> |
| 251 |
<li class="mr-0"> |
| 252 |
<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']); ?>"> |
| 253 |
<?php echo esc_html__('Update Now', 'adminify'); ?> |
| 254 |
</button> |
| 255 |
</li> |
| 256 |
<?php |
| 257 |
} elseif (('latest_installed' === $install_status['status']) || ('newer_installed' === $install_status['status'])) { |
| 258 |
if (is_plugin_active($install_status['file'])) { |
| 259 |
?> |
| 260 |
<li class="mr-0"> |
| 261 |
<button type="button" class="button button-disabled" disabled="disabled"> |
| 262 |
<?php echo esc_html__('Activated', 'adminify'); ?> |
| 263 |
</button> |
| 264 |
</li> |
| 265 |
<?php |
| 266 |
} elseif (current_user_can('activate_plugin', $install_status['file'])) { |
| 267 |
?> |
| 268 |
<button class="button activate-now" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>"> |
| 269 |
<?php echo esc_html__('Activate', 'adminify'); ?> |
| 270 |
</button> |
| 271 |
<?php |
| 272 |
} else { |
| 273 |
?> |
| 274 |
<li class="mr-0"> |
| 275 |
<button type="button" class="button button-disabled" disabled="disabled"> |
| 276 |
<?php echo esc_html__('Installed', 'adminify'); ?> |
| 277 |
</button> |
| 278 |
</li> |
| 279 |
<?php |
| 280 |
} |
| 281 |
} |
| 282 |
?> |
| 283 |
</ul> |
| 284 |
</div> |
| 285 |
</div> |
| 286 |
</div> |
| 287 |
<?php |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Activate Plugins |
| 293 |
* |
| 294 |
* @author Jewel Theme <support@jeweltheme.com> |
| 295 |
*/ |
| 296 |
public function jltwp_adminify_recommended_activate_plugin() |
| 297 |
{ |
| 298 |
try { |
| 299 |
if (isset($_POST['file'])) { |
| 300 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; |
| 301 |
|
| 302 |
if (!wp_verify_nonce($nonce, 'jltwp_adminify_recommended_nonce')) { |
| 303 |
wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify'))); |
| 304 |
} |
| 305 |
|
| 306 |
if ((is_multisite() && !is_network_admin()) || !current_user_can('install_plugins')) { |
| 307 |
wp_send_json_error(array('mess' => __('Invalid access', 'adminify'))); |
| 308 |
} |
| 309 |
|
| 310 |
$plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); |
| 311 |
$plugin_links = array_values(wp_list_pluck($this->plugins_list, 'download_link')); |
| 312 |
|
| 313 |
if (!in_array($plugin, $plugin_links)) { |
| 314 |
wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify'))); |
| 315 |
} |
| 316 |
|
| 317 |
$file = sanitize_text_field(wp_unslash($_POST['file'])); |
| 318 |
$result = activate_plugin($file); |
| 319 |
|
| 320 |
if (is_wp_error($result)) { |
| 321 |
wp_send_json_error( |
| 322 |
array( |
| 323 |
'mess' => $result->get_error_message(), |
| 324 |
) |
| 325 |
); |
| 326 |
} |
| 327 |
wp_send_json_success( |
| 328 |
array( |
| 329 |
'mess' => __('Activate success', 'adminify'), |
| 330 |
) |
| 331 |
); |
| 332 |
} |
| 333 |
} catch (\Exception $ex) { |
| 334 |
wp_send_json_error( |
| 335 |
array( |
| 336 |
'mess' => __('Error exception.', 'adminify'), |
| 337 |
array( |
| 338 |
'error' => $ex, |
| 339 |
), |
| 340 |
) |
| 341 |
); |
| 342 |
} catch (\Error $ex) { |
| 343 |
wp_send_json_error( |
| 344 |
array( |
| 345 |
'mess' => __('Error.', 'adminify'), |
| 346 |
array( |
| 347 |
'error' => $ex, |
| 348 |
), |
| 349 |
) |
| 350 |
); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Upgrade Plugins required Libraries |
| 356 |
* |
| 357 |
* @author Jewel Theme <support@jeweltheme.com> |
| 358 |
*/ |
| 359 |
public function jltwp_adminify_recommended_upgrade_plugin() |
| 360 |
{ |
| 361 |
try { |
| 362 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 363 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 364 |
require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; |
| 365 |
require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php'; |
| 366 |
|
| 367 |
if (isset($_POST['plugin'])) { |
| 368 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; |
| 369 |
|
| 370 |
if (!wp_verify_nonce($nonce, 'jltwp_adminify_recommended_nonce')) { |
| 371 |
wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify'))); |
| 372 |
} |
| 373 |
|
| 374 |
if ((is_multisite() && !is_network_admin()) || !current_user_can('install_plugins')) { |
| 375 |
wp_send_json_error(array('mess' => __('Invalid access', 'adminify'))); |
| 376 |
} |
| 377 |
|
| 378 |
$plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); |
| 379 |
$plugin_links = array_values(wp_list_pluck($this->plugins_list, 'download_link')); |
| 380 |
|
| 381 |
if (!in_array($plugin, $plugin_links)) { |
| 382 |
wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify'))); |
| 383 |
} |
| 384 |
|
| 385 |
$type = isset($_POST['type']) ? sanitize_text_field(wp_unslash($_POST['type'])) : 'install'; |
| 386 |
$skin = new \WP_Ajax_Upgrader_Skin(); |
| 387 |
$upgrader = new \Plugin_Upgrader($skin); |
| 388 |
|
| 389 |
if ('install' === $type) { |
| 390 |
$result = $upgrader->install($plugin); |
| 391 |
|
| 392 |
if (is_wp_error($result)) { |
| 393 |
wp_send_json_error( |
| 394 |
array( |
| 395 |
'mess' => $result->get_error_message(), |
| 396 |
) |
| 397 |
); |
| 398 |
} |
| 399 |
$args = array( |
| 400 |
'slug' => $upgrader->result['destination_name'], |
| 401 |
'fields' => array( |
| 402 |
'short_description' => true, |
| 403 |
'icons' => true, |
| 404 |
'banners' => false, |
| 405 |
'added' => false, |
| 406 |
'reviews' => false, |
| 407 |
'sections' => false, |
| 408 |
'requires' => false, |
| 409 |
'rating' => false, |
| 410 |
'ratings' => false, |
| 411 |
'downloaded' => false, |
| 412 |
'last_updated' => false, |
| 413 |
'added' => false, |
| 414 |
'tags' => false, |
| 415 |
'compatibility' => false, |
| 416 |
'homepage' => false, |
| 417 |
'donate_link' => false, |
| 418 |
), |
| 419 |
); |
| 420 |
$plugin_data = plugins_api('plugin_information', $args); |
| 421 |
|
| 422 |
if ($plugin_data && !is_wp_error($plugin_data)) { |
| 423 |
$install_status = \install_plugin_install_status($plugin_data); |
| 424 |
$active_plugin = activate_plugin($install_status['file']); |
| 425 |
|
| 426 |
if (is_wp_error($active_plugin)) { |
| 427 |
wp_send_json_error( |
| 428 |
array( |
| 429 |
'mess' => $active_plugin->get_error_message(), |
| 430 |
) |
| 431 |
); |
| 432 |
} else { |
| 433 |
wp_send_json_success( |
| 434 |
array( |
| 435 |
'mess' => __('Install success', 'adminify'), |
| 436 |
) |
| 437 |
); |
| 438 |
} |
| 439 |
} else { |
| 440 |
wp_send_json_error( |
| 441 |
array( |
| 442 |
'mess' => 'Error', |
| 443 |
) |
| 444 |
); |
| 445 |
} |
| 446 |
} else { |
| 447 |
$is_active = is_plugin_active($plugin); |
| 448 |
$result = $upgrader->upgrade($plugin); |
| 449 |
|
| 450 |
if (is_wp_error($result)) { |
| 451 |
wp_send_json_error( |
| 452 |
array( |
| 453 |
'mess' => $result->get_error_message(), |
| 454 |
) |
| 455 |
); |
| 456 |
} else { |
| 457 |
activate_plugin($plugin); |
| 458 |
wp_send_json_success( |
| 459 |
array( |
| 460 |
'mess' => __('Update success', 'adminify'), |
| 461 |
'active' => $is_active, |
| 462 |
) |
| 463 |
); |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
} catch (\Exception $ex) { |
| 468 |
wp_send_json_error( |
| 469 |
array( |
| 470 |
'mess' => __('Error exception.', 'adminify'), |
| 471 |
array( |
| 472 |
'error' => $ex, |
| 473 |
), |
| 474 |
) |
| 475 |
); |
| 476 |
} catch (\Error $ex) { |
| 477 |
wp_send_json_error( |
| 478 |
array( |
| 479 |
'mess' => __('Error.', 'adminify'), |
| 480 |
array( |
| 481 |
'error' => $ex, |
| 482 |
), |
| 483 |
) |
| 484 |
); |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
|