Skip to content

Commit

Permalink
Refactor code to show uploaded images
Browse files Browse the repository at this point in the history
Also removed title "uploaded images", it's redundant
  • Loading branch information
rilwis committed Oct 6, 2012
1 parent 1a14701 commit 01de3f3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 132 deletions.
5 changes: 1 addition & 4 deletions inc/fields/file.php
Expand Up @@ -75,8 +75,6 @@ static function wp_ajax_delete_file()
*/
static function html( $html, $meta, $field )
{
$i18n_msg = _x( 'Uploaded files', 'file upload', 'rwmb' );
$i18n_del_file = _x( 'Delete this file', 'file upload', 'rwmb' );
$i18n_delete = _x( 'Delete', 'file upload', 'rwmb' );
$i18n_title = _x( 'Upload files', 'file upload', 'rwmb' );
$i18n_more = _x( '+ Add new file', 'file upload', 'rwmb' );
Expand All @@ -87,7 +85,6 @@ static function html( $html, $meta, $field )
// Uploaded files
if ( ! empty( $meta ) )
{
$html .= "<h4>{$i18n_msg}</h4>";
$html .= '<ol class="rwmb-uploaded">';
$li = '<li>%s (<a title="%s" class="rwmb-delete-file" href="#" rel="%s">%s</a>)</li>';

Expand All @@ -97,7 +94,7 @@ static function html( $html, $meta, $field )
$html .= sprintf(
$li,
$attachment,
$i18n_del_file,
$i18n_delete,
$attachment_id,
$i18n_delete
);
Expand Down
84 changes: 46 additions & 38 deletions inc/fields/image.php
Expand Up @@ -75,51 +75,16 @@ static function wp_ajax_reorder_images()
*/
static function html( $html, $meta, $field )
{
global $wpdb;

$i18n_msg = _x( 'Uploaded files', 'image upload', 'rwmb' );
$i18n_del_file = _x( 'Delete this file', 'image upload', 'rwmb' );
$i18n_delete = _x( 'Delete', 'image upload', 'rwmb' );
$i18n_edit = _x( 'Edit', 'image upload', 'rwmb' );
$i18n_title = _x( 'Upload files', 'image upload', 'rwmb' );
$i18n_more = _x( '+ Add new image', 'image upload', 'rwmb' );
$i18n_title = _x( 'Upload images', 'image upload', 'rwmb' );
$i18n_more = _x( '+ Add new image', 'image upload', 'rwmb' );

$html = wp_nonce_field( "rwmb-delete-file_{$field['id']}", "nonce-delete-file_{$field['id']}", false, false );
$html .= wp_nonce_field( "rwmb-reorder-images_{$field['id']}", "nonce-reorder-images_{$field['id']}", false, false );
$html .= "<input type='hidden' class='field-id' value='{$field['id']}' />";

// Uploaded images
if ( ! empty( $meta ) )
{
$html .= "<h4>{$i18n_msg}</h4>";
$html .= "<ul class='rwmb-images rwmb-uploaded'>";
$li = '
<li id="item_%s">
<img src="%s" />
<div class="rwmb-image-bar">
<a title="%s" class="rwmb-edit-file" href="%s" target="_blank">%s</a> |
<a title="%s" class="rwmb-delete-file" href="#" rel="%s">%s</a>
</div>
</li>
';

foreach ( $meta as $image )
{
$src = wp_get_attachment_image_src( $image, 'thumbnail' );
$src = $src[0];
$link = get_edit_post_link( $image );

$html .= sprintf(
$li,
$image,
$src,
$i18n_edit, $link, $i18n_edit,
$i18n_del_file, $image, $i18n_delete
);
}

$html .= '</ul>';
}
$html .= self::get_uploaded_images( $meta );

// Show form upload
$html .= sprintf(
Expand All @@ -136,6 +101,49 @@ static function html( $html, $meta, $field )
return $html;
}

/**
* Get HTML markup for uploaded images
*
* @param array $images
*
* @return string
*/
static function get_uploaded_images( $images )
{
$i18n_delete = _x( 'Delete', 'image upload', 'rwmb' );
$i18n_edit = _x( 'Edit', 'image upload', 'rwmb' );

$html = '<ul class="rwmb-images rwmb-uploaded">';

$li = '
<li id="item_%s">
<img src="%s" />
<div class="rwmb-image-bar">
<a title="%s" class="rwmb-edit-file" href="%s" target="_blank">%s</a> |
<a title="%s" class="rwmb-delete-file" href="#" rel="%s">%s</a>
</div>
</li>
';
foreach ( $images as $image )
{
$src = wp_get_attachment_image_src( $image, 'thumbnail' );
$src = $src[0];
$link = get_edit_post_link( $image );

$html .= sprintf(
$li,
$image,
$src,
$i18n_edit, $link, $i18n_edit,
$i18n_delete, $image, $i18n_delete
);
}

$html .= '</ul>';

return $html;
}

/**
* Standard meta retrieval
*
Expand Down
48 changes: 2 additions & 46 deletions inc/fields/plupload-image.php
Expand Up @@ -103,39 +103,6 @@ static function admin_enqueue_scripts()
);
}

/**
* 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 );

return sprintf(
'<li id="item_%s">
<img src="%s" />
<div class="rwmb-image-bar">
<a title="%s" class="rwmb-edit-file" href="%s" target="_blank">%s</a> |
<a title="%s" class="rwmb-delete-file" href="#" rel="%s">%s</a>
</div>
</li>',
$img_id,
$src,
$i18n_edit, $link, $i18n_edit,
$i18n_del_file, $img_id, $i18n_delete
);

}

/**
* Get field HTML
*
Expand All @@ -150,9 +117,6 @@ static function html( $html, $meta, $field )
if ( ! is_array( $meta ) )
$meta = ( array ) $meta;

$i18n_msg = _x( 'Uploaded files', 'image upload', 'rwmb' );
$i18n_title = _x( 'Upload files', 'image upload', 'rwmb' );

// 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' );
Expand All @@ -177,25 +141,17 @@ static function html( $html, $meta, $field )
$classes[] = 'hidden';
}

$html .= "<h4 class='rwmb-uploaded-title'>{$i18n_msg}</h4>";
$html .= "<ul class='rwmb-images rwmb-uploaded'>";
foreach ( $meta as $image )
{
$html .= self::img_html( $image );
}
$html .= '</ul>';
$html .= self::get_uploaded_images( $meta );

// Show form upload
$html .= sprintf(
'<h4>%s</h4>
<div id="%s-dragdrop" class="%s">
'<div id="%s-dragdrop" class="%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>',
$i18n_title,
$img_prefix,
implode( ' ', $classes ),
$i18n_drop,
Expand Down
47 changes: 3 additions & 44 deletions inc/fields/thickbox-image.php
Expand Up @@ -32,58 +32,17 @@ static function admin_enqueue_scripts()
*/
static function html( $html, $meta, $field )
{
global $wpdb;

$i18n_msg = _x( 'Uploaded files', 'image upload', 'rwmb' );
$i18n_delete = _x( 'Delete', 'image upload', 'rwmb' );
$i18n_edit = _x( 'Edit', 'image upload', 'rwmb' );
$i18n_upload = _x( 'Upload image', 'image upload', 'rwmb' );
$i18n_title = _x( 'Upload images', 'image upload', 'rwmb' );

$html = wp_nonce_field( "rwmb-delete-file_{$field['id']}", "nonce-delete-file_{$field['id']}", false, false );
$html .= wp_nonce_field( "rwmb-reorder-images_{$field['id']}", "nonce-reorder-images_{$field['id']}", false, false );
$html .= "<input type='hidden' class='field-id' value='{$field['id']}' />";

// Uploaded images
if ( ! empty( $meta ) )
{
$html .= "<h4>{$i18n_msg}</h4>";
$html .= "<ul class='rwmb-images rwmb-uploaded'>";

$li = '
<li id="item_%s">
<img src="%s" />
<div class="rwmb-image-bar">
<a title="%s" class="rwmb-edit-file" href="%s" target="_blank">%s</a> |
<a title="%s" class="rwmb-delete-file" href="#" rel="%s">%s</a>
</div>
</li>
';

foreach ( $meta as $image )
{
$src = wp_get_attachment_image_src( $image, 'thumbnail' );
$src = $src[0];
$link = get_edit_post_link( $image );

$html .= sprintf(
$li,
$image,
$src,
$i18n_edit, $link, $i18n_edit,
$i18n_delete, $image, $i18n_delete
);
}

$html .= '</ul>';
}
else
{
// Place holder for images
$html .= "<ul class='rwmb-images rwmb-uploaded'></ul>";
}
$html .= self::get_uploaded_images( $meta );

// Show form upload
$html .= "<a href='#' class='button-secondary rwmb-thickbox-upload' rel='{$field['id']}'>{$i18n_upload}</a>";
$html .= "<a href='#' class='button rwmb-thickbox-upload' rel='{$field['id']}'>{$i18n_title}</a>";

return $html;
}
Expand Down

0 comments on commit 01de3f3

Please sign in to comment.