true,
'action' => 'plupload_image_upload'
) );
$attachment = array(
'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 ) )
{
$response = new WP_Ajax_Response();
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file_attr['file'] ) );
if ( isset( $_REQUEST['field_id'] ) )
{
// Save file ID in meta field
add_post_meta( $post_id, $_REQUEST['field_id'], $id, false );
}
$response->add( array(
'what' => 'rwmb_image_response',
'data' => self::img_html( $id )
) );
$response->send();
}
// Faster than die();
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(), 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) );
wp_localize_script( 'rwmb-plupload-image', 'rwmb_plupload_defaults', array(
'runtimes' => 'html5,silverlight,flash,html4',
'file_data_name' => 'async-upload',
'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' ),
'filters' => array( array(
'title' => _x( 'Allowed Image Files', 'image upload', 'rwmb' ),
'extensions' => 'jpg,jpeg,gif,png'
) ),
'multipart' => true,
'urlstream_upload' => true,
) );
}
/**
* Get image html
*
* @param int $img_id
*
* @return string
*/
static function img_html( $img_id )
{
$i18n_del_file = _x( 'Delete this file', 'image upload', 'rwmb' );
$i18n_delete = _x( 'Delete', 'image upload', 'rwmb' );
$i18n_edit = _x( 'Edit', 'image upload', 'rwmb' );
$src = wp_get_attachment_image_src( $img_id, 'thumbnail' );
$src = $src[0];
$link = get_edit_post_link( $img_id );
$html = <<
{$i18n_drop}
{$i18n_or}