| @@ -1,0 +1,287 @@ | ||
| 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\Integrations\Elementor\IvyFormsElementorLoader; | |
| 18 | +use IvyForms\Services\Integrations\Gutenberg\Blocks\GutenbergBlock; | |
| 19 | +use IvyForms\Services\Integrations\IntegrationService; | |
| 20 | +use IvyForms\Services\Menu\MenuManager; | |
| 21 | +use IvyForms\Services\Payment\PaymentCspHeaders; | |
| 22 | +use IvyForms\Services\Preview\PreviewService; | |
| 23 | +use IvyForms\Services\Settings\SettingsService; | |
| 24 | +use IvyForms\Services\Shortcode\Helpers\ShortcodeHelper; | |
| 25 | +use IvyForms\Services\Shortcode\ShortcodeService; | |
| 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; | |
| 31 | + | |
| 32 | +/** | |
| 33 | + * Plugin | |
| 34 | + */ | |
| 35 | +class Plugin | |
| 36 | +{ | |
| 37 | + /** | |
| 38 | + * Instance | |
| 39 | + */ | |
| 40 | + private static ?object $instance = null; | |
| 41 | + | |
| 42 | + private ContainerBuilder $builder; | |
| 43 | + /** | |
| 44 | + * Container | |
| 45 | + */ | |
| 46 | + public ?Container $container = null; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Settings Service | |
| 50 | + */ | |
| 51 | + private ?SettingsService $settingsService = null; | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Load plugin | |
| 55 | + * | |
| 56 | + * @return object initialized object of class. | |
| 57 | + * @throws Exception | |
| 58 | + */ | |
| 59 | + public static function getInstance(): ?object | |
| 60 | + { | |
| 61 | + if (null === self::$instance) { | |
| 62 | + self::$instance = new self(); | |
| 63 | + } | |
| 64 | + return self::$instance; | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Constructor | |
| 69 | + * | |
| 70 | + * @throws Exception | |
| 71 | + * @since 0.1.0 | |
| 72 | + */ | |
| 73 | + public function __construct() | |
| 74 | + { | |
| 75 | + $this->builder = new ContainerBuilder(); | |
| 76 | + $this->builder->addDefinitions(IVYFORMS_PATH . '/backend/src/Config/repository.php'); | |
| 77 | + $this->builder->addDefinitions(IVYFORMS_PATH . '/backend/src/Config/services.php'); | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Allow to hook into container before build | |
| 81 | + * @since 0.1.0 | |
| 82 | + * | |
| 83 | + * Arguments: | |
| 84 | + * - ContainerBuilder $builder The IvyForms DI container builder (base) | |
| 85 | + */ | |
| 86 | + do_action('ivyforms/boot/extend_container_builder', $this->builder); | |
| 87 | + | |
| 88 | + $this->container = $this->builder->build(); | |
| 89 | + | |
| 90 | + $this->settingsService = $this->container->get(SettingsService::class); | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * The code that runs during plugin activation | |
| 94 | + */ | |
| 95 | + register_activation_hook( | |
| 96 | + IVYFORMS_FILE, | |
| 97 | + [ PluginInstallLifecycle::class, 'activation' ] | |
| 98 | + ); | |
| 99 | + register_activation_hook( | |
| 100 | + IVYFORMS_FILE, | |
| 101 | + [ PluginInstallLifecycle::class, 'activationSettings' ] | |
| 102 | + ); | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * The code that runs during plugin deactivation | |
| 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 | + */ | |
| 115 | + register_uninstall_hook( | |
| 116 | + IVYFORMS_FILE, | |
| 117 | + [ PluginInstallLifecycle::class, 'deletion'] | |
| 118 | + ); | |
| 119 | + | |
| 120 | + /** Init load */ | |
| 121 | + add_action('plugins_loaded', [ $this, 'init' ]); | |
| 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 | + | |
| 128 | + /** Register API calls */ | |
| 129 | + add_action('rest_api_init', [$this, 'registerRoutes']); | |
| 130 | + | |
| 131 | + /** Isolate API calls */ | |
| 132 | + add_action('wp_ajax_ivyforms_api', [ $this, 'registerRoutes' ]); | |
| 133 | + add_action('wp_ajax_nopriv_ivyforms_api', [ $this, 'registerRoutes' ]); | |
| 134 | + | |
| 135 | + /** Activation hook for new site on multisite setup */ | |
| 136 | + add_action('wpmu_new_blog', [ PluginInstallLifecycle::class, 'initNewSite' ]); | |
| 137 | + | |
| 138 | + add_filter('wp_script_attributes', ['IvyForms\Services\Shortcode\ShortcodeService', 'addScriptAttribute']); | |
| 139 | + | |
| 140 | + // Register admin hooks via AdminHooks class | |
| 141 | + $adminHooks = new AdminHooks($this->settingsService); | |
| 142 | + $adminHooks->register(); | |
| 143 | + } | |
| 144 | + | |
| 145 | + /** | |
| 146 | + * @throws DependencyException | |
| 147 | + * @throws NotFoundException | |
| 148 | + */ | |
| 149 | + public function registerRoutes(): void | |
| 150 | + { | |
| 151 | + // Register REST API routes after the container is built | |
| 152 | + Routes::registerRoutes($this->container); | |
| 153 | + } | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * Init | |
| 157 | + * @return void | |
| 158 | + * @throws ValidationException|InvalidArgumentException | |
| 159 | + */ | |
| 160 | + public function init(): void | |
| 161 | + { | |
| 162 | + new PreviewService(); | |
| 163 | + $this->initIntegrationService(); | |
| 164 | + $this->initMenuManager(); | |
| 165 | + $this->registerShortcode(); | |
| 166 | + PaymentCspHeaders::register(); | |
| 167 | + $this->registerGutenbergBlock(); | |
| 168 | + $this->registerElementorWidget(); | |
| 169 | + $this->initUsageTrackingService(); | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * Initialize integration service | |
| 174 | + * | |
| 175 | + * @return void | |
| 176 | + */ | |
| 177 | + private function initIntegrationService(): void | |
| 178 | + { | |
| 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 | + } | |
| 188 | + } | |
| 189 | + | |
| 190 | + /** | |
| 191 | + * Initialize usage tracking service | |
| 192 | + * | |
| 193 | + * @return void | |
| 194 | + */ | |
| 195 | + private function initUsageTrackingService(): void | |
| 196 | + { | |
| 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 | + } | |
| 206 | + } | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * Initialize menu manager | |
| 210 | + * | |
| 211 | + * @return void | |
| 212 | + */ | |
| 213 | + private function initMenuManager(): void | |
| 214 | + { | |
| 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()); | |
| 223 | + } | |
| 224 | + } | |
| 225 | + } | |
| 226 | + | |
| 227 | + /** | |
| 228 | + * Register shortcode | |
| 229 | + * | |
| 230 | + * @return void | |
| 231 | + */ | |
| 232 | + private function registerShortcode(): void | |
| 233 | + { | |
| 234 | + if (is_admin()) { | |
| 235 | + return; | |
| 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 | + ); | |
| 286 | + } | |
| 287 | +} | |