| @@ -8,32 +8,27 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | namespace IvyForms\Plugin; |
| 11 | 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 | 12 | use Exception; |
| 23 | 13 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 14 | +use IvyForms\Common\Exceptions\ValidationException; | |
| 24 | 15 | use IvyForms\Routes\Routes; |
| 25 | 16 | 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; | |
| 17 | +use IvyForms\Services\Integrations\Elementor\IvyFormsElementorLoader; | |
| 18 | +use IvyForms\Services\Integrations\Gutenberg\Blocks\GutenbergBlock; | |
| 19 | +use IvyForms\Services\Integrations\IntegrationService; | |
| 31 | 20 | use IvyForms\Services\Menu\MenuManager; |
| 21 | +use IvyForms\Services\Payment\PaymentCspHeaders; | |
| 32 | 22 | use IvyForms\Services\Preview\PreviewService; |
| 33 | 23 | use IvyForms\Services\Settings\SettingsService; |
| 24 | +use IvyForms\Services\Shortcode\Helpers\ShortcodeHelper; | |
| 34 | 25 | use IvyForms\Services\Shortcode\ShortcodeService; |
| 35 | -use IvyForms\Services\Integrations\IntegrationService; | |
| 26 | +use IvyForms\Services\Usage\UsageTrackingService; | |
| 27 | +use IvyForms\Vendor\DI\Container; | |
| 28 | +use IvyForms\Vendor\DI\ContainerBuilder; | |
| 29 | +use IvyForms\Vendor\DI\DependencyException; | |
| 30 | +use IvyForms\Vendor\DI\NotFoundException; | |
| 36 | 31 | |
| 37 | 32 | /** |
| 38 | 33 | * Plugin |
| 39 | 34 | */ |
| @@ -98,13 +93,13 @@ | ||
| 98 | 93 | * The code that runs during plugin activation |
| 99 | 94 | */ |
| 100 | 95 | register_activation_hook( |
| 101 | 96 | IVYFORMS_FILE, |
| 102 | - [ self::class, 'activation' ] | |
| 97 | + [ PluginInstallLifecycle::class, 'activation' ] | |
| 103 | 98 | ); |
| 104 | 99 | register_activation_hook( |
| 105 | 100 | IVYFORMS_FILE, |
| 106 | - [ self::class, 'activationSettings' ] | |
| 101 | + [ PluginInstallLifecycle::class, 'activationSettings' ] | |
| 107 | 102 | ); |
| 108 | 103 | |
| 109 | 104 | /** |
| 110 | 105 | * The code that runs during plugin deactivation |
| @@ -110,9 +105,9 @@ | ||
| 110 | 105 | * The code that runs during plugin deactivation |
| 111 | 106 | */ |
| 112 | 107 | register_deactivation_hook( |
| 113 | 108 | IVYFORMS_FILE, |
| 114 | - [ self::class, 'deactivation' ] | |
| 109 | + [ PluginInstallLifecycle::class, 'deactivation' ] | |
| 115 | 110 | ); |
| 116 | 111 | |
| 117 | 112 | /** |
| 118 | 113 | * The code that runs when plugin is deleted |
| @@ -118,14 +113,16 @@ | ||
| 118 | 113 | * The code that runs when plugin is deleted |
| 119 | 114 | */ |
| 120 | 115 | register_uninstall_hook( |
| 121 | 116 | IVYFORMS_FILE, |
| 122 | - [ self::class, 'deletion'] | |
| 117 | + [ PluginInstallLifecycle::class, 'deletion'] | |
| 123 | 118 | ); |
| 124 | 119 | |
| 125 | 120 | /** Init load */ |
| 126 | 121 | add_action('plugins_loaded', [ $this, 'init' ]); |
| 127 | 122 | |
| 123 | + add_action('init', [PluginInstallLifecycle::class, 'maybeEnsureAdministratorCapabilities'], 0); | |
| 124 | + | |
| 128 | 125 | /** Load translations at init action (WordPress 6.7+ requirement) */ |
| 129 | 126 | add_action('init', [ $this, 'loadTranslations' ]); |
| 130 | 127 | |
| 131 | 128 | /** Register API calls */ |
| @@ -135,9 +132,9 @@ | ||
| 135 | 132 | add_action('wp_ajax_ivyforms_api', [ $this, 'registerRoutes' ]); |
| 136 | 133 | add_action('wp_ajax_nopriv_ivyforms_api', [ $this, 'registerRoutes' ]); |
| 137 | 134 | |
| 138 | 135 | /** Activation hook for new site on multisite setup */ |
| 139 | - add_action('wpmu_new_blog', [ $this, 'initNewSite' ]); | |
| 136 | + add_action('wpmu_new_blog', [ PluginInstallLifecycle::class, 'initNewSite' ]); | |
| 140 | 137 | |
| 141 | 138 | add_filter('wp_script_attributes', ['IvyForms\Services\Shortcode\ShortcodeService', 'addScriptAttribute']); |
| 142 | 139 | |
| 143 | 140 | // Register admin hooks via AdminHooks class |
| @@ -154,120 +151,124 @@ | ||
| 154 | 151 | // Register REST API routes after the container is built |
| 155 | 152 | Routes::registerRoutes($this->container); |
| 156 | 153 | } |
| 157 | 154 | |
| 158 | - | |
| 159 | 155 | /** |
| 160 | - * @param bool $networkWide | |
| 161 | - * @throws InvalidArgumentException | |
| 156 | + * Init | |
| 157 | + * @return void | |
| 158 | + * @throws ValidationException|InvalidArgumentException | |
| 162 | 159 | */ |
| 163 | - public static function activation(bool $networkWide): void | |
| 160 | + public function init(): void | |
| 164 | 161 | { |
| 165 | - //Network activation | |
| 166 | - if ($networkWide && function_exists('is_multisite') && is_multisite()) { | |
| 167 | - ActivationMultisite::init(); | |
| 168 | - } | |
| 169 | - | |
| 170 | - ActivationDatabaseHook::init(); | |
| 162 | + new PreviewService(); | |
| 163 | + $this->initIntegrationService(); | |
| 164 | + $this->initMenuManager(); | |
| 165 | + $this->registerShortcode(); | |
| 166 | + PaymentCspHeaders::register(); | |
| 167 | + $this->registerGutenbergBlock(); | |
| 168 | + $this->registerElementorWidget(); | |
| 169 | + $this->initUsageTrackingService(); | |
| 171 | 170 | } |
| 172 | 171 | |
| 173 | 172 | /** |
| 174 | - * @param int $siteId | |
| 175 | - * @throws InvalidArgumentException | |
| 173 | + * Initialize integration service | |
| 174 | + * | |
| 175 | + * @return void | |
| 176 | 176 | */ |
| 177 | - public static function initNewSite(int $siteId): void | |
| 177 | + private function initIntegrationService(): void | |
| 178 | 178 | { |
| 179 | - ActivationNewSiteMultisite::init($siteId); | |
| 179 | + try { | |
| 180 | + /** @var IntegrationService $integrationService */ | |
| 181 | + $integrationService = $this->container->get(IntegrationService::class); | |
| 182 | + $integrationService->init(); | |
| 183 | + } catch (DependencyException | NotFoundException $e) { | |
| 184 | + if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 185 | + error_log('Integration service error: ' . $e->getMessage()); | |
| 186 | + } | |
| 187 | + } | |
| 180 | 188 | } |
| 181 | 189 | |
| 182 | 190 | /** |
| 183 | - * @throws Exception | |
| 191 | + * Initialize usage tracking service | |
| 192 | + * | |
| 193 | + * @return void | |
| 184 | 194 | */ |
| 185 | - public static function activationSettings(): void | |
| 195 | + private function initUsageTrackingService(): void | |
| 186 | 196 | { |
| 187 | - ActivationSettingsHook::init(); | |
| 197 | + try { | |
| 198 | + /** @var UsageTrackingService $usageTrackingService */ | |
| 199 | + $usageTrackingService = $this->container->get(UsageTrackingService::class); | |
| 200 | + $usageTrackingService->init(); | |
| 201 | + } catch (DependencyException | NotFoundException $e) { | |
| 202 | + if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 203 | + error_log('Usage tracking service error: ' . $e->getMessage()); | |
| 204 | + } | |
| 205 | + } | |
| 188 | 206 | } |
| 189 | 207 | |
| 190 | 208 | /** |
| 191 | - * Deactivation function when plugin is deactivated | |
| 192 | - * Deletes all IvyForms data if delete_on_uninstall is enabled | |
| 193 | - * @throws InvalidArgumentException | |
| 209 | + * Initialize menu manager | |
| 210 | + * | |
| 211 | + * @return void | |
| 194 | 212 | */ |
| 195 | - public static function deactivation(): void | |
| 213 | + private function initMenuManager(): void | |
| 196 | 214 | { |
| 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(); | |
| 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()); | |
| 206 | 223 | } |
| 207 | - | |
| 208 | - DeleteDatabaseHook::delete(); | |
| 209 | 224 | } |
| 210 | 225 | } |
| 211 | 226 | |
| 212 | 227 | /** |
| 213 | - * Uninstall function when plugin is deleted | |
| 214 | - * @throws InvalidArgumentException | |
| 228 | + * Register shortcode | |
| 229 | + * | |
| 230 | + * @return void | |
| 215 | 231 | */ |
| 216 | - public static function deletion(): void | |
| 232 | + private function registerShortcode(): void | |
| 217 | 233 | { |
| 218 | - $settingsService = new SettingsService(); | |
| 234 | + if (is_admin()) { | |
| 235 | + return; | |
| 236 | + } | |
| 219 | 237 | |
| 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 | - } | |
| 238 | + add_action('wp_enqueue_scripts', [ShortcodeHelper::class, 'registerFrontendStyleHandles'], 1); | |
| 228 | 239 | |
| 229 | - DeleteDatabaseHook::delete(); | |
| 230 | - } | |
| 240 | + add_shortcode('ivyforms', function ($atts = []) { | |
| 241 | + return ShortcodeService::shortcodeHandler($atts); | |
| 242 | + }); | |
| 231 | 243 | } |
| 232 | 244 | |
| 233 | 245 | /** |
| 234 | - * Init | |
| 246 | + * Register Gutenberg block | |
| 247 | + * | |
| 235 | 248 | * @return void |
| 236 | - * @throws ValidationException|InvalidArgumentException | |
| 237 | 249 | */ |
| 238 | - public function init(): void | |
| 250 | + private function registerGutenbergBlock(): void | |
| 239 | 251 | { |
| 252 | + if (!function_exists('register_block_type')) { | |
| 253 | + return; | |
| 254 | + } | |
| 240 | 255 | |
| 241 | - new PreviewService(); | |
| 256 | + add_action('init', [GutenbergBlock::class, 'register']); | |
| 257 | + } | |
| 242 | 258 | |
| 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 | - } | |
| 259 | + /** | |
| 260 | + * Register Elementor widget | |
| 261 | + * | |
| 262 | + * @return void | |
| 263 | + */ | |
| 264 | + private function registerElementorWidget(): void | |
| 265 | + { | |
| 266 | + if (!defined('ELEMENTOR_VERSION')) { | |
| 267 | + return; | |
| 252 | 268 | } |
| 253 | 269 | |
| 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 | + IvyFormsElementorLoader::instance(); | |
| 270 | 271 | } |
| 271 | 272 | |
| 272 | 273 | /** |
| 273 | 274 | * Load plugin translations |