diff --git a/lib/api/compiler/class-beans-compiler.php b/lib/api/compiler/class-beans-compiler.php index 50d47c5a..dba7dccc 100644 --- a/lib/api/compiler/class-beans-compiler.php +++ b/lib/api/compiler/class-beans-compiler.php @@ -431,7 +431,21 @@ public function get_internal_content() { // Replace URL with path. $fragment = beans_url_to_path( $fragment ); - // Stop here if it isn't a valid file. + // Fix path on some Windows and Ubuntu systems. + // @ticket 332. + if ( ! file_exists( $fragment ) || 0 === @filesize( $fragment ) ) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- Valid use case. + + if ( false !== strpos( $fragment, '/wp-' ) ) { + $fragment = beans_sanitize_path( '.' . $fragment ); + } + } + + // Account for edge cases not covered before. + if ( ! file_exists( $fragment ) || 0 === @filesize( $fragment ) ) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- Valid use case. + $fragment = $this->resolve_fragment_location_by_url_parse( $fragment ); + } + + // Stop here if it still isn't a valid file. if ( ! file_exists( $fragment ) || 0 === @filesize( $fragment ) ) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- Valid use case. return false; } @@ -441,6 +455,20 @@ public function get_internal_content() { return $GLOBALS['wp_filesystem']->get_contents( $fragment ); } + /** + * Resolve an edge case asset location by running through Beans's URL parsing. + * + * @since 1.6.0 + * @param string $fragment Unresolved asset fragment path. + * + * @return string The fragment path as modified by URL parsing. + */ + public function resolve_fragment_location_by_url_parse( $fragment ) { + $fragment = beans_path_to_url( $fragment ); + + return beans_url_to_path( $fragment ); + } + /** * Get external file content. * diff --git a/tests/phpunit/unit/api/compiler/beans-compiler/combineFragments.php b/tests/phpunit/unit/api/compiler/beans-compiler/combineFragments.php index edf2fde1..d7487992 100644 --- a/tests/phpunit/unit/api/compiler/beans-compiler/combineFragments.php +++ b/tests/phpunit/unit/api/compiler/beans-compiler/combineFragments.php @@ -94,6 +94,9 @@ public function test_should_return_empty_string_when_fragment_does_not_exist() { Monkey\Functions\when( 'beans_url_to_path' )->returnArg(); Monkey\Functions\when( 'wp_remote_get' )->justReturn(); Monkey\Functions\when( 'is_wp_error' )->justReturn( true ); + Monkey\Functions\when( 'is_main_site' )->justReturn(); + Monkey\Functions\when( 'get_blog_details' )->justReturn(); + Monkey\Functions\when( 'get_current_blog_id' )->justReturn(); // Run the test. $compiler->combine_fragments();