/** * Insert posts * * @param array $post_objects Array of the post objects. * @return array The post id or WP_Error object. * @since 0.1.0 */ public static function insert_posts($post_objects) { $inserted_posts = array(); foreach ($post_objects as $post) { $post_id = self::insert_post($post); $inserted_posts[] = $post_id; } History::save_history($inserted_posts); return $inserted_posts; }
private function delete_form($inserted_posts) { $posts = History::post_ids_to_posts($inserted_posts); if (!$posts) { echo '<p>Posts were already deleted.</p>'; return; } $success = History::get_num_success($inserted_posts); $fail = History::get_num_fail($inserted_posts); echo '<p>'; if ($success === 1) { echo $success . ' post imported. '; } else { echo $success . ' posts imported. '; } if ($fail === 1) { echo $fail . ' post failed to import. '; } else { echo $fail . ' posts failed to import. '; } echo '</p>'; echo '<form method="post" action="' . esc_url(add_query_arg(array('step' => 3), $this->get_action())) . '">'; wp_nonce_field('acsv-import-delete'); echo '<table class="wp-list-table widefat fixed posts">'; echo '<thead><tr style="color: #dedede;">'; echo '<th scope="col" class="manage-column column-cb check-column"><input type="checkbox" id="cb-select-all-1" /></th><th scope="col">Title</th><th scope="col">Type</th><th scope="col">Status</th><th scope="col">Author</th><th scope="col">Date</th>'; echo '</tr></thead>'; foreach ($posts as $p) { printf('<tr><th scope="row" class="check-column"><input type="checkbox" name="acsv-import-id[]" value="%d" /></th><td class="post-title page-title column-title"><a href="post.php?post=%d&action=edit" target="_blank">%s</a></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>', intval($p['ID']), intval($p['ID']), esc_html($p['Title']), esc_html($p['Type']), esc_html($p['Status']), esc_html($p['Author']), esc_html($p['Date'])); } echo '</table>'; echo '<p style="text-align: right;"><input type="submit" name="submit" id="submit" class="button advanced-csv-importer" value="Move to Trash" /></p>'; echo '</form>'; }
/** * Importing author and title. * * @test */ public function insert_posts_author() { $posts = array(array('post_author' => 'admin', 'post_title' => 'original post'), array('post_author' => 'foo', 'post_title' => 'original post'), array('post_author' => 'bar', 'post_title' => 'original post')); $inserted_posts = \ACSV\Main::insert_posts($posts); for ($i = 0; $i < count($inserted_posts); $i++) { $post = get_post($inserted_posts[$i]); $this->assertSame("1", $post->post_author, $posts[$i]['post_author'] . ' should be 1.'); } $logs = \ACSV\History::get_history(); $this->assertEquals(1, count($logs)); $log_name = \ACSV\History::get_log_name($inserted_posts); $log = \ACSV\History::get_imported_post_ids($log_name); $this->assertEquals(3, count($log)); }
/** * Display importing log. * * @param array $inserted_posts An array of the post ids * @return none */ private function get_imported_data($inserted_posts, $format = 'table') { $posts = History::post_ids_to_posts($inserted_posts); WP_CLI\Utils\format_items($format, $posts, array('ID', 'Title', 'Type', 'Status', 'Author', 'Date')); $fail = History::get_num_fail($inserted_posts); if ($fail) { WP_CLI::warning('Failed to import: ' . $fail); } }