PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.18
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.18
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / FormBuilder / ShortCodeParser.php

ShortCodeParser.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.18, at app/Services/FormBuilder/ShortCodeParser.php

424 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services\FormBuilder;
4
5 use FluentForm\App;
6 use FluentForm\App\Modules\Form\FormDataParser;
7 use FluentForm\App\Modules\Form\FormFieldsParser;
8 use FluentForm\App\Services\Browser\Browser;
9 use FluentForm\Framework\Helpers\ArrayHelper;
10
11 class ShortCodeParser
12 {
13 protected static $form = null;
14
15 protected static $entry = null;
16
17 protected static $browser = null;
18
19 protected static $formFields = null;
20
21 protected static $store = [
22 'inputs' => null,
23 'original_inputs' => null,
24 'user' => null,
25 'post' => null,
26 'other' => null,
27 'submission' => null,
28 ];
29
30 public static function parse($parsable, $entryId, $data = [], $form = null, $isUrl = false, $providerOrIsHTML = false)
31 {
32 try {
33 static::setDependencies($entryId, $data, $form);
34
35 if (is_array($parsable)) {
36 return static::parseShortCodeFromArray($parsable, $isUrl, $providerOrIsHTML);
37 }
38
39 return static::parseShortCodeFromString($parsable, $isUrl, $providerOrIsHTML);
40 } catch (\Exception $e) {
41 if (defined('WP_DEBUG') && WP_DEBUG) {
42 error_log($e->getTraceAsString());
43 }
44 return '';
45 }
46 }
47
48 protected static function setDependencies($entry, $data, $form)
49 {
50 static::setEntry($entry);
51 static::setData($data);
52 static::setForm($form);
53 }
54
55 protected static function setEntry($entry)
56 {
57 static::$entry = $entry;
58 }
59
60 protected static function setdata($data)
61 {
62 if (! is_null($data)) {
63 static::$store['inputs'] = $data;
64 static::$store['original_inputs'] = $data;
65 } else {
66 $data = json_decode(static::getEntry()->response, true);
67 static::$store['inputs'] = $data;
68 static::$store['original_inputs'] = $data;
69 }
70 }
71
72 protected static function setForm($form)
73 {
74 if (! is_null($form)) {
75 static::$form = $form;
76 } else {
77 static::$form = static::getEntry()->form_id;
78 }
79 }
80
81 protected static function parseShortCodeFromArray($parsable, $isUrl = false, $provider = false)
82 {
83 foreach ($parsable as $key => $value) {
84 if (is_array($value)) {
85 $parsable[$key] = static::parseShortCodeFromArray($value, $isUrl, $provider);
86 } else {
87 $isHtml = false;
88 if ($provider) {
89 $isHtml = apply_filters('ff_will_return_html', false, $provider, $key);
90 }
91 $parsable[$key] = static::parseShortCodeFromString($value, $isUrl, $isHtml);
92 }
93 }
94
95 return $parsable;
96 }
97
98 protected static function parseShortCodeFromString($parsable, $isUrl = false, $isHtml = false)
99 {
100 if (! $parsable) {
101 return '';
102 }
103 return preg_replace_callback('/{+(.*?)}/', function ($matches) use ($isUrl, $isHtml) {
104 $value = '';
105 if (false !== strpos($matches[1], 'inputs.')) {
106 $formProperty = substr($matches[1], strlen('inputs.'));
107 $value = static::getFormData($formProperty, $isHtml);
108 } elseif (false !== strpos($matches[1], 'user.')) {
109 $userProperty = substr($matches[1], strlen('user.'));
110 $value = static::getUserData($userProperty);
111 } elseif (false !== strpos($matches[1], 'embed_post.')) {
112 $postProperty = substr($matches[1], strlen('embed_post.'));
113 $value = static::getPostData($postProperty);
114 } elseif (false !== strpos($matches[1], 'wp.')) {
115 $wpProperty = substr($matches[1], strlen('wp.'));
116 $value = static::getWPData($wpProperty);
117 } elseif (false !== strpos($matches[1], 'submission.')) {
118 $submissionProperty = substr($matches[1], strlen('submission.'));
119 $value = static::getSubmissionData($submissionProperty);
120 } elseif (false !== strpos($matches[1], 'cookie.')) {
121 $scookieProperty = substr($matches[1], strlen('cookie.'));
122 $value = wpFluentForm('request')->cookie($scookieProperty);
123 } elseif (false !== strpos($matches[1], 'payment.')) {
124 $property = substr($matches[1], strlen('payment.'));
125 $value = apply_filters('fluentform_payment_smartcode', '', $property, self::getInstance());
126 } else {
127 $value = static::getOtherData($matches[1]);
128 }
129
130 if (is_array($value)) {
131 $value = fluentImplodeRecursive(', ', $value);
132 }
133
134 if ($isUrl) {
135 $value = urlencode($value);
136 }
137
138 return $value;
139 }, $parsable);
140 }
141
142 protected static function getFormData($key, $isHtml = false)
143 {
144 if (strpos($key, '.label')) {
145 $key = str_replace('.label', '', $key);
146 $isHtml = true;
147 }
148
149 if (strpos($key, '.value')) {
150 $key = str_replace('.value', '', $key);
151 return ArrayHelper::get(static::$store['original_inputs'], $key);
152 }
153
154 if (strpos($key, '.') && ! isset(static::$store['inputs'][$key])) {
155 return ArrayHelper::get(
156 static::$store['original_inputs'],
157 $key,
158 ''
159 );
160 }
161
162 if (! isset(static::$store['inputs'][$key])) {
163 static::$store['inputs'][$key] = ArrayHelper::get(
164 static::$store['inputs'],
165 $key,
166 ''
167 );
168 }
169
170 if (is_null(static::$formFields)) {
171 static::$formFields = FormFieldsParser::getShortCodeInputs(
172 static::getForm(),
173 ['admin_label', 'attributes', 'options', 'raw']
174 );
175 }
176
177 $field = ArrayHelper::get(static::$formFields, $key, '');
178
179 if (! $field) {
180 return '';
181 }
182
183 if ($isHtml) {
184 return apply_filters(
185 'fluentform_response_render_' . $field['element'],
186 static::$store['original_inputs'][$key],
187 $field,
188 static::getForm()->id,
189 $isHtml
190 );
191 }
192
193 return static::$store['inputs'][$key] = apply_filters(
194 'fluentform_response_render_' . $field['element'],
195 static::$store['inputs'][$key],
196 $field,
197 static::getForm()->id,
198 $isHtml
199 );
200 }
201
202 protected static function getUserData($key)
203 {
204 if (is_null(static::$store['user'])) {
205 static::$store['user'] = wp_get_current_user();
206 }
207 return static::$store['user']->{$key};
208 }
209
210 protected static function getPostData($key)
211 {
212 if (is_null(static::$store['post'])) {
213 $postId = static::$store['inputs']['__fluent_form_embded_post_id'];
214 static::$store['post'] = get_post($postId);
215 static::$store['post']->permalink = get_the_permalink(static::$store['post']);
216 }
217
218 if (false !== strpos($key, 'author.')) {
219 $authorProperty = substr($key, strlen('author.'));
220 $authorId = static::$store['post']->post_author;
221 if ($authorId) {
222 $data = get_the_author_meta($authorProperty, $authorId);
223 if (! is_array($data)) {
224 return $data;
225 }
226 }
227 return '';
228 } elseif (false !== strpos($key, 'meta.')) {
229 $metaKey = substr($key, strlen('meta.'));
230 $postId = static::$store['post']->ID;
231 $data = get_post_meta($postId, $metaKey, true);
232 if (! is_array($data)) {
233 return $data;
234 }
235 return '';
236 } elseif (false !== strpos($key, 'acf.')) {
237 $metaKey = substr($key, strlen('acf.'));
238 $postId = static::$store['post']->ID;
239 if (function_exists('get_field')) {
240 $data = get_field($metaKey, $postId, true);
241 if (! is_array($data)) {
242 return $data;
243 }
244 return '';
245 }
246 }
247
248 return static::$store['post']->{$key};
249 }
250
251 protected static function getWPData($key)
252 {
253 if ('admin_email' == $key) {
254 return get_option('admin_email');
255 }
256 if ('site_url' == $key) {
257 return site_url();
258 }
259 if ('site_title' == $key) {
260 return get_option('blogname');
261 }
262 return $key;
263 }
264
265 protected static function getSubmissionData($key)
266 {
267 $entry = static::getEntry();
268
269 if (empty($entry->id)) {
270 return '';
271 }
272
273 if (property_exists($entry, $key)) {
274 if ('total_paid' == $key || 'payment_total' == $key) {
275 return round($entry->{$key} / 100, 2);
276 }
277 if ('payment_method' == $key && 'test' == $key) {
278 return __('Offline', 'fluentform');
279 }
280 return $entry->{$key};
281 }
282 if ('admin_view_url' == $key) {
283 return admin_url('admin.php?page=fluent_forms&route=entries&form_id=' . $entry->form_id . '#/entries/' . $entry->id);
284 } elseif (false !== strpos($key, 'meta.')) {
285 $metaKey = substr($key, strlen('meta.'));
286 $data = App\Helpers\Helper::getSubmissionMeta($entry->id, $metaKey);
287 if (! is_array($data)) {
288 return $data;
289 }
290 return '';
291 }
292
293 return '';
294 }
295
296 protected static function getOtherData($key)
297 {
298 if (0 === strpos($key, 'date.')) {
299 $format = str_replace('date.', '', $key);
300 return date($format, strtotime(current_time('mysql')));
301 } elseif ('admin_email' == $key) {
302 return get_option('admin_email', false);
303 } elseif ('ip' == $key) {
304 return static::getRequest()->getIp();
305 } elseif ('browser.platform' == $key) {
306 return static::getUserAgent()->getPlatform();
307 } elseif ('browser.name' == $key) {
308 return static::getUserAgent()->getBrowser();
309 } elseif (in_array($key, ['all_data', 'all_data_without_hidden_fields'])) {
310 $formFields = FormFieldsParser::getEntryInputs(static::getForm());
311 if (apply_filters('fluentform_all_data_skip_password_field', __return_true())) {
312 $passwords = FormFieldsParser::getInputsByElementTypes(static::getForm(), ['input_password']);
313 if (is_array($passwords) && ! empty($passwords)) {
314 ArrayHelper::forget($formFields, array_keys($passwords));
315 }
316 }
317
318 $skipHiddenFields = ('all_data_without_hidden_fields' == $key) &&
319 apply_filters('fluentform_all_data_without_hidden_fields', __return_true());
320
321 if ($skipHiddenFields) {
322 $hiddenFields = FormFieldsParser::getInputsByElementTypes(static::getForm(), ['input_hidden']);
323 if (is_array($hiddenFields) && ! empty($hiddenFields)) {
324 ArrayHelper::forget($formFields, array_keys($hiddenFields));
325 }
326 }
327
328 $inputLabels = FormFieldsParser::getAdminLabels(static::getForm(), $formFields);
329 $response = FormDataParser::parseFormSubmission(static::getEntry(), static::getForm(), $formFields, true);
330
331 $html = '<table class="ff_all_data" width="600" cellpadding="0" cellspacing="0"><tbody>';
332 foreach ($inputLabels as $key => $label) {
333 if (array_key_exists($key, $response->user_inputs) && '' !== ArrayHelper::get($response->user_inputs, $key)) {
334 $data = ArrayHelper::get($response->user_inputs, $key);
335 if (is_array($data) || is_object($data)) {
336 continue;
337 }
338 $html .= '<tr class="field-label"><th style="padding: 6px 12px; background-color: #f8f8f8; text-align: left;"><strong>' . $label . '</strong></th></tr><tr class="field-value"><td style="padding: 6px 12px 12px 12px;">' . $data . '</td></tr>';
339 }
340 }
341 $html .= '</tbody></table>';
342 return apply_filters('fluentform_all_data_shortcode_html', $html, $formFields, $inputLabels, $response);
343 } elseif ('http_referer' === $key) {
344 return wp_get_referer();
345 } elseif (0 === strpos($key, 'pdf.download_link.')) {
346 return apply_filters('fluentform_shortcode_parser_callback_pdf.download_link.public', $key, self::getInstance());
347 }
348
349 $groups = explode('.', $key);
350 if (count($groups) > 1) {
351 $group = array_shift($groups);
352 $property = implode('.', $groups);
353 $handlerValue = apply_filters('fluentform_smartcode_group_' . $group, $property, self::getInstance());
354 if ($handlerValue != $property) {
355 return $handlerValue;
356 }
357 }
358 // This fallback actually
359 $handlerValue = apply_filters('fluentform_shortcode_parser_callback_' . $key, '{' . $key . '}', self::getInstance());
360
361 if ($handlerValue) {
362 return $handlerValue;
363 }
364 return '';
365 }
366
367 public static function getForm()
368 {
369 if (! is_object(static::$form)) {
370 static::$form = wpFluent()->table('fluentform_forms')->find(static::$form);
371 }
372
373 return static::$form;
374 }
375
376 public static function getEntry()
377 {
378 if (! is_object(static::$entry)) {
379 static::$entry = wpFluent()->table('fluentform_submissions')->find(static::$entry);
380 }
381
382 return static::$entry;
383 }
384
385 protected static function getRequest()
386 {
387 return App::make('request');
388 }
389
390 protected static function getUserAgent()
391 {
392 if (is_null(static::$browser)) {
393 static::$browser = new Browser();
394 }
395 return static::$browser;
396 }
397
398 public static function getInstance()
399 {
400 static $instance;
401 if ($instance) {
402 return $instance;
403 }
404 $instance = new static();
405 return $instance;
406 }
407
408 public static function getInputs()
409 {
410 return static::$store['original_inputs'];
411 }
412
413 public static function resetData()
414 {
415 self::$form = null;
416 self::$entry = null;
417 self::$browser = null;
418 self::$formFields = null;
419
420 FormFieldsParser::resetData();
421 FormDataParser::resetData();
422 }
423 }
424