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

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