PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.9.19.5
Pods – Custom Content Types and Fields v2.9.19.5
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 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 days ago boolean.php 2 days ago code.php 2 days ago color.php 2 days ago comment.php 2 days ago currency.php 2 days ago date.php 2 days ago datetime.php 2 days ago email.php 2 days ago file.php 2 days ago heading.php 2 days ago html.php 2 days ago link.php 2 days ago number.php 2 days ago oembed.php 2 days ago paragraph.php 2 days ago password.php 2 days ago phone.php 2 days ago pick.php 2 days ago slug.php 2 days ago taxonomy.php 2 days ago text.php 2 days ago time.php 2 days ago website.php 2 days ago wysiwyg.php 2 days 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( $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