Code Monkey home page Code Monkey logo

Comments (6)

Miraeld avatar Miraeld commented on August 25, 2024

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

protected function fetch_content( string $path ) {
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.

jeawhanlee avatar jeawhanlee commented on August 25, 2024

@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.

Miraeld avatar Miraeld commented on August 25, 2024

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.

jeawhanlee avatar jeawhanlee commented on August 25, 2024

@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.

Miraeld avatar Miraeld commented on August 25, 2024

Based on @jeawhanlee comment, here is what we could do:

In the function

protected function fetch_content( string $path ) {

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.

Tabrisrp avatar Tabrisrp commented on August 25, 2024

Looks good to me

from wp-rocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.