| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Template; |
| 4 |
|
| 5 |
use IvyForms\Common\Constants\SupportedCurrencyCodes; |
| 6 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 7 |
use IvyForms\Services\Translations\BackendStrings; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class TemplateDefaults |
| 11 |
* Holds default configuration values for form templates |
| 12 |
* |
| 13 |
* @package IvyForms\Services\Template |
| 14 |
*/ |
| 15 |
class TemplateDefaults |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Cached default form styles loaded from JSON. |
| 19 |
* |
| 20 |
* @var array<string, mixed>|null |
| 21 |
*/ |
| 22 |
private static ?array $cachedFormStyles = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Default integration settings for new forms |
| 26 |
* |
| 27 |
* @var array<string, array<string, mixed>> |
| 28 |
*/ |
| 29 |
public const DEFAULT_INTEGRATION_SETTINGS = [ |
| 30 |
'wpdatatables' => [ |
| 31 |
'enabled' => true |
| 32 |
] |
| 33 |
]; |
| 34 |
|
| 35 |
/** |
| 36 |
* Get default integration settings |
| 37 |
* |
| 38 |
* @return array<string, array<string, mixed>> |
| 39 |
*/ |
| 40 |
public static function getDefaultIntegrationSettings(): array |
| 41 |
{ |
| 42 |
return self::DEFAULT_INTEGRATION_SETTINGS; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Default payment settings for new forms. |
| 47 |
* |
| 48 |
* @var array{currency:string} |
| 49 |
*/ |
| 50 |
public const DEFAULT_PAYMENT_SETTINGS = [ |
| 51 |
'currency' => SupportedCurrencyCodes::DEFAULT_CODE, |
| 52 |
]; |
| 53 |
|
| 54 |
/** |
| 55 |
* @return array{currency:string} |
| 56 |
*/ |
| 57 |
public static function getDefaultPaymentSettings(): array |
| 58 |
{ |
| 59 |
return self::DEFAULT_PAYMENT_SETTINGS; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get default form style settings from bundled JSON (`backend/resources/defaultFormStyles.json`). |
| 64 |
* |
| 65 |
* @return array<string, mixed> |
| 66 |
* @throws InvalidArgumentException |
| 67 |
*/ |
| 68 |
public static function getDefaultFormStyles(): array |
| 69 |
{ |
| 70 |
$exceptionStrings = BackendStrings::getExceptionStrings(); |
| 71 |
|
| 72 |
if (self::$cachedFormStyles !== null) { |
| 73 |
return self::$cachedFormStyles; |
| 74 |
} |
| 75 |
|
| 76 |
$jsonPath = self::getDefaultFormStylesJsonPath(); |
| 77 |
|
| 78 |
if (!is_readable($jsonPath)) { |
| 79 |
throw new InvalidArgumentException( |
| 80 |
sprintf( |
| 81 |
$exceptionStrings['default_styles_json_not_found'], |
| 82 |
$jsonPath |
| 83 |
) |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
$jsonContent = file_get_contents($jsonPath); |
| 88 |
if ($jsonContent === false) { |
| 89 |
throw new InvalidArgumentException( |
| 90 |
sprintf($exceptionStrings['default_styles_json_read_failed'], $jsonPath) |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
try { |
| 95 |
$decoded = json_decode($jsonContent, true, 512, JSON_THROW_ON_ERROR); |
| 96 |
} catch (\JsonException $jsonException) { |
| 97 |
throw new InvalidArgumentException( |
| 98 |
sprintf($exceptionStrings['default_styles_json_parse_failed'], $jsonException->getMessage()) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
if (!is_array($decoded)) { |
| 103 |
throw new InvalidArgumentException($exceptionStrings['default_styles_json_invalid_structure']); |
| 104 |
} |
| 105 |
|
| 106 |
self::$cachedFormStyles = self::filterDefaultFormStyles($decoded); |
| 107 |
|
| 108 |
return self::$cachedFormStyles; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Default form styles from JSON, or a minimal safe structure if the file is missing or invalid. |
| 113 |
* |
| 114 |
* @return array<string, mixed> |
| 115 |
*/ |
| 116 |
public static function getDefaultFormStylesSafe(): array |
| 117 |
{ |
| 118 |
try { |
| 119 |
return self::getDefaultFormStyles(); |
| 120 |
} catch (InvalidArgumentException $e) { |
| 121 |
return self::filterDefaultFormStyles(self::getMinimalFormStyleSettingsFallback()); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Minimal style settings when JSON defaults cannot be loaded (matches sanitizer-valid shape). |
| 127 |
* |
| 128 |
* @return array<string, mixed> |
| 129 |
*/ |
| 130 |
private static function getMinimalFormStyleSettingsFallback(): array |
| 131 |
{ |
| 132 |
return [ |
| 133 |
'stylesEnabled' => false, |
| 134 |
'selectedTheme' => 'ivy-default', |
| 135 |
'styleMode' => 'quick', |
| 136 |
'customCssClass' => '', |
| 137 |
'additionalCssClasses' => '', |
| 138 |
'quickMode' => [], |
| 139 |
'advancedMode' => [], |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Allow Pro (and other extensions) to merge plan-specific default style sections. |
| 145 |
* |
| 146 |
* @param array<string, mixed> $defaults |
| 147 |
* @return array<string, mixed> |
| 148 |
*/ |
| 149 |
private static function filterDefaultFormStyles(array $defaults): array |
| 150 |
{ |
| 151 |
/** @var mixed $filtered */ |
| 152 |
$filtered = apply_filters('ivyforms/style/filter/default_form_styles', $defaults); |
| 153 |
|
| 154 |
if (!is_array($filtered)) { |
| 155 |
error_log( |
| 156 |
'IvyForms: ivyforms/style/filter/default_form_styles filter must return an array; ' |
| 157 |
. 'using bundled defaults.' |
| 158 |
); |
| 159 |
|
| 160 |
return $defaults; |
| 161 |
} |
| 162 |
|
| 163 |
return $filtered; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Absolute path to the bundled default form styles JSON. |
| 168 |
* |
| 169 |
* @return string |
| 170 |
*/ |
| 171 |
private static function getDefaultFormStylesJsonPath(): string |
| 172 |
{ |
| 173 |
$root = defined('IVYFORMS_PATH') ? IVYFORMS_PATH : dirname(__DIR__, 4); |
| 174 |
|
| 175 |
return $root . '/backend/resources/defaultFormStyles.json'; |
| 176 |
} |
| 177 |
} |
| 178 |
|