'Insufficient permissions']); } $post_type = sanitize_text_field($_POST['post_type'] ?? 'post'); $limit = absint($_POST['limit'] ?? 100); $posts = get_posts([ 'post_type' => $post_type, 'post_status' => 'publish', 'numberposts' => $limit, ]); $preview = []; foreach ($posts as $post) { $preview[] = [ 'id' => $post->ID, 'title' => $post->post_title, 'date' => $post->post_date, 'content' => wp_trim_words($post->post_content, 20), ]; } wp_send_json_success(['posts' => $preview, 'count' => count($preview)]); } /** * AJAX handler for running post import */ public function ajax_run_import_posts() { check_ajax_referer('bd_bookshelf_import_posts', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error(['message' => 'Insufficient permissions']); } $post_type = sanitize_text_field($_POST['post_type'] ?? 'post'); $limit = absint($_POST['limit'] ?? 100); $posts = get_posts([ 'post_type' => $post_type, 'post_status' => 'publish', 'numberposts' => $limit, ]); $imported = 0; $skipped = 0; foreach ($posts as $post) { // Check if already imported $existing = BDM_Book_Manager::get_book_by_title($post->post_title); if ($existing) { $skipped++; continue; } // Import book $book_id = BDM_Book_Manager::create_book([ 'title' => $post->post_title, 'description' => $post->post_content, ]); if ($book_id) { // Link to WordPress post update_post_meta($post->ID, '_bd_bookshelf_book_id', $book_id); $imported++; } } wp_send_json_success([ 'message' => sprintf('Imported %d books, skipped %d', $imported, $skipped), 'imported' => $imported, 'skipped' => $skipped, ]); } /** * AJAX handler for CSV import */ public function ajax_import_csv() { check_ajax_referer('bd_bookshelf_import_csv', 'nonce'); if (!current_user_can('manage_options')) { wp_send_json_error(['message' => 'Insufficient permissions']); } if (empty($_FILES['csv_file']['tmp_name'])) { wp_send_json_error(['message' => 'No file uploaded']); } $file = $_FILES['csv_file']['tmp_name']; $handle = fopen($file, 'r'); if (!$handle) { wp_send_json_error(['message' => 'Unable to open file']); } // Get header row $header = fgetcsv($handle); $imported = 0; $skipped = 0; $errors = []; while (($row = fgetcsv($handle)) !== false) { $data = array_combine($header, $row); if (empty($data['title'])) { $errors[] = 'Row ' . ($imported + $skipped + 1) . ': Missing title'; continue; } // Check if already imported $existing = BDM_Book_Manager::get_book_by_title($data['title']); if ($existing) { $skipped++; continue; } // Create book $book_id = BDM_Book_Manager::create_book([ 'title' => $data['title'], 'description' => $data['description'] ?? '', 'isbn_ebook' => $data['isbn_ebook'] ?? '', 'isbn_paperback' => $data['isbn_paperback'] ?? '', 'isbn_hardcover' => $data['isbn_hardcover'] ?? '', 'isbn_audio' => $data['isbn_audio'] ?? '', 'amazon_asin' => $data['amazon_asin'] ?? '', 'goodreads_id' => $data['goodreads_id'] ?? '', ]); if ($book_id) { $imported++; } else { $errors[] = 'Row ' . ($imported + $skipped + 1) . ': Failed to create book'; } } fclose($handle); wp_send_json_success([ 'message' => sprintf('Imported %d books, skipped %d, %d errors', $imported, $skipped, count($errors)), 'imported' => $imported, 'skipped' => $skipped, 'errors' => $errors, ]); } }