PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.8
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.8
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 / EditorShortcodeParser.php

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

410 lines 12.4 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\Helpers\Helper;
6 use FluentForm\App\Services\Browser\Browser;
7
8 class EditorShortcodeParser
9 {
10 /**
11 * Available dynamic short codes
12 * @var null
13 */
14 private static $dynamicShortcodes = null;
15
16 /**
17 * mappings of methods to parse the shortcode
18 * @var array
19 */
20 private static $handlers = [
21 'ip' => 'parseIp',
22 'date.m/d/Y' => 'parseDate',
23 'date.d/m/Y' => 'parseDate',
24
25 'embed_post.ID' => 'parsePostProperties',
26 'embed_post.post_title' => 'parsePostProperties',
27 'embed_post.permalink' => 'parsePostProperties',
28 'http_referer' => 'parseWPProperties',
29
30 'wp.admin_email' => 'parseWPProperties',
31 'wp.site_url' => 'parseWPProperties',
32 'wp.site_title' => 'parseWPProperties',
33
34 'user.ID' => 'parseUserProperties',
35 'user.display_name' => 'parseUserProperties',
36 'user.first_name' => 'parseUserProperties',
37 'user.last_name' => 'parseUserProperties',
38 'user.user_email' => 'parseUserProperties',
39 'user.user_login' => 'parseUserProperties',
40
41 'browser.name' => 'parseBrowserProperties',
42 'browser.platform' => 'parseBrowserProperties',
43
44 'get.param_name' => 'parseRequestParam',
45 'random_string.param_name' => 'parseRandomString',
46 ];
47
48 /**
49 * Filter dynamic shortcodes in input value
50 *
51 * @param string $value
52 *
53 * @return string
54 */
55 public static function filter($value, $form)
56 {
57 if (0 === strpos($value, '{ ')) {
58 // it's the css
59 return $value;
60 }
61
62 if (is_null(static::$dynamicShortcodes)) {
63 static::$dynamicShortcodes = fluentFormEditorShortCodes();
64 }
65
66 $filteredValue = '';
67
68 foreach (static::parseValue($value) as $handler) {
69 if (isset(static::$handlers[$handler])) {
70 return call_user_func_array(
71 [__CLASS__, static::$handlers[$handler]],
72 ['{' . $handler . '}', $form]
73 );
74 }
75
76 if (false !== strpos($handler, 'get.')) {
77 return static::parseRequestParam($handler);
78 }
79 if (false !== strpos($handler, 'random_string.')) {
80 return static::parseRandomString($handler);
81 }
82
83 if (false !== strpos($handler, 'user.')) {
84 $parsedValue = self::parseUserProperties($handler);
85 if (is_array($parsedValue) || is_object($parsedValue)) {
86 return '';
87 }
88 return esc_html($parsedValue);
89 }
90
91 if (false !== strpos($handler, 'date.')) {
92 return esc_html(self::parseDate($handler));
93 }
94
95 if (false !== strpos($handler, 'embed_post.meta.')) {
96 $key = substr(str_replace(['{', '}'], '', $value), 16);
97 global $post;
98 if ($post) {
99 $metaValue = get_post_meta($post->ID, $key, true);
100 if (!is_array($metaValue) && !is_object($metaValue)) {
101 return esc_html($metaValue);
102 }
103 }
104 return '';
105 }
106
107 if (false !== strpos($handler, 'embed_post.')) {
108 return self::parsePostProperties($handler, $form);
109 }
110
111 if (false !== strpos($handler, 'cookie.')) {
112 $scookieProperty = substr($handler, strlen('cookie.'));
113
114 return array_key_exists($scookieProperty, $_COOKIE) ? wp_unslash($_COOKIE[$scookieProperty]) : '';
115 }
116
117 if (false !== strpos($handler, 'dynamic.')) {
118 $dynamicKey = substr($handler, strlen('dynamic.'));
119 // maybe has fallback value
120 $dynamicKey = explode('|', $dynamicKey);
121 $fallBack = '';
122 $ref = '';
123 if (count($dynamicKey) > 1) {
124 $fallBack = $dynamicKey[1];
125 }
126 if (isset($dynamicKey[0])) {
127 $ref = $dynamicKey[0];
128 }
129
130 if ('payment_summary' == $ref) {
131 return fluentform_sanitize_html('<div class="ff_dynamic_value ff_dynamic_payment_summary" data-ref="payment_summary"><div class="ff_payment_summary"></div><div class="ff_payment_summary_fallback">' . $fallBack . '</div></div>');
132 }
133
134 return fluentform_sanitize_html('<span class="ff_dynamic_value" data-ref="' . $ref . '" data-fallback="' . $fallBack . '">' . $fallBack . '</span>');
135 }
136
137 // if it's multi line then just return
138 if (false !== strpos($handler, PHP_EOL)) { // most probably it's a css
139 return '{' . $handler . '}';
140 }
141
142 $handlerArray = explode('.', $handler);
143
144 if (count($handlerArray) > 1) {
145 // it's a grouped handler
146 $group = array_shift($handlerArray);
147 $parsedValue = apply_filters('fluentform_editor_shortcode_callback_group_' . $group, '{' . $handler . '}', $form, $handlerArray);
148 return apply_filters('fluentform/editor_shortcode_callback_group_' . $group, $parsedValue, $form, $handlerArray);
149 }
150
151 $parsedValue = apply_filters('fluentform_editor_shortcode_callback_' . $handler, '{' . $handler . '}', $form);
152 return apply_filters('fluentform/editor_shortcode_callback_' . $handler, $parsedValue, $form);
153 }
154
155 return $filteredValue;
156 }
157
158 /**
159 * Parse request query param.
160 *
161 * @param string $value
162 * @param \stdClass $form
163 *
164 * @return string
165 */
166 public static function parseRequestParam($value)
167 {
168 $exploded = explode('.', $value);
169 $param = array_pop($exploded);
170 $value = wpFluentForm('request')->get($param);
171
172 if (!$value) {
173 return '';
174 }
175
176 if (is_array($value)) {
177 return esc_attr(implode(', ', $value));
178 }
179
180 return esc_attr($value);
181 }
182
183 /**
184 * Parse the curly braced shortcode into array
185 *
186 * @param string $value
187 *
188 * @return mixed
189 */
190 public static function parseValue($value)
191 {
192 if (!is_array($value)) {
193 return preg_split(
194 '/{(.*?)}/',
195 $value,
196 -1,
197 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
198 );
199 }
200
201 return $value;
202 }
203
204 /**
205 * Declare all parsers and must be [private] static methods
206 */
207
208 /**
209 * Parse loggedin user properties
210 *
211 * @param string $value
212 *
213 * @return string
214 */
215 private static function parseUserProperties($value, $form = null)
216 {
217 if ($user = wp_get_current_user()) {
218 $prop = substr(str_replace(['{', '}'], '', $value), 5);
219
220 if (false !== strpos($prop, 'meta.')) {
221 $metaKey = substr($prop, strlen('meta.'));
222 $metaKey = sanitize_text_field($metaKey);
223 if (empty($metaKey)) {
224 return '';
225 }
226 $userId = $user->ID;
227 $data = get_user_meta($userId, $metaKey, true);
228 $data = Helper::safeUnserialize($data);
229 if (!is_array($data)) {
230 return esc_html($data);
231 }
232 return esc_html(implode(',', $data));
233 }
234
235 return esc_html($user->{$prop});
236 }
237
238 return '';
239 }
240
241 /**
242 * Parse embedded post properties
243 *
244 * @param string $value
245 *
246 * @return string
247 */
248 private static function parsePostProperties($value, $form = null)
249 {
250 global $post;
251 if (!$post) {
252 return '';
253 }
254
255 $key = $prop = substr(str_replace(['{', '}'], '', $value), 11);
256
257 if (false !== strpos($key, 'author.')) {
258 $authorProperty = substr($key, strlen('author.'));
259 $authorId = $post->post_author;
260 if ($authorId) {
261 $data = get_the_author_meta($authorProperty, $authorId);
262 if (!is_array($data)) {
263 return esc_html($data);
264 }
265 }
266 return '';
267 } elseif (false !== strpos($key, 'meta.')) {
268 $metaKey = substr($key, strlen('meta.'));
269 $postId = $post->ID;
270 $data = get_post_meta($postId, $metaKey, true);
271 if (!is_array($data)) {
272 return esc_html($data);
273 }
274 return '';
275 } elseif (false !== strpos($key, 'acf.')) {
276 $metaKey = substr($key, strlen('acf.'));
277 $postId = $post->ID;
278 if (function_exists('get_field')) {
279 $data = get_field($metaKey, $postId, true);
280 if (!is_array($data)) {
281 return esc_html($data);
282 }
283 return '';
284 }
285 }
286
287 if ('permalink' == $prop) {
288 return site_url(esc_attr(urldecode(wpFluentForm('request')->server('REQUEST_URI'))));
289 }
290
291 if (property_exists($post, $prop)) {
292 return esc_html($post->{$prop});
293 }
294 return '';
295 }
296
297 /**
298 * Parse WP Properties
299 *
300 * @param string $value
301 *
302 * @return string
303 */
304 private static function parseWPProperties($value, $form = null)
305 {
306 if ('{wp.admin_email}' == $value) {
307 return esc_html(get_option('admin_email'));
308 }
309 if ('{wp.site_url}' == $value) {
310 return esc_url(site_url());
311 }
312 if ('{wp.site_title}' == $value) {
313 return esc_html(get_option('blogname'));
314 }
315 if ('{http_referer}' == $value) {
316 return esc_url(wp_get_referer());
317 }
318
319 return '';
320 }
321
322 /**
323 * Parse browser/user-agent properties
324 *
325 * @param string $value
326 *
327 * @return string
328 */
329 private static function parseBrowserProperties($value, $form = null)
330 {
331 $browser = new Browser();
332 if ('{browser.name}' == $value) {
333 return esc_html($browser->getBrowser());
334 } elseif ('{browser.platform}' == $value) {
335 return esc_html($browser->getPlatform());
336 }
337
338 return '';
339 }
340
341 /**
342 * Parse ip shortcode
343 *
344 * @param string $value
345 *
346 * @return string
347 */
348 private static function parseIp($value, $form = null)
349 {
350 $rawIp = wpFluentForm('request')->getIp();
351 $ip = sanitize_text_field($rawIp);
352 return $ip ? esc_html($ip) : $value;
353 }
354
355 /**
356 * Parse date shortcode
357 *
358 * @param string $value
359 *
360 * @return string
361 */
362 private static function parseDate($value, $form = null)
363 {
364 $format = substr(str_replace(['}', '{'], '', $value), 5);
365 $date = date($format, strtotime(current_time('mysql')));
366 return $date ? esc_html($date) : '';
367 }
368
369 /**
370 * Parse request query param.
371 *
372 * @param string $value
373 * @param \stdClass $form
374 *
375 * @return string
376 */
377 public static function parseQueryParam($value)
378 {
379 $exploded = explode('.', $value);
380 $param = array_pop($exploded);
381 $value = wpFluentForm('request')->get($param);
382
383 if (!$value) {
384 return '';
385 }
386
387 if (is_array($value)) {
388 return sanitize_textarea_field(implode(', ', $value));
389 }
390
391 return sanitize_textarea_field($value);
392 }
393
394 /**
395 * Generate random a string with prefix
396 *
397 * @param $value
398 *
399 * @return string
400 */
401 public static function parseRandomString($value)
402 {
403 $exploded = explode('.', $value);
404 $prefix = array_pop($exploded);
405 $value = $prefix . uniqid();
406
407 return esc_html(apply_filters('fluentform/shortcode_parser_callback_random_string', $value, $prefix, new static()));
408 }
409 }
410