Skip to content

Commit b4d05ba

Browse files
schlesseraclaude
andcommitted
Provide plugin and theme directories as built-in variables
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ef3ec05 commit b4d05ba

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎features/behat-steps.feature‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,23 @@ 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+
When I run `wp eval 'echo WP_PLUGIN_DIR;'`
608+
Then STDOUT should be:
609+
"""
610+
{PLUGIN_DIR}
611+
"""
612+
613+
When I run `wp eval 'echo get_theme_root();'`
614+
Then STDOUT should be:
615+
"""
616+
{THEME_DIR}
617+
"""
618+
And the {PLUGIN_DIR}/akismet directory should exist
619+
And the {THEME_DIR} directory should exist
620+
604621
@require-wp
605622
Scenario: Test custom wp-content directory
606623
Given a WP installation
@@ -613,6 +630,18 @@ Feature: Test that WP-CLI Behat steps work as expected
613630
WP_CONTENT_DIR
614631
"""
615632

633+
When I run `wp eval 'echo WP_PLUGIN_DIR;'`
634+
Then STDOUT should be:
635+
"""
636+
{PLUGIN_DIR}
637+
"""
638+
639+
When I run `wp eval 'echo get_theme_root();'`
640+
Then STDOUT should be:
641+
"""
642+
{THEME_DIR}
643+
"""
644+
616645
@require-wp
617646
Scenario: Test WP multisite installation
618647
Given a WP multisite installation
@@ -658,6 +687,18 @@ Feature: Test that WP-CLI Behat steps work as expected
658687
When I run `wp core version`
659688
Then STDOUT should not be empty
660689

690+
When I run `wp eval 'echo WP_PLUGIN_DIR;'`
691+
Then STDOUT should be:
692+
"""
693+
{PLUGIN_DIR}
694+
"""
695+
696+
When I run `wp eval 'echo get_theme_root();'`
697+
Then STDOUT should be:
698+
"""
699+
{THEME_DIR}
700+
"""
701+
661702
# Skipped on Windows because of curl getaddrinfo() errors.
662703
@require-wp @require-composer @skip-windows
663704
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)