| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Settings hook for activation |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace IvyForms\Services\InstallActions; |
| 8 |
|
| 9 |
use Exception; |
| 10 |
use IvyForms\Services\Settings\SettingsService; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class ActivationSettingsHook |
| 14 |
* |
| 15 |
* @package IvyForms\Services\InstallActions |
| 16 |
*/ |
| 17 |
class ActivationSettingsHook |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Initialize the plugin |
| 21 |
* |
| 22 |
* @throws Exception |
| 23 |
*/ |
| 24 |
public static function init(): void |
| 25 |
{ |
| 26 |
self::initGeneralSettings(); |
| 27 |
self::initIntegrationsSettings(); |
| 28 |
self::initPaymentSettings(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @param string $category |
| 33 |
* @param mixed[] $settings |
| 34 |
*/ |
| 35 |
public static function initSettings(string $category, array $settings): void |
| 36 |
{ |
| 37 |
$settingsStorage = new SettingsService(); |
| 38 |
|
| 39 |
if (!$settingsStorage->getCategorySettings($category)) { |
| 40 |
$settingsStorage->setCategorySettings( |
| 41 |
$category, |
| 42 |
[] |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
foreach ($settings as $key => $value) { |
| 47 |
if (null === $settingsStorage->getSetting($category, $key)) { |
| 48 |
$settingsStorage->setSetting( |
| 49 |
$category, |
| 50 |
$key, |
| 51 |
$value |
| 52 |
); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Init General Settings |
| 59 |
*/ |
| 60 |
private static function initGeneralSettings(): void |
| 61 |
{ |
| 62 |
|
| 63 |
$settings = [ |
| 64 |
'version' => IVYFORMS_VERSION, |
| 65 |
'changelog_lite_version' => '', |
| 66 |
'changelog_pro_version' => '', |
| 67 |
'fullscreen' => false, |
| 68 |
]; |
| 69 |
|
| 70 |
self::initSettings('general', $settings); |
| 71 |
|
| 72 |
// On activation/reactivation, sync stored changelog versions to current versions |
| 73 |
// so changelog is only shown on real version updates. |
| 74 |
$settingsStorage = new SettingsService(); |
| 75 |
$settingsStorage->setSetting('general', 'changelog_lite_version', IVYFORMS_VERSION); |
| 76 |
|
| 77 |
if (defined('IVYFORMS_PRO_VERSION')) { |
| 78 |
$settingsStorage->setSetting('general', 'changelog_pro_version', IVYFORMS_PRO_VERSION); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Init Integrations Settings |
| 84 |
*/ |
| 85 |
private static function initIntegrationsSettings(): void |
| 86 |
{ |
| 87 |
$settings = [ |
| 88 |
'wpdatatables' => [ |
| 89 |
'enabled' => false, |
| 90 |
], |
| 91 |
'ameliabooking' => [ |
| 92 |
'enabled' => false, |
| 93 |
], |
| 94 |
'mailchimp' => [ |
| 95 |
'enabled' => false, |
| 96 |
], |
| 97 |
'square' => [ |
| 98 |
'enabled' => false, |
| 99 |
], |
| 100 |
// Google Sheets last — matches integration list/menu ordering |
| 101 |
'google_sheets' => [ |
| 102 |
'enabled' => false, |
| 103 |
'connected' => false, |
| 104 |
'authType' => '', |
| 105 |
'accessToken' => '', |
| 106 |
'refreshToken' => '', |
| 107 |
'tokenExpiresAt' => 0, |
| 108 |
'accountEmail' => '', |
| 109 |
], |
| 110 |
]; |
| 111 |
|
| 112 |
self::initSettings('integrations', $settings); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Init payment settings (Square). |
| 117 |
*/ |
| 118 |
private static function initPaymentSettings(): void |
| 119 |
{ |
| 120 |
$settings = [ |
| 121 |
'square' => [ |
| 122 |
'connected' => false, |
| 123 |
'merchantId' => '', |
| 124 |
'merchantName' => '', |
| 125 |
'locationId' => '', |
| 126 |
'locationName' => '', |
| 127 |
'currency' => '', |
| 128 |
'accessToken' => '', |
| 129 |
'refreshToken' => '', |
| 130 |
'expiresAt' => '', |
| 131 |
], |
| 132 |
]; |
| 133 |
|
| 134 |
self::initSettings('payment', $settings); |
| 135 |
} |
| 136 |
} |
| 137 |
|