PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 2.2
Admin Columns v2.2
7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / column / custom-field.php
codepress-admin-columns / classes / column Last commit date
comment 12 years ago link 12 years ago media 12 years ago post 12 years ago user 12 years ago acf-placeholder.php 12 years ago custom-field.php 12 years ago taxonomy.php 12 years ago
custom-field.php
427 lines
1 <?php
2
3 /**
4 * CPAC_Column_Custom_Field
5 *
6 * @since 1.0
7 */
8 class CPAC_Column_Custom_Field extends CPAC_Column {
9
10 private $user_settings;
11
12 function __construct( $storage_model ) {
13
14 // define properties
15 $this->properties['type'] = 'column-meta';
16 $this->properties['label'] = __( 'Custom Field', 'cpac' );
17 $this->properties['classes'] = 'cpac-box-metafield';
18 $this->properties['is_cloneable'] = true;
19
20 // define additional options
21 $this->options['field'] = '';
22 $this->options['field_type'] = '';
23 $this->options['before'] = '';
24 $this->options['after'] = '';
25
26 $this->options['image_size'] = '';
27 $this->options['image_size_w'] = 80;
28 $this->options['image_size_h'] = 80;
29
30 $this->options['excerpt_length'] = 15;
31
32 $this->options['date_format'] = '';
33 $this->options['date_save_format'] = '';
34
35 // for retireving sorting preference
36 $this->user_settings = get_option( 'cpac_general_options' );
37
38 // call construct
39 parent::__construct( $storage_model );
40 }
41
42 /**
43 * @see CPAC_Column::sanitize_options()
44 * @since 1.0
45 */
46 function sanitize_options( $options ) {
47
48 if ( empty( $options['date_format'] ) ) {
49 $options['date_format'] = get_option( 'date_format' );
50 }
51
52 return $options;
53 }
54
55 /**
56 * Get Custom FieldType Options - Value method
57 *
58 * @since 1.0
59 *
60 * @return array Customfield types.
61 */
62 public function get_custom_field_types() {
63
64 $custom_field_types = array(
65 '' => __( 'Default'),
66 'checkmark' => __( 'Checkmark (true/false)', 'cpac' ),
67 'color' => __( 'Color', 'cpac' ),
68 'count' => __( 'Counter', 'cpac' ),
69 'date' => __( 'Date', 'cpac' ),
70 'excerpt' => __( 'Excerpt'),
71 'image' => __( 'Image', 'cpac' ),
72 'library_id' => __( 'Media Library', 'cpac' ),
73 'array' => __( 'Multiple Values', 'cpac' ),
74 'numeric' => __( 'Numeric', 'cpac' ),
75 'title_by_id' => __( 'Post Title (Post ID\'s)', 'cpac' ),
76 'user_by_id' => __( 'Username (User ID\'s)', 'cpac' ),
77 );
78
79 // deprecated. do not use, will be removed.
80 $custom_field_types = apply_filters( 'cpac_custom_field_types', $custom_field_types );
81
82 /**
83 * Filter the available custom field types for the meta (custom field) field
84 *
85 * @since 2.0
86 *
87 * @param array $custom_field_types Available custom field types ([type] => [label])
88 */
89 $custom_field_types = apply_filters( 'cac/column/meta/types', $custom_field_types );
90
91 return $custom_field_types;
92 }
93
94 /**
95 * Get First ID from array
96 *
97 * @since 1.0
98 *
99 * @param string $meta
100 * @return string Titles
101 */
102 public function get_ids_from_meta( $meta ) {
103
104 //remove white spaces and strip tags
105 $meta = $this->strip_trim( str_replace( ' ','', $meta ) );
106
107 // var
108 $ids = array();
109
110 // check for multiple id's
111 if ( strpos( $meta, ',' ) !== false )
112 $ids = explode( ',', $meta );
113 elseif ( is_numeric( $meta ) )
114 $ids[] = $meta;
115
116 return $ids;
117 }
118
119 /**
120 * Get Title by ID - Value method
121 *
122 * @since 1.0
123 *
124 * @param string $meta
125 * @return string Titles
126 */
127 private function get_titles_by_id( $meta ) {
128
129 $titles = array();
130
131 // display title with link
132 if ( $ids = $this->get_ids_from_meta( $meta ) ) {
133 foreach ( (array) $ids as $id ) {
134
135 if ( ! is_numeric( $id ) ) continue;
136
137 $link = get_edit_post_link( $id );
138 if ( $title = get_the_title( $id ) )
139 $titles[] = $link ? "<a href='{$link}'>{$title}</a>" : $title;
140 }
141 }
142
143 return implode('<span class="cpac-divider"></span>', $titles);
144 }
145
146 /**
147 * Get Users by ID - Value method
148 *
149 * @since 1.0
150 *
151 * @param string $meta
152 * @return string Users
153 */
154 private function get_users_by_id( $meta ) {
155
156 $names = array();
157
158 // display username
159 if ( $ids = $this->get_ids_from_meta( $meta ) ) {
160 foreach ( (array) $ids as $id ) {
161 if ( ! is_numeric( $id ) ) continue;
162
163 $userdata = get_userdata( $id );
164 if ( is_object( $userdata ) && ! empty( $userdata->display_name ) ) {
165
166 // link
167 $link = get_edit_user_link( $id );
168
169 $names[] = $link ? "<a href='{$link}'>{$userdata->display_name}</a>" : $userdata->display_name;
170 }
171 }
172 }
173
174 return implode( '<span class="cpac-divider"></span>', $names );
175 }
176
177 /**
178 * Get meta value
179 *
180 * @since 2.0.0
181 *
182 * @param string $meta Contains Meta Value
183 * @param int $id Optional Object ID
184 * @return string Users
185 */
186 function get_value_by_meta( $meta, $id = null ) {
187
188 switch ( $this->options->field_type ) :
189
190 case "image" :
191 case "library_id" :
192 $meta = implode( $this->get_thumbnails( $meta, array(
193 'image_size' => $this->options->image_size,
194 'image_size_w' => $this->options->image_size_w,
195 'image_size_h' => $this->options->image_size_h,
196 )));
197 break;
198
199 case "excerpt" :
200 $meta = $this->get_shortened_string( $meta, $this->options->excerpt_length );
201 break;
202
203 case "date" :
204 $meta = $this->get_date( $meta, $this->options->date_format );
205 break;
206
207 case "title_by_id" :
208 $meta = $this->get_titles_by_id( $meta );
209 break;
210
211 case "user_by_id" :
212 $meta = $this->get_users_by_id( $meta );
213 break;
214
215 case "checkmark" :
216 $checkmark = $this->get_asset_image( 'checkmark.png' );
217
218 if ( empty( $meta ) || 'false' === $meta || '0' === $meta ) {
219 $checkmark = '';
220 }
221
222 $meta = $checkmark;
223 break;
224
225 case "color" :
226 if ( ! empty( $meta ) ) {
227 $text_color = $this->get_text_color( $meta );
228 $meta = "<div class='cpac-color'><span style='background-color:{$meta};color:{$text_color}'>{$meta}</span></div>";
229 }
230 break;
231
232 case "count" :
233 if ( $count = $this->get_raw_value( $id, false ) )
234 $meta = count( $count );
235 break;
236
237 endswitch;
238
239 return $meta;
240 }
241
242 /**
243 * Determines text color absed on bakground coloring.
244 *
245 * @since 1.0
246 */
247 function get_text_color( $bg_color ) {
248
249 $rgb = $this->hex2rgb( $bg_color );
250
251 return $rgb && ( ( $rgb[0]*0.299 + $rgb[1]*0.587 + $rgb[2]*0.114 ) < 186 ) ? '#ffffff' : '#333333';
252 }
253
254 /**
255 * Convert hex to rgb
256 *
257 * @since 1.0
258 */
259 function hex2rgb( $hex ) {
260 $hex = str_replace( "#", "", $hex );
261
262 if(strlen($hex) == 3) {
263 $r = hexdec(substr($hex,0,1).substr($hex,0,1));
264 $g = hexdec(substr($hex,1,1).substr($hex,1,1));
265 $b = hexdec(substr($hex,2,1).substr($hex,2,1));
266 } else {
267 $r = hexdec(substr($hex,0,2));
268 $g = hexdec(substr($hex,2,2));
269 $b = hexdec(substr($hex,4,2));
270 }
271 $rgb = array($r, $g, $b);
272
273 return $rgb;
274 }
275
276 /**
277 * Get Field key
278 *
279 * @since 2.0.3
280 *
281 * @param string Custom Field Key
282 */
283 function get_field_key() {
284
285 return substr( $this->options->field, 0, 10 ) == "cpachidden" ? str_replace( 'cpachidden', '', $this->options->field ) : $this->options->field;
286 }
287
288 /**
289 * Get meta by ID
290 *
291 * @since 1.0.0
292 *
293 * @param int $id ID
294 * @return string Meta Value
295 */
296 public function get_meta_by_id( $id ) {
297
298 // get metadata
299 $meta = $this->get_raw_value( $id );
300
301 // try to turn any array into a comma seperated string for further use
302 if ( ( 'array' == $this->options->field_type && is_array( $meta ) ) || is_array( $meta ) ) {
303 $meta = $this->recursive_implode( ', ', $meta );
304 }
305
306 if ( ! is_string( $meta ) )
307 return false;
308
309 return $meta;
310 }
311
312 /**
313 * Get before value
314 *
315 * @since 1.0
316 */
317 function get_before() {
318
319 return stripslashes( $this->options->before );
320 }
321
322 /**
323 * Get after value
324 *
325 * @since 1.0
326 */
327 function get_after() {
328
329 return stripslashes( $this->options->after );
330 }
331
332 /**
333 * @see CPAC_Column::get_raw_value()
334 * @since 2.0.3
335 */
336 function get_raw_value( $id, $single = true ) {
337
338 $field_key = $this->get_field_key();
339
340 $raw_value = get_metadata( $this->storage_model->type, $id, $field_key, $single );
341
342 return apply_filters( 'cac/column/meta/raw_value', $raw_value, $id, $field_key, $this );
343 }
344
345 /**
346 * @see CPAC_Column::get_value()
347 * @since 1.0
348 */
349 function get_value( $id ) {
350
351 $value = '';
352
353 if ( $meta = $this->get_meta_by_id( $id ) ) {
354
355 // get value by meta
356 $value = $this->get_value_by_meta( $meta, $id );
357 }
358
359 $value = apply_filters( 'cac/column/meta/value', $value, $id, $this );
360
361 $before = $this->get_before();
362 $after = $this->get_after();
363
364 // add before and after string
365 if ( $value ) {
366 $value = "{$before}{$value}{$after}";
367 }
368
369 return $value;
370 }
371
372 /**
373 * @see CPAC_Column::display_settings()
374 * @since 1.0
375 */
376 function display_settings() {
377
378 //$show_hidden_meta = isset( $this->user_settings['show_hidden'] ) && '1' === $this->user_settings['show_hidden'] ? true : false;
379 $show_hidden_meta = true;
380
381 ?>
382
383 <tr class="column_field">
384 <?php $this->label_view( __( "Custom Field", 'cpac' ), __( "Select your custom field.", 'cpac' ), 'field' ); ?>
385 <td class="input">
386
387 <?php if ( $meta_keys = $this->storage_model->get_meta_keys( $show_hidden_meta ) ) : ?>
388 <select name="<?php $this->attr_name( 'field' ); ?>" id="<?php $this->attr_id( 'field' ); ?>">
389 <?php foreach ( $meta_keys as $field ) : ?>
390 <option value="<?php echo $field ?>"<?php selected( $field, $this->options->field ) ?>><?php echo substr( $field, 0, 10 ) == "cpachidden" ? str_replace( 'cpachidden','', $field ) : $field; ?></option>
391 <?php endforeach; ?>
392 </select>
393 <?php else : ?>
394 <?php _e( 'No custom fields available.', 'cpac' ); ?>
395 <?php endif; ?>
396
397 </td>
398 </tr>
399
400 <tr class="column_field_type">
401 <?php $this->label_view( __( "Field Type", 'cpac' ), __( 'This will determine how the value will be displayed.', 'cpac' ), 'field_type' ); ?>
402 <td class="input">
403 <select name="<?php $this->attr_name( 'field_type' ); ?>" id="<?php $this->attr_id( 'field_type' ); ?>">
404 <?php foreach ( $this->get_custom_field_types() as $fieldkey => $fieldtype ) : ?>
405 <option value="<?php echo $fieldkey ?>"<?php selected( $fieldkey, $this->options->field_type ) ?>><?php echo $fieldtype; ?></option>
406 <?php endforeach; ?>
407 </select>
408 </td>
409 </tr>
410
411 <?php
412 switch ( $this->options->field_type ) {
413 case 'date':
414 $this->display_field_date_format();
415 break;
416 case 'image':
417 case 'library_id':
418 $this->display_field_preview_size();
419 break;
420 case 'excerpt':
421 $this->display_field_excerpt_length();
422 break;
423 }
424
425 $this->display_field_before_after();
426 }
427 }