image_select.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: image_select
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_image_select' ) ) {
  11. class CSF_Field_image_select extends CSF_Fields {
  12. public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
  13. parent::__construct( $field, $value, $unique, $where, $parent );
  14. }
  15. public function render() {
  16. $args = wp_parse_args( $this->field, array(
  17. 'multiple' => false,
  18. 'inline' => false,
  19. 'options' => array(),
  20. ) );
  21. $inline = ( $args['inline'] ) ? ' csf--inline-list' : '';
  22. $value = ( is_array( $this->value ) ) ? $this->value : array_filter( (array) $this->value );
  23. echo $this->field_before();
  24. if ( ! empty( $args['options'] ) ) {
  25. echo '<div class="csf-siblings csf--image-group'. esc_attr( $inline ) .'" data-multiple="'. esc_attr( $args['multiple'] ) .'">';
  26. $num = 1;
  27. foreach ( $args['options'] as $key => $option ) {
  28. $type = ( $args['multiple'] ) ? 'checkbox' : 'radio';
  29. $extra = ( $args['multiple'] ) ? '[]' : '';
  30. $active = ( in_array( $key, $value ) ) ? ' csf--active' : '';
  31. $checked = ( in_array( $key, $value ) ) ? ' checked' : '';
  32. echo '<div class="csf--sibling csf--image'. esc_attr( $active ) .'">';
  33. echo '<figure>';
  34. echo '<img src="'. esc_url( $option ) .'" alt="img-'. esc_attr( $num++ ) .'" />';
  35. echo '<input type="'. esc_attr( $type ) .'" name="'. esc_attr( $this->field_name( $extra ) ) .'" value="'. esc_attr( $key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>';
  36. echo '</figure>';
  37. echo '</div>';
  38. }
  39. echo '</div>';
  40. }
  41. echo $this->field_after();
  42. }
  43. public function output() {
  44. $output = '';
  45. $bg_image = array();
  46. $important = ( ! empty( $this->field['output_important'] ) ) ? '!important' : '';
  47. $elements = ( is_array( $this->field['output'] ) ) ? join( ',', $this->field['output'] ) : $this->field['output'];
  48. if ( ! empty( $elements ) && isset( $this->value ) && $this->value !== '' ) {
  49. $output = $elements .'{background-image:url('. $this->value .')'. $important .';}';
  50. }
  51. $this->parent->output_css .= $output;
  52. return $output;
  53. }
  54. }
  55. }