Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ddev/ddev-browsersync
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2.8
Choose a base ref
...
head repository: ddev/ddev-browsersync
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2.9
Choose a head ref
  • 1 commit
  • 5 files changed
  • 1 contributor

Commits on Sep 10, 2024

  1. Improving WordPress support (#57)

    * Adding WordPress config additions (based on a similar approach by ddev-redis addon)
    
    * Replace the actual config file with the temp file
    
    * Replace the actual config file with the temp file
    
    * Include addon files in the project
    
    * Add ${DDEV_APPROOT} to shell script. Added some additional notes to README.
    
    * Added some wp-config notes to the README
    
    * Revert formatting changes to README
    
    * Changed approach so the script modified wp-config.php instead of wp-config-ddev.php
    
    * Missing " mark
    
    * Improving the removal process to do a better job of cleaning up wp-config.php
    graham73may authored Sep 10, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    a721f5a View commit details
Showing with 128 additions and 1 deletion.
  1. +35 −0 README.md
  2. +12 −1 install.yaml
  3. +24 −0 scripts/remove-wordpress-settings.sh
  4. +44 −0 scripts/setup-wordpress-settings.sh
  5. +13 −0 scripts/wp-config-ddev-browsersync.php
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -58,6 +58,10 @@ Once Browsersync is running, visit `https://<project>.ddev.site:3000` or run `dd
4. Adds a `.ddev/docker-compose.browsersync.yaml`, which exposes and routes the ports necessary.
5. Adds a `ddev browsersync` shell command, which lets you easily start Browsersync when you want it.

For WordPress projects, this add-on also:
* Adds a `wp-config-ddev-browser.php` file which modifies the WP_HOME and WP_SITEURL values to work with Browsersync.
* On install, modifies the `wp-config-ddev.php` file to include the `wp-config-ddev-browser.php` file.

## Other ways to use browsersync with this add-on

There are many other options to integrate browsersync into your project, including:
@@ -128,3 +132,34 @@ ddev exec npm run watch
- Browsersync will be running on **HTTPS** at `https://browsersync-demo.ddev.site:3000`

**Contributed and maintained by [tyler36](https://github.com/tyler36)**

### WordPress Configuration Changes.

The changes this add-on makes to the `wp-config-ddev.php` file during installation can be seen below.

The `wp-config-ddev-browserync.php` file is included before the `/** WP_HOME URL */` comment.

Before:

```php
/** WP_HOME URL */
defined( 'WP_HOME' ) || define( 'WP_HOME', 'https://projectname.ddev.site' );

/** WP_SITEURL location */
defined( 'WP_SITEURL' ) || define( 'WP_SITEURL', WP_HOME . '/' );
```

After:

```php
/** Include WP_HOME and WP_SITEURL settings required for Browsersync. */
if ( ( file_exists( __DIR__ . '/wp-config-ddev-browsersync.php' ) ) ) {
include __DIR__ . '/wp-config-ddev-browsersync.php';
}

/** WP_HOME URL */
defined( 'WP_HOME' ) || define( 'WP_HOME', 'https://projectname.ddev.site' );

/** WP_SITEURL location */
defined( 'WP_SITEURL' ) || define( 'WP_SITEURL', WP_HOME . '/' );
```
13 changes: 12 additions & 1 deletion install.yaml
Original file line number Diff line number Diff line change
@@ -13,7 +13,9 @@ project_files:
- web-build/Dockerfile.ddev-browsersync
- browser-sync.js
- commands/web/browsersync

- scripts/wp-config-ddev-browsersync.php
- scripts/remove-wordpress-settings.sh
- scripts/setup-wordpress-settings.sh

post_install_actions:
- |
@@ -25,3 +27,12 @@ post_install_actions:
#ddev-nodisplay
#ddev-description:Remove old 'docker-compose.browsersync.yaml'
if grep "#ddev-generated" $DDEV_APPROOT/.ddev/docker-compose.browsersync.yaml 2>/dev/null; then rm -f "$DDEV_APPROOT/.ddev/docker-compose.browsersync.yaml"; fi
#ddev-description:Install browsersync settings for WordPress if applicable
scripts/setup-wordpress-settings.sh
removal_actions:
- |
#ddev-nodisplay
#ddev-description:Remove browsersync settings for WordPress if applicable
rm -f "${DDEV_APPROOT}/wp-config-ddev-browsersync.php"
scripts/remove-wordpress-settings.sh
24 changes: 24 additions & 0 deletions scripts/remove-wordpress-settings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
#ddev-generated
set -e

if [[ $DDEV_PROJECT_TYPE != wordpress ]] ;
then
exit 0
fi

if ( ddev debug configyaml 2>/dev/null | grep 'disable_settings_management:\s*true' >/dev/null 2>&1 ) ; then
exit 0
fi

SETTINGS_FILE_NAME="${DDEV_APPROOT}/wp-config.php"

echo "Removing wp-config-ddev-browsersync.php from: ${SETTINGS_FILE_NAME}"

# Remove the ddev-browsersync config that we added.
awk '
/\/\*\* Include for ddev-browsersync to modify WP_HOME and WP_SITEURL\./ { skip=1 }
skip && /\}.*$/ { skip=0; getline; next }
!skip
' ${DDEV_APPROOT}/wp-config.php > ${DDEV_APPROOT}/wp-config-temp.php
mv ${DDEV_APPROOT}/wp-config-temp.php ${DDEV_APPROOT}/wp-config.php
44 changes: 44 additions & 0 deletions scripts/setup-wordpress-settings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
#ddev-generated
set -e

if [[ $DDEV_PROJECT_TYPE != wordpress ]] ;
then
exit 0
fi

if ( ddev debug configyaml 2>/dev/null | grep 'disable_settings_management:\s*true' >/dev/null 2>&1 ) ; then
exit 0
fi

cp scripts/wp-config-ddev-browsersync.php $DDEV_APPROOT/

SETTINGS_FILE_NAME="${DDEV_APPROOT}/wp-config.php"

echo "Settings file name: ${SETTINGS_FILE_NAME}"

# If wp-config.php already contains the require_once() then exit early.
if grep -q "/\*\* Include for ddev-browsersync to modify WP_HOME and WP_SITEURL. \*/" ${DDEV_APPROOT}/wp-config.php; then
exit 0
fi

echo "Adding wp-config-ddev-browsersync.php to: ${SETTINGS_FILE_NAME}"

# Append our code before the ddev config comment.
awk '
/\/\/ Include for ddev-managed settings in wp-config-ddev.php./ {
print "/** Include for ddev-browsersync to modify WP_HOME and WP_SITEURL. */"
print "$ddev_browsersync_settings = dirname( __FILE__ ) . \"/wp-config-ddev-browsersync.php\";"
print ""
print "if ( is_readable( $ddev_browsersync_settings ) ) {"
print " require_once( $ddev_browsersync_settings );"
print "}"
print ""
print "// Include for ddev-managed settings in wp-config-ddev.php."
next
}
{print}
' ${DDEV_APPROOT}/wp-config.php > ${DDEV_APPROOT}/wp-config-temp.php

# Replace the real config file with the modified version in temporary file.
mv ${DDEV_APPROOT}/wp-config-temp.php ${DDEV_APPROOT}/wp-config.php
13 changes: 13 additions & 0 deletions scripts/wp-config-ddev-browsersync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
// #ddev-generated
/** Allow any domain/port so WordPress allows browsersync's :3000 URLs */
if ( ! empty( $_SERVER['SERVER_PORT'] ) && ! empty( $_SERVER['SERVER_NAME'] ) ) {
// phpcs:ignore
$domain = sprintf( '%s://%s', $_SERVER['SERVER_PORT'] == 80 ? 'http' : 'https', $_SERVER['SERVER_NAME'] );

/** WP_HOME URL */
define( 'WP_HOME', $domain );

/** WP_SITEURL location */
define( 'WP_SITEURL', $domain );
}