| 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\Framework\Helpers\ArrayHelper; |
| 9 |
use FluentForm\App\Services\FormBuilder\EditorShortCode; |
| 10 |
use FluentForm\App\Modules\Component\BaseComponent as FluentFormComponent; |
| 11 |
|
| 12 |
if (!function_exists('wpFluentForm')) { |
| 13 |
function wpFluentForm($key = null) { |
| 14 |
return FluentForm\App::make($key); |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
if (!function_exists('wpFluentFormAddComponent')) { |
| 19 |
function wpFluentFormAddComponent(FluentFormComponent $component) { |
| 20 |
return $component->_init(); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
if (! function_exists('dd')) { |
| 25 |
function dd() |
| 26 |
{ |
| 27 |
foreach (func_get_args() as $value) { |
| 28 |
echo "<pre>"; |
| 29 |
print_r($value); |
| 30 |
echo "</pre><br>"; |
| 31 |
} |
| 32 |
die; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
if (! function_exists('fluentformMix')) { |
| 37 |
/** |
| 38 |
* Get the path to a versioned Mix file. |
| 39 |
* |
| 40 |
* @param string $path |
| 41 |
* @param string $manifestDirectory |
| 42 |
* |
| 43 |
* @return string |
| 44 |
* @throws \Exception |
| 45 |
*/ |
| 46 |
function fluentformMix($path, $manifestDirectory = '') |
| 47 |
{ |
| 48 |
$env = \FluentForm\App::make('config')->app['env']; |
| 49 |
|
| 50 |
if ($env != 'dev') { |
| 51 |
$publicUrl = \FluentForm\App::publicUrl(); |
| 52 |
|
| 53 |
return $publicUrl.$path; |
| 54 |
} |
| 55 |
|
| 56 |
static $manifests = []; |
| 57 |
|
| 58 |
if (substr($path, 0, 1) !== '/') { |
| 59 |
$path = "/".$path; |
| 60 |
} |
| 61 |
|
| 62 |
if ($manifestDirectory && substr($manifestDirectory, 0, 1) !== '/') { |
| 63 |
$manifestDirectory = "/".$manifestDirectory; |
| 64 |
} |
| 65 |
|
| 66 |
$publicPath = \FluentForm\App::publicPath(); |
| 67 |
if (file_exists($publicPath.'/hot')) { |
| 68 |
return (is_ssl() ? "https" : "http")."://localhost:8080".$path; |
| 69 |
} |
| 70 |
|
| 71 |
$manifestPath = $publicPath.$manifestDirectory.'mix-manifest.json'; |
| 72 |
|
| 73 |
if (! isset($manifests[$manifestPath])) { |
| 74 |
if (! file_exists($manifestPath)) { |
| 75 |
throw new Exception('The Mix manifest does not exist.'); |
| 76 |
} |
| 77 |
|
| 78 |
$manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true); |
| 79 |
} |
| 80 |
|
| 81 |
$manifest = $manifests[$manifestPath]; |
| 82 |
|
| 83 |
if (! isset($manifest[$path])) { |
| 84 |
throw new Exception( |
| 85 |
"Unable to locate Mix file: ".$path.". Please check your ". |
| 86 |
'webpack.mix.js output paths and try again.' |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
return \FluentForm\App::publicUrl($manifestDirectory.$manifest[$path]); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (! function_exists('fluentFormSanitizer')) { |
| 95 |
/** |
| 96 |
* Sanitize form inputs recursively. |
| 97 |
* |
| 98 |
* @param $input |
| 99 |
* |
| 100 |
* @return string $input |
| 101 |
*/ |
| 102 |
function fluentFormSanitizer($input, $attribute = null, $fields = []) |
| 103 |
{ |
| 104 |
if (is_string($input)) { |
| 105 |
if (ArrayHelper::get($fields, $attribute.'.element') === 'post_content') { |
| 106 |
return wp_kses_post($input); |
| 107 |
} else if (ArrayHelper::get($fields, $attribute.'.element') === 'textarea') { |
| 108 |
$input = sanitize_textarea_field($input); |
| 109 |
} else { |
| 110 |
$input = sanitize_text_field($input); |
| 111 |
} |
| 112 |
} elseif (is_array($input)) { |
| 113 |
foreach ($input as $key => &$value) { |
| 114 |
$attribute = $attribute ? $attribute.'['.$key.']' : $key; |
| 115 |
|
| 116 |
$value = fluentFormSanitizer($value, $attribute, $fields); |
| 117 |
|
| 118 |
$attribute = null; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return $input; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if (! function_exists('fluentFormEditorShortCodes')) { |
| 127 |
function fluentFormEditorShortCodes() { |
| 128 |
return apply_filters('fluentform_editor_shortcodes', [ |
| 129 |
EditorShortCode::getGeneralShortCodes() |
| 130 |
]); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if (! function_exists('fluentFormGetAllEditorShortCodes')) { |
| 135 |
function fluentFormGetAllEditorShortCodes($form) { |
| 136 |
return apply_filters( |
| 137 |
'fluentform_all_editor_shortcodes', |
| 138 |
EditorShortCode::getShortCodes($form) |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if (!function_exists('fluentImplodeRecursive')) { |
| 144 |
/** |
| 145 |
* Recursively implode a multi-dimentional array |
| 146 |
* @param string $glue |
| 147 |
* @param array $array |
| 148 |
* @return string |
| 149 |
*/ |
| 150 |
function fluentImplodeRecursive($glue, array $array) { |
| 151 |
$fn = function($glue, array $array) use (&$fn) { |
| 152 |
$result = ''; |
| 153 |
foreach ($array as $item) { |
| 154 |
if(is_array($item)) { |
| 155 |
$result .= $fn($glue, $item); |
| 156 |
} else { |
| 157 |
$result .= $glue . $item; |
| 158 |
} |
| 159 |
} |
| 160 |
return $result; |
| 161 |
}; |
| 162 |
|
| 163 |
return ltrim($fn($glue, $array), $glue); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
if (!function_exists('getFluentFormCountryList')) { |
| 169 |
function getFluentFormCountryList() { |
| 170 |
static $countries = null; |
| 171 |
if (is_null($countries)) { |
| 172 |
$countries = require( |
| 173 |
FluentForm\App::appPath('/Services/FormBuilder/CountryNames.php') |
| 174 |
); |
| 175 |
} |
| 176 |
return $countries; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if (!function_exists('fluentFormWasSubmitted')) { |
| 181 |
function fluentFormWasSubmitted($action = 'fluentform_submit') { |
| 182 |
return wpFluentForm('request')->get('action') == $action; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (!function_exists('isWpAsyncRequest')) { |
| 187 |
function isWpAsyncRequest($action) { |
| 188 |
return strpos(wpFluentForm('request')->get('action'), $action) !== false; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if (!function_exists('fluentFormIsHandlingSubmission')) { |
| 193 |
function fluentFormIsHandlingSubmission() { |
| 194 |
$status = fluentFormWasSubmitted() || isWpAsyncRequest('fluentform_async_request'); |
| 195 |
return apply_filters('fluentform_is_handling_submission', $status); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
function fluentform_mb_strpos($haystack, $needle) { |
| 200 |
if(function_exists('mb_strpos')) { |
| 201 |
return mb_strpos($haystack, $needle); |
| 202 |
} |
| 203 |
return strpos($haystack, $needle); |
| 204 |
} |
| 205 |
|
| 206 |
function fluentFormHandleScheduledTasks() |
| 207 |
{ |
| 208 |
// Let's run the feed actions |
| 209 |
$handler = new \FluentForm\App\Services\WPAsync\FluentFormAsyncRequest(wpFluentForm()); |
| 210 |
$handler->processActions(); |
| 211 |
} |
| 212 |
|
| 213 |
function fluentFormHandleScheduledEmailReport() |
| 214 |
{ |
| 215 |
\FluentForm\App\Services\Scheduler\Scheduler::processEmailReport(); |
| 216 |
} |