class-kirki-controls-sortable-control.php (4398B)
1 <?php 2 /** 3 * Customizer Control: sortable. 4 * 5 * @package Kirki 6 * @subpackage Controls 7 * @copyright Copyright (c) 2016, Aristeides Stathopoulos 8 * @license http://opensource.org/licenses/https://opensource.org/licenses/MIT 9 * @since 1.0 10 */ 11 12 // Exit if accessed directly. 13 if ( ! defined( 'ABSPATH' ) ) { 14 exit; 15 } 16 17 if ( ! class_exists( 'Kirki_Controls_Sortable_Control' ) ) { 18 19 /** 20 * Sortable control (uses checkboxes). 21 */ 22 class Kirki_Controls_Sortable_Control extends Kirki_Customize_Control { 23 24 /** 25 * The control type. 26 * 27 * @access public 28 * @var string 29 */ 30 public $type = 'kirki-sortable'; 31 32 /** 33 * Constructor. 34 * Supplied `$args` override class property defaults. 35 * If `$args['settings']` is not defined, use the $id as the setting ID. 36 * 37 * @param WP_Customize_Manager $manager Customizer bootstrap instance. 38 * @param string $id Control ID. 39 * @param array $args {@see WP_Customize_Control::__construct}. 40 */ 41 public function __construct( $manager, $id, $args = array() ) { 42 parent::__construct( $manager, $id, $args ); 43 add_filter( 'customize_sanitize_' . $id, array( $this, 'customize_sanitize' ) ); 44 } 45 46 /** 47 * Unserialize the setting before saving on DB. 48 * 49 * @param string $value Serialized settings. 50 * @return array 51 */ 52 public function customize_sanitize( $value ) { 53 $value = maybe_unserialize( $value ); 54 return $value; 55 } 56 57 /** 58 * Enqueue control related scripts/styles. 59 * 60 * @access public 61 */ 62 public function enqueue() { 63 wp_enqueue_script( 'kirki-sortable' ); 64 } 65 66 /** 67 * Refresh the parameters passed to the JavaScript via JSON. 68 * 69 * @access public 70 */ 71 public function to_json() { 72 parent::to_json(); 73 74 $this->json['choicesLength'] = 0; 75 if ( is_array( $this->choices ) && count( $this->choices ) ) { 76 $this->json['choicesLength'] = count( $this->choices ); 77 } 78 79 $values = $this->value() == '' ? array_keys( $this->choices ) : $this->value(); 80 $filtered_values = array(); 81 if ( is_array( $values ) && ! empty( $values ) ) { 82 foreach ( $values as $key => $value ) { 83 if ( array_key_exists( $value, $this->choices ) ) { 84 $filtered_values[ $key ] = $value; 85 } 86 } 87 } 88 89 $this->json['filteredValues'] = $filtered_values; 90 91 $this->json['invisibleKeys'] = array_diff( array_keys( $this->choices ), $filtered_values ); 92 93 $this->json['inputAttrs'] = maybe_serialize( $this->input_attrs() ); 94 95 } 96 97 /** 98 * An Underscore (JS) template for this control's content (but not its container). 99 * 100 * Class variables for this control class are available in the `data` JS object; 101 * export custom variables by overriding {@see Kirki_Customize_Control::to_json()}. 102 * 103 * @see WP_Customize_Control::print_template() 104 * 105 * @access protected 106 */ 107 protected function content_template() { 108 ?> 109 <# if ( ! data.choicesLength ) return; #> 110 111 <# if ( data.tooltip ) { #> 112 <a href="#" class="tooltip hint--left" data-hint="{{ data.tooltip }}"><span class='dashicons dashicons-info'></span></a> 113 <# } #> 114 115 <label class='kirki-sortable'> 116 <span class="customize-control-title"> 117 {{{ data.label }}} 118 </span> 119 <# if ( data.description ) { #> 120 <span class="description customize-control-description">{{{ data.description }}}</span> 121 <# } #> 122 123 <ul class="sortable"> 124 <# for ( i in data.filteredValues ) { #> 125 <# if ( data.filteredValues.hasOwnProperty( i ) ) { #> 126 <li {{{ data.inputAttrs }}} class='kirki-sortable-item' data-value='{{ data.filteredValues[i] }}'> 127 <i class='dashicons dashicons-menu'></i> 128 <i class="dashicons dashicons-visibility visibility"></i> 129 {{{ data.choices[ data.filteredValues[i] ] }}} 130 </li> 131 <# } #> 132 <# } #> 133 134 <# for ( i in data.invisibleKeys ) { #> 135 <# if ( data.invisibleKeys.hasOwnProperty( i ) ) { #> 136 <li {{{ data.inputAttrs }}} class='kirki-sortable-item invisible' data-value='{{ data.invisibleKeys[i] }}'> 137 <i class='dashicons dashicons-menu'></i> 138 <i class="dashicons dashicons-visibility visibility"></i> 139 {{{ data.choices[ data.invisibleKeys[i] ] }}} 140 </li> 141 <# } #> 142 <# } #> 143 </ul> 144 145 <div style='clear: both'></div> 146 <input type="hidden" {{ data.link }} value="" {{ data.inputAttrs }}/> 147 </label> 148 149 <?php 150 } 151 } 152 }