Plugin Directory

Changeset 1164295


Ignore:
Timestamp:
05/20/2015 04:32:59 PM (11 years ago)
Author:
manish2384
Message:

New future : AJAX form submition

Location:
aksh-mailchimp-widget/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • aksh-mailchimp-widget/trunk/aksh-mailchimpwidget.php

    r1164116 r1164295  
    22/*
    33Plugin Name: Aksh Mailchimp Widget
    4 Description: Aksh Mailchimp widget for wordpress to let your users sign up for MailChimp list. You can use it in your any widget area defined in your theme.
     4Description: Aksh Mailchimp widget for wordpress to let your users sign up for MailChimp list using Ajax. You can use it in your any widget area defined in your theme.
    55Version: 4.0
    66Author: Manish H. Gajjar
  • aksh-mailchimp-widget/trunk/include/aksh_widget.php

    r1029926 r1164295  
    66    }
    77    function widget($args, $instance) {
    8         $error = '';
    9         if (isset( $_POST['_amcw_form_nonce'] ) && wp_verify_nonce( $_POST['_amcw_form_nonce'], '_amcw_form_nonce' ) ) {
    10             if ( isset( $_POST['amcw_robocop'] ) && empty( $_POST['amcw_robocop'] ) ) {
    11                 $data = array();
    12                 // Ignore those fields, we don't need them
    13                 $ignored_fields = array( 'CPTCH_NUMBER', 'CNTCTFRM_CONTACT_ACTION', 'CPTCH_RESULT', 'CPTCH_TIME' );
    14                 foreach( $_POST as $key => $value ) {
    15                     // Sanitize key
    16                     $key = trim( strtoupper( $key ) );
    17                     // Skip field if it starts with _ or if it's in ignored_fields array
    18                     if( $key[0] === '_' || in_array( strtoupper( $key ), $ignored_fields ) ) {
    19                         continue;
    20                     }
    21                     // Sanitize value
    22                     $value = ( is_scalar( $value ) ) ? trim( $value ) : $value;
    23                     // Add value to array
    24                     $data[ $key ] = $value;
    25                 }
    26                 // strip slashes on everything
    27                 $data = stripslashes_deep( $data );
    28                 if( ! isset( $data['EMAIL'] ) || ! is_email( $data['EMAIL'] ) ) {
    29                     $error = '<span class="error">'.__('Please provide a valid email address.').'</span>';
    30                 }
    31                 else{
    32                     $name = $data['FNAME'];
    33                     $strpos = strpos( $name, ' ' );
    34 
    35                     if ( $strpos ) {
    36                         $merge_vars['FNAME'] = substr( $name, 0, $strpos );
    37                         $merge_vars['LNAME'] = substr( $name, $strpos );
    38                     } else {
    39                         $merge_vars['FNAME'] = $data['FNAME'];
    40                         $merge_vars['LNAME'] = '';
    41                     }
    42                    
    43                     $email = $data['EMAIL'];
    44                     $opts = amcw_get_options( 'general' );
    45                    
    46                     $api_url = 'https://api.mailchimp.com/2.0/';
    47                     $api_key=$opts['api_key'];
    48                     $list_id=$opts['list_id'];
    49                     if($opts['amcw_double_opt_in']){
    50                         $double_opt_in=true;
    51                     }
    52                     else{
    53                         $double_opt_in=false;
    54                     }
    55                     $email_type='html';
    56                     $method='lists/subscribe';
    57                    
    58                     if( strpos( $api_key, '-' ) !== false ) {
    59                         $api_url = 'https://' . substr( $api_key, -3 ) . '.api.mailchimp.com/2.0/';
    60                     }
    61                     $data = array(
    62                         'apikey' => $api_key,
    63                         'id' => $list_id,
    64                         'email' => array( 'email' => $email),
    65                         'merge_vars' => $merge_vars,
    66                         'email_type' => $email_type,
    67                         'double_optin' => $double_opt_in,
    68                         'update_existing' => false,
    69                         'replace_interests' =>true,
    70                         'send_welcome' => false
    71                     );
    72                    
    73                     $url = $api_url . $method . '.json';
    74                    
    75                     $response = wp_remote_post( $url, array(
    76                         'body' => $data,
    77                         'timeout' => 15,
    78                         'headers' => array('Accept-Encoding' => ''),
    79                         'sslverify' => false
    80                         )
    81                     );
    82                    
    83                     if( is_wp_error( $response ) ) {
    84                         // show error message to admins
    85                         $error = '<span class="error">HTTP Error: ' . $response->get_error_message().'</span>';
    86                     }
    87                     else{
    88                         $body = wp_remote_retrieve_body( $response );
    89                         $result = json_decode( $body );
    90                         if( is_object( $result ) ) {
    91                             if(isset( $result->error ) ) {
    92                                 if( (int) $result->code === 214 ) {
    93                                     $error = '<span class="error">'.__('Given email address is already subscribed, thank you!').'</span>';
    94                                 }
    95                                 // store error message
    96                                 $error = '<span class="error">'.$result->error.'</span>';
    97                             } else {
    98                                 $error = '<span class="success">'.__('Thank you, your sign-up request was successful! Please check your e-mail inbox.').'</span>';
    99                             }
    100                         }
    101                     }
    102                 }
    103             }
    104             else{
    105                 $error = '<span class="error">'.__('Oops. Something went wrong. Please try again later.').'</span>';
    106             }
    107         }
     8        wp_enqueue_script( 'aksh-mailchimp-widget-form', AKSH_PLUGIN_URL.'js/aksh-mailchimp-widgetform.js', array(), '20150405', true );
     9        wp_localize_script( 'aksh-mailchimp-widget-form', 'AkshMailchimpWidgetForm', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ),'amcwFormNonce'=>wp_create_nonce( 'amcw_form_nonce' )));
     10       
    10811        /* Provide some defaults */
    10912        $defaults = array( 'title' => '', 'text_before_form' => '', 'text_after_form' => '');
     
    12528            }
    12629           
    127             echo '<div class="amcw_formwrap"><form class="amcw_form" id="amcw-form" action="" method="post">';
    128            
    129             if($error!=""){echo $error;}
    130            
     30            echo '<div class="amcw_formwrap"><form class="amcw_form" id="amcw-form" action="'.admin_url( 'admin-ajax.php' ).'" method="post">';
     31            echo '<div id="statusmsg"></div>';
    13132            if($opts['form_ishidenamefield']!=true){/**Sign Up form with name field**/
    13233                /**Set default labels if not set from admin**/
     
    19192            }
    19293           
    193             echo '<textarea name="amcw_robocop" style="display: none;"></textarea>';
    194             echo '<input type="hidden" name="_amcw_form_nonce" value="'.wp_create_nonce( '_amcw_form_nonce' ).'" />';
     94            echo '<textarea name="amcw_robocop" id="amcw_robocop" style="display: none;"></textarea>';
     95            //echo '<input type="hidden" name="_amcw_form_nonce" id="_amcw_form_nonce" value="'.wp_create_nonce( '_amcw_form_nonce' ).'" />';
    19596            echo '</form></div>';
    19697           
     
    248149    }
    249150}
     151add_action( 'wp_ajax_nopriv_aksh-mailchimp-widget-submit', 'aksh_mailchimp_widget_submit');
     152add_action( 'wp_ajax_aksh-mailchimp-widget-submit','aksh_mailchimp_widget_submit');
     153function aksh_mailchimp_widget_submit(){
     154    $error = '';
     155    $amcwFormNonce=$_POST['amcwFormNonce'];
     156    if ( ! wp_verify_nonce( $amcwFormNonce, 'amcw_form_nonce' ))
     157        die ( 'Busted!');
     158    if ( isset( $_POST['amcw_robocop'] ) && empty( $_POST['amcw_robocop'] ) ) {
     159        $data = array();
     160        // Ignore those fields, we don't need them
     161        $ignored_fields = array( 'CPTCH_NUMBER', 'CNTCTFRM_CONTACT_ACTION', 'CPTCH_RESULT', 'CPTCH_TIME' );
     162        foreach( $_POST as $key => $value ) {
     163            // Sanitize key
     164            $key = trim( strtoupper( $key ) );
     165            // Skip field if it starts with _ or if it's in ignored_fields array
     166            if( $key[0] === '_' || in_array( strtoupper( $key ), $ignored_fields ) ) {
     167                continue;
     168            }
     169            // Sanitize value
     170            $value = ( is_scalar( $value ) ) ? trim( $value ) : $value;
     171            // Add value to array
     172            $data[ $key ] = $value;
     173        }
     174        // strip slashes on everything
     175        $data = stripslashes_deep( $data );
     176        if( ! isset( $data['EMAIL'] ) || ! is_email( $data['EMAIL'] ) ) {
     177            $error = '<span class="error">'.__('Please provide a valid email address.').'</span>';
     178        }
     179        else{
     180            $name = $data['FNAME'];
     181            $strpos = strpos( $name, ' ' );
     182
     183            if ( $strpos ) {
     184                $merge_vars['FNAME'] = substr( $name, 0, $strpos );
     185                $merge_vars['LNAME'] = substr( $name, $strpos );
     186            } else {
     187                $merge_vars['FNAME'] = $data['FNAME'];
     188                $merge_vars['LNAME'] = '';
     189            }
     190           
     191            $email = $data['EMAIL'];
     192            $opts = amcw_get_options( 'general' );
     193           
     194            $api_url = 'https://api.mailchimp.com/2.0/';
     195            $api_key=$opts['api_key'];
     196            $list_id=$opts['list_id'];
     197            if($opts['amcw_double_opt_in']){
     198                $double_opt_in=true;
     199            }
     200            else{
     201                $double_opt_in=false;
     202            }
     203            $email_type='html';
     204            $method='lists/subscribe';
     205           
     206            if( strpos( $api_key, '-' ) !== false ) {
     207                $api_url = 'https://' . substr( $api_key, -3 ) . '.api.mailchimp.com/2.0/';
     208            }
     209            $data = array(
     210                'apikey' => $api_key,
     211                'id' => $list_id,
     212                'email' => array( 'email' => $email),
     213                'merge_vars' => $merge_vars,
     214                'email_type' => $email_type,
     215                'double_optin' => $double_opt_in,
     216                'update_existing' => false,
     217                'replace_interests' =>true,
     218                'send_welcome' => false
     219            );
     220           
     221            $url = $api_url . $method . '.json';
     222           
     223            $response = wp_remote_post( $url, array(
     224                'body' => $data,
     225                'timeout' => 15,
     226                'headers' => array('Accept-Encoding' => ''),
     227                'sslverify' => false
     228                )
     229            );
     230           
     231            if( is_wp_error( $response ) ) {
     232                // show error message to admins
     233                $error = '<span class="error">HTTP Error: ' . $response->get_error_message().'</span>';
     234            }
     235            else{
     236                $body = wp_remote_retrieve_body( $response );
     237                $result = json_decode( $body );
     238                if( is_object( $result ) ) {
     239                    if(isset( $result->error ) ) {
     240                        if( (int) $result->code === 214 ) {
     241                            $error = '<span class="error">'.__('Given email address is already subscribed, thank you!').'</span>';
     242                        }
     243                        // store error message
     244                        $error = '<span class="error">'.$result->error.'</span>';
     245                        $response = json_encode( array( 'fail' => true, 'message'=>$error, 'amcwFormNonce'=>$amcwFormNonce) );
     246                    } else {
     247                        $error = '<span class="success">'.__('Thank you, your sign-up request was successful! Please check your e-mail inbox.').'</span>';
     248                        $response = json_encode( array( 'success' => true, 'message'=>$error, 'amcwFormNonce'=>$amcwFormNonce) );
     249                    }
     250                }
     251            }
     252        }
     253    }
     254    else{
     255        $error = '<span class="error">'.__('Oops. Something went wrong. Please try again later.').'</span>';
     256        $response = json_encode( array( 'fail' => true, 'message'=>$error,'amcwFormNonce'=>$amcwFormNonce) );
     257    }
     258    header( "Content-Type: application/json" );
     259    echo $response;
     260    // IMPORTANT: don't forget to "exit"
     261    exit;
     262}
    250263function amcw_widget_init() {
    251264    register_widget('akshMailchimpWidget');
  • aksh-mailchimp-widget/trunk/readme.txt

    r1164116 r1164295  
    55Requires at least: 3.0.1
    66Tested up to: 4.2.2
    7 Stable tag: 4.0
    87License: GPLv2 or later
    98License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1615
    1716Aksh Mailchimp widget very easy to handle in widgets. You can also add HTML texts before and/or after your mailchimp sign up form from widget. All sign up form elements are css class name ready to easy style with your theme stylesheet.
     17
     18In latest version now the form uses AJAX posting to submit form data to MailChimp.
    1819
    1920== Installation ==
Note: See TracChangeset for help on using the changeset viewer.