PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.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 / Plugin / Plugin.php

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

342 lines 9.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 use Exception;
13 use IvyForms\Common\Exceptions\InvalidArgumentException;
14 use IvyForms\Common\Exceptions\ValidationException;
15 use IvyForms\Routes\Routes;
16 use IvyForms\Services\InstallActions\ActivationDatabaseHook;
17 use IvyForms\Services\InstallActions\ActivationMultisite;
18 use IvyForms\Services\InstallActions\ActivationNewSiteMultisite;
19 use IvyForms\Services\InstallActions\ActivationSettingsHook;
20 use IvyForms\Services\InstallActions\DeleteDatabaseHook;
21 use IvyForms\Services\InstallActions\DeletionMultisite;
22 use IvyForms\Services\Integrations\Elementor\IvyFormsElementorLoader;
23 use IvyForms\Services\Integrations\Gutenberg\Blocks\GutenbergBlock;
24 use IvyForms\Services\Integrations\IntegrationService;
25 use IvyForms\Services\Menu\MenuManager;
26 use IvyForms\Services\Preview\PreviewService;
27 use IvyForms\Services\Settings\SettingsService;
28 use IvyForms\Services\Shortcode\Helpers\ShortcodeHelper;
29 use IvyForms\Services\Shortcode\ShortcodeService;
30 use IvyForms\Vendor\DI\Container;
31 use IvyForms\Vendor\DI\ContainerBuilder;
32 use IvyForms\Vendor\DI\DependencyException;
33 use IvyForms\Vendor\DI\NotFoundException;
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_deactivation_hook(
111 IVYFORMS_FILE,
112 [ self::class, 'deactivation' ]
113 );
114
115 /**
116 * The code that runs when plugin is deleted
117 */
118 register_uninstall_hook(
119 IVYFORMS_FILE,
120 [ self::class, 'deletion']
121 );
122
123 /** Init load */
124 add_action('plugins_loaded', [ $this, 'init' ]);
125
126 /** Load translations at init action (WordPress 6.7+ requirement) */
127 add_action('init', [ $this, 'loadTranslations' ]);
128
129 /** Register API calls */
130 add_action('rest_api_init', [$this, 'registerRoutes']);
131
132 /** Isolate API calls */
133 add_action('wp_ajax_ivyforms_api', [ $this, 'registerRoutes' ]);
134 add_action('wp_ajax_nopriv_ivyforms_api', [ $this, 'registerRoutes' ]);
135
136 /** Activation hook for new site on multisite setup */
137 add_action('wpmu_new_blog', [ $this, 'initNewSite' ]);
138
139 add_filter('wp_script_attributes', ['IvyForms\Services\Shortcode\ShortcodeService', 'addScriptAttribute']);
140
141 // Register admin hooks via AdminHooks class
142 $adminHooks = new AdminHooks($this->settingsService);
143 $adminHooks->register();
144 }
145
146 /**
147 * @throws DependencyException
148 * @throws NotFoundException
149 */
150 public function registerRoutes(): void
151 {
152 // Register REST API routes after the container is built
153 Routes::registerRoutes($this->container);
154 }
155 /**
156 * @param bool $networkWide
157 * @throws InvalidArgumentException
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 * @throws InvalidArgumentException
172 */
173 public static function initNewSite(int $siteId): void
174 {
175 ActivationNewSiteMultisite::init($siteId);
176 }
177
178 /**
179 * @throws Exception
180 */
181 public static function activationSettings(): void
182 {
183 ActivationSettingsHook::init();
184 }
185
186 /**
187 * Deactivation function when plugin is deactivated
188 * Deletes all IvyForms data if delete_on_uninstall is enabled
189 * @throws InvalidArgumentException
190 */
191 public static function deactivation(): void
192 {
193 $settingsService = new SettingsService();
194
195 if ($settingsService->getSetting('general', 'delete_on_uninstall')) {
196 //Network deletion
197 if (
198 function_exists('is_multisite') &&
199 is_multisite()
200 ) {
201 DeletionMultisite::delete();
202 }
203
204 DeleteDatabaseHook::delete();
205 }
206 }
207
208 /**
209 * Uninstall function when plugin is deleted
210 * @throws InvalidArgumentException
211 */
212 public static function deletion(): void
213 {
214 $settingsService = new SettingsService();
215
216 if ($settingsService->getSetting('general', 'delete_on_uninstall')) {
217 //Network deletion
218 if (
219 function_exists('is_multisite') &&
220 is_multisite()
221 ) {
222 DeletionMultisite::delete();
223 }
224
225 DeleteDatabaseHook::delete();
226 }
227 }
228
229 /**
230 * Init
231 * @return void
232 * @throws ValidationException|InvalidArgumentException
233 */
234 public function init(): void
235 {
236 new PreviewService();
237 $this->initIntegrationService();
238 $this->initMenuManager();
239 $this->registerShortcode();
240 $this->registerGutenbergBlock();
241 $this->registerElementorWidget();
242 }
243
244 /**
245 * Initialize integration service
246 *
247 * @return void
248 */
249 private function initIntegrationService(): void
250 {
251 try {
252 /** @var IntegrationService $integrationService */
253 $integrationService = $this->container->get(IntegrationService::class);
254 $integrationService->init();
255 } catch (DependencyException |NotFoundException $e) {
256 if (defined('WP_DEBUG') && WP_DEBUG) {
257 error_log('Integration service error: ' . $e->getMessage());
258 }
259 }
260 }
261
262 /**
263 * Initialize menu manager
264 *
265 * @return void
266 */
267 private function initMenuManager(): void
268 {
269 try {
270 /** @var MenuManager $menuManager*/
271 $menuManager = $this->container->get(MenuManager::class);
272 // Register the admin menus and submenus using the 'admin_menu' hook
273 $menuManager->registerMenus();
274 } catch (DependencyException |NotFoundException $e) {
275 if (defined('WP_DEBUG') && WP_DEBUG) {
276 error_log('Menu error: ' . $e->getMessage());
277 }
278 }
279 }
280
281 /**
282 * Register shortcode
283 *
284 * @return void
285 */
286 private function registerShortcode(): void
287 {
288 if (is_admin()) {
289 return;
290 }
291
292 add_action('wp_enqueue_scripts', [ShortcodeHelper::class, 'registerFrontendStyleHandles'], 1);
293
294 add_shortcode('ivyforms', function ($atts = []) {
295 return ShortcodeService::shortcodeHandler($atts);
296 });
297 }
298
299 /**
300 * Register Gutenberg block
301 *
302 * @return void
303 */
304 private function registerGutenbergBlock(): void
305 {
306 if (!function_exists('register_block_type')) {
307 return;
308 }
309
310 add_action('init', [GutenbergBlock::class, 'register']);
311 }
312
313 /**
314 * Register Elementor widget
315 *
316 * @return void
317 */
318 private function registerElementorWidget(): void
319 {
320 if (!defined('ELEMENTOR_VERSION')) {
321 return;
322 }
323
324 IvyFormsElementorLoader::instance();
325 }
326
327 /**
328 * Load plugin translations
329 * Called on 'init' action to comply with WordPress 6.7+ requirements
330 *
331 * @return void
332 */
333 public function loadTranslations(): void
334 {
335 load_plugin_textdomain(
336 'ivyforms',
337 false,
338 dirname(plugin_basename(IVYFORMS_FILE)) . '/languages/' . get_locale() . '/'
339 );
340 }
341 }
342