| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
use function wp_parse_args; |
| 7 |
use function current_user_can; |
| 8 |
use Elementor\Core\Settings\Page\Model; |
| 9 |
|
| 10 |
use Elementor\Plugin as ElementorPlugin; |
| 11 |
use Elementor\TemplateLibrary\Source_Local as ElementorLocal; |
| 12 |
|
| 13 |
class Elementor extends ElementorLocal { |
| 14 |
/** |
| 15 |
* Get template data. |
| 16 |
* |
| 17 |
* @inheritDoc |
| 18 |
* |
| 19 |
* @param array $args Custom template arguments. |
| 20 |
* |
| 21 |
* @return array Remote Template data. |
| 22 |
*/ |
| 23 |
public function get_data( array $args ) { |
| 24 |
ElementorPlugin::$instance->editor->set_edit_mode( true ); |
| 25 |
|
| 26 |
$args['content'] = $this->replace_elements_ids( $args['content'] ); |
| 27 |
$args['content'] = $this->process_export_import_content( $args['content'], 'on_import' ); |
| 28 |
|
| 29 |
// $post_id = false; // FIXME: need to check later on. |
| 30 |
// $document = ElementorPlugin::$instance->documents->get( $post_id ); |
| 31 |
// if ( $document ) { |
| 32 |
// $args['content'] = $document->get_elements_raw_data( $args['content'], true ); |
| 33 |
// } |
| 34 |
return $args; |
| 35 |
} |
| 36 |
|
| 37 |
public function import_in_library( $data ) { |
| 38 |
if ( empty( $data ) ) { |
| 39 |
return new WP_Error( 'file_error', 'Invalid File' ); |
| 40 |
} |
| 41 |
|
| 42 |
$content = $data['content']; |
| 43 |
|
| 44 |
if ( ! is_array( $content ) ) { |
| 45 |
return new WP_Error( 'file_error', 'Invalid File' ); |
| 46 |
} |
| 47 |
|
| 48 |
$page_settings = $this->page_settings( $data ); |
| 49 |
|
| 50 |
$template_id = $this->save_item( [ |
| 51 |
'content' => $content, |
| 52 |
'title' => $data['title'], |
| 53 |
'type' => $data['type'], |
| 54 |
'page_settings' => $page_settings, |
| 55 |
] ); |
| 56 |
|
| 57 |
if ( is_wp_error( $template_id ) ) { |
| 58 |
return $template_id; |
| 59 |
} |
| 60 |
|
| 61 |
return $this->get_item( $template_id ); |
| 62 |
} |
| 63 |
|
| 64 |
public function create_page( $template_data ){ |
| 65 |
$page_settings = $this->page_settings( $template_data ); |
| 66 |
|
| 67 |
$defaults = [ |
| 68 |
'post_title' => isset( $template_data['page_title'] ) ? $template_data['page_title'] : 'Templately: ' . $template_data['title'], |
| 69 |
'page_settings' => $page_settings, |
| 70 |
'status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending', |
| 71 |
]; |
| 72 |
|
| 73 |
$template_data = wp_parse_args( $template_data, $defaults ); |
| 74 |
|
| 75 |
$document = ElementorPlugin::$instance->documents->create( |
| 76 |
$template_data['type'], |
| 77 |
[ |
| 78 |
'post_title' => $template_data['post_title'], |
| 79 |
'post_status' => $template_data['status'], |
| 80 |
'post_type' => 'page', |
| 81 |
] |
| 82 |
); |
| 83 |
|
| 84 |
if ( is_wp_error( $document ) ) { |
| 85 |
/** |
| 86 |
* @var WP_Error $document |
| 87 |
*/ |
| 88 |
return $document; |
| 89 |
} |
| 90 |
|
| 91 |
$document->save( [ |
| 92 |
'elements' => $template_data['content'], |
| 93 |
'settings' => $page_settings, |
| 94 |
] ); |
| 95 |
|
| 96 |
return $document->get_main_id(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @param $template_data |
| 101 |
* @noinspection DuplicatedCode |
| 102 |
* |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
private function page_settings( $template_data ) { |
| 106 |
$page_settings = []; |
| 107 |
|
| 108 |
if ( ! empty( $template_data['page_settings'] ) ) { |
| 109 |
$page = new Model( [ |
| 110 |
'id' => 0, |
| 111 |
'settings' => $template_data['page_settings'], |
| 112 |
] ); |
| 113 |
|
| 114 |
$page_settings_data = $this->process_element_export_import_content( $page, 'on_import' ); |
| 115 |
|
| 116 |
if ( ! empty( $page_settings_data['settings'] ) ) { |
| 117 |
$page_settings = $page_settings_data['settings']; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return $page_settings; |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
|