| 1 |
<?php |
| 2 |
/** |
| 3 |
* Selection Lite |
| 4 |
* Carefully selected Elementor addons bundle, for building the most awesome websites |
| 5 |
* |
| 6 |
* @encoding UTF-8 |
| 7 |
* @version 1.12 |
| 8 |
* @copyright (C) 2018-2024 Merkulove ( https://merkulov.design/ ). All rights reserved. |
| 9 |
* @license GPLv3 |
| 10 |
* @contributors merkulove, vladcherviakov, phoenixmkua, podolianochka, viktorialev01 |
| 11 |
* @support help@merkulov.design |
| 12 |
**/ |
| 13 |
|
| 14 |
namespace Merkulove\SelectionLite\Unity; |
| 15 |
|
| 16 |
/** Exit if accessed directly. */ |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
header( 'Status: 403 Forbidden' ); |
| 19 |
header( 'HTTP/1.1 403 Forbidden' ); |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* SINGLETON: Class used to render UI controls. |
| 25 |
* |
| 26 |
* @since 1.0 |
| 27 |
* |
| 28 |
**/ |
| 29 |
final class UI { |
| 30 |
|
| 31 |
/** |
| 32 |
* The one true UI. |
| 33 |
* |
| 34 |
* @since 1.0 |
| 35 |
* @var UI |
| 36 |
**/ |
| 37 |
private static $instance; |
| 38 |
|
| 39 |
/** |
| 40 |
* Render snackbar. |
| 41 |
* |
| 42 |
* @see https://material-components.github.io/material-components-web-catalog/#/component/snackbar |
| 43 |
* @access public |
| 44 |
* |
| 45 |
* @param $message - HTML message to show. |
| 46 |
* @param string $design - Snackbar message design (info, error, warning) |
| 47 |
* @param int $timeout - Auto-close timeout (-1 or 4000-10000) |
| 48 |
* @param bool $closeable - Can a user close? |
| 49 |
* @param array $buttons - Additional buttons array( [ 'caption', 'link' ] ) |
| 50 |
* @param string $class_name - CSS class name |
| 51 |
* |
| 52 |
* @since 1.0 |
| 53 |
* @access public |
| 54 |
* |
| 55 |
* @return void |
| 56 |
**/ |
| 57 |
public function render_snackbar( $message, $design = '', $timeout = 5000, $closeable = true, $buttons = [], $class_name = '' ) { |
| 58 |
?> |
| 59 |
<div class="mdc-snackbar <?php echo ( $design === '' ) ? 'mdc-info' : 'mdc-' . esc_attr( $design ); ?> <?php echo esc_attr( $class_name ); ?>" data-timeout="<?php echo esc_attr( $timeout ); ?>"> |
| 60 |
<div class="mdc-snackbar__surface"> |
| 61 |
<div class="mdc-snackbar__label" role="status" |
| 62 |
aria-live="polite"><?php echo wp_kses_post( $message ); ?></div> |
| 63 |
<div class="mdc-snackbar__actions"> |
| 64 |
<?php foreach ( $buttons as $btn) : ?> |
| 65 |
<button class="mdc-button mdc-snackbar__action" type="button" onclick="window.open( '<?php echo esc_attr( $btn[ 'link' ] ); ?>', '_blank' )" title="<?php echo esc_attr( $btn[ 'caption' ] ); ?>"><?php echo esc_html( $btn[ 'caption' ] ); ?></button> |
| 66 |
<?php endforeach; ?> |
| 67 |
<?php if ( $closeable ) : ?> |
| 68 |
<button class="mdc-icon-button mdc-snackbar__dismiss material-icons" title="<?php esc_html_e( 'Dismiss' ); ?>" type="button">close</button> |
| 69 |
<?php endif; ?> |
| 70 |
</div> |
| 71 |
</div> |
| 72 |
</div> |
| 73 |
<?php |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Render select field. |
| 78 |
* |
| 79 |
* @param array $options - Options for select. Required. |
| 80 |
* @param string $selected - Selected value. Optional. |
| 81 |
* @param string $label - Label for select. Optional. |
| 82 |
* @param string $helper_text - Text after select. Optional. |
| 83 |
* @param array $attributes - Additional attributes for select: id, name, class. Optional. |
| 84 |
* |
| 85 |
* @since 1.0.0 |
| 86 |
* @access public |
| 87 |
* |
| 88 |
* @return void |
| 89 |
**/ |
| 90 |
public function render_select( $options, $selected = '', $label = '', $helper_text = '', $attributes = [] ) { |
| 91 |
|
| 92 |
if ( ! count( $options ) ) { return; } |
| 93 |
|
| 94 |
/** Prepare html attributes. */ |
| 95 |
$name = isset( $attributes['name'] ) ? $attributes['name'] : ''; |
| 96 |
$id = isset( $attributes['id'] ) ? $attributes['id'] : 'mdp-' . uniqid( '', true ); |
| 97 |
|
| 98 |
$class = isset( $attributes['class'] ) ? $attributes['class'] : ''; |
| 99 |
$class = 'mdc-select mdc-select-width mdc-select--outlined ' . $class; |
| 100 |
$class = trim( $class ); |
| 101 |
|
| 102 |
/** Check selected option. If we don't have it, select first one. */ |
| 103 |
if ( ! array_key_exists( $selected, $options ) ) { |
| 104 |
|
| 105 |
reset( $options ); |
| 106 |
|
| 107 |
/** @noinspection CallableParameterUseCaseInTypeContextInspection */ |
| 108 |
$selected = key( $options ); |
| 109 |
|
| 110 |
} |
| 111 |
?> |
| 112 |
|
| 113 |
<div class="<?php echo esc_attr( $class ); ?>"> |
| 114 |
<input type="hidden" |
| 115 |
<?php echo ( $id ) ? 'id="' . esc_attr( $id ) . '"' : ''; ?> |
| 116 |
<?php echo ( $name ) ? 'name="' . esc_attr( $name ) . '"' : ''; ?> |
| 117 |
value="<?php echo esc_attr( $selected ); ?>" |
| 118 |
> |
| 119 |
|
| 120 |
<div class="mdc-select__anchor mdc-select-width"> |
| 121 |
<i class="mdc-select__dropdown-icon"></i> |
| 122 |
<div id="<?php echo esc_attr( $id ) ?>-text" class="mdc-select__selected-text" aria-labelledby="outlined-select-label"><?php echo esc_html( $options[$selected] ); ?></div> |
| 123 |
<div class="mdc-notched-outline"> |
| 124 |
<div class="mdc-notched-outline__leading"></div> |
| 125 |
<div class="mdc-notched-outline__notch"> |
| 126 |
<?php if ( $label ) : ?> |
| 127 |
<span id="<?php echo esc_attr( $id ) ?>-label" class="mdc-floating-label"><?php echo esc_html( $label ); ?></span> |
| 128 |
<?php endif; ?> |
| 129 |
</div> |
| 130 |
<div class="mdc-notched-outline__trailing"></div> |
| 131 |
</div> |
| 132 |
</div> |
| 133 |
|
| 134 |
<div class="mdc-select__menu mdc-menu mdc-menu-surface mdc-select-width" role="listbox"> |
| 135 |
<ul class="mdc-list"> |
| 136 |
<?php foreach ( $options as $key => $value ) : ?> |
| 137 |
<?php $selected_class = ( $key === $selected ) ? 'mdc-list-item--selected' : ''; ?> |
| 138 |
<li class="mdc-list-item <?php echo esc_attr( $selected_class ); ?>" data-value="<?php echo esc_attr( $key ) ?>" role="option"><?php echo wp_kses_post( $value ) ?></li> |
| 139 |
<?php endforeach; ?> |
| 140 |
</ul> |
| 141 |
</div> |
| 142 |
</div> |
| 143 |
|
| 144 |
<?php if ( $helper_text ) : ?> |
| 145 |
<div class="mdc-text-field-helper-line"> |
| 146 |
<div class="mdc-select-helper-text mdc-select-helper-text--persistent" aria-hidden="true"><?php echo wp_kses_post( $helper_text ); ?></div> |
| 147 |
</div> |
| 148 |
<?php endif; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Render the Switch |
| 154 |
* |
| 155 |
* @param string $value - Switch value on/off |
| 156 |
* @param string $label - Switch label |
| 157 |
* @param string $helper_text - Switch helper text |
| 158 |
* @param array $attributes - Additional attributes for the switch: id, name, class. Optional. |
| 159 |
* |
| 160 |
* @since 1.0 |
| 161 |
* @access public |
| 162 |
**/ |
| 163 |
public function render_switcher( $value, $label = '', $helper_text = '', $attributes = [] ) { |
| 164 |
|
| 165 |
/** Prepare html attributes. */ |
| 166 |
$id = isset( $attributes['id'] ) ? $attributes['id'] : ''; |
| 167 |
$name = isset( $attributes['name'] ) ? $attributes['name'] : ''; |
| 168 |
|
| 169 |
$class = isset( $attributes['class'] ) ? trim( $attributes['class'] ) : ''; |
| 170 |
$class = ' mdc-switch ' . $class; |
| 171 |
if ( $value === 'on' ) { |
| 172 |
$class .= ' mdc-switch--checked '; |
| 173 |
} |
| 174 |
$class = trim( $class ); |
| 175 |
?> |
| 176 |
|
| 177 |
<div <?php echo ( $class ) ? 'class="' . esc_attr( $class ) . '"' : ''; ?>> |
| 178 |
<div class="mdc-switch__track"></div> |
| 179 |
<div class="mdc-switch__thumb-underlay"> |
| 180 |
<div class="mdc-switch__thumb"> |
| 181 |
<!--suppress HtmlFormInputWithoutLabel --> |
| 182 |
<input |
| 183 |
<?php echo ( $id ) ? 'id="' . esc_attr( $id . '-i' ) . '"' : ''; ?> |
| 184 |
<?php echo ( $name ) ? 'name="' . esc_attr( $name ) . '"' : ''; ?> |
| 185 |
type='hidden' |
| 186 |
value='off'> |
| 187 |
<!--suppress HtmlFormInputWithoutLabel --> |
| 188 |
<input |
| 189 |
<?php echo ( $id ) ? 'id="' . esc_attr( $id ) . '"' : ''; ?> |
| 190 |
<?php echo ( $name ) ? 'name="' . esc_attr( $name ) . '"' : ''; ?> |
| 191 |
class="mdc-switch__native-control" type="checkbox" role="switch" <?php echo ( $value === 'on' ) ? 'checked' : ''; ?>> |
| 192 |
</div> |
| 193 |
</div> |
| 194 |
</div> |
| 195 |
<label <?php echo isset( $attributes['id'] ) ? 'for="'. esc_attr( $attributes['id'] ) .'"' : '' ?>class="mdc-switch-label"><?php echo esc_html( $label ) ?></label> |
| 196 |
|
| 197 |
<?php if ( $helper_text ) : ?> |
| 198 |
<div class="mdc-text-field-helper-line"> |
| 199 |
<div class="mdc-switcher-helper mdc-text-field-helper-text mdc-text-field-helper-text--persistent"><?php echo wp_kses_post( $helper_text ); ?></div> |
| 200 |
</div> |
| 201 |
<?php endif; |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Main UI Instance. |
| 207 |
* Insures that only one instance of UI exists in memory at any one time. |
| 208 |
* |
| 209 |
* @static |
| 210 |
* @since 1.0 |
| 211 |
* @access public |
| 212 |
* |
| 213 |
* @return UI |
| 214 |
**/ |
| 215 |
public static function get_instance() { |
| 216 |
|
| 217 |
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) { |
| 218 |
|
| 219 |
self::$instance = new self; |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
return self::$instance; |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
} |
| 228 |
|