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

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

220 lines 6.3 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
6 use Exception;
7 use Templately\Core\Importer\Utils\Utils;
8 use Templately\Utils\Helper;
9
10 class Finalizer extends BaseRunner {
11 private $options = [];
12 private $type_to_check = [ 'templates', 'content' ];
13 private $imported_data;
14
15 private $type = '';
16 private $sub_type = '';
17 private $extra_content;
18
19 private $total_counts = 0;
20
21 /**
22 * @var array|mixed
23 */
24 protected $map_post_ids = [];
25 /**
26 * @var array|mixed
27 */
28 protected $map_term_ids = [];
29
30 public function get_name(): string {
31 return 'finalize';
32 }
33
34 public function get_label(): string {
35 return __( 'Finalizing Your Imports', 'templately' );
36 }
37
38 public function log_message(): string {
39 return __( 'Finalizing Your Imports', 'templately' );
40 }
41
42 public function should_run( $data, $imported_data = [] ): bool {
43 $data = [];
44
45 foreach ( $this->type_to_check as $type ) {
46 $contents = ! empty ( $this->manifest[ $type ] ) ? $this->manifest[ $type ] : [];
47 if ( $type == 'templates' ) {
48 $this->prepare( $data, $contents, $type );
49 } else {
50 foreach ( $contents as $post_type => $templates ) {
51 $this->prepare( $data, $templates, $type, $post_type );
52 }
53 }
54 }
55 $this->options = &$data;
56
57 return ! empty( $data ) || $this->platform == 'gutenberg';
58 }
59
60 private function prepare( &$data, $templates, $type, $sub_type = null ) {
61 if ( empty( $templates ) || ! is_array( $templates ) ) {
62 return;
63 }
64 foreach ( $templates as $id => $template ) {
65 if ( ! isset( $template['data'] ) && !isset( $template['__attachments']) && !isset($template['has_logo']) ) {
66 continue;
67 }
68
69 // if ( ! isset( $template['data']['form'] ) && ! isset( $template['data']['nav_menus'] )) {
70 // continue;
71 // }
72
73 $this->total_counts += 1;
74
75 if ( $sub_type ) {
76 $data[ $type ][ $sub_type ][ $id ] = $template;
77 } else {
78 $data[ $type ][ $id ] = $template;
79 }
80 }
81 }
82
83 public function import( $data, $imported_data ): array {
84 $this->imported_data = &$imported_data;
85
86 $this->json->imported_data = $this->imported_data;
87 $this->json->map_post_ids = Utils::map_old_new_post_ids( $this->imported_data );
88 $this->json->map_term_ids = Utils::map_old_new_term_ids( $this->imported_data );
89 if ( ! empty( $imported_data['extra-content'] ) ) {
90 $this->extra_content = $imported_data['extra-content'];
91 }
92
93 add_action('templately_import.finalize_gutenberg_attachment', [$this, 'post_log'], 10, 2);
94
95 // Get the processed templates from the session data
96 $processed = $this->origin->get_progress();
97
98 if(empty($processed)){
99 $this->log( 0 );
100 $processed = ["__started__"];
101 $this->origin->update_progress( $processed);
102 }
103
104 foreach ( $this->options as $type => $contents ) {
105 $this->type = $type;
106
107 // If the template has been processed, skip it
108 if (in_array($type, $processed)) {
109 continue;
110 }
111
112 if ( $type == 'templates' ) {
113 $this->finalize_imports( $contents, $type );
114
115 $processed[] = $type;
116 $this->origin->update_progress( $processed);
117 // If it's not the last item, send the SSE message and exit
118 if( end($this->options) !== $contents ) {
119 $this->sse_message( [
120 'type' => 'continue',
121 'action' => 'continue',
122 'results' => __METHOD__ . '::' . __LINE__,
123 ] );
124 exit;
125 }
126 } else {
127 foreach ( $contents as $post_type => $templates ) {
128 // If the template has been processed, skip it
129 if (in_array("$type::$post_type", $processed)) {
130 continue;
131 }
132
133 $this->sub_type = $post_type;
134 $this->finalize_imports( $templates, $type, $post_type );
135
136 $processed[] = "$type::$post_type";
137 $this->origin->update_progress( $processed);
138 // If it's not the last item, send the SSE message and exit
139 if( end($contents) !== $templates ) {
140 $this->sse_message( [
141 'type' => 'continue',
142 'action' => 'continue',
143 'results' => __METHOD__ . '::' . __LINE__,
144 ] );
145 exit;
146 }
147 }
148 }
149 }
150
151 if ( $this->platform == 'gutenberg' ) {
152 $this->regenerate_assets();
153 }
154
155 return [];
156 }
157
158 private function regenerate_assets() {
159 $upload_dir = wp_upload_dir();
160 if ( is_dir( $upload_dir['basedir'] . '/eb-style/' ) ) {
161 array_map( 'unlink', glob( $upload_dir['basedir'] . '/eb-style/*.min.css' ) );
162 rmdir( $upload_dir['basedir'] . '/eb-style/' );
163 }
164 }
165
166 private function finalize_imports( $templates, $type, $post_type = null ) {
167 // Get the processed templates from the session data
168 $processed = $this->origin->get_progress();
169
170 foreach ( $templates as $old_template_id => $template_settings ) {
171 // If the template has been processed, skip it
172 if (in_array($old_template_id, $processed)) {
173 continue;
174 }
175
176 try {
177 $path = $this->dir_path . $this->type . DIRECTORY_SEPARATOR;
178 if ( ! empty( $this->sub_type ) ) {
179 $path .= $this->sub_type . DIRECTORY_SEPARATOR;
180 }
181
182 if($post_type && isset($this->imported_data[$type]['__attachments'][$post_type][$old_template_id])){
183 $template_settings['__attachments'] = $this->imported_data[$type]['__attachments'][$post_type][$old_template_id];
184 }
185 else if(empty($post_type) && isset($this->imported_data[$type]['__attachments'][$old_template_id])){
186 $template_settings['__attachments'] = $this->imported_data[$type]['__attachments'][$old_template_id];
187 }
188
189 $path .= "{$old_template_id}.json";
190 $template_json = Utils::read_json_file( $path );
191 $params = $this->origin->get_request_params();
192 $this->json->prepare( $template_json, $template_settings, $this->extra_content['form'][ $old_template_id ] ?? [], $params )->update();
193
194 // Broadcast Log
195 $progress = floor( ( 100 * count($processed) ) / $this->total_counts );
196 $this->log( $progress );
197 } catch ( Exception $e ) {
198 continue;
199 }
200
201 // Add the template to the processed templates and update the session data
202 $processed[] = $old_template_id;
203 $this->origin->update_progress( $processed);
204
205 // If it's not the last item, send the SSE message and exit
206 if( end($templates) !== $template_settings) {
207 $this->sse_message( [
208 'type' => 'continue',
209 'action' => 'continue',
210 'results' => __METHOD__ . '::' . __LINE__,
211 ] );
212 exit;
213 }
214 }
215 }
216
217 public function post_log($id, $size_dimension = null){
218 $this->log(-1, "Imported attachment: $id" . ( $size_dimension ? " - $size_dimension" : ''), 'eventLog');
219 }
220 }