22
33namespace 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