color.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Field: color
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Field_color' ) ) {
  11. class CSF_Field_color 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. $default_attr = ( ! empty( $this->field['default'] ) ) ? ' data-default-color="'. esc_attr( $this->field['default'] ) .'"' : '';
  17. echo $this->field_before();
  18. echo '<input type="text" name="'. esc_attr( $this->field_name() ) .'" value="'. esc_attr( $this->value ) .'" class="csf-color"'. $default_attr . $this->field_attributes() .'/>';
  19. echo $this->field_after();
  20. }
  21. public function output() {
  22. $output = '';
  23. $elements = ( is_array( $this->field['output'] ) ) ? $this->field['output'] : array_filter( (array) $this->field['output'] );
  24. $important = ( ! empty( $this->field['output_important'] ) ) ? '!important' : '';
  25. $mode = ( ! empty( $this->field['output_mode'] ) ) ? $this->field['output_mode'] : 'color';
  26. if ( ! empty( $elements ) && isset( $this->value ) && $this->value !== '' ) {
  27. foreach ( $elements as $key_property => $element ) {
  28. if ( is_numeric( $key_property ) ) {
  29. $output = implode( ',', $elements ) .'{'. $mode .':'. $this->value . $important .';}';
  30. break;
  31. } else {
  32. $output .= $element .'{'. $key_property .':'. $this->value . $important .'}';
  33. }
  34. }
  35. }
  36. $this->parent->output_css .= $output;
  37. return $output;
  38. }
  39. }
  40. }