/** * 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); }
/** * Process the form * * @param string $entry Contact form entry to process * @param string $form Gravity Form to process * @return void * @author James Inman */ public function do_form_processing($entry, $form) { $options = array_merge(get_option('clockwork_options'), get_option('clockwork_gravityforms')); $meta = RGFormsModel::get_form_meta($entry['form_id']); $active = $meta['clockwork_active']; $phone = explode(',', $meta['clockwork_to']); $message = $meta['clockwork_message']; $message = str_replace('%form%', $meta['title'], $message); $message = preg_replace('/%([0-9]+\\.?[0-9]*)%/e', '$entry["$1"]', $message); if ($active == '1' && !empty($phone)) { try { $clockwork = new WordPressClockwork($options['api_key']); $messages = array(); foreach ($phone as $to) { $messages[] = array('from' => $options['from'], 'to' => $to, 'message' => $message); } $result = $clockwork->send($messages); } catch (ClockworkException $e) { $result = "Error: " . $e->getMessage(); } catch (Exception $e) { $result = "Error: " . $e->getMessage(); } } }