/**
  * Get SendGrid stats from API and return JSON response,
  * this function work like a page and is used for ajax request by javascript functions
  *
  * @return void;
  */
 public static function get_ajax_statistics()
 {
     if (!isset($_POST['sendgrid_nonce']) || !wp_verify_nonce($_POST['sendgrid_nonce'], 'sendgrid-nonce')) {
         die('Permissions check failed');
     }
     $parameters = array();
     $parameters['auth_method'] = Sendgrid_Tools::get_auth_method();
     $parameters['api_username'] = Sendgrid_Tools::get_username();
     $parameters['api_password'] = Sendgrid_Tools::get_password();
     $parameters['apikey'] = Sendgrid_Tools::get_api_key();
     $parameters['data_type'] = 'global';
     if (array_key_exists('days', $_POST)) {
         $parameters['days'] = $_POST['days'];
     } else {
         $parameters['start_date'] = $_POST['start_date'];
         $parameters['end_date'] = $_POST['end_date'];
     }
     $endpoint = 'v3/stats';
     if (isset($_POST['type']) && 'general' != $_POST['type']) {
         if ('wordpress' == $_POST['type']) {
             $parameters['categories'] = 'wp_sendgrid_plugin';
         } else {
             $parameters['categories'] = urlencode($_POST['type']);
         }
         $endpoint = 'v3/categories/stats';
     }
     echo Sendgrid_Tools::do_request($endpoint, $parameters);
     die;
 }
 /**
  * Check template
  *
  * @param   string  $template   sendgrid template
  * @return  bool
  */
 public static function check_template($template)
 {
     if ('' == $template) {
         return true;
     }
     $url = 'v3/templates/' . $template;
     $parameters['auth_method'] = Sendgrid_Tools::get_auth_method();
     $parameters['api_username'] = Sendgrid_Tools::get_username();
     $parameters['api_password'] = Sendgrid_Tools::get_password();
     $parameters['apikey'] = Sendgrid_Tools::get_api_key();
     $response = Sendgrid_Tools::do_request($url, $parameters);
     if (!$response) {
         return false;
     }
     $response = json_decode($response, true);
     if (isset($response['error']) or isset($response['errors']) && isset($response['errors'][0]['message'])) {
         return false;
     }
     return true;
 }
 /**
  * Returns the unsubscribe groups from SendGrid
  *
  * @return  mixed   an array of groups if the request is successful, false otherwise.
  */
 public static function get_all_unsubscribe_groups()
 {
     $url = 'v3/asm/groups';
     $parameters['auth_method'] = Sendgrid_Tools::get_auth_method();
     $parameters['api_username'] = Sendgrid_Tools::get_username();
     $parameters['api_password'] = Sendgrid_Tools::get_password();
     $parameters['apikey'] = Sendgrid_Tools::get_api_key();
     if ('apikey' == $parameters['auth_method'] and 'true' != self::get_asm_permission()) {
         return false;
     }
     $response = Sendgrid_Tools::do_request($url, $parameters);
     if (!$response) {
         return false;
     }
     $response = json_decode($response, true);
     if (isset($response['error']) or isset($response['errors']) and isset($response['errors'][0]['message'])) {
         return false;
     }
     return $response;
 }