| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class for logging events and errors |
| 4 |
* |
| 5 |
* @package Analytify |
| 6 |
* @subpackage Logging |
| 7 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 8 |
* @since 1.2.4 |
| 9 |
*/ |
| 10 |
|
| 11 |
// Exit if accessed directly. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; } |
| 14 |
|
| 15 |
/** |
| 16 |
* Analytify_Logging Class |
| 17 |
* |
| 18 |
* A general use class for logging events and errors. |
| 19 |
* |
| 20 |
* @since 1.2.4 |
| 21 |
*/ |
| 22 |
class Analytify_Logging { |
| 23 |
|
| 24 |
/** |
| 25 |
* Set up the Analytify Logging Class |
| 26 |
* |
| 27 |
* @since 1.2.4 |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
// Create the log post type. |
| 31 |
add_action( 'init', array( $this, 'register_post_type' ), 1 ); |
| 32 |
|
| 33 |
// Create types taxonomy and default types. |
| 34 |
add_action( 'init', array( $this, 'register_taxonomy' ), 1 ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Registers the analytify_log Post Type |
| 39 |
* |
| 40 |
* @access public |
| 41 |
* @since 1.2.4 |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function register_post_type() { |
| 45 |
/* Logs post type */ |
| 46 |
$log_args = array( |
| 47 |
'labels' => array( 'name' => __( 'Logs', 'wp-analytify' ) ), |
| 48 |
'public' => false, |
| 49 |
'exclude_from_search' => true, |
| 50 |
'publicly_queryable' => false, |
| 51 |
'show_ui' => false, |
| 52 |
'query_var' => false, |
| 53 |
'rewrite' => false, |
| 54 |
'capability_type' => 'post', |
| 55 |
'supports' => array( 'title', 'editor' ), |
| 56 |
'can_export' => true, |
| 57 |
); |
| 58 |
|
| 59 |
register_post_type( 'analytify_log', $log_args ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Registers the Type Taxonomy |
| 64 |
* |
| 65 |
* The "Type" taxonomy is used to determine the type of log entry |
| 66 |
* |
| 67 |
* @access public |
| 68 |
* @since 1.2.4 |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function register_taxonomy() { |
| 72 |
register_taxonomy( 'analytify_log_type', 'analytify_log', array( 'public' => false ) ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Log types |
| 77 |
* |
| 78 |
* Sets up the default log types and allows for new ones to be created |
| 79 |
* |
| 80 |
* @access public |
| 81 |
* @since 1.2.4 |
| 82 |
* @return array<string, mixed> $terms |
| 83 |
*/ |
| 84 |
public function log_types() { |
| 85 |
$terms = array( |
| 86 |
'errors', |
| 87 |
'messages', |
| 88 |
); |
| 89 |
|
| 90 |
return apply_filters( 'analytify_log_types', $terms ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Check if a log type is valid |
| 95 |
* |
| 96 |
* Checks to see if the specified type is in the registered list of types |
| 97 |
* |
| 98 |
* @access public |
| 99 |
* @since 1.2.4 |
| 100 |
* @uses Analytify_Logging::log_types() |
| 101 |
* @param string $type Log type. |
| 102 |
* @return bool Whether log type is valid |
| 103 |
*/ |
| 104 |
public function valid_type( $type ) { |
| 105 |
return in_array( $type, $this->log_types(), true ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Create new log entry |
| 110 |
* |
| 111 |
* This is just a simple and fast way to log something. Use $this->insert_log() |
| 112 |
* if you need to store custom meta data |
| 113 |
* |
| 114 |
* @access public |
| 115 |
* @since 1.2.4 |
| 116 |
* @uses _Logging::insert_log() |
| 117 |
* @param string $title Log entry title. |
| 118 |
* @param string $message Log entry message. |
| 119 |
* @param int $parent Log entry parent. |
| 120 |
* @param string $type Log type (default: null). |
| 121 |
* @return int Log ID |
| 122 |
*/ |
| 123 |
public function add( $title = '', $message = '', $parent = 0, $type = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.parentFound,WordPress.DB.SlowDBQuery.slow_db_query_tax_query,WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Parameter name is acceptable and slow query warnings are acknowledged. |
| 124 |
$log_data = array( |
| 125 |
'post_title' => $title, |
| 126 |
'post_content' => $message, |
| 127 |
'post_parent' => $parent, |
| 128 |
'log_type' => $type, |
| 129 |
); |
| 130 |
|
| 131 |
return $this->insert_log( $log_data ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Easily retrieves log items for a particular object ID |
| 136 |
* |
| 137 |
* @access public |
| 138 |
* @since 1.2.4 |
| 139 |
* @uses Analytify_Logging::get_connected_logs() |
| 140 |
* @param int $object_id (default: 0). |
| 141 |
* @param string $type Log type (default: null). |
| 142 |
* @param int $paged Page number (default: null). |
| 143 |
* @return array<string, mixed> Array of the connected logs |
| 144 |
*/ |
| 145 |
public function get_logs( $object_id = 0, $type = null, $paged = null ) { |
| 146 |
return $this->get_connected_logs( |
| 147 |
array( |
| 148 |
'post_parent' => $object_id, |
| 149 |
'paged' => $paged, |
| 150 |
'log_type' => $type, |
| 151 |
) |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Stores a log entry |
| 157 |
* |
| 158 |
* @access public |
| 159 |
* @since 1.2.4 |
| 160 |
* @uses Analytify_Logging::valid_type() |
| 161 |
* @param array<string, mixed> $log_data Log entry data. |
| 162 |
* @param array<string, mixed> $log_meta Log entry meta. |
| 163 |
* @return int The ID of the newly created log item |
| 164 |
*/ |
| 165 |
public function insert_log( $log_data = array(), $log_meta = array() ) { |
| 166 |
$defaults = array( |
| 167 |
'post_type' => 'analytify_log', |
| 168 |
'post_status' => 'publish', |
| 169 |
'post_parent' => 0, |
| 170 |
'post_content' => '', |
| 171 |
'log_type' => false, |
| 172 |
); |
| 173 |
|
| 174 |
$args = wp_parse_args( $log_data, $defaults ); |
| 175 |
do_action( 'analytify_pre_insert_log', $log_data, $log_meta ); |
| 176 |
|
| 177 |
// Store the log entry. |
| 178 |
$log_id = wp_insert_post( $args ); |
| 179 |
|
| 180 |
// Set the log type, if any. |
| 181 |
if ( $log_data['log_type'] && $this->valid_type( $log_data['log_type'] ) ) { |
| 182 |
wp_set_object_terms( $log_id, $log_data['log_type'], 'analytify_log_type', false ); |
| 183 |
} |
| 184 |
|
| 185 |
// Set log meta, if any. |
| 186 |
if ( $log_id && ! empty( $log_meta ) ) { |
| 187 |
foreach ( (array) $log_meta as $key => $meta ) { |
| 188 |
update_post_meta( $log_id, '_analytify_log_' . sanitize_key( $key ), $meta ); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
do_action( 'analytify_post_insert_log', $log_id, $log_data, $log_meta ); |
| 193 |
return $log_id; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Update and existing log item |
| 198 |
* |
| 199 |
* @access public |
| 200 |
* @since 1.2.4 |
| 201 |
* @param array<string, mixed> $log_data Log entry data. |
| 202 |
* @param array<string, mixed> $log_meta Log entry meta. |
| 203 |
* @return bool True if successful, false otherwise |
| 204 |
*/ |
| 205 |
public function update_log( $log_data = array(), $log_meta = array() ) { |
| 206 |
|
| 207 |
do_action( 'analytify_pre_update_log', $log_data, $log_meta ); |
| 208 |
|
| 209 |
$defaults = array( |
| 210 |
'post_type' => 'analytify_log', |
| 211 |
'post_status' => 'publish', |
| 212 |
'post_parent' => 0, |
| 213 |
); |
| 214 |
|
| 215 |
$args = wp_parse_args( $log_data, $defaults ); |
| 216 |
|
| 217 |
// Store the log entry. |
| 218 |
$log_id = wp_update_post( $args ); |
| 219 |
|
| 220 |
if ( $log_id && ! empty( $log_meta ) ) { |
| 221 |
foreach ( (array) $log_meta as $key => $meta ) { |
| 222 |
if ( ! empty( $meta ) ) { |
| 223 |
update_post_meta( $log_id, '_analytify_log_' . sanitize_key( $key ), $meta ); } |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
do_action( 'analytify_post_update_log', $log_id, $log_data, $log_meta ); |
| 228 |
|
| 229 |
return $log_id ? true : false; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Retrieve all connected logs |
| 234 |
* |
| 235 |
* Used for retrieving logs related to particular items, such as a specific purchase. |
| 236 |
* |
| 237 |
* @access private |
| 238 |
* @since 1.2.4 |
| 239 |
* @param array<string, mixed> $args Query arguments. |
| 240 |
* @return mixed array if logs were found, false otherwise |
| 241 |
*/ |
| 242 |
public function get_connected_logs( $args = array() ) { |
| 243 |
$defaults = array( |
| 244 |
'post_type' => 'analytify_log', |
| 245 |
'posts_per_page' => 20, |
| 246 |
'post_status' => 'publish', |
| 247 |
'paged' => get_query_var( 'paged' ), |
| 248 |
'log_type' => false, |
| 249 |
); |
| 250 |
|
| 251 |
$query_args = wp_parse_args( $args, $defaults ); |
| 252 |
|
| 253 |
if ( $query_args['log_type'] && $this->valid_type( $query_args['log_type'] ) ) { |
| 254 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Taxonomy query is necessary for log filtering by type. |
| 255 |
$query_args['tax_query'] = array( |
| 256 |
array( |
| 257 |
'taxonomy' => 'analytify_log_type', |
| 258 |
'field' => 'slug', |
| 259 |
'terms' => $query_args['log_type'], |
| 260 |
), |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
// Add caching to improve performance. |
| 265 |
$query_args['cache_results'] = true; |
| 266 |
$query_args['update_post_meta_cache'] = false; |
| 267 |
$query_args['update_post_term_cache'] = true; |
| 268 |
|
| 269 |
$logs = get_posts( $query_args ); |
| 270 |
|
| 271 |
if ( $logs ) { |
| 272 |
return $logs; } |
| 273 |
|
| 274 |
// No logs found. |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Retrieves number of log entries connected to particular object ID |
| 280 |
* |
| 281 |
* @access public |
| 282 |
* @since 1.2.4 |
| 283 |
* @param int $object_id (default: 0). |
| 284 |
* @param string $type Log type (default: null). |
| 285 |
* @param array<string, mixed> $meta_query Log meta query (default: null). |
| 286 |
* @param array<string, mixed> $date_query Log data query (default: null) (since 1.9). |
| 287 |
* @return int Log count |
| 288 |
*/ |
| 289 |
public function get_log_count( $object_id = 0, $type = null, $meta_query = null, $date_query = null ) { |
| 290 |
|
| 291 |
global $pagenow, $typenow; |
| 292 |
|
| 293 |
$query_args = array( |
| 294 |
'post_parent' => $object_id, |
| 295 |
'post_type' => 'analytify_log', |
| 296 |
'posts_per_page' => -1, |
| 297 |
'post_status' => 'publish', |
| 298 |
'fields' => 'ids', |
| 299 |
// Improve query performance with caching. |
| 300 |
'cache_results' => true, |
| 301 |
'update_post_meta_cache' => false, |
| 302 |
'update_post_term_cache' => true, |
| 303 |
); |
| 304 |
|
| 305 |
if ( ! empty( $type ) && $this->valid_type( $type ) ) { |
| 306 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Taxonomy query is necessary for log filtering by type. |
| 307 |
$query_args['tax_query'] = array( |
| 308 |
array( |
| 309 |
'taxonomy' => 'analytify_log_type', |
| 310 |
'field' => 'slug', |
| 311 |
'terms' => $type, |
| 312 |
), |
| 313 |
); |
| 314 |
} |
| 315 |
|
| 316 |
if ( ! empty( $meta_query ) ) { |
| 317 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Meta query is necessary for log filtering by metadata. |
| 318 |
$query_args['meta_query'] = $meta_query; |
| 319 |
} |
| 320 |
|
| 321 |
if ( ! empty( $date_query ) ) { |
| 322 |
$query_args['date_query'] = $date_query; |
| 323 |
} |
| 324 |
|
| 325 |
$logs = new WP_Query( $query_args ); |
| 326 |
|
| 327 |
return (int) $logs->post_count; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Delete a log |
| 332 |
* |
| 333 |
* @access public |
| 334 |
* @since 1.2.4 |
| 335 |
* @uses Analytify_Logging::valid_type |
| 336 |
* @param int $object_id (default: 0). |
| 337 |
* @param string $type Log type (default: null). |
| 338 |
* @param array<string, mixed> $meta_query Log meta query (default: null). |
| 339 |
* @return void |
| 340 |
*/ |
| 341 |
public function delete_logs( $object_id = 0, $type = null, $meta_query = null ) { |
| 342 |
$query_args = array( |
| 343 |
'post_parent' => $object_id, |
| 344 |
'post_type' => 'analytify_log', |
| 345 |
'posts_per_page' => -1, |
| 346 |
'post_status' => 'publish', |
| 347 |
'fields' => 'ids', |
| 348 |
// Improve query performance with caching. |
| 349 |
'cache_results' => true, |
| 350 |
'update_post_meta_cache' => false, |
| 351 |
'update_post_term_cache' => true, |
| 352 |
); |
| 353 |
|
| 354 |
if ( ! empty( $type ) && $this->valid_type( $type ) ) { |
| 355 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Taxonomy query is necessary for log filtering by type. |
| 356 |
$query_args['tax_query'] = array( |
| 357 |
array( |
| 358 |
'taxonomy' => 'analytify_log_type', |
| 359 |
'field' => 'slug', |
| 360 |
'terms' => $type, |
| 361 |
), |
| 362 |
); |
| 363 |
} |
| 364 |
|
| 365 |
if ( ! empty( $meta_query ) ) { |
| 366 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Meta query is necessary for log filtering by metadata. |
| 367 |
$query_args['meta_query'] = $meta_query; |
| 368 |
} |
| 369 |
|
| 370 |
$logs = get_posts( $query_args ); |
| 371 |
|
| 372 |
if ( $logs ) { |
| 373 |
foreach ( $logs as $log ) { |
| 374 |
wp_delete_post( $log, true ); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
// Initiate the logging system. |
| 381 |
$GLOBALS['analytify_logs'] = new Analytify_Logging(); |
| 382 |
|
| 383 |
// phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed -- Mixed structure is acceptable for this type of file |
| 384 |
|
| 385 |
/** |
| 386 |
* Record a log entry |
| 387 |
* |
| 388 |
* This is just a simple wrapper function for the log class add() function |
| 389 |
* |
| 390 |
* @since 1.3.3 |
| 391 |
* |
| 392 |
* @param string $title Log title. |
| 393 |
* @param string $message Log message. |
| 394 |
* @param int $parent Parent ID. |
| 395 |
* @param mixed $type Log type. |
| 396 |
* |
| 397 |
* @global $analytify_logs Analytify Logs Object |
| 398 |
* |
| 399 |
* @uses Analytify_Logging::add() |
| 400 |
* |
| 401 |
* @return mixed ID of the new log entry |
| 402 |
*/ |
| 403 |
function analytify_record_log( $title = '', $message = '', $parent = 0, $type = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.parentFound,Universal.Files.SeparateFunctionsFromOO.Mixed -- Parameter name and mixed structure are acceptable |
| 404 |
global $analytify_logs; |
| 405 |
$log = $analytify_logs->add( $title, $message, $parent, $type ); |
| 406 |
return $log; |
| 407 |
} |
| 408 |
|