PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.3 6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 All 156 releases
← All changes | modules/logs/classes/class-log.php +74 -19 5.1 → 6.2.2 View file →
@@ -8,8 +8,15 @@
8 8 */
9 9
10 10 namespace MainWP\Dashboard\Module\Log;
11 11
12 +use MainWP\Dashboard\MainWP_Execution_Helper;
13 +
14 +// Exit if accessed directly.
15 +if ( ! defined( 'ABSPATH' ) ) {
16 + exit;
17 +}
18 +
12 19 /**
13 20 * Class Logs
14 21 */
15 22 class Log {
@@ -56,8 +63,14 @@
56 63 if ( is_null( $site_id ) ) {
57 64 $site_id = 0;
58 65 }
59 66
67 + $disable_cron_log = apply_filters( 'mainwp_module_log_disable_cron_log', false, $connector, $message, $args, $site_id, $context, $action, $user_id, $state );
68 +
69 + if ( $disable_cron_log && defined( 'DOING_CRON' ) && DOING_CRON ) {
70 + return false; // disable log cron tasks.
71 + }
72 +
60 73 $cron_tracking = apply_filters( 'mainwp_module_log_cron_tracking', true, $connector, $message, $args, $site_id, $context, $action, $user_id, $state );
61 74
62 75 $author = new Log_Author( $user_id );
63 76
@@ -67,24 +80,37 @@
67 80 if ( ! $cron_tracking && 'wp_cron' === $agent ) {
68 81 return false;
69 82 }
70 83
71 - $user = new \WP_User( $user_id );
84 + $user = new \WP_User( $user_id );
85 + $user_login = ! empty( $user->user_login ) ? (string) $user->user_login : '';
72 86
73 87 $user_meta = array(
74 - 'user_login' => (string) ! empty( $user->user_login ) ? $user->user_login : '',
88 + 'user_login' => $user_login,
75 89 'agent' => (string) $agent,
76 90 );
77 91
78 - if ( 'wp_cli' === $agent && function_exists( 'posix_getuid' ) ) {
79 - $uid = posix_getuid();
80 - $user_info = posix_getpwuid( $uid );
92 + $system_user = '';
93 + if ( 'wp_cli' === $agent ) {
94 + $system_user = 'wp_cli';
95 + if ( is_callable( 'posix_getuid' ) && is_callable( 'posix_getpwuid' ) ) {
96 + $uid = posix_getuid();
97 + $user_info = posix_getpwuid( $uid );
81 98
82 - $user_meta['system_user_id'] = (int) $uid;
83 - $user_meta['system_user_name'] = (string) $user_info['name'];
99 + $user_meta['system_user_id'] = (int) $uid;
100 + $user_meta['system_user_name'] = (string) $user_info['name'];
101 +
102 + if ( ! empty( $user_meta['system_user_name'] ) ) {
103 + $system_user = $user_meta['system_user_name'];
104 + }
105 + }
106 + } elseif ( 'wp_cron' === $agent ) {
107 + $system_user = 'wp_cron';
108 + } elseif ( 'wp_rest_api' === $agent ) {
109 + $system_user = 'wp_rest_api';
84 110 }
85 111
86 - $dura = isset( $args['duration'] ) ? floatval( $args['duration'] ) : $this->manager->executor->get_exec_time();
112 + $dura = isset( $args['duration'] ) ? floatval( $args['duration'] ) : MainWP_Execution_Helper::get_run_time();
87 113
88 114 if ( isset( $args['duration'] ) ) {
89 115 unset( $args['duration'] );
90 116 }
@@ -100,8 +126,17 @@
100 126 }
101 127
102 128 $dura = $dura / $dura_bulk;
103 129
130 + $created = microtime( true );
131 +
132 + if ( ! empty( $args['created'] ) ) {
133 + $created = (float) $args['created'];
134 + unset( $args['created'] );
135 + }
136 +
137 + $created = Log_Manager::normalize_to_microseconds( $created ); // Store timestamps (including microseconds) as a BIGINT.
138 +
104 139 // Prevent any meta with null values from being logged.
105 140 $logs_meta = array_filter(
106 141 $args,
107 142 function ( $e ) {
@@ -108,22 +143,31 @@
108 143 return ! is_null( $e );
109 144 }
110 145 );
111 146
147 + if ( empty( $logs_meta['user_login'] ) ) {
148 + if ( ! empty( $user_meta['user_login'] ) ) {
149 + $logs_meta['user_login'] = $user_meta['user_login'];
150 + } elseif ( ! empty( $system_user ) ) {
151 + $logs_meta['user_login'] = $system_user;
152 + }
153 + }
154 +
112 155 // Add user meta to Log meta.
113 - $logs_meta['user_meta'] = $user_meta;
156 + $logs_meta['user_meta_json'] = wp_json_encode( $user_meta );
114 157
115 158 $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,
159 + 'site_id' => (int) $site_id,
160 + 'user_id' => (int) $user_id,
161 + 'user_login' => (string) $user_login,
162 + 'item' => (string) vsprintf( $message, $args ),
163 + 'connector' => (string) $connector,
164 + 'context' => (string) $context,
165 + 'action' => (string) $action,
166 + 'duration' => $dura,
167 + 'created' => $created,
168 + 'state' => $state,
169 + 'meta' => (array) $logs_meta,
126 170 );
127 171
128 172 if ( 0 === $recordarr['site_id'] ) {
129 173 unset( $recordarr['site_id'] );
@@ -133,8 +177,19 @@
133 177 unset( $recordarr['state'] );
134 178 }
135 179 // This is helpful in development environments.
136 180 // error_log( $this->debug_backtrace( $recordarr ) ); //phpcs:ignore -- development.
181 + return $this->log_record( $recordarr );
182 + }
183 +
184 + /**
185 + * Log record.
186 + *
187 + * @param array $recordarr sprintf (and extra) arguments to use.
188 + *
189 + * @return bool|WP_Error True if updated, otherwise false|WP_Error
190 + */
191 + public function log_record( $recordarr ) { //phpcs:ignore -- NOSONAR - compatible.
137 192 return $this->manager->db->insert( $recordarr );
138 193 }
139 194
140 195 /**