Beans

The WooCommerce Beans Beans class.

Defined (1)

The class is defined in the following location(s).

/includes/beans.php  
  1. class Beans 
  2. static $signature = 'PHP API'; // Set this value to whatever the API is used for  
  3. static $fail_silently = true; // Set this to false in dev env 
  4.  
  5. static $endpoint = "http://api.beans.cards/v1/"; 
  6. static $domain = "www.beans.cards"; 
  7. static $access_token_path = 'oauth/access_token'; 
  8.  
  9. const VERSION = '1.3.0'; 
  10.  
  11. private static $secret = ""; 
  12. private static $error_log_type = 0; // PHP error_log: $message_type argument 
  13. private static $error_log_destination = ''; // PHP error_log: $destination argument 
  14. private static $error_log_extra = ''; // PHP error_log: $extra_headers argument 
  15.  
  16. // Consider using init instead of object construct  
  17. public function __construct($config)  
  18. self::init('', $config); 
  19.  
  20. public static function init($secret_key) 
  21. self::$secret = $secret_key; 
  22.  
  23. public static function get($path, $arg=null, $fail_silently=null) 
  24. {  
  25. return self::make_request($path, $arg, $fail_silently, 'GET'); 
  26. }  
  27.  
  28. public static function post($path, $arg=null, $fail_silently=null) 
  29. {  
  30. return self::make_request($path, $arg, $fail_silently, 'POST'); 
  31.  
  32. public static function delete($path, $arg=null, $fail_silently=null) 
  33. {  
  34. return self::make_request($path, $arg, $fail_silently, 'DELETE'); 
  35.  
  36. // Use This function to avoid handling errors 
  37. public static function set_error_log($type, $destination='', $extra='') 
  38. self::$error_log_type = $type; 
  39. self::$error_log_destination = $destination; 
  40. self::$error_log_extra = $extra; 
  41.  
  42. public static function get_token_from_cookie($cookies=null, $fail_silently=null) { 
  43. if(!$cookies) $cookies = $_COOKIE; 
  44.  
  45. if(isset($cookies['beans_user'])) { 
  46. $code = $cookies['beans_user']; 
  47. return Beans::get(self::$access_token_path, array('code'=>$code), $fail_silently); 
  48. return null; 
  49.  
  50. private static function make_request($path, $data=null, $fail_silently=null, $method=null) 
  51. $url = self::$endpoint.$path; 
  52. if(!$data) $data = array(); 
  53. $ua = array( 
  54. 'bindings_version' => self::VERSION,  
  55. 'application' => self::$signature,  
  56. 'lang' => 'PHP',  
  57. 'lang_version' => phpversion(),  
  58. 'publisher' => 'Beans',  
  59. );  
  60. $data_string = json_encode($data); 
  61. // Set Request Options 
  62. $curlConfig = array( 
  63. CURLOPT_URL => $url,  
  64. CURLOPT_RETURNTRANSFER => true,  
  65. CURLOPT_CUSTOMREQUEST => $method,  
  66. CURLOPT_POSTFIELDS => $data_string,  
  67. CURLOPT_HTTPAUTH => CURLAUTH_BASIC,  
  68. CURLOPT_USERPWD => self::$secret.':',  
  69. CURLOPT_HTTPHEADER => array(  
  70. 'Content-Type: application/json',  
  71. 'Content-Length: ' . strlen($data_string),  
  72. 'X-Beans-Client-User-Agent: '. json_encode($ua),  
  73. ); 
  74.  
  75. //Make HTTP Request 
  76. $ch = curl_init(); 
  77. curl_setopt_array($ch, $curlConfig); 
  78. $response = curl_exec($ch); 
  79. $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
  80. $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); 
  81. curl_close($ch); 
  82.  
  83. // Check for HTTP Error 
  84. if($content_type!='application/json') { 
  85. $error = array( 
  86. 'code' => $http_status,  
  87. 'message' => 'HTTP Error: '.$http_status,  
  88. ); 
  89. return self::handle_error($error, $fail_silently); 
  90.  
  91. // Load response 
  92. $response = json_decode($response, TRUE); 
  93.  
  94. if($response['error']) 
  95. return self::handle_error($response['error'], $fail_silently); 
  96.  
  97. return $response['result']; 
  98.  
  99. private static function handle_error($error_string, $fail_silently=null) 
  100. if( $fail_silently === null ) 
  101. $fail_silently = self::$fail_silently; 
  102.  
  103. $error = new BeansException($error_string); 
  104.  
  105. if($fail_silently) { 
  106. error_log($error, self::$error_log_type, self::$error_log_destination, self::$error_log_extra); 
  107. return null; 
  108. else 
  109. throw $error;