0, 'id' => 0, 'fields' => '', 'per_page' => 20, 'status' => 'read,unread', 'search' => 'yes', 'user' => 'all', 'view' => 'yes', 'empty' => __( 'No entries yet.', 'fieldwren' ), ), (array) $atts, 'fwren_entries' ); $form_id = absint( $a['form'] ? $a['form'] : $a['id'] ); if ( ! Form::exists( $form_id ) ) { return current_user_can( 'fwren_manage_forms' ) ? '

' . esc_html__( 'Entries: form not found.', 'fieldwren' ) . '

' : ''; } $own = 'current' === $a['user']; if ( $own && ! is_user_logged_in() ) { return '

' . esc_html__( 'Please log in to see your entries.', 'fieldwren' ) . ' ' . esc_html__( 'Log in', 'fieldwren' ) . '

'; } if ( ! $own && ! current_user_can( 'fwren_view_entries' ) ) { return ''; } wp_enqueue_style( 'fwren-front-entries' ); $schema = Schema::get( $form_id ); $keys = array_values( array_filter( array_map( 'sanitize_key', array_map( 'trim', explode( ',', (string) $a['fields'] ) ) ) ) ); $keys = array_values( array_filter( $keys, static function ( $k ) use ( $schema ) { return isset( $schema['fields'][ $k ] ) && empty( $schema['fields'][ $k ]['sensitive'] ); } ) ); if ( ! $keys ) { foreach ( $schema['order'] as $k ) { $f = $schema['fields'][ $k ]; if ( empty( $f['sensitive'] ) && 'hidden' !== $f['type'] && empty( $f['signature'] ) ) { $keys[] = $k; } if ( count( $keys ) >= 5 ) { break; } } } $statuses = array_values( array_intersect( array_map( 'trim', explode( ',', strtolower( (string) $a['status'] ) ) ), self::STATUSES ) ); $statuses = $statuses ? $statuses : array( 'read', 'unread' ); $per = max( 1, min( 100, (int) $a['per_page'] ) ); $sk = 'fwren_es_' . $form_id; $pk = 'fwren_ep_' . $form_id; $search = 'yes' === $a['search'] && isset( $_GET[ $sk ] ) ? sanitize_text_field( wp_unslash( $_GET[ $sk ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification $page = max( 1, absint( $_GET[ $pk ] ?? 1 ) ); // phpcs:ignore WordPress.Security.NonceVerification list( $rows, $total ) = self::query( $form_id, $statuses, $own ? get_current_user_id() : 0, $search, $per, $page ); $h = '
'; if ( 'yes' === $a['search'] ) { $h .= ''; } if ( ! $rows ) { return $h . '

' . esc_html( $a['empty'] ) . '

'; } $h .= '
'; foreach ( $keys as $k ) { $h .= ''; } if ( 'yes' === $a['view'] ) { $h .= ''; } $h .= ''; $df = get_option( 'date_format' ); foreach ( $rows as $r ) { $h .= ''; foreach ( $keys as $k ) { $f = $schema['fields'][ $k ]; $type = Fields::get( $f['type'] ); $v = $r['response'][ $k ] ?? ''; $h .= ''; } if ( 'yes' === $a['view'] ) { $h .= ''; } $h .= ''; } $h .= '
#' . esc_html__( 'Date', 'fieldwren' ) . '' . esc_html( Schema::label( $schema['fields'][ $k ] ) ) . '' . esc_html__( 'View', 'fieldwren' ) . '
' . (int) $r['serial'] . '' . esc_html( wp_date( $df, strtotime( $r['created_at'] . ' UTC' ) ) ) . '' . ( $type ? wp_kses_post( $type->format( $v, $f, 'html' ) ) : esc_html( is_array( $v ) ? implode( ', ', $v ) : (string) $v ) ) . '' . esc_html__( 'View', 'fieldwren' ) . '
'; $pages = (int) ceil( $total / $per ); if ( $pages > 1 ) { $h .= ''; } return apply_filters( 'fwren/front_entries/html', $h . '', $form_id, $rows, $a ); } /** * Query. * * @return array [ rows, total ] */ private static function query( $form_id, array $statuses, $user_id, $search, $per, $page ) { global $wpdb; $t = Install::table( 'entries' ); $where = 'form_id = %d AND status IN (' . implode( ',', array_fill( 0, count( $statuses ), '%s' ) ) . ')'; $args = array_merge( array( (int) $form_id ), $statuses ); if ( $user_id ) { $where .= ' AND user_id = %d'; $args[] = (int) $user_id; } if ( '' !== $search ) { $where .= ' AND response LIKE %s'; $args[] = '%' . $wpdb->esc_like( $search ) . '%'; } $total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $t WHERE $where", $args ) ); // phpcs:ignore $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $t WHERE $where ORDER BY id DESC LIMIT %d OFFSET %d", array_merge( $args, array( (int) $per, (int) ( ( $page - 1 ) * $per ) ) ) ), ARRAY_A ); // phpcs:ignore foreach ( $rows as &$r ) { $r['response'] = json_decode( (string) $r['response'], true ); $r['response'] = is_array( $r['response'] ) ? $r['response'] : array(); } return array( $rows, $total ); } /** ?fwren_entry=ID&token=… */ public static function public_view() { if ( empty( $_GET['fwren_entry'] ) || empty( $_GET['token'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification return; } $id = absint( $_GET['fwren_entry'] ); // phpcs:ignore WordPress.Security.NonceVerification $token = sanitize_text_field( wp_unslash( $_GET['token'] ) ); // phpcs:ignore WordPress.Security.NonceVerification $entry = $id ? Entries::get( $id ) : null; $saved = $entry ? (string) Entries::get_meta( $id, '_view_token' ) : ''; if ( ! $entry || '' === $saved || ! hash_equals( $saved, $token ) || in_array( $entry['status'], array( 'trash', 'spam' ), true ) ) { Helpers::page( __( 'Entry not found', 'fieldwren' ), '

' . esc_html__( 'This link is invalid or the entry was removed.', 'fieldwren' ) . '

', 404 ); } $form_id = (int) $entry['form_id']; $schema = Schema::get( $form_id ); $date = wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $entry['created_at'] . ' UTC' ) ); $statuses = Entries::statuses(); /* translators: 1: entry number, 2: date */ $html = '

' . esc_html( sprintf( __( 'Entry #%1$d · %2$s', 'fieldwren' ), (int) $entry['serial'], $date ) ); if ( in_array( $entry['status'], array( 'pending', 'unconfirmed', 'declined' ), true ) ) { $html .= ' · ' . esc_html( $statuses[ $entry['status'] ] ?? $entry['status'] ) . ''; } $html .= '

' . Smartcodes::all_data( $schema, (array) $entry['response'], 'html' ); $html = apply_filters( 'fwren/front_entries/view_html', $html, $entry ); Helpers::page( get_the_title( $form_id ), $html ); } }