$module = Module::instance();
$actions = $module->actions_registrar->get();
$errors = array_merge( $this->messages['error'], $this->messages['admin_error'] );
/**
* Filters the record before it sent to actions after submit.
*
* @since 3.3.0
*
* @param Form_Record $record The form record.
* @param Ajax_Handler $this The class that handle the submission of the record
*/
$record = apply_filters( 'elementor_pro/forms/record/actions_before', $record, $this );
foreach ( $actions as $action ) {
if ( ! in_array( $action->get_name(), $form['settings']['submit_actions'], true ) ) {
continue;
}
$exception = null;
try {
$action->run( $record, $this );
$this->handle_bc_errors( $errors );
} catch ( \Exception $e ) {
$exception = $e;
// Add an admin error.
if ( ! in_array( $exception->getMessage(), $this->messages['admin_error'], true ) ) {
$this->add_admin_error_message( "{$action->get_label()} {$exception->getMessage()}" );
}
// Add a user error.
$this->add_error_message( $this->get_default_message( self::ERROR, $this->current_form['settings'] ) );
}
$errors = array_merge( $this->messages['error'], $this->messages['admin_error'] );
do_action( 'elementor_pro/forms/actions/after_run', $action, $exception );
}
$activity_log = $module->get_component( 'activity_log' );
if ( $activity_log ) {
$activity_log->run( $record, $this );
}
$cf7db = $module->get_component( 'cf7db' );
if ( $cf7db ) {
$cf7db->run( $record, $this );
}
/**
* New Elementor form record.
*
* Fires before a new form record is send by ajax.
*
* @since 1.0.0
*
* @param Form_Record $record An instance of the form record.
* @param Ajax_Handler $this An instance of the ajax handler.
*/
do_action( 'elementor_pro/forms/new_record', $record, $this );
$this->send();
}
public function add_success_message( $message ) {
$this->messages['success'][] = $message;
return $this;
}
public function add_response_data( $key, $data ) {
$this->data[ $key ] = $data;
return $this;
}
public function add_error_message( $message ) {
$this->messages['error'][] = $message;
$this->set_success( false );
return $this;
}
public function add_error( $field, $message = '' ) {
if ( is_array( $field ) ) {
$this->errors += $field;
} else {
$this->errors[ $field ] = $message;
}
$this->set_success( false );
return $this;
}
public function add_admin_error_message( $message ) {
$this->messages['admin_error'][] = $message;
$this->set_success( false );
return $this;
}
public function set_success( $bool ) {
$this->is_success = $bool;
return $this;
}
public function send() {
if ( $this->is_success ) {
wp_send_json_success( [
'message' => $this->get_default_message( self::SUCCESS, $this->current_form['settings'] ),
'data' => $this->data,
] );
}
if ( empty( $this->messages['error'] ) && ! empty( $this->errors ) ) {
$this->add_error_message( $this->get_default_message( self::INVALID_FORM, $this->current_form['settings'] ) );
}
$error_msg = implode( '
', $this->messages['error'] );
if ( current_user_can( 'edit_post', $_POST['post_id'] ) && ! empty( $this->messages['admin_error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$this->add_admin_error_message( esc_html__( 'This Message is not visible for site visitors.', 'elementor-pro' ) );
$error_msg .= '