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.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 / GutenbergContent.php

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

183 lines 5.1 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 $processed_templates = $this->get_progress();
43
44 if(empty($processed_templates)){
45 $this->log( 0 );
46 $processed_templates = ["__started__"];
47 $this->update_progress( $processed_templates);
48 }
49
50 if(isset($this->manifest['has_settings']) && $this->manifest['has_settings'] && !in_array("global_colors", $processed_templates)){
51 $file = $this->dir_path . "settings.json";
52 $settings = Utils::read_json_file( $file );
53
54 if(!empty($data['color'])){
55 if (isset($settings['global_colors'])) {
56 foreach ($settings['global_colors'] as $key => $color) {
57 $settings['global_colors'][$key]['color'] = $data['color'][$color['var']] ?? $color['color'];
58 }
59 }
60
61 if (isset($settings['custom_colors'])) {
62 foreach ($settings['custom_colors'] as $key => $color) {
63 $settings['custom_colors'][$key]['color'] = $data['color'][$color['var']] ?? $color['color'];
64 }
65 }
66 }
67 if(!empty($data['logo']['id'])){
68 $site_logo_id = $data['logo']['id'];
69 $settings['site_logo'] = $site_logo_id;
70 Utils::update_option( 'site_logo', $site_logo_id );
71 $this->origin->update_imported_list('attachment', $data['logo']['id']);
72 }
73 else if(!empty($data['logo']) && empty(get_option('site_logo'))){
74 // demo logo
75 $site_logo = Utils::upload_logo($data['logo'], $this->session_id);
76 if(!empty($site_logo['id'])){
77 $settings['site_logo'] = $site_logo['id'];
78 Utils::update_option( 'site_logo', $site_logo['id'] );
79 $this->origin->update_imported_list('attachment', $site_logo['id']);
80 }
81 }
82
83
84 if(!empty($data['typography'])){
85 $settings["global_typography"] = $data['typography'];
86 }
87
88 $settings = array_map('json_encode', $settings);
89
90 // Save the settings to the 'eb_global_styles' option
91 Utils::update_option('eb_global_styles', $settings);
92
93 $processed_templates[] = "global_colors";
94 $this->update_progress( $processed_templates);
95 }
96
97 $processed = 0;
98 $total = array_reduce($contents, function($carry, $item) {
99 return $carry + count($item);
100 }, 0);
101
102 $results = $this->loop( $contents, function($type, $posts, $results ) use($total) {
103 return $this->loop( $posts, function($id, $settings, $result ) use($type, $total, $results) {
104 $path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR;
105
106 $import = $this->import_page_content( $id, $type, $path, $settings );
107
108 if ( ! $import ) {
109 $result[ $type ]['failed'][ $id ] = $import;
110 } else {
111 Utils::import_page_settings( $import['id'], $settings );
112 $result[ $type ]['succeed'][ $id ] = $import['id'];
113 }
114
115 // Broadcast Log
116 $processed = 0;
117 $results = Helper::recursive_wp_parse_args($result, $results);
118 array_walk_recursive($results, function($item) use (&$processed) {
119 $processed++;
120 });
121 $progress = floor( ( 100 * $processed ) / $total );
122 $this->log( $progress );
123
124 $result['__attachments'][$type][ $id ] = isset($import['__attachments']) ? $import['__attachments'] : [];
125
126 return $result;
127 }, $type);
128 });
129
130 return [ 'content' => $results ];
131 }
132
133 /**
134 * @param $id
135 * @param $type
136 * @param $path
137 * @param $settings
138 *
139 * @return false|int|void|WP_Error
140 */
141 private function import_page_content( $id, $type, $path, $settings ) {
142 try {
143 $json_content = Utils::read_json_file( $path . '/' . $type . '/' . $id . '.json' );
144 if ( ! empty( $json_content ) ) {
145
146 /**
147 * TODO:
148 *
149 * We can check if there is any data for settings.
150 * if yes: ignore content from insert.
151 *
152 * Process the content while finalizing.
153 */
154
155 $post_data = [
156 'post_title' => $json_content['title'] ?? ucfirst( $type ) . ' - (by Templately)',
157 'post_status' => 'publish',
158 'post_type' => $type,
159 'post_content' => wp_slash( $json_content['content'] ),
160 'page_template' => PageTemplates::TEMPLATE_HEADER_FOOTER
161 ];
162 $inserted = wp_insert_post( $post_data );
163
164 if ( is_wp_error( $inserted ) ) {
165 return false;
166 }
167
168 $attachments = $this->json->parse_images($json_content['content']);
169
170 $result = [];
171 if (!empty($attachments)) {
172 $result['__attachments'] = $attachments;
173 }
174 $result['id'] = $inserted;
175
176 return $result;
177 }
178 } catch ( \Exception $e ) {
179 return false;
180 }
181 }
182
183 }