/
plupload-image.php
180 lines (159 loc) · 5.11 KB
/
plupload-image.php
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
<?php
// Prevent loading this file directly
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'RWMB_Plupload_Image_Field' ) )
{
class RWMB_Plupload_Image_Field extends RWMB_Image_Field
{
/**
* Add field actions
*
* @return void
*/
static function add_actions()
{
parent::add_actions();
add_action( 'wp_ajax_rwmb_plupload_image_upload', array( __CLASS__, 'handle_upload' ) );
}
/**
* Upload
* Ajax callback function
*
* @return string Error or (XML-)response
*/
static function handle_upload()
{
$post_id = is_numeric( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : 0;
$field_id = isset( $_REQUEST['field_id'] ) ? $_REQUEST['field_id'] : '';
check_ajax_referer( "rwmb-upload-images_{$field_id}" );
// You can use WP's wp_handle_upload() function:
$file = $_FILES['async-upload'];
$file_attr = wp_handle_upload( $file, array( 'test_form' => false ) );
$attachment = array(
'guid' => $file_attr['url'],
'post_mime_type' => $file_attr['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file['name'] ) ),
'post_content' => '',
'post_status' => 'inherit',
);
// Adds file as attachment to WordPress
$id = wp_insert_attachment( $attachment, $file_attr['file'], $post_id );
if ( ! is_wp_error( $id ) )
{
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file_attr['file'] ) );
// Save file ID in meta field
add_post_meta( $post_id, $field_id, $id, false );
RW_Meta_Box::ajax_response( self::img_html( $id ), 'success' );
}
exit;
}
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
// Enqueue same scripts and styles as for file field
parent::admin_enqueue_scripts();
wp_enqueue_style( 'rwmb-plupload-image', RWMB_CSS_URL . 'plupload-image.css', array( 'wp-admin' ), RWMB_VER );
wp_enqueue_script( 'rwmb-plupload-image', RWMB_JS_URL . 'plupload-image.js', array( 'jquery-ui-sortable', 'wp-ajax-response', 'plupload-all' ), RWMB_VER, true );
wp_localize_script( 'rwmb-plupload-image', 'RWMB', array( 'url' => RWMB_URL ) );
}
/**
* Get field HTML
*
* @param string $html
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $html, $meta, $field )
{
if ( ! is_array( $meta ) )
$meta = ( array ) $meta;
// Filter to change the drag & drop box background string
$i18n_drop = apply_filters( 'rwmb_upload_drop_string', _x( 'Drop images here', 'image upload', 'rwmb' ) );
$i18n_or = _x( 'or', 'image upload', 'rwmb' );
$i18n_select = _x( 'Select Files', 'image upload', 'rwmb' );
// Uploaded images
// Check for max_file_uploads
$classes = array( 'rwmb-drag-drop', 'drag-drop', 'hide-if-no-js', 'new-files');
if ( ! empty( $field['max_file_uploads'] ) && count( $meta ) >= (int) $field['max_file_uploads'] )
$classes[] = 'hidden';
$html .= self::get_uploaded_images( $meta, $field );
// Show form upload
$html .= sprintf(
'<div id="%s-dragdrop" class="%s" data-upload_nonce="%s" data-js_options="%s">
<div class = "drag-drop-inside">
<p class="drag-drop-info">%s</p>
<p>%s</p>
<p class="drag-drop-buttons"><input id="%s-browse-button" type="button" value="%s" class="button" /></p>
</div>
</div>',
$field['id'],
implode( ' ', $classes ),
wp_create_nonce( "rwmb-upload-images_{$field['id']}" ),
esc_attr( json_encode( $field['js_options'] ) ),
$i18n_drop,
$i18n_or,
$field['id'],
$i18n_select
);
return $html;
}
/**
* Get field value
* It's the combination of new (uploaded) images and saved images
*
* @param array $new
* @param array $old
* @param int $post_id
* @param array $field
*
* @return array|mixed
*/
static function value( $new, $old, $post_id, $field )
{
$new = (array) $new;
return array_unique( array_merge( $old, $new ) );
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize_field( $field )
{
$field['js_options'] = array(
'runtimes' => 'html5,silverlight,flash,html4',
'file_data_name' => 'async-upload',
//'container' => $field['id'] . '-container',
'browse_button' => $field['id'] . '-browse-button',
'drop_element' => $field['id'] . '-dragdrop',
'multiple_queues' => true,
'max_file_size' => wp_max_upload_size() . 'b',
'url' => admin_url( 'admin-ajax.php' ),
'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),
'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
'multipart' => true,
'urlstream_upload' => true,
'filters' => array(
array(
'title' => _x( 'Allowed Image Files', 'image upload', 'rwmb' ),
'extensions' => 'jpg,jpeg,gif,png',
),
),
'multipart_params' => array(
'field_id' => $field['id'],
'action' => 'rwmb_plupload_image_upload',
)
);
$field = parent::normalize_field( $field );
return $field;
}
}
}