/** * Import posts or pages from a CSV file. * * ## OPTIONS * * <file> * : The name of the CSV file to import. * * [--charset=<charset>] * : Character set of the CSV file. Defaults to UTF-8. * */ function import($args, $assoc_args) { if (isset($assoc_args['charset']) && $assoc_args['charset']) { add_filter('advanced_csv_importer_csv_format', function ($format) use($assoc_args) { $format['from_charset'] = strtoupper($assoc_args['charset']); return $format; }); } $post_objects = Main::get_post_objects($args[0]); if (is_wp_error($post_objects)) { WP_CLI::error($post_objects->get_error_message()); } $inserted_posts = Main::insert_posts($post_objects); $this->get_imported_data($inserted_posts); }
/** * The form of the second step. * * @param none * @return none */ private function step2() { $file = wp_import_handle_upload(); if (isset($file['error'])) { return new WP_Error('Error', esc_html($file['error'])); } else { if (!file_exists($file['file'])) { return new WP_Error('Error', sprintf(__('The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'advanced-csv-importer'), esc_html($file['file']))); } } $csv_file = get_attached_file($file['id']); $post_objects = Main::get_post_objects($csv_file); if (is_wp_error($post_objects)) { echo '<p><strong>' . __('Failed to open file.', 'advanced-csv-importer') . '</strong></p>'; wp_import_cleanup($file['id']); return $post_objects; } else { $inserted_posts = Main::insert_posts($post_objects); wp_import_cleanup($file['id']); return $inserted_posts; } }
/** * @test */ public function insert_posts_01() { $post_objects = \ACSV\Main::get_post_objects(dirname(__FILE__) . '/_data/wp/sample.csv'); $inserted_posts = \ACSV\Main::insert_posts($post_objects); $this->assertSame(4, count($inserted_posts)); for ($i = 0; $i < count($post_objects); $i++) { if (is_wp_error($inserted_posts[$i])) { continue; } $post = $post_objects[$i]; $p = get_post($inserted_posts[$i]); if ($post['post_name']) { $this->assertSame($p->post_name, strtolower($post['post_name'])); } $this->assertSame($p->post_title, $post['post_title']); $this->assertSame($p->post_content, $post['post_content']); } }