Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

desc('Setup initial deploy');
task('setup', [
'wordpress:set-wp-home',
'scaffold:env',
]);

Expand Down
17 changes: 13 additions & 4 deletions wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@
run('curl {{ url }}/kinsta-clear-cache-all/');
});

task('wordpress:set-wp-home', function () {
// add WP_HOME to environment file
$file = realpath($_SERVER['DOCUMENT_ROOT']) . '/config/environments/' . currentHost()->getAlias() . '.php';
$lines = file($file);
$newLines = [];
foreach ($lines as $line) {
if (strpos($line, "Config::define('WP_HOME'") === false) {
$newLines[] = $line;
}
}
$newLines[] = 'Config::define(\'WP_HOME\', \'' . get('url', 'https://') . '\');';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is quite difficult to read because of the escaping. compare to eg.

Suggested change
$newLines[] = 'Config::define(\'WP_HOME\', \'' . get('url', 'https://') . '\');';
$newLines[] = sprintf("Config::define('WP_HOME', '%s');", get('url', 'https://'));

file_put_contents($file, implode('', $newLines));
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn’t this run on the remote host? it needs to run locally so you can commit it to git (or else a deploy would wipe the change).

current_path would hold the path https://github.com/deployphp/deployer/blob/fb6f2154ef36f03e4e4b0644206b06722ee09fd8/recipe/common.php#L99.

you can use __DIR__ to get root of the project, you can confirm it works by looking at how the rsync works https://github.com/deployphp/deployer/blob/master/contrib/rsync.php#L135

note that no production.php config exists on sites currently, you should check it exists and is writable: is_writable()


task('scaffold:env', function () {
if (test('[ -f {{deploy_path}}/shared/.env ]')) {
Expand All @@ -53,8 +66,6 @@
$dbName = ask('DB_NAME', get('scaffold_machine_name', ''));
$dbUser = ask('DB_USER', get('scaffold_machine_name', ''));
$dbPassword = askHiddenResponse('DB_PASSWORD');
$wpEnv = ask('WP_ENV', currentHost()->getAlias());
$wpHome = ask('WP_HOME', $url);

@oxyc oxyc Dec 1, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep these but default them to empty? And only add them if user has set them.

$domainCurrentSite = ask('DOMAIN_CURRENT_SITE', parse_url($url, PHP_URL_HOST));

run('mkdir -p {{deploy_path}}/shared');
Expand All @@ -63,8 +74,6 @@
run('sed -i "/^DB_NAME=/c\\DB_NAME=' . $dbName . '" {{deploy_path}}/shared/.env');
run('sed -i "/^DB_USER=/c\\DB_USER=' . $dbUser . '" {{deploy_path}}/shared/.env');
run('sed -i "/^DB_PASSWORD=/c\\DB_PASSWORD=\'' . $dbPassword . '\'" {{deploy_path}}/shared/.env');
run('sed -i "/^WP_ENV=/c\\WP_ENV=' . $wpEnv . '" {{deploy_path}}/shared/.env');
run('sed -i "/^WP_HOME=/c\\WP_HOME=' . $wpHome . '" {{deploy_path}}/shared/.env');
run('sed -i "/^DOMAIN_CURRENT_SITE=/c\\DOMAIN_CURRENT_SITE=' . $domainCurrentSite . '" {{deploy_path}}/shared/.env');

foreach (['AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT'] as $key) {
Expand Down