PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.0.4
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.0.4
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 / Import.php

Import.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.0.4, at includes/API/Import.php

167 lines 5.7 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 Templately\Core\Platform\Elementor;
6 use Templately\Core\Platform\Gutenberg;
7
8 /**
9 * @method Elementor|Gutenberg platform( $id );
10 */
11 class Import extends API {
12 private $endpoint = 'import';
13
14 public function register_routes() {
15 $this->post( 'insert', [ $this, 'insert_template' ] );
16 $this->post( $this->endpoint, [ $this, 'import_in_library' ] );
17 $this->post( $this->endpoint . '/page', [ $this, 'import_as_page' ] );
18 }
19
20 public function insert_template() {
21 $platform = $this->get_param( 'platform', 'elementor' );
22 $origin = $this->get_param( 'origin', 'remote' );
23 $id = $this->get_param( 'id', 0, 'intval' );
24 $postId = $this->get_param( 'postId', 0, 'intval' );
25
26 if( $id <= 0 || $id == null ) {
27 return $this->error('invalid_item_id', __( 'Invalid ID is provided.', 'templately' ), 'get_content', 404 );
28 }
29
30 switch ( $origin ) {
31 case 'cloud':
32 $template_data = $this->get_cloud_content( $id, $platform );
33 break;
34 case 'remote':
35 $template_data = $this->get_remote_content( $id );
36 break;
37 case 'local':
38 default:
39 $template_data = $this->get_local_content( $id, $platform );
40 $template_data = isset( $template_data['data'] ) ? $template_data['data'] : [];
41 break;
42 }
43
44 /**
45 * A fallback for old cloud items.
46 */
47 $template_data = is_string( $template_data ) ? json_decode( wp_unslash($template_data), true ) : $template_data;
48
49 if( is_wp_error( $template_data ) ) {
50 return $template_data;
51 }
52
53 if( empty( $template_data ) || ! is_array( $template_data ) || empty( $template_data['content'] ) ) {
54 return $this->error(
55 'invalid_template_data',
56 __( 'Template data is invalid. Please kindly contact Templately Support.', 'templately' ),
57 'insert', 404
58 );
59 }
60
61 return $this->platform( $platform )->insert( $template_data, $postId );
62 }
63
64 private function get_cloud_content( $id = null, $platform = 'elementor' ){
65 $response = $this->http()->query(
66 'myCloudInsert', '',
67 [
68 'api_key' => $this->api_key,
69 'file_id' => $id,
70 'file_type' => $platform
71 ]
72 )->post();
73
74 if( is_wp_error( $response ) ) {
75 // FIXME: need to return an error with handler in react app
76 return $this->error( 'invalid_data', $response->get_error_message(), 'get_cloud_content', 404 );
77 }
78
79 return is_string( $response ) ? json_decode( $response, true ) : $response;
80 }
81
82 private function get_remote_content( $id = null ){
83
84 $response = $this->http()->query(
85 'itemContent',
86 'status, message, data',
87 [
88 'api_key' => $this->api_key,
89 'id' => $id
90 ]
91 )->post();
92
93 if( is_wp_error( $response ) ) {
94 // FIXME: need to return an error with handler in react app
95 return $this->error( 'invalid_data', $response->get_error_message(), 'get_remote_content', 404 );
96 }
97
98 if( isset( $response['status'] ) && $response['status'] === 'error' ) {
99 return $this->error( 'invalid_data', $response['message'], 'get_content', 404 );
100 }
101 if( isset( $response['status'] ) && $response['status'] === 'required-pro-acc' ) {
102 return $this->error( 'required_pro_templately', $response['message'], 'get_remote_content', 404 );
103 }
104
105 if( ! empty( $response[ 'data' ] ) && is_string( $response[ 'data' ] ) ) {
106 $data = json_decode( $response[ 'data' ], true );
107 } else {
108 $data = isset( $response[ 'data' ] ) ? (array) $response[ 'data' ] : [];
109 }
110
111 return $data;
112 }
113
114 private function get_local_content( $id = null, $platform = 'elementor' ){
115 /**
116 * @var Elementor $platformInstance
117 */
118 $platformInstance = $this->platform( $platform );
119 $response = $platformInstance->get_content( $id );
120
121 if( isset( $response['status'] ) && $response['status'] === 'error' ) {
122 return $this->error( 'invalid_data', $response['message'], 'get_local_content', 404 );
123 }
124
125 return $response;
126 }
127
128 public function get_content ( $id = null, $platform = 'elementor', $origin = 'remote' ) {
129 switch ( $origin ) {
130 case 'cloud':
131 $template_data = $this->get_cloud_content( $id, $platform );
132 break;
133 case 'remote':
134 $template_data = $this->get_remote_content( $id );
135 break;
136 case 'local':
137 default:
138 $template_data = $this->get_local_content( $id, $platform );
139 break;
140 }
141
142 return $template_data;
143 }
144
145 public function import_in_library(){
146 $platform = $this->get_param( 'platform', 'elementor' );
147 $id = $this->get_param( 'id', 0, 'intval' );
148
149 if( $id == 0 ) {
150 return $this->error('invalid_item_id', __( 'Invalid ID is provided.', 'templately' ), 'import/page', 404 );
151 }
152
153 return $this->platform( $platform )->import_in_library( $id, $this );
154 }
155
156 public function import_as_page(){
157 $platform = $this->get_param( 'platform', 'elementor' );
158 $id = $this->get_param( 'id', 0, 'intval' );
159 $title = $this->get_param( 'title' );
160
161 if( $id == 0 ) {
162 return $this->error('invalid_item_id', __( 'Invalid ID is provided.', 'templately' ), 'import/page', 404 );
163 }
164
165 return $this->platform( $platform )->create_page( $id, $title, $this );
166 }
167 }