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