| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace WPAdminify\Libs; | |
| 3 | +namespace PXLBSAdminify\Libs; | |
| 4 | 4 | |
| 5 | 5 | // No, Direct access Sir !!! |
| 6 | 6 | if (!defined('ABSPATH')) { |
| 7 | 7 | exit; |
| @@ -24,11 +24,9 @@ | ||
| 24 | 24 | public $plugins_list = []; |
| 25 | 25 | public $sub_menu; |
| 26 | 26 | public $menu_order; |
| 27 | 27 | |
| 28 | - public $server_url = 'https://coupon.wpadminify.com/'; | |
| 29 | 28 | |
| 30 | - | |
| 31 | 29 | /** |
| 32 | 30 | * Constructor method |
| 33 | 31 | * |
| 34 | 32 | * @param integer $menu_order . |
| @@ -47,33 +45,33 @@ | ||
| 47 | 45 | add_action('network_admin_menu', array($this, 'admin_menu'), 1000); |
| 48 | 46 | } else { |
| 49 | 47 | add_action('admin_menu', array($this, 'admin_menu'), 1000); |
| 50 | 48 | } |
| 51 | - add_action('wp_ajax_jltwp_adminify_addons_upgrade_plugin', array($this, 'jltwp_adminify_addons_upgrade_plugin')); | |
| 52 | - add_action('wp_ajax_jltwp_adminify_addons_activate_plugin', array($this, 'jltwp_adminify_addons_activate_plugin')); | |
| 49 | + add_action('wp_ajax_pxlbsadminify_addons_upgrade_plugin', array($this, 'pxlbsadminify_addons_upgrade_plugin')); | |
| 50 | + add_action('wp_ajax_pxlbsadminify_addons_activate_plugin', array($this, 'pxlbsadminify_addons_activate_plugin')); | |
| 53 | 51 | // Notify the site admin when a renamed legacy addon is detected |
| 54 | 52 | // alongside its replacement. Per WordPress.org plugin guidelines, |
| 55 | 53 | // we must not deactivate or activate plugins automatically; the |
| 56 | 54 | // user has to perform the swap themselves from the Plugins screen. |
| 57 | 55 | add_action('admin_notices', array($this, 'maybe_renamed_addon_notice')); |
| 58 | - add_action( 'rest_api_init', array( $this , 'jltwp_adminify_addons_rest_routes') ); | |
| 56 | + add_action( 'rest_api_init', array( $this , 'addons_rest_routes') ); | |
| 59 | 57 | } |
| 60 | 58 | |
| 61 | - public function jltwp_adminify_addons_rest_routes() { | |
| 59 | + public function addons_rest_routes() { | |
| 62 | 60 | register_rest_route('adminify/v1', '/get-addons-list', array( |
| 63 | 61 | 'methods' => 'GET', |
| 64 | - 'callback' => [$this, 'jltwp_adminify_get_addons_plugins_list'], | |
| 65 | - 'permission_callback' => [$this, 'adminify_is_admin_user'], | |
| 62 | + 'callback' => [$this, 'get_addons_plugins_list'], | |
| 63 | + 'permission_callback' => [$this, 'check_is_admin_user'], | |
| 66 | 64 | )); |
| 67 | 65 | |
| 68 | 66 | register_rest_route('adminify/v1', '/install-addons', array( |
| 69 | 67 | 'methods' => 'POST', |
| 70 | - 'callback' => [$this, 'jltwp_adminify_install_addons'], | |
| 71 | - 'permission_callback' => [$this, 'adminify_verify_nonce_and_permissions'], | |
| 68 | + 'callback' => [$this, 'install_addons'], | |
| 69 | + 'permission_callback' => [$this, 'check_verify_nonce_and_permissions'], | |
| 72 | 70 | )); |
| 73 | 71 | } |
| 74 | 72 | |
| 75 | - public function adminify_is_admin_user() { | |
| 73 | + public function check_is_admin_user() { | |
| 76 | 74 | if ( is_multisite() && ! is_super_admin() ) { |
| 77 | 75 | return new \WP_Error('rest_forbidden', __('You are not allowed to access this resource.', 'adminify'), array('status' => 403)); |
| 78 | 76 | } |
| 79 | 77 | if ( ! current_user_can('manage_options') ) { |
| @@ -81,9 +79,9 @@ | ||
| 81 | 79 | } |
| 82 | 80 | return true; |
| 83 | 81 | } |
| 84 | 82 | |
| 85 | - public function adminify_verify_nonce_and_permissions() { | |
| 83 | + public function check_verify_nonce_and_permissions() { | |
| 86 | 84 | // The install-addons endpoint may both install AND activate |
| 87 | 85 | // addons depending on each addon's current status, so the |
| 88 | 86 | // caller must hold BOTH capabilities. On multisite this also |
| 89 | 87 | // requires super admin. |
| @@ -108,10 +106,15 @@ | ||
| 108 | 106 | return true; |
| 109 | 107 | } |
| 110 | 108 | |
| 111 | 109 | |
| 112 | - public function jltwp_adminify_get_addons_plugins_list() { | |
| 113 | - $plugins = $this->plugins_list; | |
| 110 | + public function get_addons_plugins_list() { | |
| 111 | + // Fetch the catalogue on demand. This callback only runs on the | |
| 112 | + // Add-ons page (a user action), so the remote request is not made on | |
| 113 | + // routine admin page loads. | |
| 114 | + $plugins = ( method_exists( $this, 'get_adminify_plugins_lists' ) ) | |
| 115 | + ? (array) $this->get_adminify_plugins_lists() | |
| 116 | + : (array) $this->plugins_list; | |
| 114 | 117 | unset($plugins['master-addons']); |
| 115 | 118 | $all_plugins = get_plugins(); |
| 116 | 119 | $active_plugins = get_option('active_plugins'); |
| 117 | 120 | foreach( $plugins as $slug => $plugin){ |
| @@ -133,15 +136,15 @@ | ||
| 133 | 136 | |
| 134 | 137 | } |
| 135 | 138 | |
| 136 | 139 | |
| 137 | - public function jltwp_adminify_install_addons( $request ) { | |
| 140 | + public function install_addons( $request ) { | |
| 138 | 141 | $addons = $request->get_param('addons'); |
| 139 | 142 | if ( empty($addons) || ! is_array($addons) ) { |
| 140 | 143 | return new \WP_Error('no_addons', __('No addons were selected.', 'adminify'), array('status' => 400)); |
| 141 | 144 | } |
| 142 | 145 | |
| 143 | - $plugins_list = $this->jltwp_adminify_get_addons_plugins_list()->data; | |
| 146 | + $plugins_list = $this->get_addons_plugins_list()->data; | |
| 144 | 147 | foreach( $addons as $key => $plugin ) { |
| 145 | 148 | $plugin = sanitize_key( $plugin ); |
| 146 | 149 | if ( ! isset( $plugins_list[ $plugin ] ) ) { |
| 147 | 150 | continue; |
| @@ -149,9 +152,9 @@ | ||
| 149 | 152 | if ( $plugins_list[ $plugin ]['status'] === 'activated' ) { |
| 150 | 153 | continue; |
| 151 | 154 | } |
| 152 | 155 | if ( $plugins_list[ $plugin ]['status'] === 'installed' ) { |
| 153 | - $this->jltwp_adminify_activate_plugin_by_slug( $plugin ); | |
| 156 | + $this->activate_plugin_by_slug( $plugin ); | |
| 154 | 157 | continue; |
| 155 | 158 | } |
| 156 | 159 | $params = [ |
| 157 | 160 | 'request_type' => 'rest', |
| @@ -157,15 +160,15 @@ | ||
| 157 | 160 | 'request_type' => 'rest', |
| 158 | 161 | 'plugin' => $plugins_list[ $plugin ]['download_link'], |
| 159 | 162 | ]; |
| 160 | 163 | |
| 161 | - $this->jltwp_adminify_addons_upgrade_plugin( $params ); | |
| 164 | + $this->pxlbsadminify_addons_upgrade_plugin( $params ); | |
| 162 | 165 | } |
| 163 | 166 | |
| 164 | 167 | return rest_ensure_response(['message' => __('Addons processed.', 'adminify'), 'addons' => $addons]); |
| 165 | 168 | } |
| 166 | 169 | |
| 167 | - function jltwp_adminify_activate_plugin_by_slug($slug) { | |
| 170 | + function activate_plugin_by_slug($slug) { | |
| 168 | 171 | // Activation requires the activate_plugins capability in |
| 169 | 172 | // addition to whatever capability gated the calling endpoint. |
| 170 | 173 | // On multisite, activation must be performed by a super admin. |
| 171 | 174 | if ( is_multisite() && ! is_super_admin() ) { |
| @@ -316,27 +319,10 @@ | ||
| 316 | 319 | * |
| 317 | 320 | * @return void |
| 318 | 321 | */ |
| 319 | 322 | |
| 320 | - public function jltwp_adminify_addons_check() | |
| 323 | + public function addons_check() | |
| 321 | 324 | { |
| 322 | - | |
| 323 | - $license = jltwp_adminify()->_get_license(); | |
| 324 | - | |
| 325 | - if (!is_object($license) || !$license->is_valid() || !$license->is_active()) return; | |
| 326 | - | |
| 327 | - if ( $this->is_eligible_for_coupon() ) { | |
| 328 | - // Get the coupon | |
| 329 | - $coupon = $this->maybe_create_and_get_coupon(); | |
| 330 | - if (!empty($coupon) && !empty($coupon['code'])) { | |
| 331 | - echo '<h3>' . sprintf( | |
| 332 | - /* translators: %s: Coupon code */ | |
| 333 | - esc_html__('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>', 'adminify'), | |
| 334 | - esc_attr($coupon['code']) | |
| 335 | - ) . '</h3> '; | |
| 336 | - } | |
| 337 | - } | |
| 338 | - | |
| 339 | 325 | echo '<style> |
| 340 | 326 | #fs_addons .fs-cards-list{ display: flex; } |
| 341 | 327 | #fs_addons .fs-cards-list .fs-card .fs-inner .fs-cta .button{ |
| 342 | 328 | top: 112px; |
| @@ -345,73 +331,10 @@ | ||
| 345 | 331 | border-radius: 3px !important; |
| 346 | 332 | }</style>'; |
| 347 | 333 | } |
| 348 | 334 | |
| 349 | - public function is_eligible_for_coupon() { | |
| 350 | 335 | |
| 351 | - $is_eligible = get_option('wp_adminify_addon__is_eligible_for_coupon', null); | |
| 352 | 336 | |
| 353 | - if ( $is_eligible !== null ) return wp_validate_boolean($is_eligible); | |
| 354 | - $args = [ | |
| 355 | - 'license' => base64_encode(json_encode(jltwp_adminify()->_get_license())), | |
| 356 | - 'action' => 'check_eligibility' | |
| 357 | - ]; | |
| 358 | - | |
| 359 | - $request_uri = add_query_arg($args, $this->server_url); | |
| 360 | - | |
| 361 | - $response = wp_remote_get($request_uri); | |
| 362 | - | |
| 363 | - if (!is_wp_error($response) && $response['response']['code'] === 200) { | |
| 364 | - $file_contents = wp_remote_retrieve_body($response); | |
| 365 | - $is_eligible = json_decode($file_contents, true); | |
| 366 | - update_option('wp_adminify_addon__is_eligible_for_coupon', wp_validate_boolean($is_eligible)); | |
| 367 | - return $is_eligible; | |
| 368 | - } | |
| 369 | - | |
| 370 | - return false; | |
| 371 | - } | |
| 372 | - | |
| 373 | - public function maybe_delete_corrupted_coupon(){ | |
| 374 | - $coupon_delete_check = get_option('wp_adminify_addon__coupon_is_deleted', false); | |
| 375 | - if($coupon_delete_check != true){ | |
| 376 | - delete_option('wp_adminify_addon__coupon'); | |
| 377 | - update_option('wp_adminify_addon__coupon_is_deleted', true); | |
| 378 | - } | |
| 379 | - } | |
| 380 | - | |
| 381 | - public function maybe_create_and_get_coupon() | |
| 382 | - { | |
| 383 | - $this->maybe_delete_corrupted_coupon(); | |
| 384 | - $coupon = get_option('wp_adminify_addon__coupon'); | |
| 385 | - | |
| 386 | - if (!empty($coupon)) return $coupon; | |
| 387 | - | |
| 388 | - // communicate hit hserver get coupon | |
| 389 | - $args = [ | |
| 390 | - 'license' => base64_encode(json_encode(jltwp_adminify()->_get_license())), | |
| 391 | - 'action' => 'get_coupon' | |
| 392 | - ]; | |
| 393 | - | |
| 394 | - $response = wp_remote_get(add_query_arg($args, $this->server_url)); | |
| 395 | - | |
| 396 | - if (!is_wp_error($response) && $response['response']['code'] === 200) { | |
| 397 | - | |
| 398 | - $file_contents = wp_remote_retrieve_body($response); | |
| 399 | - $response_data = json_decode($file_contents, true); | |
| 400 | - | |
| 401 | - if (!empty($response_data) && is_array($response_data) && !empty($response_data['id']) && !empty($response_data['code']) ) { | |
| 402 | - $coupon = [ | |
| 403 | - 'id' => $response_data['id'], | |
| 404 | - 'code' => $response_data['code'] | |
| 405 | - ]; | |
| 406 | - update_option('wp_adminify_addon__coupon', $coupon); | |
| 407 | - } | |
| 408 | - } | |
| 409 | - | |
| 410 | - return $coupon; | |
| 411 | - } | |
| 412 | - | |
| 413 | - | |
| 414 | 337 | /** |
| 415 | 338 | * Header |
| 416 | 339 | */ |
| 417 | 340 | public function header() |
| @@ -421,9 +344,9 @@ | ||
| 421 | 344 | <div class='wp-adminify-addons-title'> |
| 422 | 345 | <h2> |
| 423 | 346 | <?php echo esc_html__('Add Ons for Adminify', 'adminify'); ?> |
| 424 | 347 | </h2> |
| 425 | - <?php $this->jltwp_adminify_addons_check(); ?> | |
| 348 | + <?php $this->addons_check(); ?> | |
| 426 | 349 | </div> |
| 427 | 350 | <div class='wp-adminify-addons-menu'> |
| 428 | 351 | <div class="wp-filter"> |
| 429 | 352 | <ul class="filter-links"> |
| @@ -476,10 +399,19 @@ | ||
| 476 | 399 | * Body |
| 477 | 400 | */ |
| 478 | 401 | public function plugins() |
| 479 | 402 | { |
| 403 | + // $this->plugins_list is populated at construction only from the | |
| 404 | + // cached catalogue, which is empty until a live fetch runs. The | |
| 405 | + // Add-ons page render is itself an explicit user action, so fall | |
| 406 | + // back to the bundled catalogue here so the cards always show. | |
| 407 | + $plugins_list = $this->plugins_list; | |
| 480 | 408 | |
| 481 | - foreach ($this->plugins_list as $key => $plugin) { | |
| 409 | + if ( empty( $plugins_list ) && method_exists( $this, 'get_adminify_plugins_lists' ) ) { | |
| 410 | + $plugins_list = (array) $this->get_adminify_plugins_lists(); | |
| 411 | + } | |
| 412 | + | |
| 413 | + foreach ($plugins_list as $key => $plugin) { | |
| 482 | 414 | $install_status = \install_plugin_install_status($plugin); |
| 483 | 415 | $classes = implode(' ', $plugin['type']); |
| 484 | 416 | |
| 485 | 417 | $more_details = self_admin_url( |
| @@ -619,9 +551,9 @@ | ||
| 619 | 551 | * Activate Plugins |
| 620 | 552 | * |
| 621 | 553 | * @author Jewel Theme <support@jeweltheme.com> |
| 622 | 554 | */ |
| 623 | - public function jltwp_adminify_addons_activate_plugin() | |
| 555 | + public function pxlbsadminify_addons_activate_plugin() | |
| 624 | 556 | { |
| 625 | 557 | if (empty($_POST['plugin'])) { |
| 626 | 558 | return; |
| 627 | 559 | } |
| @@ -627,9 +559,9 @@ | ||
| 627 | 559 | } |
| 628 | 560 | try { |
| 629 | 561 | $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; |
| 630 | 562 | |
| 631 | - if (!wp_verify_nonce($nonce, 'jltwp_adminify_addons_nonce')) { | |
| 563 | + if (!wp_verify_nonce($nonce, 'pxlbsadminify_addons_nonce')) { | |
| 632 | 564 | wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify'))); |
| 633 | 565 | } |
| 634 | 566 | |
| 635 | 567 | // Security check - only administrators can activate plugins |
| @@ -639,12 +571,23 @@ | ||
| 639 | 571 | |
| 640 | 572 | $plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); |
| 641 | 573 | $plugin_links = array_values(wp_list_pluck($this->plugins_list, 'slug')); |
| 642 | 574 | |
| 643 | - if (!in_array(dirname($plugin), $plugin_links)) { | |
| 575 | + if (!in_array(dirname($plugin), $plugin_links, true)) { | |
| 644 | 576 | wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify'))); |
| 645 | 577 | } |
| 646 | 578 | |
| 579 | + // Resolve against the list of actually installed plugins so that | |
| 580 | + // only a known plugin file is ever passed to activate_plugin(). | |
| 581 | + if (!function_exists('get_plugins')) { | |
| 582 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 583 | + } | |
| 584 | + $installed_plugins = array_keys(get_plugins()); | |
| 585 | + | |
| 586 | + if (!in_array($plugin, $installed_plugins, true)) { | |
| 587 | + wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify'))); | |
| 588 | + } | |
| 589 | + | |
| 647 | 590 | $result = activate_plugin($plugin); |
| 648 | 591 | |
| 649 | 592 | if (is_wp_error($result)) { |
| 650 | 593 | wp_send_json_error( |
| @@ -702,9 +645,9 @@ | ||
| 702 | 645 | * Upgrade Plugins required Libraries |
| 703 | 646 | * |
| 704 | 647 | * @author Jewel Theme <support@jeweltheme.com> |
| 705 | 648 | */ |
| 706 | - public function jltwp_adminify_addons_upgrade_plugin( $params = null ) | |
| 649 | + public function pxlbsadminify_addons_upgrade_plugin( $params = null ) | |
| 707 | 650 | { |
| 708 | 651 | if ($params == null && empty($_POST['plugin'])) { |
| 709 | 652 | return; |
| 710 | 653 | } |
| @@ -717,9 +660,9 @@ | ||
| 717 | 660 | |
| 718 | 661 | if($params == null){ |
| 719 | 662 | $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; |
| 720 | 663 | |
| 721 | - if (!wp_verify_nonce($nonce, 'jltwp_adminify_addons_nonce')) { | |
| 664 | + if (!wp_verify_nonce($nonce, 'pxlbsadminify_addons_nonce')) { | |
| 722 | 665 | wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify'))); |
| 723 | 666 | } |
| 724 | 667 | $plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); |
| 725 | 668 | }else{ |