| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\GoogleSheets\Hooks; |
| 4 |
|
| 5 |
use IvyForms\Routes\GoogleSheets\GoogleSheets; |
| 6 |
use IvyForms\Services\Settings\SettingsService; |
| 7 |
use IvyForms\Vendor\DI\Container; |
| 8 |
|
| 9 |
/** |
| 10 |
* Initializes Google Sheets integration settings, hooks, and routes. |
| 11 |
*/ |
| 12 |
class InitializationHook |
| 13 |
{ |
| 14 |
private SettingsService $settingsService; |
| 15 |
|
| 16 |
public function __construct(SettingsService $settingsService) |
| 17 |
{ |
| 18 |
$this->settingsService = $settingsService; |
| 19 |
} |
| 20 |
|
| 21 |
public function initialize(): void |
| 22 |
{ |
| 23 |
$this->initializeSettings(); |
| 24 |
$this->registerSettingsFilter(); |
| 25 |
$this->registerHooks(); |
| 26 |
$this->registerRoutes(); |
| 27 |
} |
| 28 |
|
| 29 |
private function registerSettingsFilter(): void |
| 30 |
{ |
| 31 |
$settingsHook = new SettingsHook(); |
| 32 |
|
| 33 |
add_filter( |
| 34 |
'ivyforms/global/settings/get_all', |
| 35 |
[$settingsHook, 'addIntegrationData'], |
| 36 |
10, |
| 37 |
2 |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
private function initializeSettings(): void |
| 42 |
{ |
| 43 |
$settings = $this->settingsService->getSetting('integrations', 'google_sheets'); |
| 44 |
|
| 45 |
if ($settings === null) { |
| 46 |
$this->settingsService->setSetting('integrations', 'google_sheets', [ |
| 47 |
'enabled' => false, |
| 48 |
'connected' => false, |
| 49 |
'authType' => '', |
| 50 |
'accessToken' => '', |
| 51 |
'refreshToken' => '', |
| 52 |
'tokenExpiresAt' => 0, |
| 53 |
'accountEmail' => '', |
| 54 |
]); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
private function registerHooks(): void |
| 59 |
{ |
| 60 |
try { |
| 61 |
$basePlugin = \IvyForms\Plugin\Plugin::getInstance(); |
| 62 |
if (!$basePlugin || !$basePlugin->container) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$entryHooks = $basePlugin->container->get(GoogleSheetsEntryHooks::class); |
| 67 |
$entryHooks->register(); |
| 68 |
|
| 69 |
$formHooks = $basePlugin->container->get(GoogleSheetsFormHooks::class); |
| 70 |
$formHooks->register(); |
| 71 |
} catch (\Exception $e) { |
| 72 |
error_log('IvyForms: Failed to register Google Sheets hooks: ' . $e->getMessage()); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
private function registerRoutes(): void |
| 77 |
{ |
| 78 |
add_action( |
| 79 |
'ivyforms/rest/register_additional_routes', |
| 80 |
static function (Container $container, string $namespace): void { |
| 81 |
GoogleSheets::registerRoutes($container, $namespace); |
| 82 |
}, |
| 83 |
10, |
| 84 |
2 |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
|