avatar.php
3 years ago
boolean.php
3 years ago
code.php
2 years ago
color.php
1 year ago
comment.php
4 years ago
currency.php
9 months ago
date.php
2 years ago
datetime.php
1 year ago
email.php
1 year ago
file.php
11 months ago
heading.php
1 year ago
html.php
2 years ago
link.php
1 year ago
number.php
9 months ago
oembed.php
1 year ago
paragraph.php
2 years ago
password.php
1 year ago
phone.php
1 year ago
pick.php
11 months ago
slug.php
3 years ago
taxonomy.php
4 years ago
text.php
2 years ago
time.php
2 years ago
website.php
11 months ago
wysiwyg.php
1 year ago
color.php
177 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package Pods\Fields |
| 5 | */ |
| 6 | class PodsField_Color extends PodsField { |
| 7 | |
| 8 | /** |
| 9 | * {@inheritdoc} |
| 10 | */ |
| 11 | public static $type = 'color'; |
| 12 | |
| 13 | /** |
| 14 | * {@inheritdoc} |
| 15 | */ |
| 16 | public static $label = 'Color Picker'; |
| 17 | |
| 18 | /** |
| 19 | * {@inheritdoc} |
| 20 | */ |
| 21 | public static $prepare = '%s'; |
| 22 | |
| 23 | /** |
| 24 | * {@inheritdoc} |
| 25 | */ |
| 26 | public function setup() { |
| 27 | |
| 28 | static::$label = __( 'Color Picker', 'pods' ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * {@inheritdoc} |
| 33 | */ |
| 34 | public function options() { |
| 35 | |
| 36 | $options = array( |
| 37 | static::$type . '_select_label' => array( |
| 38 | 'label' => __( 'Select Color Label', 'pods' ), |
| 39 | 'placeholder' => __( 'Select Color', 'pods' ), |
| 40 | 'default' => '', |
| 41 | 'type' => 'text', |
| 42 | ), |
| 43 | static::$type . '_clear_label' => array( |
| 44 | 'label' => __( 'Clear Label', 'pods' ), |
| 45 | 'placeholder' => __( 'Clear', 'pods' ), |
| 46 | 'default' => '', |
| 47 | 'type' => 'text', |
| 48 | ), |
| 49 | ); |
| 50 | |
| 51 | return $options; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * {@inheritdoc} |
| 56 | */ |
| 57 | public function schema( $options = null ) { |
| 58 | |
| 59 | $schema = 'VARCHAR(7)'; |
| 60 | |
| 61 | return $schema; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * {@inheritdoc} |
| 66 | */ |
| 67 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
| 68 | |
| 69 | $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; |
| 70 | $form_field_type = PodsForm::$field_type; |
| 71 | |
| 72 | $value = $this->normalize_value_for_input( $value, $options ); |
| 73 | |
| 74 | // WP Color Picker for 3.5+ |
| 75 | $field_type = 'color'; |
| 76 | |
| 77 | if ( isset( $options['name'] ) && ! pods_permission( $options ) ) { |
| 78 | if ( pods_v( 'read_only', $options, false ) ) { |
| 79 | $options['readonly'] = true; |
| 80 | |
| 81 | $field_type = 'text'; |
| 82 | } else { |
| 83 | return; |
| 84 | } |
| 85 | } elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
| 86 | $options['readonly'] = true; |
| 87 | |
| 88 | $field_type = 'text'; |
| 89 | } |
| 90 | |
| 91 | if ( ! empty( $options['disable_dfv'] ) ) { |
| 92 | return pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
| 93 | } |
| 94 | |
| 95 | // Default labels. |
| 96 | if ( empty( $options[ static::$type . '_select_label' ] ) ) { |
| 97 | $options[ static::$type . '_select_label' ] = __( 'Select Color', 'pods' ); |
| 98 | } |
| 99 | if ( empty( $options[ static::$type . '_clear_label' ] ) ) { |
| 100 | $options[ static::$type . '_clear_label' ] = __( 'Clear', 'pods' ); |
| 101 | } |
| 102 | |
| 103 | $type = pods_v( 'type', $options, static::$type ); |
| 104 | |
| 105 | $args = compact( array_keys( get_defined_vars() ) ); |
| 106 | $args = (object) $args; |
| 107 | |
| 108 | $this->render_input_script( $args ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * {@inheritdoc} |
| 113 | */ |
| 114 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
| 115 | $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params ); |
| 116 | |
| 117 | $errors = array(); |
| 118 | |
| 119 | if ( is_array( $validate ) ) { |
| 120 | $errors = $validate; |
| 121 | } |
| 122 | |
| 123 | $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); |
| 124 | |
| 125 | if ( is_array( $check ) ) { |
| 126 | $errors = $check; |
| 127 | } else { |
| 128 | $color = str_replace( '#', '', $check ); |
| 129 | |
| 130 | if ( 0 < strlen( $value ) && '' === $check ) { |
| 131 | if ( $this->is_required( $options ) ) { |
| 132 | $errors[] = __( 'This field is required.', 'pods' ); |
| 133 | } else { |
| 134 | // @todo Ask for a specific format in error message |
| 135 | $errors[] = __( 'Invalid value provided for this field.', 'pods' ); |
| 136 | } |
| 137 | } elseif ( ! empty( $color ) && ! in_array( strlen( $color ), array( 3, 6 ), true ) ) { |
| 138 | $errors[] = __( 'Invalid Hex Color value provided for this field.', 'pods' ); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if ( ! empty( $errors ) ) { |
| 143 | return $errors; |
| 144 | } |
| 145 | |
| 146 | return $validate; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * {@inheritdoc} |
| 151 | */ |
| 152 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
| 153 | |
| 154 | $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; |
| 155 | |
| 156 | $value = str_replace( '#', '', $value ); |
| 157 | |
| 158 | if ( 0 < strlen( (string) $value ) ) { |
| 159 | $value = '#' . $value; |
| 160 | } |
| 161 | |
| 162 | return $value; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * {@inheritdoc} |
| 167 | */ |
| 168 | public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
| 169 | |
| 170 | if ( ! empty( $value ) ) { |
| 171 | $value = $value . ' <span style="display:inline-block;width:25px;height:25px;border:1px solid #333;background-color:' . $value . '"></span>'; |
| 172 | } |
| 173 | |
| 174 | return $value; |
| 175 | } |
| 176 | } |
| 177 |