Plugin Directory

Changeset 1508038


Ignore:
Timestamp:
10/04/2016 09:33:02 AM (10 years ago)
Author:
spindogs
Message:

release v1.3.1

Location:
wp-platform/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • wp-platform/trunk/classes/Http.php

    r1491664 r1508038  
    44class Http {
    55
    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;
    1517
    16     /**
    17     * @return mixed
    18     */
    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        }
    2426
    25         if ($this->authorization) {
    26             $headers[] = 'Authorization: '.$this->authorization.'';
    27         }
     27        $url = $this->base_url.$this->uri;
     28        $ch = curl_init();
     29        $headers = array();
    2830
    29         if ($this->content_type) {
    30             $headers[] = 'Content-Type: '.$this->content_type.'';
    31         }
     31        if ($this->authorization) {
     32            $headers[] = 'Authorization: '.$this->authorization.'';
     33        }
    3234
    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        }
    3838
    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        }
    4244
    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        }
    4648
    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        }
    5052
    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        }
    5656
    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        }
    6062
    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        }
    6366
    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);
    6970
    70         return $rtn;
     71        $rtn = curl_exec($ch);
     72        $this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    7173
    72     }
     74        //debug
     75        $this->debug = curl_getinfo($ch);
     76        $this->debug['response'] = $rtn;
    7377
    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    }
    8797
    8898}
  • wp-platform/trunk/classes/Mailchimp.php

    r1487792 r1508038  
    22namespace Platform;
    33
    4 class Mailchimp {
     4use Platform\Exception;
    55
    6     public $apikey;
    7     public $format = 'json';
    8     public $list_id;
     6class Mailchimp extends Http {
    97
    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;
    2010
    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';
    2620
    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    }
    3027
    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;
    3235
    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        }
    3840
    39         $url = 'https://'.$dc.'.api.mailchimp.com/2.0'.$uri.'.'.$this->format;
     41        $rtn = parent::call();
    4042
    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        }
    4746
    48         $rtn = json_decode($rtn);
     47        return $rtn;
     48    }
    4949
    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);
    5356
    54         return $rtn;
     57        if (isset($split[1])) {
     58            $dc = $split[1];
     59        } else {
     60            $dc = 'us1';
     61        }
    5562
    56     }
     63        $this->base_url = 'https://'.$dc.'.api.mailchimp.com/3.0';
     64    }
    5765
    5866}
  • wp-platform/trunk/classes/Route.php

    r1487792 r1508038  
    44class Route {
    55
    6     protected static $rules;
     6    protected static $rules;
    77
    8     protected $regex;
    9     protected $controller;
    10     protected $action;
     8    protected $regex;
     9    protected $controller;
     10    protected $action;
    1111
    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     }
     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    }
    2121
    22     /**
    23     * @return Route
    24     */
    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    }
    3131
    32     /**
    33     * @return void
    34     */
    35     public static function dispatch()
    36     {
    37         //setup routes
    38         $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';
    3939
    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        }
    4545
    46         //standardise uri
    47         $uri = URI;
    48         $uri = trim($uri, '/');
     46        if (!static::$rules) {
     47            return;
     48        }
    4949
    50         //see if the url matches any rules
    51         $matches = array();
     50        //standardise uri
     51        $uri = URI;
     52        $uri = trim($uri, '/');
    5253
    53         foreach (static::$rules as $route) {
     54        //see if the url matches any rules
     55        $matches = array();
    5456
    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) {
    6058
    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.'$/';
    6464
    65         }
     65            if (preg_match($rule, $uri, $matches)) {
     66                break;
     67            }
    6668
    67         //print_r($matches);exit;
     69        }
    6870
    69         if (!$matches) {
    70             return;
    71         }
     71        //print_r($matches);exit;
    7272
    73         $params = $matches;
    74         unset($params[0]);
    75         $params = array_values($params);
    76         $num_params = count($params);
     73        if (!$matches) {
     74            return;
     75        }
    7776
    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);
    8181
    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;
    8585
    86     }
     86        $controller = new $controller_name();
     87        $controller->call($action, $params);
     88        exit;
     89
     90    }
    8791
    8892}
  • wp-platform/trunk/plugin.php

    r1507416 r1508038  
    22/**
    33 * Plugin Name: WP-Platform
    4  * Version: 1.2.3
     4 * Version: 1.3.1
    55 * Description: Provides platform to allow developers to build bespoke functionality in an MVC and OOP fashion
    66 */
Note: See TracChangeset for help on using the changeset viewer.