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