PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.11
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.11
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.11, at includes/Core/Importer/Runners/ElementorContent.php

230 lines 6.8 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 = $data["imported_data"]["content"] ?? [];
39 $contents = $this->manifest['content'];
40 $path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR;
41 $processed_templates = $this->origin->get_progress();
42
43 // $total = array_reduce( $contents, function ( $carry, $item ) {
44 // return $carry + count( $item );
45 // }, 0 );
46 // $processed = 0;
47
48 /**
49 * Check if there is any active kit?
50 * If not, create one.
51 */
52
53 $kits_manager = Plugin::$instance->kits_manager;
54
55 $active_kit = $kits_manager->get_active_id();
56 $kit = $kits_manager->get_kit( $active_kit );
57 $old_logo = $kit->get_settings('site_logo');
58
59 if(empty($processed_templates)){
60 $this->log( 0 );
61 $processed_templates = ["__started__"];
62 $this->origin->update_progress( $processed_templates);
63 }
64
65 if(isset($this->manifest['has_settings']) && $this->manifest['has_settings'] && !in_array("global_colors", $processed_templates)){
66 // backing up the active kit id before updating the new one
67 if(!get_option("__templately_" . $kits_manager::OPTION_ACTIVE)){
68 add_option("__templately_" . $kits_manager::OPTION_ACTIVE, $active_kit, '', 'no');
69 }
70 else{
71 update_option("__templately_" . $kits_manager::OPTION_ACTIVE, $active_kit, 'no');
72 }
73
74 $file = $this->dir_path . "settings.json";
75 $settings = Utils::read_json_file( $file );
76
77 if(isset($settings['site_name'])){
78 unset($settings['site_name']);
79 }
80 if(isset($settings['site_description'])){
81 unset($settings['site_description']);
82 }
83 if(!empty($data['color'])){
84 if (!empty($settings['system_colors'])) {
85 foreach ($settings['system_colors'] as $key => $color) {
86 $settings['system_colors'][$key]['color'] = $data['color'][$color['_id']] ?? $color['color'];
87 }
88 }
89 if (!empty($settings['custom_colors'])) {
90 foreach ($settings['custom_colors'] as $key => $color) {
91 $settings['custom_colors'][$key]['color'] = $data['color'][$color['_id']] ?? $color['color'];
92 }
93 }
94 }
95
96 if (!empty($data['logo']['id'])) {
97 $settings['site_logo'] = $data['logo'];
98 Utils::backup_option_value( 'site_logo' );
99 $this->origin->update_imported_list('attachment', $data['logo']['id']);
100 } elseif (!empty($data['logo'])) {
101 $settings['site_logo'] = $old_logo;
102
103 // If there's no old logo id, try to upload a new logo
104 if (empty($old_logo['id'])) {
105 $site_logo = Utils::upload_logo($data['logo']);
106
107 // If the upload was successful, use the new logo, otherwise use the old one
108 if(!empty($site_logo['id'])){
109 $settings['site_logo'] = $site_logo;
110 Utils::backup_option_value( 'site_logo' );
111 $this->origin->update_imported_list('attachment', $site_logo['id']);
112 }
113 }
114 }
115
116
117 $kit_id = $kits_manager->create_new_kit( $this->manifest['name'], $settings, true );
118
119 $kit = $kits_manager->get_kit( $kit_id );
120
121 // $kit->update_settings( ['site_logo' => $settings['site_logo']] );
122
123 // Create an array with the post ID and the new title
124 $post_data = array(
125 'ID' => $kit_id,
126 'post_title' => $this->manifest['name'] . " Kit",
127 );
128 // Update the post
129 wp_update_post( $post_data );
130
131 $processed_templates[] = "global_colors";
132 $this->origin->update_progress( $processed_templates, [ 'content' => $results ]);
133 }
134
135 $active_kit = $kits_manager->get_active_id();
136 $kit = $kits_manager->get_kit( $active_kit );
137
138 if ( ! $kit->get_id() ) {
139 $kit = $kits_manager->create_default();
140 update_option( $kits_manager::OPTION_ACTIVE, $kit );
141 }
142
143 // $processed = 0;
144 $total = array_reduce($contents, function($carry, $item) {
145 return $carry + count($item);
146 }, 0);
147
148 foreach ( $contents as $post_type => $post ) {
149 foreach ( $post as $id => $content_settings ) {
150 if (in_array("$post_type::$id", $processed_templates)) {
151 continue;
152 }
153 if ( post_type_exists( $post_type ) ) {
154
155 $import = $this->import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings );
156
157 if ( ! $import ) {
158 $results[ $post_type ]['failed'][ $id ] = $import;
159 } else {
160 Utils::import_page_settings( $import, $content_settings );
161 $results[ $post_type ]['succeed'][ $id ] = $import;
162 }
163
164 // Broadcast Log
165 $processed = 0;
166 array_walk_recursive($results, function($item) use (&$processed) {
167 $processed++;
168 });
169 $progress = floor( ( 100 * $processed ) / $total );
170 $this->log( $progress, null, 'eventLog' );
171 }
172
173 // Add the template to the processed templates and update the session data
174 $processed_templates[] = "$post_type::$id";
175 $this->origin->update_progress( $processed_templates, [ 'content' => $results ]);
176
177 // If it's not the last item, send the SSE message and exit
178 if( end($contents) !== $post || end($post) !== $content_settings) {
179 $this->sse_message( [
180 'type' => 'continue',
181 'action' => 'continue',
182 'results' => __METHOD__ . '::' . __LINE__,
183 ] );
184 exit;
185 }
186 }
187 }
188
189 return [ 'content' => $results ];
190 }
191
192 /**
193 * @throws Exception
194 */
195 private function import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings ) {
196 try {
197 $template = $this->factory->create( $content_settings['doc_type'], [
198 'post_title' => $content_settings['title'],
199 'post_status' => 'publish',
200 'post_type' => $post_type,
201 ] );
202
203 $file = $path . $post_type . DIRECTORY_SEPARATOR . "{$id}.json";
204 $post_data = Utils::read_json_file( $file );
205
206 if ( ! empty( $content_settings['data'] ) ) {
207 /**
208 * TODO:
209 *
210 * We can check if there is any data for settings.
211 * if yes: ignore content from insert.
212 *
213 * Process the content while finalizing.
214 */
215 // $this->json->prepare( $post_data['content'], $id, $content_settings['data'], $imported_data );
216
217 $post_data['content'] = [];
218 }
219
220 unset($content_settings['conditions']);
221 $post_data['import_settings'] = $content_settings;
222
223 $template->import( $post_data );
224
225 return $template->get_main_id();
226 } catch ( Exception $e ) {
227 return false;
228 }
229 }
230 }