all repos — wp-autoless @ 90b7e3d3bcc402e14d377c16a5e7f91a488a6917

wp-autoless.php (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
<?php
/*
Plugin Name: wp-autoless
Plugin URI: https://github.com/petermolnar/wp-autoless
Description:
Version: 0.1
Author: Peter Molnar <hello@petermolnar.net>
Author URI: http://petermolnar.net/
License: GPLv3
*/

/*  Copyright 2016 Peter Molnar ( hello@petermolnar.net )

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License, version 3, as
	published by the Free Software Foundation.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

namespace WP_AUTOLESS;

require __DIR__ . '/vendor/autoload.php';

\add_action ( 'init', 'WP_AUTOLESS\compile' );

function compile () {
	$tdir = \get_template_directory();
	$lessfiles = find_less_files ( $tdir );

	foreach ( $lessfiles as $lessfile ) {
		$lessmtime = filemtime( $lessfile );
		$cssfile = str_replace( '.less', '.css', $lessfile );
		$cssmtime = file_exists ( $cssfile ) ? filemtime( $cssfile ) : '0';

		if ( $cssmtime < $lessmtime ) {
			try {
				$less = new \lessc;
				//$less->setFormatter("classic");
				$less->setFormatter("compressed");
				$less->compileFile( $lessfile, $cssfile );
			}
			catch (Exception $e) {
				debug('Something went wrong with LESS: ' . $e->getMessage(), 4);
			}

			touch ( $cssfile, $lessmtime );
		}
	}
}

function find_less_files ( $dir ) {
	$r = array();
	$list = scandir( $dir );

	foreach ($list as $key => $name ) {
		$path = realpath( $dir . DIRECTORY_SEPARATOR . $name );
		if ( strstr ( $path, 'lessphp') )
			continue;

		if ( is_dir( $path ) && ! in_array ( $name, array( '.', '..' ) ) ) {
			$sub = find_less_files( $path );
			$r = array_merge( $r, $sub );
		}
		elseif ( preg_match ( '/\.less$/i', $name ) ) {
			array_push( $r, $path );
		}
	}

	$r = array_unique( $r );
	return $r;
}


/**
 *
 * debug messages; will only work if WP_DEBUG is on
 * or if the level is LOG_ERR, but that will kill the process
 *
 * @param string $message
 * @param int $level
 *
 * @output log to syslog | wp_die on high level
 * @return false on not taking action, true on log sent
 */
function debug( $message, $level = LOG_NOTICE ) {
	if ( empty( $message ) )
		return false;

	if ( @is_array( $message ) || @is_object ( $message ) )
		$message = json_encode($message);

	$levels = array (
		LOG_EMERG => 0, // system is unusable
		LOG_ALERT => 1, // Alert 	action must be taken immediately
		LOG_CRIT => 2, // Critical 	critical conditions
		LOG_ERR => 3, // Error 	error conditions
		LOG_WARNING => 4, // Warning 	warning conditions
		LOG_NOTICE => 5, // Notice 	normal but significant condition
		LOG_INFO => 6, // Informational 	informational messages
		LOG_DEBUG => 7, // Debug 	debug-level messages
	);

	// number for number based comparison
	// should work with the defines only, this is just a make-it-sure step
	$level_ = $levels [ $level ];

	// in case WordPress debug log has a minimum level
	if ( defined ( '\WP_DEBUG_LEVEL' ) ) {
		$wp_level = $levels [ \WP_DEBUG_LEVEL ];
		if ( $level_ > $wp_level ) {
			return false;
		}
	}

	// ERR, CRIT, ALERT and EMERG
	if ( 3 >= $level_ ) {
		\wp_die( '<h1>Error:</h1>' . '<p>' . $message . '</p>' );
		exit;
	}

	$trace = debug_backtrace();
	$caller = $trace[1];
	$parent = $caller['function'];

	if (isset($caller['class']))
		$parent = $caller['class'] . '::' . $parent;

	if (isset($caller['namespace']))
		$parent = $caller['namespace'] . '::' . $parent;

	return error_log( "{$parent}: {$message}" );
}