roborourke / wp-less

Provides a LESS compiler compatible with wp_enqueue_style() for rapid CSS development in themes and plugins.
MIT License
217 stars 55 forks source link

LESS variable updating in cache but not in CSS #41

Closed rahularyan closed 11 years ago

rahularyan commented 11 years ago

I am including wp-less in my theme, But I am stucked. I am changing variable from get_option, like:

$vars['headerBG'] = get_option('color_header');

And I am calling this variable in theme.less, Like:

header{
    background: @headerBG;
}

Everything work fine until here, Compile result is :

header{
    background: #333333;
}

now if I change value of get_option('color_header') then as usual cache file theme.less.cache and theme.less.css get updated. Variable @headerBGs new value was updated in cache files but not in css files. NOTE: CSS file is recompiling but color is not updating.

That is my problem! please help me to fix it.

rahularyan commented 11 years ago

Solved this:

            if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) || $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] ) {
                file_put_contents( $cache_path, serialize( array( 'vars' => $this->vars, 'less' => $less_cache ) ) );
                file_put_contents( $css_path, $less_cache[ 'compiled' ] );
            }elseif ($this->vars !== $cache[ 'vars' ]){
                $less_cache = $less->cachedCompile( $cache[ 'less' ], apply_filters( 'less_force_compile', true ) );
                file_put_contents( $cache_path, serialize( array( 'vars' => $this->vars, 'less' => $less_cache ) ) );
                file_put_contents( $css_path, $less_cache[ 'compiled' ] );
            }
rossedman commented 11 years ago

Currently having the same problem. I replaced this with your code above:

if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) || $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] || $this->vars !== $cache[ 'vars' ] ) {
    file_put_contents( $cache_path, serialize( array( 'vars' => $this->vars, 'less' => $less_cache ) ) );
    file_put_contents( $css_path, $less_cache[ 'compiled' ] );
}

Not sure if I did that correctly but I have no reload. However, the variable is changing in my cache file. I am trying to use Advanced Custom Fields with this. It works but I have to clear the cache everytime.

rossedman commented 11 years ago

I updated this code to this:

        if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) || $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] ) {
            file_put_contents( $cache_path, serialize( array( 'vars' => $this->vars, 'less' => $less_cache ) ) );
            file_put_contents( $css_path, $less_cache[ 'compiled' ] );
        } else ($this->vars != $cache[ 'vars' ]){
            $less_cache = $less->cachedCompile( $cache[ 'less' ], apply_filters( 'less_force_compile', true ) );
            file_put_contents( $cache_path, serialize( array( 'vars' => $this->vars, 'less' => $less_cache ) ) );
            file_put_contents( $css_path, $less_cache[ 'compiled' ] );
        }

The elseif statement was allowing it to not recompile. Also you had "!==" instead of "!=".

rahularyan commented 11 years ago

My code is working fine, you can see my project https://github.com/rahularyan/rabs, I will upload it tomorrow

rossedman commented 11 years ago

@rahularyan great! That is strange it did not work for me and these adjustments did. Very cool though. Just trying to help!