| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Database Site Actions |
| 4 |
* |
| 5 |
* This file handles all interactions with the Site Actions DB. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MainWP\Dashboard\Module\Log; |
| 11 |
|
| 12 |
use MainWP\Dashboard\MainWP_DB; |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Class Log_DB_Driver_WPDB |
| 21 |
*/ |
| 22 |
class Log_DB_Driver_WPDB implements Log_DB_Driver { |
| 23 |
/** |
| 24 |
* Holds Query class |
| 25 |
* |
| 26 |
* @var Query |
| 27 |
*/ |
| 28 |
protected $query; |
| 29 |
|
| 30 |
/** |
| 31 |
* Hold records table name |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
public $table; |
| 36 |
|
| 37 |
/** |
| 38 |
* Hold meta table name |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public $table_meta; |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Hold wp table name |
| 47 |
* |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
public $table_wp; |
| 51 |
|
| 52 |
/** |
| 53 |
* Class constructor. |
| 54 |
*/ |
| 55 |
public function __construct() { |
| 56 |
global $wpdb; |
| 57 |
$this->query = new Log_Query(); |
| 58 |
$this->table = MainWP_DB::instance()->get_table_name( 'wp_logs' ); |
| 59 |
$this->table_meta = MainWP_DB::instance()->get_table_name( 'wp_logs_meta' ); |
| 60 |
$this->table_wp = MainWP_DB::instance()->get_table_name( 'wp' ); |
| 61 |
|
| 62 |
$wpdb->mainwp_tbl_logs = $this->table; |
| 63 |
$wpdb->mainwp_tbl_logs_meta = $this->table_meta; |
| 64 |
$wpdb->mainwp_tbl_wp = $this->table_wp; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Insert a record. |
| 69 |
* |
| 70 |
* @param array $data Data to insert. |
| 71 |
* |
| 72 |
* @return int |
| 73 |
*/ |
| 74 |
public function insert_record( $data ) { // phpcs:ignore -- NOSONAR - complex method.a |
| 75 |
global $wpdb; |
| 76 |
|
| 77 |
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
$meta = array(); |
| 82 |
if ( array_key_exists( 'meta', $data ) ) { |
| 83 |
$meta = $data['meta']; |
| 84 |
unset( $data['meta'] ); |
| 85 |
} |
| 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 |
|
| 91 |
$result = $wpdb->insert( $this->table, $data ); //phpcs:ignore -- ok. |
| 92 |
if ( ! $result ) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
$record_id = $wpdb->insert_id; |
| 97 |
|
| 98 |
// Insert record meta. |
| 99 |
foreach ( (array) $meta as $key => $vals ) { |
| 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 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
return $record_id; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Insert record meta |
| 115 |
* |
| 116 |
* @param int $record_id Record ID. |
| 117 |
* @param string $key Meta Key. |
| 118 |
* @param string $val Meta Data. |
| 119 |
* |
| 120 |
* @return array |
| 121 |
*/ |
| 122 |
public function insert_meta( $record_id, $key, $val ) { |
| 123 |
global $wpdb; |
| 124 |
|
| 125 |
return $wpdb->replace( //phpcs:ignore -- ok. |
| 126 |
$this->table_meta, |
| 127 |
array( |
| 128 |
'meta_log_id' => $record_id, |
| 129 |
'meta_key' => $key, //phpcs:ignore -- ok. |
| 130 |
'meta_value' => $val, //phpcs:ignore -- ok. |
| 131 |
) |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Retrieve records |
| 137 |
* |
| 138 |
* @param array $args Query arguments. |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public function get_records( $args ) { |
| 143 |
return $this->query->query( $args ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Returns array of existing values for requested column. |
| 148 |
* Used to fill search filters with only used items, instead of all items. |
| 149 |
* |
| 150 |
* GROUP BY allows query to find just the first occurrence of each value in the column, |
| 151 |
* increasing the efficiency of the query. |
| 152 |
* |
| 153 |
* @param string $column Column being filtered. |
| 154 |
* |
| 155 |
* @return array |
| 156 |
*/ |
| 157 |
public function get_column_values( $column ) { |
| 158 |
global $wpdb; |
| 159 |
return (array) $wpdb->get_results( //phpcs:ignore -- ok. |
| 160 |
"SELECT DISTINCT $column FROM $wpdb->mainwp_tbl_logs", // @codingStandardsIgnoreLine can't prepare column name |
| 161 |
'ARRAY_A' |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Public getter to return table names |
| 167 |
* |
| 168 |
* @return array |
| 169 |
*/ |
| 170 |
public function get_table_names() { |
| 171 |
return array( |
| 172 |
$this->table, |
| 173 |
$this->table_meta, |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Purge storage. |
| 179 |
* |
| 180 |
* @param \MainWP\Dashboard\Module\Log\Log_Manager $manager Instance of the Log_Manager. |
| 181 |
*/ |
| 182 |
public function purge_storage( $manager ) { |
| 183 |
// @DEVTODO: Not doing anything here until the deactivation/uninstall flow has been rethought. |
| 184 |
} |
| 185 |
} |
| 186 |
|