diff --git a/plugins/wpe-headless/includes/replacement/callbacks.php b/plugins/wpe-headless/includes/replacement/callbacks.php index 0760c0600..a40e3fa0e 100644 --- a/plugins/wpe-headless/includes/replacement/callbacks.php +++ b/plugins/wpe-headless/includes/replacement/callbacks.php @@ -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 ); diff --git a/plugins/wpe-headless/includes/settings/callbacks.php b/plugins/wpe-headless/includes/settings/callbacks.php index ea593c147..cebb90fa6 100644 --- a/plugins/wpe-headless/includes/settings/callbacks.php +++ b/plugins/wpe-headless/includes/settings/callbacks.php @@ -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(); ?>
assertSame( '', wpe_headless_content_media_replacement( '' ) ); + $this->assertSame( '', wpe_headless_image_source_replacement( '' ) ); # Do replace exact domain match - $this->assertSame( '', wpe_headless_content_media_replacement( '' ) ); + $this->assertSame( '', wpe_headless_image_source_replacement( '' ) ); + $this->assertSame( '', wpe_headless_image_source_replacement( '' ) ); 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( '', wpe_headless_image_source_replacement( '' ) ); + } + + /** + * 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 ) ); } /** diff --git a/plugins/wpe-headless/tests/integration/settings/test-functions.php b/plugins/wpe-headless/tests/integration/settings/test-functions.php index ce9cf7081..86a7ea50d 100644 --- a/plugins/wpe-headless/tests/integration/settings/test-functions.php +++ b/plugins/wpe-headless/tests/integration/settings/test-functions.php @@ -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() ); + } }