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

330 lines 9.9 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 } elseif (strpos($handler, 'get.') !== false) {
75 return static::parseQueryParam($handler);
76 } elseif (strpos($handler, 'random_string.') !== false) {
77 return static::parseRandomString($handler);
78 } else if (strpos($handler, 'user.') !== false) {
79 $value = self::parseUserProperties($handler);
80 if (is_array($value) || is_object($value)) {
81 return '';
82 }
83 return $value;
84 } else if (strpos($handler, 'date.') !== false) {
85 return self::parseDate($handler);
86 } else if (strpos($handler, 'embed_post.meta.') !== false) {
87 $key = substr(str_replace(['{', '}'], '', $value), 16);
88 global $post;
89 if ($post) {
90 $value = get_post_meta($post->ID, $key, true);
91 if (!is_array($value) && !is_object($value)) {
92 return $value;
93 }
94 }
95 return '';
96 } else if (strpos($handler, 'embed_post.') !== false) {
97 return self::parsePostProperties($handler, $form);
98 } else if (strpos($handler, 'cookie.') !== false) {
99 $scookieProperty = substr($handler, strlen('cookie.'));
100 return ArrayHelper::get($_COOKIE, $scookieProperty);
101 } else if (strpos($handler, 'dynamic.') !== false) {
102 $dynamicKey = substr($handler, strlen('dynamic.'));
103 // maybe has fallback value
104 $dynamicKey = explode('|', $dynamicKey);
105 $fallBack = '';
106 $ref = '';
107 if (count($dynamicKey) > 1) {
108 $fallBack = $dynamicKey[1];
109 }
110 $ref = $dynamicKey[0];
111
112 if ($ref == 'payment_summary') {
113 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>';
114 }
115
116 return '<span class="ff_dynamic_value" data-ref="' . $ref . '" data-fallback="' . $fallBack . '">' . $fallBack . '</span>';
117 } else {
118 // This can be the css
119 $handlerValue = apply_filters('fluentform_editor_shortcode_callback_' . $handler, '{' . $handler . '}', $form);
120 // In not found then return the original please
121 $filteredValue = $handlerValue;
122 }
123 }
124
125 return $filteredValue;
126 }
127
128 /**
129 * Parse the curly braced shortcode into array
130 * @param string $value
131 * @return mixed
132 */
133 public static function parseValue($value)
134 {
135 if (!is_array($value)) {
136 return preg_split(
137 '/{(.*?)}/',
138 $value,
139 null,
140 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
141 );
142 }
143
144 return $value;
145 }
146
147 /**
148 * Declare all parsers and must be [private] static methods
149 */
150
151 /**
152 * Parse loggedin user properties
153 * @param string $value
154 * @return string
155 */
156 private static function parseUserProperties($value, $form = null)
157 {
158 if ($user = wp_get_current_user()) {
159 $prop = substr(str_replace(['{', '}'], '', $value), 5);
160
161 if (strpos($prop, 'meta.') !== false) {
162 $metaKey = substr($prop, strlen('meta.'));
163 $userId = $user->ID;
164 $data = get_user_meta($userId, $metaKey, true);
165 if (!is_array($data)) {
166 return $data;
167 }
168 return '';
169 }
170
171 return $user->{$prop};
172 }
173
174 return '';
175 }
176
177 /**
178 * Parse embedded post properties
179 * @param string $value
180 * @return string
181 */
182 private static function parsePostProperties($value, $form = null)
183 {
184 global $post;
185 if (!$post) {
186 return '';
187 }
188
189 $key = $prop = substr(str_replace(['{', '}'], '', $value), 11);
190
191 if (strpos($key, 'author.') !== false) {
192 $authorProperty = substr($key, strlen('author.'));
193 $authorId = $post->post_author;
194 if ($authorId) {
195 $data = get_the_author_meta($authorProperty, $authorId);
196 if (!is_array($data)) {
197 return $data;
198 }
199 }
200 return '';
201 } else if (strpos($key, 'meta.') !== false) {
202 $metaKey = substr($key, strlen('meta.'));
203 $postId = $post->ID;
204 $data = get_post_meta($postId, $metaKey, true);
205 if (!is_array($data)) {
206 return $data;
207 }
208 return '';
209 } else if (strpos($key, 'acf.') !== false) {
210 $metaKey = substr($key, strlen('acf.'));
211 $postId = $post->ID;
212 if (function_exists('get_field')) {
213 $data = get_field($metaKey, $postId, true);
214 if (!is_array($data)) {
215 return $data;
216 }
217 return '';
218 }
219 }
220
221 if ($prop == 'permalink') {
222 return htmlspecialchars(site_url(wp_unslash($_SERVER['REQUEST_URI'])));
223 }
224
225 if (property_exists($post, $prop)) {
226 return $post->{$prop};
227 }
228 return '';
229 }
230
231
232 /**
233 * Parse WP Properties
234 * @param string $value
235 * @return string
236 */
237 private static function parseWPProperties($value, $form = null)
238 {
239 if ($value == '{wp.admin_email}') {
240 return get_option('admin_email');
241 }
242 if ($value == '{wp.site_url}') {
243 return site_url();
244 }
245 if ($value == '{wp.site_title}') {
246 return get_option('blogname');
247 }
248 if ($value == '{http_referer}') {
249 return wp_get_referer();
250 }
251
252 return '';
253 }
254
255 /**
256 * Parse browser/user-agent properties
257 * @param string $value
258 * @return string
259 */
260 private static function parseBrowserProperties($value, $form = null)
261 {
262 $browser = new Browser;
263 if ($value == '{browser.name}') {
264 return $browser->getBrowser();
265 } elseif ($value == '{browser.platform}') {
266 return $browser->getPlatform();
267 }
268
269 return '';
270 }
271
272 /**
273 * Parse ip shortcode
274 * @param string $value
275 * @return string
276 */
277 private static function parseIp($value, $form = null)
278 {
279 $ip = Request::getIp();
280 return $ip ? $ip : $value;
281 }
282
283 /**
284 * Parse date shortcode
285 * @param string $value
286 * @return string
287 */
288 private static function parseDate($value, $form = null)
289 {
290 $format = substr(str_replace(['}', '{'], '', $value), 5);
291 $date = date($format, strtotime(current_time('mysql')));
292 return $date ? $date : '';
293 }
294
295 /**
296 * Parse request query param.
297 *
298 * @param string $value
299 * @param \stdClass $form
300 * @return string
301 */
302 public static function parseQueryParam($value)
303 {
304 $exploded = explode('.', $value);
305 $param = array_pop($exploded);
306 if (!isset($_REQUEST[$param])) {
307 return '';
308 }
309 $value = $_REQUEST[$param];
310 if (is_array($value)) {
311 return sanitize_textarea_field(implode(', ', $value));
312 }
313 return sanitize_textarea_field($value);
314 }
315
316 /**
317 * Generate random a string with prefix
318 *
319 * @param $value
320 * @return string
321 */
322 public static function parseRandomString($value)
323 {
324
325 $exploded = explode ('.',$value);
326 $prefix = array_pop($exploded) ;
327 return $prefix.uniqid () ;
328 }
329 }
330