@@ -145,6 +145,9 @@ class Find_Command {
145145 * These fields are optionally available:
146146 *
147147 * * wp_path - Path that can be passed to `--path=<path>` global parameter.
148+ * * db_host - Host name for the database.
149+ * * db_user - User name for the database.
150+ * * db_name - Database name for the database.
148151 *
149152 * ## OPTIONS
150153 *
@@ -204,11 +207,16 @@ public function __invoke( $args, $assoc_args ) {
204207 $ this ->resolved_aliases [ rtrim ( $ target ['path ' ], '/ ' ) ] = $ alias ;
205208 }
206209
210+ $ fields = array ( 'version_path ' , 'version ' , 'depth ' , 'alias ' );
211+ if ( ! empty ( $ assoc_args ['fields ' ] ) ) {
212+ $ fields = explode ( ', ' , $ assoc_args ['fields ' ] );
213+ }
214+
207215 $ this ->start_time = microtime ( true );
208216 $ this ->log ( "Searching for WordPress installations in ' {$ path }' " );
209217 $ this ->recurse_directory ( $ this ->base_path );
210218 $ this ->log ( "Finished search for WordPress installations in ' {$ path }' " );
211- $ formatter = new \WP_CLI \Formatter ( $ assoc_args , array ( ' version_path ' , ' version ' , ' depth ' , ' alias ' ) );
219+ $ formatter = new \WP_CLI \Formatter ( $ assoc_args , $ fields );
212220 $ formatter ->display_items ( $ this ->found_wp );
213221 }
214222
@@ -245,16 +253,41 @@ private function recurse_directory( $path ) {
245253 // version.php file.
246254 if ( '/wp-includes/ ' === substr ( $ path , -13 )
247255 && file_exists ( $ path . 'version.php ' ) ) {
248- $ version_path = $ path . 'version.php ' ;
249- $ wp_path = substr ( $ path , 0 , -13 );
250- $ alias = isset ( $ this ->resolved_aliases [ $ wp_path ] ) ? $ this ->resolved_aliases [ $ wp_path ] : '' ;
256+ $ version_path = $ path . 'version.php ' ;
257+ $ wp_path = substr ( $ path , 0 , -13 );
258+ $ alias = isset ( $ this ->resolved_aliases [ $ wp_path ] ) ? $ this ->resolved_aliases [ $ wp_path ] : '' ;
259+ $ wp_path = rtrim ( $ wp_path , '/ ' ) . '/ ' ;
260+
251261 $ this ->found_wp [ $ version_path ] = array (
252262 'version_path ' => $ version_path ,
253263 'version ' => self ::get_wp_version ( $ version_path ),
254- 'wp_path ' => str_replace ( ' wp-includes/version.php ' , '' , $ version_path ) ,
264+ 'wp_path ' => rtrim ( $ wp_path , '/ ' ) . ' / ' ,
255265 'depth ' => $ this ->current_depth - 1 ,
256266 'alias ' => $ alias ,
267+ 'db_host ' => '' ,
268+ 'db_name ' => '' ,
269+ 'db_user ' => '' ,
257270 );
271+
272+ $ config_path = self ::get_wp_config_path ( $ wp_path );
273+ if ( $ config_path ) {
274+ try {
275+ $ transformer = new WPConfigTransformer ( $ config_path );
276+ foreach ( array ( 'db_host ' , 'db_name ' , 'db_user ' ) as $ constant ) {
277+ $ value = $ transformer ->get_value ( 'constant ' , strtoupper ( $ constant ) );
278+ // Clean up strings.
279+ $ first = substr ( $ value , 0 , 1 );
280+ $ last = substr ( $ value , -1 );
281+ $ both = array_unique ( array ( $ first , $ last ) );
282+ if ( in_array ( $ both , array ( array ( '" ' ), array ( "' " ) ), true ) ) {
283+ $ value = substr ( $ value , 1 , -1 );
284+ }
285+ $ this ->found_wp [ $ version_path ][ $ constant ] = $ value ;
286+ }
287+ } catch ( \Exception $ e ) {
288+ $ this ->log ( "Could not process the 'wp-config.php' transformation: " . $ exception ->getMessage () );
289+ }
290+ }
258291 $ this ->log ( "Found WordPress installation at ' {$ version_path }' " );
259292 return ;
260293 }
@@ -292,6 +325,25 @@ private static function get_wp_version( $path ) {
292325 return ! empty ( $ matches [1 ] ) ? $ matches [1 ] : '' ;
293326 }
294327
328+ /**
329+ * Get the wp-config.php path for the installation.
330+ *
331+ * Adapted from WP_CLI\Utils\locate_wp_config()
332+ */
333+ private static function get_wp_config_path ( $ installation_dir ) {
334+ $ path = false ;
335+ if ( file_exists ( $ installation_dir . 'wp-config.php ' ) ) {
336+ $ path = $ installation_dir . 'wp-config.php ' ;
337+ } elseif ( file_exists ( $ installation_dir . '../wp-config.php ' ) && ! file_exists ( $ installation_dir . '/../wp-settings.php ' ) ) {
338+ $ path = $ installation_dir . '../wp-config.php ' ;
339+ }
340+
341+ if ( $ path ) {
342+ $ path = realpath ( $ path );
343+ }
344+ return $ path ;
345+ }
346+
295347 /**
296348 * Log informational message to STDOUT depending on verbosity.
297349 */
0 commit comments