PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.21
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.21
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 3.6.66 All 195 releases
fluentform / app / Services / FormBuilder / ShortCodeParser.php

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

347 lines 11.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\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 return wp_date(str_replace('date.', '', $key));
270 } elseif ($key == 'admin_email') {
271 return get_option('admin_email', false);
272 } elseif ($key == 'ip') {
273 return static::getRequest()->getIp();
274 } elseif ($key == 'browser.platform') {
275 return static::getUserAgent()->getPlatform();
276 } elseif ($key == 'browser.name') {
277 return static::getUserAgent()->getBrowser();
278 } elseif ($key == 'all_data') {
279 $formFields = FormFieldsParser::getEntryInputs(static::getForm());
280 $inputLabels = FormFieldsParser::getAdminLabels(static::getForm(), $formFields);
281 $response = FormDataParser::parseFormSubmission(static::getEntry(), static::getForm(), $formFields, true);
282
283 $html = '<table class="ff_all_data" width="600" cellpadding="0" cellspacing="0"><tbody>';
284 foreach ($inputLabels as $key => $label) {
285 if (array_key_exists($key, $response->user_inputs) && ArrayHelper::get($response->user_inputs, $key)) {
286 $data = ArrayHelper::get($response->user_inputs, $key);
287 if (is_array($data) || is_object($data)) {
288 continue;
289 }
290 $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>';
291 }
292 }
293 $html .= '</tbody></table>';
294 return $html;
295 }
296 // This fallback actually
297 $handlerValue = apply_filters('fluentform_shortcode_parser_callback_' . $key, '{'.$key.'}', self::getInstance());
298
299 if ($handlerValue) {
300 return $handlerValue;
301 }
302 return '';
303 }
304
305 public static function getForm()
306 {
307 if (!is_object(static::$form)) {
308 static::$form = wpFluent()->table('fluentform_forms')->find(static::$form);
309 }
310
311 return static::$form;
312 }
313
314 public static function getEntry()
315 {
316 if (!is_object(static::$entry)) {
317 static::$entry = wpFluent()->table('fluentform_submissions')->find(static::$entry);
318 }
319
320 return static::$entry;
321 }
322
323 protected static function getRequest()
324 {
325 return App::make('request');
326 }
327
328 protected static function getUserAgent()
329 {
330 if (is_null(static::$browser)) {
331 static::$browser = new Browser();
332 }
333 return static::$browser;
334 }
335
336 public static function getInstance()
337 {
338 static $instance;
339 if($instance) {
340 return $instance;
341 }
342 $instance = new static();
343 return $instance;
344 }
345 }
346
347