PluginProbe
The Innovative Form Builder – IvyForms / 0.6.1-backup
The Innovative Form Builder – IvyForms v0.6.1-backup
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Plugin / AdminHooks.php

AdminHooks.php in The Innovative Form Builder – IvyForms 0.6.1-backup, at backend/src/Plugin/AdminHooks.php

179 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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/src/assets/js/admin/admin-deactivation-modal.js',
76 [],
77 IVYFORMS_VERSION,
78 true
79 );
80
81 // Localize script data for deactivation modal
82 wp_localize_script(
83 'ivyforms-deactivation-modal',
84 'ivyformsData',
85 [
86 'version' => IVYFORMS_VERSION,
87 'restApiUrl' => rest_url('ivyforms/v1/deactivation-feedback'),
88 'restNonce' => wp_create_nonce('wp_rest'),
89 ]
90 );
91 }
92 }
93
94 /**
95 * Check a plugin version and handle activation/deactivation if needed
96 *
97 * @return void
98 */
99 private function checkVersionAndHandleActivation(): void
100 {
101 if (!function_exists('activate_plugin')) {
102 // @phpstan-ignore requireOnce.fileNotFound
103 require_once ABSPATH . 'wp-admin/includes/plugin.php';
104 }
105
106 $settingsService = new SettingsService();
107
108 $currentVersion = IVYFORMS_VERSION;
109 $savedVersion = $settingsService->getSetting('general', 'version');
110
111 if ($savedVersion !== $currentVersion) {
112 deactivate_plugins(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type
113 activate_plugin(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type
114
115 $settingsService->setSetting('general', 'version', $currentVersion);
116 }
117 }
118
119 /**
120 * Remove admin notices on IvyForms admin pages
121 *
122 * @return void
123 */
124 public function removeAdminNotices(): void
125 {
126 // Check if we're on an IvyForms admin page
127 if ($this->isIvyFormsAdminPage()) {
128 // Remove all admin notices
129 remove_all_actions('admin_notices');
130 remove_all_actions('all_admin_notices');
131 // For plugins using user_admin_notices or network_admin_notices
132 remove_all_actions('user_admin_notices');
133 remove_all_actions('network_admin_notices');
134 wp_register_style(
135 'ivyforms_admin_global',
136 IVYFORMS_URL . 'frontend/css/global-admin.css',
137 array(),
138 IVYFORMS_VERSION
139 );
140 wp_enqueue_style('ivyforms_admin_global');
141 }
142 }
143
144 /**
145 * Check if current page is an IvyForms admin page
146 *
147 * @SuppressWarnings(PHPMD)
148 *
149 * @return bool
150 */
151 private function isIvyFormsAdminPage(): bool
152 {
153 if (!is_admin()) {
154 return false;
155 }
156 // Adjust this based on your page's actual slug/identifier
157 return (isset($_GET['page']) && strpos($_GET['page'], 'ivyforms') !== false);
158 }
159
160 /**
161 * Add admin body class
162 *
163 * @param string $classes
164 * @return string
165 */
166 public function addAdminBodyClass(string $classes): string
167 {
168 if ($this->settingsService === null) {
169 return $classes;
170 }
171
172 if ($this->settingsService->getSetting('general', 'fullscreen')) {
173 $classes .= ' ivyforms-fullscreen-mode ';
174 }
175
176 return $classes;
177 }
178 }
179