Skip to content

Commit

Permalink
feat: Add callback for content src= replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
markkelnar committed Jan 25, 2021
1 parent d761371 commit 861b10e
Show file tree
Hide file tree
Showing 3 changed files with 38 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
26 changes: 26 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,32 @@ 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 = '/';
}

$content = str_replace( "src=\"{$frontend_uri}", "src=\"{$site_url}", $content );

return str_replace( 'src="//', 'src="/', $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,17 @@ 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.com' );
add_filter( 'wpe_headless_domain_replacement_enabled', '__return_true' );
$this->assertSame( '<img src="http://example.org">', wpe_headless_content_media_replacement( '<img src="http://foo.com">' ) );
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 861b10e

Please sign in to comment.