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

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