PluginProbe
Imsanity / 2.7.0
Imsanity v2.7.0
2.9.4 2.9.3 2.9.2 trunk 2.4.3 2.5.0 2.6.0 2.7.0 2.7.2 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.9.0 2.9.1
imsanity / media.php

media.php in Imsanity 2.7.0, at media.php

131 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Imsanity Media Library functions.
4 *
5 * @package Imsanity
6 */
7
8 /**
9 * Add column header for Imsanity info/actions in the media library listing.
10 *
11 * @param array $columns A list of columns in the media library.
12 * @return array The new list of columns.
13 */
14 function imsanity_media_columns( $columns ) {
15 $columns['imsanity'] = esc_html__( 'Imsanity', 'imsanity' );
16 return $columns;
17 }
18
19 /**
20 * Print Imsanity info/actions in the media library.
21 *
22 * @param string $column_name The name of the column being displayed.
23 * @param int $id The attachment ID number.
24 * @param array $meta Optional. The attachment metadata. Default null.
25 */
26 function imsanity_custom_column( $column_name, $id, $meta = null ) {
27 // Once we get to the EWWW IO custom column.
28 if ( 'imsanity' === $column_name ) {
29 $id = (int) $id;
30 if ( is_null( $meta ) ) {
31 // Retrieve the metadata.
32 $meta = wp_get_attachment_metadata( $id );
33 }
34 echo '<div id="imsanity-media-status-' . (int) $id . '" class="imsanity-media-status" data-id="' . (int) $id . '">';
35 if ( false && function_exists( 'print_r' ) ) {
36 $print_meta = print_r( $meta, true );
37 $print_meta = preg_replace( array( '/ /', '/\n+/' ), array( '&nbsp;', '<br />' ), $print_meta );
38 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>';
39 }
40 if ( is_array( $meta ) && ! empty( $meta['file'] ) && false !== strpos( $meta['file'], 'https://images-na.ssl-images-amazon.com' ) ) {
41 echo esc_html__( 'Amazon-hosted image', 'imsanity' ) . '</div>';
42 return;
43 }
44 if ( is_array( $meta ) && ! empty( $meta['cloudinary'] ) ) {
45 echo esc_html__( 'Cloudinary image', 'imsanity' ) . '</div>';
46 return;
47 }
48 if ( is_array( $meta ) & class_exists( 'WindowsAzureStorageUtil' ) && ! empty( $meta['url'] ) ) {
49 echo '<div>' . esc_html__( 'Azure Storage image', 'imsanity' ) . '</div>';
50 return;
51 }
52 if ( is_array( $meta ) && class_exists( 'Amazon_S3_And_CloudFront' ) && preg_match( '/^(http|s3|gs)\w*:/', get_attached_file( $id ) ) ) {
53 echo '<div>' . esc_html__( 'Offloaded Media', 'imsanity' ) . '</div>';
54 return;
55 }
56 if ( is_array( $meta ) && class_exists( 'S3_Uploads' ) && preg_match( '/^(http|s3|gs)\w*:/', get_attached_file( $id ) ) ) {
57 echo '<div>' . esc_html__( 'Amazon S3 image', 'imsanity' ) . '</div>';
58 return;
59 }
60 if ( is_array( $meta ) & class_exists( 'wpCloud\StatelessMedia' ) && ! empty( $meta['gs_link'] ) ) {
61 echo '<div>' . esc_html__( 'WP Stateless image', 'imsanity' ) . '</div>';
62 return;
63 }
64 $file_path = imsanity_attachment_path( $meta, $id );
65 if ( is_array( $meta ) & function_exists( 'ilab_get_image_sizes' ) && ! empty( $meta['s3'] ) && empty( $file_path ) ) {
66 echo esc_html__( 'Media Cloud image', 'imsanity' ) . '</div>';
67 return;
68 }
69 // If the file does not exist.
70 if ( empty( $file_path ) ) {
71 echo esc_html__( 'Could not retrieve file path.', 'imsanity' ) . '</div>';
72 return;
73 }
74 // Let folks filter the allowed mime-types for resizing.
75 $allowed_types = apply_filters( 'imsanity_allowed_mimes', array( 'image/png', 'image/gif', 'image/jpeg' ), $meta['file'] );
76 if ( is_string( $allowed_types ) ) {
77 $allowed_types = array( $allowed_types );
78 } elseif ( ! is_array( $allowed_types ) ) {
79 $allowed_types = array();
80 }
81 $ftype = imsanity_quick_mimetype( $meta['file'] );
82 if ( ! in_array( $ftype, $allowed_types, true ) ) {
83 echo '</div>';
84 return;
85 }
86
87 list( $imagew, $imageh ) = getimagesize( $file_path );
88 if ( empty( $imagew ) || empty( $imageh ) ) {
89 $imagew = $meta['width'];
90 $imageh = $meta['height'];
91 }
92
93 if ( empty( $imagew ) || empty( $imageh ) ) {
94 echo esc_html( 'Unknown dimensions', 'imsanity' );
95 return;
96 }
97 echo '<div>' . (int) $imagew . 'w x ' . (int) $imageh . 'h</div>';
98
99 $maxw = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
100 $maxh = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
101 if ( $imagew > $maxw || $imageh > $maxh ) {
102 if ( current_user_can( 'activate_plugins' ) ) {
103 $manual_nonce = wp_create_nonce( 'imsanity-manual-resize' );
104 // Give the user the option to optimize the image right now.
105 printf(
106 '<div><button class="imsanity-manual-resize button button-secondary" data-id="%1$d" data-nonce="%2$s">%3$s</button>',
107 $id,
108 esc_attr( $manual_nonce ),
109 esc_html__( 'Resize Image', 'imsanity' )
110 );
111 }
112 } elseif ( current_user_can( 'activate_plugins' ) && imsanity_get_option( 'imsanity_delete_originals', false ) && ! empty( $meta['original_image'] ) && function_exists( 'wp_get_original_image_path' ) ) {
113 $original_image = wp_get_original_image_path( $id );
114 if ( empty( $original_image ) || ! is_file( $original_image ) ) {
115 $original_image = wp_get_original_image_path( $id, true );
116 }
117 if ( ! empty( $original_image ) && is_file( $original_image ) && is_writable( $original_image ) ) {
118 $manual_nonce = wp_create_nonce( 'imsanity-manual-resize' );
119 // Give the user the option to optimize the image right now.
120 printf(
121 '<div><button class="imsanity-manual-remove-original button button-secondary" data-id="%1$d" data-nonce="%2$s">%3$s</button>',
122 $id,
123 esc_attr( $manual_nonce ),
124 esc_html__( 'Remove Original', 'imsanity' )
125 );
126 }
127 }
128 echo '</div>';
129 }
130 }
131