PluginProbe
The Innovative Form Builder – IvyForms / 0.4
The Innovative Form Builder – IvyForms v0.4
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 / Plugin.php

Plugin.php in The Innovative Form Builder – IvyForms 0.4, at backend/src/Plugin/Plugin.php

299 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Loader.
5 *
6 * @package IvyForms
7 * @since 0.0.1
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\Vendor\DI\Container;
18 use IvyForms\Vendor\DI\DependencyException;
19 use IvyForms\Vendor\DI\NotFoundException;
20 use Exception;
21 use IvyForms\Common\Exceptions\InvalidArgumentException;
22 use IvyForms\Routes\Routes;
23 use IvyForms\Services\InstallActions\ActivationDatabaseHook;
24 use IvyForms\Services\InstallActions\ActivationMultisite;
25 use IvyForms\Services\InstallActions\ActivationNewSiteMultisite;
26 use IvyForms\Services\InstallActions\ActivationSettingsHook;
27 use IvyForms\Services\InstallActions\DeleteDatabaseHook;
28 use IvyForms\Services\InstallActions\DeletionMultisite;
29 use IvyForms\Services\Menu\MenuManager;
30 use IvyForms\Services\Preview\PreviewService;
31 use IvyForms\Services\Settings\SettingsService;
32 use IvyForms\Services\Shortcode\ShortcodeService;
33
34 /**
35 * Plugin
36 */
37 class Plugin
38 {
39 /**
40 * Instance
41 */
42 private static ?object $instance = null;
43
44 /**
45 * Container
46 */
47 public ?Container $container = null;
48
49 /**
50 * Settings Service
51 */
52 private ?SettingsService $settingsService = null;
53
54 /**
55 * Load plugin
56 *
57 * @return object initialized object of class.
58 * @throws Exception
59 */
60 public static function getInstance(Container $container): ?object
61 {
62 if (null === self::$instance) {
63 self::$instance = new self($container);
64 }
65 return self::$instance;
66 }
67
68 /**
69 * Constructor
70 *
71 * @throws Exception
72 * @since 0.0.1
73 */
74 public function __construct(Container $container)
75 {
76 $this->container = $container;
77 $this->settingsService = $this->container->get(SettingsService::class);
78
79 /**
80 * The code that runs during plugin activation
81 */
82 register_activation_hook(
83 IVYFORMS_FILE,
84 [ self::class, 'activation' ]
85 );
86 register_activation_hook(
87 IVYFORMS_FILE,
88 [ self::class, 'activationSettings' ]
89 );
90
91 /**
92 * The code that runs during plugin deactivation
93 */
94 register_uninstall_hook(
95 IVYFORMS_FILE,
96 [ self::class, 'deletion']
97 );
98
99 /** Init load */
100 add_action('plugins_loaded', [ $this, 'init' ]);
101
102 /** Register API calls */
103 add_action('rest_api_init', [$this, 'registerRoutes']);
104
105 /** Isolate API calls */
106 add_action('wp_ajax_ivyforms_api', [ $this, 'registerRoutes' ]);
107 add_action('wp_ajax_nopriv_ivyforms_api', [ $this, 'registerRoutes' ]);
108
109 /** Activation hook for new site on multisite setup */
110 add_action('wpmu_new_blog', [ $this, 'initNewSite' ]);
111
112 add_filter('wp_script_attributes', ['IvyForms\Services\Shortcode\ShortcodeService', 'addScriptAttribute']);
113
114 add_action('admin_init', [$this, 'adminInit']);
115 add_filter('admin_body_class', [$this, 'addAdminBodyClass']);
116 }
117
118 /**
119 * Handle admin initialization tasks
120 *
121 * @return void
122 */
123 public function adminInit(): void
124 {
125 $this->checkVersionAndHandleActivation();
126
127 $this->removeAdminNotices();
128 }
129
130 /**
131 * Check a plugin version and handle activation/deactivation if needed
132 *
133 * @return void
134 */
135 private function checkVersionAndHandleActivation(): void
136 {
137 if (!function_exists('activate_plugin')) {
138 // @phpstan-ignore requireOnce.fileNotFound
139 require_once ABSPATH . 'wp-admin/includes/plugin.php';
140 }
141
142 $settingsService = new SettingsService();
143
144 $currentVersion = IVYFORMS_VERSION;
145 $savedVersion = $settingsService->getSetting('general', 'version');
146
147 if ($savedVersion !== $currentVersion) {
148 deactivate_plugins(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type
149 activate_plugin(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type
150
151 $settingsService->setSetting('general', 'version', $currentVersion);
152 }
153 }
154
155 /**
156 * @throws DependencyException
157 * @throws NotFoundException
158 */
159 public function registerRoutes(): void
160 {
161 // Register REST API routes after the container is built
162 Routes::registerRoutes($this->container);
163 }
164
165 /**
166 * Remove admin notices on IvyForms admin pages
167 */
168 public function removeAdminNotices(): void
169 {
170 // Check if we're on an IvyForms admin page
171 if ($this->isIvyFormsAdminPage()) {
172 // Remove all admin notices
173 remove_all_actions('admin_notices');
174 remove_all_actions('all_admin_notices');
175 // For plugins using user_admin_notices or network_admin_notices
176 remove_all_actions('user_admin_notices');
177 remove_all_actions('network_admin_notices');
178 wp_register_style(
179 'ivyforms_admin_global',
180 IVYFORMS_URL . 'frontend/css/global-admin.css',
181 array(),
182 IVYFORMS_VERSION
183 );
184 wp_enqueue_style('ivyforms_admin_global');
185 }
186 }
187
188 /**
189 * Check if current page is an IvyForms admin page
190 *
191 * @SuppressWarnings(PHPMD)
192 *
193 * @return bool
194 */
195 private function isIvyFormsAdminPage(): bool
196 {
197 if (!is_admin()) {
198 return false;
199 }
200 // Adjust this based on your page's actual slug/identifier
201 return (isset($_GET['page']) && strpos($_GET['page'], 'ivyforms') !== false);
202 }
203
204 /**
205 * @param bool $networkWide
206 */
207 public static function activation(bool $networkWide): void
208 {
209 //Network activation
210 if ($networkWide && function_exists('is_multisite') && is_multisite()) {
211 ActivationMultisite::init();
212 }
213
214 ActivationDatabaseHook::init();
215 }
216
217 /**
218 * @param int $siteId
219 */
220 public static function initNewSite(int $siteId): void
221 {
222 ActivationNewSiteMultisite::init($siteId);
223 }
224
225 /**
226 * @throws Exception
227 */
228 public static function activationSettings(): void
229 {
230 ActivationSettingsHook::init();
231 }
232
233 /**
234 * Uninstall function when plugin is deleted
235 * @throws InvalidArgumentException
236 */
237 public static function deletion(): void
238 {
239 $settingsService = new SettingsService();
240
241 if ($settingsService->getSetting('activation', 'deleteTables')) {
242 //Network deletion
243 if (
244 function_exists('is_multisite') &&
245 is_multisite()
246 ) {
247 DeletionMultisite::delete();
248 }
249
250 DeleteDatabaseHook::delete();
251 }
252 }
253
254 /**
255 * Init
256 * @return void
257 */
258 public function init(): void
259 {
260
261 new PreviewService();
262
263 try {
264 /** @var MenuManager $menuManager*/
265 $menuManager = $this->container->get(MenuManager::class);
266 // Register the admin menus and submenus using the 'admin_menu' hook
267 $menuManager->registerMenus();
268 } catch (DependencyException |NotFoundException $e) {
269 if (defined('WP_DEBUG') && WP_DEBUG) {
270 error_log('Menu error: ' . $e->getMessage());
271 }
272 }
273
274 if (!is_admin()) {
275 add_shortcode('ivyforms', function ($atts = []) {
276 return ShortcodeService::shortcodeHandler($atts);
277 });
278 }
279 }
280 /**
281 * Add admin body class
282 *
283 * @param string $classes
284 * @return string
285 */
286 public function addAdminBodyClass(string $classes): string
287 {
288 if ($this->settingsService === null) {
289 return $classes;
290 }
291
292 if ($this->settingsService->getSetting('general', 'fullscreen')) {
293 $classes .= ' ivyforms-fullscreen-mode ';
294 }
295
296 return $classes;
297 }
298 }
299