$requested_mimes Optional MIME types to narrow to. Ignored when empty. * * @return array Extension-regex => MIME type, WordPress's native shape. * @since 1.4.0 */ public static function for_user( int $user_id, array $requested_mimes = [] ): array { $allowed = get_allowed_mime_types( $user_id ); if ( empty( $requested_mimes ) ) { return $allowed; } return self::intersect( $allowed, $requested_mimes ); } /** * Narrow an allowlist to only the entries matching a set of MIME types. * * @param array $allowlist Extension-regex => MIME type. * @param array $requested_mimes MIME types to keep. * * @return array The narrowed allowlist. Empty when nothing matched. * @since 1.4.0 */ public static function intersect( array $allowlist, array $requested_mimes ): array { $requested = array_map( 'strtolower', array_map( 'strval', $requested_mimes ) ); $narrowed = []; foreach ( $allowlist as $ext => $mime ) { if ( in_array( strtolower( (string) $mime ), $requested, true ) ) { $narrowed[ $ext ] = $mime; } } return $narrowed; } /** * Flatten an allowlist to a deduplicated, human/model-readable MIME type list. * * @param array $allowlist Extension-regex => MIME type. * * @return array Unique MIME types. * @since 1.4.0 */ public static function mime_list( array $allowlist ): array { return array_values( array_unique( array_values( $allowlist ) ) ); } }