'repeater', 'id' => 'cx-ui-repeater-id', 'name' => 'cx-ui-repeater-name', 'value' => array(), 'fields' => array(), 'label' => '', 'add_label' => 'Add Item', 'class' => '', 'ui_kit' => true, 'required' => false, 'title_field' => '', ); /** * Stored data to process it while renderinr row * * @var array */ public $data = array(); /** * Repeater instances counter * * @var integer */ public static $instance_id = 0; /** * Current onstance TMPL name * * @var string */ public $tmpl_name = ''; /** * Holder for templates to print it in bottom of customizer page * * @var string */ public static $customizer_tmpl_to_print = null; /** * Is tmpl scripts already printed in customizer * * @var boolean */ public static $customizer_tmpl_printed = false; /** * Child repeater instances * * @var array */ private $_childs = array(); /** * Check if we render template for JS * * @var boolean */ private $_is_js_row = false; /** * Init. * * @since 1.0.0 */ public function init() { $this->set_tmpl_data(); add_action( 'admin_footer', array( $this, 'print_js_template' ), 0 ); add_action( 'customize_controls_print_footer_scripts', array( $this, 'fix_customizer_tmpl' ), 9999 ); } /** * Retrun scripts dependencies list for current control. * * @return array */ public function get_script_depends() { return array( 'jquery-ui-sortable', 'wp-util' ); } /** * Get required attribute. * * @return string required attribute */ public function get_required() { if ( $this->settings['required'] ) { return 'required="required"'; } return ''; } /** * Render html UI_Repeater. * * @since 1.0.1 */ public function render() { $html = ''; $class = $this->settings['class']; $ui_kit = ! empty( $this->settings['ui_kit'] ) ? 'cx-ui-kit' : ''; $value = ! empty( $this->settings['value'] ) ? count( $this->settings['value'] ) : 0 ; $title_field = ! empty( $this->settings['title_field'] ) ? 'data-title-field="' . $this->settings['title_field'] . '"' : '' ; add_filter( 'cx_control/is_repeater', '__return_true' ); $html .= sprintf( '
Class ' . $ui_class_name . ' not exist!
'; } $ui_item = new $ui_class_name( $field ); if ( 'repeater' === $ui_item->settings['type'] && true === $this->_is_js_row ) { $this->_childs[] = $ui_item; } return $ui_item->render(); } /** * Get TMPL name for current repeater instance. * * @return string */ public function get_tmpl_name() { return $this->tmpl_name; } /** * Set current repeater instance ID * * @return void */ public function set_tmpl_data() { self::$instance_id++; $this->tmpl_name = sprintf( 'repeater-template-%s', self::$instance_id ); global $wp_customize; if ( isset( $wp_customize ) ) { self::$customizer_tmpl_to_print .= $this->get_js_template(); } } /** * Print JS template for current repeater instance * * @return void */ public function print_js_template() { echo $this->get_js_template(); if ( ! empty( $this->_childs ) ) { foreach ( $this->_childs as $child ) { echo $child->get_js_template(); } } } /** * Get JS template to print * * @return string */ public function get_js_template() { $this->_is_js_row = true; return sprintf( '', $this->get_tmpl_name(), $this->render_row( '{{{data.index}}}', '{{{data.widgetId}}}', array() ) ); } /** * Outputs JS templates on customizer page * * @return void */ public function fix_customizer_tmpl() { if ( true === self::$customizer_tmpl_printed ) { return; } self::$customizer_tmpl_printed = true; echo self::$customizer_tmpl_to_print; } } }