Comments (6)
Reproduce the problem
Like explain in the issue itself,
We can create a page with the following HTML:
<div class="my-test-background-1"></div>
Creating a test.php
and put it in the rocket-test-data
folder:
<?php
header('Content-Type: text/css');
echo '.my-test-background-1 {
width: 500px;
height: 500px;
margin-left: auto;
margin-right: auto;
background-image: url("http://wp-rocket.esy.es/wp-content/uploads/2020/10/rose-165819_1920-1024x678.jpg");
}';
And adding this to functions.php
:
wp_enqueue_block_style(
'test',
array(
'handle' => 'my-test-background-1',
'src' => get_home_url( null,'wp-content/rocket-test-data/styles/test.php' ),
'path' => get_home_url( null,'wp-content/rocket-test-data/styles/test.php' )
)
);
We get the console error as mentioned.
Identify the root cause
The issue is that when we are extracting the content of the file, we keep the whole php content. Which means we are keeping the echo
element.
Scope a solution
To solve the issue, we could modify the function
in order to check the file extension and if this is a php file, we could load the php to get its content instead of getting the file content. protected function fetch_content( string $path ) {
if ( ! wp_http_validate_url( $path ) ) {
$file_extension = pathinfo($path, PATHINFO_EXTENSION);
if (strtolower($file_extension) !== 'php') {
return $this->filesystem->get_contents($path);
}
}
return wp_remote_retrieve_body( wp_remote_get( $path ) );
}
would work.
Development steps:
Effort estimation:
XS
Is a refactor needed in that part of the codebase?
No
from wp-rocket.
@Miraeld This solution would work fine if we had only echo
to worry about but if we have cases like:
$script = '.my-test-background-1 {
width: 500px;
height: 500px;
margin-left: auto;
margin-right: auto;
background-image: url("http://wp-rocket.esy.es/wp-content/uploads/2020/10/rose-165819_1920-1024x678.jpg");
}';
echo $script;
or
print '.my-test-background-1 {
width: 500px;
height: 500px;
margin-left: auto;
margin-right: auto;
background-image: url("http://wp-rocket.esy.es/wp-content/uploads/2020/10/rose-165819_1920-1024x678.jpg");
}';
to say the least, this kind of construct will trigger yet another console error.
could be an edge case though maybe @piotrbak
from wp-rocket.
I thought about that, but I wan't able to think of a global solution for this kind of thing except playing with regex ... If you have an idea in mind, please share it
from wp-rocket.
@wp-media/engineering-plugin-team I see here that we will get content here when the css url does not contain a host, so for this maybe we can do a check for a php ext and do a request with a constructed url to get the file buffer instead of the content.
from wp-rocket.
Based on @jeawhanlee comment, here is what we could do:
In the function
we can add a condition to check if this is a php file, and if this is, then we return the content of the loaded php:
protected function fetch_content( string $path ) {
if ( ! wp_http_validate_url( $path ) ) {
$file_extension = pathinfo($path, PATHINFO_EXTENSION);
if (strtolower($file_extension) !== 'php') {
return $this->filesystem->get_contents($path);
}
}
return wp_remote_retrieve_body( wp_remote_get( $path ) );
}
This would work.
I have updated the grooming accordingly
from wp-rocket.
Looks good to me
from wp-rocket.
Related Issues (20)
- Built-in test maintenance: skipped tests, warnings
- Make the link fetching process asynchronous HOT 9
- Improve license switching process to make it more user-friendly
- Improvements to homepage fetching process HOT 1
- Success Message is not displayed after countdown when RUCSS is enabled until refresh HOT 1
- Form Autofill not working in wordpress when WPROCKET enable. HOT 1
- Autodelete wp-rocket-debug.log.html if it grows bigger than a specifc size
- Increase PHPStan to rule level to 1
- Warmup URLs in database not matching those in WPR debug
- Mobile manual visit isnot added to ATF table when meta viewport header is missing HOT 3
- Introduce a safe function for apply_filter
- Add section to `lcp_atf_elements` HOT 7
- Image Optimization Tab Update HOT 2
- RUCSS counter/success notices are not working correctly in certain scenario
- Preload tag has incorrect markup if LCP element has at least 2 background-images among pseudo-elements HOT 4
- Disable cache clearing when product category term is updated not working (WC REST API Request)
- Enhance the way we detect the cached page for lcp beacon script HOT 1
- Correctly handle image types when preloading LCP images HOT 16
- 3.16: Hebrew letters break preload URL HOT 4
- Improve integration tests using function mock instead of HTTP short-circuit
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wp-rocket.