Changeset 1508038
- Timestamp:
- 10/04/2016 09:33:02 AM (10 years ago)
- Location:
- wp-platform/trunk
- Files:
-
- 4 edited
-
classes/Http.php (modified) (1 diff)
-
classes/Mailchimp.php (modified) (1 diff)
-
classes/Route.php (modified) (1 diff)
-
plugin.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
wp-platform/trunk/classes/Http.php
r1491664 r1508038 4 4 class Http { 5 5 6 public $base_url; 7 public $url; 8 public $method = 'GET'; 9 public $params = array(); 10 public $authorization; 11 public $content_type; 12 public $connect_timeout; 13 public $body; 14 protected $debug; 6 public $base_url; 7 public $url; //deprecated 8 public $uri; 9 public $method = 'GET'; 10 public $params = array(); 11 public $authorization; 12 public $content_type; 13 public $connect_timeout; 14 public $body; 15 protected $http_code; 16 protected $debug; 15 17 16 /**17 * @return mixed18 */19 public function call()20 {21 $url = $this->base_url.$this->url; 22 $ch = curl_init(); 23 $headers = array(); 18 /** 19 * @return mixed 20 */ 21 public function call() 22 { 23 if ($this->url && !$this->uri) { 24 $this->uri = $this->url; //legacy 25 } 24 26 25 if ($this->authorization) { 26 $headers[] = 'Authorization: '.$this->authorization.'';27 } 27 $url = $this->base_url.$this->uri; 28 $ch = curl_init(); 29 $headers = array(); 28 30 29 if ($this->content_type) {30 $headers[] = 'Content-Type: '.$this->content_type.'';31 }31 if ($this->authorization) { 32 $headers[] = 'Authorization: '.$this->authorization.''; 33 } 32 34 33 if ($this->params) { 34 $params = $this->params; 35 $param_string = http_build_query($params); 36 $url .= '?'.$param_string; 37 } 35 if ($this->content_type) { 36 $headers[] = 'Content-Type: '.$this->content_type.''; 37 } 38 38 39 if ($this->connect_timeout) { 40 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connect_timeout); 41 } 39 if ($this->params) { 40 $params = $this->params; 41 $param_string = http_build_query($params); 42 $url .= '?'.$param_string; 43 } 42 44 43 if ($this->body) {44 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);45 }45 if ($this->connect_timeout) { 46 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connect_timeout); 47 } 46 48 47 if ($this->method == 'POST') {48 curl_setopt($ch, CURLOPT_POST, true);49 }49 if ($this->body) { 50 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body); 51 } 50 52 51 if ($this->method == 'PUT') { 52 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 53 } elseif ($this->method == 'DELETE') { 54 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); 55 } 53 if ($this->method == 'POST') { 54 curl_setopt($ch, CURLOPT_POST, true); 55 } 56 56 57 if ($headers) { 58 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 59 } 57 if ($this->method == 'PUT') { 58 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 59 } elseif ($this->method == 'DELETE') { 60 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); 61 } 60 62 61 curl_setopt($ch, CURLOPT_URL, $url); 62 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 63 if ($headers) { 64 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 65 } 63 66 64 $rtn = curl_exec($ch); 65 $this->debug = curl_getinfo($ch); 66 $this->debug['response'] = $rtn; 67 curl_close($ch); 68 $rtn = json_decode($rtn); 67 curl_setopt($ch, CURLOPT_URL, $url); 68 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 69 curl_setopt($ch, CURLINFO_HEADER_OUT, true); 69 70 70 return $rtn; 71 $rtn = curl_exec($ch); 72 $this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 71 73 72 } 74 //debug 75 $this->debug = curl_getinfo($ch); 76 $this->debug['response'] = $rtn; 73 77 74 /** 75 * @return bool 76 */ 77 public function success() 78 { 79 if (empty($this->debug['http_code'])) { 80 return false; 81 } elseif ($this->debug['http_code'] == 200) { 82 return true; 83 } else { 84 return false; 85 } 86 } 78 curl_close($ch); 79 $rtn = json_decode($rtn); 80 return $rtn; 81 82 } 83 84 /** 85 * @return bool 86 */ 87 public function success() 88 { 89 if (empty($this->http_code)) { 90 return false; 91 } elseif ($this->http_code == 200) { 92 return true; 93 } else { 94 return false; 95 } 96 } 87 97 88 98 } -
wp-platform/trunk/classes/Mailchimp.php
r1487792 r1508038 2 2 namespace Platform; 3 3 4 class Mailchimp { 4 use Platform\Exception; 5 5 6 public $apikey; 7 public $format = 'json'; 8 public $list_id; 6 class Mailchimp extends Http { 9 7 10 /** 11 * @return mixed 12 */ 13 public function subscribe($email, $values=array()) 14 { 15 $params['email']['email'] = trim($email); 16 $params = array_merge($params, $values); 17 $uri = '/lists/subscribe'; 18 return $this->call($uri, $params); 19 } 8 public $apikey; 9 public $list_id; 20 10 21 /** 22 * @return mixed 23 */ 24 public function call($uri, $params) 25 { 11 /** 12 * @param string $email 13 * @param array $data 14 * @return mixed 15 */ 16 public function subscribe($email, $data=array()) 17 { 18 $data['email_address'] = $email; 19 $data['status'] = 'subscribed'; 26 20 27 $params['apikey'] = $this->apikey; 28 $params['id'] = $this->list_id; 29 $params = json_encode($params); 21 $this->uri = '/lists/'.$this->list_id.'/members'; 22 $this->method = 'POST'; 23 $this->body = $data; 24 $rtn = $this->call(); 25 return $rtn; 26 } 30 27 31 $split = explode('-', $this->apikey, 2); 28 /** 29 * @return mixed 30 */ 31 public function call() 32 { 33 $this->setBaseUrl(); 34 $this->authorization = 'anystring '.$this->apikey; 32 35 33 if (isset($split[1])) { 34 $dc = $split[1]; 35 } else { 36 $dc = 'us1'; 37 } 36 if ($this->body) { 37 $this->content_type = 'json'; 38 $this->body = json_encode($this->body); 39 } 38 40 39 $url = 'https://'.$dc.'.api.mailchimp.com/2.0'.$uri.'.'.$this->format;41 $rtn = parent::call(); 40 42 41 $ch = curl_init(); 42 curl_setopt($ch, CURLOPT_URL, $url); 43 curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 44 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 45 $rtn = curl_exec($ch); 46 curl_close($ch); 43 if ($this->http_code != 200) { 44 throw new Exception($rtn->detail); 45 } 47 46 48 $rtn = json_decode($rtn); 47 return $rtn; 48 } 49 49 50 if (isset($rtn->error)) { 51 throw new Exception($rtn->error); 52 } 50 /** 51 * @return str 52 */ 53 protected function setBaseUrl() 54 { 55 $split = explode('-', $this->apikey, 2); 53 56 54 return $rtn; 57 if (isset($split[1])) { 58 $dc = $split[1]; 59 } else { 60 $dc = 'us1'; 61 } 55 62 56 } 63 $this->base_url = 'https://'.$dc.'.api.mailchimp.com/3.0'; 64 } 57 65 58 66 } -
wp-platform/trunk/classes/Route.php
r1487792 r1508038 4 4 class Route { 5 5 6 protected static $rules;6 protected static $rules; 7 7 8 protected $regex;9 protected $controller;10 protected $action;8 protected $regex; 9 protected $controller; 10 protected $action; 11 11 12 /**13 * @return void14 */15 public function call($controller, $action)16 {17 $this->controller = $controller;18 $this->action = $action;19 self::$rules[] = $this;20 }12 /** 13 * @return void 14 */ 15 public function call($controller, $action) 16 { 17 $this->controller = $controller; 18 $this->action = $action; 19 self::$rules[] = $this; 20 } 21 21 22 /**23 * @return Route24 */25 public static function match($regex)26 {27 $route = new self();28 $route->regex = $regex;29 return $route;30 }22 /** 23 * @return Route 24 */ 25 public static function match($regex) 26 { 27 $route = new self(); 28 $route->regex = $regex; 29 return $route; 30 } 31 31 32 /**33 * @return void34 */35 public static function dispatch()36 {37 //setup routes38 $filepath = APP_PATH.'/routes.php';32 /** 33 * @return void 34 */ 35 public static function dispatch() 36 { 37 //setup routes 38 $filepath = APP_PATH.'/routes.php'; 39 39 40 if (file_exists($filepath)) {41 require($filepath);42 } else {43 return;44 }40 if (file_exists($filepath)) { 41 require($filepath); 42 } else { 43 return; 44 } 45 45 46 //standardise uri 47 $uri = URI;48 $uri = trim($uri, '/'); 46 if (!static::$rules) { 47 return; 48 } 49 49 50 //see if the url matches any rules 51 $matches = array(); 50 //standardise uri 51 $uri = URI; 52 $uri = trim($uri, '/'); 52 53 53 foreach (static::$rules as $route) { 54 //see if the url matches any rules 55 $matches = array(); 54 56 55 $rule = $route->regex; 56 $rule = trim($rule, '^'); 57 $rule = trim($rule, '$'); 58 $rule = str_replace('/', '\/', $rule); 59 $rule = '/^'.$rule.'$/'; 57 foreach (static::$rules as $route) { 60 58 61 if (preg_match($rule, $uri, $matches)) { 62 break; 63 } 59 $rule = $route->regex; 60 $rule = trim($rule, '^'); 61 $rule = trim($rule, '$'); 62 $rule = str_replace('/', '\/', $rule); 63 $rule = '/^'.$rule.'$/'; 64 64 65 } 65 if (preg_match($rule, $uri, $matches)) { 66 break; 67 } 66 68 67 //print_r($matches);exit; 69 } 68 70 69 if (!$matches) { 70 return; 71 } 71 //print_r($matches);exit; 72 72 73 $params = $matches; 74 unset($params[0]); 75 $params = array_values($params); 76 $num_params = count($params); 73 if (!$matches) { 74 return; 75 } 77 76 78 $action = $route->action; 79 $controller_name = $route->controller; 80 $controller_name = 'App\\Controller\\'.$controller_name; 77 $params = $matches; 78 unset($params[0]); 79 $params = array_values($params); 80 $num_params = count($params); 81 81 82 $controller = new $controller_name();83 $controller->call($action, $params);84 exit;82 $action = $route->action; 83 $controller_name = $route->controller; 84 $controller_name = 'App\\Controller\\'.$controller_name; 85 85 86 } 86 $controller = new $controller_name(); 87 $controller->call($action, $params); 88 exit; 89 90 } 87 91 88 92 } -
wp-platform/trunk/plugin.php
r1507416 r1508038 2 2 /** 3 3 * Plugin Name: WP-Platform 4 * Version: 1. 2.34 * Version: 1.3.1 5 5 * Description: Provides platform to allow developers to build bespoke functionality in an MVC and OOP fashion 6 6 */
Note: See TracChangeset
for help on using the changeset viewer.