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

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