PluginProbe
The Innovative Form Builder – IvyForms / 0.2.1
The Innovative Form Builder – IvyForms v0.2.1
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 / load.php

load.php in The Innovative Form Builder – IvyForms 0.2.1, at backend/src/load.php

266 lines 6.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\Load;
11
12 // phpcs:disable PSR1.Files.SideEffects
13 if (!defined('ABSPATH')) {
14 exit; // Exit if accessed directly
15 }
16
17 use DI\Container;
18 use DI\DependencyException;
19 use 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 * Load plugin
51 *
52 * @return object initialized object of class.
53 * @throws Exception
54 */
55 public static function getInstance(Container $container): ?object
56 {
57 if (null === self::$instance) {
58 self::$instance = new self($container);
59 }
60 return self::$instance;
61 }
62
63 /**
64 * Constructor
65 *
66 * @throws Exception
67 * @since 0.0.1
68 */
69 public function __construct(Container $container)
70 {
71 $this->container = $container;
72
73 /**
74 * The code that runs during plugin activation
75 */
76 register_activation_hook(
77 IVYFORMS_FILE,
78 [ self::class, 'activation' ]
79 );
80 register_activation_hook(
81 IVYFORMS_FILE,
82 [ self::class, 'activationSettings' ]
83 );
84
85 /**
86 * The code that runs during plugin deactivation
87 */
88 register_uninstall_hook(
89 IVYFORMS_FILE,
90 [ self::class, 'deletion']
91 );
92
93 /** Init load */
94 add_action('plugins_loaded', [ $this, 'init' ]);
95
96 /** Register API calls */
97 add_action('rest_api_init', [$this, 'registerRoutes']);
98
99 /** Isolate API calls */
100 add_action('wp_ajax_ivyforms_api', [ $this, 'registerRoutes' ]);
101 add_action('wp_ajax_nopriv_ivyforms_api', [ $this, 'registerRoutes' ]);
102
103 /** Activation hook for new site on multisite setup */
104 add_action('wpmu_new_blog', [ $this, 'initNewSite' ]);
105
106 add_filter('wp_script_attributes', ['IvyForms\Services\Shortcode\ShortcodeService', 'addScriptAttribute']);
107
108 add_action('admin_init', [$this, 'adminInit']);
109 }
110
111 /**
112 * Handle admin initialization tasks
113 *
114 * @return void
115 */
116 public function adminInit(): void
117 {
118 $this->checkVersionAndHandleActivation();
119
120 $this->removeAdminNotices();
121 }
122
123 /**
124 * Check a plugin version and handle activation/deactivation if needed
125 *
126 * @return void
127 */
128 private function checkVersionAndHandleActivation(): void
129 {
130 if (!function_exists('activate_plugin')) {
131 // @phpstan-ignore requireOnce.fileNotFound
132 require_once ABSPATH . 'wp-admin/includes/plugin.php';
133 }
134 $settingsService = new SettingsService();
135
136 $currentVersion = IVYFORMS_VERSION;
137 $savedVersion = $settingsService->getSetting('general', 'version');
138
139 if ($savedVersion !== $currentVersion) {
140 deactivate_plugins(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type
141 activate_plugin(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type
142
143 $settingsService->setSetting('general', 'version', $currentVersion);
144 }
145 }
146
147 /**
148 * @throws DependencyException
149 * @throws NotFoundException
150 */
151 public function registerRoutes(): void
152 {
153 // Register REST API routes after the container is built
154 Routes::registerRoutes($this->container);
155 }
156
157 /**
158 * Remove admin notices on IvyForms admin pages
159 */
160 public function removeAdminNotices(): void
161 {
162 // Check if we're on an IvyForms admin page
163 if ($this->isIvyFormsAdminPage()) {
164 // Remove all admin notices
165 remove_all_actions('admin_notices');
166 remove_all_actions('all_admin_notices');
167 // For plugins using user_admin_notices or network_admin_notices
168 remove_all_actions('user_admin_notices');
169 remove_all_actions('network_admin_notices');
170 }
171 }
172
173 /**
174 * Check if current page is an IvyForms admin page
175 *
176 * @SuppressWarnings(PHPMD)
177 *
178 * @return bool
179 */
180 private function isIvyFormsAdminPage(): bool
181 {
182 if (!is_admin()) {
183 return false;
184 }
185 // Adjust this based on your page's actual slug/identifier
186 return (isset($_GET['page']) && strpos($_GET['page'], 'ivyforms') !== false);
187 }
188
189 /**
190 * @param bool $networkWide
191 */
192 public static function activation(bool $networkWide): void
193 {
194 //Network activation
195 if ($networkWide && function_exists('is_multisite') && is_multisite()) {
196 ActivationMultisite::init();
197 }
198
199 ActivationDatabaseHook::init();
200 }
201
202 /**
203 * @param int $siteId
204 */
205 public static function initNewSite(int $siteId): void
206 {
207 ActivationNewSiteMultisite::init($siteId);
208 }
209
210 /**
211 * @throws Exception
212 */
213 public static function activationSettings(): void
214 {
215 ActivationSettingsHook::init();
216 }
217
218 /**
219 * Uninstall function when plugin is deleted
220 * @throws InvalidArgumentException
221 */
222 public static function deletion(): void
223 {
224 $settingsService = new SettingsService();
225
226 if ($settingsService->getSetting('activation', 'deleteTables')) {
227 //Network deletion
228 if (
229 function_exists('is_multisite') &&
230 is_multisite()
231 ) {
232 DeletionMultisite::delete();
233 }
234
235 DeleteDatabaseHook::delete();
236 }
237 }
238
239 /**
240 * Init
241 * @return void
242 */
243 public function init(): void
244 {
245
246 new PreviewService();
247
248 try {
249 /** @var MenuManager $menuManager*/
250 $menuManager = $this->container->get(MenuManager::class);
251 // Register the admin menus and submenus using the 'admin_menu' hook
252 $menuManager->registerMenus();
253 } catch (DependencyException |NotFoundException $e) {
254 if (defined('WP_DEBUG') && WP_DEBUG) {
255 error_log('Menu error: ' . $e->getMessage());
256 }
257 }
258
259 if (!is_admin()) {
260 add_shortcode('ivyforms', function ($atts = []) {
261 return ShortcodeService::shortcodeHandler($atts);
262 });
263 }
264 }
265 }
266