import_process = $import_process; $this->import_service = $import_service; add_action( 'admin_post_acafs_import_flamingo_messages', array( $this, 'acafs_import_flamingo_messages' ) ); add_action( 'acafs_render_import_export_page', array( $this, 'acafs_render_import_section' ) ); add_action( 'admin_notices', array( $this, 'acafs_show_import_notice' ) ); } /** * Handle the import form submission */ public function acafs_import_flamingo_messages() { // Permissions check if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to perform this action.', 'ac-advanced-flamingo-settings' ) ); } // Nonce check if ( ! isset( $_POST['acafs_import_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['acafs_import_nonce'] ) ), 'acafs_import_nonce' ) ) { wp_die( esc_html__( 'Security check failed.', 'ac-advanced-flamingo-settings' ) ); } // File validation if ( ! isset( $_FILES['flamingo_import_file'] ) || empty( $_FILES['flamingo_import_file']['tmp_name'] ) || ! isset( $_FILES['flamingo_import_file']['error'] ) || UPLOAD_ERR_OK !== (int) $_FILES['flamingo_import_file']['error'] ) { wp_die( esc_html__( 'No file uploaded. Please select a valid JSON file.', 'ac-advanced-flamingo-settings' ) ); } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- A server-created upload path must not be altered; validate its provenance before reading. $temporary_file = $_FILES['flamingo_import_file']['tmp_name']; if ( ! is_string( $temporary_file ) || ! is_uploaded_file( $temporary_file ) ) { wp_die( esc_html__( 'The uploaded import file is invalid.', 'ac-advanced-flamingo-settings' ) ); } $messages = $this->import_service->read_file( $temporary_file ); if ( is_wp_error( $messages ) ) { wp_die( esc_html( $messages->get_error_message() ) ); } delete_transient( 'acafs_import_result' ); delete_transient( 'acafs_import_success' ); $chunks = array_chunk( $messages, 50 ); foreach ( $chunks as $batch ) { $this->import_process->push_to_queue( $batch ); } $this->import_process->save()->dispatch(); set_transient( 'acafs_import_started', 'processing' ); wp_safe_redirect( admin_url( 'admin.php?page=acafs-message-sync&import_started=1' ) ); exit; } /** * Show import notices */ public function acafs_show_import_notice() { if ( ! current_user_can( 'manage_options' ) ) { return; } $done = get_transient( 'acafs_import_success' ); $started = get_transient( 'acafs_import_started' ); $result = get_transient( 'acafs_import_result' ); if ( ! $started && is_array( $result ) ) { $failed = ! empty( $result['failed'] ); echo '
'; printf( /* translators: 1: Processed messages, 2: Inserted messages, 3: Skipped duplicates, 4: Failed messages. */ esc_html__( 'Processed: %1$d. Imported: %2$d. Skipped: %3$d. Failed: %4$d.', 'ac-advanced-flamingo-settings' ), (int) $result['processed'], (int) $result['inserted'], (int) $result['skipped'], (int) $result['failed'] ); echo '
'; if ( $failed ) { echo '' . esc_html__( 'Some messages could not be restored. Check the import file and channel configuration before retrying failed messages. Review any incomplete records if cleanup failed.', 'ac-advanced-flamingo-settings' ) . '
'; } echo '