prefix . 'stackmime_entries'; } /** * Create or update the table. */ function stackmime_forms_install_table() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = stackmime_forms_table(); $charset = $wpdb->get_charset_collate(); dbDelta( "CREATE TABLE {$table} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, form_id varchar(64) NOT NULL DEFAULT '', status varchar(10) NOT NULL DEFAULT 'new', data longtext NOT NULL, email_status varchar(10) NOT NULL DEFAULT '', email_error text NULL, page_url varchar(500) NOT NULL DEFAULT '', user_agent varchar(255) NOT NULL DEFAULT '', ip_hash char(64) NOT NULL DEFAULT '', spam_reason varchar(50) NOT NULL DEFAULT '', created_at datetime NOT NULL, PRIMARY KEY (id), KEY form_status (form_id, status), KEY created_at (created_at) ) {$charset};" ); update_option( 'stackmime_forms_db_version', STACKMIME_FORMS_VERSION, false ); } /** * Save an entry. Returns the new ID. * * @param array $row Columns. */ function stackmime_forms_insert( $row ) { global $wpdb; $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery stackmime_forms_table(), array( 'form_id' => $row['form_id'], 'status' => $row['status'] ?? 'new', 'data' => wp_json_encode( $row['data'], JSON_UNESCAPED_UNICODE ), 'email_status' => '', 'page_url' => mb_substr( (string) ( $row['page_url'] ?? '' ), 0, 500 ), 'user_agent' => mb_substr( (string) ( $row['user_agent'] ?? '' ), 0, 255 ), 'ip_hash' => $row['ip_hash'] ?? '', 'spam_reason' => $row['spam_reason'] ?? '', 'created_at' => current_time( 'mysql', true ), ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ); wp_cache_delete( 'unread', 'stackmime_forms' ); return (int) $wpdb->insert_id; } /** * One entry, with data decoded. * * @param int $id Entry ID. */ function stackmime_forms_entry( $id ) { global $wpdb; $table = stackmime_forms_table(); $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared if ( $row ) { $row->fields = json_decode( $row->data, true ); if ( ! is_array( $row->fields ) ) { $row->fields = array(); } } return $row; } /** * Update columns of an entry. * * @param int $id Entry ID. * @param array $cols Columns => values. */ function stackmime_forms_update( $id, $cols ) { global $wpdb; $allowed = array_intersect_key( $cols, array_flip( array( 'status', 'email_status', 'email_error' ) ) ); if ( isset( $allowed['status'] ) && ! in_array( $allowed['status'], array( 'new', 'read', 'spam', 'trash' ), true ) ) { unset( $allowed['status'] ); } if ( $allowed ) { $wpdb->update( stackmime_forms_table(), $allowed, array( 'id' => (int) $id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching wp_cache_delete( 'unread', 'stackmime_forms' ); } } /** * Delete an entry permanently. * * @param int $id Entry ID. */ function stackmime_forms_delete( $id ) { global $wpdb; $wpdb->delete( stackmime_forms_table(), array( 'id' => (int) $id ), array( '%d' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching wp_cache_delete( 'unread', 'stackmime_forms' ); } /** * Query entries. * * @param array $args form, status, search, per_page, page. * @return array{rows: array, total: int} */ function stackmime_forms_query( $args ) { global $wpdb; $table = stackmime_forms_table(); $where = array( '1 = %d' ); $params = array( 1 ); if ( ! empty( $args['form'] ) ) { $where[] = 'form_id = %s'; $params[] = $args['form']; } if ( ! empty( $args['status'] ) && 'all' !== $args['status'] ) { $where[] = 'status = %s'; $params[] = $args['status']; } else { $where[] = "status IN ('new','read')"; } if ( ! empty( $args['search'] ) ) { $where[] = 'data LIKE %s'; $params[] = '%' . $wpdb->esc_like( $args['search'] ) . '%'; } $sql_where = implode( ' AND ', $where ); $per_page = max( 1, (int) ( $args['per_page'] ?? 25 ) ); $offset = max( 0, ( (int) ( $args['page'] ?? 1 ) - 1 ) * $per_page ); // Placeholders only: $sql_where is built from fixed strings above, values go through prepare(). // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber $total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table} WHERE {$sql_where}", $params ) ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE {$sql_where} ORDER BY id DESC LIMIT %d OFFSET %d", array_merge( $params, array( $per_page, $offset ) ) ) ); // phpcs:enable foreach ( $rows as $row ) { $row->fields = json_decode( $row->data, true ); if ( ! is_array( $row->fields ) ) { $row->fields = array(); } } return array( 'rows' => $rows, 'total' => $total, ); } /** * Count of unread entries (menu bubble). */ function stackmime_forms_unread_count() { $count = wp_cache_get( 'unread', 'stackmime_forms' ); if ( false === $count ) { global $wpdb; $table = stackmime_forms_table(); $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table} WHERE status = 'new'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared wp_cache_set( 'unread', $count, 'stackmime_forms', 300 ); } return (int) $count; } /** * Status counts for the list screen tabs. */ function stackmime_forms_status_counts() { global $wpdb; $table = stackmime_forms_table(); $rows = $wpdb->get_results( "SELECT status, COUNT(*) AS n FROM {$table} GROUP BY status" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared $out = array( 'new' => 0, 'read' => 0, 'spam' => 0, 'trash' => 0, ); foreach ( $rows as $row ) { $out[ $row->status ] = (int) $row->n; } return $out; } /** * Daily clean-up: spam and trash after 30 days; everything after the retention period if one is set. */ add_action( 'stackmime_forms_cleanup', function () { global $wpdb; $table = stackmime_forms_table(); $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE status IN ('spam','trash') AND created_at < %s", gmdate( 'Y-m-d H:i:s', time() - 30 * DAY_IN_SECONDS ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared $days = (int) stackmime_forms_settings()['retention_days']; if ( $days > 0 ) { $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE created_at < %s", gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared } wp_cache_delete( 'unread', 'stackmime_forms' ); } ); /** * Write entries as CSV (with a UTF-8 marker for Excel) to an open file handle. * Used by the Export button and available to add-ons. * * @param resource $out Writable handle. * @param array $rows Entries from stackmime_forms_query(). */ function stackmime_forms_write_csv( $out, $rows ) { $cols = array(); foreach ( $rows as $row ) { $form = stackmime_forms_get( $row->form_id ); foreach ( ( $form ? array_keys( $form['fields'] ) : array_keys( $row->fields ) ) as $key ) { if ( $form && stackmime_forms_is_display_type( $form['fields'][ $key ]['type'] ) ) { continue; } $cols[ $key ] = $form && isset( $form['fields'][ $key ] ) ? $form['fields'][ $key ]['label'] : $key; } } $safe = function ( $value ) { $value = (string) $value; return preg_match( '/^[=+\-@\t\r]/', $value ) ? "'" . $value : $value; }; fwrite( $out, "\xEF\xBB\xBF" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite fputcsv( $out, array_merge( array( 'ID', __( 'Received', 'stackmime-forms' ), __( 'Form', 'stackmime-forms' ), __( 'Status', 'stackmime-forms' ), __( 'Email alert', 'stackmime-forms' ) ), array_values( $cols ), array( __( 'Page', 'stackmime-forms' ) ) ) ); foreach ( $rows as $row ) { $form = stackmime_forms_get( $row->form_id ); $line = array( $row->id, wp_date( 'Y-m-d H:i', strtotime( $row->created_at . ' UTC' ) ), $form ? $form['title'] : $row->form_id, $row->status, $row->email_status ); foreach ( array_keys( $cols ) as $key ) { $field = $form['fields'][ $key ] ?? array( 'type' => 'text', 'options' => array(), ); $line[] = stackmime_forms_display_value( $field, $row->fields[ $key ] ?? '' ); } $line[] = $row->page_url; fputcsv( $out, array_map( $safe, $line ) ); } }