' . esc_html( $labels[ $status ] ?? $status ) . ''; } /** @return string One-line summary of an entry (first values). */ public static function summary( array $e, $max = 3 ) { $schema = Schema::get( $e['form_id'] ); $out = array(); foreach ( $schema['order'] as $k ) { if ( ! isset( $e['response'][ $k ] ) ) { continue; } $f = $schema['fields'][ $k ]; $type = Fields::get( $f['type'] ); if ( ! $type || $type->is_empty( $e['response'][ $k ] ) || in_array( $f['type'], array( 'hidden', 'consent' ), true ) ) { continue; } $out[] = wp_strip_all_tags( $type->format( $e['response'][ $k ], $f, 'text' ) ); if ( count( $out ) >= $max ) { break; } } return wp_trim_words( implode( ' · ', $out ), 24 ); } /** @return array Bulk actions. */ public static function bulk_actions( $status ) { $a = array( 'read' => __( 'Mark as read', 'fieldwren' ), 'unread' => __( 'Mark as unread', 'fieldwren' ), 'star' => __( 'Star', 'fieldwren' ), 'unstar' => __( 'Unstar', 'fieldwren' ), 'spam' => __( 'Mark as spam', 'fieldwren' ), 'trash' => __( 'Move to trash', 'fieldwren' ), 'export' => __( 'Export selected (CSV)', 'fieldwren' ), ); if ( in_array( $status, array( 'trash', 'spam' ), true ) ) { $a = array( 'restore' => __( 'Restore', 'fieldwren' ), 'delete' => __( 'Delete permanently', 'fieldwren' ), ); } $a = apply_filters( 'fwren/entries/bulk_actions', $a ); return apply_filters( 'fwren/admin/entry_bulk_actions', $a, $status ); } /** Bulk actions, entry actions, notes, edits. */ public static function handle() { if ( ! isset( $_REQUEST['page'] ) || 'fwren-entries' !== $_REQUEST['page'] ) { // phpcs:ignore return; } // Bulk. if ( ! empty( $_POST['fwren_bulk'] ) && ! empty( $_POST['entry_ids'] ) ) { // phpcs:ignore check_admin_referer( 'fwren_entries_bulk' ); if ( ! current_user_can( 'fwren_manage_entries' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'fieldwren' ) ); } $ids = array_filter( array_map( 'absint', (array) $_POST['entry_ids'] ) ); // phpcs:ignore $action = sanitize_key( wp_unslash( $_POST['fwren_bulk'] ) ); self::apply( $action, $ids ); wp_safe_redirect( remove_query_arg( array( 'entry' ), wp_get_referer() ) ); exit; } // Single entry actions (GET with nonce). if ( ! empty( $_GET['fwren_do'] ) && ! empty( $_GET['entry'] ) ) { // phpcs:ignore $id = absint( $_GET['entry'] ); check_admin_referer( 'fwren_entry_' . $id ); if ( ! current_user_can( 'fwren_manage_entries' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'fieldwren' ) ); } $do = sanitize_key( wp_unslash( $_GET['fwren_do'] ) ); if ( 'resend' === $do ) { $e = Entries::get( $id ); $nid = sanitize_text_field( wp_unslash( $_GET['n'] ?? '' ) ); foreach ( (array) Form::settings( $e['form_id'] )['notifications'] as $n ) { if ( ( $n['id'] ?? '' ) === $nid || '' === $nid ) { Notifications::send( $n, $e, $e['response'] ); } } Entries::add_meta( $id, '_note', __( 'Notification resent.', 'fieldwren' ) ); } elseif ( 'resend_feeds' === $do ) { $e = Entries::get( $id ); if ( class_exists( '\FWREN\Integrations\Manager' ) && method_exists( '\FWREN\Integrations\Manager', 'resend' ) ) { \FWREN\Integrations\Manager::resend( $id ); } else { Submission::run_feeds( $e, $e['response'], array_filter( Form::feeds( $e['form_id'] ), static function ( $f ) { return ! empty( $f['enabled'] ); } ) ); } } else { self::apply( $do, array( $id ) ); } $back = 'delete' === $do ? admin_url( 'admin.php?page=fwren-entries' ) : admin_url( 'admin.php?page=fwren-entries&entry=' . $id ); wp_safe_redirect( $back ); exit; } // Note. if ( ! empty( $_POST['fwren_note'] ) && ! empty( $_POST['entry_id'] ) ) { // phpcs:ignore $id = absint( $_POST['entry_id'] ); check_admin_referer( 'fwren_note_' . $id ); if ( current_user_can( 'fwren_view_entries' ) ) { Entries::add_meta( $id, '_note', sanitize_textarea_field( wp_unslash( $_POST['fwren_note'] ) ) ); } wp_safe_redirect( admin_url( 'admin.php?page=fwren-entries&entry=' . $id ) ); exit; } // Edit. if ( ! empty( $_POST['fwren_edit_entry'] ) ) { // phpcs:ignore $id = absint( $_POST['fwren_edit_entry'] ); check_admin_referer( 'fwren_edit_' . $id ); if ( ! current_user_can( 'fwren_manage_entries' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'fieldwren' ) ); } $e = Entries::get( $id ); $schema = Schema::get( $e['form_id'] ); $posted = (array) wp_unslash( $_POST['v'] ?? array() ); // phpcs:ignore $values = $e['response']; foreach ( $schema['fields'] as $k => $f ) { if ( ! array_key_exists( $k, $posted ) || ! empty( $f['is_upload'] ) || ! empty( $f['sensitive'] ) ) { continue; } $type = Fields::get( $f['type'] ); $raw = $posted[ $k ]; if ( is_string( $raw ) && ( ! empty( $f['multiple'] ) || ! empty( $f['repeater'] ) || in_array( $f['type'], array( 'grid', 'name', 'address', 'ranking' ), true ) ) && '' !== $raw && ( '[' === $raw[0] || '{' === $raw[0] ) ) { $dec = json_decode( $raw, true ); $raw = is_array( $dec ) ? $dec : $raw; } $values[ $k ] = $type ? $type->sanitize( $raw, $f ) : sanitize_text_field( (string) $raw ); } Entries::update( $id, array( 'response' => $values ) ); Entries::add_meta( $id, '_note', __( 'Entry edited.', 'fieldwren' ) ); wp_safe_redirect( admin_url( 'admin.php?page=fwren-entries&entry=' . $id . '&updated=1' ) ); exit; } } /** Apply an action to entries. */ private static function apply( $action, array $ids ) { switch ( $action ) { case 'read': case 'unread': case 'spam': case 'trash': foreach ( $ids as $id ) { $e = Entries::get( $id ); if ( $e && in_array( $action, array( 'spam', 'trash' ), true ) ) { Entries::set_meta( $id, '_prev_status', $e['status'] ); } Entries::update( $id, array( 'status' => $action ) ); } break; case 'restore': foreach ( $ids as $id ) { $prev = Entries::get_meta( $id, '_prev_status' ); Entries::update( $id, array( 'status' => $prev && ! in_array( $prev, array( 'trash', 'spam' ), true ) ? $prev : 'read' ) ); } break; case 'star': case 'unstar': foreach ( $ids as $id ) { Entries::update( $id, array( 'is_favorite' => 'star' === $action ? 1 : 0 ) ); } break; case 'delete': Entries::delete( $ids ); break; case 'export': self::export_ids( $ids ); break; default: do_action( 'fwren/admin/entry_bulk/' . $action, $ids ); do_action( 'fwren/entries/bulk_action', $action, $ids ); } } /** Router. */ public static function page() { if ( ! empty( $_GET['entry'] ) ) { // phpcs:ignore self::detail( absint( $_GET['entry'] ) ); // phpcs:ignore return; } self::listing(); } /** List screen. */ private static function listing() { $form_id = absint( $_GET['form_id'] ?? 0 ); // phpcs:ignore $status = sanitize_key( wp_unslash( $_GET['status'] ?? 'all' ) ); // phpcs:ignore $search = sanitize_text_field( wp_unslash( $_GET['s'] ?? '' ) ); // phpcs:ignore $from = sanitize_text_field( wp_unslash( $_GET['from'] ?? '' ) ); // phpcs:ignore $to = sanitize_text_field( wp_unslash( $_GET['to'] ?? '' ) ); // phpcs:ignore $paged = max( 1, absint( $_GET['paged'] ?? 1 ) ); // phpcs:ignore $per = 20; list( $rows, $total ) = Entries::query( array( 'form_id' => $form_id, 'status' => $status, 'search' => $search, 'date_from' => $from, 'date_to' => $to, 'per_page' => $per, 'page' => $paged ) ); if ( $form_id && Form::exists( $form_id ) ) { Admin::form_header( $form_id, 'entries' ); } else { $form_id = 0; echo '

' . esc_html__( 'Entries', 'fieldwren' ) . '

'; } $counts = Entries::status_counts( $form_id ); $views = array( 'all' => __( 'All', 'fieldwren' ), 'unread' => __( 'Unread', 'fieldwren' ), 'read' => __( 'Read', 'fieldwren' ), 'favorite' => __( 'Starred', 'fieldwren' ), 'pending' => __( 'Awaiting approval', 'fieldwren' ), 'unconfirmed' => __( 'Unconfirmed', 'fieldwren' ), 'partial' => __( 'Partial', 'fieldwren' ), 'declined' => __( 'Declined', 'fieldwren' ), 'spam' => __( 'Spam', 'fieldwren' ), 'trash' => __( 'Trash', 'fieldwren' ) ); $all = 0; foreach ( $counts as $k => $n ) { if ( ! in_array( $k, array( 'trash', 'spam', 'partial', 'favorite', 'declined' ), true ) ) { $all += $n; } } $counts['all'] = $all; echo ''; // phpcs:ignore echo '
– '; echo '' . esc_html__( 'Export CSV', 'fieldwren' ) . ' '; echo 'JSON
'; $cols = array(); if ( $form_id ) { $schema = Schema::get( $form_id ); foreach ( $schema['order'] as $k ) { if ( ! in_array( $schema['fields'][ $k ]['type'], array( 'hidden', 'consent', 'password', 'signature' ), true ) ) { $cols[ $k ] = Schema::label( $schema['fields'][ $k ] ); } if ( count( $cols ) >= 4 ) { break; } } } echo '
'; wp_nonce_field( 'fwren_entries_bulk' ); echo '
'; $pages = (int) ceil( $total / $per ); /* translators: %s count */ echo '
' . esc_html( sprintf( _n( '%s entry', '%s entries', $total, 'fieldwren' ), number_format_i18n( $total ) ) ) . ' ' . ( $pages > 1 ? paginate_links( array( 'base' => add_query_arg( 'paged', '%#%' ), 'total' => $pages, 'current' => $paged ) ) : '' ) . '
'; // phpcs:ignore echo ''; if ( $cols ) { foreach ( $cols as $l ) { echo ''; } } else { echo ''; } echo ''; if ( ! $rows ) { echo ''; } foreach ( $rows as $r ) { $url = admin_url( 'admin.php?page=fwren-entries&entry=' . $r['id'] ); echo ''; if ( $cols ) { $schema = Schema::get( $form_id ); foreach ( array_keys( $cols ) as $k ) { $f = $schema['fields'][ $k ]; $type = Fields::get( $f['type'] ); $val = isset( $r['response'][ $k ] ) && $type ? wp_strip_all_tags( $type->format( $r['response'][ $k ], $f, 'text' ) ) : ''; echo ''; } } else { echo ''; } echo ''; // phpcs:ignore } echo '
#' . esc_html( $l ) . '' . esc_html__( 'Form', 'fieldwren' ) . '' . esc_html__( 'Summary', 'fieldwren' ) . '' . esc_html__( 'Date', 'fieldwren' ) . '' . esc_html__( 'Status', 'fieldwren' ) . '
' . esc_html__( 'No entries found.', 'fieldwren' ) . '
' . ( $r['is_favorite'] ? '★' : '' ) . '#' . (int) $r['serial'] . '' . esc_html( wp_trim_words( $val, 12 ) ) . '' . esc_html( get_the_title( $r['form_id'] ) ) . '' . esc_html( self::summary( $r ) ) . '' . esc_html( self::date( $r['created_at'] ) ) . '' . self::status_badge( $r['status'] ) . ( $r['payment_status'] ? ' ' . esc_html( $r['payment_status'] ) . '' : '' ) . '
'; } /** Entry screen. */ private static function detail( $id ) { $e = Entries::get( $id ); if ( ! $e ) { echo '

' . esc_html__( 'Entry not found.', 'fieldwren' ) . '

'; return; } if ( 'unread' === $e['status'] && current_user_can( 'fwren_manage_entries' ) ) { Entries::update( $id, array( 'status' => 'read' ) ); $e['status'] = 'read'; } $schema = Schema::get( $e['form_id'] ); $edit = isset( $_GET['edit'] ) && current_user_can( 'fwren_manage_entries' ); // phpcs:ignore $nonce = wp_create_nonce( 'fwren_entry_' . $id ); $act = static function ( $do, $extra = array() ) use ( $id, $nonce ) { return add_query_arg( array_merge( array( 'page' => 'fwren-entries', 'entry' => $id, 'fwren_do' => $do, '_wpnonce' => $nonce ), $extra ), admin_url( 'admin.php' ) ); }; global $wpdb; $t = Install::table( 'entries' ); $prev = (int) $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $t WHERE form_id=%d AND id<%d AND status NOT IN ('trash','spam') ORDER BY id DESC LIMIT 1", $e['form_id'], $id ) ); // phpcs:ignore $next = (int) $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $t WHERE form_id=%d AND id>%d AND status NOT IN ('trash','spam') ORDER BY id ASC LIMIT 1", $e['form_id'], $id ) ); // phpcs:ignore Admin::form_header( $e['form_id'], 'entries' ); echo '
← ' . esc_html__( 'All entries', 'fieldwren' ) . ''; if ( $prev ) { echo '‹ ' . esc_html__( 'Previous', 'fieldwren' ) . ' '; } if ( $next ) { echo '' . esc_html__( 'Next', 'fieldwren' ) . ' ›'; } echo '
'; if ( isset( $_GET['updated'] ) ) { // phpcs:ignore echo '

' . esc_html__( 'Entry updated.', 'fieldwren' ) . '

'; } $notices = apply_filters( 'fwren/admin/entry_notices', array( 'optin_sent' => array( 'success', __( 'Confirmation email sent.', 'fieldwren' ) ), 'optin_failed' => array( 'error', __( 'The confirmation email could not be sent.', 'fieldwren' ) ), 'entry_approved' => array( 'success', __( 'Entry approved.', 'fieldwren' ) ), 'entry_declined' => array( 'warning', __( 'Entry declined.', 'fieldwren' ) ), 'entry_review_failed' => array( 'error', __( 'The entry could not be updated.', 'fieldwren' ) ), ) ); $nk = sanitize_key( wp_unslash( $_GET['fwren_notice'] ?? '' ) ); // phpcs:ignore if ( isset( $notices[ $nk ] ) ) { echo '

' . esc_html( $notices[ $nk ][1] ) . '

'; } echo '

' . esc_html( sprintf( /* translators: %d serial */ __( 'Entry #%d', 'fieldwren' ), $e['serial'] ) ) . '

'; if ( current_user_can( 'fwren_manage_entries' ) ) { echo $edit ? '' . esc_html__( 'Cancel', 'fieldwren' ) . '' : '' . esc_html__( 'Edit', 'fieldwren' ) . ''; } echo '
'; if ( $edit ) { echo '
'; wp_nonce_field( 'fwren_edit_' . $id ); echo ''; } echo ''; foreach ( $schema['order'] as $k ) { $f = $schema['fields'][ $k ]; $type = Fields::get( $f['type'] ); $v = $e['response'][ $k ] ?? ''; echo ''; } // Values of fields no longer in the form. foreach ( $e['response'] as $k => $v ) { if ( ! isset( $schema['fields'][ $k ] ) ) { echo ''; } } echo '
' . esc_html( Schema::label( $f ) ) . ''; if ( $edit && empty( $f['is_upload'] ) && empty( $f['sensitive'] ) ) { echo self::edit_control( $k, $f, $v ); // phpcs:ignore } else { echo ! $type || $type->is_empty( $v ) ? '—' : $type->format( $v, $f, 'html' ); // phpcs:ignore } echo '
' . esc_html( $k ) . ' (' . esc_html__( 'removed field', 'fieldwren' ) . ')' . esc_html( is_scalar( $v ) ? (string) $v : wp_json_encode( $v ) ) . '
'; if ( $edit ) { submit_button( __( 'Save entry', 'fieldwren' ) ); echo '
'; } echo '
'; // Notes. echo '

' . esc_html__( 'Notes', 'fieldwren' ) . '

'; wp_nonce_field( 'fwren_note_' . $id ); echo '

    '; foreach ( Entries::meta_rows( $id, '_note' ) as $n ) { $u = $n['user_id'] ? get_userdata( $n['user_id'] ) : null; echo '
  • ' . esc_html( $n['meta_value'] ) . '
    ' . esc_html( ( $u ? $u->display_name : __( 'System', 'fieldwren' ) ) . ' · ' . self::date( $n['created_at'] ) ) . '
  • '; } echo '
'; // Logs. list( $logs ) = Logs::query( array( 'entry_id' => $id, 'per_page' => 50 ) ); if ( $logs ) { echo '

' . esc_html__( 'Activity', 'fieldwren' ) . '

    '; foreach ( $logs as $l ) { echo '
  • ' . esc_html( $l['component'] ) . ' ' . esc_html( $l['title'] ) . ' ' . esc_html( self::date( $l['created_at'] ) ) . '
    ' . esc_html( wp_trim_words( (string) $l['message'], 30 ) ) . '
  • '; } echo '
'; } echo '
'; // Sidebar. $user = $e['user_id'] ? get_userdata( $e['user_id'] ) : null; $ua = (string) $e['user_agent']; echo '
'; } /** Edit control for one value. */ private static function edit_control( $k, array $f, $v ) { $name = 'v[' . $k . ']'; if ( ! empty( $f['options'] ) && empty( $f['repeater'] ) && 'ranking' !== $f['type'] ) { $multi = ! empty( $f['multiple'] ); $h = ''; } if ( in_array( $f['type'], array( 'name', 'address' ), true ) ) { $h = ''; foreach ( (array) $f['parts'] as $p ) { $h .= ' '; } return $h; } if ( is_array( $v ) ) { return ''; } if ( 'textarea' === $f['type'] ) { return ''; } return ''; } /** Export filtered entries. */ public static function export() { check_admin_referer( 'fwren_export_entries' ); if ( ! current_user_can( 'fwren_view_entries' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'fieldwren' ) ); } list( $rows ) = Entries::query( array( 'form_id' => absint( $_GET['form_id'] ?? 0 ), 'status' => sanitize_key( wp_unslash( $_GET['status'] ?? 'all' ) ), 'search' => sanitize_text_field( wp_unslash( $_GET['s'] ?? '' ) ), 'date_from' => sanitize_text_field( wp_unslash( $_GET['from'] ?? '' ) ), 'date_to' => sanitize_text_field( wp_unslash( $_GET['to'] ?? '' ) ), 'per_page' => 0, ) ); self::output( $rows, sanitize_key( wp_unslash( $_GET['format'] ?? 'csv' ) ), absint( $_GET['form_id'] ?? 0 ) ); } /** Export chosen ids. */ private static function export_ids( array $ids ) { list( $rows ) = Entries::query( array( 'ids' => $ids, 'status' => 'all', 'per_page' => 0 ) ); $forms = array_unique( wp_list_pluck( $rows, 'form_id' ) ); self::output( $rows, 'csv', 1 === count( $forms ) ? (int) reset( $forms ) : 0 ); } /** Stream CSV / JSON. */ private static function output( array $rows, $format, $form_id ) { $name = 'entries-' . ( $form_id ? sanitize_title( get_the_title( $form_id ) ) . '-' : '' ) . gmdate( 'Y-m-d' ); nocache_headers(); if ( 'json' === $format ) { header( 'Content-Type: application/json; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . $name . '.json"' ); echo wp_json_encode( $rows, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); // phpcs:ignore exit; } header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . $name . '.csv"' ); $out = fopen( 'php://output', 'w' ); // phpcs:ignore fwrite( $out, "\xEF\xBB\xBF" ); // phpcs:ignore -- Excel UTF-8 BOM. $keys = array(); if ( $form_id ) { $schema = Schema::get( $form_id ); foreach ( $schema['order'] as $k ) { $keys[ $k ] = Schema::label( $schema['fields'][ $k ] ); } } else { foreach ( $rows as $r ) { foreach ( array_keys( $r['response'] ) as $k ) { $keys[ $k ] = $k; } } } fputcsv( $out, array_merge( array( 'ID', 'Serial', 'Form', 'Status', 'Date', 'IP', 'Source URL' ), array_values( $keys ), array( 'Payment', 'Payment status' ) ) ); foreach ( $rows as $r ) { $schema = Schema::get( $r['form_id'] ); $line = array( $r['id'], $r['serial'], get_the_title( $r['form_id'] ), $r['status'], self::date( $r['created_at'] ), $r['ip'], $r['source_url'] ); foreach ( array_keys( $keys ) as $k ) { $v = $r['response'][ $k ] ?? ''; $f = $schema['fields'][ $k ] ?? null; $type = $f ? Fields::get( $f['type'] ) : null; $cell = $type && $f ? wp_strip_all_tags( $type->format( $v, $f, 'text' ) ) : ( is_scalar( $v ) ? (string) $v : wp_json_encode( $v ) ); // Neutralise spreadsheet formulas. if ( '' !== $cell && in_array( $cell[0], array( '=', '+', '-', '@' ), true ) && ! is_numeric( $cell ) ) { $cell = "'" . $cell; } $line[] = $cell; } $line[] = $r['payment_total'] ? Entries::money( (int) $r['payment_total'], $r['currency'], $r['form_id'] ) : ''; $line[] = $r['payment_status']; fputcsv( $out, $line ); } fclose( $out ); // phpcs:ignore exit; } }