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 7 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
44 changes: 37 additions & 7 deletions plugins/wpe-headless/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,58 @@ function wpe_headless_content_replacement( $content ) {
return str_replace( 'href="//', 'href="/', $content );
}

add_filter( 'the_content', 'wpe_headless_content_media_replacement' );
add_filter( 'the_content', 'wpe_headless_image_source_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() ) {
function wpe_headless_image_source_replacement( $content ) {
if ( ! wpe_headless_is_image_source_replacement_enabled() ) {
return $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_image_source_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_image_source_srcset_replacement( $sources ) {
if ( ! wpe_headless_is_image_source_replacement_enabled() ) {
return $sources;
}

$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
13 changes: 10 additions & 3 deletions plugins/wpe-headless/includes/settings/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,10 @@ function wpe_headless_display_frontend_uri_field() {
* @return void
*/
function wpe_headless_display_enable_disable_fields() {
$disable_theme = wpe_headless_is_themes_disabled();
$enable_rewrites = wpe_headless_is_rewrites_enabled();
$enable_redirects = wpe_headless_is_redirects_enabled();
$disable_theme = wpe_headless_is_themes_disabled();
$enable_rewrites = wpe_headless_is_rewrites_enabled();
$enable_redirects = wpe_headless_is_redirects_enabled();
$enable_image_source = wpe_headless_is_image_source_replacement_enabled();

?>
<fieldset>
Expand All @@ -286,6 +287,12 @@ function wpe_headless_display_enable_disable_fields() {
<input type="checkbox" id="enable_redirects" name="wpe_headless[enable_redirects]" value="1" <?php checked( $enable_redirects ); ?> />
<?php esc_html_e( 'Enable public route redirects', 'wpe-headless' ); ?>
</label>
<br />

<label for="enable_image_source">
<input type="checkbox" id="enable_image_source" name="wpe_headless[enable_image_source]" value="1" <?php checked( $enable_image_source ); ?> />
<?php esc_html_e( 'Enable media images served from WP endpoint', 'wpe-headless' ); ?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be more clear for the checkbox label? cc @trevanhetzel

Suggested change
<?php esc_html_e( 'Enable media images served from WP endpoint', 'wpe-headless' ); ?>
<?php esc_html_e( 'Use the WordPress domain for media URLs in post content', 'wpe-headless' ); ?>

</label>
</fieldset>
<?php
}
Expand Down
10 changes: 10 additions & 0 deletions plugins/wpe-headless/includes/settings/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ function wpe_headless_is_events_enabled() {
return '1' === wpe_headless_get_setting( 'events_enabled' );
}

/**
* Determine if sourcing images from WP domain is enabled.
*
* @return bool True if image sources from WP are enabled, false if else.
*/
function wpe_headless_is_image_source_replacement_enabled() {
return '1' === wpe_headless_get_setting( 'enable_image_source' );
}


/**
* Get the secret key setting.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,60 @@ 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_image_source_replacement() replaces the frontend_uri value when image replacement is enabled.
*/
public function test_wpe_headless_content_media_replacement_filters_content_when_content_replacement_enabled() {
public function test_wpe_headless_image_source_replacement_filters_content_when_image_replacement_enabled() {
wpe_headless_update_setting( 'frontend_uri', 'http://foo.co' );
add_filter( 'wpe_headless_domain_replacement_enabled', '__return_true' );
wpe_headless_update_setting( 'enable_image_source', '1' );
# 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">' ) );
$this->assertSame( '<img src="http://foo.com/image.png">', wpe_headless_image_source_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_image_source_replacement( '<img src="http://foo.co/image.png">' ) );
$this->assertSame( '<img src="http://example.org/image.png">', wpe_headless_image_source_replacement( '<img src="/image.png">' ) );
wpe_headless_update_setting( 'frontend_uri', null );
remove_filter( 'wpe_headless_domain_replacement_enabled', '__return_true' );
wpe_headless_update_setting( 'enable_image_source', '0' );
}

/**
* Tests wpe_headless_image_source_srcset_replacement() replaces the frontend_uri value when image replacement is enabled.
*/
public function test_wpe_headless_image_source_srcset_replacement_filters_content_when_image_replacement_enabled() {
wpe_headless_update_setting( 'frontend_uri', 'http://foo.co' );
wpe_headless_update_setting( 'enable_image_source', '1' );
# 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_image_source_srcset_replacement( $sources ) );
wpe_headless_update_setting( 'frontend_uri', null );
wpe_headless_update_setting( 'enable_image_source', '0' );
}

/**
* Tests wpe_headless_image_source_replacement() doesn't replace when image replacement is not enabled.
*/
public function test_wpe_headless_image_source_replacement_filters_content_when_image_replacement_not_enabled() {
wpe_headless_update_setting( 'enable_image_source', '0' );
$this->assertSame( '<img src="/image.png">', wpe_headless_image_source_replacement( '<img src="/image.png">' ) );
}

/**
* Tests wpe_headless_image_source_srcset_replacement() doesn't replace when image replacement is not enabled.
*/
public function test_wpe_headless_image_source_srcset_replacement_filters_content_when_image_replacement_not_enabled() {
wpe_headless_update_setting( 'enable_image_source', '0' );
$sources = array (
100 => array('url' => '/wp-content/uploads/image.jpg'),
);
$this->assertSame( $sources, wpe_headless_image_source_srcset_replacement( $sources ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ public function wpe_headless_is_themes_disabled_will_return_true_if_disabled() {

$this->assertTrue( wpe_headless_is_themes_disabled() );
}

/** @test */
public function wpe_headless_is_image_source_replacement_enabled_will_return_true_if_disabled() {
delete_option( 'wpe_headless' );

$this->assertFalse( wpe_headless_is_image_source_replacement_enabled() );

update_option( 'wpe_headless', array( 'enable_image_source' => '1' ) );

$this->assertTrue( wpe_headless_is_image_source_replacement_enabled() );
}
}