submission; if ( empty( $submission ) ) { return false; } if ( $hash && $hash != amem_form_instance_hash( $submission['form']['key'], $submission['args'] ) ) { return false; } return true; } /** * Return true if submission failed * * @since 1.0 */ function amem_submission_failed( $key = false ) { $submission = amem()->submission; if ( empty( $submission ) ) { return false; } if ( $key && $key != $submission['form']['key'] ) { return false; } if ( isset( $submission['errors'] ) && ! empty( $submission['errors'] ) ) { return true; } if ( amem()->errors->has_errors() ) return true; return false; } /** * Adds a general error for a form submission. * Used during the before_submission hook to stop submission. * * @since 1.0 */ function amem_add_submission_error( $message, $input=null ) { return amem()->errors->add($input, $message); } /** * AMem errors are basically controlled by ACF * * @since 1.0 * */ function amem_add_error( $field_key_or_name, $message ) { // Form-level / internal errors field → ACF global error (form notice), not a field notice. $form_level_keys = array( false, '', 'field_amem_errors', '_amem_errors' ); if ( in_array( $field_key_or_name, $form_level_keys, true ) ) { acf_add_validation_error( false, $message ); return; } $field = amem_get_field_object( $field_key_or_name ); if ( $field ) { if ( $field['prefix'] ) $input_name = sprintf( '%s[%s]', $field['prefix'], $field['key'] ); acf_add_validation_error( $input_name, $message ); } else { acf_add_validation_error( false, $message ); } } /** * Add inline error message without submission * @since 1.0.0 * @param string $code * @param string $message */ function amem_add_form_error( $code, $message ) { return amem()->errors->form_error($code, $message); } /** * Calculates a unique hash for a form instance, based on the form key and arguments. * * @since 1.0 * */ function amem_form_instance_hash( $form_key, $args ) { $args['form'] = $form_key; // Sort args to make the hash order-independent ksort( $args ); return md5( wp_json_encode( $args ) ); }