Skip to content

Instantly share code, notes, and snippets.

@igmoweb
Created September 1, 2015 17:51
Show Gist options
  • Save igmoweb/f86804391061b0e88960 to your computer and use it in GitHub Desktop.
Save igmoweb/f86804391061b0e88960 to your computer and use it in GitHub Desktop.
Repeater Kirki Hooks. First attempt in a good direction, maybe
<?php
add_action( 'customize_register', 'kirki_hooks_customize_register' );
function kirki_hooks_customize_register( $wp_customize ) {
class Kirki_Customize_Control_Repeater_Setting extends WP_Customize_Setting {
/**
* In Repeater, one setting is one row full of settings
* fields save all the fields and their types
*
* @var array
*/
public $fields = array();
public function __construct( $manager, $id, $args = array() ) {
parent::__construct( $manager, $id, $args );
}
public function get_fields() {
return $this->fields;
}
}
class Kirki_Customize_Control_Repeater_Control extends WP_Customize_Control {
public $type = 'jt-select';
public function enqueue() {
wp_enqueue_script( 'jt-customize-controls', plugin_dir_url( __FILE__ ) . 'kirki-hooks.js', array( 'jquery' ) );
}
public function get_fields( $setting_key = 'default' ) {
if ( ! isset( $this->settings[ $setting_key ] ) )
return array();
return $this->settings[ $setting_key ]->get_fields();
}
public function to_json() {
// Call parent to_json() method to get the core defaults like "label", "description", etc.
parent::to_json();
$settings_keys = wp_list_pluck( $this->settings, 'id' );
$this->json['value'] = array();
foreach ( $this->settings as $key => $setting ) {
$this->json['rows'][ $key ] = $this->get_fields( $key );
$this->json['value'][ $key ] = $this->value( $key );
$this->json['link'][ $key ] = $this->get_link( $key );
}
// The control choices.
$this->json['choices'] = $this->choices;
}
public function content_template() {
?>
<#
if ( ! data.rows ) {
return;
}
var rowFields, fields;
for ( var i = 0; i < data.rows.length; i++ ) {
rowFields = data.rows[ i ];
for ( var j = 0; j < rowFields.length; j++ ) {
field = rowFields[j];
link = data.link[i];
value = data.value[i];
console.log(data);
console.log(link);
#>
<label>
<# if ( data.label ) { #>
<span class="customize-control-title">{{ data.label }}</span>
<# } #>
<# if ( data.description ) { #>
<span class="description customize-control-description">{{{ data.description }}}</span>
<# } #>
<input type="text" {{{ link }}} value="{{{ value }}}" />
</label>
<#
}
#>
<hr/>
<#
}
#>
<?php
}
}
// Register the control type.
$wp_customize->register_control_type( 'Kirki_Customize_Control_Repeater_Control' );
$wp_customize->add_section( 'themename_test', array(
'title' => __( 'Test', 'themename' ),
'priority' => 35,
) );
$wp_customize->add_setting( new Kirki_Customize_Control_Repeater_Setting( $wp_customize, 'setting_1', array(
'fields' => array(
array(
'id' => 'subsetting_1',
'type' => 'text',
'default' => 'subsetting1'
),
array(
'id' => 'subsetting_1',
'type' => 'text',
'default' => 'subsetting2'
)
)
) ) );
$wp_customize->add_setting( new Kirki_Customize_Control_Repeater_Setting( $wp_customize, 'setting_2', array(
'fields' => array(
array(
'id' => 'subsetting_1',
'type' => 'text',
'default' => 'subsetting1'
),
array(
'id' => 'subsetting_1',
'type' => 'text',
'default' => 'subsetting2'
)
)
) ) );
$settings = array( 'setting_1', 'setting_2' );
$wp_customize->add_control( new Kirki_Customize_Control_Repeater_Control( $wp_customize, 'control_id', array(
'section' => 'themename_test', // Required, core or custom.
'label' => __( 'Setting Test' ),
'settings' => $settings
) ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment