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

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