Update to Kirki 4.2.0

This commit is contained in:
AlxMedia 2023-08-04 15:43:32 +02:00
parent 0185d24e05
commit 2880533a51
440 changed files with 6230 additions and 5211 deletions

View file

@ -0,0 +1,125 @@
<?php
/**
* The code editor control.
*
* Creates a code editor control.
*
* @package kirki-framework/control-code
* @license MIT (https://oss.ninja/mit?organization=Kirki%20Framework)
* @since 1.0.2
*/
namespace Kirki\Control;
use Kirki\Control\Base;
/**
* Slider control.
*
* @since 1.0.2
*/
class Code extends Base {
/**
* The control version.
*
* @since 1.0.2
* @access public
* @var string
*/
public static $control_ver = '1.0.2';
/**
* Customize control type.
*
* @since 1.0.2
* @var string
*/
public $type = 'code_editor';
/**
* Type of code that is being edited.
*
* @since 1.0.2
* @var string
*/
public $code_type = '';
/**
* Code editor settings.
*
* @see wp_enqueue_code_editor()
* @since 1.0.2
* @var array|false
*/
public $editor_settings = array();
/**
* Enqueue control related scripts/styles.
*
* @since 1.0.2
*/
public function enqueue() {
$this->editor_settings = wp_enqueue_code_editor(
array_merge(
array(
'type' => $this->code_type,
'codemirror' => array(
'indentUnit' => 2,
'tabSize' => 2,
),
),
$this->editor_settings
)
);
}
/**
* Refresh the parameters passed to the JavaScript via JSON.
*
* @since 1.0.2
*
* @see WP_Customize_Control::json()
*
* @return array Array of parameters passed to the JavaScript.
*/
public function json() {
$json = parent::json();
$json['editor_settings'] = $this->editor_settings;
$json['input_attrs'] = $this->input_attrs;
return $json;
}
/**
* Don't render the control content from PHP, as it's rendered via JS on load.
*
* @since 1.0.2
*/
public function render_content() {}
/**
* Render a JS template for control display.
*
* @since 1.0.2
*/
public function content_template() {
?>
<# var elementIdPrefix = 'el' + String( Math.random() ); #>
<# if ( data.label ) { #>
<label for="{{ elementIdPrefix }}_editor" class="customize-control-title">
{{ data.label }}
</label>
<# } #>
<# if ( data.description ) { #>
<span class="description customize-control-description">{{{ data.description }}}</span>
<# } #>
<div class="customize-control-notifications-container"></div>
<textarea id="{{ elementIdPrefix }}_editor"
<# _.each( _.extend( { 'class': 'code' }, data.input_attrs ), function( value, key ) { #>
{{{ key }}}="{{ value }}"
<# }); #>
></textarea>
<?php
}
}

View file

@ -0,0 +1,140 @@
<?php
/**
* Override field methods
*
* @package kirki-framework/control-code
* @copyright Copyright (c) 2023, Themeum
* @license https://opensource.org/licenses/MIT
* @since 1.0
*/
namespace Kirki\Field;
use Kirki\Field;
/**
* Field overrides.
*/
class Code extends Field {
/**
* The field type.
*
* @access public
* @since 1.0
* @var string
*/
public $type = 'kirki-code';
/**
* The control class-name.
*
* @access protected
* @since 0.1
* @var string
*/
protected $control_class = '\Kirki\Control\Code';
/**
* Filter arguments before creating the setting.
*
* @access public
* @since 0.1
* @param array $args The field arguments.
* @param WP_Customize_Manager $wp_customize The customizer instance.
* @return array
*/
public function filter_setting_args( $args, $wp_customize ) {
if ( $args['settings'] === $this->args['settings'] ) {
$args = parent::filter_setting_args( $args, $wp_customize );
// Set the sanitize-callback if none is defined.
if ( ! isset( $args['sanitize_callback'] ) || ! $args['sanitize_callback'] ) {
$args['sanitize_callback'] = function( $value ) {
/**
* Code fields should not be filtered by default.
* Their values usually contain CSS/JS and it it the responsibility
* of the theme/plugin that registers this field
* to properly apply any necessary sanitization.
*/
return $value;
};
}
}
return $args;
}
/**
* Filter arguments before creating the control.
*
* @access public
* @since 0.1
* @param array $args The field arguments.
* @param WP_Customize_Manager $wp_customize The customizer instance.
* @return array
*/
public function filter_control_args( $args, $wp_customize ) {
if ( $args['settings'] === $this->args['settings'] ) {
$args = parent::filter_control_args( $args, $wp_customize );
$args['type'] = 'code_editor';
$args['input_attrs'] = [
'aria-describedby' => 'kirki-code editor-keyboard-trap-help-1 editor-keyboard-trap-help-2 editor-keyboard-trap-help-3 editor-keyboard-trap-help-4',
];
if ( ! isset( $args['choices']['language'] ) ) {
return;
}
$language = $args['choices']['language'];
switch ( $language ) {
case 'json':
case 'xml':
$language = 'application/' . $language;
break;
case 'http':
$language = 'message/' . $language;
break;
case 'js':
case 'javascript':
$language = 'text/javascript';
break;
case 'txt':
$language = 'text/plain';
break;
case 'css':
case 'jsx':
case 'html':
$language = 'text/' . $language;
break;
default:
$language = ( 'js' === $language ) ? 'javascript' : $language;
$language = ( 'htm' === $language ) ? 'html' : $language;
$language = ( 'yml' === $language ) ? 'yaml' : $language;
$language = 'text/x-' . $language;
break;
}
if ( ! isset( $args['editor_settings'] ) ) {
$args['editor_settings'] = [];
}
if ( ! isset( $args['editor_settings']['codemirror'] ) ) {
$args['editor_settings']['codemirror'] = [];
}
if ( ! isset( $args['editor_settings']['codemirror']['mode'] ) ) {
$args['editor_settings']['codemirror']['mode'] = $language;
}
if ( 'text/x-scss' === $args['editor_settings']['codemirror']['mode'] ) {
$args['editor_settings']['codemirror'] = array_merge(
$args['editor_settings']['codemirror'],
[
'lint' => false,
'autoCloseBrackets' => true,
'matchBrackets' => true,
]
);
}
}
return $args;
}
}