diff --git a/docs/previews/README.md b/docs/previews/README.md index 0034a1464..fb59ef4c9 100644 --- a/docs/previews/README.md +++ b/docs/previews/README.md @@ -19,7 +19,7 @@ Checkout the [example project](/examples/preview) to see how it works. ## WPE Headless Plugin -In order to enable previews in WordPress, you'll first need to install the [wpe-headless plugin](https://github.com/wpengine/headless-framework/releases/download/v0.0.1-alpha.1/wpe-headless-plugin.zip). You also need to install [WPGraphQL](https://wordpress.org/plugins/wp-graphql/). +In order to enable previews in WordPress, you'll first need to install the [wpe-headless plugin](https://wp-product-info.wpesvc.net/v1/plugins/wpe-headless?download). You also need to install [WPGraphQL](https://wordpress.org/plugins/wp-graphql/). The plugin enables an OAuth flow for users to authenticate with WordPress and receive an access token which is used for subsequent API calls (i.e. GQL/REST). diff --git a/plugins/wpe-headless/includes/replacement/callbacks.php b/plugins/wpe-headless/includes/replacement/callbacks.php index a113599b4..0760c0600 100644 --- a/plugins/wpe-headless/includes/replacement/callbacks.php +++ b/plugins/wpe-headless/includes/replacement/callbacks.php @@ -35,6 +35,30 @@ function wpe_headless_content_replacement( $content ) { return str_replace( 'href="//', 'href="/', $content ); } +add_filter( 'the_content', 'wpe_headless_content_media_replacement' ); +/** + * Callback for WordPress 'the_content' filter to replace paths to media. + * + * @param string $content The post content. + * + * @return string The post content. + * @todo Needs work... + */ +function wpe_headless_content_media_replacement( $content ) { + if ( ! wpe_headless_domain_replacement_enabled() ) { + return $content; + } + + $frontend_uri = wpe_headless_get_setting( 'frontend_uri' ); + $site_url = site_url(); + + if ( ! $frontend_uri ) { + $frontend_uri = '/'; + } + + return str_replace( "src=\"{$frontend_uri}/", "src=\"{$site_url}/", $content ); +} + add_filter( 'preview_post_link', 'wpe_headless_post_preview_link', 10, 2 ); /** * Callback for WordPress 'preview_post_link' filter. diff --git a/plugins/wpe-headless/tests/integration/replacement/test-replacement-callbacks.php b/plugins/wpe-headless/tests/integration/replacement/test-replacement-callbacks.php index 97c77ea4e..089da2c50 100644 --- a/plugins/wpe-headless/tests/integration/replacement/test-replacement-callbacks.php +++ b/plugins/wpe-headless/tests/integration/replacement/test-replacement-callbacks.php @@ -57,6 +57,20 @@ public function test_wpe_headless_content_replacement_filters_content_when_conte remove_filter( 'wpe_headless_domain_replacement_enabled', '__return_true' ); } + /** + * Tests wpe_headless_content_media_replacement() replaces the frontend_uri value when content replacement is enabled. + */ + public function test_wpe_headless_content_media_replacement_filters_content_when_content_replacement_enabled() { + wpe_headless_update_setting( 'frontend_uri', 'http://foo.co' ); + add_filter( 'wpe_headless_domain_replacement_enabled', '__return_true' ); + # Do not replace partial domain match. + $this->assertSame( '', wpe_headless_content_media_replacement( '' ) ); + # Do replace exact domain match + $this->assertSame( '', wpe_headless_content_media_replacement( '' ) ); + wpe_headless_update_setting( 'frontend_uri', null ); + remove_filter( 'wpe_headless_domain_replacement_enabled', '__return_true' ); + } + /** * Tests get_permalink() returns the original value when content replacement is not enabled. *