/**
  * Saves the General Settings parameters sent from the settings page
  *
  * @param  mixed   $params    array of parameters from $_POST
  *
  * @return mixed              response array with message and status
  */
 private static function save_general_settings($params)
 {
     if (!isset($params['auth_method'])) {
         $params['auth_method'] = Sendgrid_Tools::get_auth_method();
     }
     switch ($params['auth_method']) {
         case 'apikey':
             if (!defined('SENDGRID_API_KEY')) {
                 if (!isset($params['sendgrid_apikey']) or empty($params['sendgrid_apikey'])) {
                     $response = array('message' => 'API Key is empty.', 'status' => 'error');
                     Sendgrid_Tools::set_api_key('');
                     break;
                 }
                 if (!Sendgrid_Tools::check_api_key($params['sendgrid_apikey'], true)) {
                     $response = array('message' => 'API Key is invalid or without permissions.', 'status' => 'error');
                     break;
                 }
                 if ('true' == Sendgrid_Tools::get_mc_opt_use_transactional() and !Sendgrid_Tools::check_api_key_mc($params['sendgrid_apikey'])) {
                     $response = array('message' => 'This API key is also used for the Subscription Widget but does not have Marketing Campaigns permissions.', 'status' => 'error');
                 }
                 Sendgrid_Tools::set_api_key($params['sendgrid_apikey']);
             }
             break;
         case 'credentials':
             if (!isset($params['sendgrid_username']) and !isset($params['sendgrid_password'])) {
                 break;
             }
             $save_username = true;
             $save_password = true;
             if (!isset($params['sendgrid_username'])) {
                 $save_username = false;
                 $params['sendgrid_username'] = Sendgrid_Tools::get_username();
             }
             if (!isset($params['sendgrid_password'])) {
                 $save_password = false;
                 $params['sendgrid_password'] = Sendgrid_Tools::get_username();
             }
             if (isset($params['sendgrid_username']) and !$params['sendgrid_username'] or isset($params['sendgrid_password']) and !$params['sendgrid_password']) {
                 $response = array('message' => 'Username or password is empty.', 'status' => 'error');
             } elseif (!Sendgrid_Tools::check_username_password($params['sendgrid_username'], $params['sendgrid_password'], true)) {
                 $response = array('message' => 'Username and password are invalid.', 'status' => 'error');
                 break;
             }
             if ($save_username) {
                 Sendgrid_Tools::set_username($params['sendgrid_username']);
             }
             if ($save_password) {
                 Sendgrid_Tools::set_password($params['sendgrid_password']);
             }
             break;
     }
     if (isset($params['sendgrid_name'])) {
         update_option('sendgrid_from_name', $params['sendgrid_name']);
     }
     if (isset($params['sendgrid_email'])) {
         if (!empty($params['sendgrid_email']) and !Sendgrid_Tools::is_valid_email($params['sendgrid_email'])) {
             $response = array('message' => 'Sending email address is not valid.', 'status' => 'error');
         } else {
             update_option('sendgrid_from_email', $params['sendgrid_email']);
         }
     }
     if (isset($params['sendgrid_reply_to'])) {
         if (!empty($params['sendgrid_reply_to']) and !Sendgrid_Tools::is_valid_email($params['sendgrid_reply_to'])) {
             $response = array('message' => 'Reply email address is not valid.', 'status' => 'error');
         } else {
             update_option('sendgrid_reply_to', $params['sendgrid_reply_to']);
         }
     }
     if (isset($params['sendgrid_categories'])) {
         update_option('sendgrid_categories', $params['sendgrid_categories']);
     }
     if (isset($params['sendgrid_stats_categories'])) {
         update_option('sendgrid_stats_categories', $params['sendgrid_stats_categories']);
     }
     if (isset($params['sendgrid_template'])) {
         if (!Sendgrid_Tools::check_template($params['sendgrid_template'])) {
             $response = array('message' => 'Template not found.', 'status' => 'error');
         } else {
             update_option('sendgrid_template', $params['sendgrid_template']);
         }
     }
     if (isset($params['send_method'])) {
         update_option('sendgrid_api', $params['send_method']);
     }
     if (isset($params['auth_method']) && in_array($params['auth_method'], Sendgrid_Tools::$allowed_auth_methods)) {
         update_option('sendgrid_auth_method', $params['auth_method']);
     }
     if (isset($params['sendgrid_port'])) {
         update_option('sendgrid_port', $params['sendgrid_port']);
     }
     if (isset($params['content_type'])) {
         update_option('sendgrid_content_type', $params['content_type']);
     }
     if (isset($params['unsubscribe_group'])) {
         Sendgrid_Tools::set_unsubscribe_group($params['unsubscribe_group']);
     }
     if (isset($response) and $response['status'] == 'error') {
         return $response;
     }
     return array('message' => 'Options are saved.', 'status' => 'updated');
 }