PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.60
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.60
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 3.6.60, at app/Services/FormBuilder/ShortCodeParser.php

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