Skip to content

Commit

Permalink
fix: Update SnapWP environment variables (#57)
Browse files Browse the repository at this point in the history
* refactor: update env var names

* refactor: replace `NEXT_URL` with `NEXT_PUBLIC_URL`

* feat: add commented property to variables

* fix: linting errors

* fix: remove trailing whitespace

* refactor: remove commented from variables, calculate it based on defaults

* fix: formatting errors in Admin.php

* fix : tests

* chore: cleanup

* feat: update docs

* fix : tests

---------

Co-authored-by: Ta5r <[email protected]>
Co-authored-by: Dovid Levine <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2025
1 parent 7d52e4e commit 8c2be0a
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 54 deletions.
12 changes: 8 additions & 4 deletions access-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ function snapwp_helper_get_env_variables() {
return new \WP_Error( 'graphql_not_found', 'WPGraphQL must be installed and activated.', [ 'status' => 500 ] );
}

$upload_dir = wp_get_upload_dir();

return [
'NODE_TLS_REJECT_UNAUTHORIZED' => '',
'NEXT_URL' => '',
'HOME_URL' => get_home_url(),
'GRAPHQL_ENDPOINT' => graphql_get_endpoint(),
'NODE_TLS_REJECT_UNAUTHORIZED' => '',
'NEXT_PUBLIC_URL' => '',
'NEXT_PUBLIC_WORDPRESS_URL' => get_home_url(),
'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => graphql_get_endpoint(),
'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => str_replace( ABSPATH, '', $upload_dir['basedir'] ),
'NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX' => rest_get_url_prefix(),
];
}
}
14 changes: 8 additions & 6 deletions docs/rest-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ curl -X GET \

### Parameters

This endpoint does not require any parameters to be passed in the request body. The .env file content is generated based on WordPress settings.

- `NEXT_URL` (Required): The headless frontend domain URL.
- `HOME_URL` (Required): The WordPress "frontend" domain URL.
- `GRAPHQL_ENDPOINT`: The WordPress GraphQL endpoint. (Default: `graphql`)
- `NODE_TLS_REJECT_UNAUTHORIZED`: Enable if connecting to a self-signed cert. (Default: commented out)
This endpoint does not require any parameters to be passed in the request body. The .env file content is generated based on WordPress settings. Unchanged variables will be commented out.

- `NODE_TLS_REJECT_UNAUTHORIZED`: Enable if connecting to a self-signed cert. (Default: `0`)
- `NEXT_PUBLIC_URL` (Required): The headless frontend domain URL. (Default: `http://localhost:3000`)
- `NEXT_PUBLIC_WORDPRESS_URL` (Required): The WordPress "frontend" domain URL.
- `NEXT_PUBLIC_GRAPHQL_ENDPOINT`: The WordPress GraphQL endpoint. (Default: `graphql`)
- `NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH`: The WordPress Uploads directory path. (Default: `wp-content/uploads`)
- `NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX`: The WordPress REST URL Prefix. (Default: `wp-json`)

Note: This endpoint requires authentication with administrator privileges.

