Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to configure site constants #14371

Merged
merged 6 commits into from
Mar 22, 2019
Merged
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
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
- ./bin/run-wp-unit-tests.sh

- name: E2E tests (Admin with plugins) (1/2)
env: WP_VERSION=latest POPULAR_PLUGINS=true
env: WP_VERSION=latest SCRIPT_DEBUG=false POPULAR_PLUGINS=true
install:
- ./bin/setup-local-env.sh
script:
Expand All @@ -74,7 +74,7 @@ jobs:
- npm run test-e2e -- --ci --cacheDirectory="$HOME/.jest-cache" --runTestsByPath $( awk 'NR % 2 == 0' < ~/.jest-e2e-tests )

- name: E2E tests (Admin with plugins) (2/2)
env: WP_VERSION=latest POPULAR_PLUGINS=true
env: WP_VERSION=latest SCRIPT_DEBUG=false POPULAR_PLUGINS=true
install:
- ./bin/setup-local-env.sh
script:
Expand All @@ -83,7 +83,7 @@ jobs:
- npm run test-e2e -- --ci --cacheDirectory="$HOME/.jest-cache" --runTestsByPath $( awk 'NR % 2 == 1' < ~/.jest-e2e-tests )

- name: E2E tests (Author without plugins) (1/2)
env: WP_VERSION=latest E2E_ROLE=author
env: WP_VERSION=latest SCRIPT_DEBUG=false E2E_ROLE=author
install:
- ./bin/setup-local-env.sh
script:
Expand All @@ -92,7 +92,7 @@ jobs:
- npm run test-e2e -- --ci --cacheDirectory="$HOME/.jest-cache" --runTestsByPath $( awk 'NR % 2 == 0' < ~/.jest-e2e-tests )

- name: E2E tests (Author without plugins) (2/2)
env: WP_VERSION=latest E2E_ROLE=author
env: WP_VERSION=latest SCRIPT_DEBUG=false E2E_ROLE=author
install:
- ./bin/setup-local-env.sh
script:
Expand Down
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ Alternatively, you can use your own local WordPress environment and clone this r

Next, open a terminal (or if on Windows, a command prompt) and navigate to the repository you cloned. Now type `npm install` to get the dependencies all set up. Then you can type `npm run dev` in your terminal or command prompt to keep the plugin building in the background as you work on it.

WordPress comes with specific [debug systems](https://codex.wordpress.org/Debugging_in_WordPress) designed to simplify the process as well as standardize code across core, plugins and themes. It is possible to use environment variables (`WP_DEBUG` and `SCRIPT_DEBUG`) to update a site's configuration constants located in `wp-config.php` file. Both flags can be disabled at any time by running the following command:
```
SCRIPT_DEBUG=false WP_DEBUG=false ./bin/setup-local-env.sh
```
By default, both flags will be set to `true`.

### On A Remote Server

Open a terminal (or if on Windows, a command prompt) and navigate to the repository you cloned. Now type `npm install` to get the dependencies all set up. Once that finishes, you can type `npm run build`. You can now upload the entire repository to your `wp-content/plugins` directory on your web server and activate the plugin from the WordPress admin.
Expand Down
2 changes: 2 additions & 0 deletions bin/bootstrap-env.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env bash

WP_VERSION=${WP_VERSION-latest}
WP_DEBUG=${WP_DEBUG-true}
Copy link
Member

Choose a reason for hiding this comment

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

Possible thought on the error: Is this the correct syntax for assigning the default? Is the colon : not required?

See:

Copy link
Member

Choose a reason for hiding this comment

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

Also a bit curious why we don't use a .env file: https://docs.docker.com/compose/env-file/

Copy link
Member Author

Choose a reason for hiding this comment

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

I followed other occurrences in the file. I think it works correctly, although I personally don't know which syntax would be preferred.

The issue was caused by the fact that PHP didn't have those constants defined and WP CLI's implementation is limited to updating existing constants ... c34d3c0 fixes it.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, re-reading the above document, the colon behavior is explained:

Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.

I expect it should be fine.

Copy link
Member

Choose a reason for hiding this comment

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

The issue was caused by the fact that PHP didn't have those constants defined and WP CLI's implementation is limited to updating existing constants ... c34d3c0 fixes it.

So, to clarify, despite the fact that in c34d3c0 you're hard-coding the constants, bin/install-wordpress.sh will subsequently update them according to the value of the environment variable? And it has to be hard-coded because otherwise WP-CLI will refuse to update them (as they don't otherwise exist in the file)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this is complex to explain but correct. Each constant needs to be defined in wp-config.php if we want to be able to update it with WP-CLI.

