/** * @test */ public function editorWorksWithoutDefiningAbspath() { file_put_contents($this->commonConfigPath, '<?php define(\'DB_NAME\', \'vp01\'); $table_prefix = \'wp_\'; require_once(ABSPATH . \'wp-settings.php\'); '); $a = new WpConfigEditor($this->commonConfigPath, false); $a->updateConfigConstant('TEST', 'value'); $expectedContent = '<?php define(\'DB_NAME\', \'vp01\'); $table_prefix = \'wp_\'; define(\'TEST\', \'value\'); require_once(ABSPATH . \'wp-settings.php\'); '; $this->assertEquals($expectedContent, file_get_contents($this->commonConfigPath)); }
/** * Sets or updates constant or variable in wp-config.php * * ## OPTIONS * * <constant> * : Name of constant or variable that will be changed. * * <value> * : Desired value. Supported types are: string, int, float and bool. * * [--plain] * : The value will be used as is - without type detection, quoting etc. * * [--variable] * : Will set a variable instead of constant. Useful for $table_prefix. * * [--common] * : The constant / variable will be set in wp-config.common.php. * * @subcommand update-config * * * @when before_wp_load */ public function updateConfig($args = [], $assoc_args = []) { require_once __DIR__ . '/VPCommandUtils.php'; require_once __DIR__ . '/../Initialization/WpConfigSplitter.php'; require_once __DIR__ . '/../Utils/WpConfigEditor.php'; require_once __DIR__ . '/../Utils/WordPressMissingFunctions.php'; $wpConfigPath = WordPressMissingFunctions::getWpConfigPath(); $updateCommonConfig = isset($assoc_args['common']); if ($updateCommonConfig) { $wpConfigPath = dirname($wpConfigPath) . '/' . WpConfigSplitter::COMMON_CONFIG_NAME; } if ($wpConfigPath === false) { WP_CLI::error('Config file does not exist. Please run `wp core config` first.'); } $constantOrVariableName = $args[0]; $isVariable = isset($assoc_args['variable']); $usePlainValue = isset($assoc_args['plain']); $value = $usePlainValue ? $args[1] : VPCommandUtils::fixTypeOfValue($args[1]); $wpConfigEditor = new WpConfigEditor($wpConfigPath, $updateCommonConfig); try { if ($isVariable) { $wpConfigEditor->updateConfigVariable($constantOrVariableName, $value, $usePlainValue); } else { $wpConfigEditor->updateConfigConstant($constantOrVariableName, $value, $usePlainValue); } } catch (\Exception $e) { WP_CLI::error('Cannot find place for defining the ' . ($isVariable ? 'variable' : 'constant') . '. Config was probably edited manually.'); } }