Expand Down
10 changes: 5 additions & 5 deletions src/Modules/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ public function render_menu(): void {
<tbody>
<?php foreach ( $variables as $key => $value ) : ?>
<?php
if ( in_array( $key, [ 'NODE_TLS_REJECT_UNAUTHORIZED', 'NEXT_URL' ], true ) ) {
if ( in_array( $key, [ 'NODE_TLS_REJECT_UNAUTHORIZED', 'NEXT_PUBLIC_URL' ], true ) ) {
continue; }
?>
<tr>
<td><?php echo esc_html( $key ); ?></td>
<td><?php echo esc_html( $value ); ?></td>
<td><?php echo wp_kses_post( sprintf( '<code>%s</code>', $value ) ); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
Expand Down Expand Up @@ -164,8 +164,8 @@ public function render_menu(): void {
<?php
printf(
// translators: %s is the command, wrapped in code tags.
esc_html__( 'Then update the %s variable with the URL of your WordPress site.', 'snapwp-helper' ),
'<code>NEXT_URL</code>'
esc_html__( 'Then update the %s variable with the URL for your headless frontend.', 'snapwp-helper' ),
'<code>NEXT_PUBLIC_URL</code>'
);
?>
</p>
Expand All @@ -180,7 +180,7 @@ public function render_menu(): void {
<?php
printf(
// Translators: %1$s and %2$s are the commands, wrapped in code tags.
esc_html__( 'Run %1$s (for development) or %2$s (for production) and visit the `NEXT_URL` from `.env` (updated in Step 2), in your browser to see SnapWP in action!.', 'snapwp-helper' ),
esc_html__( 'Run %1$s (for development) or %2$s (for production) and visit the `NEXT_PUBLIC_URL` from `.env` (updated in Step 2), in your browser to see SnapWP in action!.', 'snapwp-helper' ),
'<code>npm run dev</code>',
'<code>npm run build && npm run start</code>'
);
Expand Down
26 changes: 18 additions & 8 deletions src/Modules/EnvGenerator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,16 @@ protected function prepare_variables( array $variables ): ?string {
foreach ( $variables as $name => $value ) {
$variable_output = $this->prepare_variable( $name, $value );

if ( null !== $variable_output ) {
$output .= $variable_output;
if ( empty( $variable_output ) ) {
continue;
}

// Add a newline if there's already content.
if ( ! empty( $output ) ) {
$output .= "\n";
}

$output .= $variable_output;
}

return $output ?: null;
Expand Down Expand Up @@ -94,13 +101,16 @@ protected function prepare_variable( string $name, ?string $value ): ?string {

// Determine the final value to output.
$resolved_value = ! empty( $value ) ? $value : $default;
if ( empty( $resolved_value ) ) {
$resolved_value = null;
}

$comment = ! empty( $description ) ? sprintf( "\n# %s\n", $description ) : '';
$output = null !== $resolved_value ? sprintf( '%s=%s\n', $name, $resolved_value ) : sprintf( '# %s=\'0\'\n', $name );
// Prepare the output.
$comment = ! empty( $description ) ? sprintf( "\n# %s\n", $description ) : '';
$env_output = sprintf( '%s=%s', $name, $resolved_value );

// Comment out variables if they're not required and have the default value.
if ( ! $required && $resolved_value === $default ) {
$env_output = '# ' . $env_output;
}

return $comment . $output;
return $comment . $env_output;
}
}
22 changes: 16 additions & 6 deletions src/Modules/EnvGenerator/VariableRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,36 @@ class VariableRegistry {
* @var array<string,array{description:string,default:string,required:bool}>
*/
private const VARIABLES = [
'NODE_TLS_REJECT_UNAUTHORIZED' => [
'NODE_TLS_REJECT_UNAUTHORIZED' => [
'description' => 'Enable if connecting to a self-signed cert',
'default' => '',
'default' => '0',
'required' => false,
],
'NEXT_URL' => [
'NEXT_PUBLIC_URL' => [
'description' => 'The headless frontend domain URL',
'default' => '',
'default' => 'http://localhost:3000',
'required' => false,
],
'HOME_URL' => [
'NEXT_PUBLIC_WORDPRESS_URL' => [
'description' => 'The WordPress "frontend" domain URL',
'default' => '',
'required' => true,
],
'GRAPHQL_ENDPOINT' => [
'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => [
'description' => 'The WordPress GraphQL endpoint',
'default' => 'graphql',
'required' => false,
],
'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => [
'description' => 'The WordPress Uploads directory path',
'default' => 'wp-content/uploads',
'required' => false,
],
'NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX' => [
'description' => 'The WordPress REST URL Prefix',
'default' => 'wp-json',
'required' => false,
],
];

/**
Expand Down
101 changes: 77 additions & 24 deletions tests/Integration/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace SnapWP\Helper\Tests\Integration;

use lucatume\WPBrowser\TestCase\WPTestCase;
use SnapWP\Helper\Modules\EnvGenerator\Generator;
use SnapWP\Helper\Modules\EnvGenerator\VariableRegistry;
use lucatume\WPBrowser\TestCase\WPTestCase;

/**
* Class GeneratorTest
Expand All @@ -24,10 +24,12 @@ public function testGeneratorInitialization(): void {
$registry = new VariableRegistry();

$values = [
'NODE_TLS_REJECT_UNAUTHORIZED' => '',
'NEXT_URL' => 'http://localhost:3000',
'HOME_URL' => 'https://headless-demo.local',
'GRAPHQL_ENDPOINT' => '',
'NODE_TLS_REJECT_UNAUTHORIZED' => '',
'NEXT_PUBLIC_URL' => 'http://localhost:3000',
'NEXT_PUBLIC_WORDPRESS_URL' => 'https://headless-demo.local',
'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => '',
'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => '',
'NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX' => '',
];

$generator = new Generator( $values, $registry );
Expand All @@ -41,19 +43,38 @@ public function testGeneratorInitialization(): void {
public function testGenerateEnvContent(): void {
$registry = new VariableRegistry();
$values = [
'NODE_TLS_REJECT_UNAUTHORIZED' => '5',
'NEXT_URL' => 'http://localhost:3000',
'HOME_URL' => 'https://headless-demo.local',
'GRAPHQL_ENDPOINT' => '/test_endpoint',
'INVALID_VARIABLE' => 'should-not-be-included', // This should not be included in the output.
'NODE_TLS_REJECT_UNAUTHORIZED' => '5',
'NEXT_PUBLIC_URL' => 'http://localhost:3000',
'NEXT_PUBLIC_WORDPRESS_URL' => 'https://headless-demo.local',
'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => '/test_endpoint',
'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => 'uploads',
'NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX' => 'api',
'INVALID_VARIABLE' => 'should-not-be-included', // This should not be included in the output.
];

$generator = new Generator( $values, $registry );

// Generate the .env content.
$content = $generator->generate();

$expectedContent = "\n# Enable if connecting to a self-signed cert\nNODE_TLS_REJECT_UNAUTHORIZED=5\\n\n# The headless frontend domain URL\nNEXT_URL=http://localhost:3000\\n\n# The WordPress \"frontend\" domain URL\nHOME_URL=https://headless-demo.local\\n\n# The WordPress GraphQL endpoint\nGRAPHQL_ENDPOINT=/test_endpoint\\n";
$expectedContent = '
# Enable if connecting to a self-signed cert
NODE_TLS_REJECT_UNAUTHORIZED=5
# The headless frontend domain URL
# NEXT_PUBLIC_URL=http://localhost:3000
# The WordPress "frontend" domain URL
NEXT_PUBLIC_WORDPRESS_URL=https://headless-demo.local
# The WordPress GraphQL endpoint
NEXT_PUBLIC_GRAPHQL_ENDPOINT=/test_endpoint
# The WordPress Uploads directory path
NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=uploads
# The WordPress REST URL Prefix
NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=api';

$this->assertSame( $expectedContent, $content );
}
Expand All @@ -64,10 +85,12 @@ public function testGenerateEnvContent(): void {
public function testMissingRequiredValuesEnvContent(): void {
$registry = new VariableRegistry();
$values = [
'NODE_TLS_REJECT_UNAUTHORIZED' => '',
'NEXT_URL' => '',
'HOME_URL' => '',
'GRAPHQL_ENDPOINT' => '',
'NODE_TLS_REJECT_UNAUTHORIZED' => '',
'NEXT_PUBLIC_URL' => '',
'NEXT_PUBLIC_WORDPRESS_URL' => '',
'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => '',
'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => '',
'NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX' => '',
];

$generator = new Generator( $values, $registry );
Expand All @@ -88,10 +111,12 @@ public function testDefaultValuesForEnvContent(): void {

// CASE : For NODE_TLS_REJECT_UNAUTHORIZED with no default value, Generator class should comment out the variable in .ENV content.
$values = [
'NODE_TLS_REJECT_UNAUTHORIZED' => '',
'NEXT_URL' => 'http://localhost:3000',
'HOME_URL' => 'https://headless-demo.local',
'GRAPHQL_ENDPOINT' => '/test_endpoint',
'NODE_TLS_REJECT_UNAUTHORIZED' => '',
'NEXT_PUBLIC_URL' => '',
'NEXT_PUBLIC_WORDPRESS_URL' => 'https://headless-demo.local',
'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => '/test_endpoint',
'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => '',
'NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX' => '',
];

$generator = new Generator( $values, $registry );
Expand All @@ -100,24 +125,52 @@ public function testDefaultValuesForEnvContent(): void {
$content = $generator->generate();

// Define expected content.
$expectedContent = "\n# Enable if connecting to a self-signed cert\n# NODE_TLS_REJECT_UNAUTHORIZED='0'\\n\n# The headless frontend domain URL\nNEXT_URL=http://localhost:3000\\n\n# The WordPress \"frontend\" domain URL\nHOME_URL=https://headless-demo.local\\n\n# The WordPress GraphQL endpoint\nGRAPHQL_ENDPOINT=/test_endpoint\\n";
$expectedContent = '
# Enable if connecting to a self-signed cert
# NODE_TLS_REJECT_UNAUTHORIZED=0
# The headless frontend domain URL
# NEXT_PUBLIC_URL=http://localhost:3000
# The WordPress "frontend" domain URL
NEXT_PUBLIC_WORDPRESS_URL=https://headless-demo.local
# The WordPress GraphQL endpoint
NEXT_PUBLIC_GRAPHQL_ENDPOINT=/test_endpoint
# The WordPress Uploads directory path
# NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=wp-content/uploads
# The WordPress REST URL Prefix
# NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=wp-json';

$this->assertSame( $expectedContent, $content );

// CASE : For GRAPHQL_ENDPOINT, Generator should use the default value of the variable.
$values = [
'NODE_TLS_REJECT_UNAUTHORIZED' => '',
'NEXT_URL' => 'http://localhost:3000',
'HOME_URL' => 'https://headless-demo.local',
'GRAPHQL_ENDPOINT' => '',
'NEXT_PUBLIC_URL' => 'http://localhost:3000',
'NEXT_PUBLIC_WORDPRESS_URL' => 'https://headless-demo.local',
'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => '',
];

$generator = new Generator( $values, $registry );

// Generate the .env content.
$content = $generator->generate();

$expectedContent = "\n# Enable if connecting to a self-signed cert\n# NODE_TLS_REJECT_UNAUTHORIZED='0'\\n\n# The headless frontend domain URL\nNEXT_URL=http://localhost:3000\\n\n# The WordPress \"frontend\" domain URL\nHOME_URL=https://headless-demo.local\\n\n# The WordPress GraphQL endpoint\nGRAPHQL_ENDPOINT=graphql\\n";
$expectedContent = '
# Enable if connecting to a self-signed cert
# NODE_TLS_REJECT_UNAUTHORIZED=0
# The headless frontend domain URL
# NEXT_PUBLIC_URL=http://localhost:3000
# The WordPress "frontend" domain URL
NEXT_PUBLIC_WORDPRESS_URL=https://headless-demo.local
# The WordPress GraphQL endpoint
# NEXT_PUBLIC_GRAPHQL_ENDPOINT=graphql';

$this->assertSame( $expectedContent, $content );
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Integration/RestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function testRegisterRoutes(): void {

/**
* Tests if the endpoint is accessible, and the env content in response is correct.
* Assuming standard default values.
*/
public function testGenerateEnvEndpoint(): void {

Expand All @@ -86,7 +87,7 @@ public function testGenerateEnvEndpoint(): void {
$this->assertNotEmpty( $actual_data['content'] );
$search = '\n';
$replace = '';
$expected = "\n# Enable if connecting to a self-signed cert\n# NODE_TLS_REJECT_UNAUTHORIZED='0'\n# The headless frontend domain URL\n# NEXT_URL='0'\n# The WordPress \"frontend\" domain URL\nHOME_URL=" . get_home_url() . "\n# The WordPress GraphQL endpoint\nGRAPHQL_ENDPOINT=" . graphql_get_endpoint();
$expected = "\n# Enable if connecting to a self-signed cert\n# NODE_TLS_REJECT_UNAUTHORIZED=0\n\n# The headless frontend domain URL\n# NEXT_PUBLIC_URL=http://localhost:3000\n\n# The WordPress \"frontend\" domain URL\nNEXT_PUBLIC_WORDPRESS_URL=" . get_home_url() . "\n\n# The WordPress GraphQL endpoint\n# NEXT_PUBLIC_GRAPHQL_ENDPOINT=" . graphql_get_endpoint() . "\n\n# The WordPress Uploads directory path\n# NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=" . str_replace( ABSPATH, '', wp_get_upload_dir()['basedir'] ) . "\n\n# The WordPress REST URL Prefix\n# NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=" . rest_get_url_prefix();

$this->assertEquals( $expected, str_replace( $search, $replace, $actual_data['content'] ) );

Expand Down

0 comments on commit 8c2be0a

Please sign in to comment.