/**
 * add_extra_media_buttons function.
 *
 * Adds the "Use as Post Thumbnail" button to the add media thickbox.
 *
 * @param array $form_fields
 * @param object $media
 * @return array $form_fields
 */
function tj_add_extra_media_buttons($form_fields, $media)
{
    $buttons = get_option('custom_media_buttons');
    if ($_GET['button']) {
        $button_id = $_GET['button'];
    } else {
        preg_match('/button=([A-z0-9_][^&]*)/', $_SERVER['HTTP_REFERER'], $matches);
        $button_id = $matches[1];
    }
    if (!isset($button_id) || !$button_id) {
        return $form_fields;
    }
    if (isset($button_id) && ($button = $buttons[$button_id])) {
        $buttons_html = '<input type="submit" class="button" name="' . $button['id'] . '[' . $media->ID . ']" value="' . esc_attr($button['button_text']) . '" />';
    }
    if (!$button['hide_other_options']) {
        $send = '<input type="submit" class="button" name="send[' . $media->ID . ']" value="' . esc_attr(__('Insert into Post')) . '" />';
    } else {
        ?>
	
		<style type="text/css">
			.slidetoggle tr.post_title, .slidetoggle tr.image_alt, .slidetoggle tr.post_excerpt, .slidetoggle tr.post_content, .slidetoggle tr.url, .slidetoggle tr.align, .slidetoggle tr.image-size { display: none; }
		</style>
		
<?php 
    }
    if ($send) {
        $send = "<input type='submit' class='button' name='send[{$media->ID}]' value='" . esc_attr__('Insert into Post') . "' />";
    }
    if (current_user_can('delete_post', $media->ID)) {
        if (!EMPTY_TRASH_DAYS) {
            $delete = "<a href=\"" . wp_nonce_url("post.php?action=delete&amp;post={$media->ID}", 'delete-post_' . $media->ID) . "\" id=\"del[{$media->ID}]\" class=\"delete\">" . __('Delete Permanently') . "</a>";
        } else {
            $delete = "<a href=\"" . wp_nonce_url("post.php?action=trash&amp;post={$media->ID}", 'trash-post_' . $media->ID) . "\" id=\"del[{$media->ID}]\" class=\"delete\">" . __('Move to Trash') . "</a> <a href=\"" . wp_nonce_url("post.php?action=untrash&amp;post={$media->ID}", 'untrash-post_' . $media->ID) . "\" id=\"undo[{$media->ID}]\" class=\"undo hidden\">" . __('Undo?') . "</a>";
        }
    } else {
        $delete = '';
    }
    $thumbnail = '';
    if ('image' == $type && current_theme_supports('post-thumbnails') && get_post_image_id($_GET['post_id']) != $media->ID) {
        $thumbnail = "<a class='wp-post-thumbnail' href='#' onclick='WPSetAsThumbnail(\"{$media->ID}\");return false;'>" . esc_html__("Use as thumbnail") . "</a>";
    }
    // Create the buttons array
    $form_fields['buttons'] = array('tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>{$send} {$thumbnail} {$buttons_html} {$delete}</td></tr>\n");
    return $form_fields;
}
예제 #2
0
파일: media.php 프로젝트: jao/jpcamargo
/**
 * Retrieve HTML form for modifying the image attachment.
 *
 * @since unknown
 *
 * @param int $attachment_id Attachment ID for modification.
 * @param string|array $args Optional. Override defaults.
 * @return string HTML form for attachment.
 */
function get_media_item($attachment_id, $args = null)
{
    global $redir_tab;
    if (($attachment_id = intval($attachment_id)) && ($thumb_url = get_attachment_icon_src($attachment_id))) {
        $thumb_url = $thumb_url[0];
    } else {
        return false;
    }
    $default_args = array('errors' => null, 'send' => true, 'delete' => true, 'toggle' => true, 'show_title' => true);
    $args = wp_parse_args($args, $default_args);
    extract($args, EXTR_SKIP);
    $toggle_on = __('Show');
    $toggle_off = __('Hide');
    $post = get_post($attachment_id);
    $filename = basename($post->guid);
    $title = esc_attr($post->post_title);
    if ($_tags = get_the_tags($attachment_id)) {
        foreach ($_tags as $tag) {
            $tags[] = $tag->name;
        }
        $tags = esc_attr(join(', ', $tags));
    }
    $post_mime_types = get_post_mime_types();
    $keys = array_keys(wp_match_mime_types(array_keys($post_mime_types), $post->post_mime_type));
    $type = array_shift($keys);
    $type_html = "<input type='hidden' id='type-of-{$attachment_id}' value='" . esc_attr($type) . "' />";
    $form_fields = get_attachment_fields_to_edit($post, $errors);
    if ($toggle) {
        $class = empty($errors) ? 'startclosed' : 'startopen';
        $toggle_links = "\n\t<a class='toggle describe-toggle-on' href='#'>{$toggle_on}</a>\n\t<a class='toggle describe-toggle-off' href='#'>{$toggle_off}</a>";
    } else {
        $class = 'form-table';
        $toggle_links = '';
    }
    $display_title = !empty($title) ? $title : $filename;
    // $title shouldn't ever be empty, but just in case
    $display_title = $show_title ? "<div class='filename new'>" . wp_html_excerpt($display_title, 60) . "</div>" : '';
    $gallery = isset($_REQUEST['tab']) && 'gallery' == $_REQUEST['tab'] || isset($redir_tab) && 'gallery' == $redir_tab ? true : false;
    $order = '';
    foreach ($form_fields as $key => $val) {
        if ('menu_order' == $key) {
            if ($gallery) {
                $order = '<div class="menu_order"> <input class="menu_order_input" type="text" id="attachments[' . $attachment_id . '][menu_order]" name="attachments[' . $attachment_id . '][menu_order]" value="' . $val['value'] . '" /></div>';
            } else {
                $order = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . $val['value'] . '" />';
            }
            unset($form_fields['menu_order']);
            break;
        }
    }
    $media_dims = '';
    $meta = wp_get_attachment_metadata($post->ID);
    if (is_array($meta) && array_key_exists('width', $meta) && array_key_exists('height', $meta)) {
        $media_dims .= "<span id='media-dims-{$post->ID}'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
    }
    $image_edit_button = '';
    if (gd_edit_image_support($post->post_mime_type)) {
        $nonce = wp_create_nonce("image_editor-{$post->ID}");
        $image_edit_button = "<input type='button' id='imgedit-open-btn-{$post->ID}' onclick='imageEdit.open({$post->ID}, \"{$nonce}\")' class='button' value='" . esc_attr__('Edit image') . "' /> <img src='images/wpspin_light.gif' class='imgedit-wait-spin' alt='' />";
    }
    $item = "\n\t{$type_html}\n\t{$toggle_links}\n\t{$order}\n\t{$display_title}\n\t<table class='slidetoggle describe {$class}'>\n\t\t<thead class='media-item-info' id='media-head-{$post->ID}'>\n\t\t<tr>\n\t\t\t<td class='A1B1' id='thumbnail-head-{$post->ID}' rowspan='5'><img class='thumbnail' src='{$thumb_url}' alt='' /></td>\n\t\t\t<td>{$filename}</td>\n\t\t</tr>\n\t\t<tr><td>{$post->post_mime_type}</td></tr>\n\t\t<tr><td>" . mysql2date(get_option('time_format'), $post->post_date) . "</td></tr>\n\t\t<tr><td>" . apply_filters('media_meta', $media_dims, $post) . "</td></tr>\n\t\t<tr><td class='A1B1'>{$image_edit_button}</td></tr>\n\t\t</thead>\n\t\t<tbody>\n\t\t<tr><td colspan='2' class='imgedit-response' id='imgedit-response-{$post->ID}'></td></tr>\n\t\t<tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-{$post->ID}'></td></tr>\n";
    $defaults = array('input' => 'text', 'required' => false, 'value' => '', 'extra_rows' => array());
    $delete_href = wp_nonce_url("post.php?action=trash&amp;post={$attachment_id}", 'trash-post_' . $attachment_id);
    if ($send) {
        $send = "<input type='submit' class='button' name='send[{$attachment_id}]' value='" . esc_attr__('Insert into Post') . "' />";
    }
    if ($delete) {
        $delete = current_user_can('delete_post', $attachment_id) ? "<a href=\"{$delete_href}\" id=\"del[{$attachment_id}]\" class=\"delete\">" . __('Move to Trash') . "</a>" : "";
    }
    if ('image' == $type && get_post_image_id($_GET['post_id']) != $attachment_id) {
        $thumbnail = "<a class='wp-post-thumbnail' href='#' onclick='WPSetAsThumbnail(\"{$attachment_id}\");return false;'>" . esc_html__("Use as thumbnail") . "</a>";
    }
    if (($send || $thumbnail || $delete) && !isset($form_fields['buttons'])) {
        $form_fields['buttons'] = array('tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>{$send} {$thumbnail} {$delete}</td></tr>\n");
    }
    $hidden_fields = array();
    foreach ($form_fields as $id => $field) {
        if ($id[0] == '_') {
            continue;
        }
        if (!empty($field['tr'])) {
            $item .= $field['tr'];
            continue;
        }
        $field = array_merge($defaults, $field);
        $name = "attachments[{$attachment_id}][{$id}]";
        if ($field['input'] == 'hidden') {
            $hidden_fields[$name] = $field['value'];
            continue;
        }
        $required = $field['required'] ? '<abbr title="required" class="required">*</abbr>' : '';
        $aria_required = $field['required'] ? " aria-required='true' " : '';
        $class = $id;
        $class .= $field['required'] ? ' form-required' : '';
        $item .= "\t\t<tr class='{$class}'>\n\t\t\t<th valign='top' scope='row' class='label'><label for='{$name}'><span class='alignleft'>{$field['label']}</span><span class='alignright'>{$required}</span><br class='clear' /></label></th>\n\t\t\t<td class='field'>";
        if (!empty($field[$field['input']])) {
            $item .= $field[$field['input']];
        } elseif ($field['input'] == 'textarea') {
            $item .= "<textarea type='text' id='{$name}' name='{$name}'" . $aria_required . ">" . esc_html($field['value']) . "</textarea>";
        } else {
            $item .= "<input type='text' id='{$name}' name='{$name}' value='" . esc_attr($field['value']) . "'" . $aria_required . "/>";
        }
        if (!empty($field['helps'])) {
            $item .= "<p class='help'>" . join("</p>\n<p class='help'>", array_unique((array) $field['helps'])) . '</p>';
        }
        $item .= "</td>\n\t\t</tr>\n";
        $extra_rows = array();
        if (!empty($field['errors'])) {
            foreach (array_unique((array) $field['errors']) as $error) {
                $extra_rows['error'][] = $error;
            }
        }
        if (!empty($field['extra_rows'])) {
            foreach ($field['extra_rows'] as $class => $rows) {
                foreach ((array) $rows as $html) {
                    $extra_rows[$class][] = $html;
                }
            }
        }
        foreach ($extra_rows as $class => $rows) {
            foreach ($rows as $html) {
                $item .= "\t\t<tr><td></td><td class='{$class}'>{$html}</td></tr>\n";
            }
        }
    }
    if (!empty($form_fields['_final'])) {
        $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
    }
    $item .= "\t</tbody>\n";
    $item .= "\t</table>\n";
    foreach ($hidden_fields as $name => $value) {
        $item .= "\t<input type='hidden' name='{$name}' id='{$name}' value='" . esc_attr($value) . "' />\n";
    }
    if ($post->post_parent < 1 && isset($_REQUEST['post_id'])) {
        $parent = (int) $_REQUEST['post_id'];
        $parent_name = "attachments[{$attachment_id}][post_parent]";
        $item .= "\t<input type='hidden' name='{$parent_name}' id='{$parent_name}' value='" . $parent . "' />\n";
    }
    return $item;
}
예제 #3
0
function get_the_post_image($post_id = NULL, $size = 'thumbnail', $attr = '')
{
    global $id;
    $post_id = NULL === $post_id ? $id : $post_id;
    $post_image_id = get_post_image_id($post_id);
    $size = apply_filters('post_image_size', $size);
    if ($post_image_id) {
        do_action('begin_fetch_post_image_html', $post_id, $post_image_id, $size);
        // for "Just In Time" filtering of all of wp_get_attachment_image()'s filters
        $html = wp_get_attachment_image($post_image_id, $size, false, $attr);
        do_action('end_fetch_post_image_html', $post_id, $post_image_id, $size);
    } else {
        $html = '';
    }
    return apply_filters('post_image_html', $html, $post_id, $post_image_id);
}