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