admin
21 hours ago
api
3 years ago
database
5 months ago
deprecated
1 month ago
donors
5 months ago
emails
9 months ago
forms
21 hours ago
frontend
6 years ago
gateways
9 months ago
libraries
9 months ago
payments
2 months ago
actions.php
9 months ago
ajax-functions.php
2 days ago
class-give-async-process.php
1 year ago
class-give-background-updater.php
9 months ago
class-give-cache-setting.php
1 year ago
class-give-cache.php
9 months ago
class-give-cli-commands.php
1 year ago
class-give-comment.php
9 months ago
class-give-cron.php
9 months ago
class-give-donate-form.php
1 year ago
class-give-donor.php
2 years ago
class-give-email-access.php
5 years ago
class-give-license-handler.php
1 month ago
class-give-logging.php
9 months ago
class-give-readme-parser.php
4 years ago
class-give-roles.php
5 months ago
class-give-scripts.php
2 weeks ago
class-give-session.php
9 months ago
class-give-stats.php
6 years ago
class-give-template-loader.php
6 years ago
class-give-tooltips.php
6 years ago
class-give-translation.php
4 years ago
class-notices.php
9 months ago
country-functions.php
7 months ago
currencies-list.php
7 months ago
currency-functions.php
3 years ago
error-tracking.php
6 years ago
filters.php
9 months ago
formatting.php
9 months ago
install.php
9 months ago
login-register.php
2 years ago
misc-functions.php
1 month ago
plugin-compatibility.php
6 years ago
post-types.php
1 year ago
price-functions.php
6 years ago
process-donation.php
1 year ago
setting-functions.php
6 years ago
shortcodes.php
1 year ago
template-functions.php
1 year ago
user-functions.php
3 years ago
class-give-logging.php
316 lines
| 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 | //phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
| 195 | public function insert_log( $log_data = [], $log_meta = [] ) { |
| 196 | // Extract data from parameters |
| 197 | $data = $this->getLogData( $log_data, $log_meta ); |
| 198 | |
| 199 | $backtrace = debug_backtrace(); |
| 200 | |
| 201 | // Add more context |
| 202 | if ( |
| 203 | isset( $backtrace[1] ) && |
| 204 | ! array_diff( [ 'file', 'line', 'function', 'class' ], array_keys( $backtrace[1] ) ) |
| 205 | ) { |
| 206 | $data['context']['file'] = $backtrace[1]['file']; |
| 207 | $data['context']['line'] = $backtrace[1]['line']; |
| 208 | $data['context']['function'] = $backtrace[1]['function']; |
| 209 | $data['context']['class'] = $backtrace[1]['class']; |
| 210 | } |
| 211 | |
| 212 | try { |
| 213 | $log = LogFactory::makeFromArray( $data ); |
| 214 | $log->save(); |
| 215 | |
| 216 | return $log->getId(); |
| 217 | } catch ( Exception $exception ) { |
| 218 | error_log( $exception->getMessage() ); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Update and existing log item |
| 224 | * |
| 225 | * @deprecated 2.10.0 |
| 226 | * @use Log::LOG_TYPE( $message ); |
| 227 | * @see Log |
| 228 | * |
| 229 | * @since 1.0 |
| 230 | * @access public |
| 231 | * |
| 232 | * @param array $log_data Log entry data. |
| 233 | * @param array $log_meta Log entry meta. |
| 234 | * |
| 235 | * @return bool|null True if successful, false otherwise. |
| 236 | */ |
| 237 | public function update_log( $log_data = [], $log_meta = [] ) { |
| 238 | return $this->insert_log( $log_data, $log_meta ); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Retrieve all connected logs |
| 243 | * |
| 244 | * Used for retrieving logs related to particular items, such as a specific donation. |
| 245 | * For new table params check: Give_DB_Logs::get_column_defaults and Give_DB_Logs::get_sql#L262 |
| 246 | * |
| 247 | * @deprecated 2.10.0 |
| 248 | * |
| 249 | * @since 1.0 |
| 250 | * @since 2.0 Added new table logic. |
| 251 | * @access public |
| 252 | * |
| 253 | * @param array $args Query arguments. |
| 254 | * |
| 255 | * @return array|false Array if logs were found, false otherwise. |
| 256 | */ |
| 257 | public function get_connected_logs( $args = [] ) { |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Retrieve Log Count |
| 263 | * |
| 264 | * Retrieves number of log entries connected to particular object ID. |
| 265 | * |
| 266 | * @deprecated 2.10.0 |
| 267 | * @use LogRepository::getTotalCount() |
| 268 | * @see LogRepository |
| 269 | * |
| 270 | * @since 1.0 |
| 271 | * @access public |
| 272 | * |
| 273 | * @param int $object_id Log object ID. Default is 0. |
| 274 | * @param string $type Log type. Default is empty string. |
| 275 | * @param array $meta_query Log meta query. Default is null. |
| 276 | * @param array $date_query Log data query. Default is null. |
| 277 | * |
| 278 | * @return int Log count. |
| 279 | */ |
| 280 | public function get_log_count( $object_id = 0, $type = '', $meta_query = null, $date_query = null ) { |
| 281 | if ( $object_id ) { |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | if ( ! empty( $type ) ) { |
| 286 | $logs = $this->logRepository->getLogsByType( $type ); |
| 287 | return count( $logs ); |
| 288 | } |
| 289 | |
| 290 | return $this->logRepository->getTotalCount(); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Delete Logs |
| 295 | * |
| 296 | * Remove log entries connected to particular object ID. |
| 297 | * |
| 298 | * @deprecated 2.10.0 |
| 299 | * @use LogRepository::deleteLogs() |
| 300 | * @see LogRepository |
| 301 | * |
| 302 | * @since 1.0 |
| 303 | * @access public |
| 304 | * |
| 305 | * @param int $object_id Log object ID. Default is 0. |
| 306 | * @param string $type Log type. Default is empty string. |
| 307 | * @param array $meta_query Log meta query. Default is null. |
| 308 | * |
| 309 | * @return void |
| 310 | */ |
| 311 | public function delete_logs( $object_id = 0, $type = '', $meta_query = null ) { |
| 312 | $this->logRepository->deleteLogs(); |
| 313 | } |
| 314 | |
| 315 | } |
| 316 |