| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Declare common (backend|frontend) global functions here |
| 5 |
* but try not to use any global functions unless you need. |
| 6 |
*/ |
| 7 |
|
| 8 |
use FluentForm\App\Modules\Component\BaseComponent as FluentFormComponent; |
| 9 |
use FluentForm\App\Services\FormBuilder\EditorShortCode; |
| 10 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 11 |
|
| 12 |
if (!function_exists('wpFluentForm')) { |
| 13 |
function wpFluentForm($key = null) |
| 14 |
{ |
| 15 |
return FluentForm\App::make($key); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
if (!function_exists('wpFluentFormAddComponent')) { |
| 20 |
function wpFluentFormAddComponent(FluentFormComponent $component) |
| 21 |
{ |
| 22 |
return $component->_init(); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
if (!function_exists('dd')) { |
| 27 |
function dd() |
| 28 |
{ |
| 29 |
foreach (func_get_args() as $value) { |
| 30 |
echo "<pre>"; |
| 31 |
print_r($value); |
| 32 |
echo "</pre><br>"; |
| 33 |
} |
| 34 |
die; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
if (!function_exists('fluentformMix')) { |
| 39 |
/** |
| 40 |
* Get the path to a versioned Mix file. |
| 41 |
* |
| 42 |
* @param string $path |
| 43 |
* @param string $manifestDirectory |
| 44 |
* |
| 45 |
* @return string |
| 46 |
* @throws \Exception |
| 47 |
*/ |
| 48 |
function fluentformMix($path, $manifestDirectory = '') |
| 49 |
{ |
| 50 |
$publicUrl = \FluentForm\App::publicUrl(); |
| 51 |
return $publicUrl . $path; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if (!function_exists('fluentFormSanitizer')) { |
| 56 |
/** |
| 57 |
* Sanitize form inputs recursively. |
| 58 |
* |
| 59 |
* @param $input |
| 60 |
* |
| 61 |
* @return string $input |
| 62 |
*/ |
| 63 |
function fluentFormSanitizer($input, $attribute = null, $fields = []) |
| 64 |
{ |
| 65 |
if (is_string($input)) { |
| 66 |
if (ArrayHelper::get($fields, $attribute . '.element') === 'post_content' || ArrayHelper::get($fields, $attribute . '.element') === 'rich_text_input' ) { |
| 67 |
return wp_kses_post($input); |
| 68 |
} elseif (ArrayHelper::get($fields, $attribute . '.element') === 'textarea') { |
| 69 |
$input = sanitize_textarea_field($input); |
| 70 |
} elseif (ArrayHelper::get($fields, $attribute . '.element') === 'input_email') { |
| 71 |
$input = strtolower(sanitize_text_field($input)); |
| 72 |
} elseif (ArrayHelper::get($fields, $attribute . '.element') === 'input_url') { |
| 73 |
$input = sanitize_url($input); |
| 74 |
} else { |
| 75 |
$input = sanitize_text_field($input); |
| 76 |
} |
| 77 |
} elseif (is_array($input)) { |
| 78 |
foreach ($input as $key => &$value) { |
| 79 |
$attribute = $attribute ? $attribute . '[' . $key . ']' : $key; |
| 80 |
|
| 81 |
$value = fluentFormSanitizer($value, $attribute, $fields); |
| 82 |
|
| 83 |
$attribute = null; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return $input; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if (!function_exists('fluentFormEditorShortCodes')) { |
| 92 |
function fluentFormEditorShortCodes() |
| 93 |
{ |
| 94 |
return apply_filters('fluentform_editor_shortcodes', [ |
| 95 |
EditorShortCode::getGeneralShortCodes() |
| 96 |
]); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if (!function_exists('fluentFormGetAllEditorShortCodes')) { |
| 101 |
function fluentFormGetAllEditorShortCodes($form) |
| 102 |
{ |
| 103 |
return apply_filters( |
| 104 |
'fluentform_all_editor_shortcodes', |
| 105 |
EditorShortCode::getShortCodes($form), |
| 106 |
$form |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if (!function_exists('fluentImplodeRecursive')) { |
| 112 |
/** |
| 113 |
* Recursively implode a multi-dimentional array |
| 114 |
* @param string $glue |
| 115 |
* @param array $array |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
function fluentImplodeRecursive($glue, array $array) |
| 119 |
{ |
| 120 |
$fn = function ($glue, array $array) use (&$fn) { |
| 121 |
$result = ''; |
| 122 |
foreach ($array as $item) { |
| 123 |
if (is_array($item)) { |
| 124 |
$result .= $fn($glue, $item); |
| 125 |
} else { |
| 126 |
$result .= $glue . $item; |
| 127 |
} |
| 128 |
} |
| 129 |
return $result; |
| 130 |
}; |
| 131 |
|
| 132 |
return ltrim($fn($glue, $array), $glue); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
function fluentform_get_active_theme_slug() |
| 138 |
{ |
| 139 |
if ($ins = get_option('_ff_ins_by')) { |
| 140 |
return sanitize_text_field($ins); |
| 141 |
} |
| 142 |
|
| 143 |
if (defined('TEMPLATELY_FILE')) { |
| 144 |
return 'templately'; |
| 145 |
} |
| 146 |
return get_option('template'); |
| 147 |
} |
| 148 |
|
| 149 |
if (!function_exists('getFluentFormCountryList')) { |
| 150 |
function getFluentFormCountryList() |
| 151 |
{ |
| 152 |
static $countries = null; |
| 153 |
if (is_null($countries)) { |
| 154 |
$countries = require( |
| 155 |
FluentForm\App::appPath('/Services/FormBuilder/CountryNames.php') |
| 156 |
); |
| 157 |
} |
| 158 |
return $countries; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if (!function_exists('fluentFormWasSubmitted')) { |
| 163 |
function fluentFormWasSubmitted($action = 'fluentform_submit') |
| 164 |
{ |
| 165 |
return wpFluentForm('request')->get('action') == $action; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if (!function_exists('isWpAsyncRequest')) { |
| 170 |
function isWpAsyncRequest($action) |
| 171 |
{ |
| 172 |
return strpos(wpFluentForm('request')->get('action'), $action) !== false; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if (!function_exists('fluentFormIsHandlingSubmission')) { |
| 177 |
function fluentFormIsHandlingSubmission() |
| 178 |
{ |
| 179 |
$status = fluentFormWasSubmitted() || isWpAsyncRequest('fluentform_async_request'); |
| 180 |
return apply_filters('fluentform_is_handling_submission', $status); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
function fluentform_mb_strpos($haystack, $needle) |
| 185 |
{ |
| 186 |
if (function_exists('mb_strpos')) { |
| 187 |
return mb_strpos($haystack, $needle); |
| 188 |
} |
| 189 |
return strpos($haystack, $needle); |
| 190 |
} |
| 191 |
|
| 192 |
function fluentFormHandleScheduledTasks() |
| 193 |
{ |
| 194 |
// Let's run the feed actions |
| 195 |
$handler = new \FluentForm\App\Services\WPAsync\FluentFormAsyncRequest(wpFluentForm()); |
| 196 |
$handler->processActions(); |
| 197 |
|
| 198 |
$rand = mt_rand(1, 10); |
| 199 |
if ($rand >= 7) { |
| 200 |
do_action('fluentform_maybe_scheduled_jobs'); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
function fluentFormHandleScheduledEmailReport() |
| 205 |
{ |
| 206 |
\FluentForm\App\Services\Scheduler\Scheduler::processEmailReport(); |
| 207 |
} |
| 208 |
|
| 209 |
function fluentform_upgrade_url() |
| 210 |
{ |
| 211 |
return 'https://fluentforms.com/pricing/?utm_source=plugin&utm_medium=wp_install&utm_campaign=ff_upgrade&theme_style=' . fluentform_get_active_theme_slug(); |
| 212 |
} |
| 213 |
|
| 214 |
function fluentform_integrations_url() |
| 215 |
{ |
| 216 |
return 'https://fluentforms.com/integration/?utm_source=plugin&utm_medium=wp_install&utm_campaign=ff_upgrade&theme_style=' . fluentform_get_active_theme_slug(); |
| 217 |
} |
| 218 |
|
| 219 |
function fluentFormApi($module = 'forms') |
| 220 |
{ |
| 221 |
if ($module == 'forms') { |
| 222 |
return (new \FluentForm\App\Api\Form()); |
| 223 |
} elseif ($module == 'submissions') { |
| 224 |
return (new \FluentForm\App\Api\Submission()); |
| 225 |
} |
| 226 |
|
| 227 |
throw new \Exception('No Module found with name '. $module); |
| 228 |
} |
| 229 |
|
| 230 |
function fluentFormGetRandomPhoto() |
| 231 |
{ |
| 232 |
$photos = [ |
| 233 |
'demo_1.jpg', |
| 234 |
'demo_2.jpg', |
| 235 |
'demo_3.jpg', |
| 236 |
'demo_4.jpg', |
| 237 |
'demo_5.jpg' |
| 238 |
]; |
| 239 |
|
| 240 |
$selected = array_rand($photos, 1); |
| 241 |
|
| 242 |
$photoName = $photos[$selected]; |
| 243 |
|
| 244 |
return fluentformMix('img/conversational/' . $photoName); |
| 245 |
} |
| 246 |
|
| 247 |
if (! function_exists('fluentFormRender')) { |
| 248 |
function fluentFormRender($atts) |
| 249 |
{ |
| 250 |
$shortcodeDefaults = array( |
| 251 |
'id' => null, |
| 252 |
'title' => null, |
| 253 |
'css_classes' => '', |
| 254 |
'permission' => '', |
| 255 |
'type' => 'classic', |
| 256 |
'permission_message' => __('Sorry, You do not have permission to view this form', 'fluentform') |
| 257 |
); |
| 258 |
$atts = shortcode_atts($shortcodeDefaults, $atts); |
| 259 |
|
| 260 |
return (new \FluentForm\App\Modules\Component\Component(wpFluentForm()))->renderForm($atts); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Print internal content (not user input) without escaping. |
| 266 |
*/ |
| 267 |
function fluentFormPrintUnescapedInternalString($string) |
| 268 |
{ |
| 269 |
echo $string; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
function fluentform_options_sanitize($options) |
| 274 |
{ |
| 275 |
$maps = [ |
| 276 |
'label' => 'wp_kses_post', |
| 277 |
'value' => 'sanitize_text_field', |
| 278 |
'image' => 'sanitize_url', |
| 279 |
'calc_value' => 'sanitize_text_field' |
| 280 |
]; |
| 281 |
|
| 282 |
$mapKeys = array_keys($maps); |
| 283 |
|
| 284 |
foreach ($options as $optionIndex => $option) { |
| 285 |
$attributes = array_filter(\FluentForm\Framework\Helpers\ArrayHelper::only($option, $mapKeys)); |
| 286 |
foreach ($attributes as $key => $value) { |
| 287 |
$options[$optionIndex][$key] = call_user_func($maps[$key], $value); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
return $options; |
| 292 |
} |
| 293 |
|
| 294 |
function fluentform_sanitize_html($html) |
| 295 |
{ |
| 296 |
if (!$html) { |
| 297 |
return $html; |
| 298 |
} |
| 299 |
|
| 300 |
$tags = wp_kses_allowed_html('post'); |
| 301 |
$tags['style'] = [ |
| 302 |
'types' => [], |
| 303 |
]; |
| 304 |
// iframe |
| 305 |
$tags['iframe'] = [ |
| 306 |
'width' => [], |
| 307 |
'height' => [], |
| 308 |
'src' => [], |
| 309 |
'srcdoc' => [], |
| 310 |
'title' => [], |
| 311 |
'frameborder' => [], |
| 312 |
'allow' => [], |
| 313 |
'class' => [], |
| 314 |
'id' => [], |
| 315 |
'allowfullscreen' => [], |
| 316 |
'style' => [], |
| 317 |
]; |
| 318 |
//button |
| 319 |
$tags['button']['onclick'] = []; |
| 320 |
|
| 321 |
//svg |
| 322 |
if (empty($tags['svg'])) { |
| 323 |
$svg_args = array( |
| 324 |
'svg' => array( |
| 325 |
'class' => true, |
| 326 |
'aria-hidden' => true, |
| 327 |
'aria-labelledby' => true, |
| 328 |
'role' => true, |
| 329 |
'xmlns' => true, |
| 330 |
'width' => true, |
| 331 |
'height' => true, |
| 332 |
'viewbox' => true, // <= Must be lower case! |
| 333 |
), |
| 334 |
'g' => array('fill' => true), |
| 335 |
'title' => array('title' => true), |
| 336 |
'path' => array( |
| 337 |
'd' => true, |
| 338 |
'fill' => true, |
| 339 |
) |
| 340 |
); |
| 341 |
$tags = array_merge($tags, $svg_args); |
| 342 |
} |
| 343 |
|
| 344 |
$tags = apply_filters('fluentform_allowed_html_tags', $tags); |
| 345 |
|
| 346 |
return wp_kses($html, $tags); |
| 347 |
} |
| 348 |
|
| 349 |
if (!function_exists('fluentform_backend_sanitizer')) { |
| 350 |
/** |
| 351 |
* Sanitize inputs recursively. |
| 352 |
* |
| 353 |
* @param array $input |
| 354 |
* @param array $sanitizeMap |
| 355 |
* @return array $input |
| 356 |
*/ |
| 357 |
function fluentform_backend_sanitizer($array, $sanitizeMap = []) |
| 358 |
{ |
| 359 |
foreach ($array as $key => &$value) { |
| 360 |
if (is_array($value)) { |
| 361 |
$value = fluentform_backend_sanitizer($value, $sanitizeMap); |
| 362 |
} else { |
| 363 |
$method = ArrayHelper::get($sanitizeMap, $key); |
| 364 |
|
| 365 |
if (is_callable($method)) { |
| 366 |
$value = call_user_func($method, $value); |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
return apply_filters('fluent_backend_sanitized_values', $array); |
| 372 |
} |
| 373 |
} |
| 374 |
|