| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. |
| 2 |
/** |
| 3 |
* |
| 4 |
* Field: dimensions |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
* @version 1.0.0 |
| 8 |
* |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'ADMINIFY_Field_dimensions' ) ) { |
| 11 |
class ADMINIFY_Field_dimensions extends ADMINIFY_Fields { |
| 12 |
|
| 13 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 14 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 15 |
} |
| 16 |
|
| 17 |
public function render() { |
| 18 |
|
| 19 |
$args = wp_parse_args( $this->field, array( |
| 20 |
'width_icon' => '<i class="fas fa-arrows-alt-h"></i>', |
| 21 |
'height_icon' => '<i class="fas fa-arrows-alt-v"></i>', |
| 22 |
'width_placeholder' => esc_html__( 'width', 'adminify' ), |
| 23 |
'height_placeholder' => esc_html__( 'height', 'adminify' ), |
| 24 |
'width' => true, |
| 25 |
'height' => true, |
| 26 |
'unit' => true, |
| 27 |
'show_units' => true, |
| 28 |
'units' => array( 'px', '%', 'em' ) |
| 29 |
) ); |
| 30 |
|
| 31 |
$default_values = array( |
| 32 |
'width' => '', |
| 33 |
'height' => '', |
| 34 |
'unit' => 'px', |
| 35 |
); |
| 36 |
|
| 37 |
$value = wp_parse_args( $this->value, $default_values ); |
| 38 |
$unit = ( count( $args['units'] ) === 1 && ! empty( $args['unit'] ) ) ? $args['units'][0] : ''; |
| 39 |
$is_unit = ( ! empty( $unit ) ) ? ' adminify--is-unit' : ''; |
| 40 |
|
| 41 |
echo $this->field_before(); |
| 42 |
|
| 43 |
echo '<div class="adminify--inputs" data-depend-id="'. esc_attr( $this->field['id'] ) .'">'; |
| 44 |
|
| 45 |
if ( ! empty( $args['width'] ) ) { |
| 46 |
$placeholder = ( ! empty( $args['width_placeholder'] ) ) ? ' placeholder="'. esc_attr( $args['width_placeholder'] ) .'"' : ''; |
| 47 |
echo '<div class="adminify--input">'; |
| 48 |
echo ( ! empty( $args['width_icon'] ) ) ? '<span class="adminify--label adminify--icon">'. $args['width_icon'] .'</span>' : ''; |
| 49 |
echo '<input type="number" name="'. esc_attr( $this->field_name( '[width]' ) ) .'" value="'. esc_attr( $value['width'] ) .'"'. $placeholder .' class="adminify-input-number'. esc_attr( $is_unit ) .'" step="any" />'; |
| 50 |
echo ( ! empty( $unit ) ) ? '<span class="adminify--label adminify--unit">'. esc_attr( $args['units'][0] ) .'</span>' : ''; |
| 51 |
echo '</div>'; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! empty( $args['height'] ) ) { |
| 55 |
$placeholder = ( ! empty( $args['height_placeholder'] ) ) ? ' placeholder="'. esc_attr( $args['height_placeholder'] ) .'"' : ''; |
| 56 |
echo '<div class="adminify--input">'; |
| 57 |
echo ( ! empty( $args['height_icon'] ) ) ? '<span class="adminify--label adminify--icon">'. $args['height_icon'] .'</span>' : ''; |
| 58 |
echo '<input type="number" name="'. esc_attr( $this->field_name( '[height]' ) ) .'" value="'. esc_attr( $value['height'] ) .'"'. $placeholder .' class="adminify-input-number'. esc_attr( $is_unit ) .'" step="any" />'; |
| 59 |
echo ( ! empty( $unit ) ) ? '<span class="adminify--label adminify--unit">'. esc_attr( $args['units'][0] ) .'</span>' : ''; |
| 60 |
echo '</div>'; |
| 61 |
} |
| 62 |
|
| 63 |
if ( ! empty( $args['unit'] ) && ! empty( $args['show_units'] ) && count( $args['units'] ) > 1 ) { |
| 64 |
echo '<div class="adminify--input">'; |
| 65 |
echo '<select name="'. esc_attr( $this->field_name( '[unit]' ) ) .'">'; |
| 66 |
foreach ( $args['units'] as $unit ) { |
| 67 |
$selected = ( $value['unit'] === $unit ) ? ' selected' : ''; |
| 68 |
echo '<option value="'. esc_attr( $unit ) .'"'. esc_attr( $selected ) .'>'. esc_attr( $unit ) .'</option>'; |
| 69 |
} |
| 70 |
echo '</select>'; |
| 71 |
echo '</div>'; |
| 72 |
} |
| 73 |
|
| 74 |
echo '</div>'; |
| 75 |
|
| 76 |
echo $this->field_after(); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
public function output() { |
| 81 |
|
| 82 |
$output = ''; |
| 83 |
$element = ( is_array( $this->field['output'] ) ) ? join( ',', $this->field['output'] ) : $this->field['output']; |
| 84 |
$prefix = ( ! empty( $this->field['output_prefix'] ) ) ? $this->field['output_prefix'] .'-' : ''; |
| 85 |
$important = ( ! empty( $this->field['output_important'] ) ) ? '!important' : ''; |
| 86 |
$unit = ( ! empty( $this->value['unit'] ) ) ? $this->value['unit'] : 'px'; |
| 87 |
$width = ( isset( $this->value['width'] ) && $this->value['width'] !== '' ) ? $prefix .'width:'. $this->value['width'] . $unit . $important .';' : ''; |
| 88 |
$height = ( isset( $this->value['height'] ) && $this->value['height'] !== '' ) ? $prefix .'height:'. $this->value['height'] . $unit . $important .';' : ''; |
| 89 |
|
| 90 |
if ( $width !== '' || $height !== '' ) { |
| 91 |
$output = $element .'{'. $width . $height .'}'; |
| 92 |
} |
| 93 |
|
| 94 |
$this->parent->output_css .= $output; |
| 95 |
|
| 96 |
return $output; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
} |
| 102 |
|