| @@ -17,8 +17,26 @@ | ||
| 17 | 17 | */ |
| 18 | 18 | class API { |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | + * Holds the base class object. | |
| 22 | + * | |
| 23 | + * @since 1.0.0 | |
| 24 | + * | |
| 25 | + * @var object | |
| 26 | + */ | |
| 27 | + public $base; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Holds the API endpoint. | |
| 31 | + * | |
| 32 | + * @since 1.0.0 | |
| 33 | + * | |
| 34 | + * @var string | |
| 35 | + */ | |
| 36 | + public $api_endpoint = ''; | |
| 37 | + | |
| 38 | + /** | |
| 21 | 39 | * Sanitizes API arguments, by removing false or empty |
| 22 | 40 | * arguments in the array. |
| 23 | 41 | * |
| 24 | 42 | * @since 1.0.0 |
| @@ -28,9 +46,9 @@ | ||
| 28 | 46 | */ |
| 29 | 47 | public function sanitize_arguments( $args ) { |
| 30 | 48 | |
| 31 | 49 | foreach ( $args as $key => $value ) { |
| 32 | - if ( empty( $value ) || ! $value ) { | |
| 50 | + if ( empty( $value ) ) { | |
| 33 | 51 | unset( $args[ $key ] ); |
| 34 | 52 | } |
| 35 | 53 | } |
| 36 | 54 | |
| @@ -44,9 +62,9 @@ | ||
| 44 | 62 | * @since 1.0.0 |
| 45 | 63 | * |
| 46 | 64 | * @param string $cmd Command (required). |
| 47 | 65 | * @param array $params Params (optional). |
| 48 | - * @return mixed WP_Error | object | |
| 66 | + * @return mixed \WP_Error | object | |
| 49 | 67 | */ |
| 50 | 68 | public function get( $cmd, $params = array() ) { |
| 51 | 69 | |
| 52 | 70 | return $this->request( $cmd, 'get', $params ); |
| @@ -59,9 +77,9 @@ | ||
| 59 | 77 | * @since 1.0.0 |
| 60 | 78 | * |
| 61 | 79 | * @param string $cmd Command (required). |
| 62 | 80 | * @param array $params Params (optional). |
| 63 | - * @return mixed WP_Error | object | |
| 81 | + * @return mixed \WP_Error | object | |
| 64 | 82 | */ |
| 65 | 83 | public function post( $cmd, $params = array() ) { |
| 66 | 84 | |
| 67 | 85 | return $this->request( $cmd, 'post', $params ); |
| @@ -75,9 +93,9 @@ | ||
| 75 | 93 | * |
| 76 | 94 | * @param string $cmd Command. |
| 77 | 95 | * @param string $method Method (get|post). |
| 78 | 96 | * @param array $params Parameters (optional). |
| 79 | - * @return mixed WP_Error | object | |
| 97 | + * @return mixed \WP_Error | object | |
| 80 | 98 | */ |
| 81 | 99 | private function request( $cmd, $method = 'get', $params = array() ) { |
| 82 | 100 | |
| 83 | 101 | // Set timeout. |
| @@ -109,13 +127,14 @@ | ||
| 109 | 127 | * @param string $cmd API Command. |
| 110 | 128 | * @param string $method Method (post|get). |
| 111 | 129 | * @param array $params Parameters. |
| 112 | 130 | * @param int $timeout Timeout, in seconds (default: 10). |
| 113 | - * @return mixed WP_Error | object | |
| 131 | + * @return mixed \WP_Error | object | |
| 114 | 132 | */ |
| 115 | 133 | private function request_wordpress( $url, $cmd, $method, $params, $timeout = 20 ) { |
| 116 | 134 | |
| 117 | 135 | // Send request. |
| 136 | + $response = new \WP_Error( $this->base->plugin->filter_name . '_api_invalid_method', __( 'Invalid request method.', 'wp-to-buffer' ) ); | |
| 118 | 137 | switch ( $method ) { |
| 119 | 138 | /** |
| 120 | 139 | * GET |
| 121 | 140 | */ |