PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
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 / modules / full-site-import / Runners / WPContent.php

WPContent.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at modules/full-site-import/Runners/WPContent.php

254 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Modules\FullSiteImport\Runners;
4
5 use Templately\Modules\FullSiteImport\Utils\FluentImport;
6 use Templately\Modules\FullSiteImport\Utils\Utils;
7 use Templately\Modules\FullSiteImport\WPImport;
8 use Templately\Utils\Helper;
9
10 class WPContent extends BaseRunner {
11 protected $selected_types = [];
12
13 /**
14 * @var int
15 */
16 protected $total = 1;
17
18 protected $processed = [];
19
20 public function get_name(): string {
21 return 'wp-content';
22 }
23
24 public function get_label(): string {
25 return __( 'WordPress Contents', 'templately' );
26 }
27
28 public function log_message(): string {
29 return __( 'Importing Content (Pages, Posts, Products, Navigation)', 'templately' );
30 }
31
32 public function should_run( $data, $imported_data = [] ): bool {
33 return ! empty( $this->manifest['wp-content'] );
34 // return isset( $data[ 'wp-content' ] );
35 }
36
37 public function import( $data, $imported_data ): array {
38 $path = $this->dir_path . 'wp-content' . DIRECTORY_SEPARATOR;
39 $post_types = $this->filter_post_types( $data['selected_post_types'] ?? [] );
40
41 $this->import_actions();
42
43 $contents = $this->get_loop_result($this->manifest['wp-content'], 'wp-content_manifest_content', false);
44 $this->processed = $this->get_loop_result([], 'wp-content_processed', false);
45 $this->total = array_reduce( $contents, function ( $carry, $item ) {
46 return $carry + count( $item );
47 }, 0 );
48
49
50 $results = $this->loop( $post_types, function($key, $type, $results ) use($path, $imported_data, $data) {
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 /**
101 * The meta every item this run creates is stamped with.
102 *
103 * The session key is ours and is not negotiable — revert and orphan
104 * reclamation both find imported content through it, so a contributor that
105 * dropped or rewrote it would strand content this import created. It is
106 * therefore re-applied AFTER the filter rather than merged before it.
107 *
108 * Everything else is open. Modules that want to record something about
109 * imported content attach here instead of hooking `wp_insert_post` and
110 * guessing which inserts were ours; the importer already applies this bag
111 * to every post and term it creates, so a contributor reaches all of them
112 * without knowing which runner produced what. The dependency points the
113 * right way — this runner names no module (Constitution VIII).
114 *
115 * @since 3.8.0
116 *
117 * @param string $entity 'post' or 'term'.
118 * @return array<string, mixed> Meta keys mapped to values.
119 */
120 protected function item_meta( $entity ) {
121 $meta = apply_filters(
122 'templately_fsi_item_meta',
123 [ self::META_SESSION_KEY => $this->session_id ],
124 $entity,
125 $this->session_id
126 );
127
128 if ( ! is_array( $meta ) ) {
129 $meta = [];
130 }
131
132 $meta[ self::META_SESSION_KEY ] = $this->session_id;
133
134 return $meta;
135 }
136
137 protected function _import_type_data( $type, $path, $imported_data ): array {
138 $args = [
139 'fetch_attachments' => true,
140 'origin' => $this->origin,
141 'session_id' => $this->session_id,
142 'json' => $this->json,
143 'posts' => Utils::map_old_new_post_ids( $imported_data ),
144 'terms' => Utils::map_old_new_term_ids( $imported_data ),
145 'taxonomies' => [],
146 'posts_meta' => $this->item_meta( 'post' ),
147 'terms_meta' => $this->item_meta( 'term' ),
148 ];
149
150 if ( isset( $imported_data['archive_settings'] ) ) {
151 foreach ($imported_data['archive_settings'] as $key => $archive_settings) {
152 $args['posts'][ $archive_settings['old_id'] ] = $archive_settings['page_id'];
153 $args['posts'][ $archive_settings['archive_id'] ] = $archive_settings['page_id'];
154 }
155 }
156
157 if($type == 'fluent-products'){
158 $file = $path . $type . '/' . 'products.json';
159 $fluent_import = new FluentImport( $file );
160 $result = $fluent_import->import();
161 return $result;
162 }
163
164 $file = $path . $type . '/' . $type . '.xml';
165
166 if ( ! file_exists( $file ) ) {
167 return [];
168 }
169
170 $wp_importer = new WPImport( $file, $args );
171 $result = $wp_importer->run();
172
173 return $result;
174 }
175
176 protected function import_type_data( $type, $path, $imported_data ): array {
177 $result = $this->_import_type_data( $type, $path, $imported_data );
178 if(isset($result['summary'])){
179 return $result['summary'];
180 }
181 return [];
182 }
183
184 protected function import_actions( $remove = false ) {
185 if ( ! $remove ) {
186 add_action( 'templately_import.process_post', [ $this, 'post_log' ], 10, 2 );
187 add_action( 'templately_import.process_term', [ $this, 'post_log' ], 10, 2 );
188 add_action( 'templately_import_start', [ $this, 'update_total' ], 10 );
189
190 return;
191 }
192
193 remove_action( 'templately_import.process_post', [ $this, 'post_log' ] );
194 remove_action( 'templately_import.process_term', [ $this, 'post_log' ] );
195 remove_action( 'templately_import_start', [ $this, 'update_total' ] );
196 }
197
198 public function post_log( $post, $result ) {
199 if ( isset( $post['post_type'] ) ) {
200 if ( ! isset( $this->manifest['wp-content'][ $post['post_type'] ] ) ) {
201 return;
202 }
203
204 $commonItems = array_intersect(array_keys($result['succeed']), $this->manifest['wp-content'][ $post['post_type'] ]);
205 $this->processed[ $post['post_type'] ] = count($commonItems);
206
207 $type = $post['post_type'];
208 $title = $post['post_title'];
209
210 $this->set_loop_result( $this->processed, 'wp-content_processed');
211 } elseif ( isset( $post['term_id'] ) ) {
212 /**
213 * FIXME: We should fix it later, with a proper count of terms and make a total with post itself.
214 */
215 return;
216 }
217
218 if ( empty( $type ) || empty( $title ) ) {
219 return;
220 }
221
222 $failed = 0;
223 if ( ! empty( $result['failed'] ) ) {
224 $failed = count( $result['failed'] );
225 }
226
227 $processed = array_reduce( $this->processed, function ( $carry, $item ) {
228 return $carry + $item;
229 }, 0 );
230
231 $progress = $this->total > 0 ? floor( ( 100 * ( $processed + $failed ) ) / $this->total ) : 100;
232
233 $this->log( $progress);
234 }
235
236 public function update_total( $WPImport ) {
237 $contents = $this->get_loop_result([], 'wp-content_manifest_content', false);
238
239 foreach ($WPImport->posts as $key => $post) {
240 $postType = $post['post_type'];
241 $postId = $post['post_id'];
242
243 if (!isset($contents[$postType]) || !in_array($postId, $contents[$postType])) {
244 $contents[$postType][] = $postId;
245 }
246 }
247
248 $this->total = array_reduce( $contents, function ( $carry, $item ) {
249 return $carry + count( $item );
250 }, 0 );
251
252 $this->set_loop_result( $contents, 'wp-content_manifest_content');
253 }
254 }