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

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