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

221 lines 6.5 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\FluentImport;
6 use Templately\Core\Importer\Utils\Utils;
7 use Templately\Core\Importer\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 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 if($type == 'fluent-products'){
125 $file = $path . $type . '/' . 'products.json';
126 $fluent_import = new FluentImport( $file );
127 $result = $fluent_import->import();
128 return $result;
129 }
130
131 $file = $path . $type . '/' . $type . '.xml';
132
133 if ( ! file_exists( $file ) ) {
134 return [];
135 }
136
137 $wp_importer = new WPImport( $file, $args );
138 $result = $wp_importer->run();
139
140 return $result;
141 }
142
143 protected function import_type_data( $type, $path, $imported_data ): array {
144 $result = $this->_import_type_data( $type, $path, $imported_data );
145 if(isset($result['summary'])){
146 return $result['summary'];
147 }
148 return [];
149 }
150
151 protected function import_actions( $remove = false ) {
152 if ( ! $remove ) {
153 add_action( 'templately_import.process_post', [ $this, 'post_log' ], 10, 2 );
154 add_action( 'templately_import.process_term', [ $this, 'post_log' ], 10, 2 );
155 add_action( 'templately_import_start', [ $this, 'update_total' ], 10 );
156
157 return;
158 }
159
160 remove_action( 'templately_import.process_post', [ $this, 'post_log' ] );
161 remove_action( 'templately_import.process_term', [ $this, 'post_log' ] );
162 remove_action( 'templately_import_start', [ $this, 'update_total' ] );
163 }
164
165 public function post_log( $post, $result ) {
166 if ( isset( $post['post_type'] ) ) {
167 if ( ! isset( $this->manifest['wp-content'][ $post['post_type'] ] ) ) {
168 return;
169 }
170
171 $commonItems = array_intersect(array_keys($result['succeed']), $this->manifest['wp-content'][ $post['post_type'] ]);
172 $this->processed[ $post['post_type'] ] = count($commonItems);
173
174 $type = $post['post_type'];
175 $title = $post['post_title'];
176
177 $this->set_loop_result( $this->processed, 'wp-content_processed');
178 } elseif ( isset( $post['term_id'] ) ) {
179 /**
180 * FIXME: We should fix it later, with a proper count of terms and make a total with post itself.
181 */
182 return;
183 }
184
185 if ( empty( $type ) || empty( $title ) ) {
186 return;
187 }
188
189 $failed = 0;
190 if ( ! empty( $result['failed'] ) ) {
191 $failed = count( $result['failed'] );
192 }
193
194 $processed = array_reduce( $this->processed, function ( $carry, $item ) {
195 return $carry + $item;
196 }, 0 );
197
198 $progress = $this->total > 0 ? floor( ( 100 * ( $processed + $failed ) ) / $this->total ) : 100;
199
200 $this->log( $progress);
201 }
202
203 public function update_total( $WPImport ) {
204 $contents = $this->get_loop_result([], 'wp-content_manifest_content', false);
205
206 foreach ($WPImport->posts as $key => $post) {
207 $postType = $post['post_type'];
208 $postId = $post['post_id'];
209
210 if (!isset($contents[$postType]) || !in_array($postId, $contents[$postType])) {
211 $contents[$postType][] = $postId;
212 }
213 }
214
215 $this->total = array_reduce( $contents, function ( $carry, $item ) {
216 return $carry + count( $item );
217 }, 0 );
218
219 $this->set_loop_result( $contents, 'wp-content_manifest_content');
220 }
221 }