← All changes
|
backend/src/Services/Shortcode/Helpers/ShortcodeHelper.php
+43
-8
1.1.1
→
1.5.1
View file →
| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace IvyForms\Services\Shortcode\Helpers; |
| 4 | 4 | |
| 5 | 5 | use IvyForms\Factory\Field\FieldFactory; |
| 6 | 6 | use IvyForms\Factory\Security\SecurityServiceFactory; |
| 7 | +use IvyForms\Services\Payment\SquareEnvironment; | |
| 7 | 8 | use IvyForms\Services\Settings\SettingsService; |
| 8 | 9 | use IvyForms\Services\Security\SecurityService; |
| 9 | 10 | use IvyForms\Services\Translations\FrontendStrings; |
| 10 | 11 | use IvyForms\Routes\Routes; |
| @@ -154,10 +155,22 @@ | ||
| 154 | 155 | |
| 155 | 156 | // Ensure style handles are available for dependencies before enqueuing. |
| 156 | 157 | self::registerFrontendStyleHandles(); |
| 157 | 158 | |
| 159 | + $mainScriptDeps = []; | |
| 160 | + if (self::formHasPaymentField($formList)) { | |
| 161 | + wp_enqueue_script( | |
| 162 | + 'square-web-payments-sdk', | |
| 163 | + SquareEnvironment::getWebPaymentsSdkUrl(), | |
| 164 | + [], | |
| 165 | + null, | |
| 166 | + true | |
| 167 | + ); | |
| 168 | + $mainScriptDeps[] = 'square-web-payments-sdk'; | |
| 169 | + } | |
| 170 | + | |
| 158 | 171 | // Enqueue main script and styles |
| 159 | - self::enqueueMainScript($scriptId, $scriptSrc, $scriptVersion); | |
| 172 | + self::enqueueMainScript($scriptId, $scriptSrc, $scriptVersion, $mainScriptDeps); | |
| 160 | 173 | |
| 161 | 174 | if ($styleSrc) { // @phpstan-ignore-line |
| 162 | 175 | self::enqueueStyles($styleSrc, $stylePublicSrc, $scriptVersion); |
| 163 | 176 | } |
| @@ -164,8 +177,10 @@ | ||
| 164 | 177 | |
| 165 | 178 | // Enqueue captcha scripts and localize captcha config |
| 166 | 179 | self::enqueueCaptchaAndLocalize($scriptId, $formList); |
| 167 | 180 | |
| 181 | + SquareFrontendConfigHelper::localizeConfigIfNeeded($scriptId, $formList); | |
| 182 | + | |
| 168 | 183 | // Localize common frontend data (forms, labels, urls, api settings, date formats) |
| 169 | 184 | self::localizeFormListAndLabels($scriptId, $formList); |
| 170 | 185 | |
| 171 | 186 | do_action('ivyforms/shortcode/enqueue_scripts', $scriptId); |
| @@ -192,19 +207,20 @@ | ||
| 192 | 207 | |
| 193 | 208 | /** |
| 194 | 209 | * Enqueue the main frontend script |
| 195 | 210 | * |
| 196 | - * @param string $scriptId | |
| 197 | - * @param string $scriptSrc | |
| 198 | - * @param mixed $scriptVersion | |
| 199 | - * @return void | |
| 211 | + * @param array<string> $deps | |
| 200 | 212 | */ |
| 201 | - private static function enqueueMainScript(string $scriptId, string $scriptSrc, $scriptVersion): void | |
| 202 | - { | |
| 213 | + private static function enqueueMainScript( | |
| 214 | + string $scriptId, | |
| 215 | + string $scriptSrc, | |
| 216 | + ?string $scriptVersion, | |
| 217 | + array $deps = [] | |
| 218 | + ): void { | |
| 203 | 219 | wp_enqueue_script( |
| 204 | 220 | $scriptId, |
| 205 | 221 | $scriptSrc, |
| 206 | - [], | |
| 222 | + $deps, | |
| 207 | 223 | $scriptVersion, |
| 208 | 224 | true |
| 209 | 225 | ); |
| 210 | 226 | } |
| @@ -279,8 +295,27 @@ | ||
| 279 | 295 | * |
| 280 | 296 | * @param string $scriptId |
| 281 | 297 | * @param array<string, array<string, mixed>> $formList |
| 282 | 298 | * @return void |
| 299 | + */ | |
| 300 | + /** | |
| 301 | + * @param array<string, array<string, mixed>> $formList | |
| 302 | + */ | |
| 303 | + private static function formHasPaymentField(array $formList): bool | |
| 304 | + { | |
| 305 | + foreach ($formList as $formData) { | |
| 306 | + foreach ($formData['fields'] ?? [] as $field) { | |
| 307 | + if (is_array($field) && ($field['type'] ?? '') === 'payment-method') { | |
| 308 | + return true; | |
| 309 | + } | |
| 310 | + } | |
| 311 | + } | |
| 312 | + | |
| 313 | + return false; | |
| 314 | + } | |
| 315 | + | |
| 316 | + /** | |
| 317 | + * @param array<string, array<string, mixed>> $formList | |
| 283 | 318 | */ |
| 284 | 319 | private static function localizeFormListAndLabels(string $scriptId, array $formList): void |
| 285 | 320 | { |
| 286 | 321 | wp_localize_script( |