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
|
<?php /** * The image field which uploads images via HTML <input type="file">. * * @package Meta Box */
/** * Image field class which uses <input type="file"> to upload. */ class RWMB_Image_Field extends RWMB_File_Field { /** * Enqueue scripts and styles. */ public static function admin_enqueue_scripts() { parent::admin_enqueue_scripts(); wp_enqueue_style( 'rwmb-image', RWMB_CSS_URL . 'image.css', array(), RWMB_VER ); }
/** * Get HTML for uploaded file. * * @param int $file Attachment (file) ID. * @param int $index File index. * @param array $field Field data. * @return string */ protected static function file_html( $file, $index, $field ) { list( $src ) = wp_get_attachment_image_src( $file, 'thumbnail' ); return sprintf( '<li id="item_%s"> <img src="%s"> <div class="rwmb-image-bar"> <a href="%s" target="_blank"><span class="dashicons dashicons-edit"></span></a> | <a href="#" class="rwmb-file-delete" data-attachment_id="%s">×</a> </div> </li>', $file, $src, get_edit_post_link( $file ), $file ); }
/** * Format a single value for the helper functions. * * @param array $field Field parameters. * @param array $value The value. * @return string */ public static function format_single_value( $field, $value ) { $output = '<ul>'; foreach ( $value as $file ) { $img = sprintf( '<img src="%s" alt="%s">', esc_url( $file['url'] ), esc_attr( $file['alt'] ) );
// Link thumbnail to full size image? if ( isset( $args['link'] ) && $args['link'] ) { $img = sprintf( '<a href="%s" title="%s">%s</a>', esc_url( $file['full_url'] ), esc_attr( $file['title'] ), $img ); } $output .= "<li>$img</li>"; } $output .= '</ul>'; return $output; }
/** * Get uploaded file information. * * @param int $file Attachment image ID (post ID). Required. * @param array $args Array of arguments (for size). * * @return array|bool False if file not found. Array of image info on success. */ public static function file_info( $file, $args = array() ) { $path = get_attached_file( $file ); if ( ! $path ) { return false; }
$args = wp_parse_args( $args, array( 'size' => 'thumbnail', ) ); $image = wp_get_attachment_image_src( $file, $args['size'] ); $attachment = get_post( $file ); $info = array( 'ID' => $file, 'name' => basename( $path ), 'path' => $path, 'url' => $image[0], 'full_url' => wp_get_attachment_url( $file ), 'title' => $attachment->post_title, 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content, 'alt' => get_post_meta( $file, '_wp_attachment_image_alt', true ), ); if ( function_exists( 'wp_get_attachment_image_srcset' ) ) { $info['srcset'] = wp_get_attachment_image_srcset( $file ); }
$info = wp_parse_args( $info, wp_get_attachment_metadata( $file ) );
// Do not overwrite width and height by returned value of image meta. $info['width'] = $image[1]; $info['height'] = $image[2];
return $info; } }
|