diff --git a/src/js/_enqueues/wp/theme.js b/src/js/_enqueues/wp/theme.js index ff5d72ad2801e..669e577b6bbdb 100644 --- a/src/js/_enqueues/wp/theme.js +++ b/src/js/_enqueues/wp/theme.js @@ -434,6 +434,33 @@ themes.view.Theme = wp.Backbone.View.extend({ if ( this.model.get( 'displayAuthor' ) ) { this.$el.addClass( 'display-author' ); } + + // Flags themes that cannot be previewed in the theme installer. + if ( themes.isInstall ) { + this.$el.toggleClass( 'no-preview', ! this.isPreviewable( this.model ) ); + } + }, + + // Whether a theme is compatible with the current WordPress and PHP versions, and can be previewed. + isPreviewable: function( model ) { + return !! ( model.get( 'compatible_wp' ) && model.get( 'compatible_php' ) ); + }, + + // Gets the closest theme that can be previewed, before (-1) or after (1) the given one. + getAdjacentPreviewable: function( model, direction ) { + var collection = this.model.collection, + index = collection.indexOf( model ) + direction, + adjacent; + + while ( index >= 0 && index < collection.length ) { + adjacent = collection.at( index ); + + if ( this.isPreviewable( adjacent ) ) { + return adjacent; + } + + index += direction; + } }, // Adds a class to the currently active theme @@ -501,6 +528,11 @@ themes.view.Theme = wp.Backbone.View.extend({ return this.touchDrag = false; } + // Themes that are incompatible with the current WordPress or PHP version can't be previewed. + if ( ! this.isPreviewable( this.model ) ) { + return; + } + // Allow direct link path to installing a theme. if ( $( event.target ).not( '.install-theme-preview' ).parents( '.theme-actions' ).length ) { return; @@ -554,8 +586,8 @@ themes.view.Theme = wp.Backbone.View.extend({ current = self.current; } - // Get next theme model. - self.current = self.model.collection.at( self.model.collection.indexOf( current ) + 1 ); + // Get the next theme model that can be previewed. + self.current = self.getAdjacentPreviewable( current, 1 ); // If we have no more themes, bail. if ( _.isUndefined( self.current ) ) { @@ -586,12 +618,12 @@ themes.view.Theme = wp.Backbone.View.extend({ current = self.current; } - // Get previous theme model. - self.current = self.model.collection.at( self.model.collection.indexOf( current ) - 1 ); + // Get the previous theme model that can be previewed. + self.current = self.getAdjacentPreviewable( current, -1 ); // If we have no more themes, bail. if ( _.isUndefined( self.current ) ) { - return; + return self.current = current; } preview.model = self.current; @@ -616,8 +648,8 @@ themes.view.Theme = wp.Backbone.View.extend({ previousThemeButton = $themeInstaller.find( '.previous-theme' ), nextThemeButton = $themeInstaller.find( '.next-theme' ); - // Disable previous at the zero position. - if ( 0 === this.model.collection.indexOf( current ) ) { + // Disable previous if there is no previous theme that can be previewed. + if ( _.isUndefined( this.getAdjacentPreviewable( current, -1 ) ) ) { previousThemeButton .addClass( 'disabled' ) .prop( 'disabled', true ); @@ -625,8 +657,8 @@ themes.view.Theme = wp.Backbone.View.extend({ nextThemeButton.trigger( 'focus' ); } - // Disable next if the next model is undefined. - if ( _.isUndefined( this.model.collection.at( this.model.collection.indexOf( current ) + 1 ) ) ) { + // Disable next if there is no next theme that can be previewed. + if ( _.isUndefined( this.getAdjacentPreviewable( current, 1 ) ) ) { nextThemeButton .addClass( 'disabled' ) .prop( 'disabled', true ); @@ -2060,6 +2092,13 @@ themes.RunInstaller = { // Handles `theme` route event. // Queries the API for the passed theme slug. themes.router.on( 'route:preview', function( slug ) { + var theme = self.view.view.theme, + model = self.view.collection.findWhere( { 'slug': slug } ); + + // Bail if the requested theme is not in the collection or can't be previewed. + if ( theme && ( ! model || ! theme.isPreviewable( model ) ) ) { + return; + } // Remove existing handlers. if ( themes.preview ) { @@ -2068,9 +2107,9 @@ themes.RunInstaller = { } // If the theme preview is active, set the current theme. - if ( self.view.view.theme && self.view.view.theme.preview ) { - self.view.view.theme.model = self.view.collection.findWhere( { 'slug': slug } ); - self.view.view.theme.preview(); + if ( theme && theme.preview ) { + theme.model = model; + theme.preview(); } else { // Select the theme by slug. diff --git a/src/wp-admin/css/themes.css b/src/wp-admin/css/themes.css index 5b71c223b6ce6..7f22a935f1d84 100644 --- a/src/wp-admin/css/themes.css +++ b/src/wp-admin/css/themes.css @@ -175,6 +175,16 @@ body.js .theme-browser.search-loading { opacity: 0.4; } +/* Themes that cannot be previewed in the theme installer */ +.theme-browser .theme.no-preview { + cursor: default; +} + +.theme-browser.rendered .theme.no-preview:hover .theme-screenshot img, +.theme-browser.rendered .theme.no-preview.focus .theme-screenshot img { + opacity: 1; +} + .theme-browser .theme .more-details { -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; opacity: 0; diff --git a/src/wp-admin/theme-install.php b/src/wp-admin/theme-install.php index 8e6fc5d1eea2c..a23b92edf0d7b 100644 --- a/src/wp-admin/theme-install.php +++ b/src/wp-admin/theme-install.php @@ -390,7 +390,9 @@

<# } #> - + <# if ( data.compatible_wp && data.compatible_php ) { #> + + <# } #>