avatar.php
2 weeks ago
boolean.php
2 weeks ago
code.php
2 weeks ago
color.php
2 weeks ago
comment.php
2 weeks ago
currency.php
2 weeks ago
date.php
2 weeks ago
datetime.php
2 weeks ago
email.php
2 weeks ago
file.php
2 weeks ago
heading.php
2 weeks ago
html.php
2 weeks ago
link.php
2 weeks ago
number.php
2 weeks ago
oembed.php
2 weeks ago
paragraph.php
2 weeks ago
password.php
2 weeks ago
phone.php
2 weeks ago
pick.php
2 weeks ago
slug.php
2 weeks ago
taxonomy.php
2 weeks ago
text.php
2 weeks ago
time.php
2 weeks ago
website.php
2 weeks ago
wysiwyg.php
2 weeks ago
color.php
188 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 . '_repeatable' => array( |
| 38 | 'label' => __( 'Repeatable Field', 'pods' ), |
| 39 | 'default' => 0, |
| 40 | 'type' => 'boolean', |
| 41 | 'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ), |
| 42 | 'boolean_yes_label' => '', |
| 43 | 'dependency' => true, |
| 44 | 'developer_mode' => true, |
| 45 | ), |
| 46 | static::$type . '_select_label' => array( |
| 47 | 'label' => __( 'Select Color Label', 'pods' ), |
| 48 | 'placeholder' => __( 'Select Color', 'pods' ), |
| 49 | 'default' => '', |
| 50 | 'type' => 'text', |
| 51 | ), |
| 52 | static::$type . '_clear_label' => array( |
| 53 | 'label' => __( 'Clear Label', 'pods' ), |
| 54 | 'placeholder' => __( 'Clear', 'pods' ), |
| 55 | 'default' => '', |
| 56 | 'type' => 'text', |
| 57 | ), |
| 58 | ); |
| 59 | |
| 60 | return $options; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * {@inheritdoc} |
| 65 | */ |
| 66 | public function schema( $options = null ) { |
| 67 | |
| 68 | $schema = 'VARCHAR(7)'; |
| 69 | |
| 70 | return $schema; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * {@inheritdoc} |
| 75 | */ |
| 76 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
| 77 | |
| 78 | $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; |
| 79 | $form_field_type = PodsForm::$field_type; |
| 80 | |
| 81 | if ( is_array( $value ) ) { |
| 82 | $value = implode( ' ', $value ); |
| 83 | } |
| 84 | |
| 85 | // WP Color Picker for 3.5+ |
| 86 | $field_type = 'color'; |
| 87 | |
| 88 | if ( isset( $options['name'] ) && ! pods_permission( $options ) ) { |
| 89 | if ( pods_v( 'read_only', $options, false ) ) { |
| 90 | $options['readonly'] = true; |
| 91 | |
| 92 | $field_type = 'text'; |
| 93 | } else { |
| 94 | return; |
| 95 | } |
| 96 | } elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
| 97 | $options['readonly'] = true; |
| 98 | |
| 99 | $field_type = 'text'; |
| 100 | } |
| 101 | |
| 102 | if ( ! empty( $options['disable_dfv'] ) ) { |
| 103 | return pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
| 104 | } |
| 105 | |
| 106 | // Default labels. |
| 107 | if ( empty( $options[ static::$type . '_select_label' ] ) ) { |
| 108 | $options[ static::$type . '_select_label' ] = __( 'Select Color', 'pods' ); |
| 109 | } |
| 110 | if ( empty( $options[ static::$type . '_clear_label' ] ) ) { |
| 111 | $options[ static::$type . '_clear_label' ] = __( 'Clear', 'pods' ); |
| 112 | } |
| 113 | |
| 114 | $type = pods_v( 'type', $options, static::$type ); |
| 115 | |
| 116 | $args = compact( array_keys( get_defined_vars() ) ); |
| 117 | $args = (object) $args; |
| 118 | |
| 119 | $this->render_input_script( $args ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * {@inheritdoc} |
| 124 | */ |
| 125 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
| 126 | $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params ); |
| 127 | |
| 128 | $errors = array(); |
| 129 | |
| 130 | if ( is_array( $validate ) ) { |
| 131 | $errors = $validate; |
| 132 | } |
| 133 | |
| 134 | $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); |
| 135 | |
| 136 | if ( is_array( $check ) ) { |
| 137 | $errors = $check; |
| 138 | } else { |
| 139 | $color = str_replace( '#', '', $check ); |
| 140 | |
| 141 | if ( 0 < strlen( $value ) && '' === $check ) { |
| 142 | if ( $this->is_required( $options ) ) { |
| 143 | $errors[] = __( 'This field is required.', 'pods' ); |
| 144 | } else { |
| 145 | // @todo Ask for a specific format in error message |
| 146 | $errors[] = __( 'Invalid value provided for this field.', 'pods' ); |
| 147 | } |
| 148 | } elseif ( ! empty( $color ) && ! in_array( strlen( $color ), array( 3, 6 ), true ) ) { |
| 149 | $errors[] = __( 'Invalid Hex Color value provided for this field.', 'pods' ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if ( ! empty( $errors ) ) { |
| 154 | return $errors; |
| 155 | } |
| 156 | |
| 157 | return $validate; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * {@inheritdoc} |
| 162 | */ |
| 163 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
| 164 | |
| 165 | $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; |
| 166 | |
| 167 | $value = str_replace( '#', '', $value ); |
| 168 | |
| 169 | if ( 0 < strlen( $value ) ) { |
| 170 | $value = '#' . $value; |
| 171 | } |
| 172 | |
| 173 | return $value; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * {@inheritdoc} |
| 178 | */ |
| 179 | public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
| 180 | |
| 181 | if ( ! empty( $value ) ) { |
| 182 | $value = $value . ' <span style="display:inline-block;width:25px;height:25px;border:1px solid #333;background-color:' . $value . '"></span>'; |
| 183 | } |
| 184 | |
| 185 | return $value; |
| 186 | } |
| 187 | } |
| 188 |