PostType::POST_TYPE, 'post_status' => 'any', 'posts_per_page' => -1, ) ); $rows = array(); foreach ( $faqs as $faq ) { $cats = wp_get_post_terms( $faq->ID, Taxonomy::TAXONOMY, array( 'fields' => 'names' ) ); $rows[] = array( 'question' => $faq->post_title, 'answer' => $faq->post_content, 'categories' => is_wp_error( $cats ) ? '' : implode( '|', $cats ), 'status' => $faq->post_status, ); } if ( 'json' === $format ) { header( 'Content-Type: application/json; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=faqs-' . gmdate( 'Y-m-d' ) . '.json' ); echo wp_json_encode( $rows, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); exit; } header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=faqs-' . gmdate( 'Y-m-d' ) . '.csv' ); $out = fopen( 'php://output', 'w' ); fputcsv( $out, array( 'question', 'answer', 'categories', 'status' ) ); foreach ( $rows as $row ) { fputcsv( $out, $row ); } fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose exit; } /** * Import FAQs from an uploaded CSV or JSON file. * * @return void */ public function import() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Permission denied.', 'accordion-faq-plugin' ) ); } check_admin_referer( 'accordion_faq_import' ); $redirect = admin_url( 'admin.php?page=' . Menu::SLUG . '-tools' ); if ( empty( $_FILES['afaq_file']['tmp_name'] ) ) { wp_safe_redirect( add_query_arg( 'afaq_import', 'nofile', $redirect ) ); exit; } $tmp = sanitize_text_field( wp_unslash( $_FILES['afaq_file']['tmp_name'] ) ); $name = isset( $_FILES['afaq_file']['name'] ) ? sanitize_file_name( wp_unslash( $_FILES['afaq_file']['name'] ) ) : ''; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $contents = file_get_contents( $tmp ); $imported = 0; $rows = array(); if ( preg_match( '/\.json$/i', $name ) ) { $decoded = json_decode( $contents, true ); $rows = is_array( $decoded ) ? $decoded : array(); } else { $lines = preg_split( '/\r\n|\r|\n/', (string) $contents ); $header = null; foreach ( $lines as $line ) { if ( '' === trim( $line ) ) { continue; } $cols = str_getcsv( $line ); if ( null === $header ) { $header = array_map( 'strtolower', array_map( 'trim', $cols ) ); continue; } $rows[] = array_combine( $header, array_pad( $cols, count( $header ), '' ) ); } } $allowed_status = array( 'publish', 'draft', 'pending', 'private' ); foreach ( $rows as $row ) { $question = isset( $row['question'] ) ? sanitize_text_field( $row['question'] ) : ''; if ( '' === $question ) { continue; } // Respect an exported status column; fall back to publish. $status = isset( $row['status'] ) ? sanitize_key( $row['status'] ) : 'publish'; if ( ! in_array( $status, $allowed_status, true ) ) { $status = 'publish'; } $post_id = wp_insert_post( array( 'post_type' => PostType::POST_TYPE, 'post_status' => $status, 'post_title' => $question, 'post_content' => isset( $row['answer'] ) ? wp_kses_post( $row['answer'] ) : '', ) ); if ( $post_id && ! is_wp_error( $post_id ) ) { if ( ! empty( $row['categories'] ) ) { $names = array_filter( array_map( 'trim', explode( '|', (string) $row['categories'] ) ) ); wp_set_object_terms( $post_id, $names, Taxonomy::TAXONOMY ); } $imported++; } } wp_safe_redirect( add_query_arg( 'afaq_import', $imported, $redirect ) ); exit; } }