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

Filter to force compile #39

Closed bradyvercher closed 11 years ago

bradyvercher commented 11 years ago

Changes made to functions or variables registered in PHP, such as when working with the Theme Customizer, don't cause LESS files to be recompiled, so the changes aren't visible in the Customizer preview or the front-end unless a LESS file is updated manually.

A second parameter is available in the lessc::cachedCompile() method for forcing the files to be rebuilt, so this filter allows it to be updated when necessary.

roborourke commented 11 years ago

Thanks Brady, that's probably the simplest way to force compiling. I was imagining something like accessing the lessc class instance directly to do that but in wordpress terms a lot easier to add/remove a filter when necessary.

bradyvercher commented 11 years ago

Ahh, I see. That wasn't immediately obvious, but makes sense. I was thinking of just toggling the filter when necessary using __return_true or __return_false. Thanks for the merge.

anrussell commented 11 years ago

Hello,

Please pardon my ignorance, but I'm confused as how to use this.

I'm using ACF Lite for my theme options (which I'll need to update soon, I know), and I can hook into acf_save_post to do something when the options are saved, but I'm not sure how exactly to use this filter in there.

function updateCss() {
    //???
}
add_action('acf_save_post', 'updateCss', 20);

I'm a bit novice, so please let me know if I'm way off base.

bradyvercher commented 11 years ago

Hi @amandabourbois. This filter only works when the plugin checks the LESS style sheet for changes, which is just before they're output to the page, so it won't actually do anything otherwise. In your case, what you would need to do is set a flag when your options are saved so you can force the style sheets to be recompiled the next time they're requested.

Here's how I envisioned it working:

function prefix_acf_save_post() {
    // Set a flag to recompile LESS on the next front end request.
    update_option( 'prefix_force_recompile', 'yes' );
}
add_action( 'acf_save_post', 'prefix_acf_save_post' );

Then in a hook on the frontend before the style sheet is output, you would check the flag, set the filter if needed, then reset the flag.

function prefix_check_acf_save_flag() {
    if ( 'yes' == get_option( 'prefix_force_recompile' ) ) {
        // Force the LESS style sheets to be recompiled.
        add_filter( 'less_force_compile', '__return_true' );

        // Reset the flag so the style sheets won't be recompiled on every request.
        delete_option( 'prefix_force_recompile' );
    }
}
add_action( 'wp_enqueue_script', 'prefix_check_acf_save_flag' );
anrussell commented 11 years ago

That helps a lot, thanks!

roborourke commented 11 years ago

Including your less vars via the hooks described on the readme should cause it to recompile as well. Alternatively you can compile any output on the fly using the lessc class directly if you need to.

usaphp commented 11 years ago

@sanchothefat that does not work for me - when I change a variable value inside of that php hook - it does not recompile, but when I change something in .less file - that triggers a recompile. Is there anyway to forse it to recompile if a variable is changed in the hook? (I am talking about the hook from readme file)