';
$li = '
%s (%s)';
foreach ( $meta as $attachment_id )
{
$attachment = wp_get_attachment_link( $attachment_id );
$html .= sprintf(
$li,
$attachment,
$i18n_delete,
$field['id'],
$attachment_id,
$i18n_delete
);
}
$html .= '';
}
// Show form upload
$html .= sprintf(
'%s
',
$i18n_title,
$field['id'],
$i18n_more
);
return $html;
}
/**
* 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['multiple'] = true;
$field['std'] = empty( $field['std'] ) ? array() : $field['std'];
return $field;
}
}
}