From e62e0e99a6e51a83fdbb92f2b6fb54ab7192058f Mon Sep 17 00:00:00 2001 From: Christoph Herr Date: Sun, 9 Sep 2018 21:28:22 -0400 Subject: [PATCH 1/2] Fixed Beans Compiler path handling on some systems. --- lib/api/compiler/class-beans-compiler.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/api/compiler/class-beans-compiler.php b/lib/api/compiler/class-beans-compiler.php index 50d47c5a..0fc9cd51 100644 --- a/lib/api/compiler/class-beans-compiler.php +++ b/lib/api/compiler/class-beans-compiler.php @@ -431,7 +431,16 @@ 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 ); + } + } + + // 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; } From 0b10341c4c9d995ab7f025da042fa530edc27d40 Mon Sep 17 00:00:00 2001 From: Christoph Herr Date: Mon, 10 Sep 2018 12:05:30 -0400 Subject: [PATCH 2/2] Added handling for edge cases. --- lib/api/compiler/class-beans-compiler.php | 23 +++++++++++++++++-- .../beans-compiler/combineFragments.php | 3 +++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/api/compiler/class-beans-compiler.php b/lib/api/compiler/class-beans-compiler.php index 0fc9cd51..dba7dccc 100644 --- a/lib/api/compiler/class-beans-compiler.php +++ b/lib/api/compiler/class-beans-compiler.php @@ -432,14 +432,19 @@ public function get_internal_content() { $fragment = beans_url_to_path( $fragment ); // Fix path on some Windows and Ubuntu systems. - // @ticket 332 + // @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 ); + $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; @@ -450,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();