PluginProbe
The Innovative Form Builder – IvyForms / 0.2.1
The Innovative Form Builder – IvyForms v0.2.1
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
ivyforms / backend / src / Services / Shortcode / ShortcodeService.php

ShortcodeService.php in The Innovative Form Builder – IvyForms 0.2.1, at backend/src/Services/Shortcode/ShortcodeService.php

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