= (int) $field['max_file_uploads'] )
$new_file_classes[] = 'hidden';
// Show form upload
$html .= sprintf(
'
',
implode( ' ', $new_file_classes ),
$i18n_title,
$field['id'],
$i18n_more
);
return $html;
}
static function get_uploaded_files( $files, $field )
{
$delete_nonce = wp_create_nonce( "rwmb-delete-file_{$field['id']}" );
$classes = array('rwmb-file', 'rwmb-uploaded');
if ( count( $files ) <= 0 )
$classes[] = 'hidden';
$ol = '';
$html = sprintf(
$ol,
implode( ' ', $classes ),
$field['id'],
$delete_nonce,
$field['force_delete'] ? 1 : 0,
$field['max_file_uploads'],
$field['mime_type']
);
foreach ( $files as $attachment_id )
{
$html .= self::file_html( $attachment_id );
}
$html .= '
';
return $html;
}
static function file_html( $attachment_id )
{
$i18n_delete = apply_filters( 'rwmb_file_delete_string', _x( 'Delete', 'file upload', 'rwmb' ) );
$i18n_edit = apply_filters( 'rwmb_file_edit_string', _x( 'Edit', 'file upload', 'rwmb' ) );
$li = '
%s
';
$mime_type = get_post_mime_type( $attachment_id );
return sprintf(
$li,
wp_get_attachment_image( $attachment_id, array(60,60), true ),
wp_get_attachment_url($attachment_id),
get_the_title( $attachment_id ),
$mime_type,
$i18n_edit,
get_edit_post_link( $attachment_id ),
$i18n_edit,
$i18n_delete,
$attachment_id,
$i18n_delete
);
}
/**
* Get meta values to save
*
* @param mixed $new
* @param mixed $old
* @param int $post_id
* @param array $field
*
* @return array|mixed
*/
static function value( $new, $old, $post_id, $field )
{
$name = $field['id'];
if ( empty( $_FILES[ $name ] ) )
return $new;
$new = array();
$files = self::fix_file_array( $_FILES[ $name ] );
foreach ( $files as $file_item )
{
$file = wp_handle_upload( $file_item, array( 'test_form' => false ) );
if ( ! isset( $file['file'] ) )
continue;
$file_name = $file['file'];
$attachment = array(
'post_mime_type' => $file['type'],
'guid' => $file['url'],
'post_parent' => $post_id,
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ),
'post_content' => '',
);
$id = wp_insert_attachment( $attachment, $file_name, $post_id );
if ( ! is_wp_error( $id ) )
{
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file_name ) );
// Save file ID in meta field
$new[] = $id;
}
}
return array_unique( array_merge( $old, $new ) );
}
/**
* Fixes the odd indexing of multiple file uploads from the format:
* $_FILES['field']['key']['index']
* To the more standard and appropriate:
* $_FILES['field']['index']['key']
*
* @param array $files
*
* @return array
*/
static function fix_file_array( $files )
{
$output = array();
foreach ( $files as $key => $list )
{
foreach ( $list as $index => $value )
{
$output[$index][$key] = $value;
}
}
return $output;
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize_field( $field )
{
$field = wp_parse_args( $field, array(
'std' => array(),
'force_delete' => false,
'max_file_uploads' => 0,
'mime_type' => '',
) );
$field['multiple'] = true;
return $field;
}
/**
* Standard meta retrieval
*
* @param mixed $meta
* @param int $post_id
* @param array $field
* @param bool $saved
*
* @return mixed
*/
static function meta( $meta, $post_id, $saved, $field )
{
global $wpdb;
$meta = RW_Meta_Box::meta( $meta, $post_id, $saved, $field );
if ( empty( $meta ) )
return array();
return (array) $meta;
}
}
}