PluginProbe ʕ •ᴥ•ʔ
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.5
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.5
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.13.0 1.13.1 1.13.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.2 2.6.0
sureforms / inc / abilities / entries / entry-parser.php
sureforms / inc / abilities / entries Last commit date
bulk-get-entries.php 3 weeks ago delete-entry.php 5 months ago entry-parser.php 3 weeks ago get-entry.php 3 weeks ago list-entries.php 3 weeks ago update-entry-status.php 5 months ago
entry-parser.php
125 lines
1 <?php
2 /**
3 * Entry Parser Trait.
4 *
5 * Shared entry parsing logic for abilities that need to transform
6 * raw entry data into structured output with decoded field labels.
7 *
8 * @package sureforms
9 * @since 2.5.2
10 */
11
12 namespace SRFM\Inc\Abilities\Entries;
13
14 use SRFM\Inc\Helper;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // Exit if accessed directly.
18 }
19
20 /**
21 * Entry_Parser trait.
22 *
23 * Provides the shared parse_entry() method used by
24 * Get_Entry and Bulk_Get_Entries abilities.
25 *
26 * @since 2.5.2
27 */
28 trait Entry_Parser {
29 /**
30 * Parse a raw entry array into the standard response shape.
31 *
32 * Handles form data decoding, form title lookup,
33 * submission info building (with IP masking), and user info.
34 *
35 * @param array<string,mixed> $entry Raw entry from the database.
36 * @since 2.5.2
37 * @return array<string,mixed> Parsed entry data (without entry_id — caller prepends it).
38 */
39 protected function parse_entry( array $entry ) {
40 // Parse form data with decoded labels (submitter-controlled; escape at the sink).
41 $form_data = [];
42 $excluded_fields = Helper::get_excluded_fields();
43 $entry_form_data = $entry['form_data'] ?? [];
44
45 if ( is_array( $entry_form_data ) ) {
46 foreach ( $entry_form_data as $field_name => $value ) {
47 if ( ! is_string( $field_name ) || in_array( $field_name, $excluded_fields, true ) ) {
48 continue;
49 }
50 if ( false === str_contains( $field_name, '-lbl-' ) ) {
51 continue;
52 }
53
54 $label_parts = explode( '-lbl-', $field_name );
55 $label = isset( $label_parts[1] ) ? explode( '-', $label_parts[1] )[0] : '';
56 $label = $label ? Helper::decode( $label ) : '';
57 $field_block_name = Helper::get_block_name_from_field( $field_name );
58
59 $form_data[] = [
60 'label' => $label,
61 'value' => $value,
62 'block_name' => $field_block_name,
63 ];
64 }
65 }
66
67 // Get form info.
68 $raw_form_id = $entry['form_id'] ?? 0;
69 $form_id = absint( is_numeric( $raw_form_id ) ? (int) $raw_form_id : 0 );
70 $form_title = get_post_field( 'post_title', $form_id );
71 // Translators: %d is the form ID.
72 $form_name = ! empty( $form_title ) ? $form_title : sprintf( __( 'SureForms Form #%d', 'sureforms' ), $form_id );
73
74 // Build submission info with IP masking.
75 $submission_info_raw = is_array( $entry['submission_info'] ?? null ) ? $entry['submission_info'] : [];
76 $ip = (string) ( $submission_info_raw['user_ip'] ?? '' );
77 if ( ! empty( $ip ) ) {
78 if ( str_contains( $ip, ':' ) ) {
79 // IPv6: keep first segment, mask rest.
80 $parts = explode( ':', $ip );
81 $ip = count( $parts ) > 1 ? $parts[0] . ':*:*:*:*:*:*:*' : '***';
82 } else {
83 // IPv4: keep first octet, mask rest.
84 $parts = explode( '.', $ip );
85 $ip = count( $parts ) === 4 ? $parts[0] . '.*.*.*' : '***';
86 }
87 }
88
89 $submission_info = [
90 'user_ip' => $ip,
91 'browser_name' => (string) ( $submission_info_raw['browser_name'] ?? '' ),
92 'device_name' => (string) ( $submission_info_raw['device_name'] ?? '' ),
93 // Re-sanitize at the exposure boundary in case the stored value
94 // was written by a future code path that bypasses form-submit.php.
95 'submission_url' => esc_url_raw( (string) ( $submission_info_raw['submission_url'] ?? '' ), [ 'http', 'https' ] ),
96 ];
97
98 // Build user info.
99 $user_id = Helper::get_integer_value( $entry['user_id'] ?? 0 );
100 $user_info = null;
101
102 if ( 0 !== $user_id ) {
103 $user_data = get_userdata( $user_id );
104
105 if ( $user_data ) {
106 $user_info = [
107 'id' => $user_id,
108 'display_name' => $user_data->display_name,
109 'profile_url' => get_author_posts_url( $user_id ),
110 ];
111 }
112 }
113
114 return [
115 'form_id' => $form_id,
116 'form_name' => $form_name,
117 'status' => $entry['status'] ?? '',
118 'created_at' => $entry['created_at'] ?? '',
119 'form_data' => $form_data,
120 'submission_info' => $submission_info,
121 'user' => $user_info,
122 ];
123 }
124 }
125