PluginProbe
The Innovative Form Builder – IvyForms / 0.3.1
The Innovative Form Builder – IvyForms v0.3.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.3.1, at backend/src/load.php

298 lines 7.8 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 * 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 = new SettingsService();
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 $settingsService = new SettingsService();
142
143 $currentVersion = IVYFORMS_VERSION;
144 $savedVersion = $settingsService->getSetting('general', 'version');
145
146 if ($savedVersion !== $currentVersion) {
147 deactivate_plugins(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type
148 activate_plugin(IVYFORMS_PLUGIN_SLUG); // @phpstan-ignore argument.type
149
150 $settingsService->setSetting('general', 'version', $currentVersion);
151 }
152 }
153
154 /**
155 * @throws DependencyException
156 * @throws NotFoundException
157 */
158 public function registerRoutes(): void
159 {
160 // Register REST API routes after the container is built
161 Routes::registerRoutes($this->container);
162 }
163
164 /**
165 * Remove admin notices on IvyForms admin pages
166 */
167 public function removeAdminNotices(): void
168 {
169 // Check if we're on an IvyForms admin page
170 if ($this->isIvyFormsAdminPage()) {
171 // Remove all admin notices
172 remove_all_actions('admin_notices');
173 remove_all_actions('all_admin_notices');
174 // For plugins using user_admin_notices or network_admin_notices
175 remove_all_actions('user_admin_notices');
176 remove_all_actions('network_admin_notices');
177 wp_register_style(
178 'ivyforms_admin_global',
179 IVYFORMS_URL . 'frontend/css/global-admin.css',
180 array(),
181 IVYFORMS_VERSION
182 );
183 wp_enqueue_style('ivyforms_admin_global');
184 }
185 }
186
187 /**
188 * Check if current page is an IvyForms admin page
189 *
190 * @SuppressWarnings(PHPMD)
191 *
192 * @return bool
193 */
194 private function isIvyFormsAdminPage(): bool
195 {
196 if (!is_admin()) {
197 return false;
198 }
199 // Adjust this based on your page's actual slug/identifier
200 return (isset($_GET['page']) && strpos($_GET['page'], 'ivyforms') !== false);
201 }
202
203 /**
204 * @param bool $networkWide
205 */
206 public static function activation(bool $networkWide): void
207 {
208 //Network activation
209 if ($networkWide && function_exists('is_multisite') && is_multisite()) {
210 ActivationMultisite::init();
211 }
212
213 ActivationDatabaseHook::init();
214 }
215
216 /**
217 * @param int $siteId
218 */
219 public static function initNewSite(int $siteId): void
220 {
221 ActivationNewSiteMultisite::init($siteId);
222 }
223
224 /**
225 * @throws Exception
226 */
227 public static function activationSettings(): void
228 {
229 ActivationSettingsHook::init();
230 }
231
232 /**
233 * Uninstall function when plugin is deleted
234 * @throws InvalidArgumentException
235 */
236 public static function deletion(): void
237 {
238 $settingsService = new SettingsService();
239
240 if ($settingsService->getSetting('activation', 'deleteTables')) {
241 //Network deletion
242 if (
243 function_exists('is_multisite') &&
244 is_multisite()
245 ) {
246 DeletionMultisite::delete();
247 }
248
249 DeleteDatabaseHook::delete();
250 }
251 }
252
253 /**
254 * Init
255 * @return void
256 */
257 public function init(): void
258 {
259
260 new PreviewService();
261
262 try {
263 /** @var MenuManager $menuManager*/
264 $menuManager = $this->container->get(MenuManager::class);
265 // Register the admin menus and submenus using the 'admin_menu' hook
266 $menuManager->registerMenus();
267 } catch (DependencyException |NotFoundException $e) {
268 if (defined('WP_DEBUG') && WP_DEBUG) {
269 error_log('Menu error: ' . $e->getMessage());
270 }
271 }
272
273 if (!is_admin()) {
274 add_shortcode('ivyforms', function ($atts = []) {
275 return ShortcodeService::shortcodeHandler($atts);
276 });
277 }
278 }
279 /**
280 * Add admin body class
281 *
282 * @param string $classes
283 * @return string
284 */
285 public function addAdminBodyClass(string $classes): string
286 {
287 if ($this->settingsService === null) {
288 return $classes;
289 }
290
291 if ($this->settingsService->getSetting('general', 'fullscreen')) {
292 $classes .= ' ivyforms-fullscreen-mode ';
293 }
294
295 return $classes;
296 }
297 }
298