PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
← All changes | backend/src/Services/Template/TemplateDefaults.php +143 -4 0.9.1 → 1.5 View file →
@@ -1,12 +1,11 @@
1 1 <?php
2 2
3 3 namespace IvyForms\Services\Template;
4 4
5 -// phpcs:disable PSR1.Files.SideEffects
6 -if (!defined('ABSPATH')) {
7 - exit; // Exit if accessed directly
8 -}
5 +use IvyForms\Common\Constants\SupportedCurrencyCodes;
6 +use IvyForms\Common\Exceptions\InvalidArgumentException;
7 +use IvyForms\Services\Translations\BackendStrings;
9 8
10 9 /**
11 10 * Class TemplateDefaults
12 11 * Holds default configuration values for form templates
@@ -15,8 +14,15 @@
15 14 */
16 15 class TemplateDefaults
17 16 {
18 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 + /**
19 25 * Default integration settings for new forms
20 26 *
21 27 * @var array<string, array<string, mixed>>
22 28 */
@@ -33,6 +39,139 @@
33 39 */
34 40 public static function getDefaultIntegrationSettings(): array
35 41 {
36 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';
37 176 }
38 177 }