From 58f172fa886373f3f96fc8d33f5dcea1ad859502 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 26 Dec 2018 17:04:52 +0100 Subject: [PATCH 1/5] Add support for nested theme folders --- features/makepot.feature | 26 +++++++++++++++++++ src/MakePotCommand.php | 54 +++++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 23 deletions(-) diff --git a/features/makepot.feature b/features/makepot.feature index a242368d..6208a593 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -2017,6 +2017,32 @@ Feature: Generate a POT file of a WordPress project Extracted 2 strings """ + Scenario: Detects theme in sub folder + Given an empty foo-themes directory + And an empty foo-themes/theme-a directory + And a foo-themes/theme-a/style.css file: + """ + /* + Theme Name: Theme A + Theme URI: https://example.com + Description: + Author: + Author URI: + Version: 0.1.0 + License: GPL-2.0+ + Text Domain: foo-theme + Domain Path: /languages + */ + """ + + When I try `wp i18n make-pot foo-theme` + Then STDOUT should be: + """ + Theme stylesheet detected. + Success: POT file successfully generated! + """ + And STDERR should be empty + Scenario: Ignore strings that are part of the exception list Given an empty directory And a exception.pot file: diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index ad9faaf2..6727fc5f 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -6,6 +6,7 @@ use Gettext\Merge; use Gettext\Translation; use Gettext\Translations; +use Symfony\Component\Finder\SplFileInfo; use WP_CLI; use WP_CLI_Command; use WP_CLI\Utils; @@ -407,40 +408,47 @@ protected function unslashit( $string ) { * @return array */ protected function get_main_file_data() { - $stylesheet = sprintf( '%s/style.css', $this->source ); + $files = new IteratorIterator( new DirectoryIterator( $this->source ) ); - if ( is_file( $stylesheet ) && is_readable( $stylesheet ) ) { - $theme_data = static::get_file_data( $stylesheet, array_combine( $this->get_file_headers( 'theme' ), $this->get_file_headers( 'theme' ) ) ); + /** @var DirectoryIterator $file */ + foreach ( $files as $file ) { + // wp-content/themes/my-theme/style.css + if ( $file->isFile() && 'style' === $file->getBasename( 'css' ) && $file->isReadable() ) { + $theme_data = static::get_file_data( $file->getRealPath(), array_combine( $this->get_file_headers( 'theme' ), $this->get_file_headers( 'theme' ) ) ); - // Stop when it contains a valid Theme Name header. - if ( ! empty( $theme_data['Theme Name'] ) ) { - WP_CLI::log( 'Theme stylesheet detected.' ); - WP_CLI::debug( sprintf( 'Theme stylesheet: %s', $stylesheet ), 'make-pot' ); + // Stop when it contains a valid Theme Name header. + if ( ! empty( $theme_data['Theme Name'] ) ) { + WP_CLI::log( 'Theme stylesheet detected.' ); + WP_CLI::debug( sprintf( 'Theme stylesheet: %s', $file->getRealPath() ), 'make-pot' ); - return $theme_data; + return $theme_data; + } } - } - $plugin_files = []; + // wp-content/themes/my-themes/theme-a/style.css + if ( $file->isDir() && is_readable( $file->getRealPath() . '/style.css' ) ) { + $theme_data = static::get_file_data( $file->getRealPath() . '/style.css', array_combine( $this->get_file_headers( 'theme' ), $this->get_file_headers( 'theme' ) ) ); - $files = new IteratorIterator( new DirectoryIterator( $this->source ) ); + // Stop when it contains a valid Theme Name header. + if ( ! empty( $theme_data['Theme Name'] ) ) { + WP_CLI::log( 'Theme stylesheet detected.' ); + WP_CLI::debug( sprintf( 'Theme stylesheet: %s', $file->getRealPath() . '/style.css' ), 'make-pot' ); - /** @var DirectoryIterator $file */ - foreach ( $files as $file ) { - if ( $file->isFile() && $file->isReadable() && 'php' === $file->getExtension() ) { - $plugin_files[] = $file->getRealPath(); + return $theme_data; + } } - } - foreach ( $plugin_files as $plugin_file ) { - $plugin_data = static::get_file_data( $plugin_file, array_combine( $this->get_file_headers( 'plugin' ), $this->get_file_headers( 'plugin' ) ) ); + // wp-content/plugins/my-plugin/my-plugin.php + if ( $file->isFile() && $file->isReadable() && 'php' === $file->getExtension() ) { + $plugin_data = static::get_file_data( $file->getRealPath(), array_combine( $this->get_file_headers( 'plugin' ), $this->get_file_headers( 'plugin' ) ) ); - // Stop when we find a file with a valid Plugin Name header. - if ( ! empty( $plugin_data['Plugin Name'] ) ) { - WP_CLI::log( 'Plugin file detected.' ); - WP_CLI::debug( sprintf( 'Plugin file: %s', $plugin_file ), 'make-pot' ); + // Stop when we find a file with a valid Plugin Name header. + if ( ! empty( $plugin_data['Plugin Name'] ) ) { + WP_CLI::log( 'Plugin file detected.' ); + WP_CLI::debug( sprintf( 'Plugin file: %s', $file->getRealPath() ), 'make-pot' ); - return $plugin_data; + return $plugin_data; + } } } From 71fa9bb324778a49fd7161ceebf33c98c1772806 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 26 Dec 2018 17:22:39 +0100 Subject: [PATCH 2/5] Fix source path --- features/makepot.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/makepot.feature b/features/makepot.feature index 6208a593..ef8e310a 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -2035,7 +2035,7 @@ Feature: Generate a POT file of a WordPress project */ """ - When I try `wp i18n make-pot foo-theme` + When I try `wp i18n make-pot foo-themes` Then STDOUT should be: """ Theme stylesheet detected. From 1e1807cd0e12fa5dab7e7bb7da3a4f34dcd7fd8b Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Thu, 31 Jan 2019 08:02:18 -0600 Subject: [PATCH 3/5] Add isDot() check to file comparison Co-Authored-By: swissspidy --- src/MakePotCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index b417798d..478358ee 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -435,7 +435,7 @@ protected function get_main_file_data() { } // wp-content/themes/my-themes/theme-a/style.css - if ( $file->isDir() && is_readable( $file->getRealPath() . '/style.css' ) ) { + if ( $file->isDir() && ! $file->isDot() && is_readable( $file->getRealPath() . '/style.css' ) ) { $theme_data = static::get_file_data( $file->getRealPath() . '/style.css', array_combine( $this->get_file_headers( 'theme' ), $this->get_file_headers( 'theme' ) ) ); // Stop when it contains a valid Theme Name header. From 7939b6a9e19b4c2407d510112e00b9d118b532dc Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Thu, 31 Jan 2019 08:03:02 -0600 Subject: [PATCH 4/5] Add missing basename check Co-Authored-By: swissspidy --- src/MakePotCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index 478358ee..977f0d3c 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -421,7 +421,7 @@ protected function get_main_file_data() { /** @var DirectoryIterator $file */ foreach ( $files as $file ) { // wp-content/themes/my-theme/style.css - if ( $file->isFile() && 'style' === $file->getBasename( 'css' ) && $file->isReadable() ) { + if ( $file->isFile() && 'style' === $file->getBasename( '.css' ) && $file->isReadable() ) { $theme_data = static::get_file_data( $file->getRealPath(), array_combine( $this->get_file_headers( 'theme' ), $this->get_file_headers( 'theme' ) ) ); // Stop when it contains a valid Theme Name header. From 1109006983c2261e9a77207427aec7206d87c3de Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Thu, 31 Jan 2019 15:54:09 +0100 Subject: [PATCH 5/5] Fix messed-up merge from master --- src/MakePotCommand.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index 977f0d3c..cf87d7e6 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -429,9 +429,10 @@ protected function get_main_file_data() { WP_CLI::log( 'Theme stylesheet detected.' ); WP_CLI::debug( sprintf( 'Theme stylesheet: %s', $file->getRealPath() ), 'make-pot' ); - $this->project_type = 'theme'; + $this->project_type = 'theme'; - return $theme_data; + return $theme_data; + } } // wp-content/themes/my-themes/theme-a/style.css @@ -443,6 +444,8 @@ protected function get_main_file_data() { WP_CLI::log( 'Theme stylesheet detected.' ); WP_CLI::debug( sprintf( 'Theme stylesheet: %s', $file->getRealPath() . '/style.css' ), 'make-pot' ); + $this->project_type = 'theme'; + return $theme_data; } } @@ -456,9 +459,10 @@ protected function get_main_file_data() { WP_CLI::log( 'Plugin file detected.' ); WP_CLI::debug( sprintf( 'Plugin file: %s', $file->getRealPath() ), 'make-pot' ); - $this->project_type = 'plugin'; + $this->project_type = 'plugin'; - return $plugin_data; + return $plugin_data; + } } }