| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Database Logs. |
| 4 |
* |
| 5 |
* This file handles all interactions with the Client DB. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MainWP\Dashboard\Module\Log; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Logs |
| 14 |
*/ |
| 15 |
class Log { |
| 16 |
|
| 17 |
/** |
| 18 |
* Log_Manager |
| 19 |
* |
| 20 |
* @var manager Hold Log_Manager class |
| 21 |
* */ |
| 22 |
public $manager; |
| 23 |
|
| 24 |
/** |
| 25 |
* Log constructor. |
| 26 |
* |
| 27 |
* Run each time the class is called. |
| 28 |
* |
| 29 |
* @param Log_Manager $manager The main manager class. |
| 30 |
*/ |
| 31 |
public function __construct( $manager ) { |
| 32 |
$this->manager = $manager; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Log handler. |
| 38 |
* |
| 39 |
* @param Connector $connector Connector responsible for logging the event. |
| 40 |
* @param string $message sprintf-ready error message string. |
| 41 |
* @param array $args sprintf (and extra) arguments to use. |
| 42 |
* @param int $site_id Target site id. |
| 43 |
* @param string $context Context of the event. |
| 44 |
* @param string $action Action of the event. |
| 45 |
* @param int|null $state action status: null - N/A, 0 - failed, 1 - success. |
| 46 |
* @param int $user_id User responsible for the event. |
| 47 |
* |
| 48 |
* @return bool|WP_Error True if updated, otherwise false|WP_Error |
| 49 |
*/ |
| 50 |
public function log( $connector, $message, $args, $site_id, $context, $action, $state = null, $user_id = null ) { //phpcs:ignore -- NOSONAR - compatible. |
| 51 |
|
| 52 |
if ( is_null( $user_id ) ) { |
| 53 |
$user_id = get_current_user_id(); |
| 54 |
} |
| 55 |
|
| 56 |
if ( is_null( $site_id ) ) { |
| 57 |
$site_id = 0; |
| 58 |
} |
| 59 |
|
| 60 |
$cron_tracking = apply_filters( 'mainwp_module_log_cron_tracking', true, $connector, $message, $args, $site_id, $context, $action, $user_id, $state ); |
| 61 |
|
| 62 |
$author = new Log_Author( $user_id ); |
| 63 |
|
| 64 |
$agent = $author->get_current_agent(); |
| 65 |
|
| 66 |
// WP Cron tracking requires opt-in and WP Cron to be enabled. |
| 67 |
if ( ! $cron_tracking && 'wp_cron' === $agent ) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
$user = new \WP_User( $user_id ); |
| 72 |
|
| 73 |
$user_meta = array( |
| 74 |
'user_login' => (string) ! empty( $user->user_login ) ? $user->user_login : '', |
| 75 |
'agent' => (string) $agent, |
| 76 |
); |
| 77 |
|
| 78 |
if ( 'wp_cli' === $agent && function_exists( 'posix_getuid' ) ) { |
| 79 |
$uid = posix_getuid(); |
| 80 |
$user_info = posix_getpwuid( $uid ); |
| 81 |
|
| 82 |
$user_meta['system_user_id'] = (int) $uid; |
| 83 |
$user_meta['system_user_name'] = (string) $user_info['name']; |
| 84 |
} |
| 85 |
|
| 86 |
$dura = isset( $args['duration'] ) ? floatval( $args['duration'] ) : $this->manager->executor->get_exec_time(); |
| 87 |
|
| 88 |
if ( isset( $args['duration'] ) ) { |
| 89 |
unset( $args['duration'] ); |
| 90 |
} |
| 91 |
|
| 92 |
$dura_bulk = 1; |
| 93 |
if ( isset( $args['duration_bulk'] ) ) { |
| 94 |
$dura_bulk = intval( $args['duration_bulk'] ); |
| 95 |
unset( $args['duration_bulk'] ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( empty( $dura_bulk ) ) { |
| 99 |
$dura_bulk = 1; |
| 100 |
} |
| 101 |
|
| 102 |
$dura = $dura / $dura_bulk; |
| 103 |
|
| 104 |
// Prevent any meta with null values from being logged. |
| 105 |
$logs_meta = array_filter( |
| 106 |
$args, |
| 107 |
function ( $e ) { |
| 108 |
return ! is_null( $e ); |
| 109 |
} |
| 110 |
); |
| 111 |
|
| 112 |
// Add user meta to Log meta. |
| 113 |
$logs_meta['user_meta'] = $user_meta; |
| 114 |
|
| 115 |
$recordarr = array( |
| 116 |
'site_id' => (int) $site_id, |
| 117 |
'user_id' => (int) $user_id, |
| 118 |
'item' => (string) vsprintf( $message, $args ), |
| 119 |
'connector' => (string) $connector, |
| 120 |
'context' => (string) $context, |
| 121 |
'action' => (string) $action, |
| 122 |
'duration' => $dura, |
| 123 |
'created' => time(), |
| 124 |
'state' => $state, |
| 125 |
'meta' => (array) $logs_meta, |
| 126 |
); |
| 127 |
|
| 128 |
if ( 0 === $recordarr['site_id'] ) { |
| 129 |
unset( $recordarr['site_id'] ); |
| 130 |
} |
| 131 |
|
| 132 |
if ( null === $recordarr['state'] || '' === $recordarr['state'] ) { |
| 133 |
unset( $recordarr['state'] ); |
| 134 |
} |
| 135 |
// This is helpful in development environments. |
| 136 |
// error_log( $this->debug_backtrace( $recordarr ) ); //phpcs:ignore -- development. |
| 137 |
return $this->manager->db->insert( $recordarr ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Helper function to send a full backtrace of calls to the PHP error log for debugging. |
| 142 |
* |
| 143 |
* @param array $recordarr Record argument array. |
| 144 |
* |
| 145 |
* @return string $output MainWP Pro Reports backtrace. |
| 146 |
*/ |
| 147 |
public function debug_backtrace( $recordarr ) { |
| 148 |
// Record details. |
| 149 |
$message = isset( $recordarr['item'] ) ? $recordarr['item'] : null; |
| 150 |
$author = isset( $recordarr['author'] ) ? $recordarr['author'] : null; |
| 151 |
$connector = isset( $recordarr['connector'] ) ? $recordarr['connector'] : null; |
| 152 |
$context = isset( $recordarr['context'] ) ? $recordarr['context'] : null; |
| 153 |
$action = isset( $recordarr['action'] ) ? $recordarr['action'] : null; |
| 154 |
|
| 155 |
// Log meta. |
| 156 |
$logs_meta = isset( $recordarr['meta'] ) ? $recordarr['meta'] : null; |
| 157 |
|
| 158 |
unset( $logs_meta['user_meta'] ); |
| 159 |
|
| 160 |
if ( $logs_meta ) { |
| 161 |
array_walk( |
| 162 |
$logs_meta, |
| 163 |
function ( &$value, $key ) { |
| 164 |
$value = sprintf( '%s: %s', $key, ( '' === $value ) ? 'null' : $value ); |
| 165 |
} |
| 166 |
); |
| 167 |
$logs_meta = implode( ', ', $logs_meta ); |
| 168 |
} |
| 169 |
|
| 170 |
// User meta. |
| 171 |
$user_meta = isset( $recordarr['meta']['user_meta'] ) ? $recordarr['meta']['user_meta'] : null; |
| 172 |
|
| 173 |
if ( $user_meta ) { |
| 174 |
array_walk( |
| 175 |
$user_meta, |
| 176 |
function ( &$value, $key ) { |
| 177 |
$value = sprintf( '%s: %s', $key, ( '' === $value ) ? 'null' : $value ); |
| 178 |
} |
| 179 |
); |
| 180 |
|
| 181 |
$user_meta = implode( ', ', $user_meta ); |
| 182 |
} |
| 183 |
|
| 184 |
// Debug backtrace. |
| 185 |
ob_start(); |
| 186 |
|
| 187 |
// @codingStandardsIgnoreStart |
| 188 |
debug_print_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // Option to ignore args requires PHP 5.3.6 |
| 189 |
// @codingStandardsIgnoreEnd |
| 190 |
|
| 191 |
$backtrace = ob_get_clean(); |
| 192 |
$backtrace = array_values( array_filter( explode( "\n", $backtrace ) ) ); |
| 193 |
|
| 194 |
return sprintf( |
| 195 |
"Pro Reports Debug Backtrace\n\n Summary | %s\n Author | %s\n Connector | %s\n Context | %s\n Action | %s\nReports Meta | %s\nAuthor Meta | %s\n\n%s\n", |
| 196 |
$message, |
| 197 |
$author, |
| 198 |
$connector, |
| 199 |
$context, |
| 200 |
$action, |
| 201 |
$logs_meta, |
| 202 |
$user_meta, |
| 203 |
implode( "\n", $backtrace ) |
| 204 |
); |
| 205 |
} |
| 206 |
} |
| 207 |
|