| 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\Vendor\DI\Container; |
| 18 |
use IvyForms\Vendor\DI\ContainerBuilder; |
| 19 |
use IvyForms\Vendor\DI\DependencyException; |
| 20 |
use IvyForms\Vendor\DI\NotFoundException; |
| 21 |
use Exception; |
| 22 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 23 |
use IvyForms\Routes\Routes; |
| 24 |
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; |
| 30 |
use IvyForms\Services\Menu\MenuManager; |
| 31 |
use IvyForms\Services\Preview\PreviewService; |
| 32 |
use IvyForms\Services\Settings\SettingsService; |
| 33 |
use IvyForms\Services\Shortcode\ShortcodeService; |
| 34 |
use IvyForms\Services\Integrations\IntegrationService; |
| 35 |
|
| 36 |
/** |
| 37 |
* Plugin |
| 38 |
*/ |
| 39 |
class Plugin |
| 40 |
{ |
| 41 |
/** |
| 42 |
* Instance |
| 43 |
*/ |
| 44 |
private static ?object $instance = null; |
| 45 |
|
| 46 |
private ContainerBuilder $builder; |
| 47 |
/** |
| 48 |
* Container |
| 49 |
*/ |
| 50 |
public ?Container $container = null; |
| 51 |
|
| 52 |
/** |
| 53 |
* Settings Service |
| 54 |
*/ |
| 55 |
private ?SettingsService $settingsService = null; |
| 56 |
|
| 57 |
/** |
| 58 |
* Load plugin |
| 59 |
* |
| 60 |
* @return object initialized object of class. |
| 61 |
* @throws Exception |
| 62 |
*/ |
| 63 |
public static function getInstance(): ?object |
| 64 |
{ |
| 65 |
if (null === self::$instance) { |
| 66 |
self::$instance = new self(); |
| 67 |
} |
| 68 |
return self::$instance; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Constructor |
| 73 |
* |
| 74 |
* @throws Exception |
| 75 |
* @since 0.1.0 |
| 76 |
*/ |
| 77 |
public function __construct() |
| 78 |
{ |
| 79 |
$this->builder = new ContainerBuilder(); |
| 80 |
$this->builder->addDefinitions(IVYFORMS_PATH . '/backend/src/Config/repository.php'); |
| 81 |
$this->builder->addDefinitions(IVYFORMS_PATH . '/backend/src/Config/services.php'); |
| 82 |
|
| 83 |
/** |
| 84 |
* Allow to hook into container before build |
| 85 |
* @since 0.1.0 |
| 86 |
* |
| 87 |
* Arguments: |
| 88 |
* - ContainerBuilder $builder The IvyForms DI container builder (base) |
| 89 |
*/ |
| 90 |
do_action('ivyforms/boot/extend_container_builder', $this->builder); |
| 91 |
|
| 92 |
$this->container = $this->builder->build(); |
| 93 |
|
| 94 |
$this->settingsService = $this->container->get(SettingsService::class); |
| 95 |
|
| 96 |
/** |
| 97 |
* The code that runs during plugin activation |
| 98 |
*/ |
| 99 |
register_activation_hook( |
| 100 |
IVYFORMS_FILE, |
| 101 |
[ self::class, 'activation' ] |
| 102 |
); |
| 103 |
register_activation_hook( |
| 104 |
IVYFORMS_FILE, |
| 105 |
[ self::class, 'activationSettings' ] |
| 106 |
); |
| 107 |
|
| 108 |
/** |
| 109 |
* The code that runs during plugin deactivation |
| 110 |
*/ |
| 111 |
register_uninstall_hook( |
| 112 |
IVYFORMS_FILE, |
| 113 |
[ self::class, 'deletion'] |
| 114 |
); |
| 115 |
|
| 116 |
/** Init load */ |
| 117 |
add_action('plugins_loaded', [ $this, 'init' ]); |
| 118 |
|
| 119 |
/** Register API calls */ |
| 120 |
add_action('rest_api_init', [$this, 'registerRoutes']); |
| 121 |
|
| 122 |
/** Isolate API calls */ |
| 123 |
add_action('wp_ajax_ivyforms_api', [ $this, 'registerRoutes' ]); |
| 124 |
add_action('wp_ajax_nopriv_ivyforms_api', [ $this, 'registerRoutes' ]); |
| 125 |
|
| 126 |
/** Activation hook for new site on multisite setup */ |
| 127 |
add_action('wpmu_new_blog', [ $this, 'initNewSite' ]); |
| 128 |
|
| 129 |
add_filter('wp_script_attributes', ['IvyForms\Services\Shortcode\ShortcodeService', 'addScriptAttribute']); |
| 130 |
|
| 131 |
// Register admin hooks via AdminHooks class |
| 132 |
$adminHooks = new AdminHooks($this->settingsService); |
| 133 |
$adminHooks->register(); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @throws DependencyException |
| 138 |
* @throws NotFoundException |
| 139 |
*/ |
| 140 |
public function registerRoutes(): void |
| 141 |
{ |
| 142 |
// Register REST API routes after the container is built |
| 143 |
Routes::registerRoutes($this->container); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* @param bool $networkWide |
| 149 |
*/ |
| 150 |
public static function activation(bool $networkWide): void |
| 151 |
{ |
| 152 |
//Network activation |
| 153 |
if ($networkWide && function_exists('is_multisite') && is_multisite()) { |
| 154 |
ActivationMultisite::init(); |
| 155 |
} |
| 156 |
|
| 157 |
ActivationDatabaseHook::init(); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* @param int $siteId |
| 162 |
*/ |
| 163 |
public static function initNewSite(int $siteId): void |
| 164 |
{ |
| 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(); |
| 191 |
} |
| 192 |
|
| 193 |
DeleteDatabaseHook::delete(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Init |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public function init(): void |
| 202 |
{ |
| 203 |
|
| 204 |
new PreviewService(); |
| 205 |
|
| 206 |
// Initialize integration registry system |
| 207 |
try { |
| 208 |
/** @var IntegrationService $integrationService */ |
| 209 |
$integrationService = $this->container->get(IntegrationService::class); |
| 210 |
$integrationService->init(); |
| 211 |
} catch (DependencyException |NotFoundException $e) { |
| 212 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 213 |
error_log('Integration service error: ' . $e->getMessage()); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
try { |
| 218 |
/** @var MenuManager $menuManager*/ |
| 219 |
$menuManager = $this->container->get(MenuManager::class); |
| 220 |
// Register the admin menus and submenus using the 'admin_menu' hook |
| 221 |
$menuManager->registerMenus(); |
| 222 |
} catch (DependencyException |NotFoundException $e) { |
| 223 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 224 |
error_log('Menu error: ' . $e->getMessage()); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (!is_admin()) { |
| 229 |
add_shortcode('ivyforms', function ($atts = []) { |
| 230 |
return ShortcodeService::shortcodeHandler($atts); |
| 231 |
}); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|