forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fce6ae
commit 0f905e9
Showing
24 changed files
with
1,549 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
$root_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR; | ||
|
||
// Require the mijireh library classes | ||
require_once $root_dir . 'Mijireh/Rest.php'; | ||
require_once $root_dir . 'Mijireh/RestJSON.php'; | ||
require_once $root_dir . 'Mijireh/Model.php'; | ||
require_once $root_dir . 'Mijireh/Address.php'; | ||
require_once $root_dir . 'Mijireh/Item.php'; | ||
require_once $root_dir . 'Mijireh/Order.php'; | ||
|
||
class Mijireh_Exception extends Exception {} | ||
class Mijireh_ClientError extends Mijireh_Exception {} /* Status: 400-499 */ | ||
class Mijireh_BadRequest extends Mijireh_ClientError {} /* Status: 400 */ | ||
class Mijireh_Unauthorized extends Mijireh_ClientError {} /* Status: 401 */ | ||
class Mijireh_NotFound extends Mijireh_ClientError {} /* Status: 404 */ | ||
class Mijireh_ServerError extends Mijireh_Exception {} /* Status: 500-599 */ | ||
class Mijireh_InternalError extends Mijireh_ServerError {} /* Status: 500 */ | ||
|
||
class Mijireh { | ||
|
||
/* Live server urls */ | ||
public static $base_url = 'https://secure.mijireh.com/'; | ||
public static $url = 'https://secure.mijireh.com/api/1/'; | ||
|
||
public static $access_key; | ||
|
||
/** | ||
* Return the job id of the slurp | ||
*/ | ||
public static function slurp($url) { | ||
$url_format = '/^(https?):\/\/'. // protocol | ||
'(([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+'. // username | ||
'(:([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+)?'. // password | ||
'@)?(?#'. // auth requires @ | ||
')((([a-z0-9][a-z0-9-]*[a-z0-9]\.)*'. // domain segments AND | ||
'[a-z][a-z0-9-]*[a-z0-9]'. // top level domain OR | ||
'|((\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])\.){3}'. | ||
'(\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])'. // IP address | ||
')(:\d+)?'. // port | ||
')(((\/+([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)*'. // path | ||
'(\?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)'. // query string | ||
'?)?)?'. // path and query string optional | ||
'(#([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?'. // fragment | ||
'$/i'; | ||
|
||
if(!preg_match($url_format, $url)) { | ||
throw new Mijireh_NotFound('Unable to slurp invalid URL: $url'); | ||
} | ||
|
||
try { | ||
$rest = new Mijireh_Rest($url); | ||
$html = $rest->get(''); | ||
$data = array( | ||
'url' => $url, | ||
'html' => $html, | ||
); | ||
$rest = new Mijireh_RestJSON(self::$url); | ||
$rest->setupAuth(self::$access_key, ''); | ||
$result = $rest->post('slurps', $data); | ||
return $result['job_id']; | ||
} | ||
catch(Mijireh_Rest_Unauthorized $e) { | ||
throw new Mijireh_Unauthorized("Unauthorized. Please check your api access key"); | ||
} | ||
catch(Mijireh_Rest_NotFound $e) { | ||
throw new Mijireh_NotFound("Mijireh resource not found: " . $rest->last_request['url']); | ||
} | ||
catch(Mijireh_Rest_ClientError $e) { | ||
throw new Mijireh_ClientError($e->getMessage()); | ||
} | ||
catch(Mijireh_Rest_ServerError $e) { | ||
throw new Mijireh_ServerError($e->getMessage()); | ||
} | ||
catch(Mijireh_Rest_UnknownResponse $e) { | ||
throw new Mijireh_Exception('Unable to slurp the URL: $url'); | ||
} | ||
} | ||
|
||
/** | ||
* Return an array of store information | ||
*/ | ||
public static function get_store_info() { | ||
$rest = new Mijireh_RestJSON(self::$url); | ||
$rest->setupAuth(self::$access_key, ''); | ||
try { | ||
$result = $rest->get('store'); | ||
return $result; | ||
} | ||
catch(Mijireh_Rest_BadRequest $e) { | ||
throw new Mijireh_BadRequest($e->getMessage()); | ||
} | ||
catch(Mijireh_Rest_Unauthorized $e) { | ||
throw new Mijireh_Unauthorized("Unauthorized. Please check your api access key"); | ||
} | ||
catch(Mijireh_Rest_NotFound $e) { | ||
throw new Mijireh_NotFound("Mijireh resource not found: " . $rest->last_request['url']); | ||
} | ||
catch(Mijireh_Rest_ClientError $e) { | ||
throw new Mijireh_ClientError($e->getMessage()); | ||
} | ||
catch(Mijireh_Rest_ServerError $e) { | ||
throw new Mijireh_ServerError($e->getMessage()); | ||
} | ||
} | ||
|
||
public static function preview_checkout_link() { | ||
if(empty(Mijireh::$access_key)) { | ||
throw new Mijireh_Exception('Access key required to view checkout preview'); | ||
} | ||
|
||
return self::$base_url . 'checkout/' . self::$access_key; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
class Mijireh_Address extends Mijireh_Model { | ||
|
||
public function __construct() { | ||
$this->init(); | ||
} | ||
|
||
public function init() { | ||
$this->_data = array( | ||
'first_name' => '', | ||
'last_name' => '', | ||
'street' => '', | ||
'city' => '', | ||
'state_province' => '', | ||
'zip_code' => '', | ||
'country' => '', | ||
'company' => '', | ||
'apt_suite' => '', | ||
'phone' => '' | ||
); | ||
} | ||
|
||
public function validate() { | ||
$is_valid = $this->_check_required_fields(); | ||
return $is_valid; | ||
} | ||
|
||
/** | ||
* Return true if all of the required fields have a non-empty value | ||
* | ||
* @return boolean | ||
*/ | ||
private function _check_required_fields() { | ||
$pass = true; | ||
$fields = array('street', 'city', 'state_province', 'zip_code', 'country'); | ||
foreach($fields as $f) { | ||
if(empty($this->_data[$f])) { | ||
$pass = false; | ||
$this->add_error("$f is required"); | ||
} | ||
} | ||
return $pass; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
class Mijireh_Item extends Mijireh_Model { | ||
|
||
private function _init() { | ||
$this->_data = array( | ||
'name' => null, | ||
'price' => null, | ||
'quantity' => 1, | ||
'sku' => null | ||
); | ||
} | ||
|
||
private function _check_required_fields() { | ||
if(empty($this->_data['name'])) { | ||
$this->add_error('item name is required.'); | ||
} | ||
|
||
if(!is_numeric($this->_data['price'])) { | ||
$this->add_error('price must be a number.'); | ||
} | ||
} | ||
|
||
private function _check_quantity() { | ||
if($this->_data['quantity'] < 1) { | ||
$this->add_error('quantity must be greater than or equal to 1'); | ||
} | ||
} | ||
|
||
public function __construct() { | ||
$this->_init(); | ||
} | ||
|
||
public function __get($key) { | ||
$value = false; | ||
if($key == 'total') { | ||
$value = $this->_data['price'] * $this->_data['quantity']; | ||
$value = number_format($value, 2, '.', ''); | ||
} | ||
else { | ||
$value = parent::__get($key); | ||
} | ||
return $value; | ||
} | ||
|
||
public function get_data() { | ||
$data = parent::get_data(); | ||
$data['total'] = $this->total; | ||
return $data; | ||
} | ||
|
||
public function validate() { | ||
$this->_check_required_fields(); | ||
$this->_check_quantity(); | ||
return count($this->_errors) == 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
class Mijireh_Model { | ||
|
||
protected $_data = array(); | ||
protected $_errors = array(); | ||
|
||
/** | ||
* Set the value of one of the keys in the private $_data array. | ||
* | ||
* @param string $key The key in the $_data array | ||
* @param string $value The value to assign to the key | ||
* @return boolean | ||
*/ | ||
public function __set($key, $value) { | ||
$success = false; | ||
if(array_key_exists($key, $this->_data)) { | ||
$this->_data[$key] = $value; | ||
$success = true; | ||
} | ||
return $success; | ||
} | ||
|
||
/** | ||
* Get the value for the key from the private $_data array. | ||
* | ||
* Return false if the requested key does not exist | ||
* | ||
* @param string $key The key from the $_data array | ||
* @return mixed | ||
*/ | ||
public function __get($key) { | ||
$value = false; | ||
if(array_key_exists($key, $this->_data)) { | ||
$value = $this->_data[$key]; | ||
} | ||
|
||
/* | ||
elseif(method_exists($this, $key)) { | ||
$value = call_user_func_array(array($this, $key), func_get_args()); | ||
} | ||
*/ | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Return true if the given $key in the private $_data array is set | ||
* | ||
* @param string $key | ||
* @return boolean | ||
*/ | ||
public function __isset($key) { | ||
return isset($this->_data[$key]); | ||
} | ||
|
||
/** | ||
* Set the value of the $_data array to null for the given key. | ||
* | ||
* @param string $key | ||
* @return void | ||
*/ | ||
public function __unset($key) { | ||
if(array_key_exists($key, $this->_data)) { | ||
$this->_data[$key] = null; | ||
} | ||
} | ||
|
||
/** | ||
* Return the private $_data array | ||
* | ||
* @return mixed | ||
*/ | ||
public function get_data() { | ||
return $this->_data; | ||
} | ||
|
||
/** | ||
* Return true if the given $key exists in the private $_data array | ||
* | ||
* @param string $key | ||
* @return boolean | ||
*/ | ||
public function field_exists($key) { | ||
return array_key_exists($key, $this->_data); | ||
} | ||
|
||
public function copy_from(array $data) { | ||
foreach($data as $key => $value) { | ||
if(array_key_exists($key, $this->_data)) { | ||
$this->_data[$key] = $value; | ||
} | ||
} | ||
} | ||
|
||
public function clear() { | ||
foreach($this->_data as $key => $value) { | ||
if($key == 'id') { | ||
$this->_data[$key] = null; | ||
} | ||
else { | ||
$this->_data[$key] = ''; | ||
} | ||
} | ||
} | ||
|
||
public function add_error($error_message) { | ||
if(!empty($error_message)) { | ||
$this->_errors[] = $error_message; | ||
} | ||
} | ||
|
||
public function clear_errors() { | ||
$this->_errors = array(); | ||
} | ||
|
||
public function get_errors() { | ||
return $this->_errors; | ||
} | ||
|
||
public function get_error_lines($glue="\n") { | ||
$error_lines = ''; | ||
if(count($this->_errors)) { | ||
$error_lines = implode($glue, $this->_errors); | ||
} | ||
return $error_lines; | ||
} | ||
|
||
public function is_valid() { | ||
return count($this->_errors) == 0; | ||
} | ||
|
||
} |
Oops, something went wrong.