mirror of
https://ghproxy.net/https://github.com/AlxMedia/curver.git
synced 2025-08-28 15:08:39 +08:00
Initial commit
This commit is contained in:
commit
ee50200fe7
353 changed files with 78977 additions and 0 deletions
|
@ -0,0 +1,155 @@
|
|||
<?php
|
||||
/**
|
||||
* Handles the CSS-variables of fields.
|
||||
*
|
||||
* @package Kirki
|
||||
* @category Modules
|
||||
* @author Aristeides Stathopoulos
|
||||
* @copyright Copyright (c) 2017, Aristeides Stathopoulos
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
* @since 3.0.28
|
||||
*/
|
||||
|
||||
/**
|
||||
* The Kirki_Modules_CSS_Vars object.
|
||||
*
|
||||
* @since 3.0.28
|
||||
*/
|
||||
class Kirki_Modules_CSS_Vars {
|
||||
|
||||
/**
|
||||
* The object instance.
|
||||
*
|
||||
* @static
|
||||
* @access private
|
||||
* @since 3.0.28
|
||||
* @var object
|
||||
*/
|
||||
private static $instance;
|
||||
|
||||
/**
|
||||
* Fields with variables.
|
||||
*
|
||||
* @access private
|
||||
* @since 3.0.28
|
||||
* @var array
|
||||
*/
|
||||
private $fields = array();
|
||||
|
||||
/**
|
||||
* CSS-variables array [var=>val].
|
||||
*
|
||||
* @access private
|
||||
* @since 3.0.35
|
||||
* @var array
|
||||
*/
|
||||
private $vars = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @access protected
|
||||
* @since 3.0.28
|
||||
*/
|
||||
protected function __construct() {
|
||||
add_action( 'init', array( $this, 'populate_vars' ) );
|
||||
add_action( 'wp_head', array( $this, 'the_style' ), 999 );
|
||||
add_action( 'admin_head', array( $this, 'the_style' ), 999 );
|
||||
add_action( 'customize_preview_init', array( $this, 'postmessage' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an instance of this object.
|
||||
* Prevents duplicate instances which avoid artefacts and improves performance.
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @since 3.0.28
|
||||
* @return object
|
||||
*/
|
||||
public static function get_instance() {
|
||||
if ( ! self::$instance ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the $vars property of this object.
|
||||
*
|
||||
* @access public
|
||||
* @since 3.0.35
|
||||
* @return void
|
||||
*/
|
||||
public function populate_vars() {
|
||||
|
||||
// Get an array of all fields.
|
||||
$fields = Kirki::$fields;
|
||||
foreach ( $fields as $id => $args ) {
|
||||
if ( ! isset( $args['css_vars'] ) || empty( $args['css_vars'] ) ) {
|
||||
continue;
|
||||
}
|
||||
$val = Kirki_Values::get_value( $args['kirki_config'], $id );
|
||||
foreach ( $args['css_vars'] as $css_var ) {
|
||||
if ( isset( $css_var[2] ) && is_array( $val ) && isset( $val[ $css_var[2] ] ) ) {
|
||||
$this->vars[ $css_var[0] ] = str_replace( '$', $val[ $css_var[2] ], $css_var[1] );
|
||||
} else {
|
||||
$this->vars[ $css_var[0] ] = str_replace( '$', $val, $css_var[1] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add styles in <head>.
|
||||
*
|
||||
* @access public
|
||||
* @since 3.0.28
|
||||
* @return void
|
||||
*/
|
||||
public function the_style() {
|
||||
if ( empty( $this->vars ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo '<style id="kirki-css-vars">';
|
||||
echo ':root{';
|
||||
foreach ( $this->vars as $var => $val ) {
|
||||
echo esc_html( $var ) . ':' . esc_html( $val ) . ';';
|
||||
}
|
||||
echo '}';
|
||||
echo '</style>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of all the variables.
|
||||
*
|
||||
* @access public
|
||||
* @since 3.0.35
|
||||
* @return array
|
||||
*/
|
||||
public function get_vars() {
|
||||
return $this->vars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues the script that handles postMessage
|
||||
* and adds variables to it using the wp_localize_script function.
|
||||
* The rest is handled via JS.
|
||||
*
|
||||
* @access public
|
||||
* @since 3.0.28
|
||||
* @return void
|
||||
*/
|
||||
public function postmessage() {
|
||||
wp_enqueue_script( 'kirki_auto_css_vars', trailingslashit( Kirki::$url ) . 'modules/css-vars/script.js', array( 'jquery', 'customize-preview' ), KIRKI_VERSION, true );
|
||||
$fields = Kirki::$fields;
|
||||
$data = array();
|
||||
foreach ( $fields as $field ) {
|
||||
if ( isset( $field['transport'] ) && 'postMessage' === $field['transport'] && isset( $field['css_vars'] ) && ! empty( $field['css_vars'] ) ) {
|
||||
$data[] = $field;
|
||||
}
|
||||
}
|
||||
wp_localize_script( 'kirki_auto_css_vars', 'kirkiCssVarFields', $data );
|
||||
}
|
||||
}
|
82
functions/kirki/modules/css-vars/script.js
Normal file
82
functions/kirki/modules/css-vars/script.js
Normal file
|
@ -0,0 +1,82 @@
|
|||
/* global kirkiCssVarFields */
|
||||
var kirkiCssVars = {
|
||||
|
||||
/**
|
||||
* Get styles.
|
||||
*
|
||||
* @since 3.0.28
|
||||
* @returns {Object}
|
||||
*/
|
||||
getStyles: function() {
|
||||
var style = jQuery( '#kirki-css-vars' ),
|
||||
styles = style.html().replace( ':root{', '' ).replace( '}', '' ).split( ';' ),
|
||||
stylesObj = {};
|
||||
|
||||
// Format styles as a object we can then tweak.
|
||||
_.each( styles, function( style ) {
|
||||
style = style.split( ':' );
|
||||
if ( style[0] && style[1] ) {
|
||||
stylesObj[ style[0] ] = style[1];
|
||||
}
|
||||
} );
|
||||
return stylesObj;
|
||||
},
|
||||
|
||||
/**
|
||||
* Builds the styles from an object.
|
||||
*
|
||||
* @since 3.0.28
|
||||
* @param {Object} vars - The vars.
|
||||
* @returns {string}
|
||||
*/
|
||||
buildStyle: function( vars ) {
|
||||
var style = '';
|
||||
|
||||
_.each( vars, function( val, name ) {
|
||||
style += name + ':' + val + ';';
|
||||
} );
|
||||
return ':root{' + style + '}';
|
||||
}
|
||||
};
|
||||
|
||||
jQuery( document ).ready( function() {
|
||||
_.each( kirkiCssVarFields, function( field ) {
|
||||
wp.customize( field.settings, function( value ) {
|
||||
value.bind( function( newVal ) {
|
||||
var styles = kirkiCssVars.getStyles();
|
||||
|
||||
_.each( field.css_vars, function( cssVar ) {
|
||||
if ( 'object' === typeof newVal ) {
|
||||
if ( cssVar[2] && newVal[ cssVar[2] ] ) {
|
||||
styles[ cssVar[0] ] = cssVar[1].replace( '$', newVal[ cssVar[2] ] );
|
||||
}
|
||||
} else {
|
||||
styles[ cssVar[0] ] = cssVar[1].replace( '$', newVal );
|
||||
}
|
||||
} );
|
||||
jQuery( '#kirki-css-vars' ).html( kirkiCssVars.buildStyle( styles ) );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
|
||||
wp.customize.bind( 'preview-ready', function() {
|
||||
wp.customize.preview.bind( 'active', function() {
|
||||
_.each( kirkiCssVarFields, function( field ) {
|
||||
wp.customize( field.settings, function( value ) {
|
||||
var styles = kirkiCssVars.getStyles(),
|
||||
newVal = window.parent.wp.customize( value.id ).get();
|
||||
_.each( field.css_vars, function( cssVar ) {
|
||||
if ( 'object' === typeof newVal ) {
|
||||
if ( cssVar[2] && newVal[ cssVar[2] ] ) {
|
||||
styles[ cssVar[0] ] = cssVar[1].replace( '$', newVal[ cssVar[2] ] );
|
||||
}
|
||||
} else {
|
||||
styles[ cssVar[0] ] = cssVar[1].replace( '$', newVal );
|
||||
}
|
||||
} );
|
||||
jQuery( '#kirki-css-vars' ).html( kirkiCssVars.buildStyle( styles ) );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
} );
|
Loading…
Add table
Add a link
Reference in a new issue