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