Skip to content

Commit

Permalink
[BUGS-7620] define WP_HOME (#115)
Browse files Browse the repository at this point in the history
* require a new application.pantheon.php file

* use application.pantheon.php to define WP_HOME
and make it available for future application.php modifications

* ignore internal errors in phpcs rulesets

* linting

* ignore application.pantheon.php filename pattern

* bump actions/cache to v4

* patch set-output with environment var
see https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/

* add the WP_HOME and WP_SITEURL values to the environment after we define them
if they're already defined, we don't run this code path

* look for the LANDO environment variable to check for Lando

* use the lando environment variable in application.php, too

* fix spacing

* ignore the putenv that we added
  • Loading branch information
jazzsequence authored Apr 15, 2024
1 parent 988a03f commit f5ccfd5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ jobs:

- name: Cache Composer dependencies
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- uses: actions/cache@v2
- uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
Expand Down
42 changes: 42 additions & 0 deletions config/application.pantheon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Pantheon platform application settings.
*
* IMPORTANT NOTE:
* Do not modify this file. This file is maintained by Pantheon.
*
* Site-specific modifications belong in config/application.php, not this file.
* This file may change in future releases and modifications would cause
* conflicts when attempting to apply upstream updates.
*/

use Roots\WPConfig\Config;
use function Env\env;

if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && ! isset( $_ENV['LANDO'] ) ) {
// We can use PANTHEON_SITE_NAME here because it's safe to assume we're on a Pantheon environment if PANTHEON_ENVIRONMENT is set.
$sitename = $_ENV['PANTHEON_SITE_NAME'];
$baseurl = $_ENV['PANTHEON_ENVIRONMENT'] . '-' . $sitename . '.pantheonsite.io';

$scheme = 'http';
if ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) {
$scheme = 'https';
}

// Define the WP_HOME and WP_SITEURL constants if they aren't already defined.
if ( ! env( 'WP_HOME' ) ) {
// If HTTP_HOST is set, use that as the base URL. It's probably more accurate.
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
$baseurl = $_SERVER['HTTP_HOST'];
}

$homeurl = $scheme . '://' . $baseurl;
Config::define( 'WP_HOME', $homeurl );
putenv( 'WP_HOME=' . $homeurl );

if ( ! env( 'WP_SITEURL' ) ) {
Config::define( 'WP_SITEURL', $homeurl . '/wp' );
putenv( 'WP_SITEURL=' . $homeurl . '/wp' );
}
}
}
9 changes: 7 additions & 2 deletions config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@
// Check if a .env file exists.
file_exists( $root_dir . '/.env' ) ||
// Also check if we're using Lando and a .env.local file exists.
( file_exists( $root_dir . '/.env.local' ) && 'lando' === $_ENV['PANTHEON_ENVIRONMENT'] )
( file_exists( $root_dir . '/.env.local' ) && isset( $_ENV['LANDO'] ) && 'ON' === $_ENV['LANDO'] )
) {
$dotenv->load();
if ( ! env( 'DATABASE_URL' ) ) {
$dotenv->required( [ 'DB_NAME', 'DB_USER', 'DB_PASSWORD' ] );
}
}

/**
* Include Pantheon application settings.
*/
require_once __DIR__ . '/application.pantheon.php';

/**
* Set up our global environment constant and load its config first
* Default: production
Expand Down Expand Up @@ -74,7 +79,7 @@
/**
* Pantheon modifications
*/
if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'lando' !== $_ENV['PANTHEON_ENVIRONMENT'] ) {
if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && ! isset( $_ENV['LANDO'] ) ) {
Config::define( 'DB_HOST', $_ENV['DB_HOST'] . ':' . $_ENV['DB_PORT'] );
} else {
/**
Expand Down
12 changes: 12 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,21 @@
<exclude name="WordPress.WP.GlobalVariablesOverride.Prohibited">
<exclude-pattern>config/application.php</exclude-pattern>
</exclude>
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase">
<exclude-pattern>config/application.pantheon.php</exclude-pattern>
</exclude>
<exclude name="WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv">
<exclude-pattern>config/application.pantheon.php</exclude-pattern>
</exclude>
</rule>
<!-- End Pantheon addition -->

<!--
Prevent errors caused by WordPress Coding Standards not supporting PHP 8.0+.
See https://github.com/WordPress/WordPress-Coding-Standards/issues/2035
-->
<ini name="error_reporting" value="E_ALL &#38; ~E_DEPRECATED" />

<!-- Show colors in console -->
<arg value="-colors"/>

Expand Down

0 comments on commit f5ccfd5

Please sign in to comment.