/var/www/html_nl/wp-content/plugins/wp-smtp/inc/Logger/Db.php


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
100
101
102
103
104
105
106
107
108
109
110
<?php

namespace WPSMTP\Logger;

class 
Db {

    private 
$db;

    private 
$table;

    private static 
$instance;

    public static function 
get_instance() {

        if ( ! 
self::$instance ) {
            
self::$instance = new static();
        }

        return 
self::$instance;
    }

    private function 
__construct() {
        global 
$wpdb;

        
$this->db    $wpdb;
        
$this->table $wpdb->prefix 'wpsmtp_logs';
    }

    public function 
insert$data ) {
        
$prepared   $this->prepare_for_database$data );
        
$result_set $this->db->insert(
            
$this->table,
            
$prepared,
            
array_fill0count$prepared ), '%s' )
        );

        if ( ! 
$result_set ) {
            
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
            
error_log'WP SMTP Log insert error: ' $this->db->last_error );

            return 
false;
        }

        return 
$this->db->insert_id;
    }

    public function 
update$data$where = [] ): void {
        
$prepared $this->prepare_for_database$data );

        
$this->db->update(
            
$this->table,
            
$prepared,
            
$where,
            
array_fill0count$prepared ), '%s' ),
            [ 
'%d' ]
        );
    }

    
/**
     * This function takes the raw values passed to {@see wp_mail()}
     * and applies similar normalization to what Core does.
     *
     * @param array $raw
     *
     * @return array
     */
    
protected function prepare_for_database( array $raw ) {
        
$to      $raw['to'] ?? [];
        
$subject = (string) ( $raw['subject'] ?? '' );
        
$message = (string) ( $raw['message'] ?? '' );
        
$headers $raw['headers'] ?? [];

        if ( ! 
is_array$to ) ) {
            
$to explode','$to );
        }

        if ( ! 
is_array$headers ) ) {
            
$headers explode"\n"str_replace"\r\n""\n"$headers ) );
        }

        
$headers $this->parse_headers$headers );

        return [
            
'to'      => wp_json_encode$to ),
            
'subject' => $subject,
            
'message' => $message,
            
'headers' => wp_json_encode$headers ),
            
'error'   => isset( $raw['error'] ) ? sanitize_text_field$raw['error'] ) : '',
        ];
    }

    protected function 
parse_headers( array $headers ) {
        
$parsed = [];

        foreach ( 
$headers as $header ) {
            if ( 
strpos$header':' ) === false ) {
                continue;
            }

            [ 
$name$content ] = explode':'trim$header ), );

            
$name    trim$name );
            
$content trim$content );

            
$parsedstrtolower$name ) ] = $content;
        }

        return 
$parsed;
    }
}