/**
  * Input box for the API key
  *
  * @return void
  * @author James Inman
  */
 public function settings_api_key_input()
 {
     try {
         if (!isset($this->clockwork)) {
             $options = get_option('clockwork_options');
             $this->clockwork = new WordPressClockwork($options['api_key']);
         }
         echo "<input id='clockwork_api_key' name='clockwork_options[api_key]' size='40' type='text' value='{$this->clockwork->key}' />";
         // Show balance
         $balance = $this->clockwork->checkBalance();
         if ($balance) {
             echo '<p><strong>Balance:</strong> ' . $balance['symbol'] . $balance['balance'] . '&nbsp;&nbsp;&nbsp;<a href="' . self::BUY_URL . '" class="button">Buy More</a></p>';
         } else {
             // We can't get the credits for some reason
             echo '<p><a href="' . self::BUY_URL . '" class="button">Buy More Credit</a></p>';
         }
     } catch (ClockworkException $e) {
         echo "<input id='clockwork_api_key' name='clockwork_options[api_key]' size='40' type='text' value='' />";
         echo '<p><a href="' . self::SIGNUP_URL . '" class="button">Get An API Key</a></p>';
     }
 }
 /**
  * Send a test SMS message
  *
  * @param string $to Mobile number to send to
  * @return void
  * @author James Inman
  */
 public function clockwork_test_message($to)
 {
     $log = array();
     global $wp_version;
     $log[] = "Using Wordpress " . $wp_version;
     $log[] = "Clockwork PHP wrapper initalised: using " . Clockwork::VERSION;
     $log[] = "Plugin wrapper initialised: using " . get_class($this) . ' ' . self::VERSION;
     $log[] = '';
     $options = get_option('clockwork_options');
     // Check API key for sanity
     if (isset($options['api_key']) && strlen($options['api_key']) == 40) {
         $log[] = "API key is set and appears valid – " . $options['api_key'];
     } else {
         $log[] = "API key is not set, or is the incorrect length.";
         $log[] = "No credit has been used for this test";
         $this->output_test_message_log($log);
         return;
     }
     // Check originator for sanity
     if (isset($options['from']) && strlen($options['from']) <= 14) {
         $log[] = "Originator is set to " . $options['from'] . " and is below 14 characters";
         // Then remove special characters
         $from = $options['from'];
         $replaced_from = preg_replace('/[^a-zA-Z0-9]/', '', $from);
         if ($from == $replaced_from) {
             $log[] = 'Replaced special characters in originator, no changes';
         } else {
             $log[] = 'Removed special characters from originator: ' . $replaced_from;
         }
         // Is it alphanumeric?
         if (preg_match('/[a-zA-Z]/', $replaced_from) == 1) {
             // Is it under 11 characters?
             if (strlen($replaced_from) <= 11) {
                 $log[] = 'Alphanumeric originator is less than 11 characters – note that some countries reject alpha originators';
             } else {
                 $log[] = 'You are trying to send with an alphanumeric originator over 11 characters in length';
                 $log[] = "No credit has been used for this test";
                 $this->output_test_message_log($log);
                 return;
             }
         } else {
             $log[] = 'Originator is set as numeric';
         }
     } else {
         $log[] = "Originator is not set, using your Clockwork account default (probably 84433)";
     }
     // Check if API key is valid
     $log[] = '';
     $clockwork = new WordPressClockwork($options['api_key']);
     if ($clockwork->checkKey()) {
         $log[] = 'API key exists according to clockworksms.com';
     }
     // Check what the balance is
     $balance_resp = $clockwork->checkBalance();
     if ($balance_resp['balance'] > 0) {
         $log[] = 'Balance is ' . $balance_resp['symbol'] . $balance_resp['balance'];
     } elseif ($balance_resp['account_type'] == 'Invoice') {
         $log[] = 'You have a credit account.  No need to check the balance.';
     } else {
         $log[] = 'Balance is 0. You need to add more credit to your Clockwork account';
         $log[] = "No credit has been used for this test";
         $this->output_test_message_log($log);
         return;
     }
     // Can we authenticate?
     $log[] = '';
     $message = 'This is a test message from Clockwork';
     if (!$clockwork->is_valid_msisdn($_GET['to'])) {
         $log[] = $_GET['to'] . ' appears an invalid number to send to, this message may not send';
     }
     $log[] = 'Attempting test send with API key ' . $options['api_key'] . ' to ' . $_GET['to'];
     try {
         $message_data = array(array('from' => $options['from'], 'to' => $_GET['to'], 'message' => $message));
         $result = $clockwork->send($message_data);
         $log[] = '';
         if (isset($result[0]['id']) && isset($result[0]['success']) && $result[0]['success'] == '1') {
             $log[] = 'Message successfully sent with ID ' . $result[0]['id'];
             $log[] = '';
             $log[] = 'Used 5p of your Clockwork credit for this test';
             $balance_resp = $clockwork->checkBalance();
             $log[] = 'Your new balance is ' . $balance_resp['symbol'] . $balance_resp['balance'];
         } else {
             $log[] = 'There was an error sending the message: error code ' . $result[0]['error_code'] . ' – ' . $result[0]['error_message'];
             $log[] = "No credit has been used for this test";
         }
     } catch (ClockworkException $e) {
         $log[] = "Error: " . $e->getMessage();
     } catch (Exception $e) {
         $log[] = "Error: " . $e->getMessage();
     }
     $this->output_test_message_log($log);
 }