Skip to content

Commit 33aa15d

Browse files
committed
feat(wordpress): allow to specify url using WP_HOME
1 parent ea66ccd commit 33aa15d

File tree

9 files changed

+42
-11
lines changed

9 files changed

+42
-11
lines changed

examples/wordpress/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ services:
2222
ports:
2323
- '8000:80'
2424
environment:
25+
WP_HOME: http://localhost:3001
2526
WP_DEFAULT_THEME: smooth-js
2627
WORDPRESS_DB_HOST: mysql:3306
2728
WORDPRESS_DB_USER: wordpress

examples/wordpress/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"private": true,
3+
"scripts": {
4+
"dev": "WP_HOME=http://localhost:3001 smooth dev"
5+
},
36
"dependencies": {
47
"graphql-tag": "^2.10.0",
58
"react": "^16.7.0",

examples/wordpress/smooth.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path'
22

33
module.exports = {
4-
baseUrl: 'http://localhost:3000',
4+
server: { port: 3001 },
55
plugins: [
66
{
77
resolve: require.resolve('smooth-backend-wordpress'),

packages/smooth-backend-wordpress/src/acf/resolvers/createDefaultResolvers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ const handlers = {
1010
return object => formatDateTime(object.acf[name])
1111
},
1212
link({ name, list }, helpers, state) {
13-
const { baseUrl } = state.options
13+
const { homeUrl } = state.options
1414
if (list) return null
1515
return object => {
1616
const link = object.acf[name]
1717
if (!link) return null
1818
return {
1919
...link,
20-
url: toRelativeUrl(baseUrl, link.url),
20+
url: toRelativeUrl(homeUrl, link.url),
2121
}
2222
}
2323
},

packages/smooth-backend-wordpress/src/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ import * as acf from './acf'
22
import * as cpt from './cpt'
33
import * as wordpress from './wordpress'
44

5+
export function resolveOptions(options) {
6+
if (!options.baseUrl) {
7+
throw new Error('"baseUrl" is required')
8+
}
9+
10+
if (!options.homeUrl && !process.env.WP_HOME) {
11+
throw new Error('"homeUrl" is required')
12+
}
13+
14+
if (!options.homeUrl && process.env.WP_HOME) {
15+
options.homeUrl = process.env.WP_HOME
16+
}
17+
18+
return options
19+
}
20+
521
export function onCreateSchemaDefinition(params) {
622
acf.onCreateSchemaDefinition(params)
723
}

packages/smooth-backend-wordpress/src/wordpress/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ const themeDir = path.join(__dirname, 'theme')
88
const pluginTpl = fs.readFileSync(path.join(pluginDir, 'index.php'), 'utf-8')
99
const themeTpl = fs.readFileSync(path.join(themeDir, 'index.php'), 'utf-8')
1010

11-
export async function onBuild({ options, config }) {
11+
export async function onBuild({ options }) {
1212
const pluginDistDir = await getPluginDir(options.basePath)
13-
const pluginContent = pluginTpl.replace(/%BASE_URL%/g, config.baseUrl)
13+
const pluginContent = pluginTpl.replace(/%BASE_URL%/g, options.homeUrl)
1414
const pluginPath = path.join(pluginDistDir, 'index.php')
1515
await writeFile(pluginPath, pluginContent)
1616

1717
const themeDistDir = await getThemeDir(options.basePath)
18-
const themeContent = themeTpl.replace(/%BASE_URL%/g, config.baseUrl)
18+
const themeContent = themeTpl.replace(/%BASE_URL%/g, options.homeUrl)
1919
const themePath = path.join(themeDistDir, 'index.php')
2020
await copyDir(themeDir, themeDistDir, {
2121
filter: filename => !/index\.php/.test(filename),

packages/smooth-backend-wordpress/src/wordpress/plugin/index.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Plugin Name: Smooth CMS
4+
* Plugin Name: Smooth.js
55
* Version: 1.0.0
66
* Author: Smooth Code
77
* Author URI: http://www.smooth-code.com
@@ -62,19 +62,28 @@ function smooth_create_post_type() {
6262
}
6363
}
6464

65+
function smooth_set_home_url() {
66+
$wp_home = getenv('WP_HOME');
67+
$home_url = $wp_home ? $wp_home : '%HOME_URL%';
68+
69+
update_option( 'home', $wp_home );
70+
}
71+
6572
add_action('init', 'smooth_create_post_type');
73+
add_action('init', 'smooth_set_home_url');
6674

6775

6876
// Modify preview link
6977

7078
add_filter('preview_post_link', function ($link) {
7179
$post = get_post();
7280
$path = parse_url($link, PHP_URL_PATH);
73-
$link = '%BASE_URL%' . $path . '?id=' . $post->ID . '&preview=1';
81+
$wp_home = getenv('WP_HOME');
82+
$home_url = $wp_home ? $wp_home : '%HOME_URL%';
83+
$link = $home_url . $path . '?id=' . $post->ID . '&preview=1';
7484
return $link;
7585
});
7686

77-
7887
// Recursively add ACF into post objects
7988

8089
add_filter('acf/rest_api/page/get_fields', function($data) {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
22

33
$requestUri = $_SERVER['REQUEST_URI'];
4-
header("Location: %BASE_URL%{$requestUri}");
4+
$wp_home = getenv('WP_HOME');
5+
$home_url = $wp_home ? $wp_home : '%HOME_URL%';
6+
header("Location: {$base_url}{$requestUri}");
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/*
2-
Theme Name: Smooth CMS
2+
Theme Name: Smooth.js
33
*/

0 commit comments

Comments
 (0)