| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Admin Hooks Handler |
| 5 |
* |
| 6 |
* @package IvyForms |
| 7 |
* @since 0.5.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace IvyForms\Plugin; |
| 11 |
|
| 12 |
use IvyForms\Services\Settings\SettingsService; |
| 13 |
|
| 14 |
/** |
| 15 |
* AdminHooks |
| 16 |
* |
| 17 |
* Handles all admin-related hooks and functionality |
| 18 |
*/ |
| 19 |
class AdminHooks |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Settings Service |
| 23 |
*/ |
| 24 |
private ?SettingsService $settingsService = null; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor |
| 28 |
* |
| 29 |
* @param SettingsService|null $settingsService |
| 30 |
*/ |
| 31 |
public function __construct(?SettingsService $settingsService = null) |
| 32 |
{ |
| 33 |
$this->settingsService = $settingsService; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Register admin hooks |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function register(): void |
| 42 |
{ |
| 43 |
add_action('admin_init', [$this, 'adminInit']); |
| 44 |
add_filter('admin_body_class', [$this, 'addAdminBodyClass']); |
| 45 |
add_action('admin_enqueue_scripts', [$this, 'adminScripts']); |
| 46 |
add_action('admin_enqueue_scripts', [$this, 'dequeueElementorScripts'], 999); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Handle admin initialization tasks |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function adminInit(): void |
| 55 |
{ |
| 56 |
$this->checkVersionAndHandleActivation(); |
| 57 |
$this->removeAdminNotices(); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Enqueue admin scripts (deactivation modal and Angie MCP server for Elementor AI integration) |
| 62 |
* |
| 63 |
* @param string $hook Current admin page hook |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function adminScripts(string $hook): void |
| 67 |
{ |
| 68 |
// Deactivation modal on plugins page |
| 69 |
if ($hook === 'plugins.php') { |
| 70 |
wp_enqueue_script( |
| 71 |
'ivyforms-deactivation-modal', |
| 72 |
IVYFORMS_URL . 'frontend/assets/js/admin-deactivation-modal.js', |
| 73 |
[], |
| 74 |
IVYFORMS_VERSION, |
| 75 |
true |
| 76 |
); |
| 77 |
$settingsService = new SettingsService(); |
| 78 |
$deleteOnUninstall = $settingsService->getSetting('general', 'delete_on_uninstall'); |
| 79 |
wp_localize_script( |
| 80 |
'ivyforms-deactivation-modal', |
| 81 |
'ivyformsData', |
| 82 |
[ |
| 83 |
'version' => IVYFORMS_VERSION, |
| 84 |
'pluginUrl' => IVYFORMS_URL, |
| 85 |
'restApiUrl' => rest_url('ivyforms/v1/deactivation-feedback'), |
| 86 |
'restNonce' => wp_create_nonce('wp_rest'), |
| 87 |
'deleteOnUninstall' => $deleteOnUninstall, |
| 88 |
'i18n' => [ |
| 89 |
'warningTitle' => __('Warning', 'ivyforms'), |
| 90 |
'warningMessage' => __( |
| 91 |
'All IvyForms database tables and settings will be deleted when ' |
| 92 |
. 'this plugin is deactivated. To prevent this,', |
| 93 |
'ivyforms' |
| 94 |
), |
| 95 |
'warningLinkText' => __('disable this option in settings', 'ivyforms'), |
| 96 |
] |
| 97 |
] |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
// Angie MCP server for Elementor AI integration |
| 102 |
if (function_exists('is_plugin_active') && is_plugin_active('angie/angie.php')) { |
| 103 |
$wpVersion = get_bloginfo('version'); |
| 104 |
if (version_compare($wpVersion, '6.5', '<')) { |
| 105 |
return; |
| 106 |
} |
| 107 |
$mcpServerPath = IVYFORMS_PATH . '/frontend/dist/ivyforms-angie.js'; |
| 108 |
if (!file_exists($mcpServerPath)) { |
| 109 |
return; |
| 110 |
} |
| 111 |
// @phpstan-ignore-next-line |
| 112 |
wp_enqueue_script_module( |
| 113 |
'ivyforms-angie-mcp', |
| 114 |
IVYFORMS_URL . 'frontend/dist/ivyforms-angie.js', |
| 115 |
array(), |
| 116 |
IVYFORMS_VERSION |
| 117 |
); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Check a plugin version and handle activation/deactivation if needed |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
private function checkVersionAndHandleActivation(): void |
| 127 |
{ |
| 128 |
if (!function_exists('activate_plugin')) { |
| 129 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 130 |
} |
| 131 |
|
| 132 |
$settingsService = new SettingsService(); |
| 133 |
|
| 134 |
$currentVersion = IVYFORMS_VERSION; |
| 135 |
$savedVersion = $settingsService->getSetting('general', 'version'); |
| 136 |
|
| 137 |
if ($savedVersion !== $currentVersion) { |
| 138 |
deactivate_plugins(IVYFORMS_PLUGIN_SLUG); |
| 139 |
activate_plugin(IVYFORMS_PLUGIN_SLUG); |
| 140 |
|
| 141 |
$settingsService->setSetting('general', 'version', $currentVersion); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Remove admin notices on IvyForms admin pages |
| 147 |
* |
| 148 |
* @return void |
| 149 |
*/ |
| 150 |
public function removeAdminNotices(): void |
| 151 |
{ |
| 152 |
// Check if we're on an IvyForms admin page |
| 153 |
if ($this->isIvyFormsAdminPage()) { |
| 154 |
// Remove all admin notices |
| 155 |
remove_all_actions('admin_notices'); |
| 156 |
remove_all_actions('all_admin_notices'); |
| 157 |
// For plugins using user_admin_notices or network_admin_notices |
| 158 |
remove_all_actions('user_admin_notices'); |
| 159 |
remove_all_actions('network_admin_notices'); |
| 160 |
wp_register_style( |
| 161 |
'ivyforms_admin_global', |
| 162 |
IVYFORMS_URL . 'frontend/assets/css/global-admin.css', |
| 163 |
array(), |
| 164 |
IVYFORMS_VERSION |
| 165 |
); |
| 166 |
wp_enqueue_style('ivyforms_admin_global'); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Check if current page is an IvyForms admin page |
| 172 |
* |
| 173 |
* @SuppressWarnings(PHPMD) |
| 174 |
* |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
private function isIvyFormsAdminPage(): bool |
| 178 |
{ |
| 179 |
if (!is_admin()) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
// Adjust this based on your page's actual slug/identifier |
| 183 |
return (isset($_GET['page']) && strpos($_GET['page'], 'ivyforms') !== false); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Add admin body class |
| 188 |
* |
| 189 |
* @param string $classes |
| 190 |
* @return string |
| 191 |
*/ |
| 192 |
public function addAdminBodyClass(string $classes): string |
| 193 |
{ |
| 194 |
if ($this->settingsService === null) { |
| 195 |
return $classes; |
| 196 |
} |
| 197 |
|
| 198 |
if ($this->settingsService->getSetting('general', 'fullscreen')) { |
| 199 |
$classes .= ' ivyforms-fullscreen-mode '; |
| 200 |
} |
| 201 |
|
| 202 |
return $classes; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Dequeue Elementor admin scripts on IvyForms pages |
| 207 |
* Prevents Elementor keyboard shortcuts (Ctrl+E for Finder) from conflicting with IvyForms |
| 208 |
* |
| 209 |
* @return void |
| 210 |
*/ |
| 211 |
public function dequeueElementorScripts(): void |
| 212 |
{ |
| 213 |
// Only dequeue on IvyForms admin pages |
| 214 |
if (!$this->isIvyFormsAdminPage()) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
// Check if Elementor is active |
| 219 |
if (!defined('ELEMENTOR_VERSION')) { |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
// Dequeue Elementor's common admin scripts that include keyboard shortcuts |
| 224 |
wp_dequeue_script('elementor-common'); |
| 225 |
wp_dequeue_script('elementor-admin-app'); |
| 226 |
wp_dequeue_script('elementor-admin'); |
| 227 |
|
| 228 |
// Also deregister to prevent other plugins from loading them |
| 229 |
wp_deregister_script('elementor-common'); |
| 230 |
wp_deregister_script('elementor-admin-app'); |
| 231 |
wp_deregister_script('elementor-admin'); |
| 232 |
} |
| 233 |
} |
| 234 |
|