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

161 lines 5.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 Templately\Core\Platform\Elementor;
6 use Templately\Core\Platform\Gutenberg;
7
8 /**
9 * @method Elementor|Gutenberg platform( $id );
10 */
11 class Import extends API {
12 private $endpoint = 'import';
13
14 public function register_routes() {
15 $this->post( 'insert', [ $this, 'insert_template' ] );
16 $this->post( $this->endpoint, [ $this, 'import_in_library' ] );
17 $this->post( $this->endpoint . '/page', [ $this, 'import_as_page' ] );
18 }
19
20 public function insert_template() {
21 $platform = $this->get_param( 'platform', 'elementor' );
22 $origin = $this->get_param( 'origin', 'remote' );
23 $id = $this->get_param( 'id', 0, 'intval' );
24
25 if( $id <= 0 || $id == null ) {
26 return $this->error('invalid_item_id', __( 'Invalid ID is provided.', 'templately' ), 'get_content', 404 );
27 }
28
29 switch ( $origin ) {
30 case 'cloud':
31 $template_data = $this->get_cloud_content( $id, $platform );
32 break;
33 case 'remote':
34 $template_data = $this->get_remote_content( $id, $platform, $origin );
35 break;
36 case 'local':
37 default:
38 $template_data = $this->get_local_content( $id, $platform );
39 $template_data = isset( $template_data['data'] ) ? $template_data['data'] : [];
40 break;
41 }
42
43 if( is_wp_error( $template_data ) ) {
44 return $template_data;
45 }
46
47 if( empty( $template_data ) || ! is_array( $template_data ) || empty( $template_data['content'] ) ) {
48 return $this->error(
49 'invalid_template_data',
50 __( 'Template data is invalid. Please kindly contact Templately Support.', 'templately' ),
51 'insert', 404
52 );
53 }
54
55 return $this->platform( $platform )->insert( $template_data );
56 }
57
58 private function get_cloud_content( $id = null, $platform = 'elementor' ){
59 $response = $this->http()->query(
60 'myCloudInsert', '',
61 [
62 'api_key' => $this->api_key,
63 'file_id' => $id,
64 'file_type' => $platform
65 ]
66 )->post();
67
68 if( is_wp_error( $response ) ) {
69 // FIXME: need to return an error with handler in react app
70 return $this->error( 'invalid_data', $response->get_error_message(), 'get_cloud_content', 404 );
71 }
72
73 return is_string( $response ) ? json_decode( $response, true ) : $response;
74 }
75
76 private function get_remote_content( $id = null ){
77
78 $response = $this->http()->query(
79 'itemContent',
80 'status, message, data',
81 [
82 'api_key' => $this->api_key,
83 'id' => $id
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_remote_content', 404 );
90 }
91
92 if( isset( $response['status'] ) && $response['status'] === 'error' ) {
93 return $this->error( 'invalid_data', $response['message'], 'get_content', 404 );
94 }
95 if( isset( $response['status'] ) && $response['status'] === 'required-pro-acc' ) {
96 return $this->error( 'required_pro_templately', $response['message'], 'get_remote_content', 404 );
97 }
98
99 if( ! empty( $response[ 'data' ] ) && is_string( $response[ 'data' ] ) ) {
100 $data = json_decode( $response[ 'data' ], true );
101 } else {
102 $data = isset( $response[ 'data' ] ) ? (array) $response[ 'data' ] : [];
103 }
104
105 return $data;
106 }
107
108 private function get_local_content( $id = null, $platform = 'elementor' ){
109 /**
110 * @var Elementor $platformInstance
111 */
112 $platformInstance = $this->platform( $platform );
113 $response = $platformInstance->get_content( $id );
114
115 if( isset( $response['status'] ) && $response['status'] === 'error' ) {
116 return $this->error( 'invalid_data', $response['message'], 'get_local_content', 404 );
117 }
118
119 return $response;
120 }
121
122 public function get_content ( $id = null, $platform = 'elementor', $origin = 'remote' ) {
123 switch ( $origin ) {
124 case 'cloud':
125 $template_data = $this->get_cloud_content( $id, $platform );
126 break;
127 case 'remote':
128 $template_data = $this->get_remote_content( $id );
129 break;
130 case 'local':
131 default:
132 $template_data = $this->get_local_content( $id, $platform );
133 break;
134 }
135
136 return $template_data;
137 }
138
139 public function import_in_library(){
140 $platform = $this->get_param( 'platform', 'elementor' );
141 $id = $this->get_param( 'id', 0, 'intval' );
142
143 if( $id == 0 ) {
144 return $this->error('invalid_item_id', __( 'Invalid ID is provided.', 'templately' ), 'import/page', 404 );
145 }
146
147 return $this->platform( $platform )->import_in_library( $id, $this );
148 }
149
150 public function import_as_page(){
151 $platform = $this->get_param( 'platform', 'elementor' );
152 $id = $this->get_param( 'id', 0, 'intval' );
153 $title = $this->get_param( 'title' );
154
155 if( $id == 0 ) {
156 return $this->error('invalid_item_id', __( 'Invalid ID is provided.', 'templately' ), 'import/page', 404 );
157 }
158
159 return $this->platform( $platform )->create_page( $id, $title, $this );
160 }
161 }