PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 2.2.1
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v2.2.1
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 / Core / Importer / Elementor.php

Elementor.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 2.2.1, at includes/Core/Importer/Elementor.php

126 lines 3.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\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