| 1 |
<?php |
| 2 |
|
| 3 |
use Give\Log\Log; |
| 4 |
use Give\Log\LogFactory; |
| 5 |
use Give\Log\LogRepository; |
| 6 |
use Give\Log\ValueObjects\LogType; |
| 7 |
use Give\Log\Helpers\LogTypeHelper; |
| 8 |
/** |
| 9 |
* Class for logging events and errors |
| 10 |
* |
| 11 |
* @package Give |
| 12 |
* @subpackage Classes/Give_Logging |
| 13 |
* @copyright Copyright (c) 2016, GiveWP |
| 14 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 15 |
* @since 1.0 |
| 16 |
*/ |
| 17 |
|
| 18 |
// Exit if accessed directly. |
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Give_Logging Class |
| 25 |
* |
| 26 |
* A general use class for logging events and errors. |
| 27 |
* |
| 28 |
* @deprecated 2.10.0 |
| 29 |
* @use Log |
| 30 |
* @see Log |
| 31 |
* |
| 32 |
* @since 1.0 |
| 33 |
*/ |
| 34 |
class Give_Logging { |
| 35 |
/** |
| 36 |
* @var LogRepository |
| 37 |
*/ |
| 38 |
private $logRepository; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var LogTypeHelper |
| 42 |
*/ |
| 43 |
private $logTypeHelper; |
| 44 |
|
| 45 |
/** |
| 46 |
* Class Constructor |
| 47 |
* |
| 48 |
* Set up the Give Logging Class. |
| 49 |
* |
| 50 |
* @since 1.0 |
| 51 |
* @access public |
| 52 |
*/ |
| 53 |
public function __construct() { |
| 54 |
$this->logRepository = give( LogRepository::class ); |
| 55 |
$this->logTypeHelper = give( LogTypeHelper::class ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Create new log entry |
| 60 |
* |
| 61 |
* This is just a simple and fast way to log something. Use $this->insert_log() |
| 62 |
* if you need to store custom meta data. |
| 63 |
* |
| 64 |
* @deprecated 2.10.0 |
| 65 |
* @use Log::LOG_TYPE( $message ); |
| 66 |
* @see Log |
| 67 |
* |
| 68 |
* @since 1.0 |
| 69 |
* @access public |
| 70 |
* |
| 71 |
* @param string $title Log entry title. Default is empty. |
| 72 |
* @param string $message Log entry message. Default is empty. |
| 73 |
* @param int $parent Log entry parent. Default is 0. |
| 74 |
* @param string $type Log type. Default is empty string. |
| 75 |
* |
| 76 |
* @return int Log ID. |
| 77 |
*/ |
| 78 |
public function add( $title = '', $message = '', $parent = 0, $type = '' ) { |
| 79 |
$log_data = [ |
| 80 |
'post_title' => $title, |
| 81 |
'post_content' => $message, |
| 82 |
'post_parent' => $parent, |
| 83 |
'log_type' => $type, |
| 84 |
]; |
| 85 |
|
| 86 |
return $this->insert_log( $log_data ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Helper method used to map the fields from the old system to the new system |
| 91 |
* |
| 92 |
* @since 2.10.0 |
| 93 |
* |
| 94 |
* @param array $logData |
| 95 |
* @param array $logMeta |
| 96 |
* |
| 97 |
* @return array |
| 98 |
*/ |
| 99 |
private function getLogData( $logData, $logMeta ) { |
| 100 |
$oldType = isset( $logData['log_type'] ) |
| 101 |
? $logData['log_type'] |
| 102 |
: LogType::getDefault(); |
| 103 |
|
| 104 |
$data = $this->logTypeHelper->getDataFromType( $oldType ); |
| 105 |
|
| 106 |
$content = esc_html__( 'Something went wrong', 'give' ); |
| 107 |
|
| 108 |
if ( isset( $logData['log_content'] ) ) { |
| 109 |
$content = $logData['log_content']; |
| 110 |
} else { |
| 111 |
if ( isset( $logData['post_title'] ) ) { |
| 112 |
$content = $logData['post_title']; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return [ |
| 117 |
'type' => $data['type'], |
| 118 |
'category' => $data['category'], |
| 119 |
'message' => $content, |
| 120 |
'context' => array_merge( $logData, $logMeta ), |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get Logs |
| 126 |
* |
| 127 |
* Retrieves log items for a particular object ID. |
| 128 |
* |
| 129 |
* @deprecated 2.10.0 |
| 130 |
* @use LogRepository::getLogs(); |
| 131 |
* @see LogRepository |
| 132 |
* |
| 133 |
* @since 1.0 |
| 134 |
* @access public |
| 135 |
* |
| 136 |
* @param int $object_id Log object ID. Default is 0. |
| 137 |
* @param string $type Log type. Default is empty string. |
| 138 |
* @param int $paged Page number Default is null. |
| 139 |
* |
| 140 |
* @return array An array of the connected logs. |
| 141 |
*/ |
| 142 |
public function get_logs( $object_id = 0, $type = '', $paged = null ) { |
| 143 |
if ( $object_id ) { |
| 144 |
$log = $this->logRepository->getLog( $object_id ); |
| 145 |
|
| 146 |
return [ |
| 147 |
(object) [ |
| 148 |
'ID' => $log->getId(), |
| 149 |
'log_date' => $log->getDate(), |
| 150 |
'log_date_gmt' => '', |
| 151 |
'log_content' => $log->getMessage(), |
| 152 |
'log_title' => $log->getCategory() . ' - ' . $log->getSource(), |
| 153 |
'log_type' => $log->getType(), |
| 154 |
], |
| 155 |
]; |
| 156 |
} |
| 157 |
|
| 158 |
if ( ! empty( $type ) ) { |
| 159 |
$data = []; |
| 160 |
$logs = $this->logRepository->getLogsByType( $type ); |
| 161 |
|
| 162 |
foreach ( $logs as $log ) { |
| 163 |
$data[] = (object) [ |
| 164 |
'ID' => $log->getId(), |
| 165 |
'log_date' => $log->getDate(), |
| 166 |
'log_date_gmt' => '', |
| 167 |
'log_content' => $log->getMessage(), |
| 168 |
'log_title' => $log->getCategory() . ' - ' . $log->getSource(), |
| 169 |
'log_type' => $log->getType(), |
| 170 |
]; |
| 171 |
} |
| 172 |
|
| 173 |
return $data; |
| 174 |
} |
| 175 |
|
| 176 |
return []; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Stores a log entry |
| 181 |
* |
| 182 |
* @deprecated 2.10.0 |
| 183 |
* @use Log::LOG_TYPE( $message ); |
| 184 |
* @see Log |
| 185 |
* |
| 186 |
* @since 1.0 |
| 187 |
* @access public |
| 188 |
* |
| 189 |
* @param array $log_data Log entry data. |
| 190 |
* @param array $log_meta Log entry meta. |
| 191 |
* |
| 192 |
* @return int The ID of the newly created log item. |
| 193 |
*/ |
| 194 |
public function insert_log( $log_data = [], $log_meta = [] ) { |
| 195 |
// Extract data from parameters |
| 196 |
$data = $this->getLogData( $log_data, $log_meta ); |
| 197 |
|
| 198 |
$backtrace = debug_backtrace(); |
| 199 |
|
| 200 |
// Add more context |
| 201 |
if ( |
| 202 |
isset( $backtrace[1] ) && |
| 203 |
! array_diff( [ 'file', 'line', 'function', 'class' ], array_keys( $backtrace[1] ) ) |
| 204 |
) { |
| 205 |
$data['context']['file'] = $backtrace[1]['file']; |
| 206 |
$data['context']['line'] = $backtrace[1]['line']; |
| 207 |
$data['context']['function'] = $backtrace[1]['function']; |
| 208 |
$data['context']['class'] = $backtrace[1]['class']; |
| 209 |
} |
| 210 |
|
| 211 |
try { |
| 212 |
$log = LogFactory::makeFromArray( $data ); |
| 213 |
$log->save(); |
| 214 |
|
| 215 |
return $log->getId(); |
| 216 |
} catch ( Exception $exception ) { |
| 217 |
error_log( $exception->getMessage() ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Update and existing log item |
| 223 |
* |
| 224 |
* @deprecated 2.10.0 |
| 225 |
* @use Log::LOG_TYPE( $message ); |
| 226 |
* @see Log |
| 227 |
* |
| 228 |
* @since 1.0 |
| 229 |
* @access public |
| 230 |
* |
| 231 |
* @param array $log_data Log entry data. |
| 232 |
* @param array $log_meta Log entry meta. |
| 233 |
* |
| 234 |
* @return bool|null True if successful, false otherwise. |
| 235 |
*/ |
| 236 |
public function update_log( $log_data = [], $log_meta = [] ) { |
| 237 |
return $this->insert_log( $log_data, $log_meta ); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Retrieve all connected logs |
| 242 |
* |
| 243 |
* Used for retrieving logs related to particular items, such as a specific donation. |
| 244 |
* For new table params check: Give_DB_Logs::get_column_defaults and Give_DB_Logs::get_sql#L262 |
| 245 |
* |
| 246 |
* @deprecated 2.10.0 |
| 247 |
* |
| 248 |
* @since 1.0 |
| 249 |
* @since 2.0 Added new table logic. |
| 250 |
* @access public |
| 251 |
* |
| 252 |
* @param array $args Query arguments. |
| 253 |
* |
| 254 |
* @return array|false Array if logs were found, false otherwise. |
| 255 |
*/ |
| 256 |
public function get_connected_logs( $args = [] ) { |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Retrieve Log Count |
| 262 |
* |
| 263 |
* Retrieves number of log entries connected to particular object ID. |
| 264 |
* |
| 265 |
* @deprecated 2.10.0 |
| 266 |
* @use LogRepository::getTotalCount() |
| 267 |
* @see LogRepository |
| 268 |
* |
| 269 |
* @since 1.0 |
| 270 |
* @access public |
| 271 |
* |
| 272 |
* @param int $object_id Log object ID. Default is 0. |
| 273 |
* @param string $type Log type. Default is empty string. |
| 274 |
* @param array $meta_query Log meta query. Default is null. |
| 275 |
* @param array $date_query Log data query. Default is null. |
| 276 |
* |
| 277 |
* @return int Log count. |
| 278 |
*/ |
| 279 |
public function get_log_count( $object_id = 0, $type = '', $meta_query = null, $date_query = null ) { |
| 280 |
if ( $object_id ) { |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
if ( ! empty( $type ) ) { |
| 285 |
$logs = $this->logRepository->getLogsByType( $type ); |
| 286 |
return count( $logs ); |
| 287 |
} |
| 288 |
|
| 289 |
return $this->logRepository->getTotalCount(); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Delete Logs |
| 294 |
* |
| 295 |
* Remove log entries connected to particular object ID. |
| 296 |
* |
| 297 |
* @deprecated 2.10.0 |
| 298 |
* @use LogRepository::deleteLogs() |
| 299 |
* @see LogRepository |
| 300 |
* |
| 301 |
* @since 1.0 |
| 302 |
* @access public |
| 303 |
* |
| 304 |
* @param int $object_id Log object ID. Default is 0. |
| 305 |
* @param string $type Log type. Default is empty string. |
| 306 |
* @param array $meta_query Log meta query. Default is null. |
| 307 |
* |
| 308 |
* @return void |
| 309 |
*/ |
| 310 |
public function delete_logs( $object_id = 0, $type = '', $meta_query = null ) { |
| 311 |
$this->logRepository->deleteLogs(); |
| 312 |
} |
| 313 |
|
| 314 |
} |
| 315 |
|