PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.3
3.8.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 All 112 releases
templately / includes / Core / Importer / Runners / ElementorContent.php

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

194 lines 5.4 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\Runners;
4
5 use Elementor\Plugin;
6 use Exception;
7 use Templately\Core\Importer\Utils\Utils;
8
9 class ElementorContent extends BaseRunner {
10 public function get_name(): string {
11 return 'content';
12 }
13
14 public function get_label(): string {
15 return __( 'Elementor', 'templately' );
16 }
17
18 public function should_run( $data, $imported_data = [] ): bool {
19 return $this->manifest['platform'] === 'elementor' && ! empty( $this->manifest['content'] );
20 }
21
22 public function should_log(): bool {
23 return true;
24 }
25
26 public function get_action(): string {
27 return 'eventLog';
28 }
29
30 public function log_message(): string {
31 return __( 'Importing Elementor Templates (Pages, Posts etc)', 'templately' );
32 }
33
34 /**
35 * @throws Exception
36 */
37 public function import( $data, $imported_data ): array {
38 $results = [];
39 $contents = $this->manifest['content'];
40 $path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR;
41
42 // $total = array_reduce( $contents, function ( $carry, $item ) {
43 // return $carry + count( $item );
44 // }, 0 );
45 // $processed = 0;
46
47 /**
48 * Check if there is any active kit?
49 * If not, create one.
50 */
51
52 $kits_manager = Plugin::$instance->kits_manager;
53
54 $active_kit = $kits_manager->get_active_id();
55 $kit = $kits_manager->get_kit( $active_kit );
56 $old_logo = $kit->get_settings('site_logo');
57
58 if(isset($this->manifest['has_settings']) && $this->manifest['has_settings']){
59 // backing up the active kit id before updating the new one
60 if(!get_option("__templately_" . $kits_manager::OPTION_ACTIVE)){
61 add_option("__templately_" . $kits_manager::OPTION_ACTIVE, $active_kit, '', 'no');
62 }
63 else{
64 update_option("__templately_" . $kits_manager::OPTION_ACTIVE, $active_kit, 'no');
65 }
66
67 $file = $this->dir_path . "settings.json";
68 $settings = Utils::read_json_file( $file );
69
70 if(!empty($data['color'])){
71 if (!empty($settings['system_colors'])) {
72 foreach ($settings['system_colors'] as $key => $color) {
73 $settings['system_colors'][$key]['color'] = $data['color'][$color['_id']] ?? $color['color'];
74 }
75 }
76 if (!empty($settings['custom_colors'])) {
77 foreach ($settings['custom_colors'] as $key => $color) {
78 $settings['custom_colors'][$key]['color'] = $data['color'][$color['_id']] ?? $color['color'];
79 }
80 }
81 }
82
83 if (!empty($data['logo']['id'])) {
84 $settings['site_logo'] = $data['logo'];
85 $this->origin->update_imported_list('attachment', $data['logo']['id']);
86 } elseif (!empty($data['logo'])) {
87 $settings['site_logo'] = $old_logo;
88
89 // If there's no old logo id, try to upload a new logo
90 if (empty($old_logo['id'])) {
91 $site_logo = Utils::upload_logo($data['logo']);
92
93 // If the upload was successful, use the new logo, otherwise use the old one
94 if(!empty($site_logo['id'])){
95 $settings['site_logo'] = $site_logo;
96 $this->origin->update_imported_list('attachment', $site_logo['id']);
97 }
98 }
99 }
100
101
102 $kit_id = $kits_manager->create_new_kit( $this->manifest['name'], $settings, true );
103
104 $kit = $kits_manager->get_kit( $kit_id );
105
106 // $kit->update_settings( ['site_logo' => $settings['site_logo']] );
107
108 // Create an array with the post ID and the new title
109 $post_data = array(
110 'ID' => $kit_id,
111 'post_title' => $this->manifest['name'] . " Kit",
112 );
113 // Update the post
114 wp_update_post( $post_data );
115
116 }
117
118 $active_kit = $kits_manager->get_active_id();
119 $kit = $kits_manager->get_kit( $active_kit );
120
121 if ( ! $kit->get_id() ) {
122 $kit = $kits_manager->create_default();
123 update_option( $kits_manager::OPTION_ACTIVE, $kit );
124 }
125
126 $processed = 0;
127 $total = array_reduce($contents, function($carry, $item) {
128 return $carry + count($item);
129 }, 0);
130
131 foreach ( $contents as $post_type => $post ) {
132 if ( ! post_type_exists( $post_type ) ) {
133 continue;
134 }
135
136 foreach ( $post as $id => $content_settings ) {
137 $import = $this->import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings );
138
139 if ( ! $import ) {
140 $results[ $post_type ]['failed'][ $id ] = $import;
141 } else {
142 Utils::import_page_settings( $import, $content_settings );
143 $results[ $post_type ]['succeed'][ $id ] = $import;
144 }
145
146 // Broadcast Log
147 $processed += 1;
148 $progress = floor( ( 100 * $processed ) / $total );
149 $this->log( $progress, null, 'eventLog' );
150 }
151 }
152
153 return [ 'content' => $results ];
154 }
155
156 /**
157 * @throws Exception
158 */
159 private function import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings ) {
160 try {
161 $template = $this->factory->create( $content_settings['doc_type'], [
162 'post_title' => $content_settings['title'],
163 'post_status' => 'publish',
164 'post_type' => $post_type,
165 ] );
166
167 $file = $path . $post_type . DIRECTORY_SEPARATOR . "{$id}.json";
168 $post_data = Utils::read_json_file( $file );
169
170 if ( ! empty( $content_settings['data'] ) ) {
171 /**
172 * TODO:
173 *
174 * We can check if there is any data for settings.
175 * if yes: ignore content from insert.
176 *
177 * Process the content while finalizing.
178 */
179 // $this->json->prepare( $post_data['content'], $id, $content_settings['data'], $imported_data );
180
181 $post_data['content'] = [];
182 }
183
184 unset($content_settings['conditions']);
185 $post_data['import_settings'] = $content_settings;
186
187 $template->import( $post_data );
188
189 return $template->get_main_id();
190 } catch ( Exception $e ) {
191 return false;
192 }
193 }
194 }