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

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