PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 1.3.2
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v1.3.2
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
← All changes | core/api/class-rest-api.php +193 -0 1-final1.3.2 View file →
@@ -1,0 +1,193 @@
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 + private static $query = null;
31 + public function __construct(){
32 + self::$query = Query::instance();
33 + \add_action( 'rest_api_init', [ $this, 'register_routes' ] );
34 + }
35 + /**
36 + * Permission check for each route
37 + *
38 + * @return boolean
39 + */
40 + public function check_permission( $request ) {
41 + $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
42 + if( ! empty( $api_key ) && \user_can($this->current_user, 'edit_posts') ) {
43 + return true;
44 + }
45 + return false;
46 + }
47 + /**
48 + * Register Rest Api Init
49 + *
50 + * @param WP_REST_Server $wp_rest_server
51 + * @return void
52 + */
53 + public function register_routes( $wp_rest_server ){
54 + /**
55 + * Get the current user
56 + * @var WP_User
57 + */
58 + $this->current_user = \wp_get_current_user();
59 + /**
60 + * Register Routes
61 + */
62 + register_rest_route( 'templately/v1', '/clouds', [
63 + 'methods' => \WP_REST_Server::READABLE,
64 + 'callback' => [ $this, 'download' ],
65 + 'permission_callback' => [ $this, 'check_permission' ],
66 + 'args' => [
67 + 'id' => [
68 + 'required' => true,
69 + 'validate_callback' => function( $id ) { return is_numeric( $id ); }
70 + ]
71 + ]
72 + ]);
73 + /**
74 + * My Favourites
75 + */
76 + register_rest_route( 'templately/v1', '/my-favourites', [
77 + 'methods' => \WP_REST_Server::READABLE,
78 + 'callback' => [ $this, 'my_favourites' ],
79 + 'permission_callback' => [ $this, 'check_permission' ]
80 + ]);
81 + /**
82 + * My Downloads
83 + */
84 + register_rest_route( 'templately/v1', '/my-downloads', [
85 + 'methods' => \WP_REST_Server::READABLE,
86 + 'callback' => [ $this, 'my_downloads' ],
87 + 'permission_callback' => [ $this, 'check_permission' ]
88 + ]);
89 + }
90 +
91 + public function my_favourites( $request ){
92 + $type = "";
93 + if( $request->has_param('type') ) {
94 + $type = $request->get_param('type');
95 + }
96 + $plan_type = 1; // 1 = all, 2 = free, 3 = pro
97 + if( $request->has_param('plan_type') ) {
98 + $plan_type = intval( $request->get_param('plan_type') );
99 + }
100 + $page = 1;
101 + if( $request->has_param('page') ) {
102 + $page = intval( $request->get_param('page') );
103 + }
104 +
105 + $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
106 + $response = self::$query->get(
107 + self::$query->prepare(
108 + '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 } } } }',
109 + $api_key, $type, $plan_type, $page
110 + ),
111 + [
112 + 'is_rest' => true,
113 + 'only_data' => true,
114 + 'query' => 'myFavouriteItem'
115 + ]
116 + );
117 +
118 + return $response;
119 + }
120 + public function my_downloads( $request ){
121 + $page = 1;
122 + if( $request->has_param('page') ) {
123 + $page = $request->get_param('page');
124 + }
125 + $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
126 + $response = self::$query->get(
127 + self::$query->prepare(
128 + 'mutation { myDownloadHistory( api_key: "%s", per_page: 10, page: %s ){ data{ name, thumbnail, downloaded_at, slug, type }, current_page, total_page } }',
129 + $api_key,
130 + $page
131 + ),
132 + [
133 + 'is_rest' => true,
134 + 'only_data' => true,
135 + 'query' => 'myDownloadHistory'
136 + ]
137 + );
138 +
139 + return $response;
140 + }
141 +
142 + public function error( $type = '' ) {
143 + switch( $type ) {
144 + case 'api':
145 + return $this->formattedError( 'api_error', __( 'Unathorized Access: You have to logged in first.', 'templately' ), 401 );
146 + break;
147 + default:
148 + return $this->formattedError( 'response_error', __( '400 Bad Request.', 'templately' ), 400 );
149 + }
150 + }
151 +
152 + public function download( $request ){
153 + if( $request->has_param('id') ) {
154 + $id = $request->get_param('id');
155 + $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
156 + if( empty( $api_key ) ) {
157 + return $this->error('api');
158 + }
159 + $response = self::$query->get(
160 + self::$query->prepare(
161 + 'mutation { downloadMyCloudItem( api_key: "%s", id: %d ){ file, status, message, file_name, file_type } }',
162 + $api_key, +$id
163 + ),
164 + [
165 + 'is_rest' => true,
166 + 'only_data' => true,
167 + 'query' => 'downloadMyCloudItem'
168 + ]
169 + );
170 +
171 + if( isset( $response['file_name'] ) ) {
172 + require_once ABSPATH . '/wp-admin/includes/file.php';
173 + $upload_dir = wp_upload_dir();
174 + $file_name = 'templately-tmp.json';
175 + $templately_temp_file = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $file_name;
176 + if ( \file_exists( $templately_temp_file ) ) {
177 + unlink( $templately_temp_file );
178 + }
179 + $templately_temp_file_url = $upload_dir['baseurl'] . DIRECTORY_SEPARATOR . $file_name;
180 + $handle = fopen( $templately_temp_file, 'x+' );
181 + fwrite( $handle, $response['file'] );
182 + fclose( $handle );
183 + $response['fileURL'] = $templately_temp_file_url;
184 + }
185 + return $response;
186 + }
187 + return $this->error();
188 + }
189 +
190 + private function formattedError( $code, $message, $http_code, $args = [] ){
191 + return new \WP_Error( "templately_$code", $message, [ 'status' => $http_code ] );
192 + }
193 +}