PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / Plugin / Plugin.php

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

293 lines 8.0 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 Exception;
18 use IvyForms\Common\Exceptions\InvalidArgumentException;
19 use IvyForms\Common\Exceptions\ValidationException;
20 use IvyForms\Routes\Routes;
21 use IvyForms\Services\InstallActions\ActivationDatabaseHook;
22 use IvyForms\Services\InstallActions\ActivationMultisite;
23 use IvyForms\Services\InstallActions\ActivationNewSiteMultisite;
24 use IvyForms\Services\InstallActions\ActivationSettingsHook;
25 use IvyForms\Services\InstallActions\DeleteDatabaseHook;
26 use IvyForms\Services\InstallActions\DeletionMultisite;
27 use IvyForms\Services\Integrations\Gutenberg\Blocks\GutenbergBlock;
28 use IvyForms\Services\Integrations\IntegrationService;
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 use IvyForms\Vendor\DI\Container;
34 use IvyForms\Vendor\DI\ContainerBuilder;
35 use IvyForms\Vendor\DI\DependencyException;
36 use IvyForms\Vendor\DI\NotFoundException;
37
38 /**
39 * Plugin
40 */
41 class Plugin
42 {
43 /**
44 * Instance
45 */
46 private static ?object $instance = null;
47
48 private ContainerBuilder $builder;
49 /**
50 * Container
51 */
52 public ?Container $container = null;
53
54 /**
55 * Settings Service
56 */
57 private ?SettingsService $settingsService = null;
58
59 /**
60 * Load plugin
61 *
62 * @return object initialized object of class.
63 * @throws Exception
64 */
65 public static function getInstance(): ?object
66 {
67 if (null === self::$instance) {
68 self::$instance = new self();
69 }
70 return self::$instance;
71 }
72
73 /**
74 * Constructor
75 *
76 * @throws Exception
77 * @since 0.1.0
78 */
79 public function __construct()
80 {
81 $this->builder = new ContainerBuilder();
82 $this->builder->addDefinitions(IVYFORMS_PATH . '/backend/src/Config/repository.php');
83 $this->builder->addDefinitions(IVYFORMS_PATH . '/backend/src/Config/services.php');
84
85 /**
86 * Allow to hook into container before build
87 * @since 0.1.0
88 *
89 * Arguments:
90 * - ContainerBuilder $builder The IvyForms DI container builder (base)
91 */
92 do_action('ivyforms/boot/extend_container_builder', $this->builder);
93
94 $this->container = $this->builder->build();
95
96 $this->settingsService = $this->container->get(SettingsService::class);
97
98 /**
99 * The code that runs during plugin activation
100 */
101 register_activation_hook(
102 IVYFORMS_FILE,
103 [ self::class, 'activation' ]
104 );
105 register_activation_hook(
106 IVYFORMS_FILE,
107 [ self::class, 'activationSettings' ]
108 );
109
110 /**
111 * The code that runs during plugin deactivation
112 */
113 register_deactivation_hook(
114 IVYFORMS_FILE,
115 [ self::class, 'deactivation' ]
116 );
117
118 /**
119 * The code that runs when plugin is deleted
120 */
121 register_uninstall_hook(
122 IVYFORMS_FILE,
123 [ self::class, 'deletion']
124 );
125
126 /** Init load */
127 add_action('plugins_loaded', [ $this, 'init' ]);
128
129 /** Load translations at init action (WordPress 6.7+ requirement) */
130 add_action('init', [ $this, 'loadTranslations' ]);
131
132 /** Register API calls */
133 add_action('rest_api_init', [$this, 'registerRoutes']);
134
135 /** Isolate API calls */
136 add_action('wp_ajax_ivyforms_api', [ $this, 'registerRoutes' ]);
137 add_action('wp_ajax_nopriv_ivyforms_api', [ $this, 'registerRoutes' ]);
138
139 /** Activation hook for new site on multisite setup */
140 add_action('wpmu_new_blog', [ $this, 'initNewSite' ]);
141
142 add_filter('wp_script_attributes', ['IvyForms\Services\Shortcode\ShortcodeService', 'addScriptAttribute']);
143
144 // Register admin hooks via AdminHooks class
145 $adminHooks = new AdminHooks($this->settingsService);
146 $adminHooks->register();
147 }
148
149 /**
150 * @throws DependencyException
151 * @throws NotFoundException
152 */
153 public function registerRoutes(): void
154 {
155 // Register REST API routes after the container is built
156 Routes::registerRoutes($this->container);
157 }
158
159
160 /**
161 * @param bool $networkWide
162 * @throws InvalidArgumentException
163 */
164 public static function activation(bool $networkWide): void
165 {
166 //Network activation
167 if ($networkWide && function_exists('is_multisite') && is_multisite()) {
168 ActivationMultisite::init();
169 }
170
171 ActivationDatabaseHook::init();
172 }
173
174 /**
175 * @param int $siteId
176 * @throws InvalidArgumentException
177 */
178 public static function initNewSite(int $siteId): void
179 {
180 ActivationNewSiteMultisite::init($siteId);
181 }
182
183 /**
184 * @throws Exception
185 */
186 public static function activationSettings(): void
187 {
188 ActivationSettingsHook::init();
189 }
190
191 /**
192 * Deactivation function when plugin is deactivated
193 * Deletes all IvyForms data if delete_on_uninstall is enabled
194 * @throws InvalidArgumentException
195 */
196 public static function deactivation(): void
197 {
198 $settingsService = new SettingsService();
199
200 if ($settingsService->getSetting('general', 'delete_on_uninstall')) {
201 //Network deletion
202 if (
203 function_exists('is_multisite') &&
204 is_multisite()
205 ) {
206 DeletionMultisite::delete();
207 }
208
209 DeleteDatabaseHook::delete();
210 }
211 }
212
213 /**
214 * Uninstall function when plugin is deleted
215 * @throws InvalidArgumentException
216 */
217 public static function deletion(): void
218 {
219 $settingsService = new SettingsService();
220
221 if ($settingsService->getSetting('general', 'delete_on_uninstall')) {
222 //Network deletion
223 if (
224 function_exists('is_multisite') &&
225 is_multisite()
226 ) {
227 DeletionMultisite::delete();
228 }
229
230 DeleteDatabaseHook::delete();
231 }
232 }
233
234 /**
235 * Init
236 * @return void
237 * @throws ValidationException|InvalidArgumentException
238 */
239 public function init(): void
240 {
241
242 new PreviewService();
243
244 // Initialize integration registry system
245 try {
246 /** @var IntegrationService $integrationService */
247 $integrationService = $this->container->get(IntegrationService::class);
248 $integrationService->init();
249 } catch (DependencyException |NotFoundException $e) {
250 if (defined('WP_DEBUG') && WP_DEBUG) {
251 error_log('Integration service error: ' . $e->getMessage());
252 }
253 }
254
255 try {
256 /** @var MenuManager $menuManager*/
257 $menuManager = $this->container->get(MenuManager::class);
258 // Register the admin menus and submenus using the 'admin_menu' hook
259 $menuManager->registerMenus();
260 } catch (DependencyException |NotFoundException $e) {
261 if (defined('WP_DEBUG') && WP_DEBUG) {
262 error_log('Menu error: ' . $e->getMessage());
263 }
264 }
265
266 if (!is_admin()) {
267 add_shortcode('ivyforms', function ($atts = []) {
268 return ShortcodeService::shortcodeHandler($atts);
269 });
270 }
271
272 // Register Gutenberg block
273 if (function_exists('register_block_type')) {
274 add_action('init', [GutenbergBlock::class, 'register']);
275 }
276 }
277
278 /**
279 * Load plugin translations
280 * Called on 'init' action to comply with WordPress 6.7+ requirements
281 *
282 * @return void
283 */
284 public function loadTranslations(): void
285 {
286 load_plugin_textdomain(
287 'ivyforms',
288 false,
289 dirname(plugin_basename(IVYFORMS_FILE)) . '/languages/' . get_locale() . '/'
290 );
291 }
292 }
293