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

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