sessions = $sessions; $this->candidates = $candidates; } /** * {@inheritdoc} */ public function register(): void { add_action( 'admin_post_' . self::ACTION_EXPORT, array( $this, 'export' ) ); add_action( 'admin_post_' . self::ACTION_SAMPLE, array( $this, 'sample' ) ); } /** * Download URL without a session; the app appends `&session_id=`. * * @since 0.1.0 * * @param string $kind `export` or `sample`. * @return string */ public static function url( string $kind ): string { $action = 'sample' === $kind ? self::ACTION_SAMPLE : self::ACTION_EXPORT; return wp_nonce_url( add_query_arg( 'action', $action, admin_url( 'admin-post.php' ) ), $action ); } /** * Sample file with the expected columns. * * @since 0.1.0 * * @return void */ public function sample(): void { $this->authorize( self::ACTION_SAMPLE ); $this->start( 'interviewnea-sample.csv' ); // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV, not HTML; cells are made safe by CsvWriter. echo CsvWriter::line( array( 'name', 'email', 'phone', 'report_time' ) ); echo CsvWriter::line( array( 'Example Candidate', 'candidate@example.com', '01712345678', '18:00' ) ); // phpcs:enable exit; } /** * Candidates of one session. * * @since 0.1.0 * * @return void */ public function export(): void { $this->authorize( self::ACTION_EXPORT ); $session_id = isset( $_GET['session_id'] ) ? absint( wp_unslash( $_GET['session_id'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified in authorize(). $session = $this->sessions->find( $session_id ); if ( ! $session ) { wp_die( esc_html__( 'Session not found.', 'interviewnea' ), '', array( 'response' => 404 ) ); } $this->start( 'interviewnea-candidates-' . $session_id . '-' . gmdate( 'Y-m-d' ) . '.csv' ); // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV, not HTML; cells are made safe by CsvWriter. foreach ( $this->lines( $session_id ) as $line ) { echo $line; } // phpcs:enable exit; } /** * CSV lines for a session, header first. * * @since 0.1.0 * * @param int $session_id Session id. * @return \Generator */ public function lines( int $session_id ): \Generator { yield CsvWriter::line( array( 'id', 'name', 'email', 'phone', 'report_time', 'status', 'order', 'checked_in_utc', 'called_utc', 'join_link_opened_utc', 'host_confirmed_join_utc', 'completed_utc', 'reminder_status', 'join_reminder_status' ) ); foreach ( $this->candidates->for_session( $session_id, 5000 ) as $c ) { yield CsvWriter::line( array( $c->id, $c->full_name, $c->email, $c->phone, $c->report_time ? substr( $c->report_time, 0, 5 ) : '', $c->status, $c->queue_order, Helpers::iso( $c->checkin_at ), Helpers::iso( $c->called_at ), Helpers::iso( $c->join_requested_at ), Helpers::iso( $c->joined_at ), Helpers::iso( $c->completed_at ), $c->reminder_status, $c->join_reminder_status, ) ); } } /** * Checks the nonce and the capability. * * @param string $action Nonce action. * @return void */ private function authorize( string $action ): void { check_admin_referer( $action ); if ( ! Capabilities::can_manage() ) { wp_die( esc_html__( 'You are not allowed to download this.', 'interviewnea' ), '', array( 'response' => 403 ) ); } } /** * Sends the download headers and the byte order mark that makes Excel read UTF-8 (Bangla) correctly. * * @param string $filename File name. * @return void */ private function start( string $filename ): void { nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( $filename ) . '"' ); header( 'X-Content-Type-Options: nosniff' ); echo "\xEF\xBB\xBF"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Byte order mark. } }