-
Notifications
You must be signed in to change notification settings - Fork 562
Expand file tree
/
Copy pathCMB2_Type_File.php
More file actions
169 lines (140 loc) · 5.01 KB
/
Copy pathCMB2_Type_File.php
File metadata and controls
169 lines (140 loc) · 5.01 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* CMB file field type
*
* @since 2.2.2
*
* @category WordPress_Plugin
* @package CMB2
* @author CMB2 team
* @license GPL-2.0+
* @link https://cmb2.io
*/
class CMB2_Type_File extends CMB2_Type_File_Base {
/**
* Handles outputting an 'file' field
*
* @param array $args Override arguments
* @return string Form input element
*/
public function render( $args = array() ) {
$args = empty( $args ) ? $this->args : $args;
$field = $this->field;
$options = (array) $field->options();
$a = $this->args = $this->parse_args( 'file', array(
'class' => 'cmb2-upload-file regular-text',
'id' => $this->_id(),
'name' => $this->_name(),
'value' => $field->escaped_value(),
'id_value' => null,
'desc' => $this->_desc( true ),
'size' => 45,
'js_dependencies' => 'media-editor',
'preview_size' => $field->args( 'preview_size' ),
'query_args' => $field->args( 'query_args' ),
// if options array and 'url' => false, then hide the url field
'type' => array_key_exists( 'url', $options ) && false === $options['url']
? 'hidden'
: 'text',
), $args );
// get an array of image size meta data, fallback to 'large'
$this->args['img_size_data'] = $img_size_data = parent::get_image_size_data(
$a['preview_size'],
'large'
);
$output = '';
$output .= parent::render( array(
'type' => $a['type'],
'class' => $a['class'],
'value' => $a['value'],
'id' => $a['id'],
'name' => $a['name'],
'size' => $a['size'],
'desc' => '',
'data-previewsize' => sprintf( '[%d,%d]', $img_size_data['width'], $img_size_data['height'] ),
'data-sizename' => $img_size_data['name'],
'data-queryargs' => ! empty( $a['query_args'] ) ? json_encode( $a['query_args'] ) : '',
'js_dependencies' => $a['js_dependencies'],
) );
// Now remove the data-iterator attribute if it exists.
// (Possible if being used within a custom field)
// This is not elegant, but compensates for CMB2_Types::_id
// automagically & inelegantly adding the data-iterator attribute.
// Single responsibility principle? pffft
$parts = explode( '"', $this->args['id'] );
$this->args['id'] = $parts[0];
$output .= sprintf(
'<input class="cmb2-upload-button button-secondary" type="button" value="%1$s" />',
esc_attr( $this->_text( 'add_upload_file_text', esc_html__( 'Add or Upload File', 'cmb2' ) ) )
);
$output .= $a['desc'];
$output .= $this->get_id_field_output();
$output .= '<div id="' . $field->id() . '-status" class="cmb2-media-status">';
if ( ! empty( $a['value'] ) ) {
$output .= $this->get_file_preview_output();
}
$output .= '</div>';
return $this->rendered( $output );
}
public function get_file_preview_output() {
if ( ! $this->is_valid_img_ext( $this->args['value'] ) ) {
return $this->file_status_output( array(
'value' => $this->args['value'],
'tag' => 'div',
'cached_id' => $this->args['id'],
) );
}
if ( $this->args['id_value'] ) {
$image = wp_get_attachment_image( $this->args['id_value'], $this->args['preview_size'], null, array(
'class' => 'cmb-file-field-image',
) );
} else {
$image = '<img style="max-width: ' . absint( $this->args['img_size_data']['width'] ) . 'px; width: 100%;" src="' . $this->args['value'] . '" class="cmb-file-field-image" alt="" />';
}
return $this->img_status_output( array(
'image' => $image,
'tag' => 'div',
'cached_id' => $this->args['id'],
) );
}
public function get_id_field_output() {
$field = $this->field;
/*
* A little bit of magic (tsk tsk) replacing the $this->types->field object,
* So that the render function is using the proper field object.
*/
$this->types->field = $this->get_id_field();
$output = parent::render( array(
'type' => 'hidden',
'class' => 'cmb2-upload-file-id',
'value' => $this->types->field->value,
'desc' => '',
) );
// We need to put the original field object back
// or other fields in a custom field will be broken.
$this->types->field = $field;
return $output;
}
public function get_id_field() {
// reset field args for attachment id
$args = array(
// if we're looking at a file in a group, we need to get the non-prefixed id
'id' => ( $this->field->group ? $this->field->args( '_id' ) : $this->args['id'] ) . '_id',
'disable_hash_data_attribute' => true,
);
// and get new field object
// (need to set it to the types field property)
$id_field = $this->field->get_field_clone( $args );
$id_value = absint( null !== $this->args['id_value'] ? $this->args['id_value'] : $id_field->escaped_value() );
// we don't want to output "0" as a value.
if ( ! $id_value ) {
$id_value = '';
}
// if there is no id saved yet, try to get it from the url
if ( $this->args['value'] && ! $id_value ) {
$id_value = CMB2_Utils::image_id_from_url( esc_url_raw( $this->args['value'] ) );
}
$id_field->value = $id_value;
return $id_field;
}
}