@@ -707,7 +707,7 @@ public function get_wp_config_code( $wp_config_path = '' ) {
707707 $ wp_config_path = Utils \locate_wp_config ();
708708 }
709709
710- $ wp_config_code = explode ( "\n" , file_get_contents ( $ wp_config_path ) );
710+ $ wp_config_code = file_get_contents ( $ wp_config_path );
711711
712712 // Detect and strip byte-order marks (BOMs).
713713 // This code assumes they can only be found on the first line.
@@ -716,34 +716,26 @@ public function get_wp_config_code( $wp_config_path = '' ) {
716716
717717 $ length = strlen ( $ bom_sequence );
718718
719- while ( substr ( $ wp_config_code[ 0 ] , 0 , $ length ) === $ bom_sequence ) {
719+ while ( substr ( $ wp_config_code , 0 , $ length ) === $ bom_sequence ) {
720720 WP_CLI ::warning (
721721 "{$ bom_name } byte-order mark (BOM) detected in wp-config.php file, stripping it for parsing. "
722722 );
723723
724- $ wp_config_code[ 0 ] = substr ( $ wp_config_code[ 0 ] , $ length );
724+ $ wp_config_code = substr ( $ wp_config_code , $ length );
725725 }
726726 }
727727
728- $ found_wp_settings = false ;
728+ $ matches = [] ;
729729
730- $ lines_to_run = [] ;
730+ preg_match_all ( ' /\s*require(_once)?\s*.*wp-settings\.php/ ' , $ wp_config_code , $ matches , PREG_OFFSET_CAPTURE ) ;
731731
732- foreach ( $ wp_config_code as $ line ) {
733- if ( preg_match ( '/^\s*require.+wp-settings\.php/ ' , $ line ) ) {
734- $ found_wp_settings = true ;
735- continue ;
736- }
737-
738- $ lines_to_run [] = $ line ;
739- }
740-
741- if ( ! $ found_wp_settings ) {
732+ if ( empty ( $ matches [0 ] ) ) {
742733 WP_CLI ::error ( 'Strange wp-config.php file: wp-settings.php is not loaded directly. ' );
743734 }
744735
745- $ source = implode ( "\n" , $ lines_to_run );
746- $ source = Utils \replace_path_consts ( $ source , $ wp_config_path );
736+ $ wp_config_code = substr ( $ wp_config_code , 0 , $ matches [0 ][0 ][1 ] );
737+
738+ $ source = Utils \replace_path_consts ( $ wp_config_code , $ wp_config_path );
747739 return preg_replace ( '|^\s*\<\?php\s*| ' , '' , $ source );
748740 }
749741
0 commit comments