| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
class CJTServicesClient { |
| 10 |
|
| 11 |
/** |
| 12 |
* put your comment there... |
| 13 |
* |
| 14 |
* @var mixed |
| 15 |
*/ |
| 16 |
protected static $instance; |
| 17 |
|
| 18 |
/** |
| 19 |
* put your comment there... |
| 20 |
* |
| 21 |
* @var mixed |
| 22 |
*/ |
| 23 |
private $sslVerify = false; |
| 24 |
|
| 25 |
/** |
| 26 |
* put your comment there... |
| 27 |
* |
| 28 |
* @var mixed |
| 29 |
*/ |
| 30 |
private $timeOut = 10; |
| 31 |
|
| 32 |
/** |
| 33 |
* put your comment there... |
| 34 |
* |
| 35 |
* @var mixed |
| 36 |
*/ |
| 37 |
private $url; |
| 38 |
|
| 39 |
/** |
| 40 |
* put your comment there... |
| 41 |
* |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
# Initialize |
| 45 |
$this->url = cssJSToolbox::getCJTWebSiteURL() . "cjtservices-api"; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* put your comment there... |
| 50 |
* |
| 51 |
* @return CJTServicesClient |
| 52 |
*/ |
| 53 |
public static function & getInstance() { |
| 54 |
# Maintain only ONE instance |
| 55 |
if (!self::$instance) { |
| 56 |
self::$instance = new CJTServicesClient(); |
| 57 |
} |
| 58 |
return self::$instance; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* put your comment there... |
| 63 |
* |
| 64 |
*/ |
| 65 |
public function getSSLVerify() { |
| 66 |
return $this->sslVerify; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* put your comment there... |
| 71 |
* |
| 72 |
*/ |
| 73 |
public function getTimeOut() { |
| 74 |
return $this->timeOut; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* put your comment there... |
| 79 |
* |
| 80 |
*/ |
| 81 |
public function getUrl() { |
| 82 |
return $this->url; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* put your comment there... |
| 87 |
* |
| 88 |
* @param mixed $module |
| 89 |
* @param mixed $method |
| 90 |
* @param mixed $params |
| 91 |
*/ |
| 92 |
public function makeCall($module, $method, $params = null, $postData = null) { |
| 93 |
# Prepare method name by replacing all UPPER Letters to Lower |
| 94 |
# precedence by _ |
| 95 |
while ( preg_match( '/[A-Z]/', $method, $upperLetter, PREG_OFFSET_CAPTURE ) ) { |
| 96 |
$method = substr_replace( $method, |
| 97 |
( '_' . strtolower( $upperLetter[ 0 ][ 0 ] ) ), |
| 98 |
$upperLetter[ 0 ][ 1 ], 1 |
| 99 |
); |
| 100 |
}; |
| 101 |
# Lowercase module name |
| 102 |
$module = strtolower( $module ); |
| 103 |
# Construct method call uri |
| 104 |
$methodUri = "{$this->url}/{$module}/{$method}"; |
| 105 |
# Defaults and E_ALL Complains |
| 106 |
if ( !$params ) { |
| 107 |
$params = array(); |
| 108 |
} |
| 109 |
# Encode parameters |
| 110 |
foreach ($params as $name => $value) { |
| 111 |
$params[ $name ] = urlencode( $value ); |
| 112 |
} |
| 113 |
# Add query string parameters |
| 114 |
$methodUri = add_query_arg( $params, $methodUri ); |
| 115 |
# Request parameters |
| 116 |
$requestParams = array( |
| 117 |
'timeout' => $this->getTimeOut(), |
| 118 |
'sslverify' => $this->getSSLVerify(), |
| 119 |
'body' => json_encode( $postData ), |
| 120 |
); |
| 121 |
# POST Server |
| 122 |
$postRequest = wp_remote_post( $methodUri, $requestParams ); |
| 123 |
if ( ! $postRequest || ( $postRequest instanceof WP_Error ) ) { |
| 124 |
throw new CJTServicesAPICallException( 'CJTStore: Unable to call CJT Services API' ); |
| 125 |
} |
| 126 |
# Make sure its CJT Services Response (E.g CJT Services is not activated or the returned |
| 127 |
# content is not belong to CJT Services response) |
| 128 |
if ( ! wp_remote_retrieve_header( $postRequest, 'cjt-store' ) ) { |
| 129 |
throw new CJTServicesAPICallException( 'CJTStore: Invalid response!!!' ); |
| 130 |
} |
| 131 |
# Return response Wordpress Handle |
| 132 |
return $postRequest; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* put your comment there... |
| 137 |
* |
| 138 |
* @param mixed $value |
| 139 |
* @return {CJTServicesClient|mixed} |
| 140 |
*/ |
| 141 |
public function & setSSLVerify($value) { |
| 142 |
# Set |
| 143 |
$this->sslVerify = $value; |
| 144 |
# Chaining |
| 145 |
return $this; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* put your comment there... |
| 150 |
* |
| 151 |
* @param mixed $value |
| 152 |
* @return CJTServicesClient |
| 153 |
*/ |
| 154 |
public function & setTimeOut($value) { |
| 155 |
# Set |
| 156 |
$this->timeOut = $value; |
| 157 |
# Chaining |
| 158 |
return $this; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
} |