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

167 lines 4.9 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 $this->loop( $this->options, function($type, $contents ) {
96 $this->type = $type;
97
98 if ( $type == 'templates' ) {
99 $this->finalize_imports( $contents, $type );
100 } else {
101 $this->loop( $contents, function($post_type, $templates ) use($type) {
102 $this->sub_type = $post_type;
103 $this->finalize_imports( $templates, $type, $post_type );
104 }, $type);
105 }
106 });
107
108 if ( $this->platform == 'gutenberg' ) {
109 $this->regenerate_assets();
110 }
111
112 return [];
113 }
114
115 private function regenerate_assets() {
116 $upload_dir = wp_upload_dir();
117 if ( is_dir( $upload_dir['basedir'] . '/eb-style/' ) ) {
118 array_map( 'unlink', glob( $upload_dir['basedir'] . '/eb-style/*.min.css' ) );
119 rmdir( $upload_dir['basedir'] . '/eb-style/' );
120 }
121 }
122
123 private function finalize_imports( $templates, $type, $post_type = null ) {
124 // used for counting
125
126 $this->loop( $templates, function($old_template_id, $template_settings ) use($type, $post_type) {
127 $processed = $this->get_progress([], 'finalized_imports', false);
128 try {
129 $path = $this->dir_path . $this->type . DIRECTORY_SEPARATOR;
130 if ( ! empty( $this->sub_type ) ) {
131 $path .= $this->sub_type . DIRECTORY_SEPARATOR;
132 }
133
134 if($post_type && isset($this->imported_data[$type]['__attachments'][$post_type][$old_template_id])){
135 $template_settings['__attachments'] = $this->imported_data[$type]['__attachments'][$post_type][$old_template_id];
136 }
137 else if(empty($post_type) && isset($this->imported_data[$type]['__attachments'][$old_template_id])){
138 $template_settings['__attachments'] = $this->imported_data[$type]['__attachments'][$old_template_id];
139 }
140
141 $path .= "{$old_template_id}.json";
142 $template_json = Utils::read_json_file( $path );
143 $params = $this->origin->get_request_params();
144 $this->json->prepare( $template_json, $template_settings, $this->extra_content['form'][ $old_template_id ] ?? [], $params )->update();
145
146 $processed[] = $old_template_id;
147 // Add the template to the processed templates and update the session data
148 $this->update_progress( $processed, null, 'finalized_imports', false);
149 // Broadcast Log
150 $progress = floor( ( 100 * count($processed) ) / $this->total_counts );
151 if(empty($progress)){
152 $xyz = 0;
153 }
154 $this->log( $progress );
155
156 } catch ( Exception $e ) {
157
158 }
159
160
161 }, "$type-$post_type");
162 }
163
164 public function post_log($id, $size_dimension = null){
165 $this->log(-1, "Imported attachment: $id" . ( $size_dimension ? " - $size_dimension" : ''), 'eventLog');
166 }
167 }