PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.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 3.1.10 All 111 releases
templately / includes / Core / Importer / Runners / WPContent.php

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

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