PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 1.3.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v1.3.0
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 / core / api / class-rest-api.php

class-rest-api.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 1.3.0, at core/api/class-rest-api.php

191 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Templately;
3
4 class REST {
5 /**
6 * Instance of REST
7 * @var REST
8 */
9 protected static $_instance = null;
10 /**
11 * Get the instance of REST
12 * @return REST
13 */
14 public static function get_instance() {
15 if ( static::$_instance === null ) {
16 static::$_instance = new static;
17 }
18
19 return static::$_instance;
20 }
21 /**
22 * Contains WP_User
23 *
24 * @var WP_User
25 */
26 protected $current_user;
27 /**
28 * Initially Invoked
29 */
30 public function __construct(){
31 \add_action( 'rest_api_init', [ $this, 'register_routes' ] );
32 }
33 /**
34 * Permission check for each route
35 *
36 * @return boolean
37 */
38 public function check_permission( $request ) {
39 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
40 if( ! empty( $api_key ) && \user_can($this->current_user, 'edit_posts') ) {
41 return true;
42 }
43 return false;
44 }
45 /**
46 * Register Rest Api Init
47 *
48 * @param WP_REST_Server $wp_rest_server
49 * @return void
50 */
51 public function register_routes( $wp_rest_server ){
52 /**
53 * Get the current user
54 * @var WP_User
55 */
56 $this->current_user = \wp_get_current_user();
57 /**
58 * Register Routes
59 */
60 register_rest_route( 'templately/v1', '/clouds', [
61 'methods' => \WP_REST_Server::READABLE,
62 'callback' => [ $this, 'download' ],
63 'permission_callback' => [ $this, 'check_permission' ],
64 'args' => [
65 'id' => [
66 'required' => true,
67 'validate_callback' => function( $id ) { return is_numeric( $id ); }
68 ]
69 ]
70 ]);
71 /**
72 * My Favourites
73 */
74 register_rest_route( 'templately/v1', '/my-favourites', [
75 'methods' => \WP_REST_Server::READABLE,
76 'callback' => [ $this, 'my_favourites' ],
77 'permission_callback' => [ $this, 'check_permission' ]
78 ]);
79 /**
80 * My Downloads
81 */
82 register_rest_route( 'templately/v1', '/my-downloads', [
83 'methods' => \WP_REST_Server::READABLE,
84 'callback' => [ $this, 'my_downloads' ],
85 'permission_callback' => [ $this, 'check_permission' ]
86 ]);
87 }
88
89 public function my_favourites( $request ){
90 $type = "";
91 if( $request->has_param('type') ) {
92 $type = $request->get_param('type');
93 }
94 $plan_type = 1; // 1 = all, 2 = free, 3 = pro
95 if( $request->has_param('plan_type') ) {
96 $plan_type = intval( $request->get_param('plan_type') );
97 }
98 $page = 1;
99 if( $request->has_param('page') ) {
100 $page = intval( $request->get_param('page') );
101 }
102
103 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
104 $response = Query::get(
105 Query::prepare(
106 'mutation { myFavouriteItem( api_key: "%s", type: "%s", plan_type: %d, per_page: 8, page: %d ){ total_page, current_page, data { id, name, rating, type, slug, favourite_count, thumbnail, price, author{ display_name, name, joined }, category{ id, name } } } }',
107 $api_key, $type, $plan_type, $page
108 ),
109 [
110 'is_rest' => true,
111 'only_data' => true,
112 'query' => 'myFavouriteItem'
113 ]
114 );
115
116 return $response;
117 }
118 public function my_downloads( $request ){
119 $page = 1;
120 if( $request->has_param('page') ) {
121 $page = $request->get_param('page');
122 }
123 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
124 $response = Query::get(
125 Query::prepare(
126 'mutation { myDownloadHistory( api_key: "%s", per_page: 10, page: %s ){ data{ name, thumbnail, downloaded_at, slug, type }, current_page, total_page } }',
127 $api_key,
128 $page
129 ),
130 [
131 'is_rest' => true,
132 'only_data' => true,
133 'query' => 'myDownloadHistory'
134 ]
135 );
136
137 return $response;
138 }
139
140 public function error( $type = '' ) {
141 switch( $type ) {
142 case 'api':
143 return $this->formattedError( 'api_error', __( 'Unathorized Access: You have to logged in first.', 'templately' ), 401 );
144 break;
145 default:
146 return $this->formattedError( 'response_error', __( '400 Bad Request.', 'templately' ), 400 );
147 }
148 }
149
150 public function download( $request ){
151 if( $request->has_param('id') ) {
152 $id = $request->get_param('id');
153 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
154 if( empty( $api_key ) ) {
155 return $this->error('api');
156 }
157 $response = Query::get(
158 Query::prepare(
159 'mutation { downloadMyCloudItem( api_key: "%s", id: %d ){ file, status, message, file_name, file_type } }',
160 $api_key, +$id
161 ),
162 [
163 'is_rest' => true,
164 'only_data' => true,
165 'query' => 'downloadMyCloudItem'
166 ]
167 );
168
169 if( isset( $response['file_name'] ) ) {
170 require_once ABSPATH . '/wp-admin/includes/file.php';
171 $upload_dir = wp_upload_dir();
172 $file_name = 'templately-tmp.json';
173 $templately_temp_file = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $file_name;
174 if ( \file_exists( $templately_temp_file ) ) {
175 unlink( $templately_temp_file );
176 }
177 $templately_temp_file_url = $upload_dir['baseurl'] . DIRECTORY_SEPARATOR . $file_name;
178 $handle = fopen( $templately_temp_file, 'x+' );
179 fwrite( $handle, $response['file'] );
180 fclose( $handle );
181 $response['fileURL'] = $templately_temp_file_url;
182 }
183 return $response;
184 }
185 return $this->error();
186 }
187
188 private function formattedError( $code, $message, $http_code, $args = [] ){
189 return new \WP_Error( "templately_$code", $message, [ 'status' => $http_code ] );
190 }
191 }