Skip to content

Commit

Permalink
dev: Update env variables for conformity (#58)
Browse files Browse the repository at this point in the history
* add : sanitize URL and path helpers in access-functions

* update : add a leading slash for uploads path and WP REST URL prefix

* fix : RESTController test failing by updating the defaults in VariableRegistry

* fix : PHPCS - Closure not using "$this" should be declared static.

* fix : failing Generator tests due to mising slash

* chore: directly prefix env variables

* fix: ensure home url has no trailing slash

* fix: make graphql endpoint required and fix default

---------

Co-authored-by: Dovid Levine <[email protected]>
  • Loading branch information
Ta5r and justlevine authored Jan 20, 2025
1 parent 39d592b commit 546b05d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 39 deletions.
10 changes: 6 additions & 4 deletions access-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ function snapwp_helper_get_env_variables() {
return $token;
}

$upload_dir = wp_get_upload_dir();
// Ensure the upload path has a leading slash for consistency.
$upload_dir = wp_get_upload_dir();
$upload_path = '/' . ltrim( str_replace( ABSPATH, '', $upload_dir['basedir'] ), '/' );

return [
'NODE_TLS_REJECT_UNAUTHORIZED' => '',
'NEXT_PUBLIC_URL' => '',
'NEXT_PUBLIC_WORDPRESS_URL' => get_home_url(),
'NEXT_PUBLIC_WORDPRESS_URL' => untrailingslashit( 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(),
'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => $upload_path,
'NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX' => '/' . rest_get_url_prefix(),
'INTROSPECTION_TOKEN' => $token,
];
}
Expand Down
8 changes: 4 additions & 4 deletions src/Modules/EnvGenerator/VariableRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ class VariableRegistry {
],
'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => [
'description' => 'The WordPress GraphQL endpoint',
'default' => 'graphql',
'required' => false,
'default' => 'index.php?graphql',
'required' => true,
],
'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => [
'description' => 'The WordPress Uploads directory path',
'default' => 'wp-content/uploads',
'default' => '/wp-content/uploads',
'required' => false,
],
'NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX' => [
'description' => 'The WordPress REST URL Prefix',
'default' => 'wp-json',
'default' => '/wp-json',
'required' => false,
],
'INTROSPECTION_TOKEN' => [
Expand Down
32 changes: 2 additions & 30 deletions tests/Integration/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,38 +139,10 @@ public function testDefaultValuesForEnvContent(): void {
NEXT_PUBLIC_GRAPHQL_ENDPOINT=/test_endpoint
# The WordPress Uploads directory path
# NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=wp-content/uploads
# 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_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 = '
# 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';
# NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=/wp-json';

$this->assertSame( $expectedContent, $content );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/RestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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\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() . "\n\n# Token used for authenticating GraphQL introspection queries\nINTROSPECTION_TOKEN=" . IntrospectionToken::get_token();
$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\nNEXT_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() . "\n\n# Token used for authenticating GraphQL introspection queries\nINTROSPECTION_TOKEN=" . IntrospectionToken::get_token();

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

Expand Down

0 comments on commit 546b05d

Please sign in to comment.