$\newcommand{\set}[1]{\{#1\}}$ $\newcommand{\mc}{\mathcal}$ $\newcommand{\Z}{\mathbb Z}$ $\newcommand{\C}{\mathbb C}$ $\newcommand{\R}{\mathbb R}$
Definitions
Let $(X, \mc X, \mu)$ be probability space and $T:X\to X$ be an invertible measure preserving transformation. Let us write $L^2$ to mean $L^2(X, \mc X, \mu)$. Let $U_T$ be the associated Koopman operator on $L^2$. We may write $Tf$ in place of $U_Tf$.
We say that $\lambda\in \C$ is an eigenvalue of the measure preserving system $(X, T)$ if there is a nonzero function $f\in L^2$ such that $Tf=\lambda f$. Given an eigenvalue $\lambda$, we say that $f\in L^2$ is an eigenfunction corresponding to $\lambda$ if $Tf=\lambda f$. Let $\mc X_1$ denote the $\sigma$-algebra generated by the set of all the eigenfunctions. Let $H_{pp}$ be the closure of the span of all the eigenfunctions.
We say that $f\in L^2$ is almost periodic if the closure of $\set{T^nf:\ n\in \Z}$ is compact in $L^2$. It is shows in Proposition 2 of this blog post of Tao, assuming ergodicity of $T$, that$f$ is almost periodic if and only if $f$ is measurable with respect to $\mc X_1$. In other words, $f$ is almost periodic if and only if $f\in L^2(X, \mc X_1, \mu)$.
Question
Exercise 5 in this blog post of Tao asks to show the following.
Exercise. Assume $T$ is $\mu$-ergodic and $f\in L^2$ be given. Then $f\in L^2(X, \mc X_1, \mu)$ if and only if $f$ is in $H_{pp}$.
(I do not think ergodicty is required but right now I am content with the ergodic case.)
The hint given is that first one may use the fact that $f\in L^2(X, \mc X_1, \mu)$ if and only if $f$ if almost periodic and also use the fact that the product of two eigenfunctions is also an eigenfunction. I am unable to see how this hint helps solve the question at hand. Independent of the hint, I thought of using the spectral theorem to push the information to $\mathbb T=\R/\Z$. The almost periodicity of $f$ in $L^2$ gives that the constant function $1$ is almost periodic in $L^2(\mathbb T, \nu)$, where $\nu$ is the spectral measure corresponding to $f$. However, I couldn't make any progress using this.
This is just an outline of the proof of Exercise 5.
To prove $L^2(X,\mathcal X_1,\mu) \subset H_{pp}$, assume $f\in L^2(X,\mathcal X_1,\mu)$. Since $\mathcal X_1$ is the smallest $\sigma$-algebra of sets with respect to which all the eigenfunctions are measurable, we know that $f$ can be approximated in $L^2(\mu)$ by a linear combination of products of eigenfunctions. Since products of eigenfunctions are again eigenfunctions, this means that $f$ can be approximated by a linear combination of eigenfunctions. Thus $f\in H_{pp}$.
To prove the reverse inclusion, assume $f\in H_{pp}$. Then $f$ can be approximated by linear combinations of eigenfunctions. Since all eigenfunctions are measurable with respect to $\mathcal X_1$, this means that $f$ can be approximated by elements of $L^2(X,\mathcal X_1,\mu)$. Since the latter space is closed, we have that $f\in L^2(X,\mathcal X_1,\mu)$.