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

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