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

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