Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Filter to force compile #39

Merged
merged 1 commit into from

4 participants

@bradyvercher

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.

@sanchothefat sanchothefat merged commit 8c68692 into sanchothefat:master
@sanchothefat
Owner

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

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.

@amandabourbois

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

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' );
@amandabourbois

That helps a lot, thanks!

@sanchothefat

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
  • "Including your less vars via the hooks described on the readme should cause it to recompile as well."

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 4, 2013
  1. @bradyvercher
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  wp-less.php
View
2  wp-less.php
@@ -194,7 +194,7 @@ public function parse_stylesheet( $src, $handle ) {
// allow devs to mess around with the less object configuration
do_action_ref_array( 'lessc', array( &$less ) );
- $less_cache = $less->cachedCompile( $cache[ 'less' ] );
+ $less_cache = $less->cachedCompile( $cache[ 'less' ], apply_filters( 'less_force_compile', false ) );
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 ) ) );
Something went wrong with that request. Please try again.