PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 1.4.9
Admin Columns v1.4.9
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 / values.php
codepress-admin-columns / classes Last commit date
values 13 years ago license.php 13 years ago sortable.php 13 years ago third_party.php 13 years ago values.php 13 years ago
values.php
539 lines
1 <?php
2
3 /**
4 * CPAC_Values Class
5 *
6 * @since 1.4.4
7 *
8 */
9 class CPAC_Values
10 {
11 protected $excerpt_length, $thumbnail_size;
12
13 /**
14 * Constructor
15 *
16 * @since 1.0
17 */
18 function __construct()
19 {
20 // number of words
21 $this->excerpt_length = 20;
22 $this->thumbnail_size = apply_filters( 'cpac_thumbnail_size', array(80,80) );
23 }
24
25 /**
26 * Admin requests for orderby column
27 *
28 * @since 1.0
29 */
30 public function get_stored_columns($type)
31 {
32 return Codepress_Admin_Columns::get_stored_columns($type);
33 }
34
35 /**
36 * Checks if column-meta key exists
37 *
38 * @since 1.0
39 */
40 public static function is_column_meta( $id = '' )
41 {
42 return Codepress_Admin_Columns::is_column_meta( $id );
43 }
44
45 /**
46 * Returns excerpt
47 *
48 * @since 1.0
49 */
50 protected function get_post_excerpt($post_id)
51 {
52 global $post;
53
54 $save_post = $post;
55 $post = get_post($post_id);
56 $excerpt = get_the_excerpt();
57 $post = $save_post;
58
59 $output = $this->get_shortened_string($excerpt, $this->excerpt_length );
60
61 return $output;
62 }
63
64 /**
65 * Returns shortened string
66 *
67 * @since 1.0
68 */
69 protected function get_shortened_string($string = '', $num_words = 55, $more = null)
70 {
71 if (!$string)
72 return false;
73
74 return wp_trim_words( $string, $num_words, $more );
75 }
76
77 /**
78 * Get image from assets folder
79 *
80 * @since 1.3.1
81 */
82 protected function get_asset_image($name = '', $title = '')
83 {
84 if ( $name )
85 return sprintf("<img alt='' src='%s' title='%s'/>", CPAC_URL."/assets/images/{$name}", $title);
86 }
87
88 /**
89 * Shorten URL
90 *
91 * @since 1.3.1
92 */
93 protected function get_shorten_url($url = '')
94 {
95 if ( !$url )
96 return false;
97
98 // shorten url
99 $short_url = url_shorten( $url );
100
101 return "<a title='{$url}' href='{$url}'>{$short_url}</a>";
102 }
103
104 /**
105 * Get column value of post attachments
106 *
107 * @since 1.0
108 */
109 protected function get_column_value_attachments( $post_id )
110 {
111 $result = '';
112 $attachment_ids = $this->get_attachment_ids($post_id);
113 if ( $attachment_ids ) {
114 foreach ( $attachment_ids as $attach_id ) {
115 if ( wp_get_attachment_image($attach_id) )
116 $result .= wp_get_attachment_image( $attach_id, $this->thumbnail_size, true );
117 }
118 }
119 return $result;
120 }
121
122 /**
123 * Get column value of post attachments
124 *
125 * @since 1.2.1
126 */
127 protected function get_attachment_ids( $post_id )
128 {
129 return Codepress_Admin_Columns::get_attachment_ids( $post_id );
130 }
131
132 /**
133 * Get a thumbnail
134 *
135 * @since 1.0
136 */
137 protected function get_thumbnail( $image = '' )
138 {
139 if ( empty($image) )
140 return false;
141
142 // get thumbnail image size
143 $image_size = $this->thumbnail_size; // w, h
144
145 // incase the thumbnail dimension is set by name
146 if ( !is_array($image_size) ) {
147 global $_wp_additional_image_sizes;
148 if ( isset($_wp_additional_image_sizes[$image_size]) ) {
149 $_size = $_wp_additional_image_sizes[$image_size];
150 $image_size = array( $_size['width'], $_size['height'] );
151 }
152 }
153
154 // fallback for image size incase the passed size name does not exists
155 if ( !isset($image_size[0]) || !isset($image_size[1]) ) {
156 $image_size = array(80, 80);
157 }
158
159 // get correct image path
160 $image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $image);
161
162 // resize image
163 if ( file_exists($image_path) && $this->is_image($image_path) ) {
164 $resized = image_resize( $image_path, $image_size[0], $image_size[1], true);
165
166 // resize worked
167 if ( ! is_wp_error( $resized ) ) {
168 $image = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $resized);
169
170 return "<img src='{$image}' alt='' width='{$image_size[0]}' height='{$image_size[1]}' />";
171 }
172
173 // resizing failed so let's return full image with maxed dimensions
174 else {
175 return "<img src='{$image}' alt='' style='max-width:{$image_size[0]}px;max-height:{$image_size[1]}px' />";
176 }
177 }
178
179 return false;
180 }
181
182 /**
183 * Checks an URL for image extension
184 *
185 * @since 1.2
186 */
187 protected function is_image($url)
188 {
189 $validExt = array('.jpg', '.jpeg', '.gif', '.png', '.bmp');
190 $ext = strrchr($url, '.');
191
192 return in_array($ext, $validExt);
193 }
194
195 /**
196 * Get a thumbnail
197 *
198 * @since 1.3.1
199 */
200 protected function get_media_thumbnails($meta)
201 {
202 $meta = $this->strip_trim( str_replace(' ','', $meta) );
203
204 // split media ids
205 $media_ids = array($meta);
206 if ( strpos($meta, ',') !== false )
207 $media_ids = explode(',', $meta);
208
209 // check if media exists
210 $thumbs = '';
211 foreach ( $media_ids as $media_id )
212 if ( is_numeric($media_id) )
213 $thumbs .= wp_get_attachment_url($media_id) ? "<span class='cpac-column-value-image'>".wp_get_attachment_image( $media_id, array(80,80), true )."</span>" : '';
214
215 return $thumbs;
216 }
217
218 /**
219 * Convert file size to readable format
220 *
221 * @since 1.4.5
222 */
223 function get_readable_filesize($size)
224 {
225 $filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
226 return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Bytes';
227 }
228
229 /**
230 * Get column value of Custom Field
231 *
232 * @since 1.0
233 */
234 protected function get_column_value_custom_field($object_id, $column_name, $meta_type = 'post')
235 {
236 /** Users */
237 if ( 'user' == $meta_type ) {
238 $type = 'wp-users';
239 }
240
241 /** Media */
242 elseif ( 'media' == $meta_type ) {
243 $type = 'wp-media';
244 $meta_type = 'post';
245 }
246
247 /** Posts */
248 else {
249 $type = get_post_type($object_id);
250 }
251
252 // get column
253 $columns = $this->get_stored_columns($type);
254
255 // inputs
256 $field = isset($columns[$column_name]['field']) ? $columns[$column_name]['field'] : '';
257 $fieldtype = isset($columns[$column_name]['field_type']) ? $columns[$column_name]['field_type'] : '';
258 $before = isset($columns[$column_name]['before']) ? $columns[$column_name]['before'] : '';
259 $after = isset($columns[$column_name]['after']) ? $columns[$column_name]['after'] : '';
260
261 // rename hidden custom fields to their original name
262 $field = substr($field,0,10) == "cpachidden" ? str_replace('cpachidden','',$field) : $field;
263
264 // Get meta field value
265 $meta = get_metadata($meta_type, $object_id, $field, true);
266
267 // multiple meta values
268 if ( ( $fieldtype == 'array' && is_array($meta) ) || is_array($meta) ) {
269 $meta = get_metadata($meta_type, $object_id, $field, true);
270 $meta = $this->recursive_implode(', ', $meta);
271 }
272
273 // make sure there are no serialized arrays or null data
274 if ( !is_string($meta) )
275 return false;
276
277 // handles each field type differently..
278 switch ($fieldtype) :
279
280 // Image
281 case "image" :
282 $meta = $this->get_thumbnail($meta);
283 break;
284
285 // Media Library ID
286 case "library_id" :
287 $meta = $this->get_media_thumbnails($meta);
288 break;
289
290 // Excerpt
291 case "excerpt" :
292 $meta = $this->get_shortened_string($meta, $this->excerpt_length);
293 break;
294
295 // Date
296 case "date" :
297 $meta = $this->get_date($meta);
298 break;
299
300 // Post Title
301 case "title_by_id" :
302 $titles = $this->get_custom_field_value_title($meta);
303 if ( $titles )
304 $meta = $titles;
305 break;
306
307 // User Name
308 case "user_by_id" :
309 $names = $this->get_custom_field_value_user($meta);
310 if ( $names )
311 $meta = $names;
312 break;
313
314 // Checkmark
315 case "checkmark" :
316 $checkmark = $this->get_asset_image('checkmark.png');
317
318 if ( empty($meta) || 'false' === $meta || '0' === $meta ) {
319 $checkmark = '';
320 }
321
322 $meta = $checkmark;
323 break;
324
325 // Color
326 case "color" :
327 if ( !empty($meta) ) {
328 $meta = "<div class='cpac-color'><span style='background-color:{$meta}'></span>{$meta}</div>";
329 }
330 break;
331
332 endswitch;
333
334 // filter for customization
335 $meta = apply_filters('cpac_get_column_value_custom_field', $meta, $fieldtype, $field, $type, $object_id );
336
337 // add before and after string
338 if ( $meta ) {
339 $meta = "{$before}{$meta}{$after}";
340 }
341
342 return $meta;
343 }
344
345 /**
346 * Get custom field value 'Title by ID'
347 *
348 * @since 1.3
349 */
350 protected function get_custom_field_value_title($meta)
351 {
352 //remove white spaces and strip tags
353 $meta = $this->strip_trim( str_replace(' ','', $meta) );
354
355 // var
356 $ids = $titles = array();
357
358 // check for multiple id's
359 if ( strpos($meta, ',') !== false )
360 $ids = explode(',',$meta);
361 elseif ( is_numeric($meta) )
362 $ids[] = $meta;
363
364 // display title with link
365 if ( $ids && is_array($ids) ) {
366 foreach ( $ids as $id ) {
367 $title = is_numeric($id) ? get_the_title($id) : '';
368 $link = get_edit_post_link($id);
369 if ( $title )
370 $titles[] = $link ? "<a href='{$link}'>{$title}</a>" : $title;
371 }
372 }
373
374 return implode('<span class="cpac-divider"></span>', $titles);
375 }
376
377 /**
378 * Get custom field value 'User by ID'
379 *
380 * @since 1.4.6.3
381 */
382 protected function get_custom_field_value_user($meta)
383 {
384 //remove white spaces and strip tags
385 $meta = $this->strip_trim( str_replace(' ','', $meta) );
386
387 // var
388 $ids = $names = array();
389
390 // check for multiple id's
391 if ( strpos($meta, ',') !== false )
392 $ids = explode(',',$meta);
393 elseif ( is_numeric($meta) )
394 $ids[] = $meta;
395
396 // display username
397 if ( $ids && is_array($ids) ) {
398 foreach ( $ids as $id ) {
399 if ( !is_numeric($id) )
400 continue;
401
402 $userdata = get_userdata($id);
403 if ( is_object($userdata) && !empty( $userdata->display_name ) ) {
404 $names[] = $userdata->display_name;
405 }
406 }
407 }
408
409 return implode('<span class="cpac-divider"></span>', $names);
410 }
411
412 /**
413 * Get column value of Custom Field
414 *
415 * @since 1.2
416 */
417 protected function get_user_column_value_custom_field($user_id, $id)
418 {
419 $columns = $this->get_stored_columns('wp-users');
420
421 // inputs
422 $field = isset($columns[$id]['field']) ? $columns[$id]['field'] : '';
423 $fieldtype = isset($columns[$id]['field_type']) ? $columns[$id]['field_type'] : '';
424 $before = isset($columns[$id]['before']) ? $columns[$id]['before'] : '';
425 $after = isset($columns[$id]['after']) ? $columns[$id]['after'] : '';
426
427 // Get meta field value
428 $meta = get_user_meta($user_id, $field, true);
429
430 // multiple meta values
431 if ( ( $fieldtype == 'array' && is_array($meta) ) || is_array($meta) ) {
432 $meta = get_user_meta($user_id, $field);
433 $meta = $this->recursive_implode(', ', $meta);
434 }
435
436 // make sure there are no serialized arrays or empty meta data
437 if ( empty($meta) || !is_string($meta) )
438 return false;
439
440 // handles each field type differently..
441 switch ($fieldtype) :
442
443 // Image
444 case "image" :
445 $meta = $this->get_thumbnail($meta);
446 break;
447
448 // Media Library ID
449 case "library_id" :
450 $meta = $this->get_media_thumbnails($meta);
451 break;
452
453 // Excerpt
454 case "excerpt" :
455 $meta = $this->get_shortened_string($meta, $this->excerpt_length);
456 break;
457
458 endswitch;
459
460 // filter for customization
461 $meta = apply_filters('cpac_get_user_column_value_custom_field', $meta, $fieldtype, $field );
462
463 // add before and after string
464 $meta = "{$before}{$meta}{$after}";
465
466 return $meta;
467 }
468
469 /**
470 * Implode for multi dimensional array
471 *
472 * @since 1.0
473 */
474 protected function recursive_implode( $glue, $pieces )
475 {
476 foreach( $pieces as $r_pieces ) {
477 if( is_array( $r_pieces ) ) {
478 $retVal[] = $this->recursive_implode( $glue, $r_pieces );
479 }
480 else {
481 $retVal[] = $r_pieces;
482 }
483 }
484 if ( isset($retVal) && is_array($retVal) )
485 return implode( $glue, $retVal );
486
487 return false;
488 }
489
490 /**
491 * Strip tags and trim
492 *
493 * @since 1.3
494 */
495 protected function strip_trim($string)
496 {
497 return Codepress_Admin_Columns::strip_trim($string);
498 }
499
500 /**
501 * Get date
502 *
503 * @since 1.3.1
504 */
505 protected function get_date( $date )
506 {
507 if ( empty( $date ) || in_array( $date, array( '0000-00-00 00:00:00', '0000-00-00', '00:00:00' ) ) )
508 return false;
509
510 // Parse with strtotime if it's:
511 // - not numeric ( like a unixtimestamp )
512 // - date format: yyyymmdd ( format used by ACF ) must start with 19xx or 20xx and is 8 long
513
514 // @todo: in theory a numeric string of 8 can also be a unixtimestamp.
515 // we need to replace this with an option to mark a date as unixtimestamp.
516 if ( ! is_numeric($date) || ( is_numeric( $date ) && strlen( trim($date) ) == 8 && ( strpos( $date, '20' ) === 0 || strpos( $date, '19' ) === 0 ) ) )
517 $date = strtotime($date);
518
519 return date_i18n( get_option('date_format'), $date );
520 }
521
522 /**
523 * Get time
524 *
525 * @since 1.3.1
526 */
527 protected function get_time($date)
528 {
529 if ( ! $date )
530 return false;
531
532 if ( ! is_numeric($date) )
533 $date = strtotime($date);
534
535 return date_i18n( get_option('time_format'), $date );
536 }
537 }
538
539 ?>