| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Give\Log; |
| 4 | 4 | |
| 5 | -use DateTime; | |
| 5 | +use Give\Framework\Exceptions\Primitives\InvalidArgumentException; | |
| 6 | 6 | use Give\Framework\Database\DB; |
| 7 | -use Give\Framework\Exceptions\Primitives\InvalidArgumentException; | |
| 8 | 7 | use WP_REST_Request; |
| 8 | +use DateTime; | |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * Class LogRepository |
| 12 | 12 | * @package Give\Log\Repositories |
| @@ -12,438 +12,420 @@ | ||
| 12 | 12 | * @package Give\Log\Repositories |
| 13 | 13 | * |
| 14 | 14 | * @since 2.10.0 |
| 15 | 15 | */ |
| 16 | -class LogRepository | |
| 17 | -{ | |
| 16 | +class LogRepository { | |
| 18 | 17 | |
| 19 | - const SORTABLE_COLUMNS = ['id', 'category', 'source', 'log_type', 'date']; | |
| 18 | + const SORTABLE_COLUMNS = [ 'id', 'category', 'source', 'log_type', 'date' ]; | |
| 20 | 19 | |
| 21 | - const LOGS_PER_PAGE = 10; | |
| 20 | + const LOGS_PER_PAGE = 10; | |
| 22 | 21 | |
| 23 | - /** | |
| 24 | - * @var string | |
| 25 | - */ | |
| 26 | - private $log_table; | |
| 22 | + /** | |
| 23 | + * @var string | |
| 24 | + */ | |
| 25 | + private $log_table; | |
| 27 | 26 | |
| 28 | - /** | |
| 29 | - * LogRepository constructor. | |
| 30 | - */ | |
| 31 | - public function __construct() | |
| 32 | - { | |
| 33 | - global $wpdb; | |
| 34 | - $this->log_table = "{$wpdb->prefix}give_log"; | |
| 35 | - } | |
| 27 | + /** | |
| 28 | + * LogRepository constructor. | |
| 29 | + */ | |
| 30 | + public function __construct() { | |
| 31 | + global $wpdb; | |
| 32 | + $this->log_table = "{$wpdb->prefix}give_log"; | |
| 33 | + } | |
| 36 | 34 | |
| 37 | - /** | |
| 38 | - * Insert Log into database | |
| 39 | - * | |
| 40 | - * @param LogModel $model | |
| 41 | - * | |
| 42 | - * @return int inserted log id | |
| 43 | - */ | |
| 44 | - public function insertLog(LogModel $model) | |
| 45 | - { | |
| 46 | - DB::insert( | |
| 47 | - $this->log_table, | |
| 48 | - [ | |
| 49 | - 'log_type' => $model->getType(), | |
| 50 | - 'data' => $model->getData($jsonEncode = true), | |
| 51 | - 'category' => $model->getCategory(), | |
| 52 | - 'source' => $model->getSource(), | |
| 53 | - 'date' => $model->getDate(), | |
| 54 | - ], | |
| 55 | - null | |
| 56 | - ); | |
| 35 | + /** | |
| 36 | + * Insert Log into database | |
| 37 | + * | |
| 38 | + * @param LogModel $model | |
| 39 | + * | |
| 40 | + * @return int inserted log id | |
| 41 | + */ | |
| 42 | + public function insertLog( LogModel $model ) { | |
| 43 | + DB::insert( | |
| 44 | + $this->log_table, | |
| 45 | + [ | |
| 46 | + 'log_type' => $model->getType(), | |
| 47 | + 'data' => $model->getData( $jsonEncode = true ), | |
| 48 | + 'category' => $model->getCategory(), | |
| 49 | + 'source' => $model->getSource(), | |
| 50 | + 'date' => $model->getDate(), | |
| 51 | + ], | |
| 52 | + null | |
| 53 | + ); | |
| 57 | 54 | |
| 58 | - return DB::last_insert_id(); | |
| 59 | - } | |
| 55 | + return DB::last_insert_id(); | |
| 56 | + } | |
| 60 | 57 | |
| 61 | - /** | |
| 62 | - * Update log | |
| 63 | - * | |
| 64 | - * @param LogModel $model | |
| 65 | - * | |
| 66 | - * @return false|int | |
| 67 | - */ | |
| 68 | - public function updateLog(LogModel $model) | |
| 69 | - { | |
| 70 | - return DB::update( | |
| 71 | - $this->log_table, | |
| 72 | - [ | |
| 73 | - 'log_type' => $model->getType(), | |
| 74 | - 'data' => $model->getData($jsonEncode = true), | |
| 75 | - 'category' => $model->getCategory(), | |
| 76 | - 'source' => $model->getSource(), | |
| 77 | - 'date' => $model->getDate(), | |
| 78 | - ], | |
| 79 | - [ | |
| 80 | - 'id' => $model->getId(), | |
| 81 | - ] | |
| 82 | - ); | |
| 83 | - } | |
| 58 | + /** | |
| 59 | + * Update log | |
| 60 | + * | |
| 61 | + * @param LogModel $model | |
| 62 | + * | |
| 63 | + * @return false|int | |
| 64 | + */ | |
| 65 | + public function updateLog( LogModel $model ) { | |
| 66 | + return DB::update( | |
| 67 | + $this->log_table, | |
| 68 | + [ | |
| 69 | + 'log_type' => $model->getType(), | |
| 70 | + 'data' => $model->getData( $jsonEncode = true ), | |
| 71 | + 'category' => $model->getCategory(), | |
| 72 | + 'source' => $model->getSource(), | |
| 73 | + 'date' => $model->getDate(), | |
| 74 | + ], | |
| 75 | + [ | |
| 76 | + 'id' => $model->getId(), | |
| 77 | + ] | |
| 78 | + ); | |
| 79 | + } | |
| 84 | 80 | |
| 85 | - /** | |
| 86 | - * Get all logs | |
| 87 | - * | |
| 88 | - * @return LogModel[] | |
| 89 | - */ | |
| 90 | - public function getLogs() | |
| 91 | - { | |
| 92 | - $logs = []; | |
| 93 | - $result = DB::get_results("SELECT * FROM {$this->log_table} ORDER BY id DESC"); | |
| 81 | + /** | |
| 82 | + * Get all logs | |
| 83 | + * | |
| 84 | + * @return LogModel[] | |
| 85 | + */ | |
| 86 | + public function getLogs() { | |
| 87 | + $logs = []; | |
| 88 | + $result = DB::get_results( "SELECT * FROM {$this->log_table} ORDER BY id DESC" ); | |
| 94 | 89 | |
| 95 | - if ($result) { | |
| 96 | - foreach ($result as $log) { | |
| 97 | - $data = json_decode($log->data, true); | |
| 90 | + if ( $result ) { | |
| 91 | + foreach ( $result as $log ) { | |
| 92 | + $data = json_decode( $log->data, true ); | |
| 98 | 93 | |
| 99 | - $logs[] = LogFactory::make( | |
| 100 | - $log->log_type, | |
| 101 | - $data['message'], | |
| 102 | - $log->category, | |
| 103 | - $log->source, | |
| 104 | - $data['context'], | |
| 105 | - $log->id, | |
| 106 | - $log->date | |
| 107 | - ); | |
| 108 | - } | |
| 109 | - } | |
| 94 | + $logs[] = LogFactory::make( | |
| 95 | + $log->log_type, | |
| 96 | + $data['message'], | |
| 97 | + $log->category, | |
| 98 | + $log->source, | |
| 99 | + $data['context'], | |
| 100 | + $log->id, | |
| 101 | + $log->date | |
| 102 | + ); | |
| 103 | + } | |
| 104 | + } | |
| 110 | 105 | |
| 111 | - return $logs; | |
| 112 | - } | |
| 106 | + return $logs; | |
| 107 | + } | |
| 113 | 108 | |
| 114 | - /** | |
| 115 | - * Get all logs for request | |
| 116 | - * | |
| 117 | - * @param WP_REST_Request $request | |
| 118 | - * | |
| 119 | - * @return LogModel[] | |
| 120 | - */ | |
| 121 | - public function getLogsForRequest(WP_REST_Request $request) | |
| 122 | - { | |
| 123 | - $logs = []; | |
| 109 | + /** | |
| 110 | + * Get all logs for request | |
| 111 | + * | |
| 112 | + * @param WP_REST_Request $request | |
| 113 | + * | |
| 114 | + * @return LogModel[] | |
| 115 | + */ | |
| 116 | + public function getLogsForRequest( WP_REST_Request $request ) { | |
| 117 | + $logs = []; | |
| 124 | 118 | |
| 125 | - $type = $request->get_param('type'); | |
| 126 | - $category = $request->get_param('category'); | |
| 127 | - $source = $request->get_param('source'); | |
| 128 | - $page = $request->get_param('page'); | |
| 129 | - $sortBy = $request->get_param('sort'); | |
| 130 | - $startDate = $request->get_param('start'); | |
| 131 | - $endDate = $request->get_param('end'); | |
| 132 | - $sortDirection = $request->get_param('direction'); | |
| 119 | + $type = $request->get_param( 'type' ); | |
| 120 | + $category = $request->get_param( 'category' ); | |
| 121 | + $source = $request->get_param( 'source' ); | |
| 122 | + $page = $request->get_param( 'page' ); | |
| 123 | + $sortBy = $request->get_param( 'sort' ); | |
| 124 | + $startDate = $request->get_param( 'start' ); | |
| 125 | + $endDate = $request->get_param( 'end' ); | |
| 126 | + $sortDirection = $request->get_param( 'direction' ); | |
| 133 | 127 | |
| 134 | - $offset = ($page - 1) * self::LOGS_PER_PAGE; | |
| 128 | + $offset = ( $page - 1 ) * self::LOGS_PER_PAGE; | |
| 135 | 129 | |
| 136 | - $query = "SELECT * FROM {$this->log_table} WHERE 1=1"; | |
| 130 | + $query = "SELECT * FROM {$this->log_table} WHERE 1=1"; | |
| 137 | 131 | |
| 138 | - if ($type) { | |
| 139 | - $query .= sprintf(' AND log_type = "%s"', esc_sql($type)); | |
| 140 | - } | |
| 132 | + if ( $type ) { | |
| 133 | + $query .= sprintf( ' AND log_type = "%s"', esc_sql( $type ) ); | |
| 134 | + } | |
| 141 | 135 | |
| 142 | - if ($category) { | |
| 143 | - $query .= sprintf(' AND category = "%s"', esc_sql($category)); | |
| 144 | - } | |
| 136 | + if ( $category ) { | |
| 137 | + $query .= sprintf( ' AND category = "%s"', esc_sql( $category ) ); | |
| 138 | + } | |
| 145 | 139 | |
| 146 | - if ($source) { | |
| 147 | - $query .= sprintf(' AND source = "%s"', esc_sql($source)); | |
| 148 | - } | |
| 140 | + if ( $source ) { | |
| 141 | + $query .= sprintf( ' AND source = "%s"', esc_sql( $source ) ); | |
| 142 | + } | |
| 149 | 143 | |
| 150 | - if ($startDate) { | |
| 151 | - $startDate = new DateTime($startDate); | |
| 152 | - $query .= sprintf(" AND date(date) >= '%s'", $startDate->format('Y-m-d')); | |
| 153 | - } | |
| 144 | + if ( $startDate ) { | |
| 145 | + $startDate = new DateTime( $startDate ); | |
| 146 | + $query .= sprintf( " AND date(date) >= '%s'", $startDate->format( 'Y-m-d' ) ); | |
| 147 | + } | |
| 154 | 148 | |
| 155 | - if ($endDate) { | |
| 156 | - $endDate = new DateTime($endDate); | |
| 157 | - $query .= sprintf(" AND date(date) <= '%s'", $endDate->format('Y-m-d')); | |
| 158 | - } | |
| 149 | + if ( $endDate ) { | |
| 150 | + $endDate = new DateTime( $endDate ); | |
| 151 | + $query .= sprintf( " AND date(date) <= '%s'", $endDate->format( 'Y-m-d' ) ); | |
| 152 | + } | |
| 159 | 153 | |
| 160 | - if ($sortBy) { | |
| 161 | - $column = (in_array($sortBy, self::SORTABLE_COLUMNS, true)) ? $sortBy : 'id'; | |
| 162 | - $direction = ($sortDirection && strtoupper($sortDirection) === 'ASC') ? 'ASC' : 'DESC'; | |
| 154 | + if ( $sortBy ) { | |
| 155 | + $column = ( in_array( $sortBy, self::SORTABLE_COLUMNS, true ) ) ? $sortBy : 'id'; | |
| 156 | + $direction = ( $sortDirection && strtoupper( $sortDirection ) === 'ASC' ) ? 'ASC' : 'DESC'; | |
| 163 | 157 | |
| 164 | - $query .= " ORDER BY `{$column}` {$direction}"; | |
| 165 | - } else { | |
| 166 | - $query .= ' ORDER BY id DESC'; | |
| 167 | - } | |
| 158 | + $query .= " ORDER BY `{$column}` {$direction}"; | |
| 159 | + } else { | |
| 160 | + $query .= ' ORDER BY id DESC'; | |
| 161 | + } | |
| 168 | 162 | |
| 169 | - // Limit | |
| 170 | - $query .= sprintf(' LIMIT %d', self::LOGS_PER_PAGE); | |
| 163 | + // Limit | |
| 164 | + $query .= sprintf( ' LIMIT %d', self::LOGS_PER_PAGE ); | |
| 171 | 165 | |
| 172 | - // Offset | |
| 173 | - if ($offset > 1) { | |
| 174 | - $query .= sprintf(' OFFSET %d', $offset); | |
| 175 | - } | |
| 166 | + // Offset | |
| 167 | + if ( $offset > 1 ) { | |
| 168 | + $query .= sprintf( ' OFFSET %d', $offset ); | |
| 169 | + } | |
| 176 | 170 | |
| 177 | - $result = DB::get_results($query); | |
| 171 | + $result = DB::get_results( $query ); | |
| 178 | 172 | |
| 179 | - if ($result) { | |
| 180 | - foreach ($result as $log) { | |
| 181 | - $data = json_decode($log->data, true); | |
| 173 | + if ( $result ) { | |
| 174 | + foreach ( $result as $log ) { | |
| 175 | + $data = json_decode( $log->data, true ); | |
| 182 | 176 | |
| 183 | - $logs[] = LogFactory::make( | |
| 184 | - $log->log_type, | |
| 185 | - $data['message'], | |
| 186 | - $log->category, | |
| 187 | - $log->source, | |
| 188 | - $data['context'], | |
| 189 | - $log->id, | |
| 190 | - $log->date | |
| 191 | - ); | |
| 192 | - } | |
| 193 | - } | |
| 177 | + $logs[] = LogFactory::make( | |
| 178 | + $log->log_type, | |
| 179 | + $data['message'], | |
| 180 | + $log->category, | |
| 181 | + $log->source, | |
| 182 | + $data['context'], | |
| 183 | + $log->id, | |
| 184 | + $log->date | |
| 185 | + ); | |
| 186 | + } | |
| 187 | + } | |
| 194 | 188 | |
| 195 | - return $logs; | |
| 196 | - } | |
| 189 | + return $logs; | |
| 190 | + } | |
| 197 | 191 | |
| 198 | - /** | |
| 199 | - * Get log by ID | |
| 200 | - * | |
| 201 | - * @param int $logId | |
| 202 | - * | |
| 203 | - * @return LogModel|null | |
| 204 | - */ | |
| 205 | - public function getLog($logId) | |
| 206 | - { | |
| 207 | - $log = DB::get_row( | |
| 208 | - DB::prepare("SELECT * FROM {$this->log_table} WHERE id = %d", $logId) | |
| 209 | - ); | |
| 192 | + /** | |
| 193 | + * Get log by ID | |
| 194 | + * | |
| 195 | + * @param int $logId | |
| 196 | + * | |
| 197 | + * @return LogModel|null | |
| 198 | + */ | |
| 199 | + public function getLog( $logId ) { | |
| 200 | + $log = DB::get_row( | |
| 201 | + DB::prepare( "SELECT * FROM {$this->log_table} WHERE id = %d", $logId ) | |
| 202 | + ); | |
| 210 | 203 | |
| 211 | - if ($log) { | |
| 212 | - $data = json_decode($log->data, true); | |
| 204 | + if ( $log ) { | |
| 205 | + $data = json_decode( $log->data, true ); | |
| 213 | 206 | |
| 214 | - return LogFactory::make( | |
| 215 | - $log->log_type, | |
| 216 | - $data['message'], | |
| 217 | - $log->category, | |
| 218 | - $log->source, | |
| 219 | - $data['context'], | |
| 220 | - $log->id, | |
| 221 | - $log->date | |
| 222 | - ); | |
| 223 | - } | |
| 207 | + return LogFactory::make( | |
| 208 | + $log->log_type, | |
| 209 | + $data['message'], | |
| 210 | + $log->category, | |
| 211 | + $log->source, | |
| 212 | + $data['context'], | |
| 213 | + $log->id, | |
| 214 | + $log->date | |
| 215 | + ); | |
| 216 | + } | |
| 224 | 217 | |
| 225 | - return null; | |
| 226 | - } | |
| 218 | + return null; | |
| 219 | + } | |
| 227 | 220 | |
| 228 | - /** | |
| 229 | - * Get logs by type | |
| 230 | - * | |
| 231 | - * @param string $type | |
| 232 | - * | |
| 233 | - * @return LogModel[] | |
| 234 | - */ | |
| 235 | - public function getLogsByType($type) | |
| 236 | - { | |
| 237 | - $logs = []; | |
| 221 | + /** | |
| 222 | + * Get logs by type | |
| 223 | + * | |
| 224 | + * @param string $type | |
| 225 | + * | |
| 226 | + * @return LogModel[] | |
| 227 | + */ | |
| 228 | + public function getLogsByType( $type ) { | |
| 229 | + $logs = []; | |
| 238 | 230 | |
| 239 | - $result = DB::get_results( | |
| 240 | - DB::prepare("SELECT * FROM {$this->log_table} WHERE log_type = %s", $type) | |
| 241 | - ); | |
| 231 | + $result = DB::get_results( | |
| 232 | + DB::prepare( "SELECT * FROM {$this->log_table} WHERE log_type = %s", $type ) | |
| 233 | + ); | |
| 242 | 234 | |
| 243 | - if ($result) { | |
| 244 | - foreach ($result as $log) { | |
| 245 | - $data = json_decode($log->data, true); | |
| 235 | + if ( $result ) { | |
| 236 | + foreach ( $result as $log ) { | |
| 237 | + $data = json_decode( $log->data, true ); | |
| 246 | 238 | |
| 247 | - $logs[] = LogFactory::make( | |
| 248 | - $log->log_type, | |
| 249 | - $data['message'], | |
| 250 | - $log->category, | |
| 251 | - $log->source, | |
| 252 | - $data['context'], | |
| 253 | - $log->id, | |
| 254 | - $log->date | |
| 255 | - ); | |
| 256 | - } | |
| 257 | - } | |
| 239 | + $logs[] = LogFactory::make( | |
| 240 | + $log->log_type, | |
| 241 | + $data['message'], | |
| 242 | + $log->category, | |
| 243 | + $log->source, | |
| 244 | + $data['context'], | |
| 245 | + $log->id, | |
| 246 | + $log->date | |
| 247 | + ); | |
| 248 | + } | |
| 249 | + } | |
| 258 | 250 | |
| 259 | - return $logs; | |
| 260 | - } | |
| 251 | + return $logs; | |
| 252 | + } | |
| 261 | 253 | |
| 262 | - /** | |
| 263 | - * Get logs by category | |
| 264 | - * | |
| 265 | - * @param string $category | |
| 266 | - * | |
| 267 | - * @return LogModel[] | |
| 268 | - */ | |
| 269 | - public function getLogsByCategory($category) | |
| 270 | - { | |
| 271 | - $logs = []; | |
| 254 | + /** | |
| 255 | + * Get logs by category | |
| 256 | + * | |
| 257 | + * @param string $category | |
| 258 | + * | |
| 259 | + * @return LogModel[] | |
| 260 | + */ | |
| 261 | + public function getLogsByCategory( $category ) { | |
| 262 | + $logs = []; | |
| 272 | 263 | |
| 273 | - $result = DB::get_results( | |
| 274 | - DB::prepare("SELECT * FROM {$this->log_table} WHERE category = %s", $category) | |
| 275 | - ); | |
| 264 | + $result = DB::get_results( | |
| 265 | + DB::prepare( "SELECT * FROM {$this->log_table} WHERE category = %s", $category ) | |
| 266 | + ); | |
| 276 | 267 | |
| 277 | - if ($result) { | |
| 278 | - foreach ($result as $log) { | |
| 279 | - $data = json_decode($log->data, true); | |
| 268 | + if ( $result ) { | |
| 269 | + foreach ( $result as $log ) { | |
| 270 | + $data = json_decode( $log->data, true ); | |
| 280 | 271 | |
| 281 | - $logs[] = LogFactory::make( | |
| 282 | - $log->log_type, | |
| 283 | - $data['message'], | |
| 284 | - $log->category, | |
| 285 | - $log->source, | |
| 286 | - $data['context'], | |
| 287 | - $log->id, | |
| 288 | - $log->date | |
| 289 | - ); | |
| 290 | - } | |
| 291 | - } | |
| 272 | + $logs[] = LogFactory::make( | |
| 273 | + $log->log_type, | |
| 274 | + $data['message'], | |
| 275 | + $log->category, | |
| 276 | + $log->source, | |
| 277 | + $data['context'], | |
| 278 | + $log->id, | |
| 279 | + $log->date | |
| 280 | + ); | |
| 281 | + } | |
| 282 | + } | |
| 292 | 283 | |
| 293 | - return $logs; | |
| 294 | - } | |
| 284 | + return $logs; | |
| 285 | + } | |
| 295 | 286 | |
| 296 | - /** | |
| 297 | - * Get logs categories | |
| 298 | - * | |
| 299 | - * @return array | |
| 300 | - */ | |
| 301 | - public function getCategories() | |
| 302 | - { | |
| 303 | - $categories = []; | |
| 304 | - $result = DB::get_results("SELECT DISTINCT category FROM {$this->log_table}"); | |
| 287 | + /** | |
| 288 | + * Get logs categories | |
| 289 | + * | |
| 290 | + * @return array | |
| 291 | + */ | |
| 292 | + public function getCategories() { | |
| 293 | + $categories = []; | |
| 294 | + $result = DB::get_results( "SELECT DISTINCT category FROM {$this->log_table}" ); | |
| 305 | 295 | |
| 306 | - if ($result) { | |
| 307 | - foreach ($result as $category) { | |
| 308 | - $categories[] = $category->category; | |
| 309 | - } | |
| 310 | - } | |
| 296 | + if ( $result ) { | |
| 297 | + foreach ( $result as $category ) { | |
| 298 | + $categories[] = $category->category; | |
| 299 | + } | |
| 300 | + } | |
| 311 | 301 | |
| 312 | - return $categories; | |
| 313 | - } | |
| 302 | + return $categories; | |
| 303 | + } | |
| 314 | 304 | |
| 315 | - /** | |
| 316 | - * Get logs sources | |
| 317 | - * | |
| 318 | - * @return array | |
| 319 | - */ | |
| 320 | - public function getSources() | |
| 321 | - { | |
| 322 | - $sources = []; | |
| 323 | - $result = DB::get_results("SELECT DISTINCT source FROM {$this->log_table}"); | |
| 305 | + /** | |
| 306 | + * Get logs sources | |
| 307 | + * | |
| 308 | + * @return array | |
| 309 | + */ | |
| 310 | + public function getSources() { | |
| 311 | + $sources = []; | |
| 312 | + $result = DB::get_results( "SELECT DISTINCT source FROM {$this->log_table}" ); | |
| 324 | 313 | |
| 325 | - if ($result) { | |
| 326 | - foreach ($result as $source) { | |
| 327 | - $sources[] = $source->source; | |
| 328 | - } | |
| 329 | - } | |
| 314 | + if ( $result ) { | |
| 315 | + foreach ( $result as $source ) { | |
| 316 | + $sources[] = $source->source; | |
| 317 | + } | |
| 318 | + } | |
| 330 | 319 | |
| 331 | - return $sources; | |
| 332 | - } | |
| 320 | + return $sources; | |
| 321 | + } | |
| 333 | 322 | |
| 334 | - /** | |
| 335 | - * Delete all logs | |
| 336 | - */ | |
| 337 | - public function deleteLogs() | |
| 338 | - { | |
| 339 | - DB::query("DELETE FROM {$this->log_table}"); | |
| 340 | - } | |
| 341 | 323 | |
| 342 | - /** | |
| 343 | - * Delete log by ID | |
| 344 | - * | |
| 345 | - * @param int $logId | |
| 346 | - */ | |
| 347 | - public function deleteLog($logId) | |
| 348 | - { | |
| 349 | - DB::query( | |
| 350 | - DB::prepare("DELETE FROM {$this->log_table} WHERE id = %d", $logId) | |
| 351 | - ); | |
| 352 | - } | |
| 324 | + /** | |
| 325 | + * Delete all logs | |
| 326 | + */ | |
| 327 | + public function deleteLogs() { | |
| 328 | + DB::query( "DELETE FROM {$this->log_table}" ); | |
| 329 | + } | |
| 353 | 330 | |
| 354 | - public function getTotalCount() | |
| 355 | - { | |
| 356 | - return DB::get_var("SELECT count(id) FROM {$this->log_table}"); | |
| 357 | - } | |
| 331 | + /** | |
| 332 | + * Delete log by ID | |
| 333 | + * | |
| 334 | + * @param int $logId | |
| 335 | + */ | |
| 336 | + public function deleteLog( $logId ) { | |
| 337 | + DB::query( | |
| 338 | + DB::prepare( "DELETE FROM {$this->log_table} WHERE id = %d", $logId ) | |
| 339 | + ); | |
| 340 | + } | |
| 358 | 341 | |
| 359 | - /** | |
| 360 | - * Get log count by column name containing value | |
| 361 | - * | |
| 362 | - * @param string $columnName | |
| 363 | - * @param string $value | |
| 364 | - * | |
| 365 | - * @return string|null | |
| 366 | - */ | |
| 367 | - public function getLogCountBy($columnName, $value) | |
| 368 | - { | |
| 369 | - if ( ! in_array($columnName, self::SORTABLE_COLUMNS, true)) { | |
| 370 | - throw new InvalidArgumentException( | |
| 371 | - sprintf('Invalid column %s', $columnName) | |
| 372 | - ); | |
| 373 | - } | |
| 342 | + public function getTotalCount() { | |
| 343 | + return DB::get_var( "SELECT count(id) FROM {$this->log_table}" ); | |
| 344 | + } | |
| 374 | 345 | |
| 375 | - return DB::get_var( | |
| 376 | - DB::prepare("SELECT count(id) FROM {$this->log_table} WHERE {$columnName}=%s", $value) | |
| 377 | - ); | |
| 378 | - } | |
| 346 | + /** | |
| 347 | + * Get log count by column name containing value | |
| 348 | + * | |
| 349 | + * @param string $columnName | |
| 350 | + * @param string $value | |
| 351 | + * | |
| 352 | + * @return string|null | |
| 353 | + */ | |
| 354 | + public function getLogCountBy( $columnName, $value ) { | |
| 355 | + if ( ! in_array( $columnName, self::SORTABLE_COLUMNS, true ) ) { | |
| 356 | + throw new InvalidArgumentException( | |
| 357 | + sprintf( 'Invalid column %s', $columnName ) | |
| 358 | + ); | |
| 359 | + } | |
| 360 | + return DB::get_var( | |
| 361 | + DB::prepare( "SELECT count(id) FROM {$this->log_table} WHERE {$columnName}=%s", $value ) | |
| 362 | + ); | |
| 363 | + } | |
| 379 | 364 | |
| 380 | - /** | |
| 381 | - * Get log count for request | |
| 382 | - * | |
| 383 | - * @param WP_REST_Request $request | |
| 384 | - * | |
| 385 | - * @return int|null | |
| 386 | - */ | |
| 387 | - public function getLogCountForRequest(WP_REST_Request $request) | |
| 388 | - { | |
| 389 | - $type = $request->get_param('type'); | |
| 390 | - $category = $request->get_param('category'); | |
| 391 | - $source = $request->get_param('source'); | |
| 392 | - $startDate = $request->get_param('start'); | |
| 393 | - $endDate = $request->get_param('end'); | |
| 394 | 365 | |
| 395 | - $query = "SELECT count(id) FROM {$this->log_table} WHERE 1=1"; | |
| 366 | + /** | |
| 367 | + * Get log count for request | |
| 368 | + * | |
| 369 | + * @param WP_REST_Request $request | |
| 370 | + * | |
| 371 | + * @return int|null | |
| 372 | + */ | |
| 373 | + public function getLogCountForRequest( WP_REST_Request $request ) { | |
| 374 | + $type = $request->get_param( 'type' ); | |
| 375 | + $category = $request->get_param( 'category' ); | |
| 376 | + $source = $request->get_param( 'source' ); | |
| 377 | + $startDate = $request->get_param( 'start' ); | |
| 378 | + $endDate = $request->get_param( 'end' ); | |
| 396 | 379 | |
| 397 | - if ($type) { | |
| 398 | - $query .= sprintf(' AND log_type = "%s"', esc_sql($type)); | |
| 399 | - } | |
| 380 | + $query = "SELECT count(id) FROM {$this->log_table} WHERE 1=1"; | |
| 400 | 381 | |
| 401 | - if ($category) { | |
| 402 | - $query .= sprintf(' AND category = "%s"', esc_sql($category)); | |
| 403 | - } | |
| 382 | + if ( $type ) { | |
| 383 | + $query .= sprintf( ' AND log_type = "%s"', esc_sql( $type ) ); | |
| 384 | + } | |
| 404 | 385 | |
| 405 | - if ($source) { | |
| 406 | - $query .= sprintf(' AND source = "%s"', esc_sql($source)); | |
| 407 | - } | |
| 386 | + if ( $category ) { | |
| 387 | + $query .= sprintf( ' AND category = "%s"', esc_sql( $category ) ); | |
| 388 | + } | |
| 408 | 389 | |
| 409 | - if ($startDate) { | |
| 410 | - $startDate = new DateTime($startDate); | |
| 411 | - $query .= sprintf(" AND date(date) >= '%s'", $startDate->format('Y-m-d')); | |
| 412 | - } | |
| 390 | + if ( $source ) { | |
| 391 | + $query .= sprintf( ' AND source = "%s"', esc_sql( $source ) ); | |
| 392 | + } | |
| 413 | 393 | |
| 414 | - if ($endDate) { | |
| 415 | - $endDate = new DateTime($endDate); | |
| 416 | - $query .= sprintf(" AND date(date) <= '%s'", $endDate->format('Y-m-d')); | |
| 417 | - } | |
| 394 | + if ( $startDate ) { | |
| 395 | + $startDate = new DateTime( $startDate ); | |
| 396 | + $query .= sprintf( " AND date(date) >= '%s'", $startDate->format( 'Y-m-d' ) ); | |
| 397 | + } | |
| 418 | 398 | |
| 419 | - return DB::get_var($query); | |
| 420 | - } | |
| 399 | + if ( $endDate ) { | |
| 400 | + $endDate = new DateTime( $endDate ); | |
| 401 | + $query .= sprintf( " AND date(date) <= '%s'", $endDate->format( 'Y-m-d' ) ); | |
| 402 | + } | |
| 421 | 403 | |
| 422 | - /** | |
| 423 | - * Get sortable columns | |
| 424 | - * | |
| 425 | - * @return string[] | |
| 426 | - */ | |
| 427 | - public function getSortableColumns() | |
| 428 | - { | |
| 429 | - return self::SORTABLE_COLUMNS; | |
| 430 | - } | |
| 404 | + return DB::get_var( $query ); | |
| 405 | + } | |
| 431 | 406 | |
| 432 | - /** | |
| 433 | - * Get logs per page limit | |
| 434 | - * | |
| 435 | - * @return int | |
| 436 | - */ | |
| 437 | - public function getLogsPerPageLimit() | |
| 438 | - { | |
| 439 | - return self::LOGS_PER_PAGE; | |
| 440 | - } | |
| 407 | + /** | |
| 408 | + * Get sortable columns | |
| 409 | + * | |
| 410 | + * @return string[] | |
| 411 | + */ | |
| 412 | + public function getSortableColumns() { | |
| 413 | + return self::SORTABLE_COLUMNS; | |
| 414 | + } | |
| 441 | 415 | |
| 442 | - /** | |
| 443 | - * Flush logs | |
| 444 | - */ | |
| 445 | - public function flushLogs() | |
| 446 | - { | |
| 447 | - DB::query("DELETE FROM {$this->log_table}"); | |
| 448 | - } | |
| 416 | + /** | |
| 417 | + * Get logs per page limit | |
| 418 | + * | |
| 419 | + * @return int | |
| 420 | + */ | |
| 421 | + public function getLogsPerPageLimit() { | |
| 422 | + return self::LOGS_PER_PAGE; | |
| 423 | + } | |
| 424 | + | |
| 425 | + /** | |
| 426 | + * Flush logs | |
| 427 | + */ | |
| 428 | + public function flushLogs() { | |
| 429 | + DB::query( "DELETE FROM {$this->log_table}" ); | |
| 430 | + } | |
| 449 | 431 | } |