PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.6.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.6.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 / GutenbergContent.php

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

197 lines 5.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 Templately\Builder\PageTemplates;
6 use Templately\Core\Importer\Utils\Utils;
7 use Templately\Utils\Helper;
8 use WP_Error;
9
10 /**
11 * @property GutenbergHelper $json
12 */
13 class GutenbergContent extends BaseRunner {
14
15 public function get_name(): string {
16 return 'content';
17 }
18
19 public function get_label(): string {
20 return __( 'Block Editor Content', 'templately' );
21 }
22
23 public function should_run( $data, $imported_data = [] ): bool {
24 return $this->platform == 'gutenberg' && ! empty( $this->manifest['content'] );
25 }
26
27 public function should_log(): bool {
28 return true;
29 }
30
31 public function get_action(): string {
32 return 'updateLog';
33 }
34
35 public function log_message(): string {
36 return __( 'Importing Gutenberg Page and Post Templates', 'templately' );
37 }
38
39 public function import( $data, $imported_data ): array {
40 $contents = $this->manifest['content'];
41
42 // Log start of import
43 if(!$this->is_key_processed('started', 'import')){
44 $this->log( 0 );
45 $this->mark_key_processed('started', 'import');
46 }
47
48 if(isset($this->manifest['has_settings']) && $this->manifest['has_settings'] && !$this->is_key_processed('global_colors', 'settings')){
49 $file = $this->dir_path . "settings.json";
50 $settings = Utils::read_json_file( $file );
51
52 if(!empty($data['color'])){
53 if (isset($settings['global_colors'])) {
54 foreach ($settings['global_colors'] as $key => $color) {
55 $settings['global_colors'][$key]['color'] = $data['color'][$color['var']] ?? $color['color'];
56 }
57 }
58
59 if (isset($settings['custom_colors'])) {
60 foreach ($settings['custom_colors'] as $key => $color) {
61 $settings['custom_colors'][$key]['color'] = $data['color'][$color['var']] ?? $color['color'];
62 }
63 }
64 }
65 if(!empty($data['logo']['id'])){
66 $site_logo_id = $data['logo']['id'];
67 $settings['site_logo'] = $site_logo_id;
68 Utils::update_option( 'site_logo', $site_logo_id );
69 $this->origin->update_imported_list('attachment', $data['logo']['id']);
70 }
71 else if (!empty($data['logo']['url']) && strpos($data['logo']['url'], 'data:image/') === 0) {
72 // Upload base64 data URL
73 $site_logo = Utils::upload_logo_base64($data['logo']['url'], $this->session_id);
74
75 if(!empty($site_logo['id'])){
76 $settings['site_logo'] = $site_logo['id'];
77 Utils::update_option( 'site_logo', $site_logo['id'] );
78 $this->origin->update_imported_list('attachment', $site_logo['id']);
79 }
80 }
81 else if(!empty($data['logo']) && empty(get_option('site_logo'))){
82 // demo logo
83 $site_logo = Utils::upload_logo($data['logo'], $this->session_id);
84 if(!empty($site_logo['id'])){
85 $settings['site_logo'] = $site_logo['id'];
86 Utils::update_option( 'site_logo', $site_logo['id'] );
87 $this->origin->update_imported_list('attachment', $site_logo['id']);
88 }
89 }
90
91
92 if(!empty($data['typography'])){
93 $settings["global_typography"] = $data['typography'];
94 }
95
96 $settings = array_map('json_encode', $settings);
97
98 // Save the settings to the 'eb_global_styles' option
99 Utils::update_option('eb_global_styles', $settings);
100
101 $this->mark_key_processed('global_colors', 'settings');
102 }
103
104 $processed = 0;
105 $total = array_reduce($contents, function($carry, $item) {
106 return $carry + count($item);
107 }, 0);
108
109 $results = $this->loop( $contents, function($type, $posts, $results ) use($total) {
110 // Inner loop accumulates results for this type
111 $inner_result = $this->loop( $posts, function($id, $settings, $result ) use($type, $results, $total) {
112 $path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR;
113
114 $import = $this->import_page_content( $id, $type, $path, $settings );
115
116 if ( ! $import ) {
117 $result[ $type ]['failed'][ $id ] = $import;
118 } else {
119 Utils::import_page_settings( $import['id'], $settings );
120 $result[ $type ]['succeed'][ $id ] = $import['id'];
121 }
122
123 // Broadcast Log - merge with outer results for accurate counting
124 $merged = array_merge($results, $result);
125 $processed = 0;
126 foreach ($merged as $t => $data) {
127 if (is_array($data) && $t !== '__attachments') {
128 $processed += count($data['succeed'] ?? []) + count($data['failed'] ?? []);
129 }
130 }
131 $progress = $total > 0 ? floor( ( 100 * $processed ) / $total ) : 100;
132 $this->log( $progress );
133
134 $result['__attachments'][$type][ $id ] = isset($import['__attachments']) ? $import['__attachments'] : [];
135
136 return $result;
137 }, $type);
138
139 // Non-recursive merge of inner result with outer results
140 $results = array_merge($results, $inner_result);
141 return $results;
142 });
143
144 return [ 'content' => $results ];
145 }
146
147 /**
148 * @param $id
149 * @param $type
150 * @param $path
151 * @param $settings
152 *
153 * @return false|int|void|WP_Error
154 */
155 private function import_page_content( $id, $type, $path, $settings ) {
156 try {
157 $json_content = Utils::read_json_file( $path . '/' . $type . '/' . $id . '.json' );
158 if ( ! empty( $json_content ) ) {
159
160 /**
161 * TODO:
162 *
163 * We can check if there is any data for settings.
164 * if yes: ignore content from insert.
165 *
166 * Process the content while finalizing.
167 */
168
169 $post_data = [
170 'post_title' => $json_content['title'] ?? ucfirst( $type ) . ' - (by Templately)',
171 'post_status' => 'publish',
172 'post_type' => $type,
173 'post_content' => wp_slash( $json_content['content'] ),
174 'page_template' => PageTemplates::TEMPLATE_HEADER_FOOTER
175 ];
176 $inserted = wp_insert_post( $post_data );
177
178 if ( is_wp_error( $inserted ) ) {
179 return false;
180 }
181
182 $attachments = $this->json->parse_images($json_content['content']);
183
184 $result = [];
185 if (!empty($attachments)) {
186 $result['__attachments'] = $attachments;
187 }
188 $result['id'] = $inserted;
189
190 return $result;
191 }
192 } catch ( \Exception $e ) {
193 return false;
194 }
195 }
196
197 }