Event type => label for the log's filter. */ public static function event_types(): array { return array( self::UPLOADED => 'Media Uploaded', self::DELETED => 'Media Deleted', ); } public static function register(): void { add_action( 'add_attachment', array( __CLASS__, 'log_uploaded' ) ); add_action( 'delete_attachment', array( __CLASS__, 'log_deleted' ) ); } /** * @param int $post_id The attachment. */ public static function log_uploaded( int $post_id ): void { self::log( self::UPLOADED, 'Media "%s" uploaded', $post_id ); } /** * @param int $post_id The attachment. */ public static function log_deleted( int $post_id ): void { self::log( self::DELETED, 'Media "%s" deleted', $post_id ); } /** * @param string $type Event type. * @param string $summary Summary format; the file name fills it. * @param int $post_id The attachment. */ private static function log( string $type, string $summary, int $post_id ): void { $post = get_post( $post_id ); if ( ! $post || 'attachment' !== $post->post_type ) { return; } $file = get_post_meta( $post_id, '_wp_attached_file', true ); $name = is_string( $file ) && '' !== $file ? wp_basename( $file ) : (string) $post->post_title; $name = EventLogger::bounded( $name, self::FILE_NAME_LENGTH ); EventLogger::log( $type, sprintf( $summary, $name ), 'attachment', $post_id, array( 'file' => $name, 'mime_type' => EventLogger::bounded( (string) $post->post_mime_type, 100 ), ) ); } }