SCRIPT_DEBUG=${SCRIPT_DEBUG-true}
DOCKER=${DOCKER-false}
DOCKER_ENV=${DOCKER_ENV-ci}
DOCKER_COMPOSE_FILE_OPTIONS="-f docker-compose.yml"
Expand Down
15 changes: 15 additions & 0 deletions bin/install-wordpress.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ if [ "$CURRENT_URL" != "http://localhost:$HOST_PORT" ]; then
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm -u 33 $CLI option update siteurl "http://localhost:$HOST_PORT" --quiet
fi

# Configure site constants.
echo -e $(status_message "Configuring site constants...")
Copy link
Member Author

Choose a reason for hiding this comment

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

There is a lot of code duplication here. Commits which simplify it are warmly welcomed :)

Copy link
Contributor

Choose a reason for hiding this comment

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

We could use macro-like behaviour with the following:

$ A=hello; B=salut
$ VAR=A; eval echo \$$VAR
hello
$ VAR=B; eval echo \$$VAR
salut

WP_DEBUG_CURRENT=$(docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run -T --rm -u 33 $CLI config get --type=constant --format=json WP_DEBUG)
if [ $WP_DEBUG != $WP_DEBUG_CURRENT ]; then
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm -u 33 $CLI config set WP_DEBUG $WP_DEBUG --raw --type=constant --quiet
WP_DEBUG_RESULT=$(docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run -T --rm -u 33 $CLI config get --type=constant --format=json WP_DEBUG)
echo -e $(status_message "WP_DEBUG: $WP_DEBUG_RESULT...")
fi
SCRIPT_DEBUG_CURRENT=$(docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run -T --rm -u 33 $CLI config get --type=constant --format=json SCRIPT_DEBUG)
if [ $SCRIPT_DEBUG != $SCRIPT_DEBUG_CURRENT ]; then
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm -u 33 $CLI config set SCRIPT_DEBUG $SCRIPT_DEBUG --raw --type=constant --quiet
SCRIPT_DEBUG_RESULT=$(docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run -T --rm -u 33 $CLI config get --type=constant --format=json SCRIPT_DEBUG)
echo -e $(status_message "SCRIPT_DEBUG: $SCRIPT_DEBUG_RESULT...")
fi

# Activate Gutenberg.
echo -e $(status_message "Activating Gutenberg...")
docker-compose $DOCKER_COMPOSE_FILE_OPTIONS run --rm -u 33 $CLI plugin activate gutenberg --quiet
Expand Down
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ services:
environment:
WORDPRESS_DB_PASSWORD: example
ABSPATH: /usr/src/wordpress/
WORDPRESS_DEBUG: 1
WORDPRESS_CONFIG_EXTRA: |
define('SCRIPT_DEBUG', true );
volumes:
- wordpress:/var/www/html
- .:/var/www/html/wp-content/plugins/gutenberg
Expand Down Expand Up @@ -56,6 +59,9 @@ services:
WORDPRESS_DB_NAME: wordpress_e2e_tests
WORDPRESS_DB_PASSWORD: example
ABSPATH: /usr/src/wordpress/
WORDPRESS_DEBUG: 1
WORDPRESS_CONFIG_EXTRA: |
define('SCRIPT_DEBUG', true );
volumes:
- wordpress_e2e_tests:/var/www/html
- .:/var/www/html/wp-content/plugins/gutenberg
Expand Down