-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathclass-fieldmanager-media.php
More file actions
188 lines (170 loc) · 5.03 KB
/
class-fieldmanager-media.php
File metadata and controls
188 lines (170 loc) · 5.03 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* Class file for Fieldmanager_Media
*
* @package Fieldmanager
*/
/**
* A field to select an attachment via the WordPress Media Manager.
*
* This field submits the selected attachment as an attachment ID (post ID).
*/
class Fieldmanager_Media extends Fieldmanager_Field {
/**
* Override field_class.
*
* @var string
*/
public $field_class = 'media';
/**
* Button Label.
*
* @var string
*/
public $button_label;
/**
* Button label in the media modal popup.
*
* @var string
*/
public $modal_button_label;
/**
* Title of the media modal popup.
*
* @var string
*/
public $modal_title;
/**
* Label for the preview of the selected image attachment.
*
* @var string
*/
public $selected_image_label;
/**
* Label for the preview of the selected non-image attachment.
*
* @var string
*/
public $selected_file_label;
/**
* Text of the link that deselects the currently selected attachment.
*
* @var string
*/
public $remove_media_label;
/**
* Class to attach to thumbnail media display.
*
* @var string
*/
public $thumbnail_class = 'thumbnail';
/**
* Which size a preview image should be. e.g. "thumbnail", "large", or some
* size created with add_image_size.
*
* @var string
*/
public $preview_size = 'thumbnail';
/**
* What mime types are available to choose from.
* Valid options are "all" or a partial or full mimetype (e.g. "image" or
* "application/pdf").
*
* @var string
*/
public $mime_type = 'all';
/**
* Static variable so we only load media JS once.
*
* @var bool
*/
public static $has_registered_media = false;
/**
* Construct default attributes.
*
* @param string $label The form label.
* @param array $options The form options.
*/
public function __construct( $label = '', $options = array() ) {
$this->button_label = __( 'Attach a File', 'fieldmanager' );
$this->modal_button_label = __( 'Select Attachment', 'fieldmanager' );
$this->modal_title = __( 'Choose an Attachment', 'fieldmanager' );
$this->selected_image_label = __( 'Uploaded image:', 'fieldmanager' );
$this->selected_file_label = __( 'Uploaded file:', 'fieldmanager' );
$this->remove_media_label = __( 'remove', 'fieldmanager' );
if ( ! self::$has_registered_media ) {
fm_add_script( 'fm_media', 'js/media/fieldmanager-media.js', array( 'jquery' ), '1.0.4' );
if ( did_action( 'admin_print_scripts' ) ) {
$this->admin_print_scripts();
} else {
add_action( 'admin_print_scripts', array( $this, 'admin_print_scripts' ) );
}
self::$has_registered_media = true;
}
parent::__construct( $label, $options );
}
/**
* Hook into admin_print_scripts action to enqueue the media for the current
* post
*/
public function admin_print_scripts() {
$post = get_post();
$args = array();
if ( ! empty( $post->ID ) ) {
$args['post'] = $post->ID;
}
wp_enqueue_media( $args ); // generally on post pages this will not have an impact.
}
/**
* Presave; ensure that the value is an absolute integer.
*
* @param int $value The new value.
* @param array $current_value The current value.
* @return int The sanitized value.
*/
public function presave( $value, $current_value = array() ) {
if ( 0 == $value || ! is_numeric( $value ) ) {
return null;
}
return absint( $value );
}
/**
* Form element.
*
* @param mixed $value The current value.
* @return string HTML string.
*/
public function form_element( $value = array() ) {
if ( is_numeric( $value ) && $value > 0 ) {
$attachment = get_post( $value );
if ( strpos( $attachment->post_mime_type, 'image/' ) === 0 ) {
$preview = esc_html( $this->selected_image_label ) . '<br />';
$preview .= '<a href="#">' . wp_get_attachment_image( $value, $this->preview_size, false, array(
'class' => $this->thumbnail_class,
) ) . '</a>';
} else {
$preview = esc_html( $this->selected_file_label ) . ' ';
$preview .= wp_get_attachment_link( $value, $this->preview_size, true, true, $attachment->post_title );
}
$preview .= sprintf( '<br /><a href="#" class="fm-media-remove fm-delete">%s</a>', esc_html( $this->remove_media_label ) );
$preview = apply_filters( 'fieldmanager_media_preview', $preview, $value, $attachment );
} else {
$preview = '';
}
return sprintf(
'<input type="button" class="fm-media-button button-secondary fm-incrementable" id="%1$s" value="%3$s" data-choose="%7$s" data-update="%8$s" data-preview-size="%6$s" data-mime-type="%9$s" %10$s />
<input type="hidden" name="%2$s" value="%4$s" class="fm-element fm-media-id" />
<div class="media-wrapper">%5$s</div>',
esc_attr( $this->get_element_id() ),
esc_attr( $this->get_form_name() ),
esc_attr( $this->button_label ),
esc_attr( $value ),
$preview,
esc_attr( $this->preview_size ),
esc_attr( $this->modal_title ),
esc_attr( $this->modal_button_label ),
esc_attr( $this->mime_type ),
$this->get_element_attributes( '', array( 'data-choose', 'data-update', 'data-preview-size', 'data-mime-type' ) )
);
}
}