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

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

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