From a721f5a910d4bc74a1d4074d527f20bfd51128c6 Mon Sep 17 00:00:00 2001 From: graham73may Date: Tue, 10 Sep 2024 01:19:21 +0100 Subject: [PATCH] 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 --- README.md | 35 ++++++++++++++++++++ install.yaml | 13 +++++++- scripts/remove-wordpress-settings.sh | 24 ++++++++++++++ scripts/setup-wordpress-settings.sh | 44 ++++++++++++++++++++++++++ scripts/wp-config-ddev-browsersync.php | 13 ++++++++ 5 files changed, 128 insertions(+), 1 deletion(-) create mode 100755 scripts/remove-wordpress-settings.sh create mode 100755 scripts/setup-wordpress-settings.sh create mode 100644 scripts/wp-config-ddev-browsersync.php diff --git a/README.md b/README.md index da3803e..ad887f6 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,10 @@ Once Browsersync is running, visit `https://.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 . '/' ); +``` diff --git a/install.yaml b/install.yaml index 551b70d..1fb6179 100644 --- a/install.yaml +++ b/install.yaml @@ -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 diff --git a/scripts/remove-wordpress-settings.sh b/scripts/remove-wordpress-settings.sh new file mode 100755 index 0000000..6ae73fa --- /dev/null +++ b/scripts/remove-wordpress-settings.sh @@ -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 diff --git a/scripts/setup-wordpress-settings.sh b/scripts/setup-wordpress-settings.sh new file mode 100755 index 0000000..9d5fb3a --- /dev/null +++ b/scripts/setup-wordpress-settings.sh @@ -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 diff --git a/scripts/wp-config-ddev-browsersync.php b/scripts/wp-config-ddev-browsersync.php new file mode 100644 index 0000000..4819fbb --- /dev/null +++ b/scripts/wp-config-ddev-browsersync.php @@ -0,0 +1,13 @@ +