From c75aafda607803c5c897c35505bb68c586fb330d Mon Sep 17 00:00:00 2001 From: robelsust Date: Tue, 10 Nov 2026 11:05:51 +0600 Subject: [PATCH 1/2] Themes: Restore dynamic tabs in the Add Themes screen via `install_themes_tabs`. Re-enables the `install_themes_tabs` filter on `wp-admin/theme-install.php` to allow registering and filtering browse tabs in the Backbone-based theme installer, matching parity with `install_plugins_tabs`. Passes the default active tab to `_wpThemeSettings` for seamless Backbone routing, escapes the tab selector attribute, and adds support for legacy `?tab=:sort` URLs. Props therssoftware, pbiron, audrasjb. See #65979. --- src/js/_enqueues/wp/theme.js | 13 +- src/wp-admin/theme-install.php | 50 ++-- .../phpunit/tests/theme/installThemesTabs.php | 265 ++++++++++++++++++ 3 files changed, 306 insertions(+), 22 deletions(-) create mode 100644 tests/phpunit/tests/theme/installThemesTabs.php diff --git a/src/js/_enqueues/wp/theme.js b/src/js/_enqueues/wp/theme.js index dabd64cc6c6ba..5ee03d80393d7 100644 --- a/src/js/_enqueues/wp/theme.js +++ b/src/js/_enqueues/wp/theme.js @@ -1802,7 +1802,7 @@ themes.view.Installer = themes.view.Appearance.extend({ .removeClass( this.activeClass ) .removeAttr( 'aria-current' ); - $( '[data-sort="' + sort + '"]' ) + $( '[data-sort="' + $.escapeSelector( sort ) + '"]' ) .addClass( this.activeClass ) .attr( 'aria-current', 'page' ); @@ -2013,6 +2013,7 @@ themes.InstallerRouter = Backbone.Router.extend({ routes: { 'theme-install.php?theme=:slug': 'preview', 'theme-install.php?browse=:sort': 'sort', + 'theme-install.php?tab=:sort': 'sort', 'theme-install.php?search=:query': 'search', 'theme-install.php': 'sort' }, @@ -2036,10 +2037,12 @@ themes.InstallerRouter = Backbone.Router.extend({ themes.RunInstaller = { init: function() { + var defaultTab = ( themes.data.settings && themes.data.settings.defaultTab ) || 'popular'; + // Set up the view. // Passes the default 'section' as an option. this.view = new themes.view.Installer({ - section: 'popular', + section: defaultTab, SearchView: themes.view.InstallerSearch }); @@ -2106,12 +2109,12 @@ themes.RunInstaller = { /* * Handles sorting / browsing routes. * Also handles the root URL triggering a sort request - * for `popular`, the default view. + * for the default view. */ themes.router.on( 'route:sort', function( sort ) { if ( ! sort ) { - sort = 'popular'; - themes.router.navigate( themes.router.baseUrl( '?browse=popular' ), { replace: true } ); + sort = ( themes.data.settings && themes.data.settings.defaultTab ) || 'popular'; + themes.router.navigate( themes.router.baseUrl( themes.router.browsePath + sort ), { replace: true } ); } self.view.sort( sort ); diff --git a/src/wp-admin/theme-install.php b/src/wp-admin/theme-install.php index 2532b30766ccf..7257c1598d354 100644 --- a/src/wp-admin/theme-install.php +++ b/src/wp-admin/theme-install.php @@ -10,7 +10,7 @@ require_once __DIR__ . '/admin.php'; require ABSPATH . 'wp-admin/includes/theme-install.php'; -$tab = ! empty( $_REQUEST['tab'] ) ? sanitize_text_field( $_REQUEST['tab'] ) : ''; +$tab = ! empty( $_REQUEST['tab'] ) ? sanitize_text_field( $_REQUEST['tab'] ) : ( ! empty( $_REQUEST['browse'] ) ? sanitize_text_field( $_REQUEST['browse'] ) : '' ); if ( ! current_user_can( 'install_themes' ) ) { wp_die( __( 'Sorry, you are not allowed to install themes on this site.' ) ); @@ -42,6 +42,34 @@ } } +$tabs = array( + 'popular' => _x( 'Popular', 'themes' ), + 'new' => _x( 'Latest', 'themes' ), + 'block-themes' => _x( 'Block Themes', 'themes' ), + 'favorites' => _x( 'Favorites', 'themes' ), +); + +if ( current_user_can( 'upload_themes' ) ) { + /* + * No longer a real tab. Here for filter compatibility. + * Gets skipped in the filter links below. + */ + $tabs['upload'] = __( 'Upload Theme' ); +} + +/** + * Filters the tabs shown on the Add Themes screen. + * + * @since 2.8.0 + * + * @param string[] $tabs Associative array of the tabs shown on the Add Themes screen. Default keys + * include 'popular', 'new', 'block-themes', 'favorites', and 'upload'. + */ +$tabs = apply_filters( 'install_themes_tabs', $tabs ); + +$browse_tabs = array_diff_key( $tabs, array( 'upload' => true ) ); +$default_tab = ! empty( $browse_tabs ) ? (string) array_key_first( $browse_tabs ) : 'popular'; + wp_localize_script( 'theme', '_wpThemeSettings', @@ -52,6 +80,7 @@ 'canInstall' => current_user_can( 'install_themes' ), 'installURI' => current_user_can( 'install_themes' ) ? self_admin_url( 'theme-install.php' ) : null, 'adminUrl' => parse_url( self_admin_url(), PHP_URL_PATH ), + 'defaultTab' => $default_tab, ), 'l10n' => array( 'addNew' => __( 'Add Theme' ), @@ -165,18 +194,6 @@

