Current File : /home/getxxhzo/tinyrnd.com/wp-content/themes/flyweb/inc/classes/class-css-generator.php |
<?php
if ( ! class_exists( 'ReduxFrameworkInstances' ) ) {
return;
}
if(!function_exists('flyweb_hex_to_rgba')){
function flyweb_hex_to_rgba($hex,$opacity = 1) {
$hex = str_replace("#",null, $hex);
$color = array();
if(strlen($hex) == 3) {
$color['r'] = hexdec(substr($hex,0,1).substr($hex,0,1));
$color['g'] = hexdec(substr($hex,1,1).substr($hex,1,1));
$color['b'] = hexdec(substr($hex,2,1).substr($hex,2,1));
$color['a'] = $opacity;
}
else if(strlen($hex) == 6) {
$color['r'] = hexdec(substr($hex, 0, 2));
$color['g'] = hexdec(substr($hex, 2, 2));
$color['b'] = hexdec(substr($hex, 4, 2));
$color['a'] = $opacity;
}
$color = "rgba(".implode(', ', $color).")";
return $color;
}
}
class CSS_Generator {
/**
* scssc class instance
*
* @access protected
* @var scssc
*/
protected $scssc = null;
/**
* ReduxFramework class instance
*
* @access protected
* @var ReduxFramework
*/
protected $redux = null;
/**
* Debug mode is turn on or not
*
* @access protected
* @var boolean
*/
protected $dev_mode = true;
/**
* opt_name of ReduxFramework
*
* @access protected
* @var string
*/
protected $opt_name = '';
/**
* Constructor
*/
function __construct() {
$this->opt_name = flyweb_get_opt_name();
if ( empty( $this->opt_name ) ) {
return;
}
$this->dev_mode = flyweb_get_opt( 'dev_mode', '0' ) === '1' ? true : false;
add_filter( 'flyweb_scssc_on', '__return_true' );
add_action( 'init', array( $this, 'init' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ), 20 );
}
/**
* init hook - 10
*/
function init() {
if ( ! class_exists( 'scssc' ) ) {
return;
}
$this->redux = ReduxFrameworkInstances::get_instance( $this->opt_name );
if ( empty( $this->redux ) || ! $this->redux instanceof ReduxFramework ) {
return;
}
add_action( 'wp', array( $this, 'generate_with_dev_mode' ) );
add_action( "redux/options/{$this->opt_name}/saved", function () {
$this->generate_file();
} );
}
function generate_with_dev_mode() {
if ( $this->dev_mode === true ) {
$this->generate_file();
}
}
/**
* Generate options and css files
*/
function generate_file() {
$scss_dir = get_template_directory() . '/css/scss/';
$css_dir = get_template_directory() . '/css/';
$this->scssc = new scssc();
$this->scssc->setImportPaths( $scss_dir );
$_options = $scss_dir . 'variables.scss';
$this->redux->filesystem->execute( 'put_contents', $_options, array(
'content' => preg_replace( "/(?<=[^\r]|^)\n/", "\r\n", $this->options_output() )
) );
$css_file = $css_dir . 'styles.css';
$this->scssc->setFormatter( 'scss_formatter' );
$this->redux->filesystem->execute( 'put_contents', $css_file, array(
'content' => preg_replace( "/(?<=[^\r]|^)\n/", "\r\n", $this->scssc->compile( '@import "theme.scss"' ) )
) );
}
/**
* Output options to _variables.scss
*
* @access protected
* @return string
*/
protected function options_output() {
ob_start();
$primary_color = flyweb_get_opt( 'primary_color', '#ff497c' );
if ( ! flyweb_is_valid_color( $primary_color ) ) {
$primary_color = '#ff497c';
}
printf( '$primary_color: %s;', esc_attr( $primary_color ) );
$secondary_color = flyweb_get_opt( 'secondary_color', '#1f2732' );
if ( ! flyweb_is_valid_color( $secondary_color ) ) {
$secondary_color = '#1f2732';
}
printf( '$secondary_color: %s;', esc_attr( $secondary_color ) );
$body_text_color = flyweb_get_opt( 'body_text_color', '#42545e' );
if ( ! flyweb_is_valid_color( $body_text_color ) ) {
$body_text_color = '#42545e';
}
printf( '$body_text_color: %s;', esc_attr( $body_text_color ) );
return ob_get_clean();
}
/**
* Hooked wp_enqueue_scripts - 20
* Make sure that the handle is enqueued from earlier wp_enqueue_scripts hook.
*/
function enqueue() {
$css = $this->inline_css();
if ( ! empty( $css ) ) {
wp_add_inline_style( 'flyweb-styles', $this->dev_mode ? $css : flyweb_css_minifier( $css ) );
}
}
/**
* Generate inline css based on theme options
*/
protected function inline_css() {
ob_start(); ?>
<?php
/* Custom Css */
$custom_css = flyweb_get_opt( 'site_css' );
if ( ! empty( $custom_css ) ) {
echo esc_attr( $custom_css );
}
return ob_get_clean();
}
}
new CSS_Generator();