⤓ ' . esc_html__( 'Download PDF', 'fieldwren' ) . ' '; } /** Bulk action entry. */ public static function bulk( $actions, $status = '' ) { if ( isset( $actions['export'] ) ) { $actions['pdf'] = __( 'Export selected (PDF)', 'fieldwren' ); } return $actions; } /** Bulk export. */ public static function bulk_export( $ids ) { self::send( array_slice( array_map( 'absint', (array) $ids ), 0, 500 ) ); } /** Single download. */ public static function download() { $id = absint( $_GET['entry'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification check_admin_referer( 'fwren_entry_pdf_' . $id ); self::send( array( $id ) ); } /** Stream the PDF. */ private static function send( array $ids ) { if ( ! current_user_can( 'fwren_view_entries' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'fieldwren' ) ); } $entries = array_values( array_filter( array_map( array( Entries::class, 'get' ), $ids ) ) ); if ( ! $entries ) { wp_die( esc_html__( 'Entry not found.', 'fieldwren' ) ); } $pdf = self::build( $entries ); $name = 1 === count( $entries ) ? 'entry-' . $entries[0]['form_id'] . '-' . $entries[0]['serial'] : 'entries-' . gmdate( 'Y-m-d' ); nocache_headers(); header( 'Content-Type: application/pdf' ); header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( $name ) . '.pdf"' ); header( 'Content-Length: ' . strlen( $pdf ) ); echo $pdf; // phpcs:ignore WordPress.Security.EscapeOutput -- Binary PDF. exit; } /** * Build the document. * * @param array[] $entries Entry rows. * @return string PDF bytes. */ public static function build( array $entries ) { $pdf = new Pdf(); $m = 46; $right = Pdf::W - $m; $labelw = 160; $valx = $m + $labelw + 14; $valw = $right - $valx; $bottom = Pdf::H - 60; $accent = '#2563eb'; foreach ( $entries as $e ) { $schema = Schema::get( $e['form_id'] ); $title = get_the_title( $e['form_id'] ); $pdf->add_page(); $pdf->rect( 0, 0, Pdf::W, 6, $accent ); $pdf->text( $m, 50, $title, 17, true ); /* translators: 1: entry number, 2: date */ $pdf->text( $m, 70, sprintf( __( 'Entry #%1$d · %2$s', 'fieldwren' ), $e['serial'], Admin_Entries::date( $e['created_at'] ) ), 9.5, false, '#6b7280' ); $statuses = Entries::statuses(); $pdf->text_right( $right, 50, $statuses[ $e['status'] ] ?? $e['status'], 9, true, '#6b7280' ); $y = 92; $pdf->line( $m, $y, $right, $y, '#d1d5db', 0.8 ); $y += 18; $rows = array(); foreach ( $schema['order'] as $k ) { $f = $schema['fields'][ $k ]; if ( ! array_key_exists( $k, $e['response'] ) || ! empty( $f['sensitive'] ) ) { continue; } $type = Fields::get( $f['type'] ); if ( ! $type || $type->is_empty( $e['response'][ $k ] ) ) { continue; } $rows[] = array( Schema::label( $f ), html_entity_decode( wp_strip_all_tags( $type->format( $e['response'][ $k ], $f, 'text' ) ), ENT_QUOTES, 'UTF-8' ) ); } if ( $e['payment_status'] ) { $rows[] = array( __( 'Payment', 'fieldwren' ), Entries::money( (int) $e['payment_total'], $e['currency'], $e['form_id'] ) . ' (' . $e['payment_status'] . ')' ); } $meta = array( __( 'Submitted from', 'fieldwren' ) => (string) $e['source_url'], __( 'IP address', 'fieldwren' ) => (string) $e['ip'], ); foreach ( $rows as $r ) { $ll = $pdf->wrap( $r[0], 9.5, $labelw, true ); $vl = $pdf->wrap( $r[1], 10, $valw ); $h = max( count( $ll ), count( $vl ) ) * 13.5 + 10; if ( $y + $h > $bottom ) { $pdf->add_page(); $pdf->rect( 0, 0, Pdf::W, 6, $accent ); /* translators: 1: form, 2: entry number */ $pdf->text( $m, 40, sprintf( __( '%1$s – entry #%2$d (continued)', 'fieldwren' ), $title, $e['serial'] ), 9, true, '#6b7280' ); $y = 62; } foreach ( $ll as $i => $l ) { $pdf->text( $m, $y + $i * 13.5, $l, 9.5, true, '#374151' ); } foreach ( $vl as $i => $l ) { $pdf->text( $valx, $y + $i * 13.5, $l, 10 ); } $y += $h; $pdf->line( $m, $y - 9, $right, $y - 9 ); } $y += 6; foreach ( $meta as $k => $v ) { if ( '' === $v ) { continue; } if ( $y + 14 > $bottom ) { $pdf->add_page(); $y = 50; } $pdf->text( $m, $y, $k . ': ' . $v, 8, false, '#9ca3af' ); $y += 12; } } // Footers. $total = $pdf->count(); for ( $i = 0; $i < $total; $i++ ) { $pdf->set_page( $i ); $pdf->text( $m, Pdf::H - 28, get_bloginfo( 'name' ), 8, false, '#9ca3af' ); /* translators: 1: page, 2: pages */ $pdf->text_right( $right, Pdf::H - 28, sprintf( __( 'Page %1$d of %2$d', 'fieldwren' ), $i + 1, $total ), 8, false, '#9ca3af' ); } return $pdf->output( 1 === count( $entries ) ? get_the_title( $entries[0]['form_id'] ) : __( 'Entries', 'fieldwren' ) ); } }