<?php
/**
* Form builder class
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'Jet_Engine_Booking_Forms_Handler' ) ) {
/**
* Define Jet_Engine_Booking_Forms_Handler class
*/
class Jet_Engine_Booking_Forms_Handler {
public $hook_key = 'jet_engine_action';
public $hook_val = 'book';
public $form = null;
public $form_fields = array();
public $refer = null;
public $manager = null;
public $notifcations = null;
public $form_data = array();
public $response_data = array();
public $is_ajax = false;
public $repeaters = array();
/**
* Constructor for the class
*/
function __construct( $manager ) {
if ( wp_doing_ajax() ) {
add_action( 'wp_ajax_jet_engine_form_booking_submit', array( $this, 'process_ajax_form' ) );
add_action( 'wp_ajax_nopriv_jet_engine_form_booking_submit', array( $this, 'process_ajax_form' ) );
} else {
if ( ! isset( $_REQUEST[ $this->hook_key ] ) || $this->hook_val !== $_REQUEST[ $this->hook_key ] ) {
return;
}
add_action( 'wp_loaded', array( $this, 'process_form' ), 0 );
}
$this->manager = $manager;
}
/**
* Is ajax form processing or not
*
* @return boolean [description]
*/
public function is_ajax() {
return $this->is_ajax;
}
/**
* Setup form data
*
* @return [type] [description]
*/
public function setup_form() {
$form_key = '_jet_engine_booking_form_id';
$refer_key = '_jet_engine_refer';
if ( ! $this->is_ajax ) {
$this->form = ! empty( $_REQUEST[ $form_key ] ) ? $_REQUEST[ $form_key ] : false;
$this->refer = ! empty( $_REQUEST[ $refer_key ] ) ? $_REQUEST[ $refer_key ] : false;
} else {
$values = ! empty( $_REQUEST['values'] ) ? $_REQUEST['values'] : array();
foreach ( $values as $data ) {
if ( $data['name'] === $form_key ) {
$this->form = $data['value'];
}
if ( $data['name'] === $refer_key ) {
$this->refer = $data['value'];
}
}
}
}
/**
* Check if current form has configured gateway
*
* @return boolean [description]
*/
public function has_gateway() {
return apply_filters( 'jet-engine/forms/handler/has-gateways', false, $this->form );
}
/**
* Process form with Ajax
*
* @return [type] [description]
*/
public function process_ajax_form() {
$this->is_ajax = true;
$this->process_form();
}
/**
* Process current form
*
* @return [type] [description]
*/
public function process_form() {
$this->setup_form();
$data = $this->get_form_data();
require_once jet_engine()->modules->modules_path( 'forms/notifications.php' );
$this->notifcations = new Jet_Engine_Booking_Forms_Notifications(
$this->form,
$data,
$this->manager,
$this
);
do_action( 'jet-engine/forms/handler/before-send', $this );
$success = $this->notifcations->send();
do_action( 'jet-engine/forms/handler/after-send', $this, $success );
if ( true === $success ) {
$this->redirect( array(
'status' => 'success',
) );
} else {
$this->redirect( array(
'status' => 'failed',
) );
}
}
/**
* Add new properties into response data
*
* @param array $data [description]
*/
public function add_response_data( $data = array() ) {
$this->response_data = array_merge( $this->response_data, $data );
}
/**
* Redirect back to refer
* @param array $args [description]
* @return [type] [description]
*/
public function redirect( $args = array() ) {
$args = wp_parse_args( $args, array(
'status' => 'success',
) );
$error_statuses = array( 'validation_failed', 'invalid_email' );
if ( $this->notifcations ) {
$specific_status = $this->notifcations->get_specific_status();
} else {
$specific_status = false;
}
if ( ! empty( $specific_status ) ) {
$args['status'] = $specific_status;
}
$query_args = array(
'status' => $args['status'],
);
$query_args = array_merge( $query_args, $this->response_data );
// Clear form-related arguments
$this->refer = remove_query_arg( array( 'values', 'status', 'fields' ), $this->refer );
if ( 'validation_failed' === $args['status'] ) {
if ( $this->is_ajax ) {
$query_args['fields'] = $args['errors'];
} else {
$query_args['fields'] = implode( ',', $args['errors'] );
}
}
$send_values = apply_filters( 'jet-engine/forms/booking/handler/send-values-on-error', true );
if ( ! $this->is_ajax && $send_values && in_array( $args['status'], $error_statuses ) ) {
$query_args['values'] = $this->form_data;
}
$query_args = apply_filters( 'jet-engine/forms/handler/query-args', $query_args, $args, $this );
if ( $this->is_ajax ) {
$messages = jet_engine()->forms->get_messages_builder( $this->form );
ob_start();
$messages->set_form_status( $args['status'] );
$messages->render_messages();
$query_args['message'] = ob_get_clean();
if ( 'validation_failed' === $args['status'] ) {
ob_start();
$messages->render_empty_field_message();
$query_args['field_message'] = ob_get_clean();
}
wp_send_json( $query_args );
} else {
$redirect = add_query_arg( $query_args, $this->refer );
wp_redirect( $redirect );
die();
}
}
/**
* Get form values from request
*
* @return [type] [description]
*/
public function get_values_from_request() {
if ( $this->is_ajax ) {
$prepared = array();
$raw = ! empty( $_REQUEST['values'] ) ? $_REQUEST['values'] : array();
if ( empty( $raw ) ) {
return $prepared;
}
foreach ( $raw as $data ) {
$name = $data['name'];
$value = $data['value'];
if ( preg_match( '/\[\d\]\[/', $name ) ) {
$name_parts = explode( '[', $name );
$name = $name_parts[0];
$index = absint( rtrim( $name_parts[1], ']' ) );
$key = rtrim( $name_parts[2], ']' );
if ( empty( $prepared[ $name ] ) ) {
$prepared[ $name ] = array();
}
if ( empty( $prepared[ $name ][ $index ] ) ) {
$prepared[ $name ][ $index ] = array();
}
if ( isset( $name_parts[3] ) ) {
if ( empty( $prepared[ $name ][ $index ][ $key ] ) ) {
$prepared[ $name ][ $index ][ $key ] = array();
}
$prepared[ $name ][ $index ][ $key ][] = $value;
} else {
$prepared[ $name ][ $index ][ $key ] = $value;
}
} elseif ( false !== strpos( $name, '[]') ) {
$name = str_replace( '[]', '', $name );
if ( empty( $prepared[ $name ] ) ) {
$prepared[ $name ] = array();
}
$prepared[ $name ][] = $value;
} else {
$prepared[ $name ] = $value;
}
}
return $prepared;
} else {
return $_REQUEST;
}
}
/**
* Get submitted form data
*
* @return [type] [description]
*/
public function get_form_data() {
$fields = $this->manager->editor->get_form_data( $this->form );
$data = array();
$errors = array();
$invalid_email = false;
$request = $this->get_values_from_request();
$skip_fields = array( 'submit', 'page_break', 'heading', 'group_break', 'repeater_end' );
$in_repeater = false;
$current_repeater = null;
usort( $fields, function( $a, $b ) {
if ( $a['y'] == $b['y'] ) {
return 0;
}
return ( $a['y'] < $b['y'] ) ? -1 : 1;
} );
foreach ( $fields as $field ) {
if ( in_array( $field['settings']['name'], $skip_fields ) ) {
continue;
}
if ( in_array( $field['settings']['type'], array( 'heading', 'group_break', 'repeater_end' ) ) ) {
if ( 'repeater_end' === $field['settings']['type'] ) {
$in_repeater = false;
$current_repeater = null;
}
Loading ...