PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.8.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.8.2
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 All 96 releases
sureforms / inc / abilities / entries / entry-parser.php
entry-parser.php
122 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 decryption, 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 decrypted labels.
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::decrypt( $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 ];
94
95 // Build user info.
96 $user_id = Helper::get_integer_value( $entry['user_id'] ?? 0 );
97 $user_info = null;
98
99 if ( 0 !== $user_id ) {
100 $user_data = get_userdata( $user_id );
101
102 if ( $user_data ) {
103 $user_info = [
104 'id' => $user_id,
105 'display_name' => $user_data->display_name,
106 'profile_url' => get_author_posts_url( $user_id ),
107 ];
108 }
109 }
110
111 return [
112 'form_id' => $form_id,
113 'form_name' => $form_name,
114 'status' => $entry['status'] ?? '',
115 'created_at' => $entry['created_at'] ?? '',
116 'form_data' => $form_data,
117 'submission_info' => $submission_info,
118 'user' => $user_info,
119 ];
120 }
121 }
122