| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\API; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
use WP_REST_Server; |
| 7 |
use WP_REST_Request; |
| 8 |
use function ucfirst; |
| 9 |
|
| 10 |
use WP_REST_Response; |
| 11 |
use Templately\Utils\Base; |
| 12 |
use Templately\Utils\Http; |
| 13 |
use Templately\Utils\Plan; |
| 14 |
use Templately\Core\Module; |
| 15 |
use function call_user_func; |
| 16 |
|
| 17 |
use Templately\Utils\Helper; |
| 18 |
use Templately\Utils\Options; |
| 19 |
use function register_rest_route; |
| 20 |
|
| 21 |
/** |
| 22 |
* @method Http http() |
| 23 |
* @method Options options() |
| 24 |
* @method Options|Http|Helper utils( string $name ) |
| 25 |
*/ |
| 26 |
abstract class API extends Base { |
| 27 |
protected $api_key; |
| 28 |
protected $request; |
| 29 |
|
| 30 |
private static $allowed_classes = [ |
| 31 |
'utils' => [ |
| 32 |
'options', |
| 33 |
'http', |
| 34 |
'helper' |
| 35 |
], |
| 36 |
'api' => [ |
| 37 |
'dependencies', |
| 38 |
], |
| 39 |
]; |
| 40 |
|
| 41 |
public function __construct() { |
| 42 |
/** |
| 43 |
* Registering as a submodule of the API module |
| 44 |
*/ |
| 45 |
Module::get_instance()->add( (object) [ |
| 46 |
'object' => $this |
| 47 |
], 'API' ); |
| 48 |
} |
| 49 |
|
| 50 |
private static function is_allowed( $type, $class ){ |
| 51 |
if( ! array_key_exists( $type, self::$allowed_classes ) ) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
if( ! class_exists( '\\Templately\\'. ucfirst( $type ) .'\\' . ucfirst( $class ) ) ) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @param $type |
| 62 |
* @param $parameters |
| 63 |
* |
| 64 |
* @return mixed|void |
| 65 |
*/ |
| 66 |
public static function __callStatic( $type, $parameters = [] ){ |
| 67 |
return ( new static )->call_user_func( $type, $parameters ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @param $type |
| 72 |
* @param $parameters |
| 73 |
* |
| 74 |
* @return mixed|void |
| 75 |
*/ |
| 76 |
public function __call( $type, $parameters = [] ){ |
| 77 |
return $this->call_user_func( $type, $parameters ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param $type |
| 82 |
* @param $parameters |
| 83 |
* |
| 84 |
* @return mixed|void |
| 85 |
*/ |
| 86 |
protected function call_user_func( $type, $parameters = [] ) { |
| 87 |
if( $type === 'http' || $type === 'options' ) { |
| 88 |
$parameters[0] = $type; |
| 89 |
$type = 'utils'; |
| 90 |
} |
| 91 |
if( ! empty ( $parameters[0] ) && self::is_allowed( $type, $parameters[0] ) ) { |
| 92 |
return call_user_func( [ '\\Templately\\'. ucfirst( $type ) .'\\' . ucfirst( $parameters[0] ), 'get_instance' ] ); |
| 93 |
} |
| 94 |
|
| 95 |
Helper::trigger_error( $this ); |
| 96 |
} |
| 97 |
|
| 98 |
protected function get_namespace( $endpoint = '' ) { |
| 99 |
return '/' . TEMPLATELY_API_NAMESPACE . ( ! empty( $endpoint ) ? "/$endpoint" : '' ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @param string $param |
| 104 |
* @param mixed $default |
| 105 |
* @param string $sanitizer |
| 106 |
* |
| 107 |
* @return false|mixed |
| 108 |
*/ |
| 109 |
public function get_param( $param, $default = '', $sanitizer = 'sanitize_text_field' ) { |
| 110 |
$_value = $this->request->get_param( $param ); |
| 111 |
if ( ! empty( $_value ) ) { |
| 112 |
if( is_callable($sanitizer) && ! is_array( $_value ) ) { |
| 113 |
return call_user_func_array( $sanitizer, [ $_value ] ); |
| 114 |
} elseif ( is_array( $_value ) && is_callable($sanitizer) ) { |
| 115 |
return array_map( $sanitizer, $_value ); |
| 116 |
} |
| 117 |
return $_value; |
| 118 |
} |
| 119 |
|
| 120 |
return $default; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @param $request WP_REST_Request for getting all route request in time. |
| 125 |
* |
| 126 |
* @return WP_Error|boolean |
| 127 |
*/ |
| 128 |
public function _permission_check( WP_REST_Request $request ) { |
| 129 |
$this->request = $request; |
| 130 |
$this->api_key = $this->utils('options')->get( 'api_key' ); |
| 131 |
if(!current_user_can('delete_posts')){ |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
add_filter('wp_redirect', '__return_false', 999); |
| 136 |
|
| 137 |
return $this->permission_check( $request ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @param $request WP_REST_Request for getting all route request in time. |
| 142 |
* |
| 143 |
* @return WP_Error|boolean |
| 144 |
*/ |
| 145 |
public function permission_check( WP_REST_Request $request ) { |
| 146 |
$this->request = $request; |
| 147 |
$this->api_key = $this->utils('options')->get( 'api_key' ); |
| 148 |
|
| 149 |
|
| 150 |
if ( ! empty( $this->api_key ) ) { |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
$_route = $request->get_route(); |
| 155 |
return $this->permission_error( '', $_route ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @param $message |
| 160 |
* @param $endpoint |
| 161 |
* |
| 162 |
* @return WP_Error |
| 163 |
*/ |
| 164 |
protected function permission_error( $message, $endpoint = '') { |
| 165 |
if( empty( $message ) ) { |
| 166 |
$message = __( 'Sorry, you need to login/re-login again.', 'templately' ); |
| 167 |
} |
| 168 |
|
| 169 |
$_additional_data = [ |
| 170 |
'status' => rest_authorization_required_code(), |
| 171 |
]; |
| 172 |
|
| 173 |
if( ! empty( $endpoint ) ) { |
| 174 |
$_additional_data['endpoint'] = $endpoint; |
| 175 |
} |
| 176 |
|
| 177 |
// Delete user logged meta data |
| 178 |
$this->utils('options') |
| 179 |
->remove('user') |
| 180 |
->remove('favourites') |
| 181 |
->remove('reviews') |
| 182 |
->remove('cloud_activity') |
| 183 |
->remove('api_key'); |
| 184 |
|
| 185 |
if( $this->utils('options')->who_am_i() === 'global' ) { |
| 186 |
$this->utils('options')->remove_global_login(); |
| 187 |
} |
| 188 |
|
| 189 |
return new WP_Error( 'invalid_api_key', $message, $_additional_data ); |
| 190 |
} |
| 191 |
|
| 192 |
public function get( $endpoint, $callback, $args = [] ){ |
| 193 |
return $this->register_endpoint( $endpoint, $callback, $args, WP_REST_Server::READABLE ); |
| 194 |
} |
| 195 |
public function post( $endpoint, $callback, $args = [] ){ |
| 196 |
return $this->register_endpoint( $endpoint, $callback, $args ); |
| 197 |
} |
| 198 |
|
| 199 |
public function register_endpoint( $endpoint, $callback, $args = [], $methods = WP_REST_Server::CREATABLE ) { |
| 200 |
return register_rest_route( |
| 201 |
TEMPLATELY_API_NAMESPACE, |
| 202 |
$endpoint, |
| 203 |
[ |
| 204 |
'methods' => $methods, |
| 205 |
'callback' => $callback, |
| 206 |
'permission_callback' => [ $this, '_permission_check' ], |
| 207 |
'args' => $args, |
| 208 |
] |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
public function response( $response, $endpoint, $status = 500, $additional_data = [] ) { |
| 213 |
if ( $response instanceof WP_Error ) { |
| 214 |
return $this->error( |
| 215 |
$response->get_error_code(), |
| 216 |
$response->get_error_message(), |
| 217 |
$endpoint, |
| 218 |
$status, |
| 219 |
$additional_data |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
return $this->success( $response ); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* @param $data |
| 228 |
* |
| 229 |
* @return WP_REST_Response |
| 230 |
*/ |
| 231 |
public function success( $data ) { |
| 232 |
return new WP_REST_Response( $data, 200 ); |
| 233 |
} |
| 234 |
/** |
| 235 |
* @param $error_code string |
| 236 |
* @param $error_message string|array |
| 237 |
* @param $endpoint string |
| 238 |
* @param $status int |
| 239 |
* @param $additional_data array |
| 240 |
* |
| 241 |
* @return WP_Error |
| 242 |
*/ |
| 243 |
public function error( $error_code, $error_message, $endpoint = '', $status = 500, $additional_data = [] ) { |
| 244 |
return Helper::error( $error_code, $error_message, $endpoint, $status, $additional_data ); |
| 245 |
} |
| 246 |
/** |
| 247 |
* @param $plan |
| 248 |
* |
| 249 |
* @return int |
| 250 |
*/ |
| 251 |
public function get_plan( $plan = 'all' ) { |
| 252 |
return Plan::get( $plan ); |
| 253 |
} |
| 254 |
} |