| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Helpers; |
| 4 |
|
| 5 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 6 |
|
| 7 |
class Helper |
| 8 |
{ |
| 9 |
static $tabIndex = 0; |
| 10 |
|
| 11 |
static $formInstance = 0; |
| 12 |
|
| 13 |
static $loadedForms = []; |
| 14 |
|
| 15 |
static $tabIndexStatus = 'na'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Sanitize form inputs recursively. |
| 19 |
* |
| 20 |
* @param $input |
| 21 |
* |
| 22 |
* @return string $input |
| 23 |
*/ |
| 24 |
public static function sanitizer($input, $attribute = null, $fields = []) |
| 25 |
{ |
| 26 |
if (is_string($input)) { |
| 27 |
if (ArrayHelper::get($fields, $attribute . '.element') === 'textarea') { |
| 28 |
$input = sanitize_textarea_field($input); |
| 29 |
} else { |
| 30 |
$input = sanitize_text_field($input); |
| 31 |
} |
| 32 |
} elseif (is_array($input)) { |
| 33 |
foreach ($input as $key => &$value) { |
| 34 |
$attribute = $attribute ? $attribute . '[' . $key . ']' : $key; |
| 35 |
|
| 36 |
$value = Helper::sanitizer($value, $attribute, $fields); |
| 37 |
|
| 38 |
$attribute = null; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return $input; |
| 43 |
} |
| 44 |
|
| 45 |
public static function makeMenuUrl($page = 'fluent_forms_settings', $component = null) |
| 46 |
{ |
| 47 |
$baseUrl = admin_url('admin.php?page=' . $page); |
| 48 |
|
| 49 |
$hash = ArrayHelper::get($component, 'hash', ''); |
| 50 |
if ($hash) { |
| 51 |
$baseUrl = $baseUrl . '#' . $hash; |
| 52 |
} |
| 53 |
|
| 54 |
$query = ArrayHelper::get($component, 'query'); |
| 55 |
|
| 56 |
if ($query) { |
| 57 |
$paramString = http_build_query($query); |
| 58 |
if ($hash) { |
| 59 |
$baseUrl .= '?' . $paramString; |
| 60 |
} else { |
| 61 |
$baseUrl .= '&' . $paramString; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return $baseUrl; |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
public static function getHtmlElementClass($value1, $value2, $class = 'active', $default = '') |
| 70 |
{ |
| 71 |
return $value1 === $value2 ? $class : $default; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Determines if the given string is a valid json. |
| 76 |
* |
| 77 |
* @param $string |
| 78 |
* |
| 79 |
* @return bool |
| 80 |
*/ |
| 81 |
public static function isJson($string) |
| 82 |
{ |
| 83 |
json_decode($string); |
| 84 |
|
| 85 |
return json_last_error() === JSON_ERROR_NONE; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
public static function isSlackEnabled() |
| 90 |
{ |
| 91 |
$globalModules = get_option('fluentform_global_modules_status'); |
| 92 |
return $globalModules && isset($globalModules['slack']) && $globalModules['slack'] == 'yes'; |
| 93 |
} |
| 94 |
|
| 95 |
public static function getEntryStatuses($form_id = false) |
| 96 |
{ |
| 97 |
$statuses = apply_filters('fluentform_entry_statuses', [ |
| 98 |
'unread' => 'Unread', |
| 99 |
'read' => 'Read' |
| 100 |
], $form_id); |
| 101 |
$statuses['trashed'] = 'Trashed'; |
| 102 |
return $statuses; |
| 103 |
} |
| 104 |
|
| 105 |
public static function getReportableInputs() |
| 106 |
{ |
| 107 |
return apply_filters('fluentform_reportable_inputs', [ |
| 108 |
'select', |
| 109 |
'input_radio', |
| 110 |
'input_checkbox', |
| 111 |
'ratings', |
| 112 |
'net_promoter', |
| 113 |
'select_country', |
| 114 |
'net_promoter_score' |
| 115 |
]); |
| 116 |
} |
| 117 |
|
| 118 |
public static function getSubFieldReportableInputs() |
| 119 |
{ |
| 120 |
return apply_filters('fluentform_subfield_reportable_inputs', [ |
| 121 |
'tabular_grid' |
| 122 |
]); |
| 123 |
} |
| 124 |
|
| 125 |
public static function getFormMeta($formId, $metaKey, $default = '') |
| 126 |
{ |
| 127 |
$meta = wpFluent()->table('fluentform_form_meta') |
| 128 |
->where('meta_key', $metaKey) |
| 129 |
->where('form_id', $formId) |
| 130 |
->first(); |
| 131 |
|
| 132 |
if (!$meta || !$meta->value) { |
| 133 |
return $default; |
| 134 |
} |
| 135 |
|
| 136 |
$metaValue = $meta->value; |
| 137 |
// decode the JSON data |
| 138 |
$result = json_decode($metaValue, true); |
| 139 |
|
| 140 |
if (json_last_error() == JSON_ERROR_NONE) { |
| 141 |
return $result; |
| 142 |
} |
| 143 |
return $metaValue; |
| 144 |
} |
| 145 |
|
| 146 |
public static function setFormMeta($formId, $metaKey, $value) |
| 147 |
{ |
| 148 |
$meta = wpFluent()->table('fluentform_form_meta') |
| 149 |
->where('meta_key', $metaKey) |
| 150 |
->where('form_id', $formId) |
| 151 |
->first(); |
| 152 |
|
| 153 |
if (!$meta) { |
| 154 |
$insetid = wpFluent()->table('fluentform_form_meta') |
| 155 |
->insert([ |
| 156 |
'meta_key' => $metaKey, |
| 157 |
'form_id' => $formId, |
| 158 |
'value' => json_encode($value) |
| 159 |
]); |
| 160 |
return $insetid; |
| 161 |
|
| 162 |
} else { |
| 163 |
|
| 164 |
if (is_array($value) || is_object($value)) { |
| 165 |
$value = json_encode($value); |
| 166 |
} |
| 167 |
|
| 168 |
wpFluent()->table('fluentform_form_meta') |
| 169 |
->where('id', $meta->id) |
| 170 |
->update([ |
| 171 |
'value' => $value |
| 172 |
]); |
| 173 |
} |
| 174 |
|
| 175 |
return $meta->id; |
| 176 |
} |
| 177 |
|
| 178 |
public static function getSubmissionMeta($submissionId, $metaKey, $default = false) |
| 179 |
{ |
| 180 |
$meta = wpFluent()->table('fluentform_submission_meta') |
| 181 |
->where('response_id', $submissionId) |
| 182 |
->where('meta_key', $metaKey) |
| 183 |
->first(); |
| 184 |
|
| 185 |
if ($meta && $meta->value) { |
| 186 |
return maybe_unserialize($meta->value); |
| 187 |
} |
| 188 |
|
| 189 |
return $default; |
| 190 |
} |
| 191 |
|
| 192 |
public static function setSubmissionMeta($submissionId, $metaKey, $value, $formId = false) |
| 193 |
{ |
| 194 |
$value = maybe_serialize($value); |
| 195 |
|
| 196 |
// check if submission exist |
| 197 |
$meta = wpFluent()->table('fluentform_submission_meta') |
| 198 |
->where('response_id', $submissionId) |
| 199 |
->where('meta_key', $metaKey) |
| 200 |
->first(); |
| 201 |
|
| 202 |
if($meta) { |
| 203 |
wpFluent()->table('fluentform_submission_meta') |
| 204 |
->where('id', $meta->id) |
| 205 |
->insert([ |
| 206 |
'value' => $value, |
| 207 |
'updated_at' => current_time('mysql') |
| 208 |
]); |
| 209 |
return $meta->id; |
| 210 |
} |
| 211 |
|
| 212 |
if(!$formId) { |
| 213 |
$submission = wpFluent()->table('fluentform_submissions') |
| 214 |
->find($submissionId); |
| 215 |
if($submission) { |
| 216 |
$formId = $submission->form_id; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return wpFluent()->table('fluentform_submission_meta') |
| 221 |
->insert([ |
| 222 |
'response_id' => $submissionId, |
| 223 |
'form_id' => $formId, |
| 224 |
'meta_key' => $metaKey, |
| 225 |
'value' => $value, |
| 226 |
'created_at' => current_time('mysql'), |
| 227 |
'updated_at' => current_time('mysql') |
| 228 |
]); |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
public static function isEntryAutoDeleteEnabled($formId) |
| 233 |
{ |
| 234 |
$settings = wpFluent()->table('fluentform_form_meta') |
| 235 |
->where('form_id', $formId) |
| 236 |
->where('meta_key', 'formSettings') |
| 237 |
->first(); |
| 238 |
|
| 239 |
if (!$settings) { |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
$formSettings = json_decode($settings->value, true); |
| 244 |
|
| 245 |
if ($formSettings && ArrayHelper::get($formSettings, 'delete_entry_on_submission') == 'yes') { |
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
public static function formExtraCssClass($form) |
| 253 |
{ |
| 254 |
if(!$form->settings) { |
| 255 |
$settings = wpFluent()->table('fluentform_form_meta') |
| 256 |
->where('form_id', $form->id) |
| 257 |
->where('meta_key', 'formSettings') |
| 258 |
->first(); |
| 259 |
|
| 260 |
$formSettings = json_decode($settings->value, true); |
| 261 |
} else { |
| 262 |
$formSettings = $form->settings; |
| 263 |
} |
| 264 |
|
| 265 |
if (!$formSettings) { |
| 266 |
return ''; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
if ($formSettings && $extraClass = ArrayHelper::get($formSettings, 'form_extra_css_class')) { |
| 271 |
return esc_attr($extraClass); |
| 272 |
} |
| 273 |
|
| 274 |
return ''; |
| 275 |
} |
| 276 |
|
| 277 |
public static function getNextTabIndex($increment = 1) |
| 278 |
{ |
| 279 |
if(self::isTabIndexEnabled()) { |
| 280 |
static::$tabIndex += $increment; |
| 281 |
return static::$tabIndex; |
| 282 |
} |
| 283 |
return ''; |
| 284 |
} |
| 285 |
|
| 286 |
public static function getFormInstaceClass($formId) |
| 287 |
{ |
| 288 |
static::$formInstance += 1; |
| 289 |
return 'ff_form_instance_'.$formId.'_'.static::$formInstance; |
| 290 |
} |
| 291 |
|
| 292 |
public static function resetTabIndex() |
| 293 |
{ |
| 294 |
static::$tabIndex = 0; |
| 295 |
} |
| 296 |
|
| 297 |
public static function isFluentAdminPage() |
| 298 |
{ |
| 299 |
$fluentPages = [ |
| 300 |
'fluent_forms', |
| 301 |
'fluent_forms_all_entries', |
| 302 |
'fluent_forms_transfer', |
| 303 |
'fluent_forms_settings', |
| 304 |
'fluent_form_add_ons', |
| 305 |
'fluent_forms_docs', |
| 306 |
'fluent_form_payment_entries' |
| 307 |
]; |
| 308 |
|
| 309 |
$status = true; |
| 310 |
|
| 311 |
if (!isset($_GET['page']) || !in_array($_GET['page'], $fluentPages)) { |
| 312 |
$status = false; |
| 313 |
} |
| 314 |
|
| 315 |
return apply_filters('fluentform_is_admin_page', $status); |
| 316 |
} |
| 317 |
|
| 318 |
public static function getShortCodeIds($content, $tag = 'fluentform', $selector = 'id') { |
| 319 |
|
| 320 |
if (false === strpos($content, '[')) { |
| 321 |
return []; |
| 322 |
} |
| 323 |
|
| 324 |
preg_match_all('/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER); |
| 325 |
if (empty($matches)) { |
| 326 |
return []; |
| 327 |
} |
| 328 |
|
| 329 |
$ids = []; |
| 330 |
|
| 331 |
foreach ($matches as $shortcode) { |
| 332 |
if (count($shortcode) >= 2 && $tag === $shortcode[2]) { |
| 333 |
// Replace braces with empty string. |
| 334 |
$parsedCode = str_replace(['[', ']', '[', ']'], '', $shortcode[0]); |
| 335 |
|
| 336 |
$result = shortcode_parse_atts($parsedCode); |
| 337 |
|
| 338 |
if (!empty($result[$selector])) { |
| 339 |
$ids[$result[$selector]] = $result[$selector]; |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
return $ids; |
| 345 |
} |
| 346 |
|
| 347 |
public static function isTabIndexEnabled() |
| 348 |
{ |
| 349 |
if(static::$tabIndexStatus == 'na') { |
| 350 |
$globalSettings = get_option('_fluentform_global_form_settings'); |
| 351 |
static::$tabIndexStatus = ArrayHelper::get($globalSettings, 'misc.tabIndex') == 'yes'; |
| 352 |
} |
| 353 |
return static::$tabIndexStatus; |
| 354 |
} |
| 355 |
|
| 356 |
public static function isMultiStepForm($formId) |
| 357 |
{ |
| 358 |
$form = wpFluent()->table('fluentform_forms')->find($formId); |
| 359 |
$fields = json_decode($form->form_fields, true); |
| 360 |
|
| 361 |
if(ArrayHelper::get($fields, 'stepsWrapper')) { |
| 362 |
return true; |
| 363 |
} |
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
public static function hasFormElement($formId, $elementName) |
| 368 |
{ |
| 369 |
$form = wpFluent()->table('fluentform_forms')->find($formId); |
| 370 |
$fieldsJson = $form->form_fields; |
| 371 |
return strpos($fieldsJson, '"element":"'.$elementName.'"') != false; |
| 372 |
} |
| 373 |
|
| 374 |
} |