Skip to content

Commit

Permalink
Fix Image URL Localization URI (#720)
Browse files Browse the repository at this point in the history
* fix #714

* fix

* fix

* fix

* fix

* lint

---------

Co-authored-by: Madhu Dollu <[email protected]>
  • Loading branch information
shimotmk and madhusudhand authored Sep 26, 2024
1 parent ffa9186 commit 55a60de
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 4 additions & 1 deletion includes/create-theme/theme-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public static function get_media_absolute_urls_from_template( $template ) {
public static function make_relative_media_url( $absolute_url ) {
if ( ! empty( $absolute_url ) && CBT_Theme_Utils::is_absolute_url( $absolute_url ) ) {
$folder_path = self::get_media_folder_path_from_url( $absolute_url );
return '<?php echo esc_url( get_stylesheet_directory_uri() ); ?>' . $folder_path . basename( $absolute_url );
if ( is_child_theme() ) {
return '<?php echo esc_url( get_stylesheet_directory_uri() ); ?>' . $folder_path . basename( $absolute_url );
}
return '<?php echo esc_url( get_template_directory_uri() ); ?>' . $folder_path . basename( $absolute_url );
}
return $absolute_url;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/test-theme-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function test_make_images_block_local() {

// The image should be replaced with a relative URL
$this->assertStringNotContainsString( 'http://example.com/image.jpg', $new_template->content );
$this->assertStringContainsString( 'get_stylesheet_directory_uri', $new_template->content );
$this->assertStringContainsString( 'get_template_directory_uri', $new_template->content );
$this->assertStringContainsString( '/assets/images', $new_template->content );

}
Expand All @@ -35,7 +35,7 @@ public function test_make_cover_block_local() {

// The image should be replaced with a relative URL
$this->assertStringNotContainsString( 'http://example.com/image.jpg', $new_template->content );
$this->assertStringContainsString( 'get_stylesheet_directory_uri', $new_template->content );
$this->assertStringContainsString( 'get_template_directory_uri', $new_template->content );
$this->assertStringContainsString( '/assets/images', $new_template->content );
}

Expand All @@ -56,7 +56,7 @@ public function test_template_with_media_correctly_prepared() {
$this->assertContains( 'http://example.com/image.jpg', $new_template->media );

// The pattern is correctly encoded
$this->assertStringContainsString( '<img src="<?php echo esc_url( get_stylesheet_directory_uri() ); ?>/assets/images/image.jpg"', $new_template->pattern );
$this->assertStringContainsString( '<img src="<?php echo esc_url( get_template_directory_uri() ); ?>/assets/images/image.jpg"', $new_template->pattern );

}

Expand All @@ -77,7 +77,7 @@ public function test_make_group_block_local() {
$this->assertContains( 'http://example.com/image.jpg', $new_template->media );

// The pattern is correctly encoded
$this->assertStringContainsString( '{"backgroundImage":{"url":"<?php echo esc_url( get_stylesheet_directory_uri() ); ?>/assets/images/image.jpg"', $new_template->pattern );
$this->assertStringContainsString( '{"backgroundImage":{"url":"<?php echo esc_url( get_template_directory_uri() ); ?>/assets/images/image.jpg"', $new_template->pattern );

}
}

0 comments on commit 55a60de

Please sign in to comment.