PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 2.2.7
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v2.2.7
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / API / API.php

API.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 2.2.7, at includes/API/API.php

235 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
132 if ( ! empty( $this->api_key ) ) {
133 return true;
134 }
135
136 $_route = $request->get_route();
137 return $this->permission_error( '', $_route );
138 }
139
140 /**
141 * @param $message
142 * @param $endpoint
143 *
144 * @return WP_Error
145 */
146 protected function permission_error( $message, $endpoint = '') {
147 if( empty( $message ) ) {
148 $message = __( 'Sorry, you need to login/re-login again.', 'templately' );
149 }
150
151 $_additional_data = [
152 'status' => rest_authorization_required_code(),
153 ];
154
155 if( ! empty( $endpoint ) ) {
156 $_additional_data['endpoint'] = $endpoint;
157 }
158
159 // Delete user logged meta data
160 $this->utils('options')
161 ->remove('user')
162 ->remove('favourites')
163 ->remove('cloud_activity')
164 ->remove('api_key');
165
166 if( $this->utils('options')->whoami() === 'global' ) {
167 $this->utils('options')->remove_global_login();
168 }
169
170 return new WP_Error( 'invalid_api_key', $message, $_additional_data );
171 }
172
173 public function get( $endpoint, $callback, $args = [] ){
174 return $this->register_endpoint( $endpoint, $callback, $args, WP_REST_Server::READABLE );
175 }
176 public function post( $endpoint, $callback, $args = [] ){
177 return $this->register_endpoint( $endpoint, $callback, $args );
178 }
179
180 public function register_endpoint( $endpoint, $callback, $args = [], $methods = WP_REST_Server::CREATABLE ) {
181 return register_rest_route(
182 TEMPLATELY_API_NAMESPACE,
183 $endpoint,
184 [
185 'methods' => $methods,
186 'callback' => $callback,
187 'permission_callback' => [ $this, 'permission_check' ],
188 'args' => $args,
189 ]
190 );
191 }
192
193 public function response( $response, $endpoint, $status = 500, $additional_data = [] ) {
194 if ( $response instanceof WP_Error ) {
195 return $this->error(
196 $response->get_error_code(),
197 $response->get_error_message(),
198 $endpoint,
199 $status,
200 $additional_data
201 );
202 }
203
204 return $this->success( $response );
205 }
206
207 /**
208 * @param $data
209 *
210 * @return WP_REST_Response
211 */
212 public function success( $data ) {
213 return new WP_REST_Response( $data, 200 );
214 }
215 /**
216 * @param $error_code string
217 * @param $error_message string|array
218 * @param $endpoint string
219 * @param $status int
220 * @param $additional_data array
221 *
222 * @return WP_Error
223 */
224 public function error( $error_code, $error_message, $endpoint = '', $status = 500, $additional_data = [] ) {
225 return Helper::error( $error_code, $error_message, $endpoint, $status, $additional_data );
226 }
227 /**
228 * @param $plan
229 *
230 * @return int
231 */
232 public function get_plan( $plan = 'all' ) {
233 return Plan::get( $plan );
234 }
235 }