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

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