1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
<?php
class PostmanLogFields {
private $fields = array(
'success' => 'sanitize_text_field',
'from_header' => '',
'to_header' => '',
'cc_header' => '',
'bcc_header' => '',
'reply_to_header' => '',
'transport_uri' => 'sanitize_text_field',
'original_to' => 'sanitize_text_field',
'original_subject' => 'sanitize_text_field',
'original_message' => null,
'original_headers' => 'sanitize_text_field',
'session_transcript' => 'sanitize_textarea_field',
);
private static $instance = null;
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new static();
}
return self::$instance;
}
private function __construct()
{
$this->fields['original_message'] = array( $this, 'sanitize_message' );
}
public function get( $post_id ) {
$data = [];
foreach ( $this->fields as $key => $sanitize_callback ) {
$meta = get_post_meta( $post_id, $key, true );
$data[$key][] = $this->maybe_json( $meta );
}
return $data;
}
public function get_fields() {
return $this->fields;
}
public function update( $post_id, $key, $value ) {
$sanitized = $this->sanitize( $key, $value );
$encode = $this->encode( $sanitized );
update_post_meta( $post_id, $key, $encode );
}
private function maybe_json( $json ) {
if ( is_array( $json ) ) {
return implode( ',', $json );
}
if ( $this->isJson( $json ) ) {
return implode( ',', json_decode( $json, true ) );
}
// Fallback
return maybe_unserialize( $json );
}
private function isJson($string) {
$result = json_decode($string, true);
$error = json_last_error();
return ( $error == JSON_ERROR_NONE && ! is_null($result) && $result != $string );
}
private function sanitize( $key, $value ) {
$callback = is_array( $value ) ? 'array_map' : 'call_user_func';
if ( ! empty( $this->fields[$key] ) ) {
return $callback( $this->fields[$key], $value );
}
return filter_var( $value, FILTER_SANITIZE_STRING | FILTER_SANITIZE_SPECIAL_CHARS );
}
private function sanitize_message( $message ) {
$allowed_tags = wp_kses_allowed_html( 'post' );
$allowed_tags['style'] = array();
return wp_kses( $message, $allowed_tags );
}
private function encode( $value ) {
if ( is_array( $value ) ) {
return wp_json_encode( $value );
}
return $value;
}
}
|