Skip to content

Commit

Permalink
Add callback for content src= replacement (#90)
Browse files Browse the repository at this point in the history
* feat: Add callback for content src= replacement

* Match full domain path in src attr
  • Loading branch information
markkelnar authored Jan 26, 2021
1 parent b98ef60 commit 125fb5f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/previews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
24 changes: 24 additions & 0 deletions plugins/wpe-headless/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<img src="http://foo.com/image.png">', wpe_headless_content_media_replacement( '<img src="http://foo.com/image.png">' ) );
# Do replace exact domain match
$this->assertSame( '<img src="http://example.org/image.png">', wpe_headless_content_media_replacement( '<img src="http://foo.co/image.png">' ) );
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.
*
Expand Down

0 comments on commit 125fb5f

Please sign in to comment.