From 9af364e9f2a133a8c5ff9f79a9e32f87130d31a2 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Tue, 21 Jan 2014 16:47:01 +0000 Subject: [PATCH 01/40] Don't throw notices if the response is for a failed connection --- wp-less.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wp-less.php b/wp-less.php index 65887c3..633452a 100644 --- a/wp-less.php +++ b/wp-less.php @@ -120,6 +120,11 @@ public function http_request_args( $r, $url ) { if ( 0 !== strpos( $url, 'http://api.wordpress.org/plugins/update-check' ) ) return $r; // Not a plugin update request. Bail immediately. + if ( $r['response']['code'] != 200 ) { + // this is a failed request! We cant modify the results if the results timed out/failed + return $r; + } + $plugins = unserialize( $r[ 'body' ][ 'plugins' ] ); unset( $plugins->plugins[plugin_basename( __FILE__ )] ); unset( $plugins->active[ array_search( plugin_basename( __FILE__ ), $plugins->active ) ] ); From d255537989f6bfe1ee5927ee101ac6efa14bcde5 Mon Sep 17 00:00:00 2001 From: Tarendai Date: Sun, 27 Apr 2014 11:15:52 +0100 Subject: [PATCH 02/40] move the wp-less class into its own file --- wp-less.class.php | 335 ++++++++++++++++++++++++++++++++++++++++++++++ wp-less.php | 334 +-------------------------------------------- 2 files changed, 337 insertions(+), 332 deletions(-) create mode 100644 wp-less.class.php diff --git a/wp-less.class.php b/wp-less.class.php new file mode 100644 index 0000000..2711f93 --- /dev/null +++ b/wp-less.class.php @@ -0,0 +1,335 @@ +plugins[plugin_basename( __FILE__ )] ); + unset( $plugins->active[ array_search( plugin_basename( __FILE__ ), $plugins->active ) ] ); + $r[ 'body' ][ 'plugins' ] = serialize( $plugins ); + + return $r; + } + + + /** + * Lessify the stylesheet and return the href of the compiled file + * + * @param string $src Source URL of the file to be parsed + * @param string $handle An identifier for the file used to create the file name in the cache + * @return string URL of the compiled stylesheet + */ + public function parse_stylesheet( $src, $handle ) { + + // we only want to handle .less files + if ( ! preg_match( '/\.less(\.php)?$/', preg_replace( '/\?.*$/', '', $src ) ) ) + return $src; + + // get file path from $src + if ( ! strstr( $src, '?' ) ) $src .= '?'; // prevent non-existent index warning when using list() & explode() + + // Match the URL schemes between WP_CONTENT_URL and $src, + // so the str_replace further down will work + $src_scheme = parse_url( $src, PHP_URL_SCHEME ); + $wp_content_url_scheme = parse_url( WP_CONTENT_URL, PHP_URL_SCHEME ); + if ( $src_scheme != $wp_content_url_scheme ) + $src = set_url_scheme( $src, $wp_content_url_scheme ); + + list( $less_path, $query_string ) = explode( '?', str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $src ) ); + + // output css file name + $css_path = trailingslashit( $this->get_cache_dir() ) . "{$handle}.css"; + + // automatically regenerate files if source's modified time has changed or vars have changed + try { + + // initialise the parser + $less = new lessc; + + // load the cache + $cache_path = "{$css_path}.cache"; + + if ( file_exists( $cache_path ) ) + $cache = unserialize( file_get_contents( $cache_path ) ); + + // vars to pass into the compiler - default @themeurl var for image urls etc... + $this->vars[ 'themeurl' ] = '~"' . get_stylesheet_directory_uri() . '"'; + $this->vars[ 'lessurl' ] = '~"' . dirname( $src ) . '"'; + $this->vars = apply_filters( 'less_vars', $this->vars, $handle ); + + // If the cache or root path in it are invalid then regenerate + if ( empty( $cache ) || empty( $cache['less']['root'] ) || ! file_exists( $cache['less']['root'] ) ) + $cache = array( 'vars' => $this->vars, 'less' => $less_path ); + + // less config + $less->setFormatter( apply_filters( 'less_compression', $this->compression ) ); + $less->setPreserveComments( apply_filters( 'less_preserve_comments', $this->preserve_comments ) ); + $less->setVariables( $this->vars ); + + // add directories to scan for imports + $import_dirs = apply_filters( 'less_import_dirs', $this->import_dirs ); + if ( ! empty( $import_dirs ) ) { + foreach( (array)$import_dirs as $dir ) + $less->addImportDir( $dir ); + } + + // register and unregister functions + foreach( $this->registered_functions as $name => $callable ) + $less->registerFunction( $name, $callable ); + + foreach( $this->unregistered_functions as $name ) + $less->unregisterFunction( $name ); + + // allow devs to mess around with the less object configuration + do_action_ref_array( 'lessc', array( &$less ) ); + + // $less->cachedCompile only checks for changed file modification times + // if using the theme customiser (changed variables not files) then force a compile + if ( $this->vars !== $cache[ 'vars' ] ) { + $force = true; + } else { + $force = false; + } + $less_cache = $less->cachedCompile( $cache[ 'less' ], apply_filters( 'less_force_compile', $force ) ); + + 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' ] ); + } + } catch ( exception $ex ) { + wp_die( $ex->getMessage() ); + } + + // restore query string it had if any + $url = trailingslashit( $this->get_cache_dir( false ) ) . "{$handle}.css" . ( ! empty( $query_string ) ? "?{$query_string}" : '' ); + + // restore original url scheme + $url = set_url_scheme( $url, $src_scheme ); + + return add_query_arg( 'ver', $less_cache[ 'updated' ], $url ); + } + + + /** + * Compile editor stylesheets registered via add_editor_style() + * + * @param string $mce_css Comma separated list of CSS file URLs + * @return string $mce_css New comma separated list of CSS file URLs + */ + public function parse_editor_stylesheets( $mce_css ) { + + // extract CSS file URLs + $style_sheets = explode( ",", $mce_css ); + + if ( count( $style_sheets ) ) { + $compiled_css = array(); + + // loop through editor styles, any .less files will be compiled and the compiled URL returned + foreach( $style_sheets as $style_sheet ) + $compiled_css[] = $this->parse_stylesheet( $style_sheet, $this->url_to_handle( $style_sheet ) ); + + $mce_css = implode( ",", $compiled_css ); + } + + // return new URLs + return $mce_css; + } + + + /** + * Get a nice handle to use for the compiled CSS file name + * + * @param string $url File URL to generate a handle from + * @return string $url Sanitized string to use for handle + */ + public function url_to_handle( $url ) { + + $url = parse_url( $url ); + $url = str_replace( '.less', '', basename( $url[ 'path' ] ) ); + $url = str_replace( '/', '-', $url ); + + return sanitize_key( $url ); + } + + + /** + * Get (and create if unavailable) the compiled CSS cache directory + * + * @param bool $path If true this method returns the cache's system path. Set to false to return the cache URL + * @return string $dir The system path or URL of the cache folder + */ + public function get_cache_dir( $path = true ) { + + // get path and url info + $upload_dir = wp_upload_dir(); + + if ( $path ) { + $dir = apply_filters( 'wp_less_cache_path', path_join( $upload_dir[ 'basedir' ], 'wp-less-cache' ) ); + // create folder if it doesn't exist yet + wp_mkdir_p( $dir ); + } else { + $dir = apply_filters( 'wp_less_cache_url', path_join( $upload_dir[ 'baseurl' ], 'wp-less-cache' ) ); + } + + return rtrim( $dir, '/' ); + } + + + /** + * Escape a string that has non alpha numeric characters variable for use within .less stylesheets + * + * @param string $str The string to escape + * @return string $str String ready for passing into the compiler + */ + public function sanitize_string( $str ) { + + return '~"' . $str . '"'; + } + + + /** + * Adds an interface to register lessc functions. See the documentation + * for details: http://leafo.net/lessphp/docs/#custom_functions + * + * @param string $name The name for function used in the less file eg. 'makebluer' + * @param string $callable (callback) Callable method or function that returns a lessc variable + * @return void + */ + public function register( $name, $callable ) { + $this->registered_functions[ $name ] = $callable; + } + + /** + * Unregisters a function + * + * @param string $name The function name to unregister + * @return void + */ + public function unregister( $name ) { + $this->unregistered_functions[ $name ] = $name; + } + + + /** + * Add less var prior to compiling + * + * @param string $name The variable name + * @param string $value The value for the variable as a string + * @return void + */ + public function add_var( $name, $value ) { + if ( is_string( $name ) ) + $this->vars[ $name ] = $value; + } + + /** + * Removes a less var + * + * @param string $name Name of the variable to remove + * @return void + */ + public function remove_var( $name ) { + if ( isset( $this->vars[ $name ] ) ) + unset( $this->vars[ $name ] ); + } +} // END class diff --git a/wp-less.php b/wp-less.php index 83ade8f..283436f 100644 --- a/wp-less.php +++ b/wp-less.php @@ -21,343 +21,13 @@ require_once( __DIR__.'/vendor/leafo/lessphp/lessc.inc.php' ); } +require_once( 'wp-less.class.php'); + if ( ! class_exists( 'wp_less' ) ) { // add on init to support theme customiser in v3.4 add_action( 'init', array( 'wp_less', 'instance' ) ); -/** - * Enables the use of LESS in WordPress - * - * See README.md for usage information - * - * @author Robert "sancho the fat" O'Rourke - * @link http://sanchothefat.com/ - * @package WP LESS - * @license MIT - * @version 2012-06-13.1701 - */ -class wp_less { - /** - * @static - * @var \wp_less Reusable object instance. - */ - protected static $instance = null; - - - /** - * Creates a new instance. Called on 'after_setup_theme'. - * May be used to access class methods from outside. - * - * @see __construct() - * @static - * @return \wp_less - */ - public static function instance() { - null === self :: $instance AND self :: $instance = new self; - return self :: $instance; - } - - - /** - * @var array Array store of callable functions used to extend the parser - */ - public $registered_functions = array(); - - - /** - * @var array Array store of function names to be removed from the compiler class - */ - public $unregistered_functions = array(); - - - /** - * @var array Variables to be passed into the compiler - */ - public $vars = array(); - - - /** - * @var string Compression class to use - */ - public $compression = 'compressed'; - - - /** - * @var bool Whether to preserve comments when compiling - */ - public $preserve_comments = false; - - - /** - * @var array Default import directory paths for lessc to scan - */ - public $import_dirs = array(); - - - /** - * Constructor - */ - public function __construct() { - - // every CSS file URL gets passed through this filter - add_filter( 'style_loader_src', array( $this, 'parse_stylesheet' ), 100000, 2 ); - - // editor stylesheet URLs are concatenated and run through this filter - add_filter( 'mce_css', array( $this, 'parse_editor_stylesheets' ), 100000 ); - - // exclude from official repo update check - add_filter( 'http_request_args', array( $this, 'http_request_args' ), 5, 2 ); - } - - /** - * Exclude from official repo update check. - * - * @link http://markjaquith.wordpress.com/2009/12/14/excluding-your-plugin-or-theme-from-update-checks/ - * - * @param array $r - * @param string $url - * @return array - */ - public function http_request_args( $r, $url ) { - - if ( 0 !== strpos( $url, 'http://api.wordpress.org/plugins/update-check' ) ) - return $r; // Not a plugin update request. Bail immediately. - - if ( $r['response']['code'] != 200 ) { - // this is a failed request! We cant modify the results if the results timed out/failed - return $r; - } - - $plugins = unserialize( $r[ 'body' ][ 'plugins' ] ); - unset( $plugins->plugins[plugin_basename( __FILE__ )] ); - unset( $plugins->active[ array_search( plugin_basename( __FILE__ ), $plugins->active ) ] ); - $r[ 'body' ][ 'plugins' ] = serialize( $plugins ); - - return $r; - } - - - /** - * Lessify the stylesheet and return the href of the compiled file - * - * @param string $src Source URL of the file to be parsed - * @param string $handle An identifier for the file used to create the file name in the cache - * @return string URL of the compiled stylesheet - */ - public function parse_stylesheet( $src, $handle ) { - - // we only want to handle .less files - if ( ! preg_match( '/\.less(\.php)?$/', preg_replace( '/\?.*$/', '', $src ) ) ) - return $src; - - // get file path from $src - if ( ! strstr( $src, '?' ) ) $src .= '?'; // prevent non-existent index warning when using list() & explode() - - // Match the URL schemes between WP_CONTENT_URL and $src, - // so the str_replace further down will work - $src_scheme = parse_url( $src, PHP_URL_SCHEME ); - $wp_content_url_scheme = parse_url( WP_CONTENT_URL, PHP_URL_SCHEME ); - if ( $src_scheme != $wp_content_url_scheme ) - $src = set_url_scheme( $src, $wp_content_url_scheme ); - - list( $less_path, $query_string ) = explode( '?', str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $src ) ); - - // output css file name - $css_path = trailingslashit( $this->get_cache_dir() ) . "{$handle}.css"; - - // automatically regenerate files if source's modified time has changed or vars have changed - try { - - // initialise the parser - $less = new lessc; - - // load the cache - $cache_path = "{$css_path}.cache"; - - if ( file_exists( $cache_path ) ) - $cache = unserialize( file_get_contents( $cache_path ) ); - - // vars to pass into the compiler - default @themeurl var for image urls etc... - $this->vars[ 'themeurl' ] = '~"' . get_stylesheet_directory_uri() . '"'; - $this->vars[ 'lessurl' ] = '~"' . dirname( $src ) . '"'; - $this->vars = apply_filters( 'less_vars', $this->vars, $handle ); - - // If the cache or root path in it are invalid then regenerate - if ( empty( $cache ) || empty( $cache['less']['root'] ) || ! file_exists( $cache['less']['root'] ) ) - $cache = array( 'vars' => $this->vars, 'less' => $less_path ); - - // less config - $less->setFormatter( apply_filters( 'less_compression', $this->compression ) ); - $less->setPreserveComments( apply_filters( 'less_preserve_comments', $this->preserve_comments ) ); - $less->setVariables( $this->vars ); - - // add directories to scan for imports - $import_dirs = apply_filters( 'less_import_dirs', $this->import_dirs ); - if ( ! empty( $import_dirs ) ) { - foreach( (array)$import_dirs as $dir ) - $less->addImportDir( $dir ); - } - // register and unregister functions - foreach( $this->registered_functions as $name => $callable ) - $less->registerFunction( $name, $callable ); - - foreach( $this->unregistered_functions as $name ) - $less->unregisterFunction( $name ); - - // allow devs to mess around with the less object configuration - do_action_ref_array( 'lessc', array( &$less ) ); - - // $less->cachedCompile only checks for changed file modification times - // if using the theme customiser (changed variables not files) then force a compile - if ( $this->vars !== $cache[ 'vars' ] ) { - $force = true; - } else { - $force = false; - } - $less_cache = $less->cachedCompile( $cache[ 'less' ], apply_filters( 'less_force_compile', $force ) ); - - 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' ] ); - } - } catch ( exception $ex ) { - wp_die( $ex->getMessage() ); - } - - // restore query string it had if any - $url = trailingslashit( $this->get_cache_dir( false ) ) . "{$handle}.css" . ( ! empty( $query_string ) ? "?{$query_string}" : '' ); - - // restore original url scheme - $url = set_url_scheme( $url, $src_scheme ); - - return add_query_arg( 'ver', $less_cache[ 'updated' ], $url ); - } - - - /** - * Compile editor stylesheets registered via add_editor_style() - * - * @param string $mce_css Comma separated list of CSS file URLs - * @return string $mce_css New comma separated list of CSS file URLs - */ - public function parse_editor_stylesheets( $mce_css ) { - - // extract CSS file URLs - $style_sheets = explode( ",", $mce_css ); - - if ( count( $style_sheets ) ) { - $compiled_css = array(); - - // loop through editor styles, any .less files will be compiled and the compiled URL returned - foreach( $style_sheets as $style_sheet ) - $compiled_css[] = $this->parse_stylesheet( $style_sheet, $this->url_to_handle( $style_sheet ) ); - - $mce_css = implode( ",", $compiled_css ); - } - - // return new URLs - return $mce_css; - } - - - /** - * Get a nice handle to use for the compiled CSS file name - * - * @param string $url File URL to generate a handle from - * @return string $url Sanitized string to use for handle - */ - public function url_to_handle( $url ) { - - $url = parse_url( $url ); - $url = str_replace( '.less', '', basename( $url[ 'path' ] ) ); - $url = str_replace( '/', '-', $url ); - - return sanitize_key( $url ); - } - - - /** - * Get (and create if unavailable) the compiled CSS cache directory - * - * @param bool $path If true this method returns the cache's system path. Set to false to return the cache URL - * @return string $dir The system path or URL of the cache folder - */ - public function get_cache_dir( $path = true ) { - - // get path and url info - $upload_dir = wp_upload_dir(); - - if ( $path ) { - $dir = apply_filters( 'wp_less_cache_path', path_join( $upload_dir[ 'basedir' ], 'wp-less-cache' ) ); - // create folder if it doesn't exist yet - wp_mkdir_p( $dir ); - } else { - $dir = apply_filters( 'wp_less_cache_url', path_join( $upload_dir[ 'baseurl' ], 'wp-less-cache' ) ); - } - - return rtrim( $dir, '/' ); - } - - - /** - * Escape a string that has non alpha numeric characters variable for use within .less stylesheets - * - * @param string $str The string to escape - * @return string $str String ready for passing into the compiler - */ - public function sanitize_string( $str ) { - - return '~"' . $str . '"'; - } - - - /** - * Adds an interface to register lessc functions. See the documentation - * for details: http://leafo.net/lessphp/docs/#custom_functions - * - * @param string $name The name for function used in the less file eg. 'makebluer' - * @param string $callable (callback) Callable method or function that returns a lessc variable - * @return void - */ - public function register( $name, $callable ) { - $this->registered_functions[ $name ] = $callable; - } - - /** - * Unregisters a function - * - * @param string $name The function name to unregister - * @return void - */ - public function unregister( $name ) { - $this->unregistered_functions[ $name ] = $name; - } - - - /** - * Add less var prior to compiling - * - * @param string $name The variable name - * @param string $value The value for the variable as a string - * @return void - */ - public function add_var( $name, $value ) { - if ( is_string( $name ) ) - $this->vars[ $name ] = $value; - } - - /** - * Removes a less var - * - * @param string $name Name of the variable to remove - * @return void - */ - public function remove_var( $name ) { - if ( isset( $this->vars[ $name ] ) ) - unset( $this->vars[ $name ] ); - } -} // END class if ( ! function_exists( 'register_less_function' ) && ! function_exists( 'unregister_less_function' ) ) { /** From 41fd3c328950094caf2b6058f93ee3dd99433de7 Mon Sep 17 00:00:00 2001 From: Tarendai Date: Sun, 27 Apr 2014 11:22:16 +0100 Subject: [PATCH 03/40] ignore the lessc directory --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f19e98c..192d4a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor/ composer.lock .DS_Store +/lessc/ \ No newline at end of file From c74e07d44d78fa26c7c6af45ea6bd2d9b685468f Mon Sep 17 00:00:00 2001 From: Tarendai Date: Sun, 27 Apr 2014 11:22:47 +0100 Subject: [PATCH 04/40] some cleanup --- wp-less.class.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index 2711f93..9a9b443 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -126,7 +126,8 @@ public function parse_stylesheet( $src, $handle ) { return $src; // get file path from $src - if ( ! strstr( $src, '?' ) ) $src .= '?'; // prevent non-existent index warning when using list() & explode() + if ( ! strstr( $src, '?' ) ) + $src .= '?'; // prevent non-existent index warning when using list() & explode() // Match the URL schemes between WP_CONTENT_URL and $src, // so the str_replace further down will work @@ -144,7 +145,7 @@ public function parse_stylesheet( $src, $handle ) { try { // initialise the parser - $less = new lessc; + $less = new lessc(); // load the cache $cache_path = "{$css_path}.cache"; @@ -190,7 +191,8 @@ public function parse_stylesheet( $src, $handle ) { } else { $force = false; } - $less_cache = $less->cachedCompile( $cache[ 'less' ], apply_filters( 'less_force_compile', $force ) ); + $force = apply_filters( 'less_force_compile', $force ); + $less_cache = $less->cachedCompile( $cache[ 'less' ], $force ); 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 ) ) ); From df1d8166511a207a2f88de6fd3c32cb76aa77b65 Mon Sep 17 00:00:00 2001 From: Tarendai Date: Sun, 27 Apr 2014 11:47:08 +0100 Subject: [PATCH 05/40] move if statement around --- wp-less.class.php | 552 +++++++++++++++++++++++----------------------- wp-less.php | 9 +- 2 files changed, 282 insertions(+), 279 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index 9a9b443..949303f 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -1,337 +1,341 @@ plugins[plugin_basename( __FILE__ )] ); + unset( $plugins->active[ array_search( plugin_basename( __FILE__ ), $plugins->active ) ] ); + $r[ 'body' ][ 'plugins' ] = serialize( $plugins ); - if ( $r['response']['code'] != 200 ) { - // this is a failed request! We cant modify the results if the results timed out/failed return $r; } - $plugins = unserialize( $r[ 'body' ][ 'plugins' ] ); - unset( $plugins->plugins[plugin_basename( __FILE__ )] ); - unset( $plugins->active[ array_search( plugin_basename( __FILE__ ), $plugins->active ) ] ); - $r[ 'body' ][ 'plugins' ] = serialize( $plugins ); - - return $r; - } + /** + * Lessify the stylesheet and return the href of the compiled file + * + * @param string $src Source URL of the file to be parsed + * @param string $handle An identifier for the file used to create the file name in the cache + * @return string URL of the compiled stylesheet + */ + public function parse_stylesheet( $src, $handle ) { + // we only want to handle .less files + if ( ! preg_match( '/\.less(\.php)?$/', preg_replace( '/\?.*$/', '', $src ) ) ) + return $src; + + // get file path from $src + if ( ! strstr( $src, '?' ) ) + $src .= '?'; // prevent non-existent index warning when using list() & explode() + + // Match the URL schemes between WP_CONTENT_URL and $src, + // so the str_replace further down will work + $src_scheme = parse_url( $src, PHP_URL_SCHEME ); + $wp_content_url_scheme = parse_url( WP_CONTENT_URL, PHP_URL_SCHEME ); + if ( $src_scheme != $wp_content_url_scheme ) + $src = set_url_scheme( $src, $wp_content_url_scheme ); + + list( $less_path, $query_string ) = explode( '?', str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $src ) ); + + // output css file name + $css_path = trailingslashit( $this->get_cache_dir() ) . "{$handle}.css"; + + // automatically regenerate files if source's modified time has changed or vars have changed + try { + + // initialise the parser + if ( !class_exists( 'lessc' ) ) { + wp_die( 'the lessphp library is missing, aborting, run composer update' ); + } + $less = new lessc(); + + // load the cache + $cache_path = "{$css_path}.cache"; + + if ( file_exists( $cache_path ) ) + $cache = unserialize( file_get_contents( $cache_path ) ); + + // vars to pass into the compiler - default @themeurl var for image urls etc... + $this->vars[ 'themeurl' ] = '~"' . get_stylesheet_directory_uri() . '"'; + $this->vars[ 'lessurl' ] = '~"' . dirname( $src ) . '"'; + $this->vars = apply_filters( 'less_vars', $this->vars, $handle ); + + // If the cache or root path in it are invalid then regenerate + if ( empty( $cache ) || empty( $cache['less']['root'] ) || ! file_exists( $cache['less']['root'] ) ) + $cache = array( 'vars' => $this->vars, 'less' => $less_path ); + + // less config + $less->setFormatter( apply_filters( 'less_compression', $this->compression ) ); + $less->setPreserveComments( apply_filters( 'less_preserve_comments', $this->preserve_comments ) ); + $less->setVariables( $this->vars ); + + // add directories to scan for imports + $import_dirs = apply_filters( 'less_import_dirs', $this->import_dirs ); + if ( ! empty( $import_dirs ) ) { + foreach( (array)$import_dirs as $dir ) + $less->addImportDir( $dir ); + } + + // register and unregister functions + foreach( $this->registered_functions as $name => $callable ) + $less->registerFunction( $name, $callable ); + + foreach( $this->unregistered_functions as $name ) + $less->unregisterFunction( $name ); + + // allow devs to mess around with the less object configuration + do_action_ref_array( 'lessc', array( &$less ) ); + + // $less->cachedCompile only checks for changed file modification times + // if using the theme customiser (changed variables not files) then force a compile + if ( $this->vars !== $cache[ 'vars' ] ) { + $force = true; + } else { + $force = false; + } + $force = apply_filters( 'less_force_compile', $force ); + $less_cache = $less->cachedCompile( $cache[ 'less' ], $force ); + + 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' ] ); + } + } catch ( exception $ex ) { + wp_die( $ex->getMessage() ); + } - /** - * Lessify the stylesheet and return the href of the compiled file - * - * @param string $src Source URL of the file to be parsed - * @param string $handle An identifier for the file used to create the file name in the cache - * @return string URL of the compiled stylesheet - */ - public function parse_stylesheet( $src, $handle ) { + // restore query string it had if any + $url = trailingslashit( $this->get_cache_dir( false ) ) . "{$handle}.css" . ( ! empty( $query_string ) ? "?{$query_string}" : '' ); - // we only want to handle .less files - if ( ! preg_match( '/\.less(\.php)?$/', preg_replace( '/\?.*$/', '', $src ) ) ) - return $src; + // restore original url scheme + $url = set_url_scheme( $url, $src_scheme ); - // get file path from $src - if ( ! strstr( $src, '?' ) ) - $src .= '?'; // prevent non-existent index warning when using list() & explode() + return add_query_arg( 'ver', $less_cache[ 'updated' ], $url ); + } - // Match the URL schemes between WP_CONTENT_URL and $src, - // so the str_replace further down will work - $src_scheme = parse_url( $src, PHP_URL_SCHEME ); - $wp_content_url_scheme = parse_url( WP_CONTENT_URL, PHP_URL_SCHEME ); - if ( $src_scheme != $wp_content_url_scheme ) - $src = set_url_scheme( $src, $wp_content_url_scheme ); - list( $less_path, $query_string ) = explode( '?', str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $src ) ); + /** + * Compile editor stylesheets registered via add_editor_style() + * + * @param string $mce_css Comma separated list of CSS file URLs + * @return string $mce_css New comma separated list of CSS file URLs + */ + public function parse_editor_stylesheets( $mce_css ) { - // output css file name - $css_path = trailingslashit( $this->get_cache_dir() ) . "{$handle}.css"; + // extract CSS file URLs + $style_sheets = explode( ",", $mce_css ); - // automatically regenerate files if source's modified time has changed or vars have changed - try { + if ( count( $style_sheets ) ) { + $compiled_css = array(); - // initialise the parser - $less = new lessc(); + // loop through editor styles, any .less files will be compiled and the compiled URL returned + foreach( $style_sheets as $style_sheet ) + $compiled_css[] = $this->parse_stylesheet( $style_sheet, $this->url_to_handle( $style_sheet ) ); - // load the cache - $cache_path = "{$css_path}.cache"; + $mce_css = implode( ",", $compiled_css ); + } - if ( file_exists( $cache_path ) ) - $cache = unserialize( file_get_contents( $cache_path ) ); + // return new URLs + return $mce_css; + } - // vars to pass into the compiler - default @themeurl var for image urls etc... - $this->vars[ 'themeurl' ] = '~"' . get_stylesheet_directory_uri() . '"'; - $this->vars[ 'lessurl' ] = '~"' . dirname( $src ) . '"'; - $this->vars = apply_filters( 'less_vars', $this->vars, $handle ); - // If the cache or root path in it are invalid then regenerate - if ( empty( $cache ) || empty( $cache['less']['root'] ) || ! file_exists( $cache['less']['root'] ) ) - $cache = array( 'vars' => $this->vars, 'less' => $less_path ); + /** + * Get a nice handle to use for the compiled CSS file name + * + * @param string $url File URL to generate a handle from + * @return string $url Sanitized string to use for handle + */ + public function url_to_handle( $url ) { - // less config - $less->setFormatter( apply_filters( 'less_compression', $this->compression ) ); - $less->setPreserveComments( apply_filters( 'less_preserve_comments', $this->preserve_comments ) ); - $less->setVariables( $this->vars ); + $url = parse_url( $url ); + $url = str_replace( '.less', '', basename( $url[ 'path' ] ) ); + $url = str_replace( '/', '-', $url ); - // add directories to scan for imports - $import_dirs = apply_filters( 'less_import_dirs', $this->import_dirs ); - if ( ! empty( $import_dirs ) ) { - foreach( (array)$import_dirs as $dir ) - $less->addImportDir( $dir ); - } + return sanitize_key( $url ); + } - // register and unregister functions - foreach( $this->registered_functions as $name => $callable ) - $less->registerFunction( $name, $callable ); - foreach( $this->unregistered_functions as $name ) - $less->unregisterFunction( $name ); + /** + * Get (and create if unavailable) the compiled CSS cache directory + * + * @param bool $path If true this method returns the cache's system path. Set to false to return the cache URL + * @return string $dir The system path or URL of the cache folder + */ + public function get_cache_dir( $path = true ) { - // allow devs to mess around with the less object configuration - do_action_ref_array( 'lessc', array( &$less ) ); + // get path and url info + $upload_dir = wp_upload_dir(); - // $less->cachedCompile only checks for changed file modification times - // if using the theme customiser (changed variables not files) then force a compile - if ( $this->vars !== $cache[ 'vars' ] ) { - $force = true; + if ( $path ) { + $dir = apply_filters( 'wp_less_cache_path', path_join( $upload_dir[ 'basedir' ], 'wp-less-cache' ) ); + // create folder if it doesn't exist yet + wp_mkdir_p( $dir ); } else { - $force = false; + $dir = apply_filters( 'wp_less_cache_url', path_join( $upload_dir[ 'baseurl' ], 'wp-less-cache' ) ); } - $force = apply_filters( 'less_force_compile', $force ); - $less_cache = $less->cachedCompile( $cache[ 'less' ], $force ); - 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' ] ); - } - } catch ( exception $ex ) { - wp_die( $ex->getMessage() ); + return rtrim( $dir, '/' ); } - // restore query string it had if any - $url = trailingslashit( $this->get_cache_dir( false ) ) . "{$handle}.css" . ( ! empty( $query_string ) ? "?{$query_string}" : '' ); - - // restore original url scheme - $url = set_url_scheme( $url, $src_scheme ); - - return add_query_arg( 'ver', $less_cache[ 'updated' ], $url ); - } - - - /** - * Compile editor stylesheets registered via add_editor_style() - * - * @param string $mce_css Comma separated list of CSS file URLs - * @return string $mce_css New comma separated list of CSS file URLs - */ - public function parse_editor_stylesheets( $mce_css ) { - - // extract CSS file URLs - $style_sheets = explode( ",", $mce_css ); - - if ( count( $style_sheets ) ) { - $compiled_css = array(); - // loop through editor styles, any .less files will be compiled and the compiled URL returned - foreach( $style_sheets as $style_sheet ) - $compiled_css[] = $this->parse_stylesheet( $style_sheet, $this->url_to_handle( $style_sheet ) ); + /** + * Escape a string that has non alpha numeric characters variable for use within .less stylesheets + * + * @param string $str The string to escape + * @return string $str String ready for passing into the compiler + */ + public function sanitize_string( $str ) { - $mce_css = implode( ",", $compiled_css ); + return '~"' . $str . '"'; } - // return new URLs - return $mce_css; - } - - - /** - * Get a nice handle to use for the compiled CSS file name - * - * @param string $url File URL to generate a handle from - * @return string $url Sanitized string to use for handle - */ - public function url_to_handle( $url ) { - - $url = parse_url( $url ); - $url = str_replace( '.less', '', basename( $url[ 'path' ] ) ); - $url = str_replace( '/', '-', $url ); - - return sanitize_key( $url ); - } - - - /** - * Get (and create if unavailable) the compiled CSS cache directory - * - * @param bool $path If true this method returns the cache's system path. Set to false to return the cache URL - * @return string $dir The system path or URL of the cache folder - */ - public function get_cache_dir( $path = true ) { - - // get path and url info - $upload_dir = wp_upload_dir(); - if ( $path ) { - $dir = apply_filters( 'wp_less_cache_path', path_join( $upload_dir[ 'basedir' ], 'wp-less-cache' ) ); - // create folder if it doesn't exist yet - wp_mkdir_p( $dir ); - } else { - $dir = apply_filters( 'wp_less_cache_url', path_join( $upload_dir[ 'baseurl' ], 'wp-less-cache' ) ); + /** + * Adds an interface to register lessc functions. See the documentation + * for details: http://leafo.net/lessphp/docs/#custom_functions + * + * @param string $name The name for function used in the less file eg. 'makebluer' + * @param string $callable (callback) Callable method or function that returns a lessc variable + * @return void + */ + public function register( $name, $callable ) { + $this->registered_functions[ $name ] = $callable; } - return rtrim( $dir, '/' ); - } - - - /** - * Escape a string that has non alpha numeric characters variable for use within .less stylesheets - * - * @param string $str The string to escape - * @return string $str String ready for passing into the compiler - */ - public function sanitize_string( $str ) { - - return '~"' . $str . '"'; - } - - - /** - * Adds an interface to register lessc functions. See the documentation - * for details: http://leafo.net/lessphp/docs/#custom_functions - * - * @param string $name The name for function used in the less file eg. 'makebluer' - * @param string $callable (callback) Callable method or function that returns a lessc variable - * @return void - */ - public function register( $name, $callable ) { - $this->registered_functions[ $name ] = $callable; - } - - /** - * Unregisters a function - * - * @param string $name The function name to unregister - * @return void - */ - public function unregister( $name ) { - $this->unregistered_functions[ $name ] = $name; - } + /** + * Unregisters a function + * + * @param string $name The function name to unregister + * @return void + */ + public function unregister( $name ) { + $this->unregistered_functions[ $name ] = $name; + } - /** - * Add less var prior to compiling - * - * @param string $name The variable name - * @param string $value The value for the variable as a string - * @return void - */ - public function add_var( $name, $value ) { - if ( is_string( $name ) ) - $this->vars[ $name ] = $value; - } + /** + * Add less var prior to compiling + * + * @param string $name The variable name + * @param string $value The value for the variable as a string + * @return void + */ + public function add_var( $name, $value ) { + if ( is_string( $name ) ) + $this->vars[ $name ] = $value; + } - /** - * Removes a less var - * - * @param string $name Name of the variable to remove - * @return void - */ - public function remove_var( $name ) { - if ( isset( $this->vars[ $name ] ) ) - unset( $this->vars[ $name ] ); + /** + * Removes a less var + * + * @param string $name Name of the variable to remove + * @return void + */ + public function remove_var( $name ) { + if ( isset( $this->vars[ $name ] ) ) + unset( $this->vars[ $name ] ); + } } -} // END class +} diff --git a/wp-less.php b/wp-less.php index 283436f..5afc083 100644 --- a/wp-less.php +++ b/wp-less.php @@ -22,12 +22,12 @@ } require_once( 'wp-less.class.php'); - -if ( ! class_exists( 'wp_less' ) ) { - // add on init to support theme customiser in v3.4 - add_action( 'init', array( 'wp_less', 'instance' ) ); +require_once( 'wp-less-admin.class.php' ); +$admin_page = new wp_less_admin(); +// add on init to support theme customiser in v3.4 +add_action( 'init', array( 'wp_less', 'instance' ) ); if ( ! function_exists( 'register_less_function' ) && ! function_exists( 'unregister_less_function' ) ) { /** @@ -80,4 +80,3 @@ function remove_less_var( $name ) { } } -} // endif; From 3e237d9c2d01ecb0cb9bbbff93f1fa355e0140f2 Mon Sep 17 00:00:00 2001 From: Tarendai Date: Sun, 27 Apr 2014 11:47:21 +0100 Subject: [PATCH 06/40] add an admin page --- wp-less-admin.class.php | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 wp-less-admin.class.php diff --git a/wp-less-admin.class.php b/wp-less-admin.class.php new file mode 100644 index 0000000..99cf5f6 --- /dev/null +++ b/wp-less-admin.class.php @@ -0,0 +1,44 @@ + +
+

