Display more errors from mailster
-
Hi, I improved the validate and add_subscriber function in classes/contactform.class.php to allow contact from7 to display more errors from mailster instead of just error messages for duplicate subscribers.
public function validate( $result, $tags ) {
if ( ! $result->is_valid() ) {
return $result;
}
if ( ! function_exists( 'mailster' ) ) {
return $result;
}
$submission = WPCF7_Submission::get_instance();
if ( ! $submission || ! $posted_data = $submission->get_posted_data() ) {
return $result;
}
$form = WPCF7_ContactForm::get_current();
$properties = $form->get_properties();
// no Mailster settings
if ( ! isset( $properties['mailster'] ) ) {
return $result;
}
$properties = $properties['mailster'];
// not enabled
if ( ! $properties['enabled'] ) {
return $result;
}
// checkbox defined but not checked
if ( isset( $properties['checkbox'] ) && $properties['checkbox'] && empty( $posted_data[ $properties['checkboxfield'] ][0] ) ) {
return $result;
}
$this->userdata = array();
$tag_keys = array_flip( array_filter( wp_list_pluck( $tags, 'name' ) ) );
foreach ( $properties['fields'] as $field => $tag ) {
$this->userdata[ $field ] = is_array( $posted_data[ $tag ] ) ? $posted_data[ $tag ][0] : $posted_data[ $tag ];
}
$this->userdata['status'] = $properties['doubleoptin'] ? 0 : 1;
if ( isset( $properties['gdpr_timestamp'] ) && $properties['gdpr_timestamp'] ) {
$this->userdata['gdpr'] = time();
}
$this->userdata = apply_filters( 'mailster_verify_subscriber', $this->userdata );
if ( is_wp_error( $this->userdata ) ) {
$result->invalidate( $tags[ $tag_keys[ $properties['fields'][ $this->userdata->get_error_code() ] ] ], $this->userdata->get_error_message() );
return $result;
}
$overwrite = $properties['overwrite'];
if ( ! $overwrite && mailster( 'subscribers' )->get_by_mail( $this->userdata['email'] ) ) {
$error_message = isset( $properties['error_message'] ) ? $properties['error_message'] : __( 'You are already registered!', 'mailster-cf7' );
$result->invalidate( $tags[ $tag_keys[ $properties['fields']['email'] ] ], $error_message );
return $result;
}
//new add+
$add_message= $this->add_subscriber(null);
if(!empty($add_message)) {
$result->invalidate( $tags[ $tag_keys[ $properties['fields']['email'] ] ], $add_message );
return $result;
}
// add_action( 'wpcf7_mail_sent', array( $this, 'add_subscriber' ) );
return $result;
}
/**
*
*
* @param unknown $contact_form
*/
public function add_subscriber( $contact_form ) {
$form = WPCF7_ContactForm::get_current();
$properties = $form->get_properties();
$properties = $properties['mailster'];
$list_ids = isset( $properties['lists'] ) ? (array) $properties['lists'] : null;
$overwrite = 1 == $properties['overwrite'];
$merge = 3 == $properties['overwrite'];
// add subscriber
$subscriber_id = mailster( 'subscribers' )->add( $this->userdata, $overwrite || $merge, $merge );
// no error
if ( ! is_wp_error( $subscriber_id ) && $list_ids ) {
mailster( 'subscribers' )->assign_lists( $subscriber_id, $list_ids );
return '';
}else {
return $subscriber_id->get_error_message();
}
}
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- You must be logged in to reply to this review.