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

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