PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 2.3.2
Admin Columns v2.3.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.php
codepress-admin-columns / classes Last commit date
column 11 years ago storage_model 11 years ago addons.php 11 years ago column.php 11 years ago settings.php 11 years ago storage_model.php 11 years ago third_party.php 11 years ago upgrade.php 11 years ago utility.php 11 years ago
column.php
1202 lines
1 <?php
2 /**
3 * CPAC_Column class
4 *
5 * @since 2.0
6 *
7 * @param object $storage_model CPAC_Storage_Model
8 */
9 class CPAC_Column {
10
11 /**
12 * A Storage Model can be a Posttype, User, Comment, Link or Media storage type.
13 *
14 * @since 2.0
15 * @var CPAC_Storage_Model $storage_model contains a CPAC_Storage_Model object which the column belongs too.
16 */
17 public $storage_model;
18
19 /**
20 * @since 2.0
21 * @var array $options contains the user set options for the CPAC_Column object.
22 */
23 public $options = array();
24
25 /**
26 * @since 2.0
27 * @var object $options_default contains the options for the CPAC_Column object before they are populated with user input.
28 */
29 protected $options_default;
30
31 /**
32 * @since 2.0
33 * @var array $properties describes the fixed properties for the CPAC_Column object.
34 */
35 public $properties = array();
36
37 /**
38 * @since 2.0
39 *
40 * @param int $id ID
41 * @return string Value
42 */
43 public function get_value( $id ) {}
44
45 /**
46 * Get the raw, underlying value for the column
47 * Not suitable for direct display, use get_value() for that
48 *
49 * @since 2.0.3
50 *
51 * @param int $id ID
52 * @return mixed Value
53 */
54 public function get_raw_value( $id ) {}
55
56 /**
57 * @since 2.0
58 */
59 protected function display_settings() {}
60
61 /**
62 * Overwrite this function in child class to sanitize
63 * user submitted values.
64 *
65 * @since 2.0
66 *
67 * @param $options array User submitted column options
68 * @return array Options
69 */
70 protected function sanitize_options( $options ) {
71
72 if ( isset( $options['date_format'] ) ) {
73 $options['date_format'] = trim( $options['date_format'] );
74 }
75
76 return $options;
77 }
78
79 /**
80 * Determine whether this column type should be available
81 *
82 * @since 2.2
83 *
84 * @return bool Whether the column type should be available
85 */
86 public function apply_conditional() {
87
88 return true;
89 }
90
91 /**
92 * An object copy (clone) is created for creating multiple column instances.
93 *
94 * @since 2.0
95 */
96 public function __clone() {
97
98 // Force a copy of this->object, otherwise it will point to same object.
99 $this->options = clone $this->options;
100 $this->properties = clone $this->properties;
101 }
102
103 /**
104 * @since 2.0
105 *
106 * @param object $storage_model CPAC_Storage_Model
107 */
108 public function __construct( CPAC_Storage_Model $storage_model ) {
109
110 $this->storage_model = $storage_model;
111
112 $this->init();
113 $this->after_setup();
114 }
115
116 /**
117 * @since 2.2
118 */
119 public function init() {
120
121 // Default properties
122 $default_properties = array(
123 'clone' => null, // Unique clone ID
124 'type' => null, // Unique type
125 'name' => null, // Unique name
126 'label' => null, // Label which describes this column.
127 'classes' => null, // Custom CSS classes for this column.
128 'hide_label' => false, // Should the Label be hidden?
129 'is_registered' => true, // Should the column be registered based on conditional logic, example usage see: 'post/page-template.php'
130 'is_cloneable' => true, // Should the column be cloneable
131 'default' => false, // Is this a WP default column,
132 'group' => 'custom',
133 'hidden' => false
134 );
135
136 foreach ( $default_properties as $property => $value ) {
137 $this->properties[ $property ] = $value;
138 }
139
140 // Default options
141 $default_options = array(
142 'before' => '', // Before field
143 'after' => '', // After field
144 'width' => null, // Width for this column.
145 'state' => 'off' // Active state for this column.
146 );
147
148 /**
149 * Filter the default options for a column instance, such as label and width
150 *
151 * @since 2.2
152 * @param array $default_options Default column options
153 * @param CPAC_Column $column_instance Column class instance
154 */
155 $default_options = apply_filters( 'cac/column/default_options', $default_options, $this );
156
157 foreach ( $default_options as $option => $value ) {
158 $this->options[ $option ] = $value;
159 }
160 }
161
162 public function after_setup() {
163
164 // Column name defaults to column type
165 if ( ! isset( $this->properties['name'] ) ) {
166 $this->properties['name'] = $this->properties['type'];
167 }
168
169 // Check whether the column should be available
170 $this->properties['is_registered'] = $this->apply_conditional();
171
172 /**
173 * Filter the properties of a column type, such as type and is_cloneable
174 * Property $column_instance added in Admin Columns 2.2
175 *
176 * @since 2.0
177 * @param array $properties Column properties
178 * @param CPAC_Column $column_instance Column class instance
179 */
180 $this->properties = apply_filters( 'cac/column/properties', $this->properties, $this );
181
182 /**
183 * Filter the properties of a column type for a specific storage model
184 * Property $column_instance added in Admin Columns 2.2
185 *
186 * @since 2.0
187 * @see Filter cac/column/properties
188 */
189 $this->properties = apply_filters( "cac/column/properties/storage_key={$this->storage_model->key}", $this->properties, $this );
190
191 // Column label defaults to column type label
192 if ( ! isset( $this->options['label'] ) ) {
193 $this->options['label'] = $this->properties['label'];
194 }
195
196 // Convert properties and options arrays to object
197 $this->options = (object) $this->options;
198 $this->properties = (object) $this->properties;
199
200 // Read options from database
201 $this->populate_options();
202
203 $this->sanitize_label();
204 }
205
206 /**
207 * Populate Options
208 * Added $options parameter in 2.2
209 *
210 * @since 2.0
211 * @param array $options Optional. Options to populate the storage model with. Defaults to options from database.
212 */
213 public function populate_options( $options = NULL ) {
214 $this->options = (object) array_merge( (array) $this->options, is_array( $options ) ? $options : $this->read() );
215 }
216
217 /**
218 * @param string $property
219 * @return mixed $value
220 */
221 public function set_properties( $property, $value ) {
222 $this->properties->{$property} = $value;
223
224 return $this;
225 }
226
227 /**
228 * @param string $option
229 * @return mixed $value
230 */
231 public function set_options( $option, $value ) {
232 $this->options->{$option} = $value;
233
234 return $this;
235 }
236
237 /**
238 * @param int $id
239 * @return object
240 */
241 public function set_clone( $id = null ) {
242
243 if ( $id !== null && $id > 0 ) {
244 $this->properties->name = "{$this->properties->type}-{$id}";
245 $this->properties->clone = $id;
246 }
247
248 return $this;
249 }
250
251 /**
252 * @since 1.0
253 */
254 public function get_before() {
255 return stripslashes( $this->options->before );
256 }
257
258 /**
259 * @since 1.0
260 */
261 public function get_after() {
262 return stripslashes( $this->options->after );
263 }
264
265 /**
266 * @since 3.2.1
267 */
268 public function get_type() {
269 return $this->properties->type;
270 }
271
272 /**
273 * Checks column type
274 *
275 * @since 3.2.1
276 * @param string $type Column type. Also work without the 'column-' prefix. Example 'column-meta' or 'meta'.
277 * @return bool Matches column type
278 */
279 public function is_type( $type ) {
280 return ( $type === $this->get_type() ) || ( 'column-' . $type === $this->get_type() );
281 }
282
283 /**
284 * @since 2.1.1
285 */
286 public function get_post_type() {
287 return $this->storage_model->get_post_type();
288 }
289
290 /**
291 * @param string $field_key
292 * @return void
293 */
294 public function attr_name( $field_name ) {
295 echo "{$this->storage_model->key}[{$this->properties->name}][{$field_name}]";
296 }
297
298 /**
299 * @param string $field_key
300 * @return string Attribute Name
301 */
302 public function attr_id( $field_name ) {
303 echo "cpac-{$this->storage_model->key}-{$this->properties->name}-{$field_name}";
304 }
305
306 /**
307 * @since 2.0
308 * @return array Column options
309 */
310 public function read() {
311 $options = (array) get_option( "cpac_options_{$this->storage_model->key}" );
312
313 if ( empty( $options[ $this->properties->name ] ) ) {
314 return array();
315 }
316
317 return $options[ $this->properties->name ];
318 }
319
320 /**
321 * @since 2.0
322 */
323 public function sanitize_label() {
324 // check if original label has changed. Example WPML adds a language column, the column heading will have to display the added flag.
325 if ( $this->properties->hide_label && $this->properties->label !== $this->options->label ) {
326 $this->options->label = $this->properties->label;
327 }
328
329 // replace urls, so export will not have to deal with them
330 $this->options->label = stripslashes( str_replace( '[cpac_site_url]', site_url(), $this->options->label ) );
331 }
332
333 /**
334 * @since 2.0
335 * @param $options array User submitted column options
336 * @return array Options
337 */
338 public function sanitize_storage( $options ) {
339
340 // excerpt length must be numeric, else we will return it's default
341 if ( isset( $options['excerpt_length'] ) ) {
342 $options['excerpt_length'] = trim( $options['excerpt_length'] );
343 if ( empty( $options['excerpt_length'] ) || ! is_numeric( $options['excerpt_length'] ) ) {
344 $options['excerpt_length'] = $this->options_default->excerpt_length;
345 }
346 }
347
348 if ( ! empty( $options['label'] ) ) {
349
350 // Label can not contains the character ':', because
351 // CPAC_Column::get_sanitized_label() will return an empty string
352 // and make an exception for site_url()
353 if ( false === strpos( $options['label'], site_url() ) ) {
354 $options['label'] = str_replace( ':', '', $options['label'] );
355 }
356 }
357
358 // used by child classes for additional sanitizing
359 $options = $this->sanitize_options( $options );
360
361 return $options;
362 }
363
364 /**
365 * @since 2.0
366 */
367 public function get_label() {
368
369 /**
370 * Filter the column instance label
371 *
372 * @since 2.0
373 *
374 * @param string $label Column instance label
375 * @param CPAC_Column $column_instance Column class instance
376 */
377 return apply_filters( 'cac/column/settings_label', stripslashes( str_replace( '[cpac_site_url]', site_url(), $this->options->label ) ), $this );
378 }
379
380 /**
381 * Sanitizes label using intern wordpress function esc_url so it matches the label sorting url.
382 *
383 * @since 1.0
384 * @param string $string
385 * @return string Sanitized string
386 */
387 public function get_sanitized_label() {
388 $string = esc_url( $this->options->label );
389 $string = str_replace( 'http://', '', $string );
390 $string = str_replace( 'https://', '', $string );
391
392 return $string;
393 }
394
395 /**
396 * @since 1.3.1
397 */
398 protected function get_shorten_url( $url = '' ) {
399 if ( ! $url ) {
400 return false;
401 }
402
403 return "<a title='{$url}' href='{$url}'>" . url_shorten( $url ) . "</a>";
404 }
405
406 /**
407 * @since 1.3
408 */
409 protected function strip_trim( $string ) {
410 return trim( strip_tags( $string ) );
411 }
412
413 /**
414 * @since 2.2.1
415 */
416 protected function get_term_field( $field, $term_id, $taxonomy ) {
417 $term_field = get_term_field( $field, $term_id, $taxonomy, 'display' );
418 if ( is_wp_error( $term_field ) ) {
419 return false;
420 }
421 return $term_field;
422 }
423
424 /**
425 * @since 1.0
426 * @param int $post_id Post ID
427 * @return string Post Excerpt.
428 */
429 protected function get_post_excerpt( $post_id, $words ) {
430 global $post;
431
432 $save_post = $post;
433 $post = get_post( $post_id );
434
435 setup_postdata( $post );
436
437 $excerpt = get_the_excerpt();
438 $post = $save_post;
439
440 if ( $post ) {
441 setup_postdata( $post );
442 }
443
444 $output = $this->get_shortened_string( $excerpt, $words );
445
446 return $output;
447 }
448
449 /**
450 * @see wp_trim_words();
451 * @since 1.0
452 * @return string Trimmed text.
453 */
454 protected function get_shortened_string( $text = '', $num_words = 30, $more = null ) {
455 if ( ! $text ) {
456 return false;
457 }
458
459 return wp_trim_words( $text, $num_words, $more );
460 }
461
462 /**
463 * @since 1.3.1
464 * @param string $name
465 * @param string $title
466 * @return string HTML img element
467 */
468 public function get_asset_image( $name = '', $title = '' ) {
469
470 if ( ! $name ) {
471 return false;
472 }
473
474 return sprintf( "<img alt='' src='%s' title='%s'/>", CPAC_URL . "assets/images/{$name}", esc_attr( $title ) );
475 }
476
477 /**
478 * @since 1.2.0
479 * @param string $url
480 * @return bool
481 */
482 protected function is_image_url( $url ) {
483
484 if ( ! is_string( $url ) ) {
485 return false;
486 }
487
488 $validExt = array('.jpg', '.jpeg', '.gif', '.png', '.bmp');
489 $ext = strrchr( $url, '.' );
490
491 return in_array( $ext, $validExt );
492 }
493
494 /**
495 * @since 1.0
496 * @return array Image Sizes.
497 */
498 public function get_all_image_sizes() {
499 $image_sizes = array(
500 'thumbnail' => __( "Thumbnail", 'cpac' ),
501 'medium' => __( "Medium", 'cpac' ),
502 'large' => __( "Large", 'cpac' ),
503 'full' => __( "Full", 'cpac' )
504 );
505
506 foreach( get_intermediate_image_sizes() as $size ) {
507 if ( ! isset( $image_sizes[$size] ) ) {
508 $image_sizes[$size] = ucwords( str_replace( '-', ' ', $size) );
509 }
510 }
511
512 return $image_sizes;
513 }
514
515 /**
516 * @since 2.2.6
517 */
518 public function get_terms_for_display( $term_ids, $taxonomy ) {
519 $values = array();
520 $term_ids = (array) $term_ids;
521 if ( $term_ids && ! is_wp_error( $term_ids ) ) {
522 $post_type = $this->get_post_type();
523 foreach ( $term_ids as $term_id ) {
524 $term = get_term( $term_id, $taxonomy );
525 $title = esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, $term->taxonomy, 'edit' ) );
526
527 $filter_key = $term->taxonomy;
528 if ( 'category' === $term->taxonomy ) {
529 $filter_key = 'category_name';
530 }
531
532 $link = "<a href='edit.php?post_type={$post_type}&{$filter_key}={$term->slug}'>{$title}</a>";
533 if ( $post_type == 'attachment' ) {
534 $link = "<a href='upload.php?taxonomy={$filter_key}&term={$term->slug}'>{$title}</a>";
535 }
536
537 $values[] = $link;
538 }
539 }
540 if ( ! $values ) {
541 return false;
542 }
543
544 return implode( ', ', $values );
545 }
546
547 /**
548 * @since 2.0
549 * @param string $name
550 * @return array Image Sizes
551 */
552 public function get_image_size_by_name( $name = '' ) {
553
554 if ( ! $name || is_array( $name ) ) {
555 return false;
556 }
557
558 global $_wp_additional_image_sizes;
559
560 if ( ! isset( $_wp_additional_image_sizes[ $name ] ) ) {
561 return false;
562 }
563
564 return $_wp_additional_image_sizes[ $name ];
565 }
566
567 /**
568 * @see image_resize()
569 * @since 2.0
570 * @return string Image URL
571 */
572 public function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
573 $resized = false;
574 $editor = wp_get_image_editor( $file );
575
576 if ( is_wp_error( $editor ) )
577 return false;
578
579 $editor->set_quality( $jpeg_quality );
580
581 $resized = $editor->resize( $max_w, $max_h, $crop );
582 if ( is_wp_error( $resized ) )
583 return false;
584
585 $dest_file = $editor->generate_filename( $suffix, $dest_path );
586
587 $saved = $editor->save( $dest_file );
588
589 if ( is_wp_error( $saved ) )
590 return false;
591
592 $resized = $dest_file;
593
594 return $resized;
595 }
596
597 /**
598 * @since: 2.2.6
599 *
600 */
601 public function get_color_for_display( $color_hex ) {
602 if ( ! $color_hex ) {
603 return false;
604 }
605 $text_color = $this->get_text_color( $color_hex );
606 return "<div class='cpac-color'><span style='background-color:{$color_hex};color:{$text_color}'>{$color_hex}</span></div>";
607 }
608
609 /**
610 * Determines text color absed on bakground coloring.
611 *
612 * @since 1.0
613 */
614 public function get_text_color( $bg_color ) {
615
616 $rgb = $this->hex2rgb( $bg_color );
617
618 return $rgb && ( ( $rgb[0]*0.299 + $rgb[1]*0.587 + $rgb[2]*0.114 ) < 186 ) ? '#ffffff' : '#333333';
619 }
620
621 /**
622 * Convert hex to rgb
623 *
624 * @since 1.0
625 */
626 public function hex2rgb( $hex ) {
627 $hex = str_replace( "#", "", $hex );
628
629 if(strlen($hex) == 3) {
630 $r = hexdec(substr($hex,0,1).substr($hex,0,1));
631 $g = hexdec(substr($hex,1,1).substr($hex,1,1));
632 $b = hexdec(substr($hex,2,1).substr($hex,2,1));
633 } else {
634 $r = hexdec(substr($hex,0,2));
635 $g = hexdec(substr($hex,2,2));
636 $b = hexdec(substr($hex,4,2));
637 }
638 $rgb = array($r, $g, $b);
639
640 return $rgb;
641 }
642
643 /**
644 * @since 1.0
645 * @param mixed $meta Image files or Image ID's
646 * @param array $args
647 * @return array HTML img elements
648 */
649 public function get_thumbnails( $images, $args = array() ) {
650
651 if ( empty( $images ) || 'false' == $images ) {
652 return array();
653 }
654
655 // turn string to array
656 if ( is_string( $images ) || is_numeric( $images ) ) {
657 if ( strpos( $images, ',' ) !== false ) {
658 $images = array_filter( explode( ',', $this->strip_trim( str_replace( ' ', '', $images ) ) ) );
659 }
660 else {
661 $images = array( $images );
662 }
663 }
664
665 // Image size
666 $defaults = array(
667 'image_size' => 'cpac-custom',
668 'image_size_w' => 80,
669 'image_size_h' => 80,
670 );
671 $args = wp_parse_args( $args, $defaults );
672
673 extract( $args );
674
675 $thumbnails = array();
676 foreach( $images as $value ) {
677
678 if ( $this->is_image_url( $value ) ) {
679
680 // get dimensions from image_size
681 if ( $sizes = $this->get_image_size_by_name( $image_size ) ) {
682 $image_size_w = $sizes['width'];
683 $image_size_h = $sizes['height'];
684 }
685
686 $image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $value );
687
688 if ( is_file( $image_path ) ) {
689
690 // try to resize image
691 if ( $resized = $this->image_resize( $image_path, $image_size_w, $image_size_h, true ) ) {
692 $thumbnails[] = "<img src='" . str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $resized ) . "' alt='' width='{$image_size_w}' height='{$image_size_h}' />";
693 }
694
695 // return full image with maxed dimensions
696 else {
697 $thumbnails[] = "<img src='{$value}' alt='' style='max-width:{$image_size_w}px;max-height:{$image_size_h}px' />";
698 }
699 }
700 }
701
702 // Media Attachment
703 elseif ( is_numeric( $value ) && wp_get_attachment_url( $value ) ) {
704
705 $src = '';
706 $width = '';
707 $height = '';
708
709 if ( ! $image_size || 'cpac-custom' == $image_size ) {
710 $width = $image_size_w;
711 $height = $image_size_h;
712
713 // to make sure wp_get_attachment_image_src() get the image with matching dimensions.
714 $image_size = array( $width, $height );
715 }
716
717 // Is Image
718 if ( $attributes = wp_get_attachment_image_src( $value, $image_size ) ) {
719 $src = $attributes[0];
720 $width = $attributes[1];
721 $height = $attributes[2];
722
723 // image size by name
724 if ( $sizes = $this->get_image_size_by_name( $image_size ) ) {
725 $width = $sizes['width'];
726 $height = $sizes['height'];
727 }
728 }
729
730 // Is File, use icon
731 elseif ( $attributes = wp_get_attachment_image_src( $value, $image_size, true ) ) {
732 $src = $attributes[0];
733
734 if ( $sizes = $this->get_image_size_by_name( $image_size ) ) {
735 $width = $sizes['width'];
736 $height = $sizes['height'];
737 }
738 }
739
740 // maximum dimensions
741 $max = max( array( $width, $height ) );
742
743 $thumbnails[] = "<span class='cpac-column-value-image' style='width:{$width}px;height:{$height}px;'><img style='max-width:{$max}px;max-height:{$max}px;' src='{$src}' alt=''/></span>";
744 }
745 }
746
747 return $thumbnails;
748 }
749
750 /**
751 * Implode for multi dimensional array
752 *
753 * @since 1.0
754 * @param string $glue
755 * @param array $pieces
756 * @return string Imploded array
757 */
758 public function recursive_implode( $glue, $pieces ) {
759 foreach( $pieces as $r_pieces ) {
760 if ( is_array( $r_pieces ) ) {
761 $retVal[] = $this->recursive_implode( $glue, $r_pieces );
762 }
763 else {
764 $retVal[] = $r_pieces;
765 }
766 }
767 if ( isset($retVal) && is_array( $retVal ) ) {
768 return implode( $glue, $retVal );
769 }
770
771 return false;
772 }
773
774 /**
775 * Get timestamp
776 *
777 * @since 2.0
778 * @param string $date
779 * @return string Formatted date
780 */
781 public function get_timestamp( $date ) {
782
783 if ( empty( $date ) || in_array( $date, array( '0000-00-00 00:00:00', '0000-00-00', '00:00:00' ) ) ) {
784 return false;
785 }
786
787 // some plugins store dates in a jquery timestamp format, format is in ms since The Epoch
788 // See http://api.jqueryui.com/datepicker/#utility-formatDate
789 // credits: nmarks
790 if ( is_numeric( $date ) && 13 === strlen( trim( $date ) ) ) {
791 $date = substr( $date, 0, -3 );
792 }
793
794 // Parse with strtotime if it's:
795 // - not numeric ( like a unixtimestamp )
796 // - date format: yyyymmdd ( format used by ACF ) must start with 19xx or 20xx and is 8 long
797
798 // @todo: in theory a numeric string of 8 can also be a unixtimestamp.
799 // we need to replace this with an option to mark a date as unixtimestamp.
800 if ( ! is_numeric( $date ) || ( is_numeric( $date ) && strlen( trim( $date ) ) == 8 && ( strpos( $date, '20' ) === 0 || strpos( $date, '19' ) === 0 ) ) ) {
801 $date = strtotime( $date );
802 }
803
804 return $date;
805 }
806
807 /**
808 * @since 1.3.1
809 * @param string $date
810 * @return string Formatted date
811 */
812 protected function get_date( $date, $format = '' ) {
813
814 if ( ! $date = $this->get_timestamp( $date ) ) {
815 return false;
816 }
817 if ( ! $format ) {
818 $format = get_option( 'date_format' );
819 }
820
821 return date_i18n( $format, $date );
822 }
823
824 /**
825 * @since 1.3.1
826 * @param string $date
827 * @return string Formatted time
828 */
829 protected function get_time( $date, $format = '' ) {
830
831 if ( ! $date = $this->get_timestamp( $date ) ) {
832 return false;
833 }
834 if ( ! $format ) {
835 $format = get_option( 'time_format' );
836 }
837
838 return date_i18n( $format, $date );
839 }
840
841 /**
842 * Get display name.
843 *
844 * Can also be used by addons.
845 *
846 * @since 2.0
847 */
848 public function get_display_name( $user_id ) {
849
850 if ( ! $userdata = get_userdata( $user_id ) ) {
851 return false;
852 }
853
854 $name = '';
855
856 if ( ! empty( $this->options->display_author_as ) ) {
857
858 $display_as = $this->options->display_author_as;
859
860 if ( 'first_last_name' == $display_as ) {
861 $first = ! empty( $userdata->first_name ) ? $userdata->first_name : '';
862 $last = ! empty( $userdata->last_name ) ? " {$userdata->last_name}" : '';
863 $name = $first.$last;
864 }
865
866 elseif ( ! empty( $userdata->{$display_as} ) ) {
867 $name = $userdata->{$display_as};
868 }
869 }
870
871 // default to display_name
872 if ( ! $name ) {
873 $name = $userdata->display_name;
874 }
875
876 return $name;
877 }
878
879 /**
880 * @since 2.0
881 * @param string $field_key
882 * @return string Attribute Name
883 */
884 public function label_view( $label, $description = '', $pointer = '' ) {
885 ?>
886 <td class="label">
887 <label for="<?php $this->attr_id( $pointer ); ?>">
888 <?php echo stripslashes( $label ); ?>
889
890 <?php if( $description ) : ?><p class="description"><?php echo $description; ?></p><?php endif; ?>
891 </label>
892 </td>
893 <?php
894 }
895
896 /**
897 * @since 2.0
898 */
899 public function display_field_date_format() {
900
901 $field_key = 'date_format';
902 $label = __( 'Date Format', 'cpac' );
903 $description = __( 'This will determine how the date will be displayed.', 'cpac' );
904
905 ?>
906 <tr class="column_<?php echo $field_key; ?>">
907 <?php $this->label_view( $label, $description, $field_key ); ?>
908 <td class="input">
909 <input type="text" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>" value="<?php echo $this->options->date_format; ?>" placeholder="<?php _e( 'Example:', 'cpac' ); ?> d M Y H:i"/>
910 <p class="description">
911 <?php printf( __( 'Leave empty for WordPress date format, change your <a href="%s">default date format here</a>.' , 'cpac' ), admin_url( 'options-general.php' ) . '#date_format_custom_radio' ); ?>
912 <a target='_blank' href='http://codex.wordpress.org/Formatting_Date_and_Time'><?php _e( 'Documentation on date and time formatting.', 'cpac' ); ?></a>
913 </p>
914 </td>
915 </tr>
916
917 <?php
918 }
919
920 /**
921 * @since 2.0
922 */
923 public function display_field_excerpt_length() {
924
925 $field_key = 'excerpt_length';
926 $label = __( 'Excerpt length', 'cpac' );
927 $description = __( 'Number of words', 'cpac' );
928
929 ?>
930 <tr class="column_<?php echo $field_key; ?>">
931 <?php $this->label_view( $label, $description, $field_key ); ?>
932 <td class="input">
933 <input type="text" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>" value="<?php echo $this->options->excerpt_length; ?>"/>
934 </td>
935 </tr>
936 <?php
937 }
938
939 /**
940 * @since 2.0
941 */
942 public function display_field_preview_size() {
943
944 $field_key = 'image_size';
945 $label = __( 'Preview size', 'cpac' );
946
947 ?>
948 <tr class="column_<?php echo $field_key; ?>">
949
950 <?php $this->label_view( $label, '', $field_key ); ?>
951
952 <td class="input">
953 <?php foreach ( $sizes = $this->get_all_image_sizes() as $id => $image_label ) : ?>
954 <label for="<?php $this->attr_id( $field_key ); ?>-<?php echo $id ?>" class="custom-size">
955 <input type="radio" value="<?php echo $id; ?>" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>-<?php echo $id ?>"<?php checked( $this->options->image_size, $id ); ?>>
956 <?php echo $image_label; ?>
957 </label>
958 <?php endforeach; ?>
959
960 <div class="custom_image_size">
961 <label for="<?php $this->attr_id( $field_key ); ?>-custom" class="custom-size image-size-custom" >
962 <input type="radio" value="cpac-custom" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>-custom"<?php checked( $this->options->image_size, 'cpac-custom' ); ?>><?php _e( 'Custom', 'cpac' ); ?>
963 </label>
964 <label for="<?php $this->attr_id( $field_key ); ?>-w" class="custom-size-w<?php echo $this->options->image_size != 'cpac-custom' ? ' hidden' : ''; ?>">
965 <input type="text" name="<?php $this->attr_name( 'image_size_w' ); ?>" id="<?php $this->attr_id( $field_key ); ?>-w" value="<?php echo $this->options->image_size_w; ?>" /><?php _e( 'width', 'cpac' ); ?>
966 </label>
967 <label for="<?php $this->attr_id( $field_key ); ?>-h" class="custom-size-h<?php echo $this->options->image_size != 'cpac-custom' ? ' hidden' : ''; ?>">
968 <input type="text" name="<?php $this->attr_name( 'image_size_h' ); ?>" id="<?php $this->attr_id( $field_key ); ?>-h" value="<?php echo $this->options->image_size_h; ?>" /><?php _e( 'height', 'cpac' ); ?>
969 </label>
970 </div>
971 </td>
972 </tr>
973 <?php
974 }
975
976 /**
977 * @since 2.1.1
978 */
979 public function display_field_before_after() {
980 ?>
981 <tr class="column_before">
982 <?php $this->label_view( __( "Before", 'cpac' ), __( 'This text will appear before the custom field value.', 'cpac' ), 'before' ); ?>
983 <td class="input">
984 <input type="text" class="cpac-before" name="<?php $this->attr_name( 'before' ); ?>" id="<?php $this->attr_id( 'before' ); ?>" value="<?php echo esc_attr( stripslashes( $this->options->before ) ); ?>"/>
985 </td>
986 </tr>
987 <tr class="column_after">
988 <?php $this->label_view( __( "After", 'cpac' ), __( 'This text will appear after the custom field value.', 'cpac' ), 'after' ); ?>
989 <td class="input">
990 <input type="text" class="cpac-after" name="<?php $this->attr_name( 'after' ); ?>" id="<?php $this->attr_id( 'after' ); ?>" value="<?php echo esc_attr( stripslashes( $this->options->after ) ); ?>"/>
991 </td>
992 </tr>
993 <?php
994 }
995
996 /**
997 * @since 2.3.2
998 */
999 public function display_field_user_format() {
1000
1001 $nametypes = array(
1002 'display_name' => __( 'Display Name', 'cpac' ),
1003 'first_name' => __( 'First Name', 'cpac' ),
1004 'last_name' => __( 'Last Name', 'cpac' ),
1005 'nickname' => __( 'Nickname', 'cpac' ),
1006 'user_login' => __( 'User Login', 'cpac' ),
1007 'user_email' => __( 'User Email', 'cpac' ),
1008 'ID' => __( 'User ID', 'cpac' ),
1009 'first_last_name' => __( 'First and Last Name', 'cpac' ),
1010 );
1011
1012 ?>
1013 <tr class="column-author-name">
1014 <?php $this->label_view( __( 'Display format', 'cpac' ), __( 'This is the format of the author name.', 'cpac' ), 'display_author_as' ); ?>
1015 <td class="input">
1016 <select name="<?php $this->attr_name( 'display_author_as' ); ?>" id="<?php $this->attr_id( 'display_author_as' ); ?>">
1017 <?php foreach ( $nametypes as $key => $label ) : ?>
1018 <option value="<?php echo $key; ?>"<?php selected( $key, $this->options->display_author_as ) ?>><?php echo $label; ?></option>
1019 <?php endforeach; ?>
1020 </select>
1021 </td>
1022 </tr>
1023 <?php
1024 }
1025
1026 /**
1027 * @since 2.0
1028 * @param array Column Objects
1029 * @return string HTML List
1030 */
1031 public function get_column_list( $columns = array(), $label = '' ) {
1032
1033 if ( empty( $columns ) ) {
1034 return false;
1035 }
1036
1037 $list = '';
1038
1039 // sort by alphabet
1040 $_columns = array();
1041
1042 foreach ( $columns as $column ) {
1043 if ( $column->properties->hidden ) {
1044 continue;
1045 }
1046
1047 $_columns[ $column->properties->type ] = ( 0 === strlen( strip_tags( $column->properties->label ) ) ) ? ucfirst( $column->properties->type ) : $column->properties->label;
1048 }
1049
1050 asort( $_columns );
1051
1052 $list = "<optgroup label='{$label}'>";
1053 foreach ( $_columns as $type => $label ) {
1054 $selected = selected( $this->properties->type, $type, false );
1055 $list .= "<option value='{$type}'{$selected}>{$label}</option>";
1056 }
1057 $list .= "</optgroup>";
1058
1059 return $list;
1060 }
1061
1062 /**
1063 * @since 2.0
1064 */
1065 public function display() {
1066
1067 $classes = implode( ' ', array_filter( array ( "cpac-box-{$this->properties->type}", $this->properties->classes ) ) );
1068
1069 // column list
1070 $column_list = '';
1071
1072 $groups = $this->storage_model->get_column_type_groups();
1073 foreach ( $groups as $group => $label ) {
1074 $column_list .= $this->get_column_list( $this->storage_model->column_types[ $group ], $label );
1075 }
1076
1077 // clone attribute
1078 $data_clone = $this->properties->is_cloneable ? " data-clone='{$this->properties->clone}'" : '';
1079
1080 ?>
1081 <div class="cpac-column <?php echo $classes; ?>" data-type="<?php echo $this->properties->type; ?>"<?php echo $data_clone; ?>>
1082 <input type="hidden" class="column-name" name="<?php echo $this->attr_name( 'column-name' ); ?>" value="<?php echo esc_attr( $this->properties->name ); ?>" />
1083 <input type="hidden" class="type" name="<?php echo $this->attr_name( 'type' ); ?>" value="<?php echo $this->properties->type; ?>" />
1084 <input type="hidden" class="clone" name="<?php echo $this->attr_name( 'clone' ); ?>" value="<?php echo $this->properties->clone; ?>" />
1085 <div class="column-meta">
1086 <table class="widefat">
1087 <tbody>
1088 <tr>
1089 <td class="column_sort"></td>
1090 <td class="column_label">
1091 <div class="inner">
1092 <div class="meta">
1093
1094 <?php
1095 /**
1096 * Fires in the meta-element for column options, which is displayed right after the column label
1097 *
1098 * @since 2.0
1099 *
1100 * @param CPAC_Column $column_instance Column class instance
1101 */
1102 do_action( 'cac/column/settings_meta', $this );
1103
1104 /**
1105 * @deprecated 2.2 Use cac/column/settings_meta instead
1106 */
1107 do_action( 'cac/column/label', $this );
1108 ?>
1109
1110 </div>
1111 <a class="toggle" href="javascript:;"><?php echo stripslashes( $this->get_label() ); ?></a>
1112 <a class="edit-button" href="javascript:;"><?php _e( 'Edit', 'cpac' ); ?></a>
1113 <a class="remove-button" href="javascript:;"><?php _e( 'Remove', 'cpac' ); ?></a>
1114 </div>
1115 </td>
1116 <td class="column_type">
1117 <div class="inner">
1118 <a href="#"><?php echo stripslashes( $this->properties->label ); ?></a>
1119 </div>
1120 </td>
1121 <td class="column_edit"></td>
1122 </tr>
1123 </tbody>
1124 </table>
1125 </div><!--.column-meta-->
1126
1127 <div class="column-form">
1128 <table class="widefat">
1129 <tbody>
1130 <tr class="column_type">
1131 <?php $this->label_view( __( 'Type', 'cpac' ), __( 'Choose a column type.', 'cpac' ) . '<em>' . __('ID','cpac') . ': ' . $this->properties->type . '</em>', 'type' ); ?>
1132 <td class="input">
1133 <select name="<?php $this->attr_name( 'type' ); ?>" id="<?php $this->attr_id( 'type' ); ?>">
1134 <?php echo $column_list; ?>
1135 </select>
1136 <div class="msg"></div>
1137 </td>
1138 </tr><!--.column_label-->
1139
1140 <tr class="column_label<?php echo $this->properties->hide_label ? ' hidden' : ''; ?>">
1141 <?php $this->label_view( __( 'Label', 'cpac' ), __( 'This is the name which will appear as the column header.', 'cpac' ), 'label' ); ?>
1142 <td class="input">
1143 <input class="text" type="text" name="<?php $this->attr_name( 'label' ); ?>" id="<?php $this->attr_id( 'label' ); ?>" value="<?php echo esc_attr( $this->options->label ); //echo sanitize_text_field( $this->options->label ); ?>" />
1144 </td>
1145 </tr><!--.column_label-->
1146
1147 <tr class="column_width">
1148 <?php $this->label_view( __( 'Width', 'cpac' ), '', 'width' ); ?>
1149 <td class="input">
1150 <div class="description width-decription" title="<?php _e( 'default', 'cpac' ); ?>">
1151 <?php echo $this->options->width > 0 ? $this->options->width . '%' : __( 'default', 'cpac' ); ?>
1152 </div>
1153 <div class="input-width-range"></div>
1154 <input type="hidden" class="input-width" name="<?php $this->attr_name( 'width' ); ?>" id="<?php $this->attr_id( 'width' ); ?>" value="<?php echo $this->options->width; ?>" />
1155
1156 </td>
1157 </tr><!--.column_width-->
1158
1159 <?php
1160 /**
1161 * Fires directly before the custom options for a column are displayed in the column form
1162 *
1163 * @since 2.0
1164 * @param CPAC_Column $column_instance Column class instance
1165 */
1166 do_action( 'cac/column/settings_before', $this );
1167 ?>
1168
1169 <?php
1170 /**
1171 * Load specific column settings.
1172 *
1173 */
1174 $this->display_settings();
1175
1176 ?>
1177
1178 <?php
1179 /**
1180 * Fires directly after the custom options for a column are displayed in the column form
1181 *
1182 * @since 2.0
1183 * @param CPAC_Column $column_instance Column class instance
1184 */
1185 do_action( 'cac/column/settings_after', $this );
1186 ?>
1187
1188 <tr class="column_action">
1189 <td colspan="2">
1190 <p>
1191 <a href="javascript:;" class="remove-button"><?php _e( 'Remove' );?></a>
1192 </p>
1193 </td>
1194 </tr>
1195
1196 </tbody>
1197 </table>
1198 </div>
1199 </div>
1200 <?php
1201 }
1202 }