| 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 |
// phpcs:disable PSR1.Files.SideEffects |
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; // Exit if accessed directly |
| 15 |
} |
| 16 |
|
| 17 |
use IvyForms\Services\Settings\SettingsService; |
| 18 |
|
| 19 |
/** |
| 20 |
* AdminHooks |
| 21 |
* |
| 22 |
* Handles all admin-related hooks and functionality |
| 23 |
*/ |
| 24 |
class AdminHooks |
| 25 |
{ |
| 26 |
/** |
| 27 |
* Settings Service |
| 28 |
*/ |
| 29 |
private ?SettingsService $settingsService = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor |
| 33 |
* |
| 34 |
* @param SettingsService|null $settingsService |
| 35 |
*/ |
| 36 |
public function __construct(?SettingsService $settingsService = null) |
| 37 |
{ |
| 38 |
$this->settingsService = $settingsService; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Register admin hooks |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function register(): void |
| 47 |
{ |
| 48 |
add_action('admin_init', [$this, 'adminInit']); |
| 49 |
add_filter('admin_body_class', [$this, 'addAdminBodyClass']); |
| 50 |
add_action('admin_enqueue_scripts', [$this, 'enqueueDeactivationModal']); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Handle admin initialization tasks |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function adminInit(): void |
| 59 |
{ |
| 60 |
$this->checkVersionAndHandleActivation(); |
| 61 |
$this->removeAdminNotices(); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Enqueue deactivation modal script on plugins page |
| 66 |
* |
| 67 |
* @param string $hook Current admin page hook |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function enqueueDeactivationModal(string $hook): void |
| 71 |
{ |
| 72 |
if ($hook === 'plugins.php') { |
| 73 |
wp_enqueue_script( |
| 74 |
'ivyforms-deactivation-modal', |
| 75 |
IVYFORMS_URL . 'frontend/assets/js/admin-deactivation-modal.js', |
| 76 |
[], |
| 77 |
IVYFORMS_VERSION, |
| 78 |
true |
| 79 |
); |
| 80 |
|
| 81 |
$settingsService = new SettingsService(); |
| 82 |
$deleteOnUninstall = $settingsService->getSetting('general', 'delete_on_uninstall'); |
| 83 |
|
| 84 |
wp_localize_script( |
| 85 |
'ivyforms-deactivation-modal', |
| 86 |
'ivyformsData', |
| 87 |
[ |
| 88 |
'version' => IVYFORMS_VERSION, |
| 89 |
'pluginUrl' => IVYFORMS_URL, |
| 90 |
'restApiUrl' => rest_url('ivyforms/v1/deactivation-feedback'), |
| 91 |
'restNonce' => wp_create_nonce('wp_rest'), |
| 92 |
'deleteOnUninstall' => $deleteOnUninstall, |
| 93 |
'i18n' => [ |
| 94 |
'warningTitle' => __('Warning', 'ivyforms'), |
| 95 |
'warningMessage' => __( |
| 96 |
'All IvyForms database tables and settings will be deleted when ' |
| 97 |
. 'this plugin is deactivated. To prevent this,', |
| 98 |
'ivyforms' |
| 99 |
), |
| 100 |
'warningLinkText' => __('disable this option in settings', 'ivyforms'), |
| 101 |
] |
| 102 |
] |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Check a plugin version and handle activation/deactivation if needed |
| 109 |
* |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
private function checkVersionAndHandleActivation(): void |
| 113 |
{ |
| 114 |
if (!function_exists('activate_plugin')) { |
| 115 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 116 |
} |
| 117 |
|
| 118 |
$settingsService = new SettingsService(); |
| 119 |
|
| 120 |
$currentVersion = IVYFORMS_VERSION; |
| 121 |
$savedVersion = $settingsService->getSetting('general', 'version'); |
| 122 |
|
| 123 |
if ($savedVersion !== $currentVersion) { |
| 124 |
deactivate_plugins(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type |
| 125 |
activate_plugin(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type |
| 126 |
|
| 127 |
$settingsService->setSetting('general', 'version', $currentVersion); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Remove admin notices on IvyForms admin pages |
| 133 |
* |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
public function removeAdminNotices(): void |
| 137 |
{ |
| 138 |
// Check if we're on an IvyForms admin page |
| 139 |
if ($this->isIvyFormsAdminPage()) { |
| 140 |
// Remove all admin notices |
| 141 |
remove_all_actions('admin_notices'); |
| 142 |
remove_all_actions('all_admin_notices'); |
| 143 |
// For plugins using user_admin_notices or network_admin_notices |
| 144 |
remove_all_actions('user_admin_notices'); |
| 145 |
remove_all_actions('network_admin_notices'); |
| 146 |
wp_register_style( |
| 147 |
'ivyforms_admin_global', |
| 148 |
IVYFORMS_URL . 'frontend/assets/css/global-admin.css', |
| 149 |
array(), |
| 150 |
IVYFORMS_VERSION |
| 151 |
); |
| 152 |
wp_enqueue_style('ivyforms_admin_global'); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Check if current page is an IvyForms admin page |
| 158 |
* |
| 159 |
* @SuppressWarnings(PHPMD) |
| 160 |
* |
| 161 |
* @return bool |
| 162 |
*/ |
| 163 |
private function isIvyFormsAdminPage(): bool |
| 164 |
{ |
| 165 |
if (!is_admin()) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
// Adjust this based on your page's actual slug/identifier |
| 169 |
return (isset($_GET['page']) && strpos($_GET['page'], 'ivyforms') !== false); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Add admin body class |
| 174 |
* |
| 175 |
* @param string $classes |
| 176 |
* @return string |
| 177 |
*/ |
| 178 |
public function addAdminBodyClass(string $classes): string |
| 179 |
{ |
| 180 |
if ($this->settingsService === null) { |
| 181 |
return $classes; |
| 182 |
} |
| 183 |
|
| 184 |
if ($this->settingsService->getSetting('general', 'fullscreen')) { |
| 185 |
$classes .= ' ivyforms-fullscreen-mode '; |
| 186 |
} |
| 187 |
|
| 188 |
return $classes; |
| 189 |
} |
| 190 |
} |
| 191 |
|