PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.9
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.9
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.9, at app/Services/FormBuilder/EditorShortcodeParser.php

406 lines 12.3 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 (null === $value || '' === $value) {
173 return '';
174 }
175
176 return esc_attr(Helper::flattenRequestValue($value));
177 }
178
179 /**
180 * Parse the curly braced shortcode into array
181 *
182 * @param string $value
183 *
184 * @return mixed
185 */
186 public static function parseValue($value)
187 {
188 if (!is_array($value)) {
189 return preg_split(
190 '/{(.*?)}/',
191 $value,
192 -1,
193 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
194 );
195 }
196
197 return $value;
198 }
199
200 /**
201 * Declare all parsers and must be [private] static methods
202 */
203
204 /**
205 * Parse loggedin user properties
206 *
207 * @param string $value
208 *
209 * @return string
210 */
211 private static function parseUserProperties($value, $form = null)
212 {
213 if ($user = wp_get_current_user()) {
214 $prop = substr(str_replace(['{', '}'], '', $value), 5);
215
216 if (false !== strpos($prop, 'meta.')) {
217 $metaKey = substr($prop, strlen('meta.'));
218 $metaKey = sanitize_text_field($metaKey);
219 if (empty($metaKey)) {
220 return '';
221 }
222 $userId = $user->ID;
223 $data = get_user_meta($userId, $metaKey, true);
224 $data = Helper::safeUnserialize($data);
225 if (!is_array($data)) {
226 return esc_html($data);
227 }
228 return esc_html(implode(',', $data));
229 }
230
231 return esc_html($user->{$prop});
232 }
233
234 return '';
235 }
236
237 /**
238 * Parse embedded post properties
239 *
240 * @param string $value
241 *
242 * @return string
243 */
244 private static function parsePostProperties($value, $form = null)
245 {
246 global $post;
247 if (!$post) {
248 return '';
249 }
250
251 $key = $prop = substr(str_replace(['{', '}'], '', $value), 11);
252
253 if (false !== strpos($key, 'author.')) {
254 $authorProperty = substr($key, strlen('author.'));
255 $authorId = $post->post_author;
256 if ($authorId) {
257 $data = get_the_author_meta($authorProperty, $authorId);
258 if (!is_array($data)) {
259 return esc_html($data);
260 }
261 }
262 return '';
263 } elseif (false !== strpos($key, 'meta.')) {
264 $metaKey = substr($key, strlen('meta.'));
265 $postId = $post->ID;
266 $data = get_post_meta($postId, $metaKey, true);
267 if (!is_array($data)) {
268 return esc_html($data);
269 }
270 return '';
271 } elseif (false !== strpos($key, 'acf.')) {
272 $metaKey = substr($key, strlen('acf.'));
273 $postId = $post->ID;
274 if (function_exists('get_field')) {
275 $data = get_field($metaKey, $postId, true);
276 if (!is_array($data)) {
277 return esc_html($data);
278 }
279 return '';
280 }
281 }
282
283 if ('permalink' == $prop) {
284 return site_url(esc_attr(urldecode(wpFluentForm('request')->server('REQUEST_URI'))));
285 }
286
287 if (property_exists($post, $prop)) {
288 return esc_html($post->{$prop});
289 }
290 return '';
291 }
292
293 /**
294 * Parse WP Properties
295 *
296 * @param string $value
297 *
298 * @return string
299 */
300 private static function parseWPProperties($value, $form = null)
301 {
302 if ('{wp.admin_email}' == $value) {
303 return esc_html(get_option('admin_email'));
304 }
305 if ('{wp.site_url}' == $value) {
306 return esc_url(site_url());
307 }
308 if ('{wp.site_title}' == $value) {
309 return esc_html(get_option('blogname'));
310 }
311 if ('{http_referer}' == $value) {
312 return esc_url(wp_get_referer());
313 }
314
315 return '';
316 }
317
318 /**
319 * Parse browser/user-agent properties
320 *
321 * @param string $value
322 *
323 * @return string
324 */
325 private static function parseBrowserProperties($value, $form = null)
326 {
327 $browser = new Browser();
328 if ('{browser.name}' == $value) {
329 return esc_html($browser->getBrowser());
330 } elseif ('{browser.platform}' == $value) {
331 return esc_html($browser->getPlatform());
332 }
333
334 return '';
335 }
336
337 /**
338 * Parse ip shortcode
339 *
340 * @param string $value
341 *
342 * @return string
343 */
344 private static function parseIp($value, $form = null)
345 {
346 $rawIp = wpFluentForm('request')->getIp();
347 $ip = sanitize_text_field($rawIp);
348 return $ip ? esc_html($ip) : $value;
349 }
350
351 /**
352 * Parse date shortcode
353 *
354 * @param string $value
355 *
356 * @return string
357 */
358 private static function parseDate($value, $form = null)
359 {
360 $format = substr(str_replace(['}', '{'], '', $value), 5);
361 $date = date($format, strtotime(current_time('mysql')));
362 return $date ? esc_html($date) : '';
363 }
364
365 /**
366 * Parse request query param.
367 *
368 * @param string $value
369 * @param \stdClass $form
370 *
371 * @return string
372 */
373 public static function parseQueryParam($value)
374 {
375 $exploded = explode('.', $value);
376 $param = array_pop($exploded);
377 $value = wpFluentForm('request')->get($param);
378
379 if (!$value) {
380 return '';
381 }
382
383 if (is_array($value)) {
384 return sanitize_textarea_field(implode(', ', $value));
385 }
386
387 return sanitize_textarea_field($value);
388 }
389
390 /**
391 * Generate random a string with prefix
392 *
393 * @param $value
394 *
395 * @return string
396 */
397 public static function parseRandomString($value)
398 {
399 $exploded = explode('.', $value);
400 $prefix = array_pop($exploded);
401 $value = $prefix . uniqid();
402
403 return esc_html(apply_filters('fluentform/shortcode_parser_callback_random_string', $value, $prefix, new static()));
404 }
405 }
406