Radio_Image.php
98 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Customizer Control: radio-image. |
| 4 | * |
| 5 | * @package kirki-framework/control-radio |
| 6 | * @copyright Copyright (c) 2023, Themeum |
| 7 | * @license https://opensource.org/licenses/MIT |
| 8 | * @since 1.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki\Control; |
| 12 | |
| 13 | use Kirki\Control\Base; |
| 14 | use Kirki\URL; |
| 15 | |
| 16 | // Exit if accessed directly. |
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | exit; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Radio Image control (modified radio). |
| 23 | */ |
| 24 | class Radio_Image extends Base { |
| 25 | |
| 26 | /** |
| 27 | * The control type. |
| 28 | * |
| 29 | * @access public |
| 30 | * @var string |
| 31 | */ |
| 32 | public $type = 'kirki-radio-image'; |
| 33 | |
| 34 | /** |
| 35 | * Enqueue control related scripts/styles. |
| 36 | * |
| 37 | * @access public |
| 38 | */ |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Refresh the parameters passed to the JavaScript via JSON. |
| 43 | * |
| 44 | * @see WP_Customize_Control::to_json() |
| 45 | * |
| 46 | * @access public |
| 47 | * @since 1.0 |
| 48 | * @return void |
| 49 | */ |
| 50 | public function to_json() { |
| 51 | parent::to_json(); |
| 52 | foreach ( $this->input_attrs as $attr => $value ) { |
| 53 | if ( 'style' !== $attr ) { |
| 54 | $this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" '; |
| 55 | continue; |
| 56 | } |
| 57 | $this->json['labelStyle'] = 'style="' . esc_attr( $value ) . '" '; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * An Underscore (JS) template for this control's content (but not its container). |
| 63 | * |
| 64 | * Class variables for this control class are available in the `data` JS object; |
| 65 | * export custom variables by overriding {@see WP_Customize_Control::to_json()}. |
| 66 | * |
| 67 | * @see WP_Customize_Control::print_template() |
| 68 | * |
| 69 | * @access protected |
| 70 | * @since 1.0 |
| 71 | * @return void |
| 72 | */ |
| 73 | protected function content_template() { |
| 74 | ?> |
| 75 | <label class="customizer-text"> |
| 76 | <# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #> |
| 77 | <# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #> |
| 78 | </label> |
| 79 | <div id="input_{{ data.id }}" class="image"> |
| 80 | <# for ( key in data.choices ) { #> |
| 81 | <# dataAlt = ( _.isObject( data.choices[ key ] ) && ! _.isUndefined( data.choices[ key ].alt ) ) ? data.choices[ key ].alt : '' #> |
| 82 | <input {{{ data.inputAttrs }}} class="image-select" type="radio" value="{{ key }}" name="_customize-radio-{{ data.id }}" id="{{ data.id }}{{ key }}" {{{ data.link }}}<# if ( data.value === key ) { #> checked="checked"<# } #> data-alt="{{ dataAlt }}"> |
| 83 | <label for="{{ data.id }}{{ key }}" {{{ data.labelStyle }}} class="{{{ data.id + key }}}"> |
| 84 | <# if ( _.isObject( data.choices[ key ] ) ) { #> |
| 85 | <img src="{{ data.choices[ key ].src }}" alt="{{ data.choices[ key ].alt }}"> |
| 86 | <span class="image-label"><span class="inner">{{ data.choices[ key ].alt }}</span></span> |
| 87 | <# } else { #> |
| 88 | <img src="{{ data.choices[ key ] }}"> |
| 89 | <# } #> |
| 90 | <span class="image-clickable"></span> |
| 91 | </label> |
| 92 | </input> |
| 93 | <# } #> |
| 94 | </div> |
| 95 | <?php |
| 96 | } |
| 97 | } |
| 98 |