@@ -145,6 +145,9 @@ class Find_Command {
145
145
* These fields are optionally available:
146
146
*
147
147
* * 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.
148
151
*
149
152
* ## OPTIONS
150
153
*
@@ -204,11 +207,16 @@ public function __invoke( $args, $assoc_args ) {
204
207
$ this ->resolved_aliases [ rtrim ( $ target ['path ' ], '/ ' ) ] = $ alias ;
205
208
}
206
209
210
+ $ fields = array ( 'version_path ' , 'version ' , 'depth ' , 'alias ' );
211
+ if ( ! empty ( $ assoc_args ['fields ' ] ) ) {
212
+ $ fields = explode ( ', ' , $ assoc_args ['fields ' ] );
213
+ }
214
+
207
215
$ this ->start_time = microtime ( true );
208
216
$ this ->log ( "Searching for WordPress installations in ' {$ path }' " );
209
217
$ this ->recurse_directory ( $ this ->base_path );
210
218
$ 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 );
212
220
$ formatter ->display_items ( $ this ->found_wp );
213
221
}
214
222
@@ -245,16 +253,41 @@ private function recurse_directory( $path ) {
245
253
// version.php file.
246
254
if ( '/wp-includes/ ' === substr ( $ path , -13 )
247
255
&& 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
+
251
261
$ this ->found_wp [ $ version_path ] = array (
252
262
'version_path ' => $ version_path ,
253
263
'version ' => self ::get_wp_version ( $ version_path ),
254
- 'wp_path ' => str_replace ( ' wp-includes/version.php ' , '' , $ version_path ) ,
264
+ 'wp_path ' => rtrim ( $ wp_path , '/ ' ) . ' / ' ,
255
265
'depth ' => $ this ->current_depth - 1 ,
256
266
'alias ' => $ alias ,
267
+ 'db_host ' => '' ,
268
+ 'db_name ' => '' ,
269
+ 'db_user ' => '' ,
257
270
);
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
+ }
258
291
$ this ->log ( "Found WordPress installation at ' {$ version_path }' " );
259
292
return ;
260
293
}
@@ -292,6 +325,25 @@ private static function get_wp_version( $path ) {
292
325
return ! empty ( $ matches [1 ] ) ? $ matches [1 ] : '' ;
293
326
}
294
327
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
+
295
347
/**
296
348
* Log informational message to STDOUT depending on verbosity.
297
349
*/
0 commit comments