Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/filter media src #97

Merged
merged 8 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions plugins/wpe-headless/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function wpe_headless_content_replacement( $content ) {
* @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() ) {
Expand All @@ -52,11 +51,42 @@ function wpe_headless_content_media_replacement( $content ) {
$frontend_uri = wpe_headless_get_setting( 'frontend_uri' );
$site_url = site_url();

if ( ! $frontend_uri ) {
$frontend_uri = '/';
// For urls with no domain or the frontend domain, replace with the wp site_url.
$patterns = array(
"#src=\"{$frontend_uri}/#",
'#src="/#',
);
return preg_replace( $patterns, "src=\"{$site_url}/", $content );
}

add_filter( 'wp_calculate_image_srcset', 'wpe_headless_content_media_srcset_replacement' );
/**
* Callback for WordPress 'the_content' filter to replace paths to media.
*
* @param array $sources One or more arrays of source data to include in the 'srcset'.
*
* @return string One or more arrays of source data.
*/
function wpe_headless_content_media_srcset_replacement( $sources ) {
if ( ! wpe_headless_domain_replacement_enabled() ) {
return $content;
markkelnar marked this conversation as resolved.
Show resolved Hide resolved
}

$frontend_uri = wpe_headless_get_setting( 'frontend_uri' );
$site_url = site_url();

if ( is_array( $sources ) ) {
// For urls with no domain or the frontend domain, replace with the wp site_url.
$patterns = array(
"#^{$frontend_uri}/#",
'#^/#',
);
foreach ( $sources as $width => $source ) {
$sources[ $width ]['url'] = preg_replace( $patterns, "$site_url/", $sources[ $width ]['url'] );
}
}

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

add_filter( 'preview_post_link', 'wpe_headless_post_preview_link', 10, 2 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,39 @@ public function test_wpe_headless_content_replacement_filters_content_when_conte
}

/**
* Tests wpe_headless_content_media_replacement() replaces the frontend_uri value when content replacement is enabled.
* Tests wpe_headless_content_media_src_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() {
public function test_wpe_headless_content_media_src_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">' ) );
$this->assertSame( '<img src="http://example.org/image.png">', wpe_headless_content_media_replacement( '<img src="/image.png">' ) );
wpe_headless_update_setting( 'frontend_uri', null );
remove_filter( 'wpe_headless_domain_replacement_enabled', '__return_true' );
}

/**
* Tests wpe_headless_content_media_srcset_replacement() replaces the frontend_uri value when content replacement is enabled.
*/
public function test_wpe_headless_content_media_srcset_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.
# Do replace exact domain match
$sources = array (
100 => array('url' => 'http://foo.com/image100.png'),
300 => array('url' => '/wp-content/uploads/image300.jpg'),
400 => array('url' => 'http://foo.co/image400.png'),
);
$expected = array (
100 => array('url' => 'http://foo.com/image100.png'),
300 => array('url' => 'http://example.org/wp-content/uploads/image300.jpg'),
400 => array('url' => 'http://example.org/image400.png'),
);
$this->assertSame( $expected, wpe_headless_content_media_srcset_replacement( $sources ) );
wpe_headless_update_setting( 'frontend_uri', null );
remove_filter( 'wpe_headless_domain_replacement_enabled', '__return_true' );
}
Expand Down