Make WordPress Core

Ticket #49194: 49194-sort-p…tes-by-title.diff

File 49194-sort-p…tes-by-title.diff, 2.4 KB (added by mattwatsoncodes, 2 months ago)

Patch to sort post templates by translated, human-readable template name instead of filename/slug, with PHPUnit test coverage.

  • src/wp-includes/class-wp-theme.php

    diff --git a/src/wp-includes/class-wp-theme.php b/src/wp-includes/class-wp-theme.php
    index 6952c59bda..d7cf008a59 100644
    a b final class WP_Theme implements ArrayAccess {  
    13851385                        }
    13861386                }
    13871387
     1388                /*
     1389                 * Sort post templates by their translated, human-readable template name
     1390                 * instead of by filename or slug.
     1391                 */
     1392                foreach ( array_keys( $post_templates ) as $type ) {
     1393                        if ( ! is_array( $post_templates[ $type ] ) ) {
     1394                                continue;
     1395                        }
     1396
     1397                        uasort( $post_templates[ $type ], 'strnatcasecmp' );
     1398                }
     1399
    13881400                return $post_templates;
    13891401        }
    13901402
  • new file tests/phpunit/data/themedir1/page-templates/template-a-zebra.php

    diff --git a/tests/phpunit/data/themedir1/page-templates/template-a-zebra.php b/tests/phpunit/data/themedir1/page-templates/template-a-zebra.php
    new file mode 100644
    index 0000000000..a5824664c7
    - +  
     1<?php
     2/*
     3   Template Name: Zebra Template
     4 */
     5?>
  • new file tests/phpunit/data/themedir1/page-templates/template-z-alpha.php

    diff --git a/tests/phpunit/data/themedir1/page-templates/template-z-alpha.php b/tests/phpunit/data/themedir1/page-templates/template-z-alpha.php
    new file mode 100644
    index 0000000000..40a1e24c4b
    - +  
     1<?php
     2/*
     3   Template Name: Alpha Template
     4 */
     5?>
  • tests/phpunit/tests/theme/wpTheme.php

    diff --git a/tests/phpunit/tests/theme/wpTheme.php b/tests/phpunit/tests/theme/wpTheme.php
    index a06fba32c5..6803696867 100644
    a b class Tests_Theme_wpTheme extends WP_UnitTestCase {  
    348348                $this->assertCount( 2, $filter->get_events(), 'Should only be 4, as second run should not be cached' );
    349349        }
    350350
     351        /**
     352         * @ticket 49194
     353         *
     354         * @covers WP_Theme::get_post_templates
     355         */
     356        public function test_get_post_templates_should_sort_templates_by_template_name() {
     357                $theme = new WP_Theme( 'page-templates', $this->theme_root );
     358
     359                $post_templates = $theme->get_post_templates();
     360
     361                $this->assertArrayHasKey( 'page', $post_templates );
     362
     363                $actual = array_intersect_key(
     364                        $post_templates['page'],
     365                        array(
     366                                'template-z-alpha.php' => true,
     367                                'template-a-zebra.php' => true,
     368                        )
     369                );
     370
     371                $this->assertSame(
     372                        array(
     373                                'template-z-alpha.php' => 'Alpha Template',
     374                                'template-a-zebra.php' => 'Zebra Template',
     375                        ),
     376                        $actual
     377                );
     378        }
     379
    351380        /**
    352381         * Test get_files for an existing theme.
    353382         *