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

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