Plugin Directory

source: imsanity/tags/2.8.7/media.php

Last change on this file was 3340527, checked in by nosilver4u, 4 months ago

tagging and releasing 2.8.7

File size: 5.5 KB
Line 
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 */
14function 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 */
26function 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' ), $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                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                $permissions = apply_filters( 'imsanity_editor_permissions', 'edit_others_posts' );
102                if ( $imagew > $maxw || $imageh > $maxh ) {
103                        if ( current_user_can( $permissions ) ) {
104                                $manual_nonce = wp_create_nonce( 'imsanity-manual-resize' );
105                                // Give the user the option to optimize the image right now.
106                                printf(
107                                        '<div><button class="imsanity-manual-resize button button-secondary" data-id="%1$d" data-nonce="%2$s">%3$s</button>',
108                                        (int) $id,
109                                        esc_attr( $manual_nonce ),
110                                        esc_html__( 'Resize Image', 'imsanity' )
111                                );
112                        }
113                } elseif ( current_user_can( $permissions ) && imsanity_get_option( 'imsanity_delete_originals', false ) && ! empty( $meta['original_image'] ) && function_exists( 'wp_get_original_image_path' ) ) {
114                        $original_image = wp_get_original_image_path( $id );
115                        if ( empty( $original_image ) || ! is_file( $original_image ) ) {
116                                $original_image = wp_get_original_image_path( $id, true );
117                        }
118                        if ( ! empty( $original_image ) && is_file( $original_image ) && is_writable( $original_image ) ) {
119                                $link_text = __( 'Remove Original', 'imsanity' );
120                        } else {
121                                $link_text = __( 'Remove Original Link', 'imsanity' );
122                        }
123                        $manual_nonce = wp_create_nonce( 'imsanity-manual-resize' );
124                        // Give the user the option to optimize the image right now.
125                        printf(
126                                '<div><button class="imsanity-manual-remove-original button button-secondary" data-id="%1$d" data-nonce="%2$s">%3$s</button>',
127                                (int) $id,
128                                esc_attr( $manual_nonce ),
129                                esc_html( $link_text )
130                        );
131                }
132                echo '</div>';
133        }
134}
Note: See TracBrowser for help on using the repository browser.