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..b42bba2d210c2 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' ),
@@ -166,17 +195,6 @@
__( 'Upload Theme' ) ) );
if ( ! empty( $tabs['upload'] ) && current_user_can( 'upload_themes' ) ) {
echo ' ';
}
@@ -210,10 +228,9 @@
-
-
-
-
+ $text ) : ?>
+
+
diff --git a/tests/phpunit/tests/theme/installThemesTabs.php b/tests/phpunit/tests/theme/installThemesTabs.php
new file mode 100644
index 0000000000000..f0a3eda90b434
--- /dev/null
+++ b/tests/phpunit/tests/theme/installThemesTabs.php
@@ -0,0 +1,278 @@
+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 );
+ }
+ }
+
+ /**
+ * 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.'
+ );
+ }
+}