Skip to content

Commit d2c7f06

Browse files
authored
Merge pull request #397 from wp-cli/builtin-directory-variables
Provide plugin and theme directories as built-in variables
2 parents ef3ec05 + b2a1609 commit d2c7f06

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎features/behat-steps.feature‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,45 @@ Feature: Test that WP-CLI Behat steps work as expected
601601
'WP_CLI_TEST_CONSTANT', 'a value'
602602
"""
603603

604+
@require-wp
605+
Scenario: Use the plugin and theme directories of a WP installation
606+
Given a WP installation
607+
Then the {PLUGIN_DIR}/akismet directory should exist
608+
And the {THEME_DIR} directory should exist
609+
610+
# Skipped on Windows because WordPress mixes path separators in these paths there.
611+
@require-wp @skip-windows
612+
Scenario: Plugin and theme directory variables match the WordPress constants
613+
Given a WP installation
614+
When I run `wp eval 'echo WP_PLUGIN_DIR;'`
615+
Then STDOUT should be:
616+
"""
617+
{PLUGIN_DIR}
618+
"""
619+
620+
When I run `wp eval 'echo get_theme_root();'`
621+
Then STDOUT should be:
622+
"""
623+
{THEME_DIR}
624+
"""
625+
626+
# Skipped on Windows because WordPress mixes path separators in these paths there.
627+
@require-wp @skip-windows
628+
Scenario: Plugin and theme directory variables follow a custom wp-content directory
629+
Given a WP installation
630+
And a custom wp-content directory
631+
When I run `wp eval 'echo WP_PLUGIN_DIR;'`
632+
Then STDOUT should be:
633+
"""
634+
{PLUGIN_DIR}
635+
"""
636+
637+
When I run `wp eval 'echo get_theme_root();'`
638+
Then STDOUT should be:
639+
"""
640+
{THEME_DIR}
641+
"""
642+
604643
@require-wp
605644
Scenario: Test custom wp-content directory
606645
Given a WP installation
@@ -612,6 +651,8 @@ Feature: Test that WP-CLI Behat steps work as expected
612651
"""
613652
WP_CONTENT_DIR
614653
"""
654+
And the {PLUGIN_DIR} directory should exist
655+
And the {THEME_DIR} directory should exist
615656

616657
@require-wp
617658
Scenario: Test WP multisite installation
@@ -658,6 +699,18 @@ Feature: Test that WP-CLI Behat steps work as expected
658699
When I run `wp core version`
659700
Then STDOUT should not be empty
660701

702+
When I run `wp eval 'echo WP_PLUGIN_DIR;'`
703+
Then STDOUT should be:
704+
"""
705+
{PLUGIN_DIR}
706+
"""
707+
708+
When I run `wp eval 'echo get_theme_root();'`
709+
Then STDOUT should be:
710+
"""
711+
{THEME_DIR}
712+
"""
713+
661714
# Skipped on Windows because of curl getaddrinfo() errors.
662715
@require-wp @require-composer @skip-windows
663716
Scenario: Test WP installation with Composer and custom vendor directory

‎src/Context/FeatureContext.php‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class FeatureContext implements Context {
165165

166166
/**
167167
* Array of variables available as {VARIABLE_NAME}. Some are always set: CORE_CONFIG_SETTINGS, DB_USER, DB_PASSWORD, DB_HOST, SRC_DIR, CACHE_DIR, WP_VERSION-version-latest.
168-
* Some are step-dependent: RUN_DIR, SUITE_CACHE_DIR, COMPOSER_LOCAL_REPOSITORY, PHAR_PATH. One is set on use: INVOKE_WP_CLI_WITH_PHP_ARGS-args.
168+
* Some are step-dependent: RUN_DIR, SUITE_CACHE_DIR, COMPOSER_LOCAL_REPOSITORY, PHAR_PATH, and PLUGIN_DIR and THEME_DIR for the WordPress files in RUN_DIR. One is set on use: INVOKE_WP_CLI_WITH_PHP_ARGS-args.
169169
* Scenarios can define their own variables using "Given save" steps. Variables are reset for each scenario.
170170
*
171171
* @var array<string, string>
@@ -1932,6 +1932,10 @@ public function download_wp( $subdir = '', $version = '' ): void {
19321932

19331933
self::copy_dir( self::$cache_dir, $dest_dir );
19341934

1935+
if ( ! $subdir ) {
1936+
$this->set_content_dir_variables( $dest_dir . '/wp-content' );
1937+
}
1938+
19351939
if ( ! is_dir( $dest_dir . '/wp-content/mu-plugins' ) ) {
19361940
mkdir( $dest_dir . '/wp-content/mu-plugins', 0777, true /*recursive*/ );
19371941
}
@@ -2284,6 +2288,21 @@ public function install_wp_with_composer( $vendor_directory = 'vendor' ): void {
22842288
}
22852289

22862290
$this->proc( 'wp core install', $install_args )->run_check();
2291+
2292+
$this->set_content_dir_variables( $this->variables['RUN_DIR'] . '/WordPress/wp-content' );
2293+
}
2294+
2295+
/**
2296+
* Expose the plugins and themes directories as {PLUGIN_DIR} and {THEME_DIR}.
2297+
*
2298+
* Matches the output of `wp plugin path` and `wp theme path`.
2299+
*
2300+
* @param string $content_dir
2301+
* @param string $plugin_dir Defaults to the plugins directory inside $content_dir.
2302+
*/
2303+
public function set_content_dir_variables( $content_dir, $plugin_dir = '' ): void {
2304+
$this->variables['PLUGIN_DIR'] = $plugin_dir ?: $content_dir . '/plugins';
2305+
$this->variables['THEME_DIR'] = $content_dir . '/themes';
22872306
}
22882307

22892308
public function composer_add_wp_cli_local_repository(): void {

‎src/Context/GivenStepDefinitions.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,8 @@ public function given_a_custom_wp_directory(): void {
552552

553553
file_put_contents( $wp_config_path, $wp_config_code );
554554

555+
$this->set_content_dir_variables( $this->variables['RUN_DIR'] . '/my-content', $this->variables['RUN_DIR'] . '/my-plugins' );
556+
555557
if ( 'sqlite' === self::$db_type ) {
556558
$db_dropin = $this->variables['RUN_DIR'] . '/my-content/db.php';
557559

0 commit comments

Comments
 (0)