FeaturedImage.php
2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace ACP\Settings\Column;
use AC;
class FeaturedImage extends AC\Settings\Column
implements AC\Settings\FormatValue {
/**
* @var string
*/
private $featured_image_display;
protected function set_name() {
$this->name = 'featured_image';
}
protected function define_options() {
return [
'featured_image_display' => 'image',
];
}
public function get_dependent_settings() {
$setting = [];
switch ( $this->get_featured_image_display() ) {
case 'image' :
$setting[] = new AC\Settings\Column\Image( $this->column );
break;
}
return $setting;
}
/**
* @param string|int|false $value
* @param mixed $original_value
*
* @return string|int|false
*/
public function format( $value, $original_value ) {
switch ( $this->get_featured_image_display() ) {
case 'filesize':
$value = $this->get_attachment_size( $value );
break;
}
return $value;
}
private function get_attachment_size( $attachment_id ) {
$file = wp_get_attachment_url( $attachment_id );
$abs = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $file );
if ( ! file_exists( $abs ) ) {
return false;
}
return ac_helper()->file->get_readable_filesize( filesize( $abs ) );
}
public function create_view() {
$select = $this->create_element( 'select' )
->set_attribute( 'data-refresh', 'column' )
->set_options( $this->get_display_options() );
$view = new AC\View( [
'label' => __( 'Display', 'codepress-admin-columns' ),
'setting' => $select,
] );
return $view;
}
protected function get_display_options() {
$options = [
'image' => __( 'Image' ),
'filesize' => __( 'Filesize', 'codepress-admin-columns' ),
];
return $options;
}
/**
* @return string
*/
public function get_featured_image_display() {
return $this->featured_image_display;
}
/**
* @param string $featured_image_display
*/
public function set_featured_image_display( $featured_image_display ) {
$this->featured_image_display = $featured_image_display;
}
}