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