PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.7.31.4
Pods – Custom Content Types and Fields v2.7.31.4
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 3 days ago boolean.php 3 days ago code.php 3 days ago color.php 3 days ago comment.php 3 days ago currency.php 3 days ago date.php 3 days ago datetime.php 3 days ago email.php 3 days ago file.php 3 days ago html.php 3 days ago link.php 3 days ago number.php 3 days ago oembed.php 3 days ago paragraph.php 3 days ago password.php 3 days ago phone.php 3 days ago pick.php 3 days ago slug.php 3 days ago taxonomy.php 3 days ago text.php 3 days ago time.php 3 days ago website.php 3 days ago wysiwyg.php 3 days ago
color.php
159 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 self::$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 );
47
48 return $options;
49 }
50
51 /**
52 * {@inheritdoc}
53 */
54 public function schema( $options = null ) {
55
56 $schema = 'VARCHAR(7)';
57
58 return $schema;
59 }
60
61 /**
62 * {@inheritdoc}
63 */
64 public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
65
66 $options = (array) $options;
67 $form_field_type = PodsForm::$field_type;
68
69 if ( is_array( $value ) ) {
70 $value = implode( ' ', $value );
71 }
72
73 // WP Color Picker for 3.5+
74 $field_type = 'color';
75
76 if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) {
77 if ( pods_v( 'read_only', $options, false ) ) {
78 $options['readonly'] = true;
79
80 $field_type = 'text';
81 } else {
82 return;
83 }
84 } elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
85 $options['readonly'] = true;
86
87 $field_type = 'text';
88 }
89
90 pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
91 }
92
93 /**
94 * {@inheritdoc}
95 */
96 public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
97 $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params );
98
99 $errors = array();
100
101 if ( is_array( $validate ) ) {
102 $errors = $validate;
103 }
104
105 $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
106
107 if ( is_array( $check ) ) {
108 $errors = $check;
109 } else {
110 $color = str_replace( '#', '', $check );
111
112 if ( 0 < strlen( $value ) && '' === $check ) {
113 if ( $this->is_required( $options ) ) {
114 $errors[] = __( 'This field is required.', 'pods' );
115 } else {
116 // @todo Ask for a specific format in error message
117 $errors[] = __( 'Invalid value provided for this field.', 'pods' );
118 }
119 } elseif ( ! empty( $color ) && ! in_array( strlen( $color ), array( 3, 6 ), true ) ) {
120 $errors[] = __( 'Invalid Hex Color value provided for this field.', 'pods' );
121 }
122 }
123
124 if ( ! empty( $errors ) ) {
125 return $errors;
126 }
127
128 return $validate;
129 }
130
131 /**
132 * {@inheritdoc}
133 */
134 public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
135
136 $options = (array) $options;
137
138 $value = str_replace( '#', '', $value );
139
140 if ( 0 < strlen( $value ) ) {
141 $value = '#' . $value;
142 }
143
144 return $value;
145 }
146
147 /**
148 * {@inheritdoc}
149 */
150 public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
151
152 if ( ! empty( $value ) ) {
153 $value = $value . ' <span style="display:inline-block;width:25px;height:25px;border:1px solid #333;background-color:' . $value . '"></span>';
154 }
155
156 return $value;
157 }
158 }
159