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
63 changes: 51 additions & 12 deletions src/js/_enqueues/wp/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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;
Expand All @@ -616,17 +648,17 @@ 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 );

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 );
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/wp-admin/css/themes.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/wp-admin/theme-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@
</p></div>
<# } #>

<span class="more-details"><?php _ex( 'Details &amp; Preview', 'theme' ); ?></span>
<# if ( data.compatible_wp && data.compatible_php ) { #>
<span class="more-details"><?php _ex( 'Details &amp; Preview', 'theme' ); ?></span>
<# } #>
<div class="theme-author">
<?php
/* translators: %s: Theme author name. */
Expand Down
Loading