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

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

222 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 use Templately\Core\Importer\Utils\Utils;
6 use Templately\Core\Importer\WPImport;
7 use Templately\Utils\Helper;
8
9 class WPContent extends BaseRunner {
10 private $selected_types = [];
11
12 /**
13 * @var int
14 */
15 private $total = 1;
16
17 private $processed = [];
18
19 public function get_name(): string {
20 return 'wp-content';
21 }
22
23 public function get_label(): string {
24 return __( 'WordPress Contents', 'templately' );
25 }
26
27 public function log_message(): string {
28 return __( 'Importing Pages, Posts, Products, Navigation, etc', 'templately' );
29 }
30
31 public function should_run( $data, $imported_data = [] ): bool {
32 return ! empty( $this->manifest['wp-content'] );
33 // return isset( $data[ 'wp-content' ] );
34 }
35
36 public function import( $data, $imported_data ): array {
37 $path = $this->dir_path . 'wp-content' . DIRECTORY_SEPARATOR;
38 $post_types = $this->filter_post_types( $data['selected_post_types'] ?? [] );
39 $taxonomies = [];
40 $terms = [];
41 $results = [
42 "wp-content" => $data["imported_data"]["wp-content"] ?? [],
43 "terms" => $data["imported_data"]["terms"] ?? [],
44 ];
45
46 $this->import_actions();
47
48 $contents = $this->manifest['wp-content'];
49 $this->total = array_reduce( $contents, function ( $carry, $item ) {
50 return $carry + count( $item );
51 }, 0 );
52
53 $processed_templates = $this->origin->get_progress();
54
55 if(empty($processed_templates)){
56 $this->log( 0 );
57 $processed_templates = ["__started__"];
58 $this->origin->update_progress( $processed_templates);
59 }
60
61 foreach ( $post_types as $type ) {
62 if (in_array($type, $processed_templates)) {
63 continue;
64 }
65
66 if(empty($data['import_demo_content']) && !in_array($type, ['wp_navigation', 'nav_menu_item'])) {
67 continue;
68 }
69 $import = $this->import_type_data( $type, $path, $imported_data, $taxonomies, $terms );
70
71 if ( empty( $import['posts'] ) ) {
72 continue;
73 }
74 $results['wp-content'][ $type ] = $import['posts'];
75 $results['terms'][ $type ] = $import['terms'];
76 $imported_data = array_merge( $imported_data, $results );
77
78 // Add the template to the processed templates and update the session data
79 $processed_templates[] = $type;
80 $this->origin->update_progress( $processed_templates, $results);
81
82 // If it's not the last item, send the SSE message and exit
83 if( end($post_types) !== $type) {
84 $this->sse_message( [
85 'type' => 'continue',
86 'action' => 'continue',
87 'results' => __METHOD__ . '::' . __LINE__,
88 ] );
89 exit;
90 }
91 }
92
93 $this->import_actions( true );
94
95 return $results;
96 }
97
98 private function filter_post_types( $selected_custom_post_types = [] ) {
99 $wp_builtin_post_types = Utils::get_builtin_wp_post_types();
100
101 foreach ( $selected_custom_post_types as $custom_post_type ) {
102 if ( post_type_exists( $custom_post_type ) ) {
103 $this->selected_types[] = $custom_post_type;
104 }
105 }
106
107 $post_types = array_merge( $wp_builtin_post_types, $this->selected_types );
108 $index = array_search( 'nav_menu_item', $post_types, true );
109 $gutenberg_index = array_search( 'wp_navigation', $post_types, true );
110
111 if ( false !== $index ) {
112 unset( $post_types[ $index ] );
113 $post_types[] = 'nav_menu_item';
114 }
115
116 if ( false !== $gutenberg_index ) {
117 unset( $post_types[ $gutenberg_index ] );
118 $post_types[] = 'wp_navigation';
119 }
120
121 return $post_types;
122 }
123
124 private function import_type_data( $type, $path, $imported_data, $taxonomies, $terms ): array {
125 $args = [
126 'fetch_attachments' => true,
127 'origin' => $this->origin,
128 'posts' => Utils::map_old_new_post_ids( $imported_data ),
129 'terms' => Utils::map_old_new_term_ids( $imported_data ),
130 'taxonomies' => ! empty( $taxonomies[ $type ] ) ? $taxonomies[ $type ] : [],
131 'posts_meta' => [
132 self::META_SESSION_KEY => $this->session_id,
133 ],
134 'terms_meta' => [
135 self::META_SESSION_KEY => $this->session_id,
136 ],
137 ];
138
139 if ( isset( $imported_data['archive_settings'] ) ) {
140 $args['posts'][ $imported_data['archive_settings']['old_id'] ] = $imported_data['archive_settings']['page_id'];
141 }
142
143 $file = $path . $type . '/' . $type . '.xml';
144
145 if ( ! file_exists( $file ) ) {
146 return [];
147 }
148
149 $wp_importer = new WPImport( $file, $args );
150 $result = $wp_importer->run();
151
152 return $result['summary'];
153 }
154
155 private function import_actions( $remove = false ) {
156 if ( ! $remove ) {
157 add_action( 'templately_import.process_post', [ $this, 'post_log' ], 10, 2 );
158 add_action( 'templately_import.process_term', [ $this, 'post_log' ], 10, 2 );
159 add_action( 'import_start', [ $this, 'update_total' ], 10 );
160
161 return;
162 }
163
164 remove_action( 'templately_import.process_post', [ $this, 'post_log' ] );
165 remove_action( 'templately_import.process_term', [ $this, 'post_log' ] );
166 remove_action( 'import_start', [ $this, 'update_total' ] );
167 }
168
169 public function post_log( $post, $result ) {
170 if ( isset( $post['post_type'] ) ) {
171 if ( ! isset( $this->manifest['wp-content'][ $post['post_type'] ] ) ) {
172 return;
173 }
174
175 $commonItems = array_intersect(array_keys($result['succeed']), $this->manifest['wp-content'][ $post['post_type'] ]);
176 $this->processed[ $post['post_type'] ] = count($commonItems);
177
178 $type = $post['post_type'];
179 $title = $post['post_title'];
180 } elseif ( isset( $post['term_id'] ) ) {
181 /**
182 * FIXME: We should fix it later, with a proper count of terms and make a total with post itself.
183 */
184 return;
185 }
186
187 if ( empty( $type ) || empty( $title ) ) {
188 return;
189 }
190
191 $failed = 0;
192 if ( ! empty( $result['failed'] ) ) {
193 $failed = count( $result['failed'] );
194 }
195
196 $processed = array_reduce( $this->processed, function ( $carry, $item ) {
197 return $carry + $item;
198 }, 0 );
199
200 $progress = $this->total > 0 ? floor( ( 100 * ( $processed + $failed ) ) / $this->total ) : 100;
201
202 $this->log( $progress);
203 }
204
205 public function update_total( $WPImport ) {
206 $contents = &$this->manifest['wp-content'];
207
208 foreach ($WPImport->posts as $key => $post) {
209 $postType = $post['post_type'];
210 $postId = $post['post_id'];
211
212 if (!isset($contents[$postType]) || !in_array($postId, $contents[$postType])) {
213 $contents[$postType][] = $postId;
214 }
215 }
216
217 $this->total = array_reduce( $contents, function ( $carry, $item ) {
218 return $carry + count( $item );
219 }, 0 );
220
221 }
222 }