Skip to content

Commit e397cf2

Browse files
authored
WP-CLI Themes: Added workaround to equalize folder naming conven… (#196)
WP-CLI Themes: Added workaround to equalize folder naming conventions across Win/Mac/Linux
2 parents 8d50bfe + 4018351 commit e397cf2

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

features/theme.feature

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,45 @@ Feature: Manage WordPress themes
543543
"""
544544
active
545545
"""
546+
547+
Scenario: Theme activation fails when slug does not match exactly
548+
Given a WP install
549+
550+
When I run `wp theme install p2`
551+
Then the return code should be 0
552+
553+
When I try `wp theme activate P2`
554+
Then STDERR should contain:
555+
"""
556+
Error: The 'P2' theme could not be found. Did you mean 'p2'?
557+
"""
558+
And STDOUT should be empty
559+
And the return code should be 1
560+
561+
When I try `wp theme activate p3`
562+
Then STDERR should contain:
563+
"""
564+
Error: The 'p3' theme could not be found. Did you mean 'p2'?
565+
"""
566+
And STDOUT should be empty
567+
And the return code should be 1
568+
569+
When I try `wp theme activate pb2`
570+
Then STDERR should contain:
571+
"""
572+
Error: The 'pb2' theme could not be found. Did you mean 'p2'?
573+
"""
574+
And STDOUT should be empty
575+
And the return code should be 1
576+
577+
When I try `wp theme activate completelyoff`
578+
Then STDERR should contain:
579+
"""
580+
Error: The 'completelyoff' theme could not be found.
581+
"""
582+
And STDERR should not contain:
583+
"""
584+
Did you mean
585+
"""
586+
And STDOUT should be empty
587+
And the return code should be 1

src/WP_CLI/Fetchers/Theme.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace WP_CLI\Fetchers;
44

5+
use WP_CLI\Utils;
6+
57
/**
68
* Fetch a WordPress theme based on one of its attributes.
79
*/
@@ -19,13 +21,47 @@ class Theme extends Base {
1921
* @return object|false
2022
*/
2123
public function get( $name ) {
22-
$theme = wp_get_theme( $name );
23-
24-
if ( ! $theme->exists() ) {
24+
// Workaround to equalize folder naming conventions across Win/Mac/Linux.
25+
// Returns false if theme stylesheet doesn't exactly match existing themes.
26+
$existing_themes = wp_get_themes( array( 'errors' => null ) );
27+
$existing_stylesheets = array_keys( $existing_themes );
28+
if ( ! in_array( $name, $existing_stylesheets, true ) ) {
29+
$inexact_match = $this->find_inexact_match( $name, $existing_themes );
30+
if ( false !== $inexact_match ) {
31+
$this->msg .= sprintf( " Did you mean '%s'?", $inexact_match );
32+
}
2533
return false;
2634
}
2735

36+
$theme = $existing_themes[ $name ];
37+
2838
return $theme;
2939
}
40+
41+
/**
42+
* Find and return the key in $existing_themes that matches $name with
43+
* a case insensitive string comparison.
44+
*
45+
* @param string $name Name of theme received by command.
46+
* @param array $existing_themes Key/value pair of existing themes, key is
47+
* a case sensitive name.
48+
* @return string|boolean Case sensitive name if match found, otherwise false.
49+
*/
50+
private function find_inexact_match( $name, $existing_themes ) {
51+
$target = strtolower( $name );
52+
$themes = array_map( 'strtolower', array_keys( $existing_themes ) );
53+
54+
if ( in_array( $target, $themes, true ) ) {
55+
return $target;
56+
}
57+
58+
$suggestion = Utils\get_suggestion( $target, $themes );
59+
60+
if ( '' !== $suggestion ) {
61+
return $suggestion;
62+
}
63+
64+
return false;
65+
}
3066
}
3167

0 commit comments

Comments
 (0)