'aksh_mailchimp_widget', 'description' => __('Displays a mailchimp sign-up form.')); parent::__construct(false, 'Aksh Mailchimp Signup', $widget_ops); } function widget($args, $instance) { $error = ''; if (isset( $_POST['_amcw_form_nonce'] ) && wp_verify_nonce( $_POST['_amcw_form_nonce'], '_amcw_form_nonce' ) ) { if ( isset( $_POST['amcw_robocop'] ) && empty( $_POST['amcw_robocop'] ) ) { $data = array(); // Ignore those fields, we don't need them $ignored_fields = array( 'CPTCH_NUMBER', 'CNTCTFRM_CONTACT_ACTION', 'CPTCH_RESULT', 'CPTCH_TIME' ); foreach( $_POST as $key => $value ) { // Sanitize key $key = trim( strtoupper( $key ) ); // Skip field if it starts with _ or if it's in ignored_fields array if( $key[0] === '_' || in_array( strtoupper( $key ), $ignored_fields ) ) { continue; } // Sanitize value $value = ( is_scalar( $value ) ) ? trim( $value ) : $value; // Add value to array $data[ $key ] = $value; } // strip slashes on everything $data = stripslashes_deep( $data ); if( ! isset( $data['EMAIL'] ) || ! is_email( $data['EMAIL'] ) ) { $error = ''.__('Please provide a valid email address.').''; } else{ $name = $data['FNAME']; $strpos = strpos( $name, ' ' ); if ( $strpos ) { $merge_vars['FNAME'] = substr( $name, 0, $strpos ); $merge_vars['LNAME'] = substr( $name, $strpos ); } else { $merge_vars['FNAME'] = $data['FNAME']; $merge_vars['LNAME'] = ''; } $email = $data['EMAIL']; $opts = amcw_get_options( 'general' ); $api_url = 'https://api.mailchimp.com/2.0/'; $api_key=$opts['api_key']; $list_id=$opts['list_id']; if($opts['amcw_double_opt_in']){ $double_opt_in=true; } else{ $double_opt_in=false; } $email_type='html'; $method='lists/subscribe'; if( strpos( $api_key, '-' ) !== false ) { $api_url = 'https://' . substr( $api_key, -3 ) . '.api.mailchimp.com/2.0/'; } $data = array( 'apikey' => $api_key, 'id' => $list_id, 'email' => array( 'email' => $email), 'merge_vars' => $merge_vars, 'email_type' => $email_type, 'double_optin' => $double_opt_in, 'update_existing' => false, 'replace_interests' =>true, 'send_welcome' => false ); $url = $api_url . $method . '.json'; $response = wp_remote_post( $url, array( 'body' => $data, 'timeout' => 15, 'headers' => array('Accept-Encoding' => ''), 'sslverify' => false ) ); if( is_wp_error( $response ) ) { // show error message to admins $error = 'HTTP Error: ' . $response->get_error_message().''; } else{ $body = wp_remote_retrieve_body( $response ); $result = json_decode( $body ); if( is_object( $result ) ) { if(isset( $result->error ) ) { if( (int) $result->code === 214 ) { $error = ''.__('Given email address is already subscribed, thank you!').''; } // store error message $error = ''.$result->error.''; } else { $error = ''.__('Thank you, your sign-up request was successful! Please check your e-mail inbox.').''; } } } } } else{ $error = ''.__('Oops. Something went wrong. Please try again later.').''; } } /* Provide some defaults */ $defaults = array( 'title' => '', 'text_before_form' => '', 'text_after_form' => ''); $instance = wp_parse_args( (array) $instance, $defaults ); extract( $args ); extract($instance); $title = apply_filters('widget_title', $title); echo $before_widget; if($title!=""){ echo $before_title . $title . $after_title; } $opts = amcw_get_options( 'general' ); if($opts['api_key']!="" && $opts['list_id']!=""){ if(!empty($text_before_form)) { ?>
'; if($error!=""){echo $error;} if($opts['form_ishidenamefield']!=true){/**Sign Up form with name field**/ /**Set default labels if not set from admin**/ if($opts['aksh_form_labelname']!=""){ $label_namefield=$opts['aksh_form_labelname']; } else{ $label_namefield=__('Your Name'); } if($opts['aksh_form_labelemail']!=""){ $label_emailfield=$opts['aksh_form_labelemail']; } else{ $label_emailfield=__('Your Email'); } /**Set default labels for subscribe button if not set from admin**/ if($opts['aksh_form_labelsubscribe']!=""){ $label_subscribebuton=$opts['aksh_form_labelsubscribe']; } else{ $label_subscribebuton=__('Subscribe'); } /**Set labels of placeholder as per admin settings*/ if($opts['form_isplaceholder']==true){ echo '

'; echo '

'; } else{ echo '

'; echo '

'; echo '

'; echo '

'; } echo '

'; } else{/**Sign Up form without name field**/ /**Set default labels if not set from admin**/ if($opts['aksh_form_labelemail']!=""){ $label_emailfield=$opts['aksh_form_labelemail']; } else{ $label_emailfield=__('Your Email'); } /**Set default labels for subscribe button if not set from admin**/ if($opts['aksh_form_labelsubscribe']!=""){ $label_subscribebuton=$opts['aksh_form_labelsubscribe']; } else{ $label_subscribebuton=__('Subscribe'); } /**Set labels of placeholder as per admin settings*/ if($opts['form_isplaceholder']==true){ echo ''; echo ''; } else{ echo '

'; echo '

'; echo '

'; } } echo ''; echo ''; echo '
'; if(!empty($text_after_form)) { ?>
__(''), 'text_before_form' => '', 'text_after_form' => ''); $instance = wp_parse_args( (array) $instance, $defaults ); extract($instance); $title = strip_tags($title); ?>

/>  

Important: Go to Settings->Aksh MailChimp, if you forget to set MailChimp API and List ID.

Donate small amount to appreciate my work and inspire me to create more plugins and widgets.

Donate Button with Credit Cards