@@ -47,4 +47,74 @@ public function test_get_post_templates_child_theme() {
4747 $ post_templates ['page ' ]
4848 );
4949 }
50+
51+ /**
52+ * @ticket 42513
53+ */
54+ public function test_get_post_templates_caches_results () {
55+ $ theme = wp_get_theme ( 'page-templates ' );
56+ $ this ->assertNotEmpty ( $ theme );
57+
58+ $ filter = new MockAction ();
59+ add_filter ( 'extra_theme_headers ' , array ( $ filter , 'filter ' ) );
60+
61+ // First call populates the cache.
62+ $ first_result = $ theme ->get_post_templates ();
63+ $ this ->assertNotEmpty ( $ first_result );
64+ $ filter_call_count = $ filter ->get_call_count ();
65+ $ this ->assertGreaterThan ( 0 , $ filter_call_count , 'The `extra_theme_headers` filter should be called at least once. ' );
66+
67+ // Second call should return the same result from cache.
68+ $ second_result = $ theme ->get_post_templates ();
69+ $ this ->assertSame ( $ first_result , $ second_result );
70+ $ this ->assertSame ( $ filter_call_count , $ filter ->get_call_count (), 'The `extra_theme_headers` filter should not have extra calls. ' );
71+ }
72+
73+ /**
74+ * @ticket 42513
75+ */
76+ public function test_get_post_templates_clears_cache_on_theme_switch () {
77+ $ theme = wp_get_theme ( 'page-templates ' );
78+ $ this ->assertNotEmpty ( $ theme );
79+
80+ $ filter = new MockAction ();
81+ add_filter ( 'extra_theme_headers ' , array ( $ filter , 'filter ' ) );
82+
83+ // Populate cache.
84+ $ theme ->get_post_templates ();
85+
86+ $ filter_call_count = $ filter ->get_call_count ();
87+ $ this ->assertGreaterThan ( 0 , $ filter_call_count , 'The `extra_theme_headers` filter should be called at least once. ' );
88+
89+ $ child_theme = wp_get_theme ( 'page-templates-child ' );
90+ switch_theme ( $ child_theme ['Template ' ], $ child_theme ['Stylesheet ' ] );
91+
92+ $ child_templates = $ child_theme ->get_post_templates ();
93+
94+ // Child theme should include its own templates.
95+ $ this ->assertArrayHasKey ( 'foo ' , $ child_templates );
96+ $ this ->assertArrayHasKey ( 'template-top-level-post-types-child.php ' , $ child_templates ['foo ' ] );
97+ $ this ->assertGreaterThan ( $ filter_call_count , $ filter ->get_call_count (), 'The `extra_theme_headers` filter should have extra calls. ' );
98+ }
99+
100+ /**
101+ * @ticket 42513
102+ */
103+ public function test_get_post_templates_uses_get_file_data () {
104+ $ theme = wp_get_theme ( 'page-templates ' );
105+ $ this ->assertNotEmpty ( $ theme );
106+
107+ $ filter = new MockAction ();
108+ add_filter ( 'extra_theme_headers ' , array ( $ filter , 'filter ' ) );
109+
110+ $ post_templates = $ theme ->get_post_templates ();
111+
112+ // Verify single-line header format is parsed correctly via get_file_data().
113+ $ this ->assertArrayHasKey ( 'page ' , $ post_templates );
114+ $ this ->assertArrayHasKey ( 'template-header.php ' , $ post_templates ['page ' ] );
115+ $ this ->assertSame ( 'This Template Header Is On One Line ' , $ post_templates ['page ' ]['template-header.php ' ] );
116+
117+ // Verify the `extra_theme_headers` filter is called.
118+ $ this ->assertGreaterThan ( 0 , $ filter ->get_call_count (), 'The `extra_theme_headers` filter should be called at least once. ' );
119+ }
50120}
0 commit comments