示例#1
0
文件: form.php 项目: Omuze/barakat
/**
 * Field: media
 */
function _kc_field_media($args)
{
    $args['field']['multiple'] = (bool) $args['field']['multiple'];
    kcSettings::add_media_field($args['id'], $args['field']);
    if (!is_array($args['db_value'])) {
        $args['db_value'] = array($args['db_value']);
    }
    foreach ($args['db_value'] as $idx => $attachment_id) {
        if (empty($attachment_id)) {
            unset($args['db_value'][$idx]);
            continue;
        }
        $attachment = get_post($attachment_id);
        if (empty($attachment) || !is_object($attachment)) {
            unset($args['db_value'][$idx]);
        }
    }
    $wrap_class = 'kc-media-selector';
    $list_class = 'kc-media-list attachments';
    if (empty($args['db_value'])) {
        $list_class .= ' hidden';
        $args['db_value'][] = '';
        // Needed to print out the item template.
    }
    if ($args['field']['multiple']) {
        $args['name'] .= '[]';
        $list_class .= ' multiple';
    } else {
        $wrap_class .= ' single-file';
    }
    $list_attr = ' id="' . esc_attr($args['id']) . '"';
    $list_attr .= ' class="' . esc_attr($list_class) . '"';
    $list_attr .= ' data-size="' . esc_attr($args['field']['preview_size']) . '"';
    $list_attr .= ' data-animate="' . esc_attr($args['field']['animate']) . '"';
    $did_once = false;
    ob_start();
    ?>
<div class="<?php 
    echo esc_attr($wrap_class);
    ?>
">
	<ul<?php 
    echo $list_attr;
    ?>
>
		<?php 
    foreach ($args['db_value'] as $attachment_id) {
        $item_class = 'attachment';
        $thumb_style = '';
        if (!empty($attachment_id)) {
            $image = wp_get_attachment_image($attachment_id, $args['field']['preview_size'], true);
            $title = get_the_title($attachment_id);
            $mime_type = substr(get_post_mime_type($attachment_id), 0, strpos($attachment->post_mime_type, '/'));
            $item_class .= sprintf(' type-%s', $mime_type);
            if ('image' === $mime_type && $args['field']['preview_size'] !== 'thumbnail' && ($image_src = wp_get_attachment_image_src($attachment_id, $args['field']['preview_size'], false))) {
                $thumb_style = ' style="width:' . $image_src[1] . 'px;height:' . $image_src[2] . 'px"';
            }
        } else {
            if ($did_once) {
                // Skip, we already printed the template.
                continue;
            }
            $image = '<img />';
            $title = '';
        }
        $did_once = true;
        ?>
			<li class="<?php 
        echo esc_attr($item_class);
        ?>
">
				<div class="attachment-preview"<?php 
        echo $thumb_style;
        ?>
>
					<div class="thumbnail"<?php 
        echo $thumb_style;
        ?>
>
						<div class="centered">
							<?php 
        echo $image;
        ?>
						</div>
						<div class="filename">
							<div><?php 
        echo esc_html($title);
        ?>
</div>
						</div>
					</div>
					<a title="<?php 
        esc_attr_e('Deselect');
        ?>
" href="#" class="check"><div class="media-modal-icon"></div></a>
				</div>
				<input type="hidden" name="<?php 
        echo esc_attr($args['name']);
        ?>
" value="<?php 
        echo esc_attr($attachment_id);
        ?>
" />
			</li>
		<?php 
    }
    ?>
	</ul>
	<p><a href="#" class="button-primary kc-media-select" data-fieldid="<?php 
    echo esc_attr($args['id']);
    ?>
"><?php 
    echo esc_html($args['field']['select_button']);
    ?>
</a></p>
</div>
<?php 
    $output = ob_get_contents();
    ob_end_clean();
    return $output;
}