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

235 lines 7.0 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 = $imported_data['wp-content_manifest_content'] ?? $this->manifest['wp-content'];
49 $this->processed = $imported_data['wp-content_processed'] ?? [];
50 $this->total = array_reduce( $contents, function ( $carry, $item ) {
51 return $carry + count( $item );
52 }, 0 );
53
54 $processed_templates = $this->origin->get_progress();
55
56 if(empty($processed_templates)){
57 $this->log( 0 );
58 $processed_templates = ["__started__"];
59 $this->origin->update_progress( $processed_templates, ['wp-content_manifest_content' => $contents]);
60 }
61
62 foreach ( $post_types as $type ) {
63 if (in_array($type, $processed_templates)) {
64 continue;
65 }
66
67 if(empty($data['import_demo_content']) && !in_array($type, ['wp_navigation', 'nav_menu_item'])) {
68 continue;
69 }
70 $import = $this->import_type_data( $type, $path, $imported_data, $taxonomies, $terms );
71
72 if ( empty( $import['posts'] ) ) {
73 continue;
74 }
75 if(isset($import['posts']['__attachments'])){
76 $results['wp-content']['__attachments'][ $type ] = $import['posts']['__attachments'];
77 unset($import['posts']['__attachments']);
78 }
79 $results['wp-content'][ $type ] = $import['posts'];
80 $results['terms'][ $type ] = $import['terms'];
81 $imported_data = array_merge( $imported_data, $results );
82
83 // Add the template to the processed templates and update the session data
84 $processed_templates[] = $type;
85 $this->origin->update_progress( $processed_templates, $results);
86
87 // If it's not the last item, send the SSE message and exit
88 if( end($post_types) !== $type) {
89 $this->sse_message( [
90 'type' => 'continue',
91 'action' => 'continue',
92 'results' => __METHOD__ . '::' . __LINE__,
93 ] );
94 exit;
95 }
96 }
97
98 $this->import_actions( true );
99
100 return $results;
101 }
102
103 private function filter_post_types( $selected_custom_post_types = [] ) {
104 $wp_builtin_post_types = Utils::get_builtin_wp_post_types();
105
106 foreach ( $selected_custom_post_types as $custom_post_type ) {
107 if ( post_type_exists( $custom_post_type ) ) {
108 $this->selected_types[] = $custom_post_type;
109 }
110 }
111
112 $post_types = array_merge( $wp_builtin_post_types, $this->selected_types );
113 $index = array_search( 'nav_menu_item', $post_types, true );
114 $gutenberg_index = array_search( 'wp_navigation', $post_types, true );
115
116 if ( false !== $index ) {
117 unset( $post_types[ $index ] );
118 $post_types[] = 'nav_menu_item';
119 }
120
121 if ( false !== $gutenberg_index ) {
122 unset( $post_types[ $gutenberg_index ] );
123 $post_types[] = 'wp_navigation';
124 }
125
126 return $post_types;
127 }
128
129 private function import_type_data( $type, $path, $imported_data, $taxonomies, $terms ): array {
130 $args = [
131 'fetch_attachments' => true,
132 'origin' => $this->origin,
133 'json' => $this->json,
134 'posts' => Utils::map_old_new_post_ids( $imported_data ),
135 'terms' => Utils::map_old_new_term_ids( $imported_data ),
136 'taxonomies' => ! empty( $taxonomies[ $type ] ) ? $taxonomies[ $type ] : [],
137 'posts_meta' => [
138 self::META_SESSION_KEY => $this->session_id,
139 ],
140 'terms_meta' => [
141 self::META_SESSION_KEY => $this->session_id,
142 ],
143 ];
144
145 if ( isset( $imported_data['archive_settings'] ) ) {
146 foreach ($imported_data['archive_settings'] as $key => $archive_settings) {
147 $args['posts'][ $archive_settings['old_id'] ] = $archive_settings['page_id'];
148 $args['posts'][ $archive_settings['archive_id'] ] = $archive_settings['page_id'];
149 }
150 }
151
152 $file = $path . $type . '/' . $type . '.xml';
153
154 if ( ! file_exists( $file ) ) {
155 return [];
156 }
157
158 $wp_importer = new WPImport( $file, $args );
159 $result = $wp_importer->run();
160
161 return $result['summary'];
162 }
163
164 private function import_actions( $remove = false ) {
165 if ( ! $remove ) {
166 add_action( 'templately_import.process_post', [ $this, 'post_log' ], 10, 2 );
167 add_action( 'templately_import.process_term', [ $this, 'post_log' ], 10, 2 );
168 add_action( 'import_start', [ $this, 'update_total' ], 10 );
169
170 return;
171 }
172
173 remove_action( 'templately_import.process_post', [ $this, 'post_log' ] );
174 remove_action( 'templately_import.process_term', [ $this, 'post_log' ] );
175 remove_action( 'import_start', [ $this, 'update_total' ] );
176 }
177
178 public function post_log( $post, $result ) {
179 if ( isset( $post['post_type'] ) ) {
180 if ( ! isset( $this->manifest['wp-content'][ $post['post_type'] ] ) ) {
181 return;
182 }
183
184 $commonItems = array_intersect(array_keys($result['succeed']), $this->manifest['wp-content'][ $post['post_type'] ]);
185 $this->processed[ $post['post_type'] ] = count($commonItems);
186
187 $type = $post['post_type'];
188 $title = $post['post_title'];
189
190 $this->origin->update_progress( null, ['wp-content_processed' => $this->processed]);
191 } elseif ( isset( $post['term_id'] ) ) {
192 /**
193 * FIXME: We should fix it later, with a proper count of terms and make a total with post itself.
194 */
195 return;
196 }
197
198 if ( empty( $type ) || empty( $title ) ) {
199 return;
200 }
201
202 $failed = 0;
203 if ( ! empty( $result['failed'] ) ) {
204 $failed = count( $result['failed'] );
205 }
206
207 $processed = array_reduce( $this->processed, function ( $carry, $item ) {
208 return $carry + $item;
209 }, 0 );
210
211 $progress = $this->total > 0 ? floor( ( 100 * ( $processed + $failed ) ) / $this->total ) : 100;
212
213 $this->log( $progress);
214 }
215
216 public function update_total( $WPImport ) {
217 $session_data = $this->origin->get_session_data();
218 $contents = $session_data['imported_data']['wp-content_manifest_content'];
219
220 foreach ($WPImport->posts as $key => $post) {
221 $postType = $post['post_type'];
222 $postId = $post['post_id'];
223
224 if (!isset($contents[$postType]) || !in_array($postId, $contents[$postType])) {
225 $contents[$postType][] = $postId;
226 }
227 }
228
229 $this->total = array_reduce( $contents, function ( $carry, $item ) {
230 return $carry + count( $item );
231 }, 0 );
232
233 $this->origin->update_progress( null, ['wp-content_manifest_content' => $contents]);
234 }
235 }