PluginProbe
The Innovative Form Builder – IvyForms / 0.5
The Innovative Form Builder – IvyForms v0.5
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.5, at backend/src/Plugin/Plugin.php

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