← All changes
|
modules/logs/classes/class-log-db-driver-wpdb.php
+149
-129
5.0.3
→
6.2.2
View file →
| @@ -10,156 +10,176 @@ | ||
| 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 { |
| 18 | - /** | |
| 19 | - * Holds Query class | |
| 20 | - * | |
| 21 | - * @var Query | |
| 22 | - */ | |
| 23 | - protected $query; | |
| 23 | + /** | |
| 24 | + * Holds Query class | |
| 25 | + * | |
| 26 | + * @var Query | |
| 27 | + */ | |
| 28 | + protected $query; | |
| 24 | 29 | |
| 25 | - /** | |
| 26 | - * Hold records table name | |
| 27 | - * | |
| 28 | - * @var string | |
| 29 | - */ | |
| 30 | - public $table; | |
| 30 | + /** | |
| 31 | + * Hold records table name | |
| 32 | + * | |
| 33 | + * @var string | |
| 34 | + */ | |
| 35 | + public $table; | |
| 31 | 36 | |
| 32 | - /** | |
| 33 | - * Hold meta table name | |
| 34 | - * | |
| 35 | - * @var string | |
| 36 | - */ | |
| 37 | - public $table_meta; | |
| 37 | + /** | |
| 38 | + * Hold meta table name | |
| 39 | + * | |
| 40 | + * @var string | |
| 41 | + */ | |
| 42 | + public $table_meta; | |
| 38 | 43 | |
| 39 | - /** | |
| 40 | - * Class constructor. | |
| 41 | - */ | |
| 42 | - public function __construct() { | |
| 43 | - global $wpdb; | |
| 44 | - $this->query = new Log_Query( $this ); | |
| 45 | - $this->table = MainWP_DB::instance()->get_table_name( 'wp_logs' ); | |
| 46 | - $this->table_meta = MainWP_DB::instance()->get_table_name( 'wp_logs_meta' ); | |
| 47 | 44 | |
| 48 | - $wpdb->mainwp_tbl_logs = $this->table; | |
| 49 | - $wpdb->mainwp_tbl_logs_meta = $this->table_meta; | |
| 50 | - } | |
| 45 | + /** | |
| 46 | + * Hold wp table name | |
| 47 | + * | |
| 48 | + * @var string | |
| 49 | + */ | |
| 50 | + public $table_wp; | |
| 51 | 51 | |
| 52 | - /** | |
| 53 | - * Insert a record. | |
| 54 | - * | |
| 55 | - * @param array $data Data to insert. | |
| 56 | - * | |
| 57 | - * @return int | |
| 58 | - */ | |
| 59 | - public function insert_record( $data ) { | |
| 60 | - global $wpdb; | |
| 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 | 61 | |
| 62 | - if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { | |
| 63 | - return false; | |
| 64 | - } | |
| 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 | + } | |
| 65 | 66 | |
| 66 | - $meta = array(); | |
| 67 | - if ( array_key_exists( 'meta', $data ) ) { | |
| 68 | - $meta = $data['meta']; | |
| 69 | - unset( $data['meta'] ); | |
| 70 | - } | |
| 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; | |
| 71 | 76 | |
| 72 | - $result = $wpdb->insert( $this->table, $data ); //phpcs:ignore -- ok. | |
| 73 | - if ( ! $result ) { | |
| 74 | - return false; | |
| 75 | - } | |
| 77 | + if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { | |
| 78 | + return false; | |
| 79 | + } | |
| 76 | 80 | |
| 77 | - $record_id = $wpdb->insert_id; | |
| 81 | + $meta = array(); | |
| 82 | + if ( array_key_exists( 'meta', $data ) ) { | |
| 83 | + $meta = $data['meta']; | |
| 84 | + unset( $data['meta'] ); | |
| 85 | + } | |
| 78 | 86 | |
| 79 | - // Insert record meta. | |
| 80 | - 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 ); | |
| 84 | - } | |
| 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 | + } | |
| 87 | 90 | |
| 88 | - return $record_id; | |
| 89 | - } | |
| 91 | + $result = $wpdb->insert( $this->table, $data ); //phpcs:ignore -- ok. | |
| 92 | + if ( ! $result ) { | |
| 93 | + return false; | |
| 94 | + } | |
| 90 | 95 | |
| 91 | - /** | |
| 92 | - * Insert record meta | |
| 93 | - * | |
| 94 | - * @param int $record_id Record ID. | |
| 95 | - * @param string $key Meta Key. | |
| 96 | - * @param string $val Meta Data. | |
| 97 | - * | |
| 98 | - * @return array | |
| 99 | - */ | |
| 100 | - public function insert_meta( $record_id, $key, $val ) { | |
| 101 | - global $wpdb; | |
| 96 | + $record_id = $wpdb->insert_id; | |
| 102 | 97 | |
| 103 | - $result = $wpdb->insert( //phpcs:ignore -- ok. | |
| 104 | - $this->table_meta, | |
| 105 | - array( | |
| 106 | - 'meta_log_id' => $record_id, | |
| 107 | - 'meta_key' => $key, //phpcs:ignore -- ok. | |
| 108 | - 'meta_value' => $val, //phpcs:ignore -- ok. | |
| 109 | - ) | |
| 110 | - ); | |
| 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 | + } | |
| 111 | 112 | |
| 112 | - return $result; | |
| 113 | - } | |
| 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; | |
| 114 | 124 | |
| 115 | - /** | |
| 116 | - * Retrieve records | |
| 117 | - * | |
| 118 | - * @param array $args Query arguments. | |
| 119 | - * | |
| 120 | - * @return array | |
| 121 | - */ | |
| 122 | - public function get_records( $args ) { | |
| 123 | - return $this->query->query( $args ); | |
| 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 | + } | |
| 125 | 134 | |
| 126 | - /** | |
| 127 | - * Returns array of existing values for requested column. | |
| 128 | - * Used to fill search filters with only used items, instead of all items. | |
| 129 | - * | |
| 130 | - * GROUP BY allows query to find just the first occurrence of each value in the column, | |
| 131 | - * increasing the efficiency of the query. | |
| 132 | - * | |
| 133 | - * @param string $column Column being filtered. | |
| 134 | - * | |
| 135 | - * @return array | |
| 136 | - */ | |
| 137 | - public function get_column_values( $column ) { | |
| 138 | - global $wpdb; | |
| 139 | - return (array) $wpdb->get_results( //phpcs:ignore -- ok. | |
| 140 | - "SELECT DISTINCT $column FROM $wpdb->mainwp_tbl_logs", // @codingStandardsIgnoreLine can't prepare column name | |
| 141 | - 'ARRAY_A' | |
| 142 | - ); | |
| 143 | - } | |
| 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 | + } | |
| 144 | 145 | |
| 145 | - /** | |
| 146 | - * Public getter to return table names | |
| 147 | - * | |
| 148 | - * @return array | |
| 149 | - */ | |
| 150 | - public function get_table_names() { | |
| 151 | - return array( | |
| 152 | - $this->table, | |
| 153 | - $this->table_meta, | |
| 154 | - ); | |
| 155 | - } | |
| 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 | + } | |
| 156 | 164 | |
| 157 | - /** | |
| 158 | - * Purge storage. | |
| 159 | - * | |
| 160 | - * @param \MainWP\Dashboard\Module\Log\Log_Manager $manager Instance of the Log_Manager. | |
| 161 | - */ | |
| 162 | - public function purge_storage( $manager ) { | |
| 163 | - // @TODO: Not doing anything here until the deactivation/uninstall flow has been rethought. | |
| 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 | + } | |
| 165 | 185 | } |