- __( 'Upload Theme' ) ) ); if ( ! empty( $tabs['upload'] ) && current_user_can( 'upload_themes' ) ) { echo ' '; } @@ -210,10 +227,9 @@
diff --git a/tests/phpunit/tests/theme/installThemesTabs.php b/tests/phpunit/tests/theme/installThemesTabs.php new file mode 100644 index 0000000000000..2f12c41d23bdc --- /dev/null +++ b/tests/phpunit/tests/theme/installThemesTabs.php @@ -0,0 +1,265 @@ +user->create( array( 'role' => 'administrator' ) ); + self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) ); + } + + /** + * Simulates tabs computation in wp-admin/theme-install.php. + * + * @return array{tabs: array, browse_tabs: array, default_tab: string} + */ + private function get_theme_install_tabs() { + $tabs = array( + 'popular' => _x( 'Popular', 'themes' ), + 'new' => _x( 'Latest', 'themes' ), + 'block-themes' => _x( 'Block Themes', 'themes' ), + 'favorites' => _x( 'Favorites', 'themes' ), + ); + + if ( current_user_can( 'upload_themes' ) ) { + /* + * No longer a real tab. Here for filter compatibility. + * Gets skipped in the filter links below. + */ + $tabs['upload'] = __( 'Upload Theme' ); + } + + /** This filter is documented in wp-admin/theme-install.php */ + $tabs = apply_filters( 'install_themes_tabs', $tabs ); + + $browse_tabs = array_diff_key( $tabs, array( 'upload' => true ) ); + $default_tab = ! empty( $browse_tabs ) ? (string) array_key_first( $browse_tabs ) : 'popular'; + + return array( + 'tabs' => $tabs, + 'browse_tabs' => $browse_tabs, + 'default_tab' => $default_tab, + ); + } + + /** + * Tests that default tabs for an administrator include all core browse tabs and upload. + * + * @ticket 65979 + */ + public function test_default_tabs_for_user_with_upload_themes_cap() { + wp_set_current_user( self::$admin_id ); + + $result = $this->get_theme_install_tabs(); + + $expected_tab_keys = array( 'popular', 'new', 'block-themes', 'favorites', 'upload' ); + $this->assertSame( + $expected_tab_keys, + array_keys( $result['tabs'] ), + 'Default tabs array should contain popular, new, block-themes, favorites, and upload keys.' + ); + + $expected_browse_keys = array( 'popular', 'new', 'block-themes', 'favorites' ); + $this->assertSame( + $expected_browse_keys, + array_keys( $result['browse_tabs'] ), + 'Browse tabs array should exclude upload and retain core browse tab keys.' + ); + + $this->assertSame( + 'popular', + $result['default_tab'], + 'Default browse tab should be popular when no filter modifies the tabs array.' + ); + } + + /** + * Tests that default tabs for a user without upload_themes capability omit the upload tab. + * + * @ticket 65979 + */ + public function test_default_tabs_for_user_without_upload_themes_cap() { + wp_set_current_user( self::$subscriber_id ); + + $result = $this->get_theme_install_tabs(); + + $expected_tab_keys = array( 'popular', 'new', 'block-themes', 'favorites' ); + $this->assertSame( + $expected_tab_keys, + array_keys( $result['tabs'] ), + 'Tabs array for user without upload capability should not contain the upload tab.' + ); + + $this->assertSame( + $expected_tab_keys, + array_keys( $result['browse_tabs'] ), + 'Browse tabs array should match tabs array when upload is not present.' + ); + + $this->assertSame( + 'popular', + $result['default_tab'], + 'Default browse tab should remain popular for user without upload capability.' + ); + } + + /** + * Tests adding a custom browse tab via the install_themes_tabs filter. + * + * @ticket 65979 + */ + public function test_add_custom_tab_via_filter() { + wp_set_current_user( self::$admin_id ); + + add_filter( + 'install_themes_tabs', + static function ( array $tabs ) { + $tabs['featured'] = 'Featured'; + return $tabs; + } + ); + + $result = $this->get_theme_install_tabs(); + + $this->assertArrayHasKey( + 'featured', + $result['tabs'], + 'Custom tab key "featured" should be present in the filtered tabs array.' + ); + $this->assertArrayHasKey( + 'featured', + $result['browse_tabs'], + 'Custom tab key "featured" should be present in browse tabs.' + ); + $this->assertSame( + 'Featured', + $result['browse_tabs']['featured'], + 'Custom tab title should match the filtered value.' + ); + $this->assertSame( + 'popular', + $result['default_tab'], + 'Default tab should remain popular when custom tab is appended.' + ); + } + + /** + * Tests suppressing the upload tab via the install_themes_tabs filter. + * + * @ticket 65979 + */ + public function test_suppress_upload_tab_via_filter() { + wp_set_current_user( self::$admin_id ); + + add_filter( + 'install_themes_tabs', + static function ( array $tabs ) { + unset( $tabs['upload'] ); + return $tabs; + } + ); + + $result = $this->get_theme_install_tabs(); + + $this->assertArrayNotHasKey( + 'upload', + $result['tabs'], + 'Upload tab should be omitted when unset via install_themes_tabs.' + ); + $this->assertArrayNotHasKey( + 'upload', + $result['browse_tabs'], + 'Upload tab should not be present in browse tabs.' + ); + $this->assertArrayHasKey( + 'popular', + $result['browse_tabs'], + 'Core browse tabs should remain intact when upload tab is suppressed.' + ); + } + + /** + * Tests changing the default tab when popular is removed or reordered. + * + * @ticket 65979 + */ + public function test_reordering_tabs_changes_default_tab() { + wp_set_current_user( self::$admin_id ); + + add_filter( + 'install_themes_tabs', + static function ( array $tabs ) { + unset( $tabs['popular'] ); + return array( 'community' => 'Community' ) + $tabs; + } + ); + + $result = $this->get_theme_install_tabs(); + + $this->assertSame( + 'community', + $result['default_tab'], + 'The first registered browse tab should become the default tab.' + ); + $this->assertArrayNotHasKey( + 'popular', + $result['browse_tabs'], + 'Popular tab should not be present in browse tabs after removal.' + ); + } + + /** + * Tests that an empty browse tabs array safely falls back to popular. + * + * @ticket 65979 + */ + public function test_empty_browse_tabs_falls_back_to_popular() { + wp_set_current_user( self::$admin_id ); + + add_filter( + 'install_themes_tabs', + static function () { + return array( 'upload' => 'Upload Theme' ); + } + ); + + $result = $this->get_theme_install_tabs(); + + $this->assertSame( + array(), + $result['browse_tabs'], + 'Browse tabs should be empty when only upload is returned.' + ); + $this->assertSame( + 'popular', + $result['default_tab'], + 'Default tab should safely fall back to popular when browse tabs are empty.' + ); + } +} From 89459c9f087161622fe61ff18c4a7fde60cd36c7 Mon Sep 17 00:00:00 2001 From: robelsust Date: Tue, 10 Nov 2026 14:25:09 +0600 Subject: [PATCH 2/2] Themes: Fix upload button markup in Add Themes screen and multisite test capabilities. - Add missing

+ ' . __( 'Upload Theme' ) . ''; } diff --git a/tests/phpunit/tests/theme/installThemesTabs.php b/tests/phpunit/tests/theme/installThemesTabs.php index 2f12c41d23bdc..f0a3eda90b434 100644 --- a/tests/phpunit/tests/theme/installThemesTabs.php +++ b/tests/phpunit/tests/theme/installThemesTabs.php @@ -32,6 +32,19 @@ class Tests_Theme_InstallThemesTabs extends WP_UnitTestCase { public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) ); + + if ( is_multisite() ) { + grant_super_admin( self::$admin_id ); + } + } + + /** + * Tear down test fixtures after all tests run. + */ + public static function wpTearDownAfterClass() { + if ( is_multisite() ) { + revoke_super_admin( self::$admin_id ); + } } /**