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

233 lines 5.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
72 $this->container = $container;
73
74 /**
75 * The code that runs during plugin activation
76 */
77 register_activation_hook(
78 IVYFORMS_FILE,
79 [ self::class, 'activation' ]
80 );
81 register_activation_hook(
82 IVYFORMS_FILE,
83 [ self::class, 'activationSettings' ]
84 );
85
86 /**
87 * The code that runs during plugin deactivation
88 */
89 register_uninstall_hook(
90 IVYFORMS_FILE,
91 [ self::class, 'deletion']
92 );
93
94 /** Init load */
95 add_action('plugins_loaded', [ $this, 'init' ]);
96
97 /** Register API calls */
98 add_action('rest_api_init', [$this, 'registerRoutes']);
99
100 /** Isolate API calls */
101 add_action('wp_ajax_ivyforms_api', [ $this, 'registerRoutes' ]);
102 add_action('wp_ajax_nopriv_ivyforms_api', [ $this, 'registerRoutes' ]);
103
104 /** Activation hook for new site on multisite setup */
105 add_action('wpmu_new_blog', [ $this, 'initNewSite' ]);
106
107 add_filter('wp_script_attributes', ['IvyForms\Services\Shortcode\ShortcodeService', 'addScriptAttribute']);
108
109 // Remove all admin notices from IvyForms admin pages
110 add_action('admin_init', [$this, 'removeAdminNotices']);
111 }
112
113
114 /**
115 * @throws DependencyException
116 * @throws NotFoundException
117 */
118 public function registerRoutes(): void
119 {
120 // Register REST API routes after the container is built
121 Routes::registerRoutes($this->container);
122 }
123
124 /**
125 * Remove admin notices on IvyForms admin pages
126 */
127 public function removeAdminNotices(): void
128 {
129 // Check if we're on an IvyForms admin page
130 if ($this->isIvyFormsAdminPage()) {
131 // Remove all admin notices
132 remove_all_actions('admin_notices');
133 remove_all_actions('all_admin_notices');
134 // For plugins using user_admin_notices or network_admin_notices
135 remove_all_actions('user_admin_notices');
136 remove_all_actions('network_admin_notices');
137 }
138 }
139
140 /**
141 * Check if current page is an IvyForms admin page
142 *
143 * @SuppressWarnings(PHPMD)
144 *
145 * @return bool
146 */
147 private function isIvyFormsAdminPage(): bool
148 {
149 if (!is_admin()) {
150 return false;
151 }
152 // Adjust this based on your page's actual slug/identifier
153 return (isset($_GET['page']) && strpos($_GET['page'], 'ivyforms') !== false);
154 }
155
156 /**
157 * @param bool $networkWide
158 */
159 public static function activation(bool $networkWide): void
160 {
161 //Network activation
162 if ($networkWide && function_exists('is_multisite') && is_multisite()) {
163 ActivationMultisite::init();
164 }
165
166 ActivationDatabaseHook::init();
167 }
168
169 /**
170 * @param int $siteId
171 */
172 public static function initNewSite(int $siteId): void
173 {
174 ActivationNewSiteMultisite::init($siteId);
175 }
176
177 /**
178 * @throws Exception
179 */
180 public static function activationSettings(): void
181 {
182 ActivationSettingsHook::init();
183 }
184
185 /**
186 * Uninstall function when plugin is deleted
187 * @throws InvalidArgumentException
188 */
189 public static function deletion(): void
190 {
191 $settingsService = new SettingsService();
192
193 if ($settingsService->getSetting('activation', 'deleteTables')) {
194 //Network deletion
195 if (
196 function_exists('is_multisite') &&
197 is_multisite()
198 ) {
199 DeletionMultisite::delete();
200 }
201
202 DeleteDatabaseHook::delete();
203 }
204 }
205
206 /**
207 * Init
208 * @return void
209 */
210 public function init(): void
211 {
212
213 new PreviewService();
214
215 try {
216 /** @var MenuManager $menuManager*/
217 $menuManager = $this->container->get(MenuManager::class);
218 // Register the admin menus and submenus using the 'admin_menu' hook
219 $menuManager->registerMenus();
220 } catch (DependencyException |NotFoundException $e) {
221 if (defined('WP_DEBUG') && WP_DEBUG) {
222 error_log('Menu error: ' . $e->getMessage());
223 }
224 }
225
226 if (!is_admin()) {
227 add_shortcode('ivyforms', function ($atts = []) {
228 return ShortcodeService::shortcodeHandler($atts);
229 });
230 }
231 }
232 }
233