PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
3.8.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 All 112 releases
templately / modules / single-import / REST / Import.php

Import.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at modules/single-import/REST/Import.php

231 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Modules\SingleImport\REST;
4
5 use Elementor\Plugin;
6 use Elementor\Core\Experiments\Manager as Experiments_Manager;
7 use Templately\API\API;
8 use Templately\Modules\FullSiteImport\Utils\Utils;
9 use Templately\Utils\Helper;
10 use Templately\Utils\Response\FatalGuard;
11
12 /**
13 * @method \Templately\Modules\ElementorIntegration\Platform\Elementor|\Templately\Modules\GutenbergIntegration\Platform\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 // A fatal mid-insert (broken template data, dependency plugin code) would
26 // otherwise reach the client as an unparseable blank 500 — capture it.
27 FatalGuard::arm( 'single-import/insert' );
28
29 $platform = $this->get_param( 'platform', 'elementor' );
30 $origin = $this->get_param( 'origin', 'remote' );
31 $id = $this->get_param( 'id', 0, 'intval' );
32 $postId = $this->get_param( 'postId', 0, 'intval' );
33 $settings = $this->get_param( 'settings', [], null );
34
35 if( $id <= 0 || $id == null ) {
36 return $this->error('invalid_item_id', __( 'Invalid ID is provided.', 'templately' ), 'get_content', 404 );
37 }
38 if($platform === 'elementor') {
39 if(Helper::enable_elementor_container()) {
40 return $this->error(
41 'retry_again',
42 __( 'Please try again we enabled container.', 'templately' ),
43 'insert', 404
44 );
45 }
46 }
47
48 @set_time_limit( 0 ); // Unlimited execution time for this request.
49 Utils::add_gd_editor_filter();
50
51 switch ( $origin ) {
52 case 'cloud':
53 $template_data = $this->get_cloud_content( $id, $platform );
54 break;
55 case 'remote':
56 $template_data = $this->get_remote_content( $id );
57 break;
58 case 'local':
59 default:
60 $template_data = $this->get_local_content( $id, $platform );
61 $template_data = isset( $template_data['data'] ) ? $template_data['data'] : [];
62 break;
63 }
64
65 /**
66 * A fallback for old cloud items.
67 */
68 $template_data = is_string( $template_data ) ? json_decode( wp_unslash($template_data), true ) : $template_data;
69
70 if( is_wp_error( $template_data ) ) {
71 return $template_data;
72 }
73
74 if( empty( $template_data ) || ! is_array( $template_data ) || empty( $template_data['content'] ) ) {
75 return $this->error(
76 'invalid_template_data',
77 __( 'Template data is invalid. Please kindly contact Templately Support.', 'templately' ),
78 'insert', 404
79 );
80 }
81
82 return $this->platform( $platform )->insert( $template_data, $postId, $settings );
83 }
84
85 private function get_cloud_content( $id = null, $platform = 'elementor' ){
86 $response = $this->http()->query(
87 'myCloudInsert', '',
88 [
89 'api_key' => $this->api_key,
90 'file_id' => $id,
91 'file_type' => $platform
92 ]
93 )->post();
94
95 if( is_wp_error( $response ) ) {
96 // FIXME: need to return an error with handler in react app
97 return $this->error( 'invalid_data', $response->get_error_message(), 'get_cloud_content', 404 );
98 }
99
100 return is_string( $response ) ? json_decode( $response, true ) : $response;
101 }
102
103 private function get_remote_content( $id = null ){
104
105 $response = $this->http()->query(
106 'itemContent',
107 'status, message, data',
108 [
109 'api_key' => $this->api_key,
110 'id' => $id
111 ]
112 )->post();
113
114 if( is_wp_error( $response ) ) {
115 // FIXME: need to return an error with handler in react app
116 return $this->error( 'invalid_data', $response->get_error_message(), 'get_remote_content', 404 );
117 }
118
119 if( isset( $response['status'] ) && $response['status'] === 'error' ) {
120 return $this->error( 'invalid_data', $response['message'], 'get_content', 404 );
121 }
122 if( isset( $response['status'] ) && $response['status'] === 'required-pro-acc' ) {
123 return $this->error( 'required_pro_templately', $response['message'], 'get_remote_content', 404 );
124 }
125 // The app can now report that a template's file could not be loaded
126 // (docs/ai-editor/plugin-instructions.md §0). There is NO `data` on this status —
127 // check status before reading data, surface the app's message, and
128 // abandon the insert (no auto-retry: a missing file stays missing).
129 if( isset( $response['status'] ) && $response['status'] === 'content-unavailable' ) {
130 $message = ! empty( $response['message'] )
131 ? $response['message']
132 : __( 'This template’s file could not be loaded. Please try again, and contact support if it keeps happening.', 'templately' );
133 return $this->error( 'content_unavailable', $message, 'get_remote_content', 404 );
134 }
135
136 if( ! empty( $response[ 'data' ] ) && is_string( $response[ 'data' ] ) ) {
137 $data = json_decode( $response[ 'data' ], true );
138 } else {
139 $data = isset( $response[ 'data' ] ) ? (array) $response[ 'data' ] : [];
140 }
141
142 return $data;
143 }
144
145 private function get_local_content( $id = null, $platform = 'elementor' ){
146 /**
147 * @var \Templately\Modules\ElementorIntegration\Platform\Elementor $platformInstance
148 */
149 $platformInstance = $this->platform( $platform );
150 $response = $platformInstance->get_content( $id );
151
152 if( isset( $response['status'] ) && $response['status'] === 'error' ) {
153 return $this->error( 'invalid_data', $response['message'], 'get_local_content', 404 );
154 }
155
156 return $response;
157 }
158
159 public function get_content ( $id = null, $platform = 'elementor', $origin = 'remote' ) {
160 switch ( $origin ) {
161 case 'cloud':
162 $template_data = $this->get_cloud_content( $id, $platform );
163 break;
164 case 'remote':
165 $template_data = $this->get_remote_content( $id );
166 break;
167 case 'local':
168 default:
169 $template_data = $this->get_local_content( $id, $platform );
170 break;
171 }
172
173 return $template_data;
174 }
175
176 public function import_in_library(){
177 FatalGuard::arm( 'single-import/library' );
178
179 $platform = $this->get_param( 'platform', 'elementor' );
180 $id = $this->get_param( 'id', 0, 'intval' );
181 $settings = $this->get_param( 'settings', [], null );
182 $template_type = $this->get_param( 'template_type', '', 'sanitize_text_field' );
183 $type = $this->get_param( 'type', '', 'sanitize_text_field' );
184
185 if( $id == 0 ) {
186 return $this->error('invalid_item_id', __( 'Invalid ID is provided.', 'templately' ), 'import/page', 404 );
187 }
188 if($platform === 'elementor') {
189 if(Helper::enable_elementor_container()) {
190 return $this->error(
191 'retry_again',
192 __( 'Please try again we enabled container.', 'templately' ),
193 'insert', 404
194 );
195 }
196 }
197
198 @set_time_limit( 0 ); // Unlimited execution time for this request.
199 Utils::add_gd_editor_filter();
200
201 return $this->platform( $platform )->import_in_library( $id, $this, $settings, $template_type, $type );
202 }
203
204 public function import_as_page(){
205 FatalGuard::arm( 'single-import/page' );
206
207 $platform = $this->get_param( 'platform', 'elementor' );
208 $id = $this->get_param( 'id', 0, 'intval' );
209 $title = $this->get_param( 'title' );
210 $settings = $this->get_param( 'settings', [], null );
211
212 if( $id == 0 ) {
213 return $this->error('invalid_item_id', __( 'Invalid ID is provided.', 'templately' ), 'import/page', 404 );
214 }
215 if($platform === 'elementor') {
216 if(Helper::enable_elementor_container()) {
217 return $this->error(
218 'retry_again',
219 __( 'Please try again we enabled container.', 'templately' ),
220 'insert', 404
221 );
222 }
223 }
224
225 @set_time_limit( 0 ); // Unlimited execution time for this request.
226 Utils::add_gd_editor_filter();
227
228 return $this->platform( $platform )->create_page( $id, $title, $this, $settings );
229 }
230 }
231