admin
3 months ago
field-groups
3 months ago
fields
1 week ago
fields-settings
3 months ago
forms
3 months ago
locations
3 months ago
modules
1 week ago
screens
3 months ago
acfe-deprecated-functions.php
2 years ago
acfe-field-functions.php
3 months ago
acfe-field-group-functions.php
3 months ago
acfe-file-functions.php
1 year ago
acfe-form-functions.php
3 months ago
acfe-helper-array-functions.php
3 months ago
acfe-helper-functions.php
3 months ago
acfe-helper-multi-functions.php
3 months ago
acfe-helper-string-functions.php
3 months ago
acfe-meta-functions.php
3 months ago
acfe-post-functions.php
3 months ago
acfe-screen-functions.php
3 months ago
acfe-template-functions.php
3 months ago
acfe-term-functions.php
3 months ago
acfe-user-functions.php
3 months ago
acfe-wp-functions.php
3 years ago
assets.php
3 months ago
compatibility-acf-5.8.php
4 months ago
compatibility-acf-5.9.php
3 months ago
compatibility-acf-6.5.php
3 months ago
compatibility-acf-6.8.6.php
1 week ago
compatibility.php
3 months ago
field-extend.php
4 months ago
field.php
4 months ago
hooks.php
3 months ago
init.php
3 months ago
local-meta.php
3 years ago
media.php
3 months ago
module-acf.php
3 months ago
module-db.php
3 months ago
module-l10n.php
3 months ago
module-legacy.php
3 years ago
module-manager.php
4 months ago
module-post.php
3 months ago
module-posts.php
4 months ago
module-upgrades.php
3 years ago
module.php
3 months ago
multilang.php
3 months ago
revisions.php
4 months ago
screen.php
3 months ago
settings.php
3 months ago
template-tags.php
3 months ago
third-party.php
3 years ago
upgrades.php
3 months ago
acfe-form-functions.php
510 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * acfe_get_pretty_forms |
| 9 | * |
| 10 | * Similar to acf_get_pretty_post_types() but for ACFE Forms |
| 11 | * Used in the Forms field type |
| 12 | * |
| 13 | * @param array $allowed |
| 14 | * |
| 15 | * @return array |
| 16 | */ |
| 17 | function acfe_get_pretty_forms($allowed = array()){ |
| 18 | |
| 19 | $forms = acfe_get_module('form')->get_items(); |
| 20 | $choices = array(); |
| 21 | |
| 22 | foreach($forms as $item){ |
| 23 | |
| 24 | // fallback to name if empty title |
| 25 | $item['title'] = !empty($item['title']) ? $item['title'] : $item['name']; |
| 26 | |
| 27 | if(empty($allowed) || in_array($item['name'], $allowed, true)){ |
| 28 | $choices[ $item['name'] ] = $item['title']; |
| 29 | } |
| 30 | |
| 31 | } |
| 32 | |
| 33 | return $choices; |
| 34 | |
| 35 | } |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * acfe_get_form_sent |
| 40 | * |
| 41 | * @return array|false |
| 42 | */ |
| 43 | function acfe_get_form_sent(){ |
| 44 | |
| 45 | // check post data |
| 46 | if(!empty($_POST['_acf_form'])){ |
| 47 | |
| 48 | // decode post data |
| 49 | $form = json_decode(acf_decrypt($_POST['_acf_form']), true); |
| 50 | |
| 51 | // validate |
| 52 | if(!empty($form) && is_array($form)){ |
| 53 | return $form; |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
| 58 | return false; |
| 59 | |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * acfe_is_form_success |
| 65 | * |
| 66 | * Check if the current page is a success form page |
| 67 | * |
| 68 | * @param false $args |
| 69 | * |
| 70 | * @return bool |
| 71 | */ |
| 72 | function acfe_is_form_success($args = false){ |
| 73 | |
| 74 | // get success form |
| 75 | $form = acfe_get_form_sent(); |
| 76 | |
| 77 | // no form found |
| 78 | if(empty($form)){ |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | // argument |
| 83 | if($args){ |
| 84 | |
| 85 | // name |
| 86 | if(is_string($args)){ |
| 87 | |
| 88 | // compare |
| 89 | return acfe_get($form, 'name') === $args; |
| 90 | |
| 91 | // array |
| 92 | }elseif(is_array($args)){ |
| 93 | |
| 94 | // cleanup keys that are subject to change (via hook or template tag) |
| 95 | unset($form['settings'], $form['attributes'], $form['validation'], $form['success'], $form['render'], $form['uniqid'], $form['cid'], $form['map']); |
| 96 | unset($args['settings'], $args['attributes'], $args['validation'], $args['success'], $args['render'], $args['uniqid'], $args['cid'], $args['map']); |
| 97 | |
| 98 | // compare |
| 99 | return $form === $args; |
| 100 | |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | |
| 105 | // return |
| 106 | return true; |
| 107 | |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * acfe_get_form_actions |
| 113 | * |
| 114 | * Retrieve all actions output |
| 115 | * |
| 116 | * @return array |
| 117 | */ |
| 118 | function acfe_get_form_actions(){ |
| 119 | |
| 120 | // get actions |
| 121 | $actions = acf_get_form_data('acfe/form/actions'); |
| 122 | $actions = acfe_as_array($actions); |
| 123 | |
| 124 | // return |
| 125 | return $actions; |
| 126 | |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * acfe_get_form_action |
| 131 | * |
| 132 | * Retrieve the latest action output |
| 133 | * |
| 134 | * @param $path |
| 135 | * @param null $default |
| 136 | * |
| 137 | * @return false|mixed|string|null |
| 138 | */ |
| 139 | function acfe_get_form_action($path = null, $default = null){ |
| 140 | |
| 141 | // get actions |
| 142 | $actions = acfe_get_form_actions(); |
| 143 | |
| 144 | // no action |
| 145 | if(empty($actions)){ |
| 146 | return $default; |
| 147 | } |
| 148 | |
| 149 | // get last action |
| 150 | $action = end($actions); |
| 151 | |
| 152 | // get by action by path |
| 153 | if(!empty($path)){ |
| 154 | $action = acfe_get($actions, $path, $default); |
| 155 | } |
| 156 | |
| 157 | return $action; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * acfe_enqueue_form |
| 164 | * |
| 165 | * Enqueue ACF scripts and append missing data when form is loaded with ajax |
| 166 | * |
| 167 | * @return void |
| 168 | */ |
| 169 | function acfe_enqueue_form(){ |
| 170 | |
| 171 | // enqueue acf scripts |
| 172 | acf_enqueue_scripts(array( |
| 173 | 'uploader' => true, // uploader for WYSIWYG field |
| 174 | )); |
| 175 | |
| 176 | // append missing data |
| 177 | acf_set_form_data('screen', 'acfe_form'); |
| 178 | acf_set_form_data('post_id', acfe_get_post_id()); |
| 179 | acf_set_form_data('validation', true); |
| 180 | |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /** |
| 185 | * acfe_import_form |
| 186 | * |
| 187 | * @param $args |
| 188 | * |
| 189 | * @return array|mixed|WP_Error |
| 190 | */ |
| 191 | function acfe_import_form($args){ |
| 192 | |
| 193 | // json string |
| 194 | if(is_string($args)){ |
| 195 | $args = json_decode($args, true); |
| 196 | } |
| 197 | |
| 198 | // validate array |
| 199 | if(!is_array($args) || empty($args)){ |
| 200 | return new WP_Error('acfe_import_form_invalid_input', __("Input is invalid: Must be a json string or an array.")); |
| 201 | } |
| 202 | |
| 203 | // module |
| 204 | $module = acfe_get_module('form'); |
| 205 | |
| 206 | /** |
| 207 | * single item |
| 208 | * |
| 209 | * array( |
| 210 | * 'title' => 'My Form', |
| 211 | * 'acfe_form_name' => 'my-form', |
| 212 | * 'acfe_form_actions' => array(...) |
| 213 | * ) |
| 214 | */ |
| 215 | if(isset($args['title'])){ |
| 216 | |
| 217 | $args = array( |
| 218 | $args |
| 219 | ); |
| 220 | |
| 221 | } |
| 222 | |
| 223 | // vars |
| 224 | $result = array(); |
| 225 | |
| 226 | // loop |
| 227 | foreach($args as $key => $item){ |
| 228 | |
| 229 | // prior 0.9 |
| 230 | // old import had name as key |
| 231 | if(!is_numeric($key) && !isset($item['name'])){ |
| 232 | $item['name'] = $key; |
| 233 | } |
| 234 | |
| 235 | // name still missing |
| 236 | // retrieve from old key acfe_form_name |
| 237 | if(!isset($item['name'])){ |
| 238 | $item['name'] = acfe_get($item, 'acfe_form_name'); |
| 239 | } |
| 240 | |
| 241 | // search database for existing item |
| 242 | $post = $module->get_item_post($item['name']); |
| 243 | if($post){ |
| 244 | $item['ID'] = $post->ID; |
| 245 | } |
| 246 | |
| 247 | // import item |
| 248 | $item = $module->import_item($item); |
| 249 | |
| 250 | $return = array( |
| 251 | 'success' => true, |
| 252 | 'post_id' => $item['ID'], |
| 253 | 'message' => 'Form "' . get_the_title($item['ID']) . '" successfully imported.', |
| 254 | ); |
| 255 | |
| 256 | $result[] = $return; |
| 257 | |
| 258 | } |
| 259 | |
| 260 | if(count($result) === 1){ |
| 261 | $result = $result[0]; |
| 262 | } |
| 263 | |
| 264 | return $result; |
| 265 | |
| 266 | } |
| 267 | |
| 268 | |
| 269 | /** |
| 270 | * acfe_form_format_value |
| 271 | * |
| 272 | * @param $raw_value |
| 273 | * @param $field |
| 274 | * @param $deprecated |
| 275 | * |
| 276 | * @return mixed |
| 277 | */ |
| 278 | function acfe_form_format_value($raw_value, $field, $deprecated = null){ |
| 279 | |
| 280 | /** |
| 281 | * deprecated backwards compatibility |
| 282 | * |
| 283 | * @since 0.8.8 |
| 284 | */ |
| 285 | if($deprecated !== null){ |
| 286 | |
| 287 | // second argument was $post_id |
| 288 | $field = $deprecated; |
| 289 | |
| 290 | // deprecated warning |
| 291 | _deprecated_function('ACF Extended: acfe_form_format_value($raw_value, $field, $deprecated) 3rd argument', '0.8.8', 'pass field array as 2nd argument'); |
| 292 | |
| 293 | } |
| 294 | |
| 295 | // vars |
| 296 | $form = acfe_get_context('form'); |
| 297 | $post_id = $form['post_id']; |
| 298 | |
| 299 | // check & delete store |
| 300 | // this fix an issue where different group subfields with same name will output same value |
| 301 | // this is because group subfields have singular name. ie: 'textarea' instead of 'group_textarea' |
| 302 | $store = acf_get_store('values'); |
| 303 | if($store->has("{$post_id}:{$field['name']}:formatted")){ |
| 304 | $store->remove("{$post_id}:{$field['name']}:formatted"); |
| 305 | } |
| 306 | |
| 307 | // default format value |
| 308 | $value = acf_format_value($raw_value, $post_id, $field); |
| 309 | |
| 310 | // filters |
| 311 | $value = apply_filters("acfe/form/format_value", $value, $raw_value, $post_id, $field, $form); |
| 312 | |
| 313 | // return |
| 314 | return $value; |
| 315 | |
| 316 | } |
| 317 | |
| 318 | |
| 319 | /** |
| 320 | * acfe_form_format_value_array |
| 321 | * |
| 322 | * @param $value |
| 323 | * |
| 324 | * @return mixed|string |
| 325 | */ |
| 326 | function acfe_form_format_value_array($value){ |
| 327 | |
| 328 | // bail early |
| 329 | if(!is_array($value)){ |
| 330 | return $value; |
| 331 | } |
| 332 | |
| 333 | // vars |
| 334 | $return = array(); |
| 335 | |
| 336 | // loop value |
| 337 | foreach($value as $i => $v){ |
| 338 | |
| 339 | $key = !is_numeric($i) ? "$i: " : ''; |
| 340 | |
| 341 | if(is_object($v)){ |
| 342 | $v = (array) $v; |
| 343 | } |
| 344 | |
| 345 | $return[] = $key . acfe_form_format_value_array($v); |
| 346 | |
| 347 | } |
| 348 | |
| 349 | return implode(', ', $return); |
| 350 | |
| 351 | } |
| 352 | |
| 353 | |
| 354 | /** |
| 355 | * _acfe_form_format_value_variations |
| 356 | * |
| 357 | * @hook acfe/form/format_value |
| 358 | */ |
| 359 | add_filter('acfe/form/format_value', '_acfe_form_format_value_variations', 9, 5); |
| 360 | function _acfe_form_format_value_variations($value, $raw_value, $post_id, $field, $form){ |
| 361 | |
| 362 | $value = apply_filters("acfe/form/format_value/form={$form['name']}", $value, $raw_value, $post_id, $field, $form); |
| 363 | $value = apply_filters("acfe/form/format_value/type={$field['type']}", $value, $raw_value, $post_id, $field, $form); |
| 364 | $value = apply_filters("acfe/form/format_value/key={$field['key']}", $value, $raw_value, $post_id, $field, $form); |
| 365 | $value = apply_filters("acfe/form/format_value/name={$field['name']}", $value, $raw_value, $post_id, $field, $form); |
| 366 | |
| 367 | // format object value |
| 368 | if(is_object($value)){ |
| 369 | $value = (array) $value; |
| 370 | } |
| 371 | |
| 372 | // format array value |
| 373 | if(is_array($value)){ |
| 374 | $value = acfe_form_format_value_array($value); |
| 375 | } |
| 376 | |
| 377 | return $value; |
| 378 | |
| 379 | } |
| 380 | |
| 381 | |
| 382 | /** |
| 383 | * acfe_form_unique_action_id |
| 384 | * |
| 385 | * Make actions names unique |
| 386 | * |
| 387 | * @param $form |
| 388 | * @param $type |
| 389 | * |
| 390 | * @return string |
| 391 | * |
| 392 | * @deprecated |
| 393 | */ |
| 394 | function acfe_form_unique_action_id($form, $type){ |
| 395 | |
| 396 | _deprecated_function('ACF Extended: acfe_form_unique_action_id()', '0.9'); |
| 397 | |
| 398 | // global |
| 399 | global $acfe_form_uniqid; |
| 400 | $acfe_form_uniqid = acfe_as_array($acfe_form_uniqid); |
| 401 | |
| 402 | $name = "{$form['name']}-{$type}"; |
| 403 | |
| 404 | if(!isset($acfe_form_uniqid[ $type ])){ |
| 405 | $acfe_form_uniqid[ $type ] = 1; |
| 406 | } |
| 407 | |
| 408 | if($acfe_form_uniqid[ $type ] > 1){ |
| 409 | $name = "{$name}-{$acfe_form_uniqid[ $type ]}"; |
| 410 | } |
| 411 | |
| 412 | $acfe_form_uniqid[ $type ]++; |
| 413 | |
| 414 | return $name; |
| 415 | |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * acfe_form_get_actions |
| 421 | * |
| 422 | * Retrieve all actions output |
| 423 | * |
| 424 | * @return array|string[] |
| 425 | * |
| 426 | * @deprecated |
| 427 | */ |
| 428 | function acfe_form_get_actions(){ |
| 429 | return acfe_get_form_actions(); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /** |
| 434 | * acfe_form_get_action |
| 435 | * |
| 436 | * Retrieve the latest action output |
| 437 | * |
| 438 | * @param false $name |
| 439 | * @param false $key |
| 440 | * |
| 441 | * @return false|mixed|null |
| 442 | * |
| 443 | * @deprecated |
| 444 | */ |
| 445 | function acfe_form_get_action($name = false, $key = false){ |
| 446 | $name = !$key ? $name : "{$name}.{$key}"; // append key to path |
| 447 | return acfe_get_form_action($name); |
| 448 | } |
| 449 | |
| 450 | |
| 451 | /** |
| 452 | * acfe_form_decrypt_args |
| 453 | * |
| 454 | * Wrapper to decrypt ACF & ACFE Forms arguments |
| 455 | * |
| 456 | * @return array|false |
| 457 | * |
| 458 | * @deprecated |
| 459 | */ |
| 460 | function acfe_form_decrypt_args(){ |
| 461 | _deprecated_function('ACF Extended: acfe_form_decrypt_args()', '0.9.0.5', "acfe_get_form_sent()"); |
| 462 | return acfe_get_form_sent(); |
| 463 | } |
| 464 | |
| 465 | |
| 466 | /** |
| 467 | * acfe_form_is_submitted |
| 468 | * |
| 469 | * check if the current page is a success form page |
| 470 | * |
| 471 | * @param false $name |
| 472 | * |
| 473 | * @return bool |
| 474 | * |
| 475 | * @deprecated |
| 476 | */ |
| 477 | function acfe_form_is_submitted($name = false){ |
| 478 | _deprecated_function('ACF Extended: acfe_form_is_submitted()', '0.8.7.5', "acfe_is_form_success()"); |
| 479 | return acfe_is_form_success($name); |
| 480 | } |
| 481 | |
| 482 | |
| 483 | /** |
| 484 | * acfe_form_is_admin |
| 485 | * |
| 486 | * Check if current screen is back-end |
| 487 | * |
| 488 | * @return bool |
| 489 | * |
| 490 | * @deprecated |
| 491 | */ |
| 492 | function acfe_form_is_admin(){ |
| 493 | _deprecated_function('ACF Extended: acfe_form_is_admin()', '0.8.8', "acfe_is_admin()"); |
| 494 | return acfe_is_admin(); |
| 495 | } |
| 496 | |
| 497 | |
| 498 | /** |
| 499 | * acfe_form_is_front |
| 500 | * |
| 501 | * Check if current screen is front-end |
| 502 | * |
| 503 | * @return bool |
| 504 | * |
| 505 | * @deprecated |
| 506 | */ |
| 507 | function acfe_form_is_front(){ |
| 508 | _deprecated_function('ACF Extended: acfe_form_is_front()', '0.8.8', "acfe_is_front()"); |
| 509 | return acfe_is_front(); |
| 510 | } |