/var/www/html_us/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
<?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 ) {

        
array_walk(
            
$data,
            static function ( &
$value ) {
                if ( 
is_array$value ) ) {
                    
$value maybe_serialize$value );
                }
            }
        );

        
$result_set $this->db->insert(
            
$this->table,
            
$data,
            
array_fill0count$data ), '%s' )
        );

        if ( ! 
$result_set ) {
            
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
            
error_log'WP SMTP Log insert error: ' $this->db->last_error );
            
// @TODO: do we have a better way to log errors?

            
return false;
        }

        return 
$this->db->insert_id;
    }

    public function 
update$data$where = [] ): void {
        
array_walk(
            
$data,
            static function ( &
$value ) {
                if ( 
is_array$value ) ) {
                    
$value maybe_serialize$value );
                }
            }
        );

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