PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.1
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.1
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! 3.1.1, at includes/API/API.php

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