Plugin Contributor
Lap
(@lapzor)
As you’re using our Premium add-on, please contact us on support at mc4wp.com for Premium support. Thank you!
Hello there, Lapzor!
We have requested the account details from our client and are waiting for them to get back to us.
Do you have any further troubleshooting tips or advice we can follow whilst we wait for that information, please?
Hello there, Lapzor!
Relatedly, we have another client that is using the free version of the plugin that is exhibiting identical behaviour. On https://www.point6.co.uk/view-our-work/ when either scrolling down or clicking on the ‘Sign up to our newsletter >’ link in the footer, the previously functional form is now returning the same ‘Oops. Something went wrong. Please try again later’ error.
[2024-10-03 09:02:20] ERROR: Form 8304 > Mailchimp API error: 400 Bad Request. Invalid Resource. The resource submitted could not be validated. - email_address : This value should not be blank.
Request:
POST https://us18.api.mailchimp.com/3.0/lists/c2ee4a285e/members
{"status":"pending","email_address":"","interests":{},"merge_fields":{"FNAME":"Testing","LNAME":"Testerson"},"email_type":"html","ip_signup":"212.82.65.104","tags":[]}
Response:
400 Bad Request
{"type":"https://mailchimp.com/developer/marketing/docs/errors/","title":"Invalid Resource","status":400,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"0d65f1f8-6cce-c298-353e-77930a891557","errors":[{"field":"email_address","message":"This value should not be blank."}}
Plugin Contributor
Lap
(@lapzor)
Are both sites using the same theme?
Maybe the theme has some custom code targeting MC4WP.
Are you able to set up a staging site and test with another theme?
Hello there, Lapzor!
The sites are using different custom themes that we developed ourselves. Both are child-themes based on Bootscore.
We have a staging environment for both sites. I shall give swapping the theme over to TwentyFour a crack, and shall let you know.
Hello there, Lapzor!
Thank you very much for the suggestion regarding swapping the theme out – it let us isolate the problem. We have some custom code to handle file uploads through a MC4WP form, that had stopped correctly working.
Summary – the file upload success check was incorrectly being applied to all forms.
Code Reference – For reference (and any other users that might find this in future), here is the full code:
class File_Uploads_Mailchimp extends Child_Theme {
/**
* Add file upload support to MC4WP forms
*
* @param array $data
* @param \MC4WP_Form $form
*
* @return array
*/
public function process_form_data($data, MC4WP_Form $form) {
$file_successfully_processed = false;
$field_name = "RECEIPT";
if (
isset($_FILES[$field_name]) &&
$_FILES[$field_name]['error'] === UPLOAD_ERR_OK // uploaded okay
) {
// Allowed mime-types and extensions
$allowed = [
"jpg" => "image/jpeg",
"png" => "image/png",
];
// Ensure needed functions exist before attempting to call:
$wp_admin_includes = ABSPATH . 'wp-admin/includes';
if ( !function_exists('wp_handle_upload') ) {
require_once( "{$wp_admin_includes}/file.php" );
}
if ( !function_exists('media_handle_upload') ) {
require_once( "{$wp_admin_includes}/media.php" );
}
if ( !function_exists('wp_read_image_metadata') ) {
require_once( "{$wp_admin_includes}/image.php" );
}
$file_type = $_FILES[$field_name]['type']; //returns the mimetype
if (
in_array($file_type, $allowed) && // Doubly ensure only images are uploaded
(int) $_FILES[$field_name]['size'] < 2621440 // 2.5MB in bytes
) {
// Handle file upload:
add_filter('upload_dir', [ $this, 'alter_upload_directory' ]); // Change the upload directory for this upload only
$upload = wp_handle_upload($_FILES[$field_name], [
'test_form' => false,
'test_file' => false,
'mimes' => $allowed,
]);
remove_filter('upload_dir', [ $this, 'alter_upload_directory' ]); // Remove the directory change.
if (
$upload &&
!isset($upload['error'])
) {
// Store the file URL in subscriber data:
$data[$field_name] = $upload['url'];
$file_successfully_processed = true;
}
}
}
if (
/* THIS ISSET() CHECK WAS MISSING, ADDING THIS ISSET() FIXED THE PROBLEM IN THIS THREAD */
isset($_FILES[$field_name]) &&
$file_successfully_processed !== true
) {
// empty data to prevent signup, but provide a receipt URL to ensure any later hooks can delete the file
// uploaded if necessary, such as on a submission error.
$data = [
'EMAIL' => '', // prevents a fatal error
'RECEIPT' => ( isset($data[$field_name]) && $data[$field_name] ) ? $data[$field_name] : '',
];
if (
isset($upload['file']) &&
$upload['file'] &&
file_exists($upload['file']) &&
is_writeable($upload['file'])
) {
unlink($upload['file']);
}
}
return $data;
}
/**
* Add the required enctype to the form.
*
* @param array $attributes
* @param $form
*/
function add_form_attributes($attributes, $form) {
$attributes['enctype'] = "multipart/form-data";
$attributes['accept'] = "image/png, image/jpeg, image/jpg";
return $attributes;
}
/**
* Changes the upload directory
*
* @param $upload
*
* @return mixed
*/
function alter_upload_directory($upload) {
$upload['subdir'] = "/_receipt-images{$upload['subdir']}";
$upload['path'] = "{$upload['basedir']}{$upload['subdir']}";
$upload['url'] = "{$upload['baseurl']}{$upload['subdir']}";
return $upload;
}
function _var_export_form($form) {
self::var_export($form);
}
public function __construct() {
parent::__construct();
$this->add_filters_from_array([
[ 'mc4wp_form_data', [ $this, 'process_form_data' ], 10, 2 ],
[ 'mc4wp_form_element_attributes', [ $this, 'add_form_attributes' ], 10, 2 ],
]);
}
}
File_Uploads_Mailchimp::get_instance();
Plugin Contributor
Lap
(@lapzor)
Great, thanks for sharing.