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

148 lines 4.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
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 private $processed_items = 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 $this->log( 0 );
94 add_action('templately_import.finalize_gutenberg_attachment', [$this, 'post_log'], 10, 2);
95
96 foreach ( $this->options as $type => $contents ) {
97 $this->type = $type;
98 if ( $type == 'templates' ) {
99 $this->finalize_imports( $contents );
100 } else {
101 foreach ( $contents as $post_type => $templates ) {
102 $this->sub_type = $post_type;
103 $this->finalize_imports( $templates );
104 }
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 ) {
124 foreach ( $templates as $old_template_id => $template_settings ) {
125 try {
126 $path = $this->dir_path . $this->type . DIRECTORY_SEPARATOR;
127 if ( ! empty( $this->sub_type ) ) {
128 $path .= $this->sub_type . DIRECTORY_SEPARATOR;
129 }
130 $path .= "{$old_template_id}.json";
131 $template_json = Utils::read_json_file( $path );
132 $params = $this->origin->get_request_params();
133 $this->json->prepare( $template_json, $template_settings, $this->extra_content['form'][ $old_template_id ] ?? [], $params )->update();
134
135 // Broadcast Log
136 $this->processed_items += 1;
137 $progress = floor( ( 100 * $this->processed_items ) / $this->total_counts );
138 $this->log( $progress );
139 } catch ( Exception $e ) {
140 continue;
141 }
142 }
143 }
144
145 public function post_log($id, $size_dimension = null){
146 $this->log(-1, "Imported attachment: $id" . ( $size_dimension ? " - $size_dimension" : ''), 'eventLog');
147 }
148 }