Skip to content

Commit

Permalink
Merge pull request #1020 from versionpress/1010-happy-blogging
Browse files Browse the repository at this point in the history
Happy blogging
  • Loading branch information
JanVoracek committed Apr 28, 2016
2 parents 1b09dbe + 14d2148 commit c799df8
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
31 changes: 30 additions & 1 deletion plugins/versionpress/src/Utils/WpConfigEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private function updateConfig($value, $replaceRegex, $definitionTemplate, $usePl
$originalContent = $wpConfigContent;
$endOfEditableSection = $this->isCommonConfig ?
strlen($originalContent) :
strpos($wpConfigContent, '/* That\'s all, stop editing! Happy blogging. */');
$this->findPositionForAddingNewDefinition($wpConfigContent);

if ($endOfEditableSection === false) {
throw new \Exception('Editable section not found.');
Expand All @@ -81,4 +81,33 @@ private function updateConfig($value, $replaceRegex, $definitionTemplate, $usePl

file_put_contents($this->wpConfigPath, $wpConfigContent);
}

private function findPositionForAddingNewDefinition($wpConfigContent)
{
// https://regex101.com/r/aB8rY4/1
$thatsAllCommentPattern = "/\\/\\*.*!.*\\*\\//"; // one-line comment containing exclamation mark
preg_match($thatsAllCommentPattern, $wpConfigContent, $matches, PREG_OFFSET_CAPTURE);

if ($matches) {
return $matches[0][1];
}

// https://regex101.com/r/fY6eC6/1
$ifDefinedAbspathPattern = "/if.*defined.*ABSPATH.*/";
preg_match($ifDefinedAbspathPattern, $wpConfigContent, $matches, PREG_OFFSET_CAPTURE);

if ($matches) {
return $matches[0][1];
}

// https://regex101.com/r/vG5rB0/1
$requireWpSettingsPattern = "/require.*wp-settings/";
preg_match($requireWpSettingsPattern, $wpConfigContent, $matches, PREG_OFFSET_CAPTURE);

if ($matches) {
return $matches[0][1];
}

return 0;
}
}
106 changes: 106 additions & 0 deletions plugins/versionpress/tests/Unit/WpConfigEditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,112 @@ public function editorUpdatesOnlyDesiredValueInWpConfig()
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . \'wp-settings.php\');
';

$this->assertEquals($expectedContent, file_get_contents($this->commonConfigPath));
}

/**
* @test
*/
public function editorWorksWithLocalizedWpConfig()
{
file_put_contents($this->commonConfigPath, '<?php
// ** MySQL settings ** //
/** The name of the database for WordPress */
define(\'DB_NAME\', \'vp01\');
$table_prefix = \'wp_\';
/* C\'est tout, ne touchez pas à ce qui suit ! Bon blogging ! */
/** Absolute path to the WordPress directory. */
if ( !defined(\'ABSPATH\') )
define(\'ABSPATH\', dirname(__FILE__) . \'/\');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . \'wp-settings.php\');
');

$a = new WpConfigEditor($this->commonConfigPath, false);
$a->updateConfigConstant('TEST', 'value');

$expectedContent = '<?php
// ** MySQL settings ** //
/** The name of the database for WordPress */
define(\'DB_NAME\', \'vp01\');
$table_prefix = \'wp_\';
define(\'TEST\', \'value\');
/* C\'est tout, ne touchez pas à ce qui suit ! Bon blogging ! */
/** Absolute path to the WordPress directory. */
if ( !defined(\'ABSPATH\') )
define(\'ABSPATH\', dirname(__FILE__) . \'/\');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . \'wp-settings.php\');
';

$this->assertEquals($expectedContent, file_get_contents($this->commonConfigPath));
}

/**
* @test
*/
public function editorWorksWithoutHappyBloggingComment()
{
file_put_contents($this->commonConfigPath, '<?php
define(\'DB_NAME\', \'vp01\');
$table_prefix = \'wp_\';
if ( !defined(\'ABSPATH\') )
define(\'ABSPATH\', dirname(__FILE__) . \'/\');
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\');
if ( !defined(\'ABSPATH\') )
define(\'ABSPATH\', dirname(__FILE__) . \'/\');
require_once(ABSPATH . \'wp-settings.php\');
';

$this->assertEquals($expectedContent, file_get_contents($this->commonConfigPath));
}

/**
* @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));
Expand Down

0 comments on commit c799df8

Please sign in to comment.