| @@ -10,8 +10,13 @@ | ||
| 10 | 10 | namespace MainWP\Dashboard\Module\Log; |
| 11 | 11 | |
| 12 | 12 | use MainWP\Dashboard\MainWP_DB; |
| 13 | 13 | |
| 14 | +// Exit if accessed directly. | |
| 15 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 16 | + exit; | |
| 17 | +} | |
| 18 | + | |
| 14 | 19 | /** |
| 15 | 20 | * Class Log_DB_Driver_WPDB |
| 16 | 21 | */ |
| 17 | 22 | class Log_DB_Driver_WPDB implements Log_DB_Driver { |
| @@ -35,19 +40,29 @@ | ||
| 35 | 40 | * @var string |
| 36 | 41 | */ |
| 37 | 42 | public $table_meta; |
| 38 | 43 | |
| 44 | + | |
| 39 | 45 | /** |
| 46 | + * Hold wp table name | |
| 47 | + * | |
| 48 | + * @var string | |
| 49 | + */ | |
| 50 | + public $table_wp; | |
| 51 | + | |
| 52 | + /** | |
| 40 | 53 | * Class constructor. |
| 41 | 54 | */ |
| 42 | 55 | public function __construct() { |
| 43 | 56 | global $wpdb; |
| 44 | - $this->query = new Log_Query( $this ); | |
| 57 | + $this->query = new Log_Query(); | |
| 45 | 58 | $this->table = MainWP_DB::instance()->get_table_name( 'wp_logs' ); |
| 46 | 59 | $this->table_meta = MainWP_DB::instance()->get_table_name( 'wp_logs_meta' ); |
| 60 | + $this->table_wp = MainWP_DB::instance()->get_table_name( 'wp' ); | |
| 47 | 61 | |
| 48 | 62 | $wpdb->mainwp_tbl_logs = $this->table; |
| 49 | 63 | $wpdb->mainwp_tbl_logs_meta = $this->table_meta; |
| 64 | + $wpdb->mainwp_tbl_wp = $this->table_wp; | |
| 50 | 65 | } |
| 51 | 66 | |
| 52 | 67 | /** |
| 53 | 68 | * Insert a record. |
| @@ -55,9 +70,9 @@ | ||
| 55 | 70 | * @param array $data Data to insert. |
| 56 | 71 | * |
| 57 | 72 | * @return int |
| 58 | 73 | */ |
| 59 | - public function insert_record( $data ) { | |
| 74 | + public function insert_record( $data ) { // phpcs:ignore -- NOSONAR - complex method.a | |
| 60 | 75 | global $wpdb; |
| 61 | 76 | |
| 62 | 77 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 63 | 78 | return false; |
| @@ -68,8 +83,12 @@ | ||
| 68 | 83 | $meta = $data['meta']; |
| 69 | 84 | unset( $data['meta'] ); |
| 70 | 85 | } |
| 71 | 86 | |
| 87 | + if ( ! empty( $data['created'] ) ) { | |
| 88 | + $data['created'] = Log_Manager::normalize_to_microseconds( $data['created'] ); // Store timestamps (including microseconds) as a BIGINT. | |
| 89 | + } | |
| 90 | + | |
| 72 | 91 | $result = $wpdb->insert( $this->table, $data ); //phpcs:ignore -- ok. |
| 73 | 92 | if ( ! $result ) { |
| 74 | 93 | return false; |
| 75 | 94 | } |
| @@ -77,15 +96,18 @@ | ||
| 77 | 96 | $record_id = $wpdb->insert_id; |
| 78 | 97 | |
| 79 | 98 | // Insert record meta. |
| 80 | 99 | foreach ( (array) $meta as $key => $vals ) { |
| 81 | - foreach ( (array) $vals as $val ) { | |
| 82 | - if ( is_scalar( $val ) && '' !== $val ) { | |
| 83 | - $this->insert_meta( $record_id, $key, $val ); | |
| 100 | + if ( is_scalar( $vals ) && '' !== $vals ) { | |
| 101 | + $this->insert_meta( $record_id, $key, $vals ); | |
| 102 | + } else { | |
| 103 | + foreach ( (array) $vals as $key_val => $val ) { | |
| 104 | + if ( is_scalar( $val ) && '' !== $val ) { | |
| 105 | + $this->insert_meta( $record_id, $key_val, $val ); | |
| 106 | + } | |
| 84 | 107 | } |
| 85 | 108 | } |
| 86 | 109 | } |
| 87 | - | |
| 88 | 110 | return $record_id; |
| 89 | 111 | } |
| 90 | 112 | |
| 91 | 113 | /** |
| @@ -99,9 +121,9 @@ | ||
| 99 | 121 | */ |
| 100 | 122 | public function insert_meta( $record_id, $key, $val ) { |
| 101 | 123 | global $wpdb; |
| 102 | 124 | |
| 103 | - return $wpdb->insert( //phpcs:ignore -- ok. | |
| 125 | + return $wpdb->replace( //phpcs:ignore -- ok. | |
| 104 | 126 | $this->table_meta, |
| 105 | 127 | array( |
| 106 | 128 | 'meta_log_id' => $record_id, |
| 107 | 129 | 'meta_key' => $key, //phpcs:ignore -- ok. |