Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/wp-includes/class-wp-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,7 @@ public function get_files( $type = null, $depth = 0, $search_parent = false ) {
*
* @since 4.7.0
* @since 5.8.0 Include block templates.
* @since 7.2.0 Templates are sorted by their translated name.
*
* @return array[] Array of page template arrays, keyed by post type and filename,
* with the value of the translated header name.
Expand Down Expand Up @@ -1392,8 +1393,15 @@ public function get_post_templates() {
$post_template = $this->translate_header( 'Template Name', $post_template );
}
}
unset( $post_type, $post_template );
}

// Sort each post type's templates by their translated name, keeping the file names as keys.
foreach ( $post_templates as &$post_type ) {
uasort( $post_type, 'strnatcasecmp' );
}
unset( $post_type );

return $post_templates;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
/*
* Template Name: Zebra Template
*/
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
/*
* Template Name: mango template
*/
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
/*
* Template Name: Apple Template
*/
?>
3 changes: 3 additions & 0 deletions tests/phpunit/data/themedir1/page-templates-sort/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
// Intentionally left blank.
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
/*
* Template Name: Section 10
*/
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
/*
* Template Name: Section 2
*/
?>
11 changes: 11 additions & 0 deletions tests/phpunit/data/themedir1/page-templates-sort/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Theme Name: Page Template Sorting Theme
Theme URI: http://example.org/
Description: An example theme for testing that page templates are sorted by name.
Version: 0.1
Author: Mr. WordPress
Author URI: http://wordpress.org/

This is just a stub to test that get_post_templates() sorts by the template name.

*/
1 change: 1 addition & 0 deletions tests/phpunit/tests/theme/themeDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function test_theme_list() {
'My Subdir Theme', // Theme in a subdirectory should work.
'Page Template Child Theme', // Theme which inherits page templates.
'Page Template Theme', // Theme with page templates for other test code.
'Page Template Sorting Theme', // Theme for testing page templates are sorted by name.
'Theme with Spaces in the Directory',
'Internationalized Theme',
'Custom Internationalized Theme',
Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/tests/theme/wpThemeGetPostTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,29 @@ public function test_get_post_templates_uses_get_file_data() {
// Verify the `extra_theme_headers` filter is called.
$this->assertGreaterThan( 0, $filter->get_call_count(), 'The `extra_theme_headers` filter should be called at least once.' );
}

/**
* Templates should be ordered by their human-readable name, not by the file name.
*
* @ticket 49194
*/
public function test_get_post_templates_are_sorted_by_name() {
$theme = wp_get_theme( 'page-templates-sort' );
$this->assertNotEmpty( $theme );

$post_templates = $theme->get_post_templates();
$this->assertArrayHasKey( 'page', $post_templates );

// Templates are sorted by name (case-insensitive, natural order), keeping the file names as keys.
$this->assertSame(
array(
'c-template.php' => 'Apple Template',
'b-template.php' => 'mango template',
'section-2.php' => 'Section 2',
'section-10.php' => 'Section 10',
'a-template.php' => 'Zebra Template',
),
$post_templates['page']
);
}
}
Loading