isset( $_GET['status'] ) ? sanitize_key( wp_unslash( $_GET['status'] ) ) : '', 'category' => isset( $_GET['category'] ) ? sanitize_key( wp_unslash( $_GET['category'] ) ) : '', 'form_id' => isset( $_GET['form_id'] ) ? absint( wp_unslash( $_GET['form_id'] ) ) : 0, 'search' => isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : '', 'paged' => isset( $_GET['paged'] ) ? max( 1, absint( wp_unslash( $_GET['paged'] ) ) ) : 1, ); // phpcs:enable WordPress.Security.NonceVerification.Recommended if ( ! array_key_exists( $filters['status'], acscf_get_statuses() ) ) { $filters['status'] = ''; } if ( ! array_key_exists( $filters['category'], acscf_get_categories() ) ) { $filters['category'] = ''; } return $filters; } /** * Query submissions for the list screen. * * @param array $filters Filters from acscf_get_list_filters(). * @param int $per_page Rows per page. * @return WP_Query */ function acscf_query_entries( $filters, $per_page = 20 ) { $args = array( 'post_type' => 'acscf_entry', 'post_status' => 'private', 'posts_per_page' => (int) $per_page, 'paged' => max( 1, (int) $filters['paged'] ), 'orderby' => 'date', 'order' => 'DESC', ); $meta_query = array(); if ( '' !== $filters['status'] ) { $meta_query[] = array( 'key' => '_acscf_status', 'value' => $filters['status'], ); } if ( '' !== $filters['category'] ) { $meta_query[] = array( 'key' => '_acscf_category', 'value' => $filters['category'], ); } if ( $filters['form_id'] > 0 ) { $meta_query[] = array( 'key' => '_acscf_form_id', 'value' => (int) $filters['form_id'], ); } if ( ! empty( $meta_query ) ) { $meta_query['relation'] = 'AND'; $args['meta_query'] = $meta_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Admin listing of a small, plugin owned post type. } if ( '' !== $filters['search'] ) { $args['s'] = $filters['search']; } return new WP_Query( $args ); } /** * Extend the admin search of stored submissions to the saved field values. * * @param string $search Search SQL. * @param WP_Query $wp_query Query object. * @return string */ function acscf_extend_entry_search( $search, $wp_query ) { global $wpdb; if ( 'acscf_entry' !== $wp_query->get( 'post_type' ) ) { return $search; } $term = $wp_query->get( 's' ); if ( ! is_string( $term ) || '' === $term ) { return $search; } $like = '%' . $wpdb->esc_like( $term ) . '%'; $ids = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Searching plugin owned meta rows for one admin screen. $wpdb->prepare( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key IN ( '_acscf_name', '_acscf_email', '_acscf_subject', '_acscf_message', '_acscf_form_name' ) AND meta_value LIKE %s", $like ) ); if ( empty( $ids ) ) { return $search; } $ids = implode( ',', array_map( 'absint', $ids ) ); $search = preg_replace( '/^\s*AND\s*\(/', " AND ( {$wpdb->posts}.ID IN ({$ids}) OR ", $search, 1 ); return $search; } add_filter( 'posts_search', 'acscf_extend_entry_search', 10, 2 ); /** * Render the inbox screen: either the list or one submission. * * @return void */ function acscf_render_entries_screen() { if ( ! current_user_can( acscf_manage_capability() ) ) { wp_die( esc_html__( 'You do not have permission to view this screen.', 'acs-contact-form' ) ); } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read only screen switch. $entry_id = isset( $_GET['entry'] ) ? absint( wp_unslash( $_GET['entry'] ) ) : 0; if ( $entry_id > 0 ) { acscf_render_entry_detail( $entry_id ); return; } acscf_render_entries_list(); } /** * Show the admin notice that belongs to the last action. * * @return void */ function acscf_render_admin_notice() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display only message code. $code = isset( $_GET['acscf_msg'] ) ? sanitize_key( wp_unslash( $_GET['acscf_msg'] ) ) : ''; $messages = array( 'updated' => __( 'Saved.', 'acs-contact-form' ), 'deleted' => __( 'Deleted.', 'acs-contact-form' ), 'noted' => __( 'Note added.', 'acs-contact-form' ), 'replied' => __( 'Marked as replied.', 'acs-contact-form' ), 'nochange' => __( 'Nothing was changed.', 'acs-contact-form' ), ); if ( ! isset( $messages[ $code ] ) ) { return; } printf( '
%s
' . esc_html__( 'Inquiry not found.', 'acs-contact-form' ) . '
'; return; } $statuses = acscf_get_statuses(); $categories = acscf_get_categories(); $ai_enabled = acscf_ai_is_enabled(); $mail_subject = sprintf( /* translators: %s: original subject. */ __( 'Re: %s', 'acs-contact-form' ), '' !== $entry['subject'] ? $entry['subject'] : $entry['title'] ); $mailto = ''; if ( is_email( $entry['email'] ) ) { $mailto = 'mailto:' . rawurlencode( $entry['email'] ) . '?subject=' . rawurlencode( $mail_subject ) . '&body=' . rawurlencode( $entry['draft'] ); } ?> 403 ) ); } } /** * Save the status and category selects of the list screen. * * @return void */ function acscf_handle_bulk_update() { check_admin_referer( 'acscf_bulk_update' ); acscf_require_manage_cap(); $changed = false; if ( isset( $_POST['status'] ) && is_array( $_POST['status'] ) ) { $statuses = map_deep( wp_unslash( $_POST['status'] ), 'sanitize_key' ); foreach ( $statuses as $entry_id => $status ) { if ( acscf_set_entry_status( absint( $entry_id ), $status ) ) { $changed = true; } } } if ( isset( $_POST['category'] ) && is_array( $_POST['category'] ) ) { $categories = map_deep( wp_unslash( $_POST['category'] ), 'sanitize_key' ); foreach ( $categories as $entry_id => $category ) { if ( acscf_set_entry_category( absint( $entry_id ), $category ) ) { $changed = true; } } } $redirect = isset( $_POST['redirect_to'] ) ? esc_url_raw( wp_unslash( $_POST['redirect_to'] ) ) : acscf_entries_admin_url(); if ( 0 !== strpos( $redirect, admin_url() ) ) { $redirect = acscf_entries_admin_url(); } acscf_redirect_with_message( $redirect, $changed ? 'updated' : 'nochange' ); } add_action( 'admin_post_acscf_bulk_update', 'acscf_handle_bulk_update' ); /** * Save the status and category of one submission. * * @return void */ function acscf_handle_update_entry() { check_admin_referer( 'acscf_update_entry' ); acscf_require_manage_cap(); $entry_id = isset( $_POST['entry'] ) ? absint( wp_unslash( $_POST['entry'] ) ) : 0; $status = isset( $_POST['status'] ) ? sanitize_key( wp_unslash( $_POST['status'] ) ) : 'new'; if ( ! empty( $_POST['mark_replied'] ) ) { $status = 'done'; } acscf_set_entry_status( $entry_id, $status ); if ( isset( $_POST['category'] ) ) { acscf_set_entry_category( $entry_id, sanitize_key( wp_unslash( $_POST['category'] ) ) ); } $code = ! empty( $_POST['mark_replied'] ) ? 'replied' : 'updated'; acscf_redirect_with_message( acscf_entry_admin_url( $entry_id ), $code ); } add_action( 'admin_post_acscf_update_entry', 'acscf_handle_update_entry' ); /** * Save the reply draft. * * @return void */ function acscf_handle_save_draft() { check_admin_referer( 'acscf_save_draft' ); acscf_require_manage_cap(); $entry_id = isset( $_POST['entry'] ) ? absint( wp_unslash( $_POST['entry'] ) ) : 0; if ( 'acscf_entry' !== get_post_type( $entry_id ) ) { acscf_redirect_with_message( acscf_entries_admin_url(), 'nochange' ); } $draft = isset( $_POST['draft'] ) ? sanitize_textarea_field( wp_unslash( $_POST['draft'] ) ) : ''; update_post_meta( $entry_id, '_acscf_draft', $draft ); acscf_redirect_with_message( acscf_entry_admin_url( $entry_id ), 'updated' ); } add_action( 'admin_post_acscf_save_draft', 'acscf_handle_save_draft' ); /** * Append a note to the reply history. * * @return void */ function acscf_handle_add_note() { check_admin_referer( 'acscf_add_note' ); acscf_require_manage_cap(); $entry_id = isset( $_POST['entry'] ) ? absint( wp_unslash( $_POST['entry'] ) ) : 0; $note = isset( $_POST['note'] ) ? sanitize_textarea_field( wp_unslash( $_POST['note'] ) ) : ''; acscf_add_entry_note( $entry_id, $note, get_current_user_id() ); acscf_redirect_with_message( acscf_entry_admin_url( $entry_id ), 'noted' ); } add_action( 'admin_post_acscf_add_note', 'acscf_handle_add_note' ); /** * Delete one submission and its files. * * @return void */ function acscf_handle_delete_entry() { check_admin_referer( 'acscf_delete_entry' ); acscf_require_manage_cap(); $entry_id = isset( $_GET['entry'] ) ? absint( wp_unslash( $_GET['entry'] ) ) : 0; $post = get_post( $entry_id ); if ( $post && 'acscf_entry' === $post->post_type ) { wp_delete_post( $entry_id, true ); } acscf_redirect_with_message( acscf_entries_admin_url(), 'deleted' ); } add_action( 'admin_post_acscf_delete_entry', 'acscf_handle_delete_entry' ); /** * Serve one stored attachment to an authorised user. * * @return void */ function acscf_handle_download() { check_admin_referer( 'acscf_download' ); acscf_require_manage_cap(); $entry_id = isset( $_GET['entry'] ) ? absint( wp_unslash( $_GET['entry'] ) ) : 0; $index = isset( $_GET['file'] ) ? absint( wp_unslash( $_GET['file'] ) ) : 0; $entry = acscf_get_entry( $entry_id ); if ( ! $entry || ! isset( $entry['attachments'][ $index ]['file'] ) ) { wp_die( esc_html__( 'File not found.', 'acs-contact-form' ), '', array( 'response' => 404 ) ); } $file = $entry['attachments'][ $index ]['file']; $storage = acscf_get_storage_dir(); $base = wp_normalize_path( trailingslashit( $storage['path'] ) ); $target = wp_normalize_path( $file ); if ( 0 !== strpos( $target, $base ) || false !== strpos( $target, '../' ) || ! file_exists( $target ) ) { wp_die( esc_html__( 'File not found.', 'acs-contact-form' ), '', array( 'response' => 404 ) ); } $filetype = wp_check_filetype( $target ); $name = $entry['attachments'][ $index ]['name']; nocache_headers(); header( 'Content-Type: ' . ( $filetype['type'] ? $filetype['type'] : 'application/octet-stream' ) ); header( 'Content-Disposition: attachment; filename="' . rawurlencode( $name ) . '"' ); header( 'Content-Length: ' . filesize( $target ) ); header( 'X-Content-Type-Options: nosniff' ); readfile( $target ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- Streaming a stored attachment as a download; the contents must not be altered. exit; } add_action( 'admin_post_acscf_download', 'acscf_handle_download' ); /** * Export the current list as CSV. * * @return void */ function acscf_handle_export_csv() { check_admin_referer( 'acscf_export_csv' ); acscf_require_manage_cap(); $filters = acscf_get_list_filters(); $filters['paged'] = 1; $query = acscf_query_entries( $filters, 2000 ); $rows = array(); $rows[] = array( __( 'ID', 'acs-contact-form' ), __( 'Received', 'acs-contact-form' ), __( 'Form name', 'acs-contact-form' ), __( 'From', 'acs-contact-form' ), __( 'Email address', 'acs-contact-form' ), __( 'Subject', 'acs-contact-form' ), __( 'Category', 'acs-contact-form' ), __( 'Status', 'acs-contact-form' ), __( 'Replied', 'acs-contact-form' ), __( 'Message body', 'acs-contact-form' ), ); foreach ( $query->posts as $post ) { $entry = acscf_get_entry( $post->ID ); if ( ! $entry ) { continue; } $body = array(); foreach ( $entry['fields'] as $field ) { $body[] = $field['label'] . ': ' . $field['value']; } $rows[] = array( $entry['id'], $entry['created'], $entry['form_name'], $entry['name'], $entry['email'], $entry['subject'], acscf_category_label( $entry['category'] ), acscf_status_label( $entry['status'] ), $entry['replied_at'], implode( " \n", $body ), ); } wp_reset_postdata(); $csv = "\xEF\xBB\xBF"; foreach ( $rows as $row ) { $csv .= implode( ',', array_map( 'acscf_csv_cell', $row ) ) . "\r\n"; } $filename = 'acs-contact-form-' . gmdate( 'Ymd-His' ) . '.csv'; nocache_headers(); header( 'Content-Type: text/csv; charset=UTF-8' ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); echo $csv; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSV file download built by acscf_csv_cell(); HTML escaping would corrupt it. exit; } add_action( 'admin_post_acscf_export_csv', 'acscf_handle_export_csv' ); /** * Quote one CSV cell, neutralising spreadsheet formula injection. * * @param mixed $value Cell value. * @return string */ function acscf_csv_cell( $value ) { $value = (string) $value; if ( '' !== $value && in_array( $value[0], array( '=', '+', '-', '@', "\t", "\r" ), true ) ) { $value = "'" . $value; } return '"' . str_replace( '"', '""', $value ) . '"'; }