| 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 |
|