| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © TMS-Plugins. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Services\Shortcode; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Services\API\IvyFormsAPI; |
| 16 |
use IvyForms\Services\Translations\FrontendStrings; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class ShortcodeService |
| 20 |
* |
| 21 |
* @package IvyForms\Services\Shortcode |
| 22 |
*/ |
| 23 |
class ShortcodeService |
| 24 |
{ |
| 25 |
public static int $counter = 0; |
| 26 |
/** |
| 27 |
* @var mixed[] |
| 28 |
*/ |
| 29 |
public static array $formList = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* Shortcode handler |
| 33 |
* |
| 34 |
* @param mixed[] $atts |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public static function shortcodeHandler(array $atts): string |
| 39 |
{ |
| 40 |
$atts = shortcode_atts( |
| 41 |
[ |
| 42 |
'id' => '0' |
| 43 |
], |
| 44 |
$atts |
| 45 |
); |
| 46 |
|
| 47 |
if (empty($atts['id'])) { |
| 48 |
return ''; |
| 49 |
} |
| 50 |
|
| 51 |
$formId = (int)$atts['id']; |
| 52 |
$form = IvyFormsAPI::getForm($formId); |
| 53 |
|
| 54 |
// Check if the form is published |
| 55 |
if (is_wp_error($form) || !($form->isPublished())) { |
| 56 |
return ''; |
| 57 |
} |
| 58 |
|
| 59 |
$counter = self::$counter++; |
| 60 |
|
| 61 |
self::getFormList($formId, $counter); |
| 62 |
|
| 63 |
self::enqueueScripts(); |
| 64 |
|
| 65 |
ob_start(); |
| 66 |
include IVYFORMS_PATH . '/view/frontend/view.php'; |
| 67 |
$html = ob_get_contents(); |
| 68 |
ob_end_clean(); |
| 69 |
|
| 70 |
return $html; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get form list |
| 75 |
* |
| 76 |
* @param int $formId |
| 77 |
* @param int $counter |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public static function getFormList(int $formId, int $counter) |
| 82 |
{ |
| 83 |
$form = IvyFormsAPI::getForm($formId); |
| 84 |
|
| 85 |
$fields = IvyFormsAPI::getFields($form->getId()); |
| 86 |
|
| 87 |
$confirmations = IvyFormsAPI::getConfirmations($form->getId()); |
| 88 |
|
| 89 |
$fieldsMap = []; |
| 90 |
|
| 91 |
foreach ($fields as $field) { |
| 92 |
$fieldsMap[] = $field->toArray(); |
| 93 |
} |
| 94 |
|
| 95 |
$confirmationsMap = []; |
| 96 |
|
| 97 |
foreach ($confirmations as $confirmation) { |
| 98 |
$confirmationsMap[] = is_object($confirmation) && method_exists($confirmation, 'toArray') |
| 99 |
? $confirmation->toArray() |
| 100 |
: $confirmation; |
| 101 |
} |
| 102 |
|
| 103 |
$form->setFields($fieldsMap); |
| 104 |
|
| 105 |
$formArr = $form->toArray(); |
| 106 |
|
| 107 |
self::$formList[$counter] = [ |
| 108 |
'form' => $formArr, |
| 109 |
'fields' => $fieldsMap, |
| 110 |
'id' => $form->getId(), |
| 111 |
'counter' => $counter, |
| 112 |
'nonce' => wp_create_nonce('ivyformsFrontSubmissionNonce_' . $form->getId()), |
| 113 |
'confirmations' => $confirmationsMap, |
| 114 |
]; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Enqueue handler |
| 119 |
*/ |
| 120 |
public static function enqueueScripts(): void |
| 121 |
{ |
| 122 |
$scriptId = IVYFORMS_DEV ? 'ivyforms_scripts_dev_vite' : 'ivyforms_script_index'; // @phpstan-ignore-line |
| 123 |
$scriptSrc = IVYFORMS_DEV ? // @phpstan-ignore-line |
| 124 |
'http://localhost:5173/src/assets/js/public/public.ts' : |
| 125 |
IVYFORMS_URL . 'frontend/dist/public.js'; |
| 126 |
|
| 127 |
$styleSrc = IVYFORMS_DEV ? '' : IVYFORMS_URL . 'frontend/dist/index.css';// @phpstan-ignore-line |
| 128 |
$scriptVersion = IVYFORMS_DEV ? null : IVYFORMS_VERSION;// @phpstan-ignore-line |
| 129 |
|
| 130 |
wp_enqueue_script( |
| 131 |
$scriptId, |
| 132 |
$scriptSrc, |
| 133 |
[], |
| 134 |
$scriptVersion, |
| 135 |
true |
| 136 |
); |
| 137 |
|
| 138 |
if ($styleSrc) { // @phpstan-ignore-line |
| 139 |
wp_enqueue_style( |
| 140 |
'ivyforms_style_index', |
| 141 |
$styleSrc, |
| 142 |
[], |
| 143 |
$scriptVersion |
| 144 |
); |
| 145 |
|
| 146 |
// TODO Create a helper service for adding styles |
| 147 |
wp_add_inline_style( |
| 148 |
'ivyforms_style_index', |
| 149 |
/** @lang CSS */ |
| 150 |
'.ivyforms-skeleton { |
| 151 |
width: 100%; |
| 152 |
max-width: 720px; |
| 153 |
margin: 0 auto; |
| 154 |
} |
| 155 |
|
| 156 |
.ivyforms-skeleton-title { |
| 157 |
height: 28px; |
| 158 |
width: 40%; |
| 159 |
background: #dbdee3; |
| 160 |
margin-bottom: 12px; |
| 161 |
} |
| 162 |
|
| 163 |
.ivyforms-skeleton-field { |
| 164 |
margin-bottom: 16px; |
| 165 |
} |
| 166 |
|
| 167 |
.ivyforms-skeleton-label { |
| 168 |
height: 22px; |
| 169 |
width: 20%; |
| 170 |
background: #dbdee3; |
| 171 |
font-size: 14px; |
| 172 |
border-radius: 4px; |
| 173 |
margin-bottom: 4px; |
| 174 |
} |
| 175 |
|
| 176 |
.ivyforms-skeleton-input { |
| 177 |
line-height: 22px; |
| 178 |
font-size: 16px; |
| 179 |
background: #dbdee3; |
| 180 |
border-radius: 8px; |
| 181 |
width: 100%; |
| 182 |
} |
| 183 |
|
| 184 |
.ivyforms-skeleton-email, |
| 185 |
.ivyforms-skeleton-textarea, |
| 186 |
.ivyforms-skeleton-text, |
| 187 |
.ivyforms-skeleton-number, |
| 188 |
.ivyforms-skeleton-phone { |
| 189 |
height: 40px; |
| 190 |
} |
| 191 |
|
| 192 |
.ivyforms-skeleton-textarea { |
| 193 |
height: 60px; |
| 194 |
} |
| 195 |
|
| 196 |
.ivyforms-skeleton-button { |
| 197 |
height: 40px; |
| 198 |
width: 100px; |
| 199 |
background: #c0c5cb; |
| 200 |
border-radius: 6px; |
| 201 |
margin-top: 12px; |
| 202 |
} |
| 203 |
|
| 204 |
.ivyforms-skeleton-animate { |
| 205 |
animation: pulse 1.5s infinite ease-in-out; |
| 206 |
} |
| 207 |
|
| 208 |
@keyframes pulse { |
| 209 |
0%, 100% { |
| 210 |
opacity: 1; |
| 211 |
} |
| 212 |
50% { |
| 213 |
opacity: 0.4; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
.ivyforms-preview { |
| 218 |
width: 100%; |
| 219 |
max-width: 720px; |
| 220 |
margin: 0 auto; |
| 221 |
background: #fff; |
| 222 |
padding: 40px; |
| 223 |
} |
| 224 |
|
| 225 |
.ivyforms-preview h1 { |
| 226 |
font-size: 50px !important; |
| 227 |
font-weight: normal !important; |
| 228 |
margin-top: 0 !important; |
| 229 |
margin-left: 0 !important; |
| 230 |
margin-bottom: 20px !important; |
| 231 |
padding: 0 !important; |
| 232 |
}' |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
wp_localize_script( |
| 237 |
$scriptId, |
| 238 |
'wpIvyFormDataList', |
| 239 |
self::$formList |
| 240 |
); |
| 241 |
|
| 242 |
wp_localize_script( |
| 243 |
$scriptId, |
| 244 |
'wpIvyLabels', |
| 245 |
array_merge( |
| 246 |
FrontendStrings::getFormRenderStrings(), |
| 247 |
) |
| 248 |
); |
| 249 |
|
| 250 |
wp_localize_script( |
| 251 |
$scriptId, |
| 252 |
'wpIvyApiSettings', |
| 253 |
[ |
| 254 |
'root' => esc_url_raw(rest_url()), |
| 255 |
'nonce' => wp_create_nonce('wp_rest') |
| 256 |
] |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Add script attribute for module type |
| 262 |
* |
| 263 |
* @param array<string, mixed> $attributes |
| 264 |
* |
| 265 |
* @return array<string, mixed> |
| 266 |
*/ |
| 267 |
public static function addScriptAttribute(array $attributes): array |
| 268 |
{ |
| 269 |
$scriptArray = [ |
| 270 |
'ivyforms_scripts_dev_main-js', |
| 271 |
'ivyforms_scripts_dev_vite-js', |
| 272 |
'ivyforms_script_index-js', |
| 273 |
'ivyforms_scripts_public-js', |
| 274 |
]; |
| 275 |
|
| 276 |
if (isset($attributes['id']) && in_array($attributes['id'], $scriptArray, true)) { |
| 277 |
$attributes['type'] = 'module'; |
| 278 |
} |
| 279 |
|
| 280 |
return $attributes; |
| 281 |
} |
| 282 |
} |
| 283 |
|