author.php
4 years ago
autosync.php
4 years ago
block-types.php
4 years ago
dev.php
4 years ago
forms-action-custom.php
4 years ago
forms-action-email.php
4 years ago
forms-action-post.php
4 years ago
forms-action-redirect.php
4 years ago
forms-action-term.php
4 years ago
forms-action-user.php
4 years ago
forms-cheatsheet.php
4 years ago
forms-front.php
4 years ago
forms-helpers.php
4 years ago
forms-hooks.php
4 years ago
forms.php
4 years ago
module.php
4 years ago
options-pages.php
4 years ago
options.class.php
4 years ago
options.php
4 years ago
post-types.php
4 years ago
single-meta.php
4 years ago
taxonomies.php
4 years ago
ui-settings.php
4 years ago
ui-term.php
4 years ago
ui-user.php
4 years ago
ui.php
4 years ago
forms-front.php
905 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_form_front')): |
| 7 | |
| 8 | class acfe_form_front{ |
| 9 | |
| 10 | function __construct(){ |
| 11 | |
| 12 | // vars |
| 13 | $this->fields = array( |
| 14 | '_validate_email' => array( |
| 15 | 'prefix' => 'acf', |
| 16 | 'name' => '_validate_email', |
| 17 | 'key' => '_validate_email', |
| 18 | 'label' => __('Validate Email', 'acf'), |
| 19 | 'type' => 'text', |
| 20 | 'value' => '', |
| 21 | 'wrapper' => array('style' => 'display:none !important;') |
| 22 | ) |
| 23 | ); |
| 24 | |
| 25 | // shortcode |
| 26 | add_shortcode('acfe_form', array($this, 'render_shortcode')); |
| 27 | |
| 28 | // save |
| 29 | add_action('acf/validate_save_post', array($this, 'validate_save_post'), 1); |
| 30 | add_action('wp', array($this, 'save_post')); |
| 31 | |
| 32 | // ajax |
| 33 | add_action('wp_ajax_acfe/form/shortcode', array($this, 'ajax_shortcode'), 20); |
| 34 | add_action('wp_ajax_nopriv_acfe/form/shortcode', array($this, 'ajax_shortcode'), 20); |
| 35 | |
| 36 | } |
| 37 | |
| 38 | function ajax_shortcode(){ |
| 39 | |
| 40 | // validate |
| 41 | if(!acf_verify_ajax()) die; |
| 42 | |
| 43 | // vars |
| 44 | $args = acf_maybe_get_POST('args', array()); |
| 45 | $title = ''; |
| 46 | |
| 47 | // loop thru args |
| 48 | foreach(array('name', 'id') as $key){ |
| 49 | |
| 50 | if(!acf_maybe_get($args, $key)) continue; |
| 51 | |
| 52 | $title = acf_maybe_get($args, $key); |
| 53 | break; |
| 54 | |
| 55 | } |
| 56 | |
| 57 | $title = is_numeric($title) ? "#{$title}" : "\"{$title}\""; |
| 58 | |
| 59 | ob_start(); |
| 60 | ?> |
| 61 | <div style="border:1px solid #ddd; padding:100px 25px; background:#f8f8f8; text-align:center;"> |
| 62 | <?php _e('Form', 'acfe'); ?> <?php echo $title; ?> |
| 63 | </div> |
| 64 | <?php echo ob_get_clean(); |
| 65 | die; |
| 66 | |
| 67 | } |
| 68 | |
| 69 | function validate_save_post(){ |
| 70 | |
| 71 | // validate front-end |
| 72 | if(!acfe_is_front()){ |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // validate screen |
| 77 | if(acf_maybe_get_POST('_acf_screen') !== 'acfe_form'){ |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // decrypt |
| 82 | if(!$form = acfe_form_decrypt_args()){ |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | $post_id = acf_maybe_get($form, 'post_id', false); |
| 87 | $form_name = acf_maybe_get($form, 'name'); |
| 88 | $form_id = acf_maybe_get($form, 'ID'); |
| 89 | |
| 90 | // bail early not valid form |
| 91 | if(!$form_name || !$form_id){ |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | // local fields |
| 96 | foreach($this->fields as $k => $field){ |
| 97 | |
| 98 | // bail early if no in $_POST |
| 99 | if(!isset($_POST['acf'][ $k ])) continue; |
| 100 | |
| 101 | // register |
| 102 | acf_add_local_field($field); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | // honeypot |
| 107 | if(!empty($acf['_validate_email'])){ |
| 108 | acf_add_validation_error('', __('Spam Detected', 'acf')); |
| 109 | } |
| 110 | |
| 111 | // set form data for validation |
| 112 | acf_set_form_data('acfe/form', $form); |
| 113 | |
| 114 | // setup meta |
| 115 | acfe_setup_meta($_POST['acf'], 'acfe/form/validation', true); |
| 116 | |
| 117 | // loop |
| 118 | if(have_rows('acfe_form_actions', $form_id)): |
| 119 | while(have_rows('acfe_form_actions', $form_id)): the_row(); |
| 120 | |
| 121 | // vars |
| 122 | $action = get_row_layout(); |
| 123 | $alias = get_sub_field('acfe_form_custom_alias'); |
| 124 | |
| 125 | // custom action |
| 126 | if($action === 'custom'){ |
| 127 | $action = get_sub_field('acfe_form_custom_action'); |
| 128 | $alias = ''; |
| 129 | } |
| 130 | |
| 131 | // actions |
| 132 | do_action("acfe/form/validation/{$action}", $form, $post_id, $alias); |
| 133 | do_action("acfe/form/validation/{$action}/form={$form_name}", $form, $post_id, $alias); |
| 134 | |
| 135 | if(!empty($alias)){ |
| 136 | do_action("acfe/form/validation/{$action}/action={$alias}", $form, $post_id, $alias); |
| 137 | } |
| 138 | |
| 139 | endwhile; |
| 140 | endif; |
| 141 | |
| 142 | // actions |
| 143 | do_action("acfe/form/validation", $form, $post_id); |
| 144 | do_action("acfe/form/validation/form={$form_name}", $form, $post_id); |
| 145 | |
| 146 | // reset |
| 147 | acfe_reset_meta(); |
| 148 | |
| 149 | // unset form data |
| 150 | acf_set_form_data('acfe/form', null); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | function save_post(){ |
| 155 | |
| 156 | // verify nonce |
| 157 | if(!acf_verify_nonce('acfe_form')){ |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // decrypt |
| 162 | if(!$form = acfe_form_decrypt_args()){ |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // ACF |
| 167 | $_POST['acf'] = acf_maybe_get_POST('acf', array()); |
| 168 | |
| 169 | // run kses on all $_POST data |
| 170 | if($form['kses']){ |
| 171 | $_POST['acf'] = wp_kses_post_deep($_POST['acf']); |
| 172 | } |
| 173 | |
| 174 | // validate save post |
| 175 | acf_validate_save_post(true); |
| 176 | |
| 177 | // vars |
| 178 | $post_id = acf_maybe_get($form, 'post_id', false); |
| 179 | $form_name = acf_maybe_get($form, 'name'); |
| 180 | $form_id = acf_maybe_get($form, 'ID'); |
| 181 | |
| 182 | // remove save post action |
| 183 | add_filter('acf/pre_update_value', '__return_false', 99); |
| 184 | |
| 185 | // upload files but do not save post |
| 186 | acf_save_post(false); |
| 187 | |
| 188 | // restore save post action |
| 189 | remove_filter('acf/pre_update_value', '__return_false', 99); |
| 190 | |
| 191 | // unset files to avoid duplicate upload |
| 192 | unset($_FILES); |
| 193 | |
| 194 | // remove shortcode (temp) |
| 195 | // https://github.com/elementor/elementor/issues/10998 |
| 196 | // https://github.com/Yoast/wordpress-seo/issues/14643 |
| 197 | remove_shortcode('acfe_form'); |
| 198 | |
| 199 | // setup meta |
| 200 | acfe_setup_meta($_POST['acf'], 'acfe/form/submit', true); |
| 201 | |
| 202 | // loop |
| 203 | if(have_rows('acfe_form_actions', $form_id)): |
| 204 | |
| 205 | while(have_rows('acfe_form_actions', $form_id)): the_row(); |
| 206 | |
| 207 | // vars |
| 208 | $action = get_row_layout(); |
| 209 | $alias = get_sub_field('acfe_form_custom_alias'); |
| 210 | |
| 211 | // action |
| 212 | do_action("acfe/form/make/{$action}", $form, $post_id, $alias); |
| 213 | |
| 214 | endwhile; |
| 215 | endif; |
| 216 | |
| 217 | // actions |
| 218 | do_action("acfe/form/submit", $form, $post_id); |
| 219 | do_action("acfe/form/submit/form={$form_name}", $form, $post_id); |
| 220 | |
| 221 | // reset |
| 222 | acfe_reset_meta(); |
| 223 | |
| 224 | // re-add shortcode |
| 225 | add_shortcode('acfe_form', array($this, 'render_shortcode')); |
| 226 | |
| 227 | // return (deprecated) |
| 228 | if($return = acf_maybe_get($form, 'return', '')){ |
| 229 | |
| 230 | // notice |
| 231 | _deprecated_function('ACF Extended - Dynamic Forms: "Redirection" setting', '0.8.7.5', "the new Redirect Action (See documentation: https://www.acf-extended.com/features/modules/dynamic-forms)"); |
| 232 | |
| 233 | // map values |
| 234 | $return = acfe_form_map_field_value($return, $post_id, $form); |
| 235 | |
| 236 | // redirect |
| 237 | wp_redirect($return); |
| 238 | exit; |
| 239 | |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | |
| 244 | function validate_form($param){ |
| 245 | |
| 246 | // get form |
| 247 | $array = $this->get_form($param); |
| 248 | |
| 249 | // bail early |
| 250 | if(!$array){ |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | // vars |
| 255 | $form_id = $array['ID']; |
| 256 | $form_name = $array['name']; |
| 257 | |
| 258 | // filters |
| 259 | $register = true; |
| 260 | $register = apply_filters("acfe/form/register", $register, $form_name, $form_id); |
| 261 | $register = apply_filters("acfe/form/register/name={$form_name}", $register, $form_name, $form_id); |
| 262 | $register = apply_filters("acfe/form/register/id={$form_id}", $register, $form_name, $form_id); |
| 263 | |
| 264 | if($register === false){ |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | // Form Attributes |
| 269 | $form_attributes = get_field('acfe_form_attributes', $form_id); |
| 270 | $fields_attributes = get_field('acfe_form_fields_attributes', $form_id); |
| 271 | |
| 272 | // Defaults |
| 273 | $defaults = array( |
| 274 | |
| 275 | // General |
| 276 | 'ID' => '', |
| 277 | 'name' => '', |
| 278 | 'title' => '', |
| 279 | |
| 280 | // Settings |
| 281 | 'post_id' => acf_get_valid_post_id(), |
| 282 | 'field_groups' => get_field('acfe_form_field_groups', $form_id), |
| 283 | 'field_groups_rules' => get_field('acfe_form_field_groups_rules', $form_id), |
| 284 | 'post_field_groups' => get_field('acfe_form_post_field_groups', $form_id), // deprecated |
| 285 | 'form' => get_field('acfe_form_form_element', $form_id), |
| 286 | 'html_before_fields' => get_field('acfe_form_html_before_fields', $form_id), |
| 287 | 'custom_html_enabled' => get_field('acfe_form_custom_html_enable', $form_id), |
| 288 | 'custom_html' => get_field('acfe_form_custom_html', $form_id), |
| 289 | 'html_after_fields' => get_field('acfe_form_html_after_fields', $form_id), |
| 290 | 'form_submit' => get_field('acfe_form_form_submit', $form_id), |
| 291 | 'submit_value' => get_field('acfe_form_submit_value', $form_id), |
| 292 | 'html_submit_button' => get_field('acfe_form_html_submit_button', $form_id), |
| 293 | 'html_submit_spinner' => get_field('acfe_form_html_submit_spinner', $form_id), |
| 294 | |
| 295 | // Submission |
| 296 | 'hide_error' => get_field('acfe_form_hide_error', $form_id), |
| 297 | 'hide_unload' => get_field('acfe_form_hide_unload', $form_id), |
| 298 | 'hide_revalidation' => get_field('acfe_form_hide_revalidation', $form_id), |
| 299 | 'errors_position' => get_field('acfe_form_errors_position', $form_id), |
| 300 | 'errors_class' => get_field('acfe_form_errors_class', $form_id), |
| 301 | 'updated_message' => get_field('acfe_form_updated_message', $form_id), |
| 302 | 'html_updated_message' => get_field('acfe_form_html_updated_message', $form_id), |
| 303 | 'updated_hide_form' => get_field('acfe_form_updated_hide_form', $form_id), |
| 304 | 'return' => get_field('acfe_form_return', $form_id), // deprecated |
| 305 | |
| 306 | // Advanced |
| 307 | 'honeypot' => get_field('acfe_form_honeypot', $form_id), |
| 308 | 'kses' => get_field('acfe_form_kses', $form_id), |
| 309 | 'uploader' => get_field('acfe_form_uploader', $form_id), |
| 310 | 'field_el' => get_field('acfe_form_form_field_el', $form_id), |
| 311 | 'label_placement' => get_field('acfe_form_label_placement', $form_id), |
| 312 | 'instruction_placement' => get_field('acfe_form_instruction_placement', $form_id), |
| 313 | |
| 314 | // Mapping |
| 315 | 'map' => array(), |
| 316 | |
| 317 | // Form Attributes |
| 318 | 'form_attributes' => array( |
| 319 | 'id' => acf_maybe_get($form_attributes, 'acfe_form_attributes_id'), |
| 320 | 'class' => acf_maybe_get($form_attributes, 'acfe_form_attributes_class'), |
| 321 | 'action' => '', |
| 322 | 'method' => 'post', |
| 323 | 'data-fields-class' => '', |
| 324 | 'data-hide-error' => '', |
| 325 | 'data-hide-unload' => '', |
| 326 | 'data-hide-revalidation'=> '', |
| 327 | 'data-errors-position' => '', |
| 328 | 'data-errors-class' => '', |
| 329 | ), |
| 330 | |
| 331 | // Fields Attributes |
| 332 | 'fields_attributes' => array( |
| 333 | 'wrapper_class' => acf_maybe_get($fields_attributes, 'acfe_form_fields_wrapper_class'), |
| 334 | 'class' => acf_maybe_get($fields_attributes, 'acfe_form_fields_class'), |
| 335 | ), |
| 336 | |
| 337 | ); |
| 338 | |
| 339 | // parse args |
| 340 | $args = wp_parse_args($array, $defaults); |
| 341 | |
| 342 | if(acf_maybe_get($array, 'form_attributes')){ |
| 343 | $args['form_attributes'] = wp_parse_args($array['form_attributes'], $defaults['form_attributes']); |
| 344 | } |
| 345 | |
| 346 | if(acf_maybe_get($array, 'fields_attributes')){ |
| 347 | $args['fields_attributes'] = wp_parse_args($array['fields_attributes'], $defaults['fields_attributes']); |
| 348 | } |
| 349 | |
| 350 | // advanced override |
| 351 | $args['form_attributes']['class'] = 'acfe-form ' . $args['form_attributes']['class']; |
| 352 | $args['form_attributes']['data-fields-class'] = $args['fields_attributes']['class']; |
| 353 | $args['form_attributes']['data-hide-error'] = $args['hide_error']; |
| 354 | $args['form_attributes']['data-hide-unload'] = $args['hide_unload']; |
| 355 | $args['form_attributes']['data-hide-revalidation'] = $args['hide_revalidation']; |
| 356 | $args['form_attributes']['data-errors-position'] = $args['errors_position']; |
| 357 | $args['form_attributes']['data-errors-class'] = $args['errors_class']; |
| 358 | |
| 359 | if(acf_maybe_get_POST('acf')){ |
| 360 | acfe_setup_meta($_POST['acf'], 'acfe/form/load', true); |
| 361 | } |
| 362 | |
| 363 | // Args |
| 364 | $args = apply_filters("acfe/form/load", $args, $args['post_id']); |
| 365 | $args = apply_filters("acfe/form/load/form={$form_name}", $args, $args['post_id']); |
| 366 | |
| 367 | // Load |
| 368 | if(have_rows('acfe_form_actions', $form_id)): |
| 369 | while(have_rows('acfe_form_actions', $form_id)): the_row(); |
| 370 | |
| 371 | $action = get_row_layout(); |
| 372 | $alias = get_sub_field('acfe_form_custom_alias'); |
| 373 | |
| 374 | // Custom Action |
| 375 | if($action === 'custom'){ |
| 376 | |
| 377 | $action = get_sub_field('acfe_form_custom_action'); |
| 378 | $alias = ''; |
| 379 | |
| 380 | } |
| 381 | |
| 382 | $args = apply_filters("acfe/form/load/{$action}", $args, $args['post_id'], $alias); |
| 383 | $args = apply_filters("acfe/form/load/{$action}/form={$form_name}", $args, $args['post_id'], $alias); |
| 384 | |
| 385 | if(!empty($alias)){ |
| 386 | $args = apply_filters("acfe/form/load/{$action}/action={$alias}", $args, $args['post_id'], $alias); |
| 387 | } |
| 388 | |
| 389 | endwhile; |
| 390 | endif; |
| 391 | |
| 392 | if(acf_maybe_get_POST('acf')){ |
| 393 | acfe_reset_meta(); |
| 394 | } |
| 395 | |
| 396 | return $args; |
| 397 | |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * ACFE Form: render_form |
| 402 | * |
| 403 | */ |
| 404 | function render_form($args = array()){ |
| 405 | |
| 406 | // bail early if no args |
| 407 | if(!$args = $this->validate_form($args)){ |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | // enqueue acf |
| 412 | acf_enqueue_scripts(); |
| 413 | |
| 414 | // success message |
| 415 | $this->form_success($args); |
| 416 | |
| 417 | // hide form on success |
| 418 | if($this->form_success_hide($args)){ |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | $fields = $this->prepare_fields($args); |
| 423 | |
| 424 | $this->form_uploader($args); |
| 425 | |
| 426 | do_action("acfe/form/render/before_form", $args); |
| 427 | do_action("acfe/form/render/before_form/id={$args['ID']}", $args); |
| 428 | do_action("acfe/form/render/before_form/name={$args['name']}", $args); |
| 429 | |
| 430 | $this->form_wrapper($args); |
| 431 | |
| 432 | do_action("acfe/form/render/before_fields", $args); |
| 433 | do_action("acfe/form/render/before_fields/id={$args['ID']}", $args); |
| 434 | do_action("acfe/form/render/before_fields/name={$args['name']}",$args); |
| 435 | |
| 436 | $this->form_data($args); |
| 437 | |
| 438 | $this->fields_wrapper($args); |
| 439 | |
| 440 | $this->render_fields($args, $fields); |
| 441 | |
| 442 | $this->fields_wrapper($args, false); |
| 443 | |
| 444 | do_action("acfe/form/render/after_fields", $args); |
| 445 | do_action("acfe/form/render/after_fields/id={$args['ID']}", $args); |
| 446 | do_action("acfe/form/render/after_fields/name={$args['name']}", $args); |
| 447 | |
| 448 | $this->form_wrapper($args, false); |
| 449 | |
| 450 | do_action("acfe/form/render/after_form", $args); |
| 451 | do_action("acfe/form/render/after_form/id={$args['ID']}", $args); |
| 452 | do_action("acfe/form/render/after_form/name={$args['name']}", $args); |
| 453 | |
| 454 | } |
| 455 | |
| 456 | function form_success($args){ |
| 457 | |
| 458 | // filter for success script |
| 459 | acf_enable_filter('acfe/form/is_success'); |
| 460 | |
| 461 | // validate |
| 462 | if(!acfe_is_form_success($args['name'])) return; |
| 463 | |
| 464 | // JS div atts |
| 465 | $atts = acf_esc_attrs(array( |
| 466 | 'class' => 'acfe-form-success', |
| 467 | 'data-form-name' => $args['name'], |
| 468 | 'data-form-id' => $args['ID'], |
| 469 | )); |
| 470 | |
| 471 | // <div class="acfe-form-success"></div> |
| 472 | echo "<div {$atts}></div>"; |
| 473 | |
| 474 | // assign message |
| 475 | $message = $args['updated_message']; |
| 476 | |
| 477 | // no success message |
| 478 | if(!$message){ |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | // map message with values in $_POST |
| 483 | if(acf_maybe_get_POST('acf')){ |
| 484 | $message = acfe_form_map_field_value($message, $args['post_id'], $args); |
| 485 | } |
| 486 | |
| 487 | // html message |
| 488 | if($args['html_updated_message']){ |
| 489 | $message = sprintf($args['html_updated_message'], wp_unslash($message)); |
| 490 | } |
| 491 | |
| 492 | echo $message; |
| 493 | |
| 494 | } |
| 495 | |
| 496 | function form_success_hide($args){ |
| 497 | |
| 498 | // hide form on success |
| 499 | if(acfe_is_form_success($args['name']) && $args['updated_hide_form']){ |
| 500 | return true; |
| 501 | } |
| 502 | |
| 503 | // show |
| 504 | return false; |
| 505 | |
| 506 | } |
| 507 | |
| 508 | function prepare_fields($args){ |
| 509 | |
| 510 | // vars |
| 511 | $fields = array(); |
| 512 | |
| 513 | // register local fields |
| 514 | foreach($this->fields as $field){ |
| 515 | acf_add_local_field($field); |
| 516 | } |
| 517 | |
| 518 | // honeypot |
| 519 | if($args['honeypot']){ |
| 520 | $fields[] = acf_get_field('_validate_email'); |
| 521 | } |
| 522 | |
| 523 | // field attributes |
| 524 | if($args['fields_attributes']['wrapper_class'] || $args['fields_attributes']['class'] || $args['label_placement'] === 'hidden'){ |
| 525 | |
| 526 | add_filter('acf/prepare_field', function($field) use($args){ |
| 527 | |
| 528 | if(!$field){ |
| 529 | return $field; |
| 530 | } |
| 531 | |
| 532 | if($args['fields_attributes']['wrapper_class']){ |
| 533 | $field['wrapper']['class'] .= ' ' . $args['fields_attributes']['wrapper_class']; |
| 534 | } |
| 535 | |
| 536 | if($args['fields_attributes']['class']){ |
| 537 | $field['class'] .= ' ' . $args['fields_attributes']['class']; |
| 538 | } |
| 539 | |
| 540 | if($args['label_placement'] === 'hidden'){ |
| 541 | $field['label'] = false; |
| 542 | } |
| 543 | |
| 544 | return $field; |
| 545 | |
| 546 | }); |
| 547 | |
| 548 | } |
| 549 | |
| 550 | // form map values |
| 551 | foreach($args['map'] as $key => $_field){ |
| 552 | |
| 553 | add_filter("acf/prepare_field/key={$key}", function($field) use($_field){ |
| 554 | |
| 555 | if(!$field){ |
| 556 | return $field; |
| 557 | } |
| 558 | |
| 559 | return array_merge($field, $_field); |
| 560 | |
| 561 | }); |
| 562 | |
| 563 | } |
| 564 | |
| 565 | return $fields; |
| 566 | |
| 567 | } |
| 568 | |
| 569 | function form_uploader($args){ |
| 570 | |
| 571 | // uploader (always set in case of multiple forms on the page) |
| 572 | acf_disable_filter('acfe/form/uploader'); |
| 573 | |
| 574 | if($args['uploader'] !== 'default'){ |
| 575 | |
| 576 | acf_enable_filter('acfe/form/uploader'); |
| 577 | acf_update_setting('uploader', $args['uploader']); |
| 578 | |
| 579 | } |
| 580 | |
| 581 | } |
| 582 | |
| 583 | function form_wrapper($args, $open = true){ |
| 584 | |
| 585 | // preview mode |
| 586 | $is_preview = acfe_is_dynamic_preview(); |
| 587 | |
| 588 | // remove <form> |
| 589 | if($is_preview){ |
| 590 | $args['form'] = false; |
| 591 | } |
| 592 | |
| 593 | // wrapper |
| 594 | $wrapper = $args['form'] ? 'form' : 'div'; |
| 595 | |
| 596 | // open |
| 597 | if($open){ |
| 598 | |
| 599 | // disabled required + fields names |
| 600 | if($is_preview){ |
| 601 | add_filter('acf/prepare_field', array($this, 'disable_fields')); |
| 602 | } |
| 603 | |
| 604 | $atts = acf_esc_attrs($args['form_attributes']); |
| 605 | |
| 606 | // <form class="acfe-form"> |
| 607 | echo "<{$wrapper} {$atts}>"; |
| 608 | |
| 609 | // close |
| 610 | }else{ |
| 611 | |
| 612 | // </form> |
| 613 | echo "</{$wrapper}>"; |
| 614 | |
| 615 | // re-enable required + fields names |
| 616 | if($is_preview){ |
| 617 | remove_filter('acf/prepare_field', array($this, 'disable_fields')); |
| 618 | } |
| 619 | |
| 620 | } |
| 621 | |
| 622 | } |
| 623 | |
| 624 | function fields_wrapper($args, $open = true){ |
| 625 | |
| 626 | // open |
| 627 | if($open){ |
| 628 | |
| 629 | $atts = array( |
| 630 | 'class' => 'acf-fields acf-form-fields' |
| 631 | ); |
| 632 | |
| 633 | if($args['label_placement'] !== 'hidden'){ |
| 634 | $atts['class'] .= " -{$args['label_placement']}"; |
| 635 | } |
| 636 | |
| 637 | $atts = acf_esc_attrs($atts); |
| 638 | |
| 639 | // <div class="acf-fields acf-form-fields"> |
| 640 | echo "<div {$atts}>"; |
| 641 | |
| 642 | // html before fields |
| 643 | echo $args['html_before_fields']; |
| 644 | |
| 645 | // close |
| 646 | }else{ |
| 647 | |
| 648 | // html after fields |
| 649 | echo $args['html_after_fields']; |
| 650 | |
| 651 | echo '</div>'; |
| 652 | |
| 653 | // form submit |
| 654 | if($args['form_submit']): ?> |
| 655 | <div class="acf-form-submit"> |
| 656 | |
| 657 | <?php printf($args['html_submit_button'], $args['submit_value']); ?> |
| 658 | <?php echo $args['html_submit_spinner']; ?> |
| 659 | |
| 660 | </div> |
| 661 | <?php endif; |
| 662 | |
| 663 | } |
| 664 | |
| 665 | } |
| 666 | |
| 667 | function form_data($args){ |
| 668 | |
| 669 | // bail early in preview mode |
| 670 | if(acfe_is_dynamic_preview()) return; |
| 671 | |
| 672 | // render form data |
| 673 | acf_form_data(array( |
| 674 | 'screen' => 'acfe_form', |
| 675 | 'post_id' => $args['post_id'], |
| 676 | 'form' => acf_encrypt(json_encode($args)) |
| 677 | )); |
| 678 | |
| 679 | } |
| 680 | |
| 681 | function render_fields($args, $fields){ |
| 682 | |
| 683 | // custom html render |
| 684 | if($args['custom_html_enabled'] && $args['custom_html']){ |
| 685 | |
| 686 | // render honeypot |
| 687 | acf_render_fields($fields, false, $args['field_el'], $args['instruction_placement']); |
| 688 | |
| 689 | // render custom html render |
| 690 | echo acfe_form_render_fields($args['custom_html'], $args['post_id'], $args); |
| 691 | |
| 692 | return; |
| 693 | |
| 694 | } |
| 695 | |
| 696 | // vars |
| 697 | $field_groups = array(); |
| 698 | $args['field_groups'] = acf_get_array($args['field_groups']); |
| 699 | |
| 700 | // post field groups (deprecated, use apply field groups rules instead) |
| 701 | if($args['post_field_groups']){ |
| 702 | |
| 703 | // Override Field Groups |
| 704 | $post_field_groups = acf_get_field_groups(array( |
| 705 | 'post_id' => $args['post_field_groups'] |
| 706 | )); |
| 707 | |
| 708 | // re-assign post field groups |
| 709 | $args['field_groups'] = wp_list_pluck($post_field_groups, 'key'); |
| 710 | |
| 711 | } |
| 712 | |
| 713 | // form field groups |
| 714 | foreach($args['field_groups'] as $key){ |
| 715 | |
| 716 | // validate field group exists |
| 717 | $field_group = acf_get_field_group($key); |
| 718 | |
| 719 | if($field_group){ |
| 720 | $field_groups[] = $field_group; |
| 721 | } |
| 722 | |
| 723 | } |
| 724 | |
| 725 | // apply field groups rules |
| 726 | if($args['field_groups_rules'] && $field_groups){ |
| 727 | |
| 728 | $post_id = get_the_ID(); |
| 729 | |
| 730 | $location = array( |
| 731 | 'post_id' => $post_id, |
| 732 | 'post_type' => get_post_type($post_id), |
| 733 | ); |
| 734 | |
| 735 | $filtered = array(); |
| 736 | |
| 737 | foreach($field_groups as $field_group){ |
| 738 | |
| 739 | // Deleted field group |
| 740 | if(!isset($field_group['location'])) continue; |
| 741 | |
| 742 | // Force active |
| 743 | $field_group['active'] = true; |
| 744 | |
| 745 | // fitler field groups |
| 746 | if(acf_get_field_group_visibility($field_group, $location)){ |
| 747 | $filtered[] = $field_group; |
| 748 | } |
| 749 | |
| 750 | } |
| 751 | |
| 752 | // assign new filtered field groups |
| 753 | $field_groups = $filtered; |
| 754 | |
| 755 | } |
| 756 | |
| 757 | // get field groups fields |
| 758 | foreach($field_groups as $field_group){ |
| 759 | |
| 760 | $_fields = acf_get_fields($field_group); |
| 761 | |
| 762 | foreach(array_keys($_fields) as $i){ |
| 763 | |
| 764 | $fields[] = acf_extract_var($_fields, $i); |
| 765 | |
| 766 | } |
| 767 | |
| 768 | } |
| 769 | |
| 770 | // render fields |
| 771 | acf_render_fields($fields, acf_uniqid('acfe_form'), $args['field_el'], $args['instruction_placement']); |
| 772 | |
| 773 | } |
| 774 | |
| 775 | function render_shortcode($atts){ |
| 776 | |
| 777 | // attributes array |
| 778 | $atts = acf_get_array($atts); |
| 779 | |
| 780 | // allow array atts |
| 781 | foreach(array_keys($atts) as $key){ |
| 782 | |
| 783 | // sub array compatibility |
| 784 | foreach(array('form_attributes_', 'fields_attributes_') as $allowed){ |
| 785 | |
| 786 | // check found allowed |
| 787 | if(!acfe_starts_with($key, $allowed)) continue; |
| 788 | |
| 789 | // explode |
| 790 | $explode = explode($allowed, $key); |
| 791 | $sub_key = $explode[1]; |
| 792 | |
| 793 | // set attributes array |
| 794 | $atts[ substr($allowed, 0, -1) ][ $sub_key ] = $atts[ $key ]; |
| 795 | unset($atts[ $key ]); |
| 796 | |
| 797 | } |
| 798 | |
| 799 | } |
| 800 | |
| 801 | // render |
| 802 | ob_start(); |
| 803 | |
| 804 | acfe_form($atts); |
| 805 | |
| 806 | return ob_get_clean(); |
| 807 | |
| 808 | } |
| 809 | |
| 810 | function disable_fields($field){ |
| 811 | |
| 812 | $field['name'] = ''; |
| 813 | $field['required'] = false; |
| 814 | |
| 815 | return $field; |
| 816 | |
| 817 | } |
| 818 | |
| 819 | function get_form($param){ |
| 820 | |
| 821 | $form_id = false; |
| 822 | $form_name = false; |
| 823 | $array = array(); |
| 824 | |
| 825 | // check array |
| 826 | if(is_array($param)){ |
| 827 | |
| 828 | // save params |
| 829 | $array = $param; |
| 830 | $param = false; |
| 831 | |
| 832 | // check keys |
| 833 | foreach(array('id', 'ID', 'name') as $key){ |
| 834 | |
| 835 | if(!acf_maybe_get($array, $key)) continue; |
| 836 | |
| 837 | $param = acf_maybe_get($array, $key); |
| 838 | break; |
| 839 | |
| 840 | } |
| 841 | |
| 842 | // key not found |
| 843 | if(!$param){ |
| 844 | return false; |
| 845 | } |
| 846 | |
| 847 | // unset keys |
| 848 | unset($array['id']); |
| 849 | unset($array['ID']); |
| 850 | unset($array['name']); |
| 851 | |
| 852 | } |
| 853 | |
| 854 | // check id |
| 855 | if(is_numeric($param)){ |
| 856 | |
| 857 | // check post type |
| 858 | if(get_post_type($param) !== 'acfe-form'){ |
| 859 | return false; |
| 860 | } |
| 861 | |
| 862 | // vars |
| 863 | $form_id = $param; |
| 864 | $form_name = get_field('acfe_form_name', $form_id); |
| 865 | |
| 866 | } |
| 867 | |
| 868 | // check name |
| 869 | elseif(is_string($param)){ |
| 870 | |
| 871 | if(!$form = get_page_by_path($param, OBJECT, 'acfe-form')){ |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | // vars |
| 876 | $form_id = $form->ID; |
| 877 | $form_name = get_field('acfe_form_name', $form_id); |
| 878 | |
| 879 | } |
| 880 | |
| 881 | // bail early |
| 882 | if(!$form_name || !$form_id){ |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | // set default params |
| 887 | $array['ID'] = $form_id; |
| 888 | $array['name'] = $form_name; |
| 889 | $array['title'] = get_the_title($form_id); |
| 890 | |
| 891 | return $array; |
| 892 | |
| 893 | } |
| 894 | |
| 895 | } |
| 896 | |
| 897 | acf_new_instance('acfe_form_front'); |
| 898 | |
| 899 | endif; |
| 900 | |
| 901 | function acfe_form($args = array()){ |
| 902 | |
| 903 | acf_get_instance('acfe_form_front')->render_form($args); |
| 904 | |
| 905 | } |