WP-Less

+ +
+ Date: Sun, 27 Apr 2014 17:29:59 +0100 Subject: [PATCH 07/40] Add myself and cftp as contributors --- wp-less.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-less.php b/wp-less.php index 5afc083..a2c211a 100644 --- a/wp-less.php +++ b/wp-less.php @@ -4,7 +4,7 @@ Plugin URI: https://github.com/sanchothefat/wp-less/ Description: Allows you to enqueue .less files and have them automatically compiled whenever a change is detected. Author: Robert O'Rourke -Contributors: Franz Josef Kaiser, Tom Willmot, Rarst +Contributors: Franz Josef Kaiser,Tom Willmot, Rarst, Tom J Nowell, Code For The PeopleGIT S Version: 2.1 Author URI: http://interconnectit.com License: MIT From 5797af55847f07e986d7b33c6fd9ddf9feb60633 Mon Sep 17 00:00:00 2001 From: Tarendai Date: Sun, 27 Apr 2014 17:30:39 +0100 Subject: [PATCH 08/40] display recent messages --- wp-less-admin.class.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/wp-less-admin.class.php b/wp-less-admin.class.php index 99cf5f6..f2c1b1b 100644 --- a/wp-less-admin.class.php +++ b/wp-less-admin.class.php @@ -28,15 +28,20 @@ public function __construct() { } public function add_pages() { - add_management_page( 'WP Less', 'WP Less', 'manage_options', 'wpless', array( $this, 'display' ) ); + add_management_page( 'WP LESS', 'WP LESS', 'manage_options', 'wpless', array( $this, 'display' ) ); } public function display() { - echo 'wp less'; ?>

