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

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

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