PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.3
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.3
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.0.3, at modules/logs/classes/class-log.php

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