-
Notifications
You must be signed in to change notification settings - Fork 7
/
media.php
133 lines (128 loc) · 5.39 KB
/
media.php
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
<?php
/**
* Imsanity Media Library functions.
*
* @package Imsanity
*/
/**
* Add column header for Imsanity info/actions in the media library listing.
*
* @param array $columns A list of columns in the media library.
* @return array The new list of columns.
*/
function imsanity_media_columns( $columns ) {
$columns['imsanity'] = esc_html__( 'Imsanity', 'imsanity' );
return $columns;
}
/**
* Print Imsanity info/actions in the media library.
*
* @param string $column_name The name of the column being displayed.
* @param int $id The attachment ID number.
* @param array $meta Optional. The attachment metadata. Default null.
*/
function imsanity_custom_column( $column_name, $id, $meta = null ) {
// Once we get to the EWWW IO custom column.
if ( 'imsanity' === $column_name ) {
$id = (int) $id;
if ( is_null( $meta ) ) {
// Retrieve the metadata.
$meta = wp_get_attachment_metadata( $id );
}
echo '<div id="imsanity-media-status-' . (int) $id . '" class="imsanity-media-status" data-id="' . (int) $id . '">';
if ( false && function_exists( 'print_r' ) ) {
$print_meta = print_r( $meta, true );
$print_meta = preg_replace( array( '/ /', '/\n+/' ), array( ' ', '<br />' ), $print_meta );
echo "<div id='imsanity-debug-meta-" . (int) $id . "' style='font-size: 10px;padding: 10px;margin:3px -10px 10px;line-height: 1.1em;'>" . wp_kses_post( $print_meta ) . '</div>';
}
if ( is_array( $meta ) && ! empty( $meta['file'] ) && false !== strpos( $meta['file'], 'https://images-na.ssl-images-amazon.com' ) ) {
echo esc_html__( 'Amazon-hosted image', 'imsanity' ) . '</div>';
return;
}
if ( is_array( $meta ) && ! empty( $meta['cloudinary'] ) ) {
echo esc_html__( 'Cloudinary image', 'imsanity' ) . '</div>';
return;
}
if ( is_array( $meta ) & class_exists( 'WindowsAzureStorageUtil' ) && ! empty( $meta['url'] ) ) {
echo '<div>' . esc_html__( 'Azure Storage image', 'imsanity' ) . '</div>';
return;
}
if ( is_array( $meta ) && class_exists( 'Amazon_S3_And_CloudFront' ) && preg_match( '/^(http|s3|gs)\w*:/', get_attached_file( $id ) ) ) {
echo '<div>' . esc_html__( 'Offloaded Media', 'imsanity' ) . '</div>';
return;
}
if ( is_array( $meta ) && class_exists( 'S3_Uploads' ) && preg_match( '/^(http|s3|gs)\w*:/', get_attached_file( $id ) ) ) {
echo '<div>' . esc_html__( 'Amazon S3 image', 'imsanity' ) . '</div>';
return;
}
if ( is_array( $meta ) & class_exists( 'wpCloud\StatelessMedia' ) && ! empty( $meta['gs_link'] ) ) {
echo '<div>' . esc_html__( 'WP Stateless image', 'imsanity' ) . '</div>';
return;
}
$file_path = imsanity_attachment_path( $meta, $id );
if ( is_array( $meta ) & function_exists( 'ilab_get_image_sizes' ) && ! empty( $meta['s3'] ) && empty( $file_path ) ) {
echo esc_html__( 'Media Cloud image', 'imsanity' ) . '</div>';
return;
}
// If the file does not exist.
if ( empty( $file_path ) ) {
echo esc_html__( 'Could not retrieve file path.', 'imsanity' ) . '</div>';
return;
}
// Let folks filter the allowed mime-types for resizing.
$allowed_types = apply_filters( 'imsanity_allowed_mimes', array( 'image/png', 'image/gif', 'image/jpeg' ), $file_path );
if ( is_string( $allowed_types ) ) {
$allowed_types = array( $allowed_types );
} elseif ( ! is_array( $allowed_types ) ) {
$allowed_types = array();
}
$ftype = imsanity_quick_mimetype( $file_path );
if ( ! in_array( $ftype, $allowed_types, true ) ) {
echo '</div>';
return;
}
list( $imagew, $imageh ) = getimagesize( $file_path );
if ( empty( $imagew ) || empty( $imageh ) ) {
$imagew = $meta['width'];
$imageh = $meta['height'];
}
if ( empty( $imagew ) || empty( $imageh ) ) {
echo esc_html( 'Unknown dimensions', 'imsanity' );
return;
}
echo '<div>' . (int) $imagew . 'w x ' . (int) $imageh . 'h</div>';
$maxw = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
$maxh = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
if ( $imagew > $maxw || $imageh > $maxh ) {
if ( current_user_can( 'activate_plugins' ) ) {
$manual_nonce = wp_create_nonce( 'imsanity-manual-resize' );
// Give the user the option to optimize the image right now.
printf(
'<div><button class="imsanity-manual-resize button button-secondary" data-id="%1$d" data-nonce="%2$s">%3$s</button>',
(int) $id,
esc_attr( $manual_nonce ),
esc_html__( 'Resize Image', 'imsanity' )
);
}
} elseif ( current_user_can( 'activate_plugins' ) && imsanity_get_option( 'imsanity_delete_originals', false ) && ! empty( $meta['original_image'] ) && function_exists( 'wp_get_original_image_path' ) ) {
$original_image = wp_get_original_image_path( $id );
if ( empty( $original_image ) || ! is_file( $original_image ) ) {
$original_image = wp_get_original_image_path( $id, true );
}
if ( ! empty( $original_image ) && is_file( $original_image ) && is_writable( $original_image ) ) {
$link_text = __( 'Remove Original', 'imsanity' );
} else {
$link_text = __( 'Remove Original Link', 'imsanity' );
}
$manual_nonce = wp_create_nonce( 'imsanity-manual-resize' );
// Give the user the option to optimize the image right now.
printf(
'<div><button class="imsanity-manual-remove-original button button-secondary" data-id="%1$d" data-nonce="%2$s">%3$s</button>',
(int) $id,
esc_attr( $manual_nonce ),
esc_html( $link_text )
);
}
echo '</div>';
}
}