Skip to content

Commit

Permalink
Fixed warning on no elements with input and added Actionhook ‚torro_f…
Browse files Browse the repository at this point in the history
…orm_show_saving_error‘ - Issue #328
  • Loading branch information
mahype committed Nov 14, 2016
1 parent 366182d commit c886994
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
69 changes: 66 additions & 3 deletions core/form-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,16 @@ private function control() {
return;
}

/**
* Should the form be shown
*
* @since 1.0.0
*
* @param boolean $torro_form_show True if form can go further. False if not
* @param int $form_id The ID of the form
*
* @return boolean $torro_form_show Fitlered value if the form can go further or not
*/
$torro_form_show = apply_filters( 'torro_form_show', true, $this->form_id );

if( true !== $torro_form_show ) {
Expand Down Expand Up @@ -426,6 +436,16 @@ private function control() {

$this->cache->add_response( $response );
$is_submit = is_wp_error( $this->form->get_next_container_id() ); // we're in the last step

/**
* Filter to stop form
*
* @since 1.0.0
*
* @param boolean $status True if form can go further. False if not.
*
* @return boolean $status The filtered status
*/
$status = apply_filters( 'torro_response_status', true, $this->form_id, $this->container_id, $is_submit );

/**
Expand All @@ -438,16 +458,52 @@ private function control() {
} else {
$result_id = $this->save_response();

if ( false === $result_id ) {
$this->content = new Torro_Error( 'torro_save_response_error', __( 'Your response couldn\'t be saved.', 'torro-forms' ) );
return;
if ( is_wp_error( $result_id ) ) {
/**
* Filter if the error have to be shown or not
*
* @since 1.0.0
*
* @param boolean $show_saving_error Do we show an error on saving or not
* @param Torro_Error $result_id On Error the $result_id becomes an Torro_Error
*
* @return boolean $show_saving_error Do we show an error on saving or not filtered
*/
$show_saving_error = apply_filters( 'torro_form_show_saving_error', true, $result_id );

if( $show_saving_error ) {
$this->content = $result_id;
return;
}
}

$html = '<div id="torro-thank-submitting">';
$html .= '<p>' . esc_html__( 'Thank you for submitting!', 'torro-forms' ) . '</p>';
$html .= '</div>';

/**
* Doing some action after Torro Response is saved
*
* @since 1.0.0
*
* @param int $form_id The ID of the form
* @param int $result_id The ID of the result
* @param array $response The whole response of the user
*/
do_action( 'torro_response_saved', $this->form_id, $result_id, $this->cache->get_response() );

/**
* Filtering the content which will be displayed after submitting form
*
* @since 1.0.0
*
* @param string $html The content which will be displayed
* @param int $form_id The ID of the form
* @param int $result_id The ID of the result
* @param array $response The whole response of the user
*
* @return string $html The filtered content which will be displayed
*/
$this->content = apply_filters( 'torro_response_saved_content', $html, $this->form_id, $result_id, $this->cache->get_response() );

$this->cache->delete_response();
Expand All @@ -469,6 +525,13 @@ private function control() {
if( isset( $errors[ $this->container_id ]['elements'] )) {
$this->errors = $errors[ $this->container_id ]['elements'];

/**
* Doing some actions if the submission has errors
*
* @since 1.0.0
*
* @param array $errors All current errors from form
*/
do_action( 'torro_submission_has_errors', $this->errors );
}
}
Expand Down
7 changes: 6 additions & 1 deletion core/models/class-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,19 +326,24 @@ public function get_categories() {
* @param int $form_id
* @param array $response
*
* @return boolean $saved
* @return int|Torro_Error $result_id The ID of the result
* @since 1.0.0
*/
public function save_response( $response ) {
$current_user = wp_get_current_user();
$user_id = $current_user && $current_user->exists() ? $current_user->ID : 0;

if( ! array_key_exists( 'containers', $response ) ) {
return new Torro_Error( 'nothing_to_save', __( 'There was nothing to save, because form has no elements containing any input.') );
}

$result_obj = torro()->results()->create( $this->id, array(
'user_id' => $user_id,
'timestamp' => current_time( 'timestamp' ),
'remote_addr' => '',
'cookie_key' => '',
) );

if ( is_wp_error( $result_obj ) ) {
return $result_obj;
}
Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@ You can also contribute to the plugin by translating it. Simply visit [translate
* Enhanced: Added password option to textfield element
* Enhanced: Added 'torro_form_action_url' filter
* Enhanced: Added 'torro_element_type_validate_input' filter for additional validations
* Enhanced: Enhanced code
* Enhanced: Added honeypot spam filter
* Enhanced: Added timetrap spam filter
* Enhanced: Added linkcount spam filter
* Enhanced: Added filter 'torro_form_show_saving_error' on whats happening after data could not be saved
* Fixed: Fixed incompatibility on ACF Calendar CSS on Torro Forms Formbuilder
* Fixed: Element PHP Notices on not existing variable $element_id
* Fixed: Element settings fields have now unique element ids
* Fixed: Page 1 couldn't be deleted
* Fixed: Warning if there is no element with an input.

= 1.0.0-beta.7 =
* Enhanced: Added a new filter for the element data sent to the template
Expand Down

0 comments on commit c886994

Please sign in to comment.