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

124 lines 3.9 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() {
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
73 public function error( $type = '' ) {
74 switch( $type ) {
75 case 'api':
76 return $this->formattedError( 'api_error', __( 'Unathorized Access: You have to logged in first.', 'templately' ), 401 );
77 break;
78 default:
79 return $this->formattedError( 'response_error', __( '400 Bad Request.', 'templately' ), 400 );
80 }
81 }
82
83 public function download( $request ){
84 if( $request->has_param('id') ) {
85 $id = $request->get_param('id');
86 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
87 if( empty( $api_key ) ) {
88 return $this->error('api');
89 }
90 $response = Query::get(
91 Query::prepare(
92 'mutation { downloadMyCloudItem( api_key: "%s", id: %d ){ file, status, message, file_name, file_type } }',
93 $api_key, +$id
94 ),
95 [
96 'is_rest' => true,
97 'only_data' => true,
98 'query' => 'downloadMyCloudItem'
99 ]
100 );
101
102 if( isset( $response['file_name'] ) ) {
103 require_once ABSPATH . '/wp-admin/includes/file.php';
104 $upload_dir = wp_upload_dir();
105 $file_name = 'templately-tmp.json';
106 $templately_temp_file = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $file_name;
107 if ( \file_exists( $templately_temp_file ) ) {
108 unlink( $templately_temp_file );
109 }
110 $templately_temp_file_url = $upload_dir['baseurl'] . DIRECTORY_SEPARATOR . $file_name;
111 $handle = fopen( $templately_temp_file, 'x+' );
112 fwrite( $handle, $response['file'] );
113 fclose( $handle );
114 $response['fileURL'] = $templately_temp_file_url;
115 }
116 return $response;
117 }
118 return $this->error();
119 }
120
121 private function formattedError( $code, $message, $http_code, $args = [] ){
122 return new \WP_Error( "templately_$code", $message, [ 'status' => $http_code ] );
123 }
124 }