PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.9.1
Pods – Custom Content Types and Fields v3.3.9.1
2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / fields / color.php
pods / classes / fields Last commit date
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
184 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 /**
9 * @package Pods\Fields
10 */
11 class PodsField_Color extends PodsField {
12
13 /**
14 * {@inheritdoc}
15 */
16 public static $type = 'color';
17
18 /**
19 * {@inheritdoc}
20 */
21 public static $label = 'Color Picker';
22
23 /**
24 * {@inheritdoc}
25 */
26 public static $prepare = '%s';
27
28 /**
29 * {@inheritdoc}
30 */
31 public function setup() {
32
33 static::$label = __( 'Color Picker', 'pods' );
34 }
35
36 /**
37 * {@inheritdoc}
38 */
39 public function options() {
40
41 $options = [
42 static::$type . '_select_label' => [
43 'label' => __( 'Select Color Label', 'pods' ),
44 'placeholder' => __( 'Select Color', 'pods' ),
45 'default' => '',
46 'type' => 'text',
47 ],
48 static::$type . '_clear_label' => [
49 'label' => __( 'Clear Label', 'pods' ),
50 'placeholder' => __( 'Clear', 'pods' ),
51 'default' => '',
52 'type' => 'text',
53 ],
54 ];
55
56 return $options;
57 }
58
59 /**
60 * {@inheritdoc}
61 */
62 public function schema( $options = null ) {
63
64 $schema = 'VARCHAR(7)';
65
66 return $schema;
67 }
68
69 /**
70 * {@inheritdoc}
71 */
72 public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
73
74 $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options;
75 $form_field_type = PodsForm::$field_type;
76
77 $value = $this->normalize_value_for_input( $value, $options );
78
79 // WP Color Picker for 3.5+
80 $field_type = 'color';
81
82 if ( isset( $options['name'] ) && ! pods_permission( $options ) ) {
83 if ( pods_v_bool( 'read_only_restricted', $options ) ) {
84 $options['readonly'] = true;
85
86 $field_type = 'text';
87 } else {
88 return;
89 }
90 } elseif ( ! pods_has_permissions( $options ) ) {
91 if ( pods_v_bool( 'read_only', $options ) ) {
92 $options['readonly'] = true;
93
94 $field_type = 'text';
95 }
96 }
97
98 if ( ! empty( $options['disable_dfv'] ) ) {
99 return pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
100 }
101
102 // Default labels.
103 if ( empty( $options[ static::$type . '_select_label' ] ) ) {
104 $options[ static::$type . '_select_label' ] = __( 'Select Color', 'pods' );
105 }
106 if ( empty( $options[ static::$type . '_clear_label' ] ) ) {
107 $options[ static::$type . '_clear_label' ] = __( 'Clear', 'pods' );
108 }
109
110 $type = pods_v( 'type', $options, static::$type );
111
112 $args = compact( array_keys( get_defined_vars() ) );
113 $args = (object) $args;
114
115 $this->render_input_script( $args );
116 }
117
118 /**
119 * {@inheritdoc}
120 */
121 public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
122 $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params );
123
124 $errors = [];
125
126 if ( is_array( $validate ) ) {
127 $errors = $validate;
128 }
129
130 $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
131
132 if ( is_array( $check ) ) {
133 $errors = $check;
134 } else {
135 $color = str_replace( '#', '', $check );
136
137 if ( 0 < strlen( $value ) && '' === $check ) {
138 if ( $this->is_required( $options ) ) {
139 $errors[] = __( 'This field is required.', 'pods' );
140 } else {
141 // @todo Ask for a specific format in error message
142 $errors[] = __( 'Invalid value provided for this field.', 'pods' );
143 }
144 } elseif ( ! empty( $color ) && ! in_array( strlen( $color ), [ 3, 6 ], true ) ) {
145 $errors[] = __( 'Invalid Hex Color value provided for this field.', 'pods' );
146 }
147 }
148
149 if ( ! empty( $errors ) ) {
150 return $errors;
151 }
152
153 return $validate;
154 }
155
156 /**
157 * {@inheritdoc}
158 */
159 public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
160
161 $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options;
162
163 $value = str_replace( '#', '', $value );
164
165 if ( 0 < strlen( (string) $value ) ) {
166 $value = '#' . $value;
167 }
168
169 return $value;
170 }
171
172 /**
173 * {@inheritdoc}
174 */
175 public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
176
177 if ( ! empty( $value ) ) {
178 $value = $value . ' <span style="display:inline-block;width:25px;height:25px;border:1px solid #333;background-color:' . $value . '"></span>';
179 }
180
181 return $value;
182 }
183 }
184