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