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