/**
  * Check template
  *
  * @param   string  $template   sendgrid template
  * @return  bool
  */
 public static function check_template($template)
 {
     $url = 'v3/templates/' . $template;
     $parameters['api_user'] = Sendgrid_Tools::get_username();
     $parameters['api_key'] = Sendgrid_Tools::get_password();
     $parameters['apikey'] = Sendgrid_Tools::get_api_key();
     $response = Sendgrid_Tools::curl_request($url, $parameters);
     if (!$response) {
         return false;
     }
     $response = json_decode($response, true);
     if (isset($response['error'])) {
         return false;
     }
     return true;
 }
 /**
  * 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['api_user'] = get_option('sendgrid_user');
     $parameters['api_key'] = get_option('sendgrid_pwd');
     $parameters['data_type'] = 'global';
     $parameters['metric'] = 'all';
     if (array_key_exists('days', $_POST)) {
         $parameters['days'] = $_POST['days'];
     } else {
         $parameters['start_date'] = $_POST['start_date'];
         $parameters['end_date'] = $_POST['end_date'];
     }
     if ($_POST['type'] and 'wordpress' == $_POST['type']) {
         $parameters['category'] = 'wp_sendgrid_plugin';
     }
     echo Sendgrid_Tools::curl_request('api/stats.get.json', $parameters);
     die;
 }
 /**
  * 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['api_user'] = Sendgrid_Tools::get_username();
     $parameters['api_key'] = Sendgrid_Tools::get_password();
     $api_key = Sendgrid_Tools::get_api_key();
     if ("apikey" == Sendgrid_Tools::get_auth_method()) {
         $parameters['apikey'] = $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::curl_request($endpoint, $parameters);
     die;
 }