*/ class Logger { /** * Add log. * * @since 2.5.0 * @param string $message * @param string $file * @param string $type * @return void */ public static function add_log( $message, $file, $type ) { self::add_log_to_db( $message, $file, $type ); } /** * Add log Action. * * @since 2.6.9 * @param string $message * @return void */ public static function add_log_action( $message ) { self::add_log_action_to_db( $message ); } /** * Log an option written from an admin-ajax handler. * * The audit trail records which option was touched and whether its value * changed, never the value itself: a FastCMP style or a custom variable can * carry content the log must not duplicate. Only a fingerprint of the old * and new values is kept, with the acting user's ID — no username, the * debug endpoint serves these logs and must not leak account names. * * A write that leaves the value unchanged is not logged: the CMP tab posts * every setting on each save, and `logs` keeps only the last 100 lines, so * logging them all would push out the errors support relies on. * * @since 4.4.0 * @param string $option The full option name written. * @param mixed $old_value The value before the write, null when the row did not exist. * @param mixed $new_value The value written. * @return void */ public static function add_option_write( $option, $old_value, $new_value ) { $old_hash = self::hash_value( $old_value ); $new_hash = self::hash_value( $new_value ); if ( $old_hash === $new_hash ) { return; } $message = sprintf( 'Option "%s" written from admin-ajax (old: %s, new: %s, user ID: %d)', $option, $old_hash, $new_hash, get_current_user_id() ); self::add_log_to_db( $message, 'admin-ajax', 'info' ); } /** * Get logs. * * @since 2.5.0 * @return array> */ public static function get_logs() { /** @var array> $clear_logs */ $clear_logs = self::get_clean_logs( 'logs' ); $clean_logs = array(); foreach ( $clear_logs as $log ) { $clean_logs[] = array( 'datelog' => $log['datelog'], 'message' => $log['message'], 'file' => $log['file'], 'type' => $log['type'], ); } return $clean_logs; } /** * Get logs action. * * @since 2.5.0 * @return array> */ public static function get_logs_action() { /** @var array> $clear_logs */ $clear_logs = self::get_clean_logs( 'logs_action' ); $clean_logs = array(); foreach ( $clear_logs as $log ) { $clean_logs[] = array( 'datelog' => $log['datelog'], 'message' => $log['message'], 'user' => $log['user'], ); } return $clean_logs; } /** * Clear old logs. * * @since 2.5.0 * @param array> $logs * @return array> */ public static function clear_old_logs( $logs ) { $logs = array_filter( $logs, /** * @param array<'datelog'|'file'|'message'|'type'|'user', string> $log * @return bool */ function ( $log ) { $now = new \DateTime(); $log_date = new \DateTime( $log['datelog'] ); $diff = $now->diff( $log_date ); if ( $diff->days > 7 ) { return false; } return true; } ); // a maximum of 100 lines must remain if ( count( $logs ) > 100 ) { array_splice( $logs, 0, count( $logs ) - 100 ); } return $logs; } /** * Add log to database. * * @since 2.5.0 * @param string $message * @param string $file * @param string $type * @return void */ private static function add_log_to_db( $message, $file, $type ) { /** @var array> $current_log */ $current_log = self::get_clean_logs( 'logs' ); array_push( $current_log, array( 'message' => $message, 'file' => $file, 'type' => $type, 'datelog' => gmdate( 'Y-m-d H:i:s' ), ) ); Options::update( 'logs', $current_log ); } /** * Add log action to database. * * @since 2.6.9 * @param string $message * @return void */ private static function add_log_action_to_db( $message ) { /** @var array> $current_log */ $current_log = self::get_clean_logs( 'logs_action' ); $current_user = wp_get_current_user(); array_push( $current_log, array( 'message' => $message, 'user' => $current_user->user_login . ' (ID: ' . $current_user->ID . ')', 'datelog' => gmdate( 'Y-m-d H:i:s' ), ) ); Options::update( 'logs_action', $current_log ); } /** * A short fingerprint of an option value for the audit trail. * * `Options::get()` reads a stored "0" or "1" back as a boolean while the * posted value is still the string, so both are cast the same way here: * otherwise an unchanged switch would hash differently on each side. * * @since 4.4.0 * @param mixed $value * @return string The first 16 hex digits of the value's SHA-256, or "none" when the row did not exist. */ private static function hash_value( $value ) { if ( $value === null ) { return 'none'; } if ( $value === '0' || $value === '1' ) { $value = (bool) $value; } $serialized = is_string( $value ) ? $value : (string) wp_json_encode( $value ); return substr( hash( 'sha256', $serialized ), 0, 16 ); } /** * Get clean logs. * * @since 2.6.9 * @param string $logs_name * @return array> */ private static function get_clean_logs( $logs_name ) { $logs = Options::get( $logs_name, array() ); if ( empty( $logs ) ) { return array(); } if ( ! is_array( $logs ) ) { return array(); } /** @var array> $logs */ $current_log = self::clear_old_logs( $logs ); return $current_log; } }