PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.4
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.4
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 5.2.1 5.2.2 5.3 All 153 releases
mainwp / modules / logs / classes / class-log.php

class-log.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.4, at modules/logs/classes/class-log.php

218 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_json'] = wp_json_encode( $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->log_record( $recordarr );
138 }
139
140 /**
141 * Log record.
142 *
143 * @param array $recordarr sprintf (and extra) arguments to use.
144 *
145 * @return bool|WP_Error True if updated, otherwise false|WP_Error
146 */
147 public function log_record( $recordarr ) { //phpcs:ignore -- NOSONAR - compatible.
148 return $this->manager->db->insert( $recordarr );
149 }
150
151 /**
152 * Helper function to send a full backtrace of calls to the PHP error log for debugging.
153 *
154 * @param array $recordarr Record argument array.
155 *
156 * @return string $output MainWP Pro Reports backtrace.
157 */
158 public function debug_backtrace( $recordarr ) {
159 // Record details.
160 $message = isset( $recordarr['item'] ) ? $recordarr['item'] : null;
161 $author = isset( $recordarr['author'] ) ? $recordarr['author'] : null;
162 $connector = isset( $recordarr['connector'] ) ? $recordarr['connector'] : null;
163 $context = isset( $recordarr['context'] ) ? $recordarr['context'] : null;
164 $action = isset( $recordarr['action'] ) ? $recordarr['action'] : null;
165
166 // Log meta.
167 $logs_meta = isset( $recordarr['meta'] ) ? $recordarr['meta'] : null;
168
169 unset( $logs_meta['user_meta'] );
170
171 if ( $logs_meta ) {
172 array_walk(
173 $logs_meta,
174 function ( &$value, $key ) {
175 $value = sprintf( '%s: %s', $key, ( '' === $value ) ? 'null' : $value );
176 }
177 );
178 $logs_meta = implode( ', ', $logs_meta );
179 }
180
181 // User meta.
182 $user_meta = isset( $recordarr['meta']['user_meta'] ) ? $recordarr['meta']['user_meta'] : null;
183
184 if ( $user_meta ) {
185 array_walk(
186 $user_meta,
187 function ( &$value, $key ) {
188 $value = sprintf( '%s: %s', $key, ( '' === $value ) ? 'null' : $value );
189 }
190 );
191
192 $user_meta = implode( ', ', $user_meta );
193 }
194
195 // Debug backtrace.
196 ob_start();
197
198 // @codingStandardsIgnoreStart
199 debug_print_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // Option to ignore args requires PHP 5.3.6
200 // @codingStandardsIgnoreEnd
201
202 $backtrace = ob_get_clean();
203 $backtrace = array_values( array_filter( explode( "\n", $backtrace ) ) );
204
205 return sprintf(
206 "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",
207 $message,
208 $author,
209 $connector,
210 $context,
211 $action,
212 $logs_meta,
213 $user_meta,
214 implode( "\n", $backtrace )
215 );
216 }
217 }
218