Images.php
4.12 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace ACP\Column\Post;
use AC;
use ACP\Export;
use ACP\Sorting;
/**
* @since 4.0.8
*/
class Images extends AC\Column
implements Sorting\Sortable, AC\Column\AjaxValue, Export\Exportable {
public function __construct() {
$this->set_type( 'column-images' );
$this->set_label( __( 'Images', 'codepress-admin-columns' ) );
}
public function sorting() {
return new Sorting\Model\Post\ImageFileSizes();
}
public function export() {
return new Export\Model\Post\ImageFileSizes( $this );
}
/**
* Returns the file size and dimensions of the image with a link to the edit media page.
*
* @param int $id
*
* @return string
*/
public function get_ajax_value( $id ) {
$items = [];
foreach ( $this->get_image_urls( $id ) as $url ) {
$size = ac_helper()->image->get_local_image_size( $url );
if ( ! $size ) {
continue;
}
$dimensions = false;
$extension = false;
$link = false;
$size_int = false;
$size_unit = false;
if ( $info = ac_helper()->image->get_local_image_info( $url ) ) {
$dimensions = $info[0] . ' x ' . $info[1];
$extension = image_type_to_extension( $info[2], false );
}
$attachment_id = ac_helper()->media->get_attachment_id_by_url( $url, true );
if ( $attachment_id ) {
$link = get_edit_post_link( $attachment_id );
}
if ( $file_size = ac_helper()->file->get_readable_filesize_as_array( $size ) ) {
$size_int = $file_size[0];
$size_unit = $file_size[1];
}
$items[] = (object) [
'file_size' => $size_int,
'file_size_label' => $size_unit,
'dimensions' => $dimensions,
'extension' => $extension,
'link' => $link,
'file_name' => basename( $url ),
];
}
if ( ! $items ) {
return false;
}
ob_start();
?>
<div class="ac-image-details">
<?php foreach ( $items as $item ) : ?>
<?php if ( $item->link ) : ?>
<a href="<?php echo esc_url( $item->link ); ?>" class="ac-image-info">
<?php echo ac_helper()->html->tooltip( ac_helper()->icon->dashicon( [ 'icon' => 'format-image' ] ), $item->file_name ); ?>
<?php else : ?>
<div class="ac-image-info">
<?php endif; ?>
<?php if ( $item->extension ) : ?>
<span class="image-extension"><?php echo $item->extension; ?></span>
<?php endif; ?>
<?php if ( $item->file_size ) : ?>
<span class="image-file-size"><?php echo $item->file_size; ?><span class="suffix"><?php echo $item->file_size_label; ?></span></span>
<?php endif; ?>
<?php if ( $item->dimensions ) : ?>
<span class="image-dimensions"><?php echo $item->dimensions; ?><span class="suffix">px</span></span>
<?php endif; ?>
<?php if ( $item->link ) : ?>
</a>
<?php else : ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php
return ob_get_clean();
}
public function get_value( $id ) {
$sizes = $this->get_raw_value( $id );
if ( ! $sizes ) {
return $this->get_empty_char();
}
$count = count( $sizes );
$label = ac_helper()->html->rounded( $count );
// File size as label with count
$label .= ac_helper()->file->get_readable_filesize( array_sum( $sizes ) );
// Total images
$tooltip = '<strong>' . sprintf( _n( '%s image', '%s images', $count, 'codepress-admin-columns' ), $count ) . '</strong>';
$value = ac_helper()->html->tooltip( $label, $tooltip );
return ac_helper()->html->get_ajax_toggle_box_link( $id, $value, $this->get_name() );
}
/**
* @param int $id
*
* @return array
*/
public function get_raw_value( $id ) {
$sizes = [];
foreach ( $this->get_image_urls( $id ) as $url ) {
if ( $size = ac_helper()->image->get_local_image_size( $url ) ) {
$sizes[] = $size;
}
}
return $sizes;
}
/**
* @param int $id
*
* @return array
*/
private function get_image_urls( $id ) {
$string = ac_helper()->post->get_raw_field( 'post_content', $id );
/**
* Parsed content for images.
*
* @param string $string
* @param int $id
* @param int $this
*
* @return string
*/
$string = apply_filters( 'ac/column/images/content', $string, $id, $this );
return array_unique( ac_helper()->image->get_image_urls_from_string( $string ) );
}
}