| @@ -1,21 +1,22 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Templately\Core\Importer\Runners; |
| 4 | 4 | |
| 5 | +use Templately\Core\Importer\Utils\FluentImport; | |
| 5 | 6 | use Templately\Core\Importer\Utils\Utils; |
| 6 | 7 | use Templately\Core\Importer\WPImport; |
| 7 | 8 | use Templately\Utils\Helper; |
| 8 | 9 | |
| 9 | 10 | class WPContent extends BaseRunner { |
| 10 | - private $selected_types = []; | |
| 11 | + protected $selected_types = []; | |
| 11 | 12 | |
| 12 | 13 | /** |
| 13 | 14 | * @var int |
| 14 | 15 | */ |
| 15 | - private $total = 1; | |
| 16 | + protected $total = 1; | |
| 16 | 17 | |
| 17 | - private $processed = []; | |
| 18 | + protected $processed = []; | |
| 18 | 19 | |
| 19 | 20 | public function get_name(): string { |
| 20 | 21 | return 'wp-content'; |
| 21 | 22 | } |
| @@ -24,9 +25,9 @@ | ||
| 24 | 25 | return __( 'WordPress Contents', 'templately' ); |
| 25 | 26 | } |
| 26 | 27 | |
| 27 | 28 | public function log_message(): string { |
| 28 | - return __( 'Importing Pages, Posts, Products, Navigation, etc', 'templately' ); | |
| 29 | + return __( 'Importing Content (Pages, Posts, Products, Navigation)', 'templately' ); | |
| 29 | 30 | } |
| 30 | 31 | |
| 31 | 32 | public function should_run( $data, $imported_data = [] ): bool { |
| 32 | 33 | return ! empty( $this->manifest['wp-content'] ); |
| @@ -35,43 +36,27 @@ | ||
| 35 | 36 | |
| 36 | 37 | public function import( $data, $imported_data ): array { |
| 37 | 38 | $path = $this->dir_path . 'wp-content' . DIRECTORY_SEPARATOR; |
| 38 | 39 | $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 | 40 | |
| 46 | 41 | $this->import_actions(); |
| 47 | 42 | |
| 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 ) { | |
| 43 | + $contents = $this->get_progress($this->manifest['wp-content'], 'wp-content_manifest_content', false); | |
| 44 | + $this->processed = $this->get_progress([], 'wp-content_processed', false); | |
| 45 | + $this->total = array_reduce( $contents, function ( $carry, $item ) { | |
| 51 | 46 | return $carry + count( $item ); |
| 52 | 47 | }, 0 ); |
| 53 | 48 | |
| 54 | - $processed_templates = $this->origin->get_progress(); | |
| 55 | 49 | |
| 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 | - | |
| 50 | + $results = $this->loop( $post_types, function($key, $type ) use($path, $imported_data, $data) { | |
| 51 | + $results = []; | |
| 67 | 52 | if(empty($data['import_demo_content']) && !in_array($type, ['wp_navigation', 'nav_menu_item'])) { |
| 68 | - continue; | |
| 53 | + return $results; | |
| 69 | 54 | } |
| 70 | - $import = $this->import_type_data( $type, $path, $imported_data, $taxonomies, $terms ); | |
| 55 | + $import = $this->import_type_data( $type, $path, $imported_data ); | |
| 71 | 56 | |
| 72 | 57 | if ( empty( $import['posts'] ) ) { |
| 73 | - continue; | |
| 58 | + return $results; | |
| 74 | 59 | } |
| 75 | 60 | if(isset($import['posts']['__attachments'])){ |
| 76 | 61 | $results['wp-content']['__attachments'][ $type ] = $import['posts']['__attachments']; |
| 77 | 62 | unset($import['posts']['__attachments']); |
| @@ -77,31 +62,18 @@ | ||
| 77 | 62 | unset($import['posts']['__attachments']); |
| 78 | 63 | } |
| 79 | 64 | $results['wp-content'][ $type ] = $import['posts']; |
| 80 | 65 | $results['terms'][ $type ] = $import['terms']; |
| 81 | - $imported_data = array_merge( $imported_data, $results ); | |
| 82 | 66 | |
| 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); | |
| 67 | + return $results; | |
| 68 | + }); | |
| 86 | 69 | |
| 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 | 70 | $this->import_actions( true ); |
| 99 | 71 | |
| 100 | 72 | return $results; |
| 101 | 73 | } |
| 102 | 74 | |
| 103 | - private function filter_post_types( $selected_custom_post_types = [] ) { | |
| 75 | + protected function filter_post_types( $selected_custom_post_types = [] ) { | |
| 104 | 76 | $wp_builtin_post_types = Utils::get_builtin_wp_post_types(); |
| 105 | 77 | |
| 106 | 78 | foreach ( $selected_custom_post_types as $custom_post_type ) { |
| 107 | 79 | if ( post_type_exists( $custom_post_type ) ) { |
| @@ -125,16 +97,17 @@ | ||
| 125 | 97 | |
| 126 | 98 | return $post_types; |
| 127 | 99 | } |
| 128 | 100 | |
| 129 | - private function import_type_data( $type, $path, $imported_data, $taxonomies, $terms ): array { | |
| 101 | + protected function _import_type_data( $type, $path, $imported_data ): array { | |
| 130 | 102 | $args = [ |
| 131 | 103 | 'fetch_attachments' => true, |
| 132 | 104 | 'origin' => $this->origin, |
| 105 | + 'session_id' => $this->session_id, | |
| 133 | 106 | 'json' => $this->json, |
| 134 | 107 | 'posts' => Utils::map_old_new_post_ids( $imported_data ), |
| 135 | 108 | 'terms' => Utils::map_old_new_term_ids( $imported_data ), |
| 136 | - 'taxonomies' => ! empty( $taxonomies[ $type ] ) ? $taxonomies[ $type ] : [], | |
| 109 | + 'taxonomies' => [], | |
| 137 | 110 | 'posts_meta' => [ |
| 138 | 111 | self::META_SESSION_KEY => $this->session_id, |
| 139 | 112 | ], |
| 140 | 113 | 'terms_meta' => [ |
| @@ -148,8 +121,15 @@ | ||
| 148 | 121 | $args['posts'][ $archive_settings['archive_id'] ] = $archive_settings['page_id']; |
| 149 | 122 | } |
| 150 | 123 | } |
| 151 | 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 | + | |
| 152 | 132 | $file = $path . $type . '/' . $type . '.xml'; |
| 153 | 133 | |
| 154 | 134 | if ( ! file_exists( $file ) ) { |
| 155 | 135 | return []; |
| @@ -157,16 +137,24 @@ | ||
| 157 | 137 | |
| 158 | 138 | $wp_importer = new WPImport( $file, $args ); |
| 159 | 139 | $result = $wp_importer->run(); |
| 160 | 140 | |
| 161 | - return $result['summary']; | |
| 141 | + return $result; | |
| 162 | 142 | } |
| 163 | 143 | |
| 164 | - private function import_actions( $remove = false ) { | |
| 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 ) { | |
| 165 | 153 | if ( ! $remove ) { |
| 166 | 154 | add_action( 'templately_import.process_post', [ $this, 'post_log' ], 10, 2 ); |
| 167 | 155 | add_action( 'templately_import.process_term', [ $this, 'post_log' ], 10, 2 ); |
| 168 | - add_action( 'import_start', [ $this, 'update_total' ], 10 ); | |
| 156 | + add_action( 'templately_import_start', [ $this, 'update_total' ], 10 ); | |
| 169 | 157 | |
| 170 | 158 | return; |
| 171 | 159 | } |
| 172 | 160 | |
| @@ -171,9 +159,9 @@ | ||
| 171 | 159 | } |
| 172 | 160 | |
| 173 | 161 | remove_action( 'templately_import.process_post', [ $this, 'post_log' ] ); |
| 174 | 162 | remove_action( 'templately_import.process_term', [ $this, 'post_log' ] ); |
| 175 | - remove_action( 'import_start', [ $this, 'update_total' ] ); | |
| 163 | + remove_action( 'templately_import_start', [ $this, 'update_total' ] ); | |
| 176 | 164 | } |
| 177 | 165 | |
| 178 | 166 | public function post_log( $post, $result ) { |
| 179 | 167 | if ( isset( $post['post_type'] ) ) { |
| @@ -186,9 +174,9 @@ | ||
| 186 | 174 | |
| 187 | 175 | $type = $post['post_type']; |
| 188 | 176 | $title = $post['post_title']; |
| 189 | 177 | |
| 190 | - $this->origin->update_progress( null, ['wp-content_processed' => $this->processed]); | |
| 178 | + $this->update_progress( $this->processed, null, 'wp-content_processed', false); | |
| 191 | 179 | } elseif ( isset( $post['term_id'] ) ) { |
| 192 | 180 | /** |
| 193 | 181 | * FIXME: We should fix it later, with a proper count of terms and make a total with post itself. |
| 194 | 182 | */ |
| @@ -213,10 +201,9 @@ | ||
| 213 | 201 | $this->log( $progress); |
| 214 | 202 | } |
| 215 | 203 | |
| 216 | 204 | public function update_total( $WPImport ) { |
| 217 | - $session_data = $this->origin->get_session_data(); | |
| 218 | - $contents = $session_data['imported_data']['wp-content_manifest_content']; | |
| 205 | + $contents = $this->get_progress([], 'wp-content_manifest_content', false); | |
| 219 | 206 | |
| 220 | 207 | foreach ($WPImport->posts as $key => $post) { |
| 221 | 208 | $postType = $post['post_type']; |
| 222 | 209 | $postId = $post['post_id']; |
| @@ -229,7 +216,7 @@ | ||
| 229 | 216 | $this->total = array_reduce( $contents, function ( $carry, $item ) { |
| 230 | 217 | return $carry + count( $item ); |
| 231 | 218 | }, 0 ); |
| 232 | 219 | |
| 233 | - $this->origin->update_progress( null, ['wp-content_manifest_content' => $contents]); | |
| 220 | + $this->update_progress( $contents, null, 'wp-content_manifest_content', false); | |
| 234 | 221 | } |
| 235 | 222 | } |