Last active
March 20, 2020 11:57
-
-
Save polevaultweb/2ff67c8ed4364bd8b0b211b9d70e4f52 to your computer and use it in GitHub Desktop.
Ninja Forms File Uploads - temporary fix to ensure file uploads still work after the unique email error is triggered in a form submission
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'plugins_loaded', function () { | |
/** | |
* Submission class extension so we can access the protected errors property. | |
* | |
* Class NF_FU_AJAX_Controllers_Submission | |
*/ | |
class NF_FU_AJAX_Controllers_Submission extends NF_AJAX_Controllers_Submission { | |
public function get_errors() { | |
return $this->_errors; | |
} | |
} | |
/** | |
* Convert a Submission object to our class. | |
* | |
* @param NF_AJAX_Controllers_Submission $submission | |
* | |
* @return NF_FU_AJAX_Controllers_Submission | |
*/ | |
function nf_fu_convert_submission_object( $submission ) { | |
$class = 'NF_FU_AJAX_Controllers_Submission'; | |
return unserialize( preg_replace( '/^O:\d+:"[^"]++"/', 'O:' . strlen( $class ) . ':"' . $class . '"', serialize( $submission ) ) ); | |
} | |
add_action( 'ninja_forms_before_response', 'nf_fu_unique_email_error_fix' ); | |
function nf_fu_unique_email_error_fix( $data ) { | |
$submission = Ninja_Forms()->controllers['submission']; | |
if ( ! $submission ) { | |
return; | |
} | |
$fu_submission = nf_fu_convert_submission_object( $submission ); | |
if ( ! $fu_submission || empty( $fu_submission->get_errors() ) ) { | |
return; | |
} | |
$errors = $fu_submission->get_errors(); | |
if ( ! isset( $errors['fields'] ) || empty( $errors['fields'] ) ) { | |
return; | |
} | |
$unique_field_error = false; | |
foreach ( $errors['fields'] as $field_id => $field_error ) { | |
if ( $field_error['slug'] === 'unique_field' ) { | |
$unique_field_error = true; | |
break; | |
} | |
} | |
if ( ! $unique_field_error ) { | |
return; | |
} | |
$tmp_dir = NF_File_Uploads()->controllers->uploads->get_temp_dir(); | |
foreach ( $data['fields'] as $field_id => $field ) { | |
if ( $field['type'] !== NF_FU_File_Uploads::TYPE ) { | |
continue; | |
} | |
foreach ( $field['files'] as $file ) { | |
$tmp_file = $tmp_dir . '/' . $file['tmp_name']; | |
copy( $file['data']['file_path'], $tmp_file ); | |
} | |
} | |
return; | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment