| 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( ' ', '<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' ), $file_path ); |
| 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( $file_path ); |
| 82 |
if ( ! in_array( $ftype, $allowed_types, true ) ) { |
| 83 |
echo '</div>'; |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$dimensions = getimagesize( $file_path ); |
| 88 |
if ( is_array( $dimensions ) && count( $dimensions ) >= 2 ) { |
| 89 |
$imagew = $dimensions[0]; |
| 90 |
$imageh = $dimensions[1]; |
| 91 |
} |
| 92 |
|
| 93 |
if ( empty( $imagew ) || empty( $imageh ) ) { |
| 94 |
$imagew = $meta['width']; |
| 95 |
$imageh = $meta['height']; |
| 96 |
} |
| 97 |
|
| 98 |
if ( empty( $imagew ) || empty( $imageh ) ) { |
| 99 |
echo esc_html__( 'Unknown dimensions', 'imsanity' ); |
| 100 |
return; |
| 101 |
} |
| 102 |
echo '<div>' . (int) $imagew . 'w x ' . (int) $imageh . 'h</div>'; |
| 103 |
|
| 104 |
if ( ! defined( 'IMSANITY_DEFAULT_MAX_WIDTH' ) ) { |
| 105 |
imsanity_init(); |
| 106 |
} |
| 107 |
$maxw = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH ); |
| 108 |
$maxh = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT ); |
| 109 |
$permissions = apply_filters( 'imsanity_editor_permissions', 'edit_others_posts' ); |
| 110 |
if ( $imagew > $maxw || $imageh > $maxh ) { |
| 111 |
if ( current_user_can( $permissions ) ) { |
| 112 |
$manual_nonce = wp_create_nonce( 'imsanity-manual-resize' ); |
| 113 |
// Give the user the option to optimize the image right now. |
| 114 |
printf( |
| 115 |
'<div><button class="imsanity-manual-resize button button-secondary" data-id="%1$d" data-nonce="%2$s">%3$s</button>', |
| 116 |
(int) $id, |
| 117 |
esc_attr( $manual_nonce ), |
| 118 |
esc_html__( 'Resize Image', 'imsanity' ) |
| 119 |
); |
| 120 |
} |
| 121 |
} elseif ( current_user_can( $permissions ) && imsanity_get_option( 'imsanity_delete_originals', false ) && ! empty( $meta['original_image'] ) && function_exists( 'wp_get_original_image_path' ) ) { |
| 122 |
$original_image = wp_get_original_image_path( $id ); |
| 123 |
if ( empty( $original_image ) || ! is_file( $original_image ) ) { |
| 124 |
$original_image = wp_get_original_image_path( $id, true ); |
| 125 |
} |
| 126 |
if ( ! empty( $original_image ) && is_file( $original_image ) && is_writable( $original_image ) ) { |
| 127 |
$link_text = __( 'Remove Original', 'imsanity' ); |
| 128 |
} else { |
| 129 |
$link_text = __( 'Remove Original Link', 'imsanity' ); |
| 130 |
} |
| 131 |
$manual_nonce = wp_create_nonce( 'imsanity-manual-resize' ); |
| 132 |
// Give the user the option to optimize the image right now. |
| 133 |
printf( |
| 134 |
'<div><button class="imsanity-manual-remove-original button button-secondary" data-id="%1$d" data-nonce="%2$s">%3$s</button>', |
| 135 |
(int) $id, |
| 136 |
esc_attr( $manual_nonce ), |
| 137 |
esc_html( $link_text ) |
| 138 |
); |
| 139 |
} |
| 140 |
echo '</div>'; |
| 141 |
} |
| 142 |
} |
| 143 |
|