Structural and Magnetic Phase Transitions near Optimal Superconductivity in BaFe2(As1−xPx)2