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

188 lines 5.2 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_el_active_kit")){
61 add_option("__templately_el_active_kit", $active_kit, '', 'no');
62 }
63
64 $file = $this->dir_path . "settings.json";
65 $settings = Utils::read_json_file( $file );
66
67 if(!empty($data['color'])){
68 if (!empty($settings['system_colors'])) {
69 foreach ($settings['system_colors'] as $key => $color) {
70 $settings['system_colors'][$key]['color'] = $data['color'][$color['_id']] ?? $color['color'];
71 }
72 }
73 if (!empty($settings['custom_colors'])) {
74 foreach ($settings['custom_colors'] as $key => $color) {
75 $settings['custom_colors'][$key]['color'] = $data['color'][$color['_id']] ?? $color['color'];
76 }
77 }
78 }
79
80 if (!empty($data['logo']['id'])) {
81 $settings['site_logo'] = $data['logo'];
82 } elseif (!empty($data['logo'])) {
83 // If there's no old logo id, try to upload a new logo
84 if (empty($old_logo['id'])) {
85 $site_logo = Utils::upload_logo($data['logo']);
86
87 // If the upload was successful, use the new logo, otherwise use the old one
88 $settings['site_logo'] = !empty($site_logo['id']) ? $site_logo : $old_logo;
89 } else {
90 // If there's an old logo id, use the old logo
91 $settings['site_logo'] = $old_logo;
92 }
93 }
94
95
96 $kit_id = $kits_manager->create_new_kit( $this->manifest['name'], $settings, true );
97
98 $kit = $kits_manager->get_kit( $kit_id );
99
100 // $kit->update_settings( ['site_logo' => $settings['site_logo']] );
101
102 // Create an array with the post ID and the new title
103 $post_data = array(
104 'ID' => $kit_id,
105 'post_title' => $this->manifest['name'] . " Kit",
106 );
107 // Update the post
108 wp_update_post( $post_data );
109
110 }
111
112 $active_kit = $kits_manager->get_active_id();
113 $kit = $kits_manager->get_kit( $active_kit );
114
115 if ( ! $kit->get_id() ) {
116 $kit = $kits_manager->create_default();
117 update_option( $kits_manager::OPTION_ACTIVE, $kit );
118 }
119
120 $processed = 0;
121 $total = array_reduce($contents, function($carry, $item) {
122 return $carry + count($item);
123 }, 0);
124
125 foreach ( $contents as $post_type => $post ) {
126 if ( ! post_type_exists( $post_type ) ) {
127 continue;
128 }
129
130 foreach ( $post as $id => $content_settings ) {
131 $import = $this->import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings );
132
133 if ( ! $import ) {
134 $results[ $post_type ]['failed'][ $id ] = $import;
135 } else {
136 Utils::import_page_settings( $import, $content_settings );
137 $results[ $post_type ]['succeed'][ $id ] = $import;
138 }
139
140 // Broadcast Log
141 $processed += 1;
142 $progress = floor( ( 100 * $processed ) / $total );
143 $this->log( $progress, null, 'eventLog' );
144 }
145 }
146
147 return [ 'content' => $results ];
148 }
149
150 /**
151 * @throws Exception
152 */
153 private function import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings ) {
154 try {
155 $template = $this->factory->create( $content_settings['doc_type'], [
156 'post_title' => $content_settings['title'],
157 'post_status' => 'publish',
158 'post_type' => $post_type,
159 ] );
160
161 $file = $path . $post_type . DIRECTORY_SEPARATOR . "{$id}.json";
162 $post_data = Utils::read_json_file( $file );
163
164 if ( ! empty( $content_settings['data'] ) ) {
165 /**
166 * TODO:
167 *
168 * We can check if there is any data for settings.
169 * if yes: ignore content from insert.
170 *
171 * Process the content while finalizing.
172 */
173 // $this->json->prepare( $post_data['content'], $id, $content_settings['data'], $imported_data );
174
175 $post_data['content'] = [];
176 }
177
178 unset($content_settings['conditions']);
179 $post_data['import_settings'] = $content_settings;
180
181 $template->import( $post_data );
182
183 return $template->get_main_id();
184 } catch ( Exception $e ) {
185 return false;
186 }
187 }
188 }