EvanHerman / yikes-inc-easy-mailchimp-extender

Easy Forms for MailChimp WordPress Plugin
http://yikesplugins.com
GNU General Public License v2.0
25 stars 28 forks source link

Fatal error received if already signed up #57

Closed bshuchter closed 10 years ago

bshuchter commented 10 years ago

Hi. The plugin displayed the following error when I submitted an email that was already subscribed to the list (plugin 3.01 with Wordpress 3.81)

Fatal error: Uncaught exception 'Mailchimp_List_AlreadySubscribed' with message 'email@email.com is already subscribed to list Subscribers. Click here to update your profile.' in /home/content/ourwebsite/wp-content/plugins/yikes-inc-easy-mailchimp-extender/classes/MCAPI_2.0.class.php:354 Stack trace: #0 /home/content/ourwebsite/wp-content/plugins/yikes-inc-easy-mailchimp-extender/classes/MCAPI_2.0.class.php(333): wpyksMCAPI->castError(Array) #1 /home/content/ourwebsite/wp-content/plugins/yikes-inc-easy-mailchimp-extender/classes/class.yksemeBase.php(882): wpyksMCAPI->call('lists/subscribe', Array) #2 /home/content/ourwebsite/wp-content/plugins/yikes-inc-easy-mailchimp-extender/process/ajax.php(74): yksemeBase->addUserToMailchimp(Array) #3 /home/content/ourwebsite/wp-content/plugins/yikes-inc-easy-mailchimp-extender/lib/lib.ajax.php(8): require_once('/hom in /home/content/ourwebsite/wp-content/plugins/yikes-inc-easy-mailchimp-extender/classes/MCAPI_2.0.class.php on line 354

EvanHerman commented 10 years ago

Hi bshuchter,

This error appears when the user has previously signed up for the listing. Int he new version of the plugin we have set up a function to catch that error message and display a more styled and user friendly message.

In the meantime, If you want to implement a fix now, open up the file 'class.yksemeBase.php' inside of yikes-inc-easy-mailchimp-extender > classes and find the function addUsersToMailchimp()

and replace that entire function with this code:

public function addUserToMailchimp($p)
    {
    if(!empty($p['form_data']))
        {
        parse_str($p['form_data'], $fd);
        if(!empty($fd['yks-mailchimp-list-id']))
            {
            $email  = false;
            $lid            = $fd['yks-mailchimp-list-id'];
            $api        = new wpyksMCAPI($this->optionVal['api-key']);
            $mv         = array();
            $optin  = $this->optionVal['optin'];

            foreach($this->optionVal['lists'][$lid]['fields'] as $field) : if($field['active'] == '1') :

                // Check to see if the field is in the post
                if(isset($fd[$field['name']])) :

                    // Grab the primary email
                    if(strtolower($field['merge']) == 'email')
                        $email  = $fd[$field['name']];

                    // It exists, set the merge var that we send to mailchimp
                    switch($field['type'])
                        {
                        default:
                            $mv[$field['merge']]    = $fd[$field['name']];
                            break;
                        case 'address':
                            $mv[$field['merge']]    = array(
                                                'addr1'     => $fd[$field['name']],
                                                'addr2'     => $fd[$field['name'].'-add2'],
                                                'city'      => $fd[$field['name'].'-city'],
                                                'state'     => $fd[$field['name'].'-state'],
                                                'zip'           => $fd[$field['name'].'-zip'],
                                                'country'   => 'US'
                                            );
                            break;
                        }

                endif;
            endif; endforeach;

            // If no email, fail
            $noemail = "The email address is blank";
            if($email === false) return $noemail;

                // try adding subscriber, catch error if thrown
                try {
                    //First try getting our user numero uno
                    $retval = $api->call('lists/subscribe', array(
                          'id'              => $lid,
                          'email'             => array( 'email' => $email ),
                          'merge_vars'        => $mv,
                          'double_optin'    => $optin
                    ));
                    return "done";
                } catch( Exception $e ) {
                //  if ( $e == 'List_AlreadySubscribed' ) 
                    $errorCode = $e->getCode;
                    if ( $errorCode = '214' ) {
                        $errorMessage = str_replace('Click here to update your profile.', '', $e->getMessage());
                        echo explode('to list', $errorMessage)[0].'.';
                        die();
                    } else { 
                        echo $e->getMessage;
                        die();
                    }
                }
            }
        }
    return "One or more fields are empty";
    }

I believe in the current version, 3.0.1, this would be line 828 to 892.

This will display a more user friendly message in the form of: email@email.com is already subscribed .

This error catch will be implementing in to the next revision of the plugin along with a few other features.

Let us know if your having any difficulties getting things set up. Thanks!

bshuchter commented 10 years ago

Hi. Thanks for your quick response. I commented out the old function and added your new one below. I'm now getting a parse error in reference to this line:

echo explode('to list', $errorMessage)[0].'.';

The error is: Parse error: syntax error, unexpected '[', expecting ',' or ';' in /home/content/r/i/c/richcohen/html/greatpondresolutions/wp-content/plugins/yikes-inc-easy-mailchimp-extender/classes/class.yksemeBase.php on line 957.

I couldn't figure out how to tweak it.

EvanHerman commented 10 years ago

Instead of commenting out try and deleting it and replacing it with the above function. I've tried it on a local installation and don't receive the error after replacing the function.

Also after replacing the function , when / where are you seeing the error? Are you getting that parse error after you try and submit the form or simply after you replace the function? Is it appearing in the head? or in place of the error that should be displayed? I'm having a hard time replicating the error.

If you'd like I can try and get it set up for you on your site. I would just need to alter a few lines of code in the plugin file. If this is a route you might consider , you can email me directly at evan@yikesinc.com. It should take me less than 5 minutes to get the error catch implemented. If you would like to wait, we should be pushing out an update in the comming days.

Thanks, Evan

bshuchter commented 10 years ago

Thanks! I can wait for the update. I can work on other things in the meantime. (When I commented out the function, the error replaced the entire Wordpress page, front-end and back-end.)