RecommendedPluginsPage.php
218 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WhatsappScoped\YayCommerce\AdminShell\Pages; |
| 4 | |
| 5 | /** |
| 6 | * Renders the "Other Plugins" admin page and handles AJAX actions. |
| 7 | * Ported from YayMail Pro OtherPluginsMenu.php — de-branded. |
| 8 | * |
| 9 | * NOTE: Exceeds 200 LOC because three AJAX handlers (get/activate/upgrade) each |
| 10 | * require independent error handling + WP upgrader integration. Splitting them |
| 11 | * into separate classes would fragment cohesive plugin-management logic. |
| 12 | * Acceptable exception per code standards. |
| 13 | * @internal |
| 14 | */ |
| 15 | class RecommendedPluginsPage |
| 16 | { |
| 17 | protected static ?self $instance = null; |
| 18 | public static function get_instance() : self |
| 19 | { |
| 20 | if (null === self::$instance) { |
| 21 | self::$instance = new self(); |
| 22 | } |
| 23 | return self::$instance; |
| 24 | } |
| 25 | protected function __construct() |
| 26 | { |
| 27 | add_action('wp_ajax_yay_recommended_get_plugin_data', [$this, 'ajax_get_plugin_data'], 9); |
| 28 | add_action('wp_ajax_yay_recommended_activate_plugin', [$this, 'ajax_activate_plugin'], 9); |
| 29 | add_action('wp_ajax_yay_recommended_upgrade_plugin', [$this, 'ajax_upgrade_plugin'], 9); |
| 30 | } |
| 31 | public static function render() : void |
| 32 | { |
| 33 | if (\function_exists('WhatsappScoped\\WC')) { |
| 34 | $featured_tab = '<li class="plugin-install-tab plugin-install-featured" data-tab="featured"><a href="#">Featured</a></li>'; |
| 35 | $woo_tab = '<li class="plugin-install-tab plugin-install-woocommerce" data-tab="woocommerce"><a href="#" class="current" aria-current="page">WooCommerce</a></li>'; |
| 36 | } else { |
| 37 | $featured_tab = '<li class="plugin-install-tab plugin-install-featured" data-tab="featured"><a href="#" class="current" aria-current="page">Featured</a></li>'; |
| 38 | $woo_tab = '<li class="plugin-install-tab plugin-install-woocommerce" data-tab="woocommerce"><a href="#">WooCommerce</a></li>'; |
| 39 | } |
| 40 | ?> |
| 41 | <script> |
| 42 | document.querySelector("#wpbody-content").innerHTML = ""; |
| 43 | </script> |
| 44 | <div class="wrap"> |
| 45 | <div class="yay-recommended-plugins-layout"> |
| 46 | <div class="yay-recommended-plugins-layout-header"> |
| 47 | <div class="wp-filter yay-recommended-plugins-header"> |
| 48 | <h2 class="yay-recommended-plugins-header-title"><?php |
| 49 | \esc_attr_e('Recommended Plugins', 'yaycommerce'); |
| 50 | ?></h2> |
| 51 | <ul class="filter-links"> |
| 52 | <?php |
| 53 | echo wp_kses_post($featured_tab); |
| 54 | ?> |
| 55 | <li class="plugin-install-tab plugin-install-all" data-tab="all"><a href="#">All</a></li> |
| 56 | <?php |
| 57 | echo wp_kses_post($woo_tab); |
| 58 | ?> |
| 59 | <li class="plugin-install-tab plugin-install-management" data-tab="management"><a href="#">Management</a></li> |
| 60 | <li class="plugin-install-tab plugin-install-marketing" data-tab="marketing"><a href="#">Marketing</a></li> |
| 61 | </ul> |
| 62 | </div> |
| 63 | </div> |
| 64 | <div class="wp-list-table widefat plugin-install"> |
| 65 | <div id="the-list"></div> |
| 66 | </div> |
| 67 | </div> |
| 68 | </div> |
| 69 | <?php |
| 70 | } |
| 71 | public static function load_data() : void |
| 72 | { |
| 73 | add_action('admin_enqueue_scripts', [self::class, 'enqueue_scripts']); |
| 74 | } |
| 75 | public static function enqueue_scripts() : void |
| 76 | { |
| 77 | $assets_url = \plugin_dir_url(__FILE__) . '../../assets/'; |
| 78 | wp_enqueue_script('plugin-install'); |
| 79 | wp_enqueue_script('thickbox'); |
| 80 | wp_enqueue_style('thickbox'); |
| 81 | wp_enqueue_style('yaycommerce-other-plugins', $assets_url . 'css/other-plugins.css', [], '1.0'); |
| 82 | wp_register_script('yaycommerce-other-plugins', $assets_url . 'js/other-plugins.js', ['jquery'], '1.0', \true); |
| 83 | wp_localize_script('yaycommerce-other-plugins', 'yayRecommended', ['nonce' => wp_create_nonce('yay_recommended_nonce'), 'admin_ajax' => \admin_url('admin-ajax.php'), 'woo_active' => \function_exists('WhatsappScoped\\WC')]); |
| 84 | wp_enqueue_script('yaycommerce-other-plugins'); |
| 85 | } |
| 86 | /** |
| 87 | * Return the plugins to display. Filterable via yay_recommended_plugins_excluded. |
| 88 | * |
| 89 | * @return array<string, array> |
| 90 | */ |
| 91 | public static function get_other_plugins() : array |
| 92 | { |
| 93 | return include __DIR__ . '/../../views/recommended-plugins-data.php'; |
| 94 | } |
| 95 | public function ajax_get_plugin_data() : void |
| 96 | { |
| 97 | try { |
| 98 | if (!\current_user_can('install_plugins')) { |
| 99 | wp_send_json_error(['mess' => \__('Permission denied', 'yaycommerce')]); |
| 100 | } |
| 101 | if (!isset($_POST['tab'])) { |
| 102 | wp_send_json_error(['mess' => 'Missing tab']); |
| 103 | } |
| 104 | $nonce = isset($_POST['nonce']) ? \sanitize_text_field($_POST['nonce']) : ''; |
| 105 | if (!wp_verify_nonce($nonce, 'yay_recommended_nonce')) { |
| 106 | wp_send_json_error(['mess' => \__('Nonce is invalid', 'yaycommerce')]); |
| 107 | } |
| 108 | require_once \ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 109 | $tab = \sanitize_text_field($_POST['tab']); |
| 110 | $recommended_data = \apply_filters('yay_recommended_plugins_excluded', self::get_other_plugins()); |
| 111 | $recommended_plugins = []; |
| 112 | foreach ($recommended_data as $key => $plugin) { |
| 113 | if (\in_array($tab, $plugin['type'], \true) || 'all' === $tab) { |
| 114 | $recommended_plugins[$key] = $plugin; |
| 115 | } |
| 116 | } |
| 117 | \ob_start(); |
| 118 | include __DIR__ . '/../../views/recommended-plugins-page.php'; |
| 119 | $html = \ob_get_clean(); |
| 120 | wp_send_json_success(['mess' => \__('Get data success', 'yaycommerce'), 'html' => $html]); |
| 121 | } catch (\Exception $ex) { |
| 122 | wp_send_json_error(['mess' => \__('Error exception.', 'yaycommerce'), ['error' => $ex]]); |
| 123 | } catch (\Error $ex) { |
| 124 | wp_send_json_error(['mess' => \__('Error.', 'yaycommerce'), ['error' => $ex]]); |
| 125 | } |
| 126 | } |
| 127 | public function ajax_activate_plugin() : void |
| 128 | { |
| 129 | try { |
| 130 | if (!\current_user_can('activate_plugins')) { |
| 131 | wp_send_json_error(['mess' => \__('Permission denied', 'yaycommerce')]); |
| 132 | } |
| 133 | if (!isset($_POST['file'])) { |
| 134 | wp_send_json_error(['mess' => 'Missing file']); |
| 135 | } |
| 136 | $nonce = isset($_POST['nonce']) ? \sanitize_text_field($_POST['nonce']) : ''; |
| 137 | if (!wp_verify_nonce($nonce, 'yay_recommended_nonce')) { |
| 138 | wp_send_json_error(['mess' => \__('Nonce is invalid', 'yaycommerce')]); |
| 139 | } |
| 140 | $file = \sanitize_text_field($_POST['file']); |
| 141 | $result = activate_plugin($file); |
| 142 | if (is_wp_error($result)) { |
| 143 | wp_send_json_error(['mess' => $result->get_error_message()]); |
| 144 | } |
| 145 | wp_send_json_success(['mess' => \__('Activate success', 'yaycommerce')]); |
| 146 | } catch (\Exception $ex) { |
| 147 | wp_send_json_error(['mess' => \__('Error exception.', 'yaycommerce'), ['error' => $ex]]); |
| 148 | } catch (\Error $ex) { |
| 149 | wp_send_json_error(['mess' => \__('Error.', 'yaycommerce'), ['error' => $ex]]); |
| 150 | } |
| 151 | } |
| 152 | public function ajax_upgrade_plugin() : void |
| 153 | { |
| 154 | try { |
| 155 | if (!\current_user_can('install_plugins')) { |
| 156 | wp_send_json_error(['mess' => \__('Permission denied', 'yaycommerce')]); |
| 157 | } |
| 158 | require_once \ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 159 | require_once \ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 160 | require_once \ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; |
| 161 | require_once \ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php'; |
| 162 | if (!isset($_POST['plugin'])) { |
| 163 | wp_send_json_error(['mess' => 'Missing plugin']); |
| 164 | } |
| 165 | $nonce = isset($_POST['nonce']) ? \sanitize_text_field($_POST['nonce']) : ''; |
| 166 | if (!wp_verify_nonce($nonce, 'yay_recommended_nonce')) { |
| 167 | wp_send_json_error(['mess' => \__('Nonce is invalid', 'yaycommerce')]); |
| 168 | } |
| 169 | $plugin = \sanitize_text_field($_POST['plugin']); |
| 170 | $type = isset($_POST['type']) ? \sanitize_text_field($_POST['type']) : 'install'; |
| 171 | $skin = new \WP_Ajax_Upgrader_Skin(); |
| 172 | $upgrader = new \Plugin_Upgrader($skin); |
| 173 | if ('install' === $type) { |
| 174 | $result = $upgrader->install($plugin); |
| 175 | if (is_wp_error($result)) { |
| 176 | wp_send_json_error(['mess' => $result->get_error_message()]); |
| 177 | } |
| 178 | $args = ['slug' => $upgrader->result['destination_name'], 'fields' => ['short_description' => \true, 'icons' => \true, 'banners' => \false, 'reviews' => \false, 'sections' => \false]]; |
| 179 | $plugin_data = plugins_api('plugin_information', $args); |
| 180 | if ($plugin_data && !is_wp_error($plugin_data)) { |
| 181 | $install_status = install_plugin_install_status($plugin_data); |
| 182 | $active_plugin = activate_plugin($install_status['file']); |
| 183 | if (is_wp_error($active_plugin)) { |
| 184 | wp_send_json_error(['mess' => $active_plugin->get_error_message()]); |
| 185 | } |
| 186 | wp_send_json_success(['mess' => \__('Install success', 'yaycommerce')]); |
| 187 | } else { |
| 188 | wp_send_json_error(['mess' => 'Error']); |
| 189 | } |
| 190 | } else { |
| 191 | $is_active = is_plugin_active($plugin); |
| 192 | $result = $upgrader->upgrade($plugin); |
| 193 | if (is_wp_error($result)) { |
| 194 | wp_send_json_error(['mess' => $result->get_error_message()]); |
| 195 | } |
| 196 | activate_plugin($plugin); |
| 197 | wp_send_json_success(['mess' => \__('Update success', 'yaycommerce'), 'active' => $is_active]); |
| 198 | } |
| 199 | } catch (\Exception $ex) { |
| 200 | wp_send_json_error(['mess' => \__('Error exception.', 'yaycommerce'), ['error' => $ex]]); |
| 201 | } catch (\Error $ex) { |
| 202 | wp_send_json_error(['mess' => \__('Error.', 'yaycommerce'), ['error' => $ex]]); |
| 203 | } |
| 204 | } |
| 205 | public function check_pro_version_exists(array $plugin_detail) : ?string |
| 206 | { |
| 207 | $all_plugins = get_plugins(); |
| 208 | $slug = $plugin_detail['slug'] ?? ''; |
| 209 | $map = ['filebird' => ['filebird-pro/filebird.php'], 'yaymail' => ['yaymail-pro/yaymail.php', 'email-customizer-for-woocommerce/yaymail.php'], 'yaycurrency' => ['yaycurrency-pro/yay-currency.php', 'multi-currency-switcher/yay-currency.php'], 'yaysmtp' => ['yaysmtp-pro/yay-smtp.php'], 'yayswatches' => ['yayswatches-pro/yay-swatches.php'], 'yayextra' => ['yayextra-pro/yayextra.php'], 'yaypricing' => ['yaypricing-pro/yaypricing.php', 'dynamic-pricing-discounts/yaypricing.php'], 'yay-customer-reviews-woocommerce' => ['yayreviews-pro/yay-customer-reviews-woocommerce.php'], 'yay-wholesale-b2b' => ['yay-wholesale-b2b-pro/yay-wholesale-b2b.php'], 'yayboost-sales-booster-for-woocommerce' => ['yayboost-pro/yayboost-sales-booster-for-woocommerce.php'], 'cf7-multi-step' => ['contact-form-7-multi-step-pro/contact-form-7-multi-step.php'], 'cf7-database' => ['contact-form-7-database-pro/cf7-database.php'], 'wp-whatsapp' => ['whatsapp-for-wordpress/whatsapp.php']]; |
| 210 | foreach ($map[$slug] ?? [] as $basename) { |
| 211 | if (\array_key_exists($basename, $all_plugins)) { |
| 212 | return $basename; |
| 213 | } |
| 214 | } |
| 215 | return null; |
| 216 | } |
| 217 | } |
| 218 |