WP-Less

+ '.$message.'

'; + } + ?>
Date: Sun, 27 Apr 2014 17:31:20 +0100 Subject: [PATCH 09/40] log messages explaining what when and why a rebuild ocurred, but only keep 20 at a time --- wp-less.class.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/wp-less.class.php b/wp-less.class.php index 949303f..6dd35f7 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -198,10 +198,23 @@ public function parse_stylesheet( $src, $handle ) { $less_cache = $less->cachedCompile( $cache[ 'less' ], $force ); if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) || $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] || $this->vars !== $cache[ 'vars' ] ) { + $message = '[ '.date('D, d M Y H:i:s').' ] Rebuilt stylesheet with handle: "'.$handle.'"
'; + if ( $this->vars !== $cache[ 'vars' ] ) { + $message .= 'Variables changed'; + } else if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) ) { + $message .= 'Empty cache or empty last update time'; + } else if ( $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] ) { + $message .= 'Update times different'; + } else { + $message .= 'Unknown! Contact the developers poste haste!!!!!!!'; + } + $message .= '
src: "'.$src.'" css path: "'.$css_path.'" and cache path: "'.$cache_path.'"'; + $this->add_message( $message ); file_put_contents( $cache_path, serialize( array( 'vars' => $this->vars, 'less' => $less_cache ) ) ); file_put_contents( $css_path, $less_cache[ 'compiled' ] ); } } catch ( exception $ex ) { + $this->add_message( 'Lessphp failure '.$ex->GetMessage() ); wp_die( $ex->getMessage() ); } @@ -287,7 +300,6 @@ public function get_cache_dir( $path = true ) { * @return string $str String ready for passing into the compiler */ public function sanitize_string( $str ) { - return '~"' . $str . '"'; } @@ -337,5 +349,16 @@ public function remove_var( $name ) { if ( isset( $this->vars[ $name ] ) ) unset( $this->vars[ $name ] ); } + + public function add_message( $message_string ) { + $messages = get_option('wpless-recent-messages'); + if ( !is_array( $messages ) ) { + $messages = array(); + } + + $messages = array_slice( $messages, 0, 19 ); + array_unshift( $messages, $message_string ); + update_option( 'wpless-recent-messages', $messages ); + } } } From 37a9ea558da2e427b91c608df651c0609ba1acd4 Mon Sep 17 00:00:00 2001 From: Tarendai Date: Sun, 27 Apr 2014 17:50:34 +0100 Subject: [PATCH 10/40] changed the message format and handled timestamps and empty message log differently --- wp-less-admin.class.php | 39 +++++++++++++++++++++++++++++---------- wp-less.class.php | 22 ++++++++++++++-------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/wp-less-admin.class.php b/wp-less-admin.class.php index f2c1b1b..13347cb 100644 --- a/wp-less-admin.class.php +++ b/wp-less-admin.class.php @@ -15,7 +15,7 @@ class wp_less_admin { * * @see __construct() * @static - * @return \wp_less + * @return \wp_less_admin */ public static function instance() { null === self :: $instance AND self :: $instance = new self; @@ -33,15 +33,34 @@ public function add_pages() { public function display() { ?> -
-

WP-Less

- - '.$message.'

'; - } - ?> +
+

WP-LESS

+ + + + + + + + '; + if ( is_array( $message ) ) { + echo ''; + echo ''; + } else { + echo ''; + } + echo ''; + } + } else { + echo ''; + } + ?> + +
TimeMessage
'.date( 'D, d M Y H:i:s', $message['time'] ).''.$message['payload'].''.$message.'
No messages
cachedCompile( $cache[ 'less' ], $force ); if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) || $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] || $this->vars !== $cache[ 'vars' ] ) { - $message = '[ '.date('D, d M Y H:i:s').' ] Rebuilt stylesheet with handle: "'.$handle.'"
'; + $payload = 'Rebuilt stylesheet with handle: "'.$handle.'"
'; if ( $this->vars !== $cache[ 'vars' ] ) { - $message .= 'Variables changed'; + $payload .= 'Variables changed'; } else if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) ) { - $message .= 'Empty cache or empty last update time'; + $payload .= 'Empty cache or empty last update time'; } else if ( $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] ) { - $message .= 'Update times different'; + $payload .= 'Update times different'; } else { - $message .= 'Unknown! Contact the developers poste haste!!!!!!!'; + $payload .= 'Unknown! Contact the developers poste haste!!!!!!!'; } - $message .= '
src: "'.$src.'" css path: "'.$css_path.'" and cache path: "'.$cache_path.'"'; - $this->add_message( $message ); + $payload .= '
src: "'.$src.'" css path: "'.$css_path.'" and cache path: "'.$cache_path.'"'; + $this->add_message( array( + 'time' => time(), + 'payload' => $payload + ) ); file_put_contents( $cache_path, serialize( array( 'vars' => $this->vars, 'less' => $less_cache ) ) ); file_put_contents( $css_path, $less_cache[ 'compiled' ] ); } } catch ( exception $ex ) { - $this->add_message( 'Lessphp failure '.$ex->GetMessage() ); + $this->add_message( array( + 'time' => time(), + 'payload' => 'Lessphp failure '.$ex->GetMessage() + ) ); wp_die( $ex->getMessage() ); } From f2f9cd42408dee3d2818efac927653c081626329 Mon Sep 17 00:00:00 2001 From: Tarendai Date: Sun, 27 Apr 2014 17:50:54 +0100 Subject: [PATCH 11/40] greater than or equal to --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d352aee..68d95db 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": ">5.2.4", + "php": ">=5.2.4", "composer/installers": "~1.0", "leafo/lessphp": "0.4.0" } From ec34bdfd434e864d81b2defe1b4557e3ce947078 Mon Sep 17 00:00:00 2001 From: Tarendai Date: Fri, 2 May 2014 15:34:59 +0100 Subject: [PATCH 12/40] prettier table with a quick note on the 20 message limit --- wp-less-admin.class.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/wp-less-admin.class.php b/wp-less-admin.class.php index 13347cb..ee341fe 100644 --- a/wp-less-admin.class.php +++ b/wp-less-admin.class.php @@ -36,9 +36,11 @@ public function display() {

WP-LESS

- +

Here are messages from attempts to build/rebuild stylesheets. Only the last 20 messages are kept.

+ +
- + From 8df927afe40c3c74a1c36beeb9e38fabe1014c07 Mon Sep 17 00:00:00 2001 From: Tarendai Date: Fri, 2 May 2014 15:35:31 +0100 Subject: [PATCH 13/40] add the protocol handle to the start of the cache and css files, so that http vs https issues are no longer causing rebuilds --- wp-less.class.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index cb78434..2c688c6 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -139,7 +139,7 @@ public function parse_stylesheet( $src, $handle ) { list( $less_path, $query_string ) = explode( '?', str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $src ) ); // output css file name - $css_path = trailingslashit( $this->get_cache_dir() ) . "{$handle}.css"; + $css_path = trailingslashit( $this->get_cache_dir() ) . "{$src_scheme}-{$handle}.css"; // automatically regenerate files if source's modified time has changed or vars have changed try { @@ -197,10 +197,16 @@ public function parse_stylesheet( $src, $handle ) { $force = apply_filters( 'less_force_compile', $force ); $less_cache = $less->cachedCompile( $cache[ 'less' ], $force ); - if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) || $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] || $this->vars !== $cache[ 'vars' ] ) { + // if they have the same values but differing order, they wont match + /*sort( $cache['less'] ); + sort( $less_cache );*/ + + if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) || $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] || $this->vars != $cache[ 'vars' ] ) { $payload = 'Rebuilt stylesheet with handle: "'.$handle.'"
'; - if ( $this->vars !== $cache[ 'vars' ] ) { + if ( $this->vars != $cache[ 'vars' ] ) { $payload .= 'Variables changed'; + $difference = array_merge(array_diff_assoc( $cache['vars'], $this->vars), array_diff_assoc($this->vars, $cache['vars'] )); + $payload .= '
'.print_r( $difference, true ).'
'; } else if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) ) { $payload .= 'Empty cache or empty last update time'; } else if ( $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] ) { @@ -208,7 +214,7 @@ public function parse_stylesheet( $src, $handle ) { } else { $payload .= 'Unknown! Contact the developers poste haste!!!!!!!'; } - $payload .= '
src: "'.$src.'" css path: "'.$css_path.'" and cache path: "'.$cache_path.'"'; + $payload .= '
src: "'.$src.'" css path: "'.$css_path.'" and cache path: "'.$cache_path.'" and scheme "'.$src_scheme.'"'; $this->add_message( array( 'time' => time(), 'payload' => $payload @@ -225,7 +231,7 @@ public function parse_stylesheet( $src, $handle ) { } // restore query string it had if any - $url = trailingslashit( $this->get_cache_dir( false ) ) . "{$handle}.css" . ( ! empty( $query_string ) ? "?{$query_string}" : '' ); + $url = trailingslashit( $this->get_cache_dir( false ) ) . "{$src_scheme}-{$handle}.css" . ( ! empty( $query_string ) ? "?{$query_string}" : '' ); // restore original url scheme $url = set_url_scheme( $url, $src_scheme ); From 899f49d94be89660eba62ea7265ff4589fe89cf1 Mon Sep 17 00:00:00 2001 From: Tarendai Date: Fri, 2 May 2014 15:56:09 +0100 Subject: [PATCH 14/40] add a missing closing code tag in a message --- wp-less.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-less.class.php b/wp-less.class.php index 2c688c6..132e26c 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -214,7 +214,7 @@ public function parse_stylesheet( $src, $handle ) { } else { $payload .= 'Unknown! Contact the developers poste haste!!!!!!!'; } - $payload .= '
src: "'.$src.'" css path: "'.$css_path.'" and cache path: "'.$cache_path.'" and scheme "'.$src_scheme.'"'; + $payload .= '
src: "'.$src.'" css path: "'.$css_path.'" and cache path: "'.$cache_path.'" and scheme "'.$src_scheme.'"'; $this->add_message( array( 'time' => time(), 'payload' => $payload From bfedcf9c1cfbd6a316792cb816ad7864ca4fc4fa Mon Sep 17 00:00:00 2001 From: Tarendai Date: Mon, 2 Jun 2014 11:48:32 +0100 Subject: [PATCH 15/40] remove the wp_die, and return the original --- wp-less.class.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index 132e26c..add0fc3 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -148,7 +148,7 @@ public function parse_stylesheet( $src, $handle ) { if ( !class_exists( 'lessc' ) ) { wp_die( 'the lessphp library is missing, aborting, run composer update' ); } - $less = new lessc(); + // load the cache $cache_path = "{$css_path}.cache"; @@ -166,6 +166,7 @@ public function parse_stylesheet( $src, $handle ) { $cache = array( 'vars' => $this->vars, 'less' => $less_path ); // less config + $less = new lessc(); $less->setFormatter( apply_filters( 'less_compression', $this->compression ) ); $less->setPreserveComments( apply_filters( 'less_preserve_comments', $this->preserve_comments ) ); $less->setVariables( $this->vars ); @@ -198,8 +199,8 @@ public function parse_stylesheet( $src, $handle ) { $less_cache = $less->cachedCompile( $cache[ 'less' ], $force ); // if they have the same values but differing order, they wont match - /*sort( $cache['less'] ); - sort( $less_cache );*/ + //sort( $cache['less'] ); + //sort( $less_cache ); if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) || $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] || $this->vars != $cache[ 'vars' ] ) { $payload = 'Rebuilt stylesheet with handle: "'.$handle.'"
'; From 53fc608b2849e625b323d732fad8e5268ce7074b Mon Sep 17 00:00:00 2001 From: Tarendai Date: Mon, 2 Jun 2014 11:50:59 +0100 Subject: [PATCH 16/40] fix the wp die --- wp-less.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wp-less.class.php b/wp-less.class.php index 132e26c..56fe106 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -146,7 +146,8 @@ public function parse_stylesheet( $src, $handle ) { // initialise the parser if ( !class_exists( 'lessc' ) ) { - wp_die( 'the lessphp library is missing, aborting, run composer update' ); + return $url; + //wp_die( 'the lessphp library is missing, aborting, run composer update' ); } $less = new lessc(); From 622541aa9a4f9080646cfd4da4d729c0ae29601b Mon Sep 17 00:00:00 2001 From: Scott Evans Date: Fri, 13 Jun 2014 15:25:58 +0100 Subject: [PATCH 17/40] Change compiler to less.php (https://github.com/oyejorge/less.php). One thing to note is that variables within URLs must be place within quotes "" as noted here: https://github.com/sanchothefat/wp-less/issues/67#issuecomment-38369775 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 68d95db..31969ab 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,6 @@ "require": { "php": ">=5.2.4", "composer/installers": "~1.0", - "leafo/lessphp": "0.4.0" + "oyejorge/less.php": "~1.7" } } From 89cef9d8c45949fdcda0f6b7000249c8608abcdf Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Wed, 1 Apr 2015 12:52:06 +0100 Subject: [PATCH 18/40] escape the log messages --- wp-less-admin.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wp-less-admin.class.php b/wp-less-admin.class.php index ee341fe..3d20aab 100644 --- a/wp-less-admin.class.php +++ b/wp-less-admin.class.php @@ -50,10 +50,10 @@ public function display() { foreach ( $recent_messages as $message ) { echo '
'; if ( is_array( $message ) ) { - echo ''; - echo ''; + echo ''; + echo ''; } else { - echo ''; + echo ''; } echo ''; } @@ -67,4 +67,4 @@ public function display() { Date: Wed, 1 Apr 2015 12:54:42 +0100 Subject: [PATCH 19/40] escape wp_die message --- wp-less.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-less.class.php b/wp-less.class.php index ec525dc..35e3754 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -229,7 +229,7 @@ public function parse_stylesheet( $src, $handle ) { 'time' => time(), 'payload' => 'Lessphp failure '.$ex->GetMessage() ) ); - wp_die( $ex->getMessage() ); + wp_die( wp_kses( $ex->getMessage() ) ); } // restore query string it had if any From b53621c59522d617eacdf0c063f2c2c11009d2a6 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Wed, 1 Apr 2015 12:57:23 +0100 Subject: [PATCH 20/40] fix the comment from appearing red in GitHub --- wp-less.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/wp-less.php b/wp-less.php index a2c211a..707bf51 100644 --- a/wp-less.php +++ b/wp-less.php @@ -1,13 +1,13 @@ .less files and have them automatically compiled whenever a change is detected. -Author: Robert O'Rourke -Contributors: Franz Josef Kaiser,Tom Willmot, Rarst, Tom J Nowell, Code For The PeopleGIT S -Version: 2.1 -Author URI: http://interconnectit.com -License: MIT + * Plugin Name: LESS CSS + * Plugin URI: https://github.com/sanchothefat/wp-less/ + * Description: Allows you to enqueue .less files and have them automatically compiled whenever a change is detected. + * Author: Robert O'Rourke + * Contributors: Franz Josef Kaiser, Tom Willmot, Rarst, Tom J Nowell, Code For The People, Automattic + * Version: 2.1 + * Author URI: http://interconnectit.com + * License: MIT */ // Busted! No direct file access From e4668100c10974058110b8d7240630bd37e233f3 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Wed, 1 Apr 2015 13:08:58 +0100 Subject: [PATCH 21/40] pull across some changes by @rmccue in the original wp less plugin --- wp-less.class.php | 53 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index 35e3754..6509a28 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -152,10 +152,7 @@ public function parse_stylesheet( $src, $handle ) { // load the cache - $cache_path = "{$css_path}.cache"; - - if ( file_exists( $cache_path ) ) - $cache = unserialize( file_get_contents( $cache_path ) ); + $cache = $this->get_cached_file_data( $handle ); // vars to pass into the compiler - default @themeurl var for image urls etc... $this->vars[ 'themeurl' ] = '~"' . get_stylesheet_directory_uri() . '"'; @@ -221,8 +218,8 @@ public function parse_stylesheet( $src, $handle ) { 'time' => time(), 'payload' => $payload ) ); - file_put_contents( $cache_path, serialize( array( 'vars' => $this->vars, 'less' => $less_cache ) ) ); - file_put_contents( $css_path, $less_cache[ 'compiled' ] ); + $this->save_parsed_css( $css_path, $less_cache[ 'compiled' ] ); + $this->update_cached_file_data( $handle, array( 'vars' => $this->vars, 'less' => $less_cache ) ); } } catch ( exception $ex ) { $this->add_message( array( @@ -240,12 +237,52 @@ public function parse_stylesheet( $src, $handle ) { return add_query_arg( 'ver', $less_cache[ 'updated' ], $url ); } + + /** + * Update parsed cache data for this file + * + * + * @param $path + * @return bool + */ + public function get_cached_file_data( $path ) { + $caches = get_option( 'wp_less_cached_files', array() ); + + if ( isset( $caches[$path] ) ) { + return $caches[$path]; + } + + return null; + } + + public function save_parsed_css( $css_path, $file_contents ) { + if ( ! apply_filters( 'less_save_css', $css_path, $file_contents ) ) { + return; + } + + file_put_contents( $css_path, $file_contents ); + } + /** + * Update parsed cache data for this file + * + * @param $path + * @param $file_data + */ + public function update_cached_file_data( $path, $file_data ) { + $file_data['less']['compiled'] = ''; + + $caches = get_option( 'wp_less_cached_files', array() ); + + $caches[$path] = $file_data; + + update_option( 'wp_less_cached_files', $caches ); + } /** * Compile editor stylesheets registered via add_editor_style() * - * @param string $mce_css Comma separated list of CSS file URLs + * @param string $mce_css Comma sepwparated list of CSS file URLs * @return string $mce_css New comma separated list of CSS file URLs */ public function parse_editor_stylesheets( $mce_css ) { @@ -297,8 +334,6 @@ public function get_cache_dir( $path = true ) { if ( $path ) { $dir = apply_filters( 'wp_less_cache_path', path_join( $upload_dir[ 'basedir' ], 'wp-less-cache' ) ); - // create folder if it doesn't exist yet - wp_mkdir_p( $dir ); } else { $dir = apply_filters( 'wp_less_cache_url', path_join( $upload_dir[ 'baseurl' ], 'wp-less-cache' ) ); } From ea6c6ebf16bd9ef6d1643b74232b8d4acb58b224 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Wed, 1 Apr 2015 13:15:44 +0100 Subject: [PATCH 22/40] switch to md5 based check, and bring some other improvements over such as caching the upload url --- wp-less.class.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index 6509a28..f33a61a 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -138,9 +138,6 @@ public function parse_stylesheet( $src, $handle ) { list( $less_path, $query_string ) = explode( '?', str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $src ) ); - // output css file name - $css_path = trailingslashit( $this->get_cache_dir() ) . "{$src_scheme}-{$handle}.css"; - // automatically regenerate files if source's modified time has changed or vars have changed try { @@ -163,6 +160,10 @@ public function parse_stylesheet( $src, $handle ) { if ( empty( $cache ) || empty( $cache['less']['root'] ) || ! file_exists( $cache['less']['root'] ) ) $cache = array( 'vars' => $this->vars, 'less' => $less_path ); + if ( empty( $cache['url'] ) ) { + $cache['url'] = trailingslashit( $this->get_cache_dir( false ) ) . "{$handle}.css"; + } + // less config $less = new lessc(); $less->setFormatter( apply_filters( 'less_compression', $this->compression ) ); @@ -200,7 +201,15 @@ public function parse_stylesheet( $src, $handle ) { //sort( $cache['less'] ); //sort( $less_cache ); - if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) || $less_cache[ 'updated' ] > $cache[ 'less' ][ 'updated' ] || $this->vars != $cache[ 'vars' ] ) { + if ( empty( $cache ) || empty( $cache[ 'less' ][ 'updated' ] ) || md5( $less_cache['compiled'] ) !== md5( $cache['less']['compiled'] ) || $this->vars !== $cache['vars'] ) { + // output css file name + $css_path = trailingslashit( $this->get_cache_dir() ) . "{$handle}.css"; + + $cache = array( + 'vars' => $this->vars, + 'less' => $less_cache, + 'url' => trailingslashit( $this->get_cache_dir( false ) ) . "{$handle}.css", + ); $payload = 'Rebuilt stylesheet with handle: "'.$handle.'"
'; if ( $this->vars != $cache[ 'vars' ] ) { $payload .= 'Variables changed'; @@ -219,7 +228,7 @@ public function parse_stylesheet( $src, $handle ) { 'payload' => $payload ) ); $this->save_parsed_css( $css_path, $less_cache[ 'compiled' ] ); - $this->update_cached_file_data( $handle, array( 'vars' => $this->vars, 'less' => $less_cache ) ); + $this->update_cached_file_data( $handle, $cache ); } } catch ( exception $ex ) { $this->add_message( array( @@ -230,7 +239,7 @@ public function parse_stylesheet( $src, $handle ) { } // restore query string it had if any - $url = trailingslashit( $this->get_cache_dir( false ) ) . "{$src_scheme}-{$handle}.css" . ( ! empty( $query_string ) ? "?{$query_string}" : '' ); + $url = $cache['url'] . ( ! empty( $query_string ) ? "?{$query_string}" : '' ); // restore original url scheme $url = set_url_scheme( $url, $src_scheme ); From cc0efc7b6524783b649fb22eb21a29a0b9c6d9ad Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Wed, 1 Apr 2015 13:19:28 +0100 Subject: [PATCH 23/40] bring across disabling of LESS compilation via an option by @joehoyle --- wp-less.class.php | 62 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index f33a61a..c0e482e 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -138,6 +138,34 @@ public function parse_stylesheet( $src, $handle ) { list( $less_path, $query_string ) = explode( '?', str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $src ) ); + $cache = $this->get_cached_file_data( $handle ); + // vars to pass into the compiler - default @themeurl var for image urls etc... + $this->vars['themeurl'] = '~"' . get_stylesheet_directory_uri() . '"'; + $this->vars['lessurl'] = '~"' . dirname( $src ) . '"'; + $this->vars = apply_filters( 'less_vars', $this->vars, $handle ); + + // The overall "version" of the LESS file is all it's vars, src etc. + $less_version = md5( serialize( array( $this->vars, $src ) ) ); + + /** + * Give the ability to disable always compiling the LESS with lessc() + * and instead just use the $vars and $version of the LESS file to + * dictate whether the LESS should be (re)generated. + * + * This means we don't need to run everything through the lessc() compiler + * on every page load. The tradeoff is making a change in a LESS file will not + * necessarily cause a (re)generation, one would need to bump the $ver param + * on wp_enqueue_script() to cause that. + */ + if ( ! get_option( 'wp_less_always_compile_less', true ) ) { + if ( ( ! empty( $cache['version'] ) ) && $cache['version'] === $less_version ) { + // restore query string it had if any + $url = $cache['url'] . ( ! empty( $query_string ) ? "?{$query_string}" : '' ); + $url = set_url_scheme( $url, $src_scheme ); + return add_query_arg( 'ver', $less_version, $url ); + } + } + // automatically regenerate files if source's modified time has changed or vars have changed try { @@ -147,15 +175,6 @@ public function parse_stylesheet( $src, $handle ) { //wp_die( 'the lessphp library is missing, aborting, run composer update' ); } - - // load the cache - $cache = $this->get_cached_file_data( $handle ); - - // vars to pass into the compiler - default @themeurl var for image urls etc... - $this->vars[ 'themeurl' ] = '~"' . get_stylesheet_directory_uri() . '"'; - $this->vars[ 'lessurl' ] = '~"' . dirname( $src ) . '"'; - $this->vars = apply_filters( 'less_vars', $this->vars, $handle ); - // If the cache or root path in it are invalid then regenerate if ( empty( $cache ) || empty( $cache['less']['root'] ) || ! file_exists( $cache['less']['root'] ) ) $cache = array( 'vars' => $this->vars, 'less' => $less_path ); @@ -206,10 +225,24 @@ public function parse_stylesheet( $src, $handle ) { $css_path = trailingslashit( $this->get_cache_dir() ) . "{$handle}.css"; $cache = array( - 'vars' => $this->vars, - 'less' => $less_cache, - 'url' => trailingslashit( $this->get_cache_dir( false ) ) . "{$handle}.css", + 'vars' => $this->vars, + 'url' => trailingslashit( $this->get_cache_dir( false ) ) . "{$handle}.css", + 'version' => $less_version, + 'less' => null ); + + /** + * If the option to not have LESS always compiled is set, + * then we dont store the whole less_cache in the options table as it's + * not needed because we only do a comparison based off $vars and $src + * (which includes the $ver param). + * + * This saves space on the options table for high performance environments. + */ + if ( get_option( 'wp_less_always_compile_less', true ) ) { + $cache['less'] = $less_cache; + } + $payload = 'Rebuilt stylesheet with handle: "'.$handle.'"
'; if ( $this->vars != $cache[ 'vars' ] ) { $payload .= 'Variables changed'; @@ -245,6 +278,11 @@ public function parse_stylesheet( $src, $handle ) { $url = set_url_scheme( $url, $src_scheme ); return add_query_arg( 'ver', $less_cache[ 'updated' ], $url ); + if ( get_option( 'wp_less_always_compile_less', true ) ) { + return add_query_arg( 'ver', $less_cache['updated'], $url ); + } else { + return add_query_arg( 'ver', $less_version, $url ); + } } /** From 37c4a9d4c791aebb437246930e16fbfda9ec2ac6 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Wed, 1 Apr 2015 13:23:16 +0100 Subject: [PATCH 24/40] Bring across fixes by @johnbillion in https://github.com/johnbillion/wp-less/commit/6a15b7a1080be98436011c899c24afbcd674bb45 --- wp-less.class.php | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index c0e482e..326c45b 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -96,7 +96,7 @@ public function __construct() { */ public function http_request_args( $r, $url ) { - if ( 0 !== strpos( $url, 'http://api.wordpress.org/plugins/update-check' ) ) + if ( ! preg_match( '#://api\.wordpress\.org/plugins/update-check/(?P[0-9.]+)/#', $url, $matches ) ) return $r; // Not a plugin update request. Bail immediately. if ( $r['response']['code'] != 200 ) { @@ -105,9 +105,35 @@ public function http_request_args( $r, $url ) { } $plugins = unserialize( $r[ 'body' ][ 'plugins' ] ); + switch ( $matches['version'] ) { + + case '1.0': + $plugins = unserialize( $r[ 'body' ][ 'plugins' ] ); + break; + + case '1.1': + $plugins = json_decode( $r[ 'body' ][ 'plugins' ] ); + break; + + default: + return $r; + break; + + } + unset( $plugins->plugins[plugin_basename( __FILE__ )] ); unset( $plugins->active[ array_search( plugin_basename( __FILE__ ), $plugins->active ) ] ); - $r[ 'body' ][ 'plugins' ] = serialize( $plugins ); + switch ( $matches['version'] ) { + + case '1.0': + $r[ 'body' ][ 'plugins' ] = serialize( $plugins ); + break; + + case '1.1': + $r[ 'body' ][ 'plugins' ] = json_encode( $plugins ); + break; + + } return $r; } From c201fe0b7f267c19b0b04cf00761c1e2e3435aac Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Wed, 1 Apr 2015 13:24:55 +0100 Subject: [PATCH 25/40] fix authors json --- composer.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 31969ab..682cc3d 100644 --- a/composer.json +++ b/composer.json @@ -7,9 +7,7 @@ { "name": "Robert O'Rourke", "email": "rob@interconnectit.com" - } - ], - "authors": [ + }, { "name": "Interconnect/IT", "homepage": "http://interconnectit.com/" From 7eb43138ca6234c8c6c6280d4ea0675554aa2d97 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Wed, 8 Apr 2015 12:31:50 +0100 Subject: [PATCH 26/40] Changed wp_kses to wp_strip_all_tags as wp_kses requires two args --- wp-less-admin.class.php | 4 ++-- wp-less.class.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/wp-less-admin.class.php b/wp-less-admin.class.php index 3d20aab..84061a8 100644 --- a/wp-less-admin.class.php +++ b/wp-less-admin.class.php @@ -51,9 +51,9 @@ public function display() { echo '
'; if ( is_array( $message ) ) { echo ''; - echo ''; + echo ''; } else { - echo ''; + echo ''; } echo ''; } diff --git a/wp-less.class.php b/wp-less.class.php index 326c45b..2c14085 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -294,7 +294,7 @@ public function parse_stylesheet( $src, $handle ) { 'time' => time(), 'payload' => 'Lessphp failure '.$ex->GetMessage() ) ); - wp_die( wp_kses( $ex->getMessage() ) ); + wp_die( wp_strip_all_tags( $ex->getMessage() ) ); } // restore query string it had if any From 449895b966d31f5924d34718913ec5fc7feb1156 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Wed, 8 Apr 2015 12:35:48 +0100 Subject: [PATCH 27/40] Still requires you to create directory if it doesnt exist. Should not have been removed --- wp-less.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wp-less.class.php b/wp-less.class.php index 2c14085..1c41351 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -407,6 +407,8 @@ public function get_cache_dir( $path = true ) { if ( $path ) { $dir = apply_filters( 'wp_less_cache_path', path_join( $upload_dir[ 'basedir' ], 'wp-less-cache' ) ); + // create folder if it doesn't exist yet + wp_mkdir_p( $dir ); } else { $dir = apply_filters( 'wp_less_cache_url', path_join( $upload_dir[ 'baseurl' ], 'wp-less-cache' ) ); } From 53de2868f7f05fe59588a206efb8249505b58b01 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Wed, 8 Apr 2015 13:52:27 +0100 Subject: [PATCH 28/40] Lets reach the if statement please --- wp-less.class.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index 1c41351..ab45cbb 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -303,12 +303,13 @@ public function parse_stylesheet( $src, $handle ) { // restore original url scheme $url = set_url_scheme( $url, $src_scheme ); - return add_query_arg( 'ver', $less_cache[ 'updated' ], $url ); + if ( get_option( 'wp_less_always_compile_less', true ) ) { return add_query_arg( 'ver', $less_cache['updated'], $url ); - } else { - return add_query_arg( 'ver', $less_version, $url ); } + + return add_query_arg( 'ver', $less_version, $url ); + } /** From 639006e6f1a8928f16a29ff6a70623c22115c4eb Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Wed, 5 Aug 2015 17:17:31 +0100 Subject: [PATCH 29/40] Changed less.php version number Wasn't loading latest stable version of less.php. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 682cc3d..5b37c4f 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,6 @@ "require": { "php": ">=5.2.4", "composer/installers": "~1.0", - "oyejorge/less.php": "~1.7" + "oyejorge/less.php": "1.7.0.x" } } From d5bc40aa8d5fea44b99d8c22e6b508312742e0d7 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Sun, 3 Apr 2016 18:33:33 +0100 Subject: [PATCH 30/40] Removed output of nonexistant var cache_path, fixes a warning --- wp-less.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-less.class.php b/wp-less.class.php index ab45cbb..f99f3be 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -281,7 +281,7 @@ public function parse_stylesheet( $src, $handle ) { } else { $payload .= 'Unknown! Contact the developers poste haste!!!!!!!'; } - $payload .= '
src: "'.$src.'" css path: "'.$css_path.'" and cache path: "'.$cache_path.'" and scheme "'.$src_scheme.'"'; + $payload .= '
src: "'.$src.'" css path: "'.$css_path.'" and scheme "'.$src_scheme.'"'; $this->add_message( array( 'time' => time(), 'payload' => $payload From fb5b6a4b8d7c96021f8628f67baaa2f796d2d795 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Mon, 11 Apr 2016 19:28:33 +0100 Subject: [PATCH 31/40] Add 2 error messages and braces --- wp-less.class.php | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index f99f3be..486bb37 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -148,19 +148,22 @@ public function http_request_args( $r, $url ) { */ public function parse_stylesheet( $src, $handle ) { // we only want to handle .less files - if ( ! preg_match( '/\.less(\.php)?$/', preg_replace( '/\?.*$/', '', $src ) ) ) + if ( ! preg_match( '/\.less(\.php)?$/', preg_replace( '/\?.*$/', '', $src ) ) ) { return $src; + } // get file path from $src - if ( ! strstr( $src, '?' ) ) + if ( ! strstr( $src, '?' ) ) { $src .= '?'; // prevent non-existent index warning when using list() & explode() + } // Match the URL schemes between WP_CONTENT_URL and $src, // so the str_replace further down will work $src_scheme = parse_url( $src, PHP_URL_SCHEME ); $wp_content_url_scheme = parse_url( WP_CONTENT_URL, PHP_URL_SCHEME ); - if ( $src_scheme != $wp_content_url_scheme ) + if ( $src_scheme != $wp_content_url_scheme ) { $src = set_url_scheme( $src, $wp_content_url_scheme ); + } list( $less_path, $query_string ) = explode( '?', str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $src ) ); @@ -197,6 +200,11 @@ public function parse_stylesheet( $src, $handle ) { // initialise the parser if ( !class_exists( 'lessc' ) ) { + $payload = 'Failed to do work, lessc library missing' + $this->add_message( array( + 'time' => time(), + 'payload' => $payload + ) ); return $url; //wp_die( 'the lessphp library is missing, aborting, run composer update' ); } @@ -223,11 +231,13 @@ public function parse_stylesheet( $src, $handle ) { } // register and unregister functions - foreach( $this->registered_functions as $name => $callable ) + foreach( $this->registered_functions as $name => $callable ) { $less->registerFunction( $name, $callable ); + } - foreach( $this->unregistered_functions as $name ) + foreach( $this->unregistered_functions as $name ) { $less->unregisterFunction( $name ); + } // allow devs to mess around with the less object configuration do_action_ref_array( 'lessc', array( &$less ) ); @@ -334,7 +344,14 @@ public function save_parsed_css( $css_path, $file_contents ) { return; } - file_put_contents( $css_path, $file_contents ); + if ( file_put_contents( $css_path, $file_contents ) == false ) { + $payload = 'Failed to write parsed css'; + $payload .= '
css path: "'.$css_path'"'; + $this->add_message( array( + 'time' => time(), + 'payload' => $payload + ) ); + } } /** From bfd87f4983aef7a779b2e03aa443b1ccf9bb5ee6 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Mon, 11 Apr 2016 19:29:09 +0100 Subject: [PATCH 32/40] closing semicolon --- wp-less.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-less.class.php b/wp-less.class.php index 486bb37..0f0b821 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -200,7 +200,7 @@ public function parse_stylesheet( $src, $handle ) { // initialise the parser if ( !class_exists( 'lessc' ) ) { - $payload = 'Failed to do work, lessc library missing' + $payload = 'Failed to do work, lessc library missing'; $this->add_message( array( 'time' => time(), 'payload' => $payload From a7f5e30b56f1444f2e538d422eeffdd6989b10c1 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Mon, 11 Apr 2016 19:30:09 +0100 Subject: [PATCH 33/40] Missing dot --- wp-less.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-less.class.php b/wp-less.class.php index 0f0b821..6409688 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -346,7 +346,7 @@ public function save_parsed_css( $css_path, $file_contents ) { if ( file_put_contents( $css_path, $file_contents ) == false ) { $payload = 'Failed to write parsed css'; - $payload .= '
css path: "'.$css_path'"'; + $payload .= '
css path: "'.$css_path.'"'; $this->add_message( array( 'time' => time(), 'payload' => $payload From df61d18176ab3cee9fc064ab6bede1e090635d41 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Mon, 11 Apr 2016 14:42:22 -0400 Subject: [PATCH 34/40] output a log message if the cache folder couldnt be created --- wp-less.class.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index 6409688..793095f 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -426,7 +426,12 @@ public function get_cache_dir( $path = true ) { if ( $path ) { $dir = apply_filters( 'wp_less_cache_path', path_join( $upload_dir[ 'basedir' ], 'wp-less-cache' ) ); // create folder if it doesn't exist yet - wp_mkdir_p( $dir ); + if ( !wp_mkdir_p( $dir ) ) { + $this->add_message( array( + 'time' => time(), + 'payload' => 'Failed to create cache directory "'.$dir.'"', + ) ); + } } else { $dir = apply_filters( 'wp_less_cache_url', path_join( $upload_dir[ 'baseurl' ], 'wp-less-cache' ) ); } @@ -494,7 +499,7 @@ public function remove_var( $name ) { public function add_message( $message_string ) { $messages = get_option('wpless-recent-messages'); - if ( !is_array( $messages ) ) { + if ( !is_array( $messages ) || empty( $messages ) ) { $messages = array(); } From 15d5cdba08ba9738ceade121d87f96f59842c3fc Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Mon, 22 Aug 2016 16:59:28 +0100 Subject: [PATCH 35/40] fixes a warning if the response body isn't set in the update check filter --- wp-less.class.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index 793095f..4b49d82 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -99,9 +99,11 @@ public function http_request_args( $r, $url ) { if ( ! preg_match( '#://api\.wordpress\.org/plugins/update-check/(?P[0-9.]+)/#', $url, $matches ) ) return $r; // Not a plugin update request. Bail immediately. - if ( $r['response']['code'] != 200 ) { - // this is a failed request! We cant modify the results if the results timed out/failed - return $r; + if ( !empty( $r['response'] ) ) { + if ( $r['response']['code'] != 200 ) { + // this is a failed request! We can't modify the results if the results timed out/failed + return $r; + } } $plugins = unserialize( $r[ 'body' ][ 'plugins' ] ); From 7911090760a0ba905cdc1936a97f07f6d4700587 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Mon, 22 Aug 2016 17:04:06 +0100 Subject: [PATCH 36/40] $r isn't an array, switch to isset for now in the update filter function --- wp-less.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-less.class.php b/wp-less.class.php index 4b49d82..6750930 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -99,7 +99,7 @@ public function http_request_args( $r, $url ) { if ( ! preg_match( '#://api\.wordpress\.org/plugins/update-check/(?P[0-9.]+)/#', $url, $matches ) ) return $r; // Not a plugin update request. Bail immediately. - if ( !empty( $r['response'] ) ) { + if ( !isset( $r['response'] ) ) { if ( $r['response']['code'] != 200 ) { // this is a failed request! We can't modify the results if the results timed out/failed return $r; From f1738e9ad32988e4fe81bd0009364bdbf1462d3d Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Mon, 22 Aug 2016 17:06:36 +0100 Subject: [PATCH 37/40] type hint $r to an array and use isset rather than !isset in the check --- wp-less.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index 6750930..75c24b0 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -94,12 +94,12 @@ public function __construct() { * @param string $url * @return array */ - public function http_request_args( $r, $url ) { + public function http_request_args( array $r, $url ) { if ( ! preg_match( '#://api\.wordpress\.org/plugins/update-check/(?P[0-9.]+)/#', $url, $matches ) ) return $r; // Not a plugin update request. Bail immediately. - if ( !isset( $r['response'] ) ) { + if ( isset( $r['response'] ) ) { if ( $r['response']['code'] != 200 ) { // this is a failed request! We can't modify the results if the results timed out/failed return $r; From 85dd8218bc0cde2bbb82836d97f9b73feea94230 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Mon, 22 Aug 2016 17:08:22 +0100 Subject: [PATCH 38/40] extract a variable out of an array lookup --- wp-less.class.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index 75c24b0..8f919a4 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -123,8 +123,10 @@ public function http_request_args( array $r, $url ) { } - unset( $plugins->plugins[plugin_basename( __FILE__ )] ); - unset( $plugins->active[ array_search( plugin_basename( __FILE__ ), $plugins->active ) ] ); + $basename = plugin_basename( __FILE__ ); + + unset( $plugins->plugins[ $basename ] ); + unset( $plugins->active[ array_search( $basename, $plugins->active ) ] ); switch ( $matches['version'] ) { case '1.0': From 1e280d6335e5f121ce0b37ae50a3bdfccb58d18d Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Mon, 22 Aug 2016 17:12:36 +0100 Subject: [PATCH 39/40] prevent a fatal error from happening with a temporary work around when filtering updates --- wp-less.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-less.class.php b/wp-less.class.php index 8f919a4..3a324eb 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -125,7 +125,7 @@ public function http_request_args( array $r, $url ) { $basename = plugin_basename( __FILE__ ); - unset( $plugins->plugins[ $basename ] ); + //unset( $plugins->plugins[ $basename ] ); @TODO: Fix unset( $plugins->active[ array_search( $basename, $plugins->active ) ] ); switch ( $matches['version'] ) { From 92e1e5ccdf66dcb7ed059c1da16d6a23aa7d1c61 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Sun, 28 Aug 2016 17:22:23 +0100 Subject: [PATCH 40/40] switch the plugin update filter to be more reliable --- wp-less.class.php | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/wp-less.class.php b/wp-less.class.php index 3a324eb..6952f81 100644 --- a/wp-less.class.php +++ b/wp-less.class.php @@ -106,38 +106,11 @@ public function http_request_args( array $r, $url ) { } } - $plugins = unserialize( $r[ 'body' ][ 'plugins' ] ); - switch ( $matches['version'] ) { - - case '1.0': - $plugins = unserialize( $r[ 'body' ][ 'plugins' ] ); - break; - - case '1.1': - $plugins = json_decode( $r[ 'body' ][ 'plugins' ] ); - break; - - default: - return $r; - break; - - } - + $plugins = json_decode( $r[ 'body' ][ 'plugins' ] ); $basename = plugin_basename( __FILE__ ); - - //unset( $plugins->plugins[ $basename ] ); @TODO: Fix + unset( $plugins->plugins->$basename ); unset( $plugins->active[ array_search( $basename, $plugins->active ) ] ); - switch ( $matches['version'] ) { - - case '1.0': - $r[ 'body' ][ 'plugins' ] = serialize( $plugins ); - break; - - case '1.1': - $r[ 'body' ][ 'plugins' ] = json_encode( $plugins ); - break; - - } + $r[ 'body' ][ 'plugins' ] = json_encode( $plugins ); return $r; }
TimeTime Message
'.date( 'D, d M Y H:i:s', $message['time'] ).''.$message['payload'].''.date( 'D, d M Y H:i:s', absint( $message['time'] ) ).''.wp_kses( $message['payload'] ).''.$message.''.wp_kses( $message ).'
'.date( 'D, d M Y H:i:s', absint( $message['time'] ) ).''.wp_kses( $message['payload'] ).''.wp_strip_all_tags( $message['payload'] ).''.wp_kses( $message ).''.wp_strip_all_tags( $message ).'