diff --git a/README.md b/README.md index 6caaeb8..b00820a 100644 --- a/README.md +++ b/README.md @@ -12,16 +12,30 @@ This wrapper lets you interact with Clockwork without the hassle of having to cr ## Usage -Require the Clockwork library: +### Installing with composer +The easiest way to get Clockwork is to use [Composer][4] to automatically download and include it in your project. Setup [Composer][4] and add us to your composer.json +```php +{ + "require": { + "mediaburst/clockworksms": "2.0.*" + } +} +``` +If you are using your own autoloader we are using the PSR-4 namespacing scheme. + +### Including directly + +Download the Clockwork library, put it in your project and require the Clockwork class and the ClockworkException classes: ```php -require 'class-Clockwork.php'; +require 'src/Clockwork.php'; +require 'src/ClockworkException.php'; ``` ### Sending a message ```php -$clockwork = new Clockwork( $API_KEY ); //Be careful not to post your API Keys to public repositories. +$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY ); //Be careful not to post your API Keys to public repositories. $message = array( 'to' => '441234567891', 'message' => 'This is a test!' ); $result = $clockwork->send( $message ); ``` @@ -31,7 +45,7 @@ $result = $clockwork->send( $message ); We recommend you use batch sizes of 500 messages or fewer. By limiting the batch size it prevents any timeouts when sending. ```php -$clockwork = new Clockwork( $API_KEY ); //Be careful not to post your API Keys to public repositories. +$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY ); //Be careful not to post your API Keys to public repositories. $messages = array( array( 'to' => '441234567891', 'message' => 'This is a test!' ), array( 'to' => '441234567892', 'message' => 'This is a test 2!' ) @@ -109,7 +123,7 @@ For example, if you send to invalid phone number "abc": Check your available SMS balance: ```php -$clockwork = new Clockwork( $API_KEY ); //Be careful not to post your API Keys to public repositories. +$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY ); //Be careful not to post your API Keys to public repositories. $clockwork->checkBalance(); ``` @@ -132,11 +146,11 @@ The Clockwork wrapper will throw a `ClockworkException` if the entire call faile ```php try { - $clockwork = new Clockwork( 'invalid_key' ); + $clockwork = new mediaburst\ClockworkSMS\Clockwork( 'invalid_key' ); $message = array( 'to' => 'abc', 'message' => 'This is a test!' ); $result = $clockwork->send( $message ); } -catch( ClockworkException $e ) +catch( mediaburst\ClockworkSMS\ClockworkException $e ) { print $e->getMessage(); // Invalid API Key @@ -185,7 +199,7 @@ In this example both messages will be sent from Clockwork: ```php $options = array( 'from' => 'Clockwork' ); -$clockwork = new Clockwork( $API_KEY, $options ); //Be careful not to post your API Keys to public repositories. +$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY, $options ); //Be careful not to post your API Keys to public repositories. $messages = array( array( 'to' => '441234567891', 'message' => 'This is a test!' ), array( 'to' => '441234567892', 'message' => 'This is a test 2!' ) @@ -200,7 +214,7 @@ Set option values individually on each message. In this example, one message will be from Clockwork and the other from 84433: ```php -$clockwork = new Clockwork( $API_KEY, $options ); //Be careful not to post your API Keys to public repositories. +$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY, $options ); //Be careful not to post your API Keys to public repositories. $messages = array( array( 'to' => '441234567891', 'message' => 'This is a test!', 'from' => 'Clockwork' ), array( 'to' => '441234567892', 'message' => 'This is a test 2!', 'from' => '84433' ) @@ -228,7 +242,7 @@ If you're seeing this error there are two fixes available, the first is easy, si ```php $options = array( 'ssl' => false ); -$clockwork = new Clockwork( $API_KEY, $options ); //Be careful not to post your API Keys to public repositories. +$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY, $options ); //Be careful not to post your API Keys to public repositories. ``` #### Setup SSL root certificates on your server @@ -254,4 +268,4 @@ and submit a pull request. [1]: mailto:hello@clockworksms.com [2]: http://www.clockworksms.com/ [3]: https://github.com/mediaburst/clockwork-php - +[4]: https://getcomposer.org/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f47569c --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "mediaburst/clockworksms", + "description": "ClockworkSMS, International SMS API", + "license": "MIT", + "keywords": ["sms"], + "authors": [ + { + "name": "Andrew Wise", + "email": "andy@mediaburst.co.uk" + } + ], + "require": {}, + "require-dev": {}, + "autoload": { + "psr-4": { + "mediaburst\\ClockworkSMS\\": "src" + } + } +} diff --git a/class-Clockwork.php b/src/Clockwork.php old mode 100755 new mode 100644 similarity index 91% rename from class-Clockwork.php rename to src/Clockwork.php index 67661a2..7d93d60 --- a/class-Clockwork.php +++ b/src/Clockwork.php @@ -9,13 +9,11 @@ * @version 1.3.2 */ -if ( !class_exists('ClockworkException') ) { - require_once('class-ClockworkException.php'); -} +namespace mediaburst\ClockworkSMS; /** * Main Clockwork API Class -* +* * @package Clockwork * @since 1.0 */ @@ -24,7 +22,7 @@ class Clockwork { /* * Version of this class */ - const VERSION = '1.3.2'; + const VERSION = '2.0.0'; /** * All Clockwork API calls start with BASE_URL @@ -56,9 +54,9 @@ class Clockwork { */ const API_BALANCE_METHOD = 'balance'; - /** + /** * Clockwork API Key - * + * * @var string * @author Martin Steel */ @@ -128,7 +126,7 @@ class Clockwork { * Possible values: * 'error' - Return an error (Messasge is not sent) * 'remove' - Remove the invalid character(s) - * 'replace' - Replace invalid characters where possible, remove others + * 'replace' - Replace invalid characters where possible, remove others * @author Martin Steel */ public $invalid_char_action; @@ -146,7 +144,7 @@ public function __construct($key, array $options = array()) { } else { $this->key = $key; } - + $this->ssl = (array_key_exists('ssl', $options)) ? $options['ssl'] : null; $this->proxy_host = (array_key_exists('proxy_host', $options)) ? $options['proxy_host'] : null; $this->proxy_port = (array_key_exists('proxy_port', $options)) ? $options['proxy_port'] : null; @@ -159,7 +157,7 @@ public function __construct($key, array $options = array()) { /** * Send some text messages - * + * * * @author Martin Steel */ @@ -173,7 +171,7 @@ public function send(array $sms) { $sms = array($sms); } - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Message'); $req_doc->appendChild($root); @@ -185,10 +183,10 @@ public function send(array $sms) { $single = $sms[$i]; $sms_node = $req_doc->createElement('SMS'); - + // Phone number - $sms_node->appendChild($req_doc->createElement('To', $single['to'])); - + $sms_node->appendChild($req_doc->createElement('To', $single['to'])); + // Message text $content_node = $req_doc->createElement('Content'); $content_node->appendChild($req_doc->createTextNode($single['message'])); @@ -249,10 +247,10 @@ public function send(array $sms) { } $req_xml = $req_doc->saveXML(); - + $resp_xml = $this->postToClockwork(self::API_SMS_METHOD, $req_xml); - $resp_doc = new DOMDocument(); - $resp_doc->loadXML($resp_xml); + $resp_doc = new \DOMDocument(); + $resp_doc->loadXML($resp_xml); $response = array(); $err_no = null; @@ -279,7 +277,7 @@ public function send(array $sms) { break; } } - if( array_key_exists('error_code', $resp ) ) + if( array_key_exists('error_code', $resp ) ) { $resp['success'] = 0; } else { @@ -300,7 +298,7 @@ public function send(array $sms) { if (isset($err_no)) { throw new ClockworkException($err_desc, $err_no); } - + if ($single_message) { return $response[0]; } else { @@ -317,7 +315,7 @@ public function send(array $sms) { */ public function checkCredit() { // Create XML doc for request - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Credit'); $req_doc->appendChild($root); $root->appendChild($req_doc->createElement('Key', $this->key)); @@ -327,14 +325,14 @@ public function checkCredit() { $resp_xml = $this->postToClockwork(self::API_CREDIT_METHOD, $req_xml); // Create XML doc for response - $resp_doc = new DOMDocument(); + $resp_doc = new \DOMDocument(); $resp_doc->loadXML($resp_xml); // Parse the response to find credit value $credit; $err_no = null; $err_desc = null; - + foreach ($resp_doc->documentElement->childNodes AS $doc_child) { switch ($doc_child->nodeName) { case "Credit": @@ -360,30 +358,30 @@ public function checkCredit() { /** * Check your account balance * - * @return array Array of account balance: + * @return array Array of account balance: * @author Martin Steel */ public function checkBalance() { // Create XML doc for request - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Balance'); $req_doc->appendChild($root); $root->appendChild($req_doc->createElement('Key', $this->key)); $req_xml = $req_doc->saveXML(); - + // POST XML to Clockwork $resp_xml = $this->postToClockwork(self::API_BALANCE_METHOD, $req_xml); // Create XML doc for response - $resp_doc = new DOMDocument(); + $resp_doc = new \DOMDocument(); $resp_doc->loadXML($resp_xml); - + // Parse the response to find balance value $balance = null; $err_no = null; $err_desc = null; $account_type = null; - + foreach ($resp_doc->documentElement->childNodes as $doc_child) { switch ($doc_child->nodeName) { case "Balance": @@ -393,16 +391,16 @@ public function checkBalance() { foreach ($doc_child->childNodes as $resp_node) { switch ($resp_node->tagName) { case "Symbol": - $symbol = $resp_node->nodeValue; + $symbol = $resp_node->nodeValue; break; case "Code": - $code = $resp_node->nodeValue; + $code = $resp_node->nodeValue; break; } } break; - case "AccountType": - $account_type = $doc_child->nodeValue; + case "AccountType": + $account_type = $doc_child->nodeValue; break; case "ErrNo": $err_no = $doc_child->nodeValue; @@ -418,7 +416,7 @@ public function checkBalance() { if (isset($err_no)) { throw new ClockworkException($err_desc, $err_no); } - + return array( 'symbol' => $symbol, 'balance' => $balance, 'code' => $code, 'account_type' => $account_type ); } @@ -430,7 +428,7 @@ public function checkBalance() { */ public function checkKey() { // Create XML doc for request - $req_doc = new DOMDocument('1.0', 'UTF-8'); + $req_doc = new \DOMDocument('1.0', 'UTF-8'); $root = $req_doc->createElement('Authenticate'); $req_doc->appendChild($root); $root->appendChild($req_doc->createElement('Key', $this->key)); @@ -440,9 +438,9 @@ public function checkKey() { $resp_xml = $this->postToClockwork(self::API_AUTH_METHOD, $req_xml); // Create XML doc for response - $resp_doc = new DOMDocument(); + $resp_doc = new \DOMDocument(); $resp_doc->loadXML($resp_xml); - + // Parse the response to see if authenticated $cust_id; $err_no = null; @@ -467,7 +465,7 @@ public function checkKey() { if (isset($err_no)) { throw new ClockworkException($err_desc, $err_no); } - return isset($cust_id); + return isset($cust_id); } /** @@ -483,7 +481,7 @@ protected function postToClockwork($method, $data) { if ($this->log) { $this->logXML("API $method Request XML", $data); } - + if( isset( $this->ssl ) ) { $ssl = $this->ssl; } else { @@ -520,6 +518,7 @@ protected function xmlPost($url, $data) { curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); curl_setopt($ch, CURLOPT_USERAGENT, 'Clockwork PHP Wrapper/1.0' . self::VERSION); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); if (isset($this->proxy_host) && isset($this->proxy_port)) { curl_setopt($ch, CURLOPT_PROXY, $this->proxy_host); curl_setopt($ch, CURLOPT_PROXYPORT, $this->proxy_port); @@ -529,9 +528,9 @@ protected function xmlPost($url, $data) { $info = curl_getinfo($ch); if ($response === false || $info['http_code'] != 200) { - throw new Exception('HTTP Error calling Clockwork API - HTTP Status: ' . $info['http_code'] . ' - cURL Erorr: ' . curl_error($ch)); + throw new \Exception('HTTP Error calling Clockwork API - HTTP Status: ' . $info['http_code'] . ' - cURL Erorr: ' . curl_error($ch)); } elseif (curl_errno($ch) > 0) { - throw new Exception('HTTP Error calling Clockwork API - cURL Error: ' . curl_error($ch)); + throw new \Exception('HTTP Error calling Clockwork API - cURL Error: ' . curl_error($ch)); } curl_close($ch); @@ -557,17 +556,17 @@ protected function xmlPost($url, $data) { $fp = @fopen($url, 'rb', false, $ctx); if (!$fp) { ini_set('track_errors',$track); - throw new Exception("HTTP Error calling Clockwork API - fopen Error: $php_errormsg"); + throw new \Exception("HTTP Error calling Clockwork API - fopen Error: $php_errormsg"); } $response = @stream_get_contents($fp); if ($response === false) { ini_set('track_errors',$track); - throw new Exception("HTTP Error calling Clockwork API - stream Error: $php_errormsg"); + throw new \Exception("HTTP Error calling Clockwork API - stream Error: $php_errormsg"); } ini_set('track_errors',$track); return $response; } else { - throw new Exception("Clockwork requires PHP5 with cURL or HTTP stream support"); + throw new \Exception("Clockwork requires PHP5 with cURL or HTTP stream support"); } } @@ -605,7 +604,7 @@ protected function sslSupport() { protected function logXML($log_msg, $xml) { // Tidy if possible if (class_exists('tidy')) { - $tidy = new tidy; + $tidy = new \tidy; $config = array( 'indent' => true, 'input-xml' => true, @@ -630,7 +629,7 @@ protected function logXML($log_msg, $xml) { protected function is_assoc($array) { return (bool)count(array_filter(array_keys($array), 'is_string')); } - + /** * Check if a number is a valid MSISDN * diff --git a/class-ClockworkException.php b/src/ClockworkException.php old mode 100755 new mode 100644 similarity index 86% rename from class-ClockworkException.php rename to src/ClockworkException.php index 522fca5..09804f3 --- a/class-ClockworkException.php +++ b/src/ClockworkException.php @@ -6,7 +6,7 @@ * @copyright Mediaburst Ltd 2012 * @license MIT * @link http://www.clockworksms.com - */ + */ /* * ClockworkException @@ -18,7 +18,10 @@ * @subpackage Exception * @since 1.0 */ -class ClockworkException extends Exception { + +namespace mediaburst\ClockworkSMS; + +class ClockworkException extends \Exception { public function __construct( $message, $code = 0 ) { // make sure everything is assigned properly diff --git a/wordpress/class-WordPressClockwork.php b/wordpress/class-WordPressClockwork.php deleted file mode 100755 index 764e42d..0000000 --- a/wordpress/class-WordPressClockwork.php +++ /dev/null @@ -1,448 +0,0 @@ - '247', - 'AD' => '376', - 'AE' => '971', - 'AF' => '93', - 'AG' => '1268', - 'AI' => '1264', - 'AL' => '355', - 'AM' => '374', - 'AO' => '244', - 'AQ' => '672', - 'AR' => '54', - 'AS' => '1684', - 'AT' => '43', - 'AU' => '61', - 'AW' => '297', - 'AX' => '358', - 'AZ' => '994', - 'BA' => '387', - 'BB' => '1246', - 'BD' => '880', - 'BE' => '32', - 'BF' => '226', - 'BG' => '359', - 'BH' => '973', - 'BI' => '257', - 'BJ' => '229', - 'BL' => '590', - 'BM' => '1441', - 'BN' => '673', - 'BO' => '591', - 'BQ' => '599', - 'BR' => '55', - 'BS' => '1242', - 'BT' => '975', - 'BW' => '267', - 'BY' => '375', - 'BZ' => '501', - 'CA' => '1', - 'CC' => '61', - 'CD' => '243', - 'CF' => '236', - 'CG' => '242', - 'CH' => '41', - 'CI' => '225', - 'CK' => '682', - 'CL' => '56', - 'CM' => '237', - 'CN' => '86', - 'CO' => '57', - 'CR' => '506', - 'CU' => '53', - 'CV' => '238', - 'CW' => '599', - 'CX' => '61', - 'CY' => '357', - 'CZ' => '420', - 'DE' => '49', - 'DJ' => '253', - 'DK' => '45', - 'DM' => '1767', - 'DO' => '1809', - 'DO' => '1829', - 'DO' => '1849', - 'DZ' => '213', - 'EC' => '593', - 'EE' => '372', - 'EG' => '20', - 'EH' => '212', - 'ER' => '291', - 'ES' => '34', - 'ET' => '251', - 'EU' => '388', - 'FI' => '358', - 'FJ' => '679', - 'FK' => '500', - 'FM' => '691', - 'FO' => '298', - 'FR' => '33', - 'GA' => '241', - 'GB' => '44', - 'GD' => '1473', - 'GE' => '995', - 'GF' => '594', - 'GG' => '44', - 'GH' => '233', - 'GI' => '350', - 'GL' => '299', - 'GM' => '220', - 'GN' => '224', - 'GP' => '590', - 'GQ' => '240', - 'GR' => '30', - 'GT' => '502', - 'GU' => '1671', - 'GW' => '245', - 'GY' => '592', - 'HK' => '852', - 'HN' => '504', - 'HR' => '385', - 'HT' => '509', - 'HU' => '36', - 'ID' => '62', - 'IE' => '353', - 'IL' => '972', - 'IM' => '44', - 'IN' => '91', - 'IO' => '246', - 'IQ' => '964', - 'IR' => '98', - 'IS' => '354', - 'IT' => '39', - 'JE' => '44', - 'JM' => '1876', - 'JO' => '962', - 'JP' => '81', - 'KE' => '254', - 'KG' => '996', - 'KH' => '855', - 'KI' => '686', - 'KM' => '269', - 'KN' => '1869', - 'KP' => '850', - 'KR' => '82', - 'KW' => '965', - 'KY' => '1345', - 'KZ' => '7', - 'LA' => '856', - 'LB' => '961', - 'LC' => '1758', - 'LI' => '423', - 'LK' => '94', - 'LR' => '231', - 'LS' => '266', - 'LT' => '370', - 'LU' => '352', - 'LV' => '371', - 'LY' => '218', - 'MA' => '212', - 'MC' => '377', - 'MD' => '373', - 'ME' => '382', - 'MF' => '590', - 'MG' => '261', - 'MH' => '692', - 'MK' => '389', - 'ML' => '223', - 'MM' => '95', - 'MN' => '976', - 'MO' => '853', - 'MP' => '1670', - 'MQ' => '596', - 'MR' => '222', - 'MS' => '1664', - 'MT' => '356', - 'MU' => '230', - 'MV' => '960', - 'MW' => '265', - 'MX' => '52', - 'MY' => '60', - 'MZ' => '258', - 'NA' => '264', - 'NC' => '687', - 'NE' => '227', - 'NF' => '672', - 'NG' => '234', - 'NI' => '505', - 'NL' => '31', - 'NO' => '47', - 'NP' => '977', - 'NR' => '674', - 'NU' => '683', - 'NZ' => '64', - 'OM' => '968', - 'PA' => '507', - 'PE' => '51', - 'PF' => '689', - 'PG' => '675', - 'PH' => '63', - 'PK' => '92', - 'PL' => '48', - 'PM' => '508', - 'PR' => '1787', - 'PR' => '1939', - 'PS' => '970', - 'PT' => '351', - 'PW' => '680', - 'PY' => '595', - 'QA' => '974', - 'QN' => '374', - 'QS' => '252', - 'QY' => '90', - 'RE' => '262', - 'RO' => '40', - 'RS' => '381', - 'RU' => '7', - 'RW' => '250', - 'SA' => '966', - 'SB' => '677', - 'SC' => '248', - 'SD' => '249', - 'SE' => '46', - 'SG' => '65', - 'SH' => '290', - 'SI' => '386', - 'SJ' => '47', - 'SK' => '421', - 'SL' => '232', - 'SM' => '378', - 'SN' => '221', - 'SO' => '252', - 'SR' => '597', - 'SS' => '211', - 'ST' => '239', - 'SV' => '503', - 'SX' => '1721', - 'SY' => '963', - 'SZ' => '268', - 'TA' => '290', - 'TC' => '1649', - 'TD' => '235', - 'TG' => '228', - 'TH' => '66', - 'TJ' => '992', - 'TK' => '690', - 'TL' => '670', - 'TM' => '993', - 'TN' => '216', - 'TO' => '676', - 'TR' => '90', - 'TT' => '1868', - 'TV' => '688', - 'TW' => '886', - 'TZ' => '255', - 'UA' => '380', - 'UG' => '256', - 'UK' => '44', - 'US' => '1', - 'UY' => '598', - 'UZ' => '998', - 'VA' => '379', - 'VA' => '39', - 'VC' => '1784', - 'VE' => '58', - 'VG' => '1284', - 'VI' => '1340', - 'VN' => '84', - 'VU' => '678', - 'WF' => '681', - 'WS' => '685', - 'XC' => '991', - 'XD' => '888', - 'XG' => '881', - 'XL' => '883', - 'XN' => '857', - 'XN' => '858', - 'XN' => '870', - 'XP' => '878', - 'XR' => '979', - 'XS' => '808', - 'XT' => '800', - 'XV' => '882', - 'YE' => '967', - 'YT' => '262', - 'ZA' => '27', - 'ZM' => '260', - 'ZW' => '263', - ); - - /** - * Legacy username - * - * @var string - */ - private $username; - - /** - * Legacy password - * - * @var string - */ - public $password; - - /** - * Create a new instance of the Clockwork wrapper - * - * Also supports passing a username and a password as first and second parameter, but ONLY when - * used in WordPressClockwork for the purposes of converting to an API key. - * - * @param string key Your Clockwork API Key - * @param array options Optional parameters for sending SMS - * @author James Inman - */ - public function __construct( $arg1, $arg2 = array() ) { - if( !isset( $arg2 ) || is_array( $arg2 ) ) { - parent::__construct( $arg1, $arg2 ); - } else { - $this->username = $arg1; - $this->password = $arg2; - } - } - - public function createAPIKey( $name = 'WordPress API Key' ) { - // Create XML doc for request - $req_doc = new DOMDocument( '1.0', 'UTF-8' ); - $root = $req_doc->createElement( 'GetKey' ); - $req_doc->appendChild( $root ); - $root->appendChild( $req_doc->createElement( 'Username', $this->username ) ); - $root->appendChild( $req_doc->createElement( 'Password', $this->password ) ); - $root->appendChild( $req_doc->createElement( 'Name', $name ) ); - $req_xml = $req_doc->saveXML(); - // POST XML to Clockwork - $resp_xml = $this->postToClockwork( self::API_GET_KEY_METHOD, $req_xml ); - - // Create XML doc for response - $resp_doc = new DOMDocument(); - $resp_doc->loadXML( $resp_xml ); - - // Parse the response to find credit value - $key = null; - $err_no = null; - $err_desc = null; - - foreach( $resp_doc->documentElement->childNodes as $doc_child ) { - switch( $doc_child->nodeName ) { - case "Key": - $key = $doc_child->nodeValue; - break; - case "ErrNo": - $err_no = $doc_child->nodeValue; - break; - case "ErrDesc": - $err_desc = $doc_child->nodeValue; - break; - default: - break; - } - } - - if( isset( $err_no ) ) { - throw new ClockworkException( $err_desc, $err_no ); - } - - $this->key = $key; - return $key; - } - - /** - * Check if the WordPress HTTP API can support SSL - * - * @returns bool True if SSL is supported - */ - public function sslSupport() { - return wp_http_supports( array( 'ssl' ) ); - } - - /** - * Make an HTTP POST using the WordPress HTTP API. - * - * @param string url URL to send to - * @param string data Data to POST - * @return string Response returned by server - */ - protected function xmlPost( $url, $data ) { - $args = array( - 'body' => $data, - 'headers' => array( 'Content-Type' => 'text/xml' ), - 'timeout' => 10, // Seconds - ); - - // Check whether WordPress should veryify the SSL certificate - if( stristr( $url, 'https://' ) ) { - $args['sslverify'] = $this->sslVerify( $url ); - } - - $result = wp_remote_post( $url, $args ); - if( is_wp_error( $result ) ) { - error_log( "POST failed: " . $result->get_error_message() ); - throw new ClockworkException( "HTTP Call failed - Error: " . $result->get_error_message() ); - } - return $result[ 'body' ]; - } - - /** - * Verify SSL conectivity to the remote host - * - * If the request fails store a flag so that we - * don't need to do the check again - */ - private function sslVerify($url) { - $opt = get_option( self::SSL_OPTIONS_KEY ); - if( !$opt ) { - $opt = array(); - } - - if( !array_key_exists( 'sslverify', $opt ) ) { - $args = array( - 'timeout' => 10 // Seconds - ); - - $result = wp_remote_post( $url, $args ); - - if( is_wp_error( $result ) ) { - $opt['sslverify'] = false; - } else { - $opt['sslverify'] = true; - } - - update_option( self::SSL_OPTIONS_KEY, $opt ); - } - return $opt['sslverify']; - } - -} diff --git a/wordpress/class-clockwork-plugin.php b/wordpress/class-clockwork-plugin.php deleted file mode 100755 index 757a318..0000000 --- a/wordpress/class-clockwork-plugin.php +++ /dev/null @@ -1,546 +0,0 @@ -convert_existing_username_and_password(); - - // Setup clockwork - try { - $options = get_option( 'clockwork_options' ); - if( is_array( $options ) && isset( $options['api_key'] ) ) { - $this->clockwork = new WordPressClockwork( $options['api_key'] ); - } - } catch( Exception $e ) { - } - - // Register the activation hook to install - register_activation_hook( __FILE__, array( $this, 'install' ) ); - - add_action( 'admin_head', array( $this, 'setup_admin_head' ) ); - add_action( 'admin_menu', array( $this, 'setup_admin_navigation' ) ); - add_action( 'admin_notices', array( $this, 'setup_admin_message' ) ); - add_action( 'admin_bar_menu', array( $this, 'setup_admin_bar' ), 999 ); - add_action( 'admin_init', array( $this, 'setup_admin_init' ) ); - add_action( 'admin_enqueue_scripts', array( $this, 'setup_clockwork_js' ) ); - - $this->plugin_callback = array( $this, 'main' ); - } - - /** - * Return the username and password from the plugin's existing options - * - * @return array Array of 'username' and 'password' - * @author James Inman - */ - abstract public function get_existing_username_and_password(); - - /** - * Setup HTML for the admin
- * - * @return void - * @author James Inman - */ - abstract public function setup_admin_head(); - - /** - * Convert existing username and password to a new API key - * - * @return void - * @author James Inman - */ - public function convert_existing_username_and_password() { - $options = get_option( 'clockwork_options' ); - if( !is_array( $options ) || !isset( $options['api_key'] ) ) { - $existing_details = $this->get_existing_username_and_password(); - - if( is_array( $existing_details ) && isset( $existing_details['username'] ) && isset( $existing_details['password'] ) ) { - try { - // We have a username and password, now go and convert them - $this->clockwork = new WordPressClockwork( $existing_details['username'], $existing_details['password'] ); - $key = $this->clockwork->createAPIKey( 'WordPress - ' . home_url() ); - // Set the Clockwork API key to be the newly created key - update_option( 'clockwork_options', array( 'api_key' => $key ) ); - } catch( ClockworkException $e ) { - return; - } - } - } - } - - /** - * Called on plugin activation - * - * @return void - * @author James Inman - */ - public function install() { - } - - /** - * Tell the user to update their Clockwork options on every admin panel page if they haven't already - * - * @return void - * @author James Inman - */ - public function setup_admin_message() { - // Don't bother showing the "You need to set your Clockwork options" message if it's that form we're viewing - if( !isset( $this->clockwork ) && ( get_current_screen()->base != 'toplevel_page_clockwork_options' ) ) { - $this->show_admin_message('You need to set your Clockwork options before you can use ' . $this->plugin_name . '.'); - } - } - - /** - * Add the Clockwork balance to the admin bar - * - * @return void - * @author James Inman - */ - public function setup_admin_bar() { - global $wp_admin_bar; - if ( !is_super_admin() || !is_admin_bar_showing() ) { - return; - } - - $options = get_option( 'clockwork_options' ); - if( isset( $options['api_key'] ) ) { - // Display a low credit notification if there's no credit - try { - if( !isset( $this->clockwork ) ) { - $clockwork = new WordPressClockwork( $options['api_key'] ); - } - $balance = $this->clockwork->checkBalance(); - if( $balance['balance'] <= 0 && $balance['account_type'] == 'PAYG' ) { - $balance_string = '£0. Top up now!'; - } else { - $balance_string = $balance['symbol'] . $balance['balance']; - } - // Add a node to the Admin bar - $wp_admin_bar->add_node( array( - 'id' => 'clockwork_balance', - 'title' => 'Clockwork: ' . $balance_string, - 'href' => self::BUY_URL ) - ); - } catch( Exception $e ) { - // Don't kill the entire admin panel because we can't get the balance - } - } - } - - /** - * Setup admin navigation: callback for 'admin_menu' - * - * @return void - * @author James Inman - */ - public function setup_admin_navigation() { - global $menu; - - $menu_exists = false; - foreach( $menu as $k => $item ) { - if( $item[0] == "Clockwork SMS" ) { - $menu_exists = true; - break; - } - } - - // Setup global Clockwork options - if( !$menu_exists ) { - add_menu_page( __( 'Clockwork SMS', $this->language_string ), __( 'Clockwork SMS', $this->language_string ), 'manage_options', 'clockwork_options', array( $this, 'clockwork_options' ), plugins_url( 'images/logo_16px_16px.png', dirname( __FILE__ ) ) ); - add_submenu_page( 'clockwork_options', __( 'Clockwork Options', $this->language_string ), __( 'Clockwork Options', $this->language_string ), 'manage_options', 'clockwork_options', array( $this, 'clockwork_options' ) ); - add_submenu_page( NULL, 'Test', 'Test', 'manage_options', 'clockwork_test_message', array( $this, 'clockwork_test_message' ) ); - } - - // Setup options for this plugin - add_submenu_page( 'clockwork_options', __( $this->plugin_name, $this->language_string ), __( $this->plugin_name, $this->language_string ), 'manage_options', $this->plugin_callback[1], $this->plugin_callback ); - } - - /** - * Set up javascript for the Clockwork admin functions - * - * @return void - * @author James Inman - */ - public function setup_clockwork_js() { - wp_enqueue_script( 'clockwork_options', plugins_url( 'js/clockwork_options.js', dirname( __FILE__ ) ), array( 'jquery' ) ); - } - - /** - * Register global Clockwork settings for API keys - * - * @return void - * @author James Inman - */ - public function setup_admin_init() { - register_setting( 'clockwork_options', 'clockwork_options', array( $this, 'clockwork_options_validate' ) ); - add_settings_section( 'clockwork_api_keys', 'API Key', array( $this, 'settings_api_key_text' ), 'clockwork' ); - add_settings_field( 'clockwork_api_key', 'Your API Key', array( $this, 'settings_api_key_input' ), 'clockwork', 'clockwork_api_keys' ); - - add_settings_section( 'clockwork_defaults', 'Default Settings', array( $this, 'settings_default_text' ), 'clockwork' ); - add_settings_field( 'clockwork_from', "'From' Number ", array( $this, 'settings_from_input' ), 'clockwork', 'clockwork_defaults' ); - } - - /** - * Introductory text for the API keys part of the form - * - * @return void - * @author James Inman - */ - public function settings_api_key_text() { - echo 'You need an API key to use the Clockwork plugins.
'; - } - - /** - * Introductory text for the default part of the form - * - * @return void - * @author James Inman - */ - public function settings_default_text() { - echo 'Default settings apply to every Clockwork plugin you have installed.
'; - } - - /** - * Input box for the API key - * - * @return void - * @author James Inman - */ - public function settings_api_key_input() { - $options = get_option( 'clockwork_options' ); - - if( isset( $options['api_key'] ) ) { - try { - if( !isset( $this->clockwork ) ) { - $this->clockwork = new WordPressClockwork( $options['api_key'] ); - } - - echo ""; - - // Show balance - $balance = $this->clockwork->checkBalance(); - if( $balance ) { - echo 'Balance: ' . $balance['symbol'] . $balance['balance'] . ' Buy More
'; - } else { // We can't get the credits for some reason - echo ''; - } - - } catch( ClockworkException $e ) { - echo ""; - echo ''; - } - - return; - } else { - echo ""; - echo ''; - } - } - - /** - * Input box for the from name - * - * @return void - * @author James Inman - */ - public function settings_from_input() { - $options = get_option( 'clockwork_options' ); - if( isset( $options['from'] ) ) { - echo ""; - } else { - echo ""; - } - - echo "Enter the number your messages will be sent from. We recommend your mobile phone number.
UK customers can use alphanumeric strings up to 11 characters.
$message