| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder; |
| 4 |
|
| 5 |
use FluentForm\App; |
| 6 |
use FluentForm\App\Modules\Form\FormDataParser; |
| 7 |
use FluentForm\App\Modules\Form\FormFieldsParser; |
| 8 |
use FluentForm\App\Services\Browser\Browser; |
| 9 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 10 |
|
| 11 |
class ShortCodeParser |
| 12 |
{ |
| 13 |
protected static $form = null; |
| 14 |
|
| 15 |
protected static $entry = null; |
| 16 |
|
| 17 |
protected static $browser = null; |
| 18 |
|
| 19 |
protected static $formFields = null; |
| 20 |
|
| 21 |
protected static $store = [ |
| 22 |
'inputs' => null, |
| 23 |
'original_inputs' => null, |
| 24 |
'user' => null, |
| 25 |
'post' => null, |
| 26 |
'other' => null, |
| 27 |
'submission' => null |
| 28 |
]; |
| 29 |
|
| 30 |
public static function parse($parsable, $entryId, $data = [], $form = null, $isUrl = false, $providerOrIsHTML = false) |
| 31 |
{ |
| 32 |
try { |
| 33 |
static::setDependencies($entryId, $data, $form); |
| 34 |
|
| 35 |
if (is_array($parsable)) { |
| 36 |
return static::parseShortCodeFromArray($parsable, $isUrl, $providerOrIsHTML); |
| 37 |
} |
| 38 |
|
| 39 |
return static::parseShortCodeFromString($parsable, $isUrl, $providerOrIsHTML); |
| 40 |
|
| 41 |
} catch (\Exception $e) { |
| 42 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 43 |
error_log($e->getTraceAsString()); |
| 44 |
} |
| 45 |
return ''; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
protected static function setDependencies($entry, $data, $form) |
| 50 |
{ |
| 51 |
static::setEntry($entry); |
| 52 |
static::setData($data); |
| 53 |
static::setForm($form); |
| 54 |
} |
| 55 |
|
| 56 |
protected static function setEntry($entry) |
| 57 |
{ |
| 58 |
static::$entry = $entry; |
| 59 |
} |
| 60 |
|
| 61 |
protected static function setdata($data) |
| 62 |
{ |
| 63 |
if (!is_null($data)) { |
| 64 |
static::$store['inputs'] = $data; |
| 65 |
static::$store['original_inputs'] = $data; |
| 66 |
} else { |
| 67 |
$data = json_decode(static::getEntry()->response, true); |
| 68 |
static::$store['inputs'] = $data; |
| 69 |
static::$store['original_inputs'] = $data; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
protected static function setForm($form) |
| 74 |
{ |
| 75 |
if (!is_null($form)) { |
| 76 |
static::$form = $form; |
| 77 |
} else { |
| 78 |
static::$form = static::getEntry()->form_id; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
protected static function parseShortCodeFromArray($parsable, $isUrl = false, $provider = false) |
| 83 |
{ |
| 84 |
foreach ($parsable as $key => $value) { |
| 85 |
if (is_array($value)) { |
| 86 |
$parsable[$key] = static::parseShortCodeFromArray($value, $isUrl, $provider); |
| 87 |
} else { |
| 88 |
$isHtml = false; |
| 89 |
if ($provider) { |
| 90 |
$isHtml = apply_filters('ff_will_return_html', false, $provider, $key); |
| 91 |
} |
| 92 |
$parsable[$key] = static::parseShortCodeFromString($value, $isUrl, $isHtml); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return $parsable; |
| 97 |
} |
| 98 |
|
| 99 |
protected static function parseShortCodeFromString($parsable, $isUrl = false, $isHtml = false) |
| 100 |
{ |
| 101 |
if (!$parsable) { |
| 102 |
return ''; |
| 103 |
} |
| 104 |
return preg_replace_callback('/{+(.*?)}/', function ($matches) use ($isUrl, $isHtml) { |
| 105 |
$value = ''; |
| 106 |
if (strpos($matches[1], 'inputs.') !== false) { |
| 107 |
$formProperty = substr($matches[1], strlen('inputs.')); |
| 108 |
$value = static::getFormData($formProperty, $isHtml); |
| 109 |
} elseif (strpos($matches[1], 'user.') !== false) { |
| 110 |
$userProperty = substr($matches[1], strlen('user.')); |
| 111 |
$value = static::getUserData($userProperty); |
| 112 |
} elseif (strpos($matches[1], 'embed_post.') !== false) { |
| 113 |
$postProperty = substr($matches[1], strlen('embed_post.')); |
| 114 |
$value = static::getPostData($postProperty); |
| 115 |
} elseif (strpos($matches[1], 'wp.') !== false) { |
| 116 |
$wpProperty = substr($matches[1], strlen('wp.')); |
| 117 |
$value = static::getWPData($wpProperty); |
| 118 |
} elseif (strpos($matches[1], 'submission.') !== false) { |
| 119 |
$submissionProperty = substr($matches[1], strlen('submission.')); |
| 120 |
$value = static::getSubmissionData($submissionProperty); |
| 121 |
} elseif (strpos($matches[1], 'cookie.') !== false) { |
| 122 |
$scookieProperty = substr($matches[1], strlen('cookie.')); |
| 123 |
$value = ArrayHelper::get($_COOKIE, $scookieProperty); |
| 124 |
} elseif (strpos($matches[1], 'payment.') !== false) { |
| 125 |
$property = substr($matches[1], strlen('payment.')); |
| 126 |
$value = apply_filters('fluentform_payment_smartcode', '', $property, self::getInstance()); |
| 127 |
} else { |
| 128 |
$value = static::getOtherData($matches[1]); |
| 129 |
} |
| 130 |
|
| 131 |
if (is_array($value)) { |
| 132 |
$value = fluentImplodeRecursive(', ', $value); |
| 133 |
} |
| 134 |
|
| 135 |
if ($isUrl) { |
| 136 |
$value = urlencode($value); |
| 137 |
} |
| 138 |
|
| 139 |
return $value; |
| 140 |
|
| 141 |
}, $parsable); |
| 142 |
} |
| 143 |
|
| 144 |
protected static function getFormData($key, $isHtml = false) |
| 145 |
{ |
| 146 |
if (strpos($key, '.label')) { |
| 147 |
$key = str_replace('.label', '', $key); |
| 148 |
$isHtml = true; |
| 149 |
} |
| 150 |
|
| 151 |
if (strpos($key, '.value')) { |
| 152 |
$key = str_replace('.value', '', $key); |
| 153 |
return ArrayHelper::get(static::$store['original_inputs'], $key); |
| 154 |
} |
| 155 |
|
| 156 |
if (strpos($key, '.') && !isset(static::$store['inputs'][$key])) { |
| 157 |
return ArrayHelper::get( |
| 158 |
static::$store['original_inputs'], $key, '' |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
if (!isset(static::$store['inputs'][$key])) { |
| 163 |
static::$store['inputs'][$key] = ArrayHelper::get( |
| 164 |
static::$store['inputs'], $key, '' |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
if (is_null(static::$formFields)) { |
| 169 |
static::$formFields = FormFieldsParser::getShortCodeInputs( |
| 170 |
static::getForm(), ['admin_label', 'attributes', 'options', 'raw'] |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
$field = ArrayHelper::get(static::$formFields, $key, ''); |
| 175 |
|
| 176 |
|
| 177 |
if (!$field) return ''; |
| 178 |
|
| 179 |
if ($isHtml) { |
| 180 |
return apply_filters( |
| 181 |
'fluentform_response_render_' . $field['element'], |
| 182 |
static::$store['original_inputs'][$key], |
| 183 |
$field, |
| 184 |
static::getForm()->id, |
| 185 |
$isHtml |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
return static::$store['inputs'][$key] = apply_filters( |
| 190 |
'fluentform_response_render_' . $field['element'], |
| 191 |
static::$store['inputs'][$key], |
| 192 |
$field, |
| 193 |
static::getForm()->id, |
| 194 |
$isHtml |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
protected static function getUserData($key) |
| 199 |
{ |
| 200 |
if (is_null(static::$store['user'])) { |
| 201 |
static::$store['user'] = wp_get_current_user(); |
| 202 |
} |
| 203 |
return static::$store['user']->{$key}; |
| 204 |
} |
| 205 |
|
| 206 |
protected static function getPostData($key) |
| 207 |
{ |
| 208 |
if (is_null(static::$store['post'])) { |
| 209 |
$postId = static::$store['inputs']['__fluent_form_embded_post_id']; |
| 210 |
static::$store['post'] = get_post($postId); |
| 211 |
static::$store['post']->permalink = get_the_permalink(static::$store['post']); |
| 212 |
} |
| 213 |
|
| 214 |
if (strpos($key, 'author.') !== false) { |
| 215 |
$authorProperty = substr($key, strlen('author.')); |
| 216 |
$authorId = static::$store['post']->post_author; |
| 217 |
if ($authorId) { |
| 218 |
$data = get_the_author_meta($authorProperty, $authorId); |
| 219 |
if (!is_array($data)) { |
| 220 |
return $data; |
| 221 |
} |
| 222 |
} |
| 223 |
return ''; |
| 224 |
} else if (strpos($key, 'meta.') !== false) { |
| 225 |
$metaKey = substr($key, strlen('meta.')); |
| 226 |
$postId = static::$store['post']->ID; |
| 227 |
$data = get_post_meta($postId, $metaKey, true); |
| 228 |
if (!is_array($data)) { |
| 229 |
return $data; |
| 230 |
} |
| 231 |
return ''; |
| 232 |
} else if (strpos($key, 'acf.') !== false) { |
| 233 |
$metaKey = substr($key, strlen('acf.')); |
| 234 |
$postId = static::$store['post']->ID; |
| 235 |
if (function_exists('get_field')) { |
| 236 |
$data = get_field($metaKey, $postId, true); |
| 237 |
if (!is_array($data)) { |
| 238 |
return $data; |
| 239 |
} |
| 240 |
return ''; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return static::$store['post']->{$key}; |
| 245 |
} |
| 246 |
|
| 247 |
protected static function getWPData($key) |
| 248 |
{ |
| 249 |
if ($key == 'admin_email') { |
| 250 |
return get_option('admin_email'); |
| 251 |
} |
| 252 |
if ($key == 'site_url') { |
| 253 |
return site_url(); |
| 254 |
} |
| 255 |
if ($key == 'site_title') { |
| 256 |
return get_option('blogname'); |
| 257 |
} |
| 258 |
return $key; |
| 259 |
} |
| 260 |
|
| 261 |
protected static function getSubmissionData($key) |
| 262 |
{ |
| 263 |
$entry = static::getEntry(); |
| 264 |
|
| 265 |
if(empty($entry->id)) { |
| 266 |
return ''; |
| 267 |
} |
| 268 |
|
| 269 |
if (property_exists($entry, $key)) { |
| 270 |
if ($key == 'total_paid' || $key == 'payment_total') { |
| 271 |
return round($entry->{$key} / 100, 2); |
| 272 |
} |
| 273 |
if ($key == 'payment_method' && $key == 'test') { |
| 274 |
return __('Offline', 'fluentform'); |
| 275 |
} |
| 276 |
return $entry->{$key}; |
| 277 |
} |
| 278 |
if ($key == 'admin_view_url') { |
| 279 |
return admin_url('admin.php?page=fluent_forms&route=entries&form_id=' . $entry->form_id . '#/entries/' . $entry->id); |
| 280 |
} else if (strpos($key, 'meta.') !== false) { |
| 281 |
$metaKey = substr($key, strlen('meta.')); |
| 282 |
$data = App\Helpers\Helper::getSubmissionMeta($entry->id, $metaKey); |
| 283 |
if (!is_array($data)) { |
| 284 |
return $data; |
| 285 |
} |
| 286 |
return ''; |
| 287 |
} |
| 288 |
|
| 289 |
return ''; |
| 290 |
} |
| 291 |
|
| 292 |
protected static function getOtherData($key) |
| 293 |
{ |
| 294 |
if (strpos($key, 'date.') === 0) { |
| 295 |
$format = str_replace('date.', '', $key); |
| 296 |
return date($format, strtotime(current_time('mysql'))); |
| 297 |
} elseif ($key == 'admin_email') { |
| 298 |
return get_option('admin_email', false); |
| 299 |
} elseif ($key == 'ip') { |
| 300 |
return static::getRequest()->getIp(); |
| 301 |
} elseif ($key == 'browser.platform') { |
| 302 |
return static::getUserAgent()->getPlatform(); |
| 303 |
} elseif ($key == 'browser.name') { |
| 304 |
return static::getUserAgent()->getBrowser(); |
| 305 |
} elseif (in_array($key, ['all_data', 'all_data_without_hidden_fields'])) { |
| 306 |
$formFields = FormFieldsParser::getEntryInputs(static::getForm()); |
| 307 |
if (apply_filters('fluentform_all_data_skip_password_field', __return_true())) { |
| 308 |
$passwords = FormFieldsParser::getInputsByElementTypes(static::getForm(), ['input_password']); |
| 309 |
if (is_array($passwords) && !empty($passwords)) { |
| 310 |
ArrayHelper::forget($formFields, array_keys($passwords)); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
$skipHiddenFields = ($key == 'all_data_without_hidden_fields') && |
| 315 |
apply_filters('fluentform_all_data_without_hidden_fields', __return_true()); |
| 316 |
|
| 317 |
if ($skipHiddenFields) { |
| 318 |
$hiddenFields = FormFieldsParser::getInputsByElementTypes(static::getForm(), ['input_hidden']); |
| 319 |
if (is_array($hiddenFields) && !empty($hiddenFields)) { |
| 320 |
ArrayHelper::forget($formFields, array_keys($hiddenFields)); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
$inputLabels = FormFieldsParser::getAdminLabels(static::getForm(), $formFields); |
| 325 |
$response = FormDataParser::parseFormSubmission(static::getEntry(), static::getForm(), $formFields, true); |
| 326 |
|
| 327 |
$html = '<table class="ff_all_data" width="600" cellpadding="0" cellspacing="0"><tbody>'; |
| 328 |
foreach ($inputLabels as $key => $label) { |
| 329 |
if (array_key_exists($key, $response->user_inputs) && ArrayHelper::get($response->user_inputs, $key) !== '') { |
| 330 |
$data = ArrayHelper::get($response->user_inputs, $key); |
| 331 |
if (is_array($data) || is_object($data)) { |
| 332 |
continue; |
| 333 |
} |
| 334 |
$html .= '<tr class="field-label"><th style="padding: 6px 12px; background-color: #f8f8f8; text-align: left;"><strong>' . $label . '</strong></th></tr><tr class="field-value"><td style="padding: 6px 12px 12px 12px;">' . $data . '</td></tr>'; |
| 335 |
} |
| 336 |
} |
| 337 |
$html .= '</tbody></table>'; |
| 338 |
return apply_filters('fluentform_all_data_shortcode_html', $html, $formFields, $inputLabels, $response); |
| 339 |
} elseif ($key === 'http_referer') { |
| 340 |
return wp_get_referer(); |
| 341 |
} elseif (strpos($key, 'pdf.download_link.') === 0) { |
| 342 |
return apply_filters('fluentform_shortcode_parser_callback_pdf.download_link.public', $key, self::getInstance()); |
| 343 |
} |
| 344 |
|
| 345 |
$groups = explode('.', $key); |
| 346 |
if(count($groups) > 1) { |
| 347 |
$group = array_shift($groups); |
| 348 |
$property = implode('.', $groups); |
| 349 |
$handlerValue = apply_filters('fluentform_smartcode_group_'.$group, $property, self::getInstance()); |
| 350 |
if($handlerValue != $property) { |
| 351 |
return $handlerValue; |
| 352 |
} |
| 353 |
} |
| 354 |
// This fallback actually |
| 355 |
$handlerValue = apply_filters('fluentform_shortcode_parser_callback_' . $key, '{' . $key . '}', self::getInstance()); |
| 356 |
|
| 357 |
if ($handlerValue) { |
| 358 |
return $handlerValue; |
| 359 |
} |
| 360 |
return ''; |
| 361 |
} |
| 362 |
|
| 363 |
public static function getForm() |
| 364 |
{ |
| 365 |
if (!is_object(static::$form)) { |
| 366 |
static::$form = wpFluent()->table('fluentform_forms')->find(static::$form); |
| 367 |
} |
| 368 |
|
| 369 |
return static::$form; |
| 370 |
} |
| 371 |
|
| 372 |
public static function getEntry() |
| 373 |
{ |
| 374 |
if (!is_object(static::$entry)) { |
| 375 |
static::$entry = wpFluent()->table('fluentform_submissions')->find(static::$entry); |
| 376 |
} |
| 377 |
|
| 378 |
return static::$entry; |
| 379 |
} |
| 380 |
|
| 381 |
protected static function getRequest() |
| 382 |
{ |
| 383 |
return App::make('request'); |
| 384 |
} |
| 385 |
|
| 386 |
protected static function getUserAgent() |
| 387 |
{ |
| 388 |
if (is_null(static::$browser)) { |
| 389 |
static::$browser = new Browser(); |
| 390 |
} |
| 391 |
return static::$browser; |
| 392 |
} |
| 393 |
|
| 394 |
public static function getInstance() |
| 395 |
{ |
| 396 |
static $instance; |
| 397 |
if ($instance) { |
| 398 |
return $instance; |
| 399 |
} |
| 400 |
$instance = new static(); |
| 401 |
return $instance; |
| 402 |
} |
| 403 |
|
| 404 |
public static function getInputs() |
| 405 |
{ |
| 406 |
return static::$store['original_inputs']; |
| 407 |
} |
| 408 |
|
| 409 |
public static function resetData() |
| 410 |
{ |
| 411 |
self::$form = null; |
| 412 |
self::$entry = null; |
| 413 |
self::$browser = null; |
| 414 |
self::$formFields = null; |
| 415 |
|
| 416 |
FormFieldsParser::resetData(); |
| 417 |
FormDataParser::resetData(); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|