wp-optimize
/
vendor
/
team-updraft
/
common-libs
/
src
/
updraft-plugin-deinstall-dialog
/
class-updraft-deinstall-dialog.php
class-updraft-deinstall-dialog.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.7.0, at vendor/team-updraft/common-libs/src/updraft-plugin-deinstall-dialog/class-updraft-deinstall-dialog.php
| 1 | <?php |
| 2 | |
| 3 | if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 | |
| 5 | if (!class_exists('Updraft_Deinstall_Dialog_v1')) { |
| 6 | |
| 7 | /** |
| 8 | * Class Updraft_Deinstall_Dialog |
| 9 | * |
| 10 | * Provides a customizable de-installation confirmation dialog for WordPress plugins, |
| 11 | * with support for jQuery UI or Thickbox modal dialogs. Handles AJAX-based confirmation |
| 12 | * and executes plugin-specific actions based on user input. |
| 13 | */ |
| 14 | class Updraft_Deinstall_Dialog_v1 { |
| 15 | |
| 16 | /** |
| 17 | * Static library version. |
| 18 | * |
| 19 | * Update this value only when changes are made that affect the dialog’s |
| 20 | * JavaScript, CSS, or rendered HTML (e.g. altering assets, markup, or UI |
| 21 | * behavior). This ensures proper cache busting and prevents conflicts with |
| 22 | * previously enqueued scripts. |
| 23 | * |
| 24 | * Backend-only PHP changes that do not affect the output or assets do not |
| 25 | * require a version bump. |
| 26 | * |
| 27 | * @var string $version Version of the dialog script. |
| 28 | */ |
| 29 | const VERSION = 'v1'; |
| 30 | |
| 31 | /** |
| 32 | * @var array $options Array of configuration options for the dialog. |
| 33 | */ |
| 34 | private $options = array(); |
| 35 | |
| 36 | /** |
| 37 | * Constructor for Updraft_Deinstall_Dialog. |
| 38 | * |
| 39 | * Initializes options and sets up necessary hooks for enqueueing assets, |
| 40 | * handling AJAX requests, and adding plugin action links. |
| 41 | * |
| 42 | * @param array $options Required. Array of dialog options. |
| 43 | * - 'script_handler' => string Required. Dialog script handler name used in AJAX and enqueue hooks. |
| 44 | * - 'plugin_slug' => string Required. Main plugin file path relative to wp-content/plugins, e.g. updraftplus/updraftplus.php |
| 45 | * - 'dialog_title' => string Optional. Title for the confirmation dialog. Default 'Deactivation Confirmation'. |
| 46 | * - 'deactivate_label' => string Optional. Label for the "Deactivate" button. Default 'Deactivate'. |
| 47 | * - 'cancel_label' => string Optional. Label for the "Cancel" button. Default 'Cancel'. |
| 48 | * - 'dialog_type' => string Optional. Type of dialog to display ('jqueryui' or 'thickbox'). Default 'jqueryui'. |
| 49 | * - 'template_file' => string Optional. Path to the HTML template for the dialog content. Default 'templates/dialog.php'. |
| 50 | * |
| 51 | * @throws InvalidArgumentException If required options 'script_handler' or 'plugin_slug' are missing. |
| 52 | */ |
| 53 | public function __construct($options) { |
| 54 | if (!isset($options['script_handler'], $options['plugin_slug'])) throw new InvalidArgumentException('Missing required options.'); |
| 55 | |
| 56 | if (isset($options['dialog_type']) && !in_array($options['dialog_type'], array('jqueryui', 'thickbox'))) unset($options['dialog_type']); |
| 57 | |
| 58 | // Set default options and merge with any passed options. |
| 59 | $defaults = array( |
| 60 | 'dialog_title' => 'Deactivation Confirmation', |
| 61 | 'deactivate_label' => 'Deactivate', |
| 62 | 'cancel_label' => 'Cancel', |
| 63 | 'dialog_type' => 'jqueryui', |
| 64 | 'template_file' => plugin_dir_path(__FILE__).'templates/dialog.php', |
| 65 | 'show_on_network_admin' => true, |
| 66 | 'show_on_subsites' => true, |
| 67 | ); |
| 68 | |
| 69 | $this->options = array_merge($defaults, $options); |
| 70 | |
| 71 | add_action('admin_enqueue_scripts', array($this, 'enqueue_dialog_assets')); |
| 72 | add_filter('plugin_action_links', array($this, 'modify_deactivate_link'), 10, 2); |
| 73 | add_filter('network_admin_plugin_action_links', array($this, 'modify_deactivate_link'), 10, 2); |
| 74 | add_action('wp_ajax_'.$this->options['script_handler'].'_deinstall_confirm', array($this, 'handle_deinstall_confirm')); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Enqueue dialog assets (JS) based on the specified dialog type. |
| 79 | * |
| 80 | * Only enqueues on the plugins page and localizes dialog data for use in JavaScript. |
| 81 | */ |
| 82 | public function enqueue_dialog_assets() { |
| 83 | global $pagenow; |
| 84 | |
| 85 | if ('plugins.php' !== $pagenow) return; |
| 86 | |
| 87 | if ($this->options['dialog_type'] === 'thickbox') { |
| 88 | wp_enqueue_script('thickbox'); |
| 89 | wp_enqueue_style('thickbox'); |
| 90 | if (version_compare($GLOBALS['wp_version'], '3.9', '<')) { |
| 91 | wp_enqueue_style('deinstall-dialog-thickbox-css', plugins_url('assets/css/deinstall-dialog-thickbox.css', __FILE__), array('thickbox'), self::VERSION); |
| 92 | } |
| 93 | $file = plugins_url('assets/js/deinstall-dialog-thickbox.js', __FILE__); |
| 94 | $dependencies = array('jquery', 'thickbox'); |
| 95 | } else { |
| 96 | $file = plugins_url('assets/js/deinstall-dialog-jqueryui.js', __FILE__); |
| 97 | $dependencies = array('jquery', 'jquery-ui-dialog'); |
| 98 | } |
| 99 | |
| 100 | $script_handler = 'updraft-deinstall-dialog-'.$this->options['script_handler'].'-'.$this->options['dialog_type'].'-'.self::VERSION; |
| 101 | |
| 102 | wp_register_script($script_handler, $file, $dependencies, self::VERSION, true); |
| 103 | |
| 104 | $data = array( |
| 105 | 'nonce' => wp_create_nonce($this->options['script_handler'].'_deinstall_nonce'), |
| 106 | 'dialog_html' => $this->load_dialog_template($this->options['template_file']), |
| 107 | 'dialog_title' => $this->options['dialog_title'], |
| 108 | 'deactivate_label' => $this->options['deactivate_label'], |
| 109 | 'cancel_label' => $this->options['cancel_label'], |
| 110 | ); |
| 111 | |
| 112 | wp_localize_script( |
| 113 | $script_handler, |
| 114 | 'updraft_deinstall_data_'.$this->options['script_handler'], |
| 115 | $data |
| 116 | ); |
| 117 | |
| 118 | wp_enqueue_script($script_handler); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Load the HTML template for the dialog. |
| 123 | * |
| 124 | * @param string $file Path to the dialog template file. |
| 125 | * @return string HTML content of the dialog. |
| 126 | */ |
| 127 | public function load_dialog_template($file) { |
| 128 | $form = '<form id="'.$this->options['script_handler'].'-deactivate-form">'; |
| 129 | ob_start(); |
| 130 | include $file; |
| 131 | $form .= ob_get_clean(); |
| 132 | $form .= '</form>'; |
| 133 | |
| 134 | return $form; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Handle AJAX request to confirm de-installation. |
| 139 | * |
| 140 | * Verifies nonce and triggers the main deinstallation action for the plugin. |
| 141 | */ |
| 142 | public function handle_deinstall_confirm() { |
| 143 | if (!current_user_can('activate_plugins') || !isset($_POST['_nonce']) || !wp_verify_nonce($_POST['_nonce'], $this->options['script_handler'].'_deinstall_nonce')) { |
| 144 | wp_send_json_error(array('message' => 'Nonce and/or capability verification failed.'), 403); |
| 145 | } |
| 146 | |
| 147 | do_action($this->options['script_handler'].'_deinstall'); |
| 148 | wp_send_json_success(); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Modify the plugin action links to attach deinstallation data attribute. |
| 153 | * |
| 154 | * @param array $links Array of action links for the plugin. |
| 155 | * @param string $file Current plugin file. |
| 156 | * @return array Modified array of action links. |
| 157 | */ |
| 158 | public function modify_deactivate_link($links, $file) { |
| 159 | |
| 160 | if (!is_array($links) || $this->options['plugin_slug'] !== $file) return $links; |
| 161 | |
| 162 | if (is_multisite()) { |
| 163 | |
| 164 | if (!is_network_admin() && empty($this->options['show_on_subsites'])) { |
| 165 | return $links; |
| 166 | } |
| 167 | |
| 168 | if (is_network_admin() && empty($this->options['show_on_network_admin'])) { |
| 169 | return $links; |
| 170 | } |
| 171 | } |
| 172 | foreach ($links as $key => $value) { |
| 173 | if ($key != 'deactivate') continue; |
| 174 | |
| 175 | $links[$key] = preg_replace( |
| 176 | '/<a(.*?)href="(.*?)"(.*?)>/i', |
| 177 | '<a$1href="$2"$3 onclick="window.updraft_deinstall_'.esc_attr($this->options['dialog_type']).'_'.self::VERSION.'(event, \''.esc_attr($this->options['script_handler']).'\');">', |
| 178 | $value |
| 179 | ); |
| 180 | } |
| 181 | return $links; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 |