| 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 |
return $user->{$prop}; |
| 168 |
} |
| 169 |
|
| 170 |
return ''; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Parse loggedin user properties |
| 175 |
* @param string $value |
| 176 |
* @return string |
| 177 |
*/ |
| 178 |
private static function parsePostProperties($value, $form = null) |
| 179 |
{ |
| 180 |
global $post; |
| 181 |
if (!$post) { |
| 182 |
return ''; |
| 183 |
} |
| 184 |
|
| 185 |
$prop = substr(str_replace(['{', '}'], '', $value), 11); |
| 186 |
if ($prop == 'permalink') { |
| 187 |
return htmlspecialchars(site_url(wp_unslash($_SERVER['REQUEST_URI']))); |
| 188 |
} |
| 189 |
if (property_exists($post, $prop)) { |
| 190 |
return $post->{$prop}; |
| 191 |
} |
| 192 |
return ''; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
/** |
| 197 |
* Parse WP Properties |
| 198 |
* @param string $value |
| 199 |
* @return string |
| 200 |
*/ |
| 201 |
private static function parseWPProperties($value, $form = null) |
| 202 |
{ |
| 203 |
if ($value == '{wp.admin_email}') { |
| 204 |
return get_option('admin_email'); |
| 205 |
} |
| 206 |
if ($value == '{wp.site_url}') { |
| 207 |
return site_url(); |
| 208 |
} |
| 209 |
if ($value == '{wp.site_title}') { |
| 210 |
return get_option('blogname'); |
| 211 |
} |
| 212 |
if ($value == '{http_referer}') { |
| 213 |
return wp_get_referer(); |
| 214 |
} |
| 215 |
|
| 216 |
return ''; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Parse browser/user-agent properties |
| 221 |
* @param string $value |
| 222 |
* @return string |
| 223 |
*/ |
| 224 |
private static function parseBrowserProperties($value, $form = null) |
| 225 |
{ |
| 226 |
$browser = new Browser; |
| 227 |
if ($value == '{browser.name}') { |
| 228 |
return $browser->getBrowser(); |
| 229 |
} elseif ($value == '{browser.platform}') { |
| 230 |
return $browser->getPlatform(); |
| 231 |
} |
| 232 |
|
| 233 |
return ''; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Parse ip shortcode |
| 238 |
* @param string $value |
| 239 |
* @return string |
| 240 |
*/ |
| 241 |
private static function parseIp($value, $form = null) |
| 242 |
{ |
| 243 |
$ip = Request::getIp(); |
| 244 |
return $ip ? $ip : $value; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Parse date shortcode |
| 249 |
* @param string $value |
| 250 |
* @return string |
| 251 |
*/ |
| 252 |
private static function parseDate($value, $form = null) |
| 253 |
{ |
| 254 |
$format = substr(str_replace(['}', '{'], '', $value), 5); |
| 255 |
$date = wp_date($format); |
| 256 |
return $date ? $date : ''; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Parse request query param. |
| 261 |
* |
| 262 |
* @param string $value |
| 263 |
* @param \stdClass $form |
| 264 |
* @return string |
| 265 |
*/ |
| 266 |
public static function parseQueryParam($value) |
| 267 |
{ |
| 268 |
$exploded = explode('.', $value); |
| 269 |
$param = array_pop($exploded); |
| 270 |
$value = $_REQUEST[$param]; |
| 271 |
if(is_array($value)) { |
| 272 |
return ''; |
| 273 |
} |
| 274 |
return isset($_REQUEST[$param]) ? wp_kses_post($value) : ''; |
| 275 |
} |
| 276 |
} |
| 277 |
|