home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-admin / includes / media.php < prev    next >
Encoding:
PHP Script  |  2008-08-10  |  50.6 KB  |  1,480 lines

  1. <?php
  2.  
  3. function media_upload_tabs() {
  4.     $_default_tabs = array(
  5.         'type' => __('Choose File'), // handler action suffix => tab text
  6.         'gallery' => __('Gallery'),
  7.         'library' => __('Media Library'),
  8.     );
  9.  
  10.     return apply_filters('media_upload_tabs', $_default_tabs);
  11. }
  12.  
  13. function update_gallery_tab($tabs) {
  14.     global $wpdb;
  15.     if ( !isset($_REQUEST['post_id']) ) {
  16.         unset($tabs['gallery']);
  17.         return $tabs;
  18.     }
  19.     if ( intval($_REQUEST['post_id']) )
  20.         $attachments = intval($wpdb->get_var($wpdb->prepare("SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $_REQUEST['post_id'])));
  21.  
  22.     $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
  23.  
  24.     return $tabs;
  25. }
  26. add_filter('media_upload_tabs', 'update_gallery_tab');
  27.  
  28. function the_media_upload_tabs() {
  29.     global $redir_tab;
  30.     $tabs = media_upload_tabs();
  31.  
  32.     if ( !empty($tabs) ) {
  33.         echo "<ul id='sidemenu'>\n";
  34.         if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) )
  35.             $current = $redir_tab;
  36.         elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) )
  37.             $current = $_GET['tab'];
  38.         else {
  39.             $keys = array_keys($tabs);
  40.             $current = array_shift($keys);
  41.         }
  42.         foreach ( $tabs as $callback => $text ) {
  43.             $class = '';
  44.             if ( $current == $callback )
  45.                 $class = " class='current'";
  46.             $href = add_query_arg(array('tab'=>$callback, 's'=>false, 'paged'=>false, 'post_mime_type'=>false, 'm'=>false));
  47.             $link = "<a href='" . clean_url($href) . "'$class>$text</a>";
  48.             echo "\t<li id='" . attribute_escape("tab-$callback") . "'>$link</li>\n";
  49.         }
  50.         echo "</ul>\n";
  51.     }
  52. }
  53.  
  54. function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = false, $size='medium') {
  55.  
  56.     $html = get_image_tag($id, $alt, $title, $align, $size);
  57.  
  58.     $rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : '';
  59.  
  60.     if ( $url )
  61.         $html = '<a href="' . clean_url($url) . "\"$rel>$html</a>";
  62.  
  63.     $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url, $size );
  64.  
  65.     return $html;
  66. }
  67.  
  68. function image_add_caption( $html, $id, $alt, $title, $align, $url, $size ) {
  69.  
  70.     if ( empty($alt) || apply_filters( 'disable_captions', '' ) ) return $html;
  71.     $id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
  72.  
  73.     preg_match( '/width="([0-9]+)/', $html, $matches );
  74.     if ( ! isset($matches[1]) ) return $html;
  75.     $width = $matches[1];
  76.  
  77.     $html = preg_replace( '/align[^\s\'"]+\s?/', '', $html );
  78.     if ( empty($align) ) $align = 'none';
  79.  
  80.     $shcode = '[caption id="' . $id . '" align="align' . $align
  81.     . '" width="' . $width . '" caption="' . $alt . '"]' . $html . '[/caption]';
  82.  
  83.     return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
  84. }
  85. add_filter( 'image_send_to_editor', 'image_add_caption', 20, 7 );
  86.  
  87. function media_send_to_editor($html) {
  88.     ?>
  89. <script type="text/javascript">
  90. /* <![CDATA[ */
  91. var win = window.dialogArguments || opener || parent || top;
  92. win.send_to_editor('<?php echo addslashes($html); ?>');
  93. /* ]]> */
  94. </script>
  95.     <?php
  96.     exit;
  97. }
  98.  
  99. // this handles the file upload POST itself, creating the attachment post
  100. function media_handle_upload($file_id, $post_id, $post_data = array()) {
  101.     $overrides = array('test_form'=>false);
  102.     $file = wp_handle_upload($_FILES[$file_id], $overrides);
  103.  
  104.     if ( isset($file['error']) )
  105.         return new WP_Error( 'upload_error', $file['error'] );
  106.  
  107.     $url = $file['url'];
  108.     $type = $file['type'];
  109.     $file = $file['file'];
  110.     $title = preg_replace('/\.[^.]+$/', '', basename($file));
  111.     $content = '';
  112.  
  113.     // use image exif/iptc data for title and caption defaults if possible
  114.     if ( $image_meta = @wp_read_image_metadata($file) ) {
  115.         if ( trim($image_meta['title']) )
  116.             $title = $image_meta['title'];
  117.         if ( trim($image_meta['caption']) )
  118.             $content = $image_meta['caption'];
  119.     }
  120.  
  121.     // Construct the attachment array
  122.     $attachment = array_merge( array(
  123.         'post_mime_type' => $type,
  124.         'guid' => $url,
  125.         'post_parent' => $post_id,
  126.         'post_title' => $title,
  127.         'post_content' => $content,
  128.     ), $post_data );
  129.  
  130.     // Save the data
  131.     $id = wp_insert_attachment($attachment, $file, $post_parent);
  132.     if ( !is_wp_error($id) ) {
  133.         wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  134.     }
  135.  
  136.     return $id;
  137.  
  138. }
  139.  
  140. function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
  141.     $overrides = array('test_form'=>false);
  142.     $file = wp_handle_sideload($file_array, $overrides);
  143.  
  144.     if ( isset($file['error']) )
  145.         return new WP_Error( 'upload_error', $file['error'] );
  146.  
  147.     $url = $file['url'];
  148.     $type = $file['type'];
  149.     $file = $file['file'];
  150.     $title = preg_replace('/\.[^.]+$/', '', basename($file));
  151.     $content = '';
  152.  
  153.     // use image exif/iptc data for title and caption defaults if possible
  154.     if ( $image_meta = @wp_read_image_metadata($file) ) {
  155.         if ( trim($image_meta['title']) )
  156.             $title = $image_meta['title'];
  157.         if ( trim($image_meta['caption']) )
  158.             $content = $image_meta['caption'];
  159.     }
  160.  
  161.     $title = @$desc;
  162.  
  163.     // Construct the attachment array
  164.     $attachment = array_merge( array(
  165.         'post_mime_type' => $type,
  166.         'guid' => $url,
  167.         'post_parent' => $post_id,
  168.         'post_title' => $title,
  169.         'post_content' => $content,
  170.     ), $post_data );
  171.  
  172.     // Save the data
  173.     $id = wp_insert_attachment($attachment, $file, $post_parent);
  174.     if ( !is_wp_error($id) ) {
  175.         wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  176.         return $url;
  177.     }
  178.     return $id;
  179. }
  180.  
  181.  
  182. // wrap iframe content (produced by $content_func) in a doctype, html head/body etc
  183. // any additional function args will be passed to content_func
  184. function wp_iframe($content_func /* ... */) {
  185. ?>
  186. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  187. <html xmlns="http://www.w3.org/1999/xhtml" <?php do_action('admin_xml_ns'); ?> <?php language_attributes(); ?>>
  188. <head>
  189. <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
  190. <title><?php bloginfo('name') ?> › <?php _e('Uploads'); ?> — <?php _e('WordPress'); ?></title>
  191. <?php
  192. wp_enqueue_style( 'global' );
  193. wp_enqueue_style( 'wp-admin' );
  194. wp_enqueue_style( 'colors' );
  195. if ( 0 === strpos( $content_func, 'media' ) )
  196.     wp_enqueue_style( 'media' );
  197.  
  198. ?>
  199. <script type="text/javascript">
  200. //<![CDATA[
  201. function addLoadEvent(func) {if ( typeof wpOnload!='function'){wpOnload=func;}else{ var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}}
  202. //]]>
  203. </script>
  204. <?php
  205. do_action('admin_print_styles');
  206. do_action('admin_print_scripts');
  207. do_action('admin_head');
  208. if ( is_string($content_func) )
  209.     do_action( "admin_head_{$content_func}" );
  210. ?>
  211. </head>
  212. <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?>>
  213. <?php
  214.     $args = func_get_args();
  215.     $args = array_slice($args, 1);
  216.     call_user_func_array($content_func, $args);
  217. ?>
  218. </body>
  219. </html>
  220. <?php
  221. }
  222.  
  223. function media_buttons() {
  224.     global $post_ID, $temp_ID;
  225.     $uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
  226.     $context = apply_filters('media_buttons_context', __('Add media: %s'));
  227.     $media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID";
  228.     $media_title = __('Add Media');
  229.     $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&type=image");
  230.     $image_title = __('Add an Image');
  231.     $video_upload_iframe_src = apply_filters('video_upload_iframe_src', "$media_upload_iframe_src&type=video");
  232.     $video_title = __('Add Video');
  233.     $audio_upload_iframe_src = apply_filters('audio_upload_iframe_src', "$media_upload_iframe_src&type=audio");
  234.     $audio_title = __('Add Audio');
  235.     $out = <<<EOF
  236.  
  237.     <a href="{$image_upload_iframe_src}&TB_iframe=true" id="add_image" class="thickbox" title='$image_title'><img src='images/media-button-image.gif' alt='$image_title' /></a>
  238.     <a href="{$video_upload_iframe_src}&TB_iframe=true" id="add_video" class="thickbox" title='$video_title'><img src='images/media-button-video.gif' alt='$video_title' /></a>
  239.     <a href="{$audio_upload_iframe_src}&TB_iframe=true" id="add_audio" class="thickbox" title='$audio_title'><img src='images/media-button-music.gif' alt='$audio_title' /></a>
  240.     <a href="{$media_upload_iframe_src}&TB_iframe=true" id="add_media" class="thickbox" title='$media_title'><img src='images/media-button-other.gif' alt='$media_title' /></a>
  241.  
  242. EOF;
  243.     printf($context, $out);
  244. }
  245. add_action( 'media_buttons', 'media_buttons' );
  246. add_action('media_upload_media', 'media_upload_handler');
  247.  
  248. function media_upload_form_handler() {
  249.     check_admin_referer('media-form');
  250.  
  251.     if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
  252.         $post = $_post = get_post($attachment_id, ARRAY_A);
  253.         if ( isset($attachment['post_content']) )
  254.             $post['post_content'] = $attachment['post_content'];
  255.         if ( isset($attachment['post_title']) )
  256.             $post['post_title'] = $attachment['post_title'];
  257.         if ( isset($attachment['post_excerpt']) )
  258.             $post['post_excerpt'] = $attachment['post_excerpt'];
  259.         if ( isset($attachment['menu_order']) )
  260.             $post['menu_order'] = $attachment['menu_order'];
  261.  
  262.         $post = apply_filters('attachment_fields_to_save', $post, $attachment);
  263.  
  264.         if ( isset($post['errors']) ) {
  265.             $errors[$attachment_id] = $post['errors'];
  266.             unset($post['errors']);
  267.         }
  268.  
  269.         if ( $post != $_post )
  270.             wp_update_post($post);
  271.  
  272.         foreach ( get_attachment_taxonomies($post) as $t )
  273.             if ( isset($attachment[$t]) )
  274.                 wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
  275.     }
  276.  
  277.     if ( isset($_POST['insert-gallery']) )
  278.         return media_send_to_editor('[gallery]');
  279.  
  280.     if ( isset($_POST['send']) ) {
  281.         $keys = array_keys($_POST['send']);
  282.         $send_id = (int) array_shift($keys);
  283.         $attachment = stripslashes_deep( $_POST['attachments'][$send_id] );
  284.         $html = $attachment['post_title'];
  285.         if ( !empty($attachment['url']) ) {
  286.             if ( strpos($attachment['url'], 'attachment_id') || false !== strpos($attachment['url'], get_permalink($_POST['post_id'])) )
  287.                 $rel = " rel='attachment wp-att-".attribute_escape($send_id)."'";
  288.             $html = "<a href='{$attachment['url']}'$rel>$html</a>";
  289.         }
  290.         $html = apply_filters('media_send_to_editor', $html, $send_id, $attachment);
  291.         return media_send_to_editor($html);
  292.     }
  293.  
  294.     return $errors;
  295. }
  296.  
  297. function media_upload_image() {
  298.     if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
  299.         // Upload File button was clicked
  300.         $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
  301.         unset($_FILES);
  302.         if ( is_wp_error($id) ) {
  303.             $errors['upload_error'] = $id;
  304.             $id = false;
  305.         }
  306.     }
  307.  
  308.     if ( !empty($_POST['insertonlybutton']) ) {
  309.         $src = $_POST['insertonly']['src'];
  310.         if ( !empty($src) && !strpos($src, '://') )
  311.             $src = "http://$src";
  312.         $alt = attribute_escape($_POST['insertonly']['alt']);
  313.         if ( isset($_POST['insertonly']['align']) ) {
  314.             $align = attribute_escape($_POST['insertonly']['align']);
  315.             $class = " class='align$align'";
  316.         }
  317.         if ( !empty($src) )
  318.             $html = "<img src='$src' alt='$alt'$class />";
  319.         return media_send_to_editor($html);
  320.     }
  321.  
  322.     if ( !empty($_POST) ) {
  323.         $return = media_upload_form_handler();
  324.  
  325.         if ( is_string($return) )
  326.             return $return;
  327.         if ( is_array($return) )
  328.             $errors = $return;
  329.     }
  330.  
  331.     if ( isset($_POST['save']) ) {
  332.         $errors['upload_notice'] = __('Saved.');
  333.         return media_upload_gallery();
  334.     }
  335.  
  336.     return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
  337. }
  338.  
  339. function media_sideload_image($file, $post_id, $desc = null) {
  340.     if (!empty($file) ) {
  341.         $file_array['name'] = basename($file);
  342.         $file_array['tmp_name'] = download_url($file);
  343.         $desc = @$desc;
  344.  
  345.         $id = media_handle_sideload($file_array, $post_id, $desc);
  346.         $src = $id;
  347.  
  348.         if ( is_wp_error($id) ) {
  349.             @unlink($file_array['tmp_name']);
  350.             return $id;
  351.         }
  352.     }
  353.  
  354.     if ( !empty($src) ) {
  355.         $alt = @$desc;
  356.         $html = "<img src='$src' alt='$alt' />";
  357.         return $html;
  358.     }
  359. }
  360.  
  361. function media_upload_audio() {
  362.     if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
  363.         // Upload File button was clicked
  364.         $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
  365.         unset($_FILES);
  366.         if ( is_wp_error($id) ) {
  367.             $errors['upload_error'] = $id;
  368.             $id = false;
  369.         }
  370.     }
  371.  
  372.     if ( !empty($_POST['insertonlybutton']) ) {
  373.         $href = $_POST['insertonly']['href'];
  374.         if ( !empty($href) && !strpos($href, '://') )
  375.             $href = "http://$href";
  376.         $title = attribute_escape($_POST['insertonly']['title']);
  377.         if ( empty($title) )
  378.             $title = basename($href);
  379.         if ( !empty($title) && !empty($href) )
  380.             $html = "<a href='$href' >$title</a>";
  381.         return media_send_to_editor($html);
  382.     }
  383.  
  384.     if ( !empty($_POST) ) {
  385.         $return = media_upload_form_handler();
  386.  
  387.         if ( is_string($return) )
  388.             return $return;
  389.         if ( is_array($return) )
  390.             $errors = $return;
  391.     }
  392.  
  393.     if ( isset($_POST['save']) ) {
  394.         $errors['upload_notice'] = __('Saved.');
  395.         return media_upload_gallery();
  396.     }
  397.  
  398.     return wp_iframe( 'media_upload_type_form', 'audio', $errors, $id );
  399. }
  400.  
  401. function media_upload_video() {
  402.     if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
  403.         // Upload File button was clicked
  404.         $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
  405.         unset($_FILES);
  406.         if ( is_wp_error($id) ) {
  407.             $errors['upload_error'] = $id;
  408.             $id = false;
  409.         }
  410.     }
  411.  
  412.     if ( !empty($_POST['insertonlybutton']) ) {
  413.         $href = $_POST['insertonly']['href'];
  414.         if ( !empty($href) && !strpos($href, '://') )
  415.             $href = "http://$href";
  416.         $title = attribute_escape($_POST['insertonly']['title']);
  417.         if ( empty($title) )
  418.             $title = basename($href);
  419.         if ( !empty($title) && !empty($href) )
  420.             $html = "<a href='$href' >$title</a>";
  421.         return media_send_to_editor($html);
  422.     }
  423.  
  424.     if ( !empty($_POST) ) {
  425.         $return = media_upload_form_handler();
  426.  
  427.         if ( is_string($return) )
  428.             return $return;
  429.         if ( is_array($return) )
  430.             $errors = $return;
  431.     }
  432.  
  433.     if ( isset($_POST['save']) ) {
  434.         $errors['upload_notice'] = __('Saved.');
  435.         return media_upload_gallery();
  436.     }
  437.  
  438.     return wp_iframe( 'media_upload_type_form', 'video', $errors, $id );
  439. }
  440.  
  441. function media_upload_file() {
  442.     if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
  443.         // Upload File button was clicked
  444.         $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
  445.         unset($_FILES);
  446.         if ( is_wp_error($id) ) {
  447.             $errors['upload_error'] = $id;
  448.             $id = false;
  449.         }
  450.     }
  451.  
  452.     if ( !empty($_POST['insertonlybutton']) ) {
  453.         $href = $_POST['insertonly']['href'];
  454.         if ( !empty($href) && !strpos($href, '://') )
  455.             $href = "http://$href";
  456.         $title = attribute_escape($_POST['insertonly']['title']);
  457.         if ( empty($title) )
  458.             $title = basename($href);
  459.         if ( !empty($title) && !empty($href) )
  460.             $html = "<a href='$href' >$title</a>";
  461.         return media_send_to_editor($html);
  462.     }
  463.  
  464.     if ( !empty($_POST) ) {
  465.         $return = media_upload_form_handler();
  466.  
  467.         if ( is_string($return) )
  468.             return $return;
  469.         if ( is_array($return) )
  470.             $errors = $return;
  471.     }
  472.  
  473.     if ( isset($_POST['save']) ) {
  474.         $errors['upload_notice'] = __('Saved.');
  475.         return media_upload_gallery();
  476.     }
  477.  
  478.     return wp_iframe( 'media_upload_type_form', 'file', $errors, $id );
  479. }
  480.  
  481. function media_upload_gallery() {
  482.     if ( !empty($_POST) ) {
  483.         $return = media_upload_form_handler();
  484.  
  485.         if ( is_string($return) )
  486.             return $return;
  487.         if ( is_array($return) )
  488.             $errors = $return;
  489.     }
  490.  
  491.     wp_enqueue_script('admin-gallery');
  492.     return wp_iframe( 'media_upload_gallery_form', $errors );
  493. }
  494.  
  495. function media_upload_library() {
  496.     if ( !empty($_POST) ) {
  497.         $return = media_upload_form_handler();
  498.  
  499.         if ( is_string($return) )
  500.             return $return;
  501.         if ( is_array($return) )
  502.             $errors = $return;
  503.     }
  504.  
  505.     return wp_iframe( 'media_upload_library_form', $errors );
  506. }
  507.  
  508. function image_attachment_fields_to_edit($form_fields, $post) {
  509.     if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  510.         $form_fields['post_title']['required'] = true;
  511.  
  512.         $form_fields['post_excerpt']['label'] = __('Caption');
  513.         $form_fields['post_excerpt']['helps'][] = __('Also used as alternate text for the image');
  514.  
  515.         $form_fields['post_content']['label'] = __('Description');
  516.  
  517.         $thumb = wp_get_attachment_thumb_url($post->ID);
  518.  
  519.         $form_fields['align'] = array(
  520.             'label' => __('Alignment'),
  521.             'input' => 'html',
  522.             'html'  => "
  523.                 <input type='radio' name='attachments[$post->ID][align]' id='image-align-none-$post->ID' value='none' checked='checked' />
  524.                 <label for='image-align-none-$post->ID' class='align image-align-none-label'>" . __('None') . "</label>
  525.                 <input type='radio' name='attachments[$post->ID][align]' id='image-align-left-$post->ID' value='left' />
  526.                 <label for='image-align-left-$post->ID' class='align image-align-left-label'>" . __('Left') . "</label>
  527.                 <input type='radio' name='attachments[$post->ID][align]' id='image-align-center-$post->ID' value='center' />
  528.                 <label for='image-align-center-$post->ID' class='align image-align-center-label'>" . __('Center') . "</label>
  529.                 <input type='radio' name='attachments[$post->ID][align]' id='image-align-right-$post->ID' value='right' />
  530.                 <label for='image-align-right-$post->ID' class='align image-align-right-label'>" . __('Right') . "</label>\n",
  531.         );
  532.         $form_fields['image-size'] = array(
  533.             'label' => __('Size'),
  534.             'input' => 'html',
  535.             'html'  => "
  536.                 " . ( $thumb ? "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-thumb-$post->ID' value='thumbnail' />
  537.                 <label for='image-size-thumb-$post->ID'>" . __('Thumbnail') . "</label>
  538.                 " : '' ) . "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-medium-$post->ID' value='medium' checked='checked' />
  539.                 <label for='image-size-medium-$post->ID'>" . __('Medium') . "</label>
  540.                 <input type='radio' name='attachments[$post->ID][image-size]' id='image-size-full-$post->ID' value='full' />
  541.                 <label for='image-size-full-$post->ID'>" . __('Full size') . "</label>",
  542.         );
  543.     }
  544.     return $form_fields;
  545. }
  546.  
  547. add_filter('attachment_fields_to_edit', 'image_attachment_fields_to_edit', 10, 2);
  548.  
  549. function media_single_attachment_fields_to_edit( $form_fields, $post ) {
  550.     unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
  551.     return $form_fields;
  552. }
  553.  
  554. function image_attachment_fields_to_save($post, $attachment) {
  555.     if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
  556.         if ( strlen(trim($post['post_title'])) == 0 ) {
  557.             $post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid']));
  558.             $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
  559.         }
  560.     }
  561.  
  562.     return $post;
  563. }
  564.  
  565. add_filter('attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2);
  566.  
  567. function image_media_send_to_editor($html, $attachment_id, $attachment) {
  568.     $post =& get_post($attachment_id);
  569.     if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  570.         $url = $attachment['url'];
  571.  
  572.         if ( isset($attachment['align']) )
  573.             $align = $attachment['align'];
  574.         else
  575.             $align = 'none';
  576.  
  577.         if ( !empty($attachment['image-size']) )
  578.             $size = $attachment['image-size'];
  579.         else
  580.             $size = 'medium';
  581.  
  582.         $rel = ( $url == get_attachment_link($attachment_id) );
  583.  
  584.         return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size);
  585.     }
  586.  
  587.     return $html;
  588. }
  589.  
  590. add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
  591.  
  592. function get_attachment_fields_to_edit($post, $errors = null) {
  593.     if ( is_int($post) )
  594.         $post =& get_post($post);
  595.     if ( is_array($post) )
  596.         $post = (object) $post;
  597.  
  598.     $edit_post = sanitize_post($post, 'edit');
  599.     $file = wp_get_attachment_url($post->ID);
  600.     $link = get_attachment_link($post->ID);
  601.  
  602.     $form_fields = array(
  603.         'post_title'   => array(
  604.             'label'      => __('Title'),
  605.             'value'      => $edit_post->post_title,
  606.         ),
  607.         'post_excerpt' => array(
  608.             'label'      => __('Caption'),
  609.             'value'      => $edit_post->post_excerpt,
  610.         ),
  611.         'post_content' => array(
  612.             'label'      => __('Description'),
  613.             'value'      => $edit_post->post_content,
  614.             'input'      => 'textarea',
  615.         ),
  616.         'url'          => array(
  617.             'label'      => __('Link URL'),
  618.             'input'      => 'html',
  619.             'html'       => "
  620.                 <input type='text' name='attachments[$post->ID][url]' value='" . attribute_escape($file) . "' /><br />
  621.                 <button type='button' class='button url-$post->ID' value=''>" . __('None') . "</button>
  622.                 <button type='button' class='button url-$post->ID' value='" . attribute_escape($file) . "'>" . __('File URL') . "</button>
  623.                 <button type='button' class='button url-$post->ID' value='" . attribute_escape($link) . "'>" . __('Post URL') . "</button>
  624.                 <script type='text/javascript'>
  625.                 jQuery('button.url-$post->ID').bind('click', function(){jQuery(this).siblings('input').val(this.value);});
  626.                 </script>\n",
  627.             'helps'      => __('Enter a link URL or click above for presets.'),
  628.         ),
  629.         'menu_order'   => array(
  630.             'label'      => __('Order'),
  631.             'value'      => $edit_post->menu_order
  632.         ),
  633.     );
  634.  
  635.     foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  636.         $t = (array) get_taxonomy($taxonomy);
  637.         if ( empty($t['label']) )
  638.             $t['label'] = $taxonomy;
  639.         if ( empty($t['args']) )
  640.             $t['args'] = array();
  641.  
  642.         $terms = get_object_term_cache($post->ID, $taxonomy);
  643.         if ( empty($terms) )
  644.             $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  645.  
  646.         $values = array();
  647.  
  648.         foreach ( $terms as $term )
  649.             $values[] = $term->name;
  650.         $t['value'] = join(', ', $values);
  651.  
  652.         $form_fields[$taxonomy] = $t;
  653.     }
  654.  
  655.     // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  656.     // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
  657.     $form_fields = array_merge_recursive($form_fields, (array) $errors);
  658.  
  659.     $form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post);
  660.  
  661.     return $form_fields;
  662. }
  663.  
  664. function get_media_items( $post_id, $errors ) {
  665.     if ( $post_id ) {
  666.         $post = get_post($post_id);
  667.         if ( $post && $post->post_type == 'attachment' )
  668.             $attachments = array($post->ID => $post);
  669.         else
  670.             $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
  671.     } else {
  672.         if ( is_array($GLOBALS['wp_the_query']->posts) )
  673.             foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
  674.                 $attachments[$attachment->ID] = $attachment;
  675.     }
  676.  
  677.     if ( empty($attachments) )
  678.         return '';
  679.  
  680.     foreach ( $attachments as $id => $attachment )
  681.         if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
  682.             $output .= "\n<div id='media-item-$id' class='media-item child-of-$attachment->post_parent preloaded'><div class='progress'><div class='bar'></div></div><div id='media-upload-error-$id'></div><div class='filename'></div>$item\n</div>";
  683.  
  684.     return $output;
  685. }
  686.  
  687. function get_media_item( $attachment_id, $args = null ) {
  688.     global $redir_tab;
  689.  
  690.     $default_args = array( 'errors' => null, 'send' => true, 'delete' => true, 'toggle' => true );
  691.     $args = wp_parse_args( $args, $default_args );
  692.     extract( $args, EXTR_SKIP );
  693.  
  694.     global $post_mime_types;
  695.     if ( ( $attachment_id = intval($attachment_id) ) && $thumb_url = get_attachment_icon_src( $attachment_id ) )
  696.         $thumb_url = $thumb_url[0];
  697.     else
  698.         return false;
  699.  
  700.     $title_label = __('Title');
  701.     $description_label = __('Description');
  702.     $tags_label = __('Tags');
  703.  
  704.     $toggle_on = __('Show');
  705.     $toggle_off = __('Hide');
  706.  
  707.     $post = get_post($attachment_id);
  708.  
  709.     $filename = basename($post->guid);
  710.     $title = attribute_escape($post->post_title);
  711.     $description = attribute_escape($post->post_content);
  712.     if ( $_tags = get_the_tags($attachment_id) ) {
  713.         foreach ( $_tags as $tag )
  714.             $tags[] = $tag->name;
  715.         $tags = attribute_escape(join(', ', $tags));
  716.     }
  717.  
  718.     if ( isset($post_mime_types) ) {
  719.         $keys = array_keys(wp_match_mime_types(array_keys($post_mime_types), $post->post_mime_type));
  720.         $type = array_shift($keys);
  721.         $type = "<input type='hidden' id='type-of-$attachment_id' value='" . attribute_escape( $type ) . "' />";
  722.     }
  723.  
  724.     $form_fields = get_attachment_fields_to_edit($post, $errors);
  725.  
  726.     if ( $toggle ) {
  727.         $class = empty($errors) ? 'startclosed' : 'startopen';
  728.         $toggle_links = "
  729.     <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
  730.     <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
  731.     } else {
  732.         $class = 'form-table';
  733.         $toggle_links = '';
  734.     }
  735.  
  736.     $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
  737.     $display_title = wp_html_excerpt($display_title, 60);
  738.  
  739.     $gallery = ( (isset($_REQUEST['tab']) && 'gallery' == $_REQUEST['tab']) || (isset($redir_tab) && 'gallery' == $redir_tab) ) ? true : false;
  740.     $order = '';
  741.  
  742.     foreach ( $form_fields as $key => $val ) {
  743.         if ( 'menu_order' == $key ) {
  744.             if ( $gallery )
  745.                 $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>';
  746.             else
  747.                 $order = '<input type="hidden" name="attachments['.$attachment_id.'][menu_order]" value="'.$val['value'].'" />';
  748.  
  749.             unset($form_fields['menu_order']);
  750.             break;
  751.         }
  752.     }
  753.  
  754.     $item = "
  755.     $type
  756.     $toggle_links
  757.     $order
  758.     <div class='filename new'>$display_title</div>
  759.     <table class='slidetoggle describe $class'>
  760.         <thead class='media-item-info'>
  761.         <tr>
  762.             <td class='A1B1' rowspan='4'><img class='thumbnail' src='$thumb_url' alt='' /></td>
  763.             <td>$filename</td>
  764.         </tr>
  765.         <tr><td>$post->post_mime_type</td></tr>
  766.         <tr><td>" . mysql2date($post->post_date, get_option('time_format')) . "</td></tr>
  767.         <tr><td>" . apply_filters('media_meta', '', $post) . "</td></tr>
  768.         </thead>
  769.         <tbody>\n";
  770.  
  771.     $defaults = array(
  772.         'input'      => 'text',
  773.         'required'   => false,
  774.         'value'      => '',
  775.         'extra_rows' => array(),
  776.     );
  777.  
  778.     $delete_href = wp_nonce_url("post.php?action=delete-post&post=$attachment_id", 'delete-post_' . $attachment_id);
  779.     if ( $send )
  780.         $send = "<input type='submit' class='button' name='send[$attachment_id]' value='" . attribute_escape( __( 'Insert into Post' ) ) . "' />";
  781.     if ( $delete )
  782.         $delete = "<a href=\"#\" class=\"del-link\" onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __('Delete') . "</a>";
  783.     if ( ( $send || $delete ) && !isset($form_fields['buttons']) )
  784.         $form_fields['buttons'] = array('tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send $delete
  785.         <div id=\"del_attachment_$attachment_id\" class=\"del-attachment\" style=\"display:none;\">" . sprintf(__("You are about to delete <strong>%s</strong>."), $filename) . " <a href=\"$delete_href\" id=\"del[$attachment_id]\" class=\"delete\">" . __('Continue') . "</a>
  786.         <a href=\"#\" class=\"del-link\" onclick=\"this.parentNode.style.display='none';return false;\">" . __('Cancel') . "</a></div></td></tr>\n");
  787.  
  788.     $hidden_fields = array();
  789.  
  790.     foreach ( $form_fields as $id => $field ) {
  791.         if ( $id{0} == '_' )
  792.             continue;
  793.  
  794.         if ( !empty($field['tr']) ) {
  795.             $item .= $field['tr'];
  796.             continue;
  797.         }
  798.  
  799.         $field = array_merge($defaults, $field);
  800.         $name = "attachments[$attachment_id][$id]";
  801.  
  802.         if ( $field['input'] == 'hidden' ) {
  803.             $hidden_fields[$name] = $field['value'];
  804.             continue;
  805.         }
  806.  
  807.         $required = $field['required'] ? '<abbr title="required" class="required">*</abbr>' : '';
  808.         $aria_required = $field['required'] ? " aria-required='true' " : '';
  809.         $class  = $id;
  810.         $class .= $field['required'] ? ' form-required' : '';
  811.  
  812.         $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'>";
  813.         if ( !empty($field[$field['input']]) )
  814.             $item .= $field[$field['input']];
  815.         elseif ( $field['input'] == 'textarea' ) {
  816.             $item .= "<textarea type='text' id='$name' name='$name'>" . attribute_escape( $field['value'] ) . $aria_required . "</textarea>";
  817.         } else {
  818.             $item .= "<input type='text' id='$name' name='$name' value='" . attribute_escape( $field['value'] ) . "'" . $aria_required . "/>";
  819.         }
  820.         if ( !empty($field['helps']) )
  821.             $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique((array) $field['helps']) ) . '</p>';
  822.         $item .= "</td>\n\t\t</tr>\n";
  823.  
  824.         $extra_rows = array();
  825.  
  826.         if ( !empty($field['errors']) )
  827.             foreach ( array_unique((array) $field['errors']) as $error )
  828.                 $extra_rows['error'][] = $error;
  829.  
  830.         if ( !empty($field['extra_rows']) )
  831.             foreach ( $field['extra_rows'] as $class => $rows )
  832.                 foreach ( (array) $rows as $html )
  833.                     $extra_rows[$class][] = $html;
  834.  
  835.         foreach ( $extra_rows as $class => $rows )
  836.             foreach ( $rows as $html )
  837.                 $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  838.     }
  839.  
  840.     if ( !empty($form_fields['_final']) )
  841.         $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  842.     $item .= "\t</tbody>\n";
  843.     $item .= "\t</table>\n";
  844.  
  845.     foreach ( $hidden_fields as $name => $value )
  846.         $item .= "\t<input type='hidden' name='$name' id='$name' value='" . attribute_escape( $value ) . "' />\n";
  847.  
  848.     return $item;
  849. }
  850.  
  851. function media_upload_header() {
  852.     ?>
  853.     <script type="text/javascript">post_id = <?php echo intval($_REQUEST['post_id']); ?>;</script>
  854.     <div id="media-upload-header">
  855.     <?php the_media_upload_tabs(); ?>
  856.     </div>
  857.     <?php
  858. }
  859.  
  860. function media_upload_form( $errors = null ) {
  861.     global $type, $tab;
  862.  
  863.     $flash_action_url = admin_url('async-upload.php');
  864.  
  865.     // If Mac and mod_security, no Flash. :(
  866.     $flash = true;
  867.     if ( false !== strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'mac') && apache_mod_loaded('mod_security') )
  868.         $flash = false;
  869.  
  870.     $flash = apply_filters('flash_uploader', $flash);
  871.     $post_id = intval($_REQUEST['post_id']);
  872.  
  873. ?>
  874. <div id="media-upload-notice">
  875. <?php if (isset($errors['upload_notice']) ) { ?>
  876.     <?php echo $errors['upload_notice']; ?>
  877. <?php } ?>
  878. </div>
  879. <div id="media-upload-error">
  880. <?php if (isset($errors['upload_error']) && is_wp_error($errors['upload_error'])) { ?>
  881.     <?php echo $errors['upload_error']->get_error_message(); ?>
  882. <?php } ?>
  883. </div>
  884.  
  885. <?php do_action('pre-upload-ui'); ?>
  886.  
  887. <?php if ( $flash ) : ?>
  888. <script type="text/javascript">
  889. <!--
  890. jQuery(function($){
  891.     swfu = new SWFUpload({
  892.             upload_url : "<?php echo attribute_escape( $flash_action_url ); ?>",
  893.             flash_url : "<?php echo includes_url('js/swfupload/swfupload_f9.swf'); ?>",
  894.             file_post_name: "async-upload",
  895.             file_types: "<?php echo apply_filters('upload_file_glob', '*.*'); ?>",
  896.             post_params : {
  897.                 "post_id" : "<?php echo $post_id; ?>",
  898.                 "auth_cookie" : "<?php if ( is_ssl() ) echo $_COOKIE[SECURE_AUTH_COOKIE]; else echo $_COOKIE[AUTH_COOKIE]; ?>",
  899.                 "_wpnonce" : "<?php echo wp_create_nonce('media-form'); ?>",
  900.                 "type" : "<?php echo $type; ?>",
  901.                 "tab" : "<?php echo $tab; ?>",
  902.                 "short" : "1"
  903.             },
  904.             file_size_limit : "<?php echo wp_max_upload_size(); ?>b",
  905.             swfupload_element_id : "flash-upload-ui", // id of the element displayed when swfupload is available
  906.             degraded_element_id : "html-upload-ui",   // when swfupload is unavailable
  907.             file_dialog_start_handler : fileDialogStart,
  908.             file_queued_handler : fileQueued,
  909.             upload_start_handler : uploadStart,
  910.             upload_progress_handler : uploadProgress,
  911.             upload_error_handler : uploadError,
  912.             upload_success_handler : uploadSuccess,
  913.             upload_complete_handler : uploadComplete,
  914.             file_queue_error_handler : fileQueueError,
  915.             file_dialog_complete_handler : fileDialogComplete,
  916.  
  917.             debug: false
  918.         });
  919.     $("#flash-browse-button").bind( "click", function(){swfu.selectFiles();});
  920. });
  921. //-->
  922. </script>
  923.  
  924. <div id="flash-upload-ui">
  925. <?php do_action('pre-flash-upload-ui'); ?>
  926.     <p><input id="flash-browse-button" type="button" value="<?php echo attribute_escape( __( 'Choose files to upload' ) ); ?>" class="button" /></p>
  927. <?php do_action('post-flash-upload-ui'); ?>
  928.     <p class="howto"><?php _e('After a file has been uploaded, you can add titles and descriptions.'); ?></p>
  929. </div>
  930.  
  931. <?php endif; // $flash ?>
  932.  
  933. <div id="html-upload-ui">
  934. <?php do_action('pre-html-upload-ui'); ?>
  935.     <p>
  936.     <input type="file" name="async-upload" id="async-upload" /> <input type="submit" class="button" name="html-upload" value="<?php echo attribute_escape(__('Upload')); ?>" /> <a href="#" onclick="return top.tb_remove();"><?php _e('Cancel'); ?></a>
  937.     </p>
  938.     <br class="clear" />
  939.     <?php if ( is_lighttpd_before_150() ): ?>
  940.     <p><?php _e('If you want to use all capabilities of the uploader, like uploading multiple files at once, please upgrade to lighttpd 1.5.'); ?></p>
  941.     <?php endif;?>
  942. <?php do_action('post-html-upload-ui'); ?>
  943. </div>
  944. <?php do_action('post-upload-ui'); ?>
  945. <?php
  946. }
  947.  
  948. function media_upload_type_form($type = 'file', $errors = null, $id = null) {
  949.     media_upload_header();
  950.  
  951.     $post_id = intval($_REQUEST['post_id']);
  952.  
  953.     $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
  954.     $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
  955.  
  956.     $callback = "type_form_$type";
  957. ?>
  958.  
  959. <form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form type-form validate" id="<?php echo $type; ?>-form">
  960. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  961. <?php wp_nonce_field('media-form'); ?>
  962. <h3><?php _e('From Computer'); ?></h3>
  963. <?php media_upload_form( $errors ); ?>
  964.  
  965. <script type="text/javascript">
  966. <!--
  967. jQuery(function($){
  968.     var preloaded = $(".media-item.preloaded");
  969.     if ( preloaded.length > 0 ) {
  970.         preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  971.     }
  972.     updateMediaForm();
  973. });
  974. -->
  975. </script>
  976. <?php if ( $id && !is_wp_error($id) ) : ?>
  977. <div id="media-items">
  978. <?php echo get_media_items( $id, $errors ); ?>
  979. </div>
  980. <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
  981.  
  982. <?php elseif ( is_callable($callback) ) : ?>
  983.  
  984. <div class="media-blank">
  985. <p style="text-align:center"><?php _e('— OR —'); ?></p>
  986. <h3><?php _e('From URL'); ?></h3>
  987. </div>
  988.  
  989. <script type="text/javascript">
  990. //<![CDATA[
  991. var addExtImage = {
  992.  
  993.     width : '',
  994.     height : '',
  995.     align : 'alignnone',
  996.  
  997.     insert : function() {
  998.         var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = null;
  999.  
  1000.         if ( '' == f.src.value || '' == t.width ) return false;
  1001.  
  1002.         if ( f.title.value ) {
  1003.             title = f.title.value.replace(/['"<>]+/g, '');
  1004.             title = ' title="'+title+'"';
  1005.         }
  1006.  
  1007.         if ( f.alt.value ) {
  1008.             alt = f.alt.value.replace(/['"<>]+/g, '');
  1009. <?php if ( ! apply_filters( 'disable_captions', '' ) ) { ?>
  1010.             caption = f.alt.value.replace(/'/g, ''').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>');
  1011. <?php } ?>
  1012.         }
  1013.  
  1014.         cls = caption ? '' : ' class="'+t.align+'"';
  1015.  
  1016.         html = '<img alt="'+alt+'" src="'+f.src.value+'"'+title+cls+' width="'+t.width+'" height="'+t.height+'" />';
  1017.  
  1018.         if ( f.url.value )
  1019.             html = '<a href="'+f.url.value+'">'+html+'</a>';
  1020.  
  1021.         if ( caption )
  1022.             html = '[caption id="" align="'+t.align+'" width="'+t.width+'" caption="'+caption+'"]'+html+'[/caption]';
  1023.  
  1024.         var win = window.dialogArguments || opener || parent || top;
  1025.         win.send_to_editor(html);
  1026.     },
  1027.  
  1028.     resetImageData : function() {
  1029.         var t = addExtImage;
  1030.  
  1031.         t.width = t.height = '';
  1032.         document.getElementById('go_button').style.color = '#bbb';
  1033.         if ( ! document.forms[0].src.value )
  1034.             document.getElementById('status_img').src = 'images/required.gif';
  1035.         else document.getElementById('status_img').src = 'images/no.png';
  1036.     },
  1037.  
  1038.     updateImageData : function() {
  1039.         var t = addExtImage;
  1040.  
  1041.         t.width = t.preloadImg.width;
  1042.         t.height = t.preloadImg.height;
  1043.         document.getElementById('go_button').style.color = '#333';
  1044.         document.getElementById('status_img').src = 'images/yes.png';
  1045.     },
  1046.  
  1047.     getImageData : function() {
  1048.         var t = addExtImage, src = document.forms[0].src.value;
  1049.  
  1050.         if ( ! src ) {
  1051.             t.resetImageData();
  1052.             return false;
  1053.         }
  1054.         document.getElementById('status_img').src = 'images/loading.gif';
  1055.         t.preloadImg = new Image();
  1056.         t.preloadImg.onload = t.updateImageData;
  1057.         t.preloadImg.onerror = t.resetImageData;
  1058.         t.preloadImg.src = src;
  1059.     }
  1060. }
  1061. //]]>
  1062. </script>
  1063.  
  1064. <div id="media-items">
  1065. <div class="media-item media-blank">
  1066. <?php echo call_user_func($callback); ?>
  1067. </div>
  1068. </div>
  1069. <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
  1070. </form>
  1071. <?php
  1072.     endif;
  1073. }
  1074.  
  1075. function media_upload_gallery_form($errors) {
  1076.     global $redir_tab;
  1077.  
  1078.     $redir_tab = 'gallery';
  1079.     media_upload_header();
  1080.  
  1081.     $post_id = intval($_REQUEST['post_id']);
  1082.     $form_action_url = admin_url("media-upload.php?type={$GLOBALS['type']}&tab=gallery&post_id=$post_id");
  1083. ?>
  1084.  
  1085. <script type="text/javascript">
  1086. <!--
  1087. jQuery(function($){
  1088.     var preloaded = $(".media-item.preloaded");
  1089.     if ( preloaded.length > 0 ) {
  1090.         preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1091.         updateMediaForm();
  1092.     }
  1093. });
  1094. -->
  1095. </script>
  1096.  
  1097. <form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form validate" id="gallery-form">
  1098. <?php wp_nonce_field('media-form'); ?>
  1099. <?php //media_upload_form( $errors ); ?>
  1100. <table class="widefat">
  1101. <thead><tr>
  1102. <th><?php _e('Media'); ?></th>
  1103. <th class="order-head"><?php _e('Order'); ?></th>
  1104. </tr></thead>
  1105. </table>
  1106. <div id="media-items">
  1107. <?php echo get_media_items($post_id, $errors); ?>
  1108. </div>
  1109. <p class="ml-submit">
  1110. <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
  1111. <input type="submit" class="button insert-gallery" name="insert-gallery" value="<?php echo attribute_escape( __( 'Insert gallery into post' ) ); ?>" />
  1112. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1113. <input type="hidden" name="type" value="<?php echo attribute_escape( $GLOBALS['type'] ); ?>" />
  1114. <input type="hidden" name="tab" value="<?php echo attribute_escape( $GLOBALS['tab'] ); ?>" />
  1115. </p>
  1116. </form>
  1117. <?php
  1118. }
  1119.  
  1120. function media_upload_library_form($errors) {
  1121.     global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
  1122.  
  1123.     media_upload_header();
  1124.  
  1125.     $post_id = intval($_REQUEST['post_id']);
  1126.  
  1127.     $form_action_url = admin_url("media-upload.php?type={$GLOBALS['type']}&tab=library&post_id=$post_id");
  1128.  
  1129.     $_GET['paged'] = intval($_GET['paged']);
  1130.     if ( $_GET['paged'] < 1 )
  1131.         $_GET['paged'] = 1;
  1132.     $start = ( $_GET['paged'] - 1 ) * 10;
  1133.     if ( $start < 1 )
  1134.         $start = 0;
  1135.     add_filter( 'post_limits', $limit_filter = create_function( '$a', "return 'LIMIT $start, 10';" ) );
  1136.  
  1137.     list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  1138.  
  1139. ?>
  1140.  
  1141. <form id="filter" action="" method="get">
  1142. <input type="hidden" name="type" value="<?php echo attribute_escape( $type ); ?>" />
  1143. <input type="hidden" name="tab" value="<?php echo attribute_escape( $tab ); ?>" />
  1144. <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
  1145. <input type="hidden" name="post_mime_type" value="<?php echo attribute_escape( $_GET['post_mime_type'] ); ?>" />
  1146.  
  1147. <div id="search-filter">
  1148.     <label class="hidden" for="post-search-input"><?php _e('Search Media');?>:</label>
  1149.     <input type="text" id="post-search-input" name="s" value="<?php the_search_query(); ?>" />
  1150.     <input type="submit" value="<?php echo attribute_escape( __( 'Search Media' ) ); ?>" class="button" />
  1151. </div>
  1152.  
  1153. <ul class="subsubsub">
  1154. <?php
  1155. $type_links = array();
  1156. $_num_posts = (array) wp_count_attachments();
  1157. $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  1158. foreach ( $matches as $_type => $reals )
  1159.     foreach ( $reals as $real )
  1160.         $num_posts[$_type] += $_num_posts[$real];
  1161. // If available type specified by media button clicked, filter by that type
  1162. if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
  1163.     $_GET['post_mime_type'] = $type;
  1164.     list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  1165. }
  1166. if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
  1167.     $class = ' class="current"';
  1168. $type_links[] = "<li><a href='" . clean_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
  1169. foreach ( $post_mime_types as $mime_type => $label ) {
  1170.     $class = '';
  1171.  
  1172.     if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  1173.         continue;
  1174.  
  1175.     if ( wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  1176.         $class = ' class="current"';
  1177.  
  1178.     $type_links[] = "<li><a href='" . clean_url(add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false))) . "'$class>" . sprintf(__ngettext($label[2][0], $label[2][1], $num_posts[$mime_type]), "<span id='$mime_type-counter'>" . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
  1179. }
  1180. echo implode(' | </li>', $type_links) . '</li>';
  1181. unset($type_links);
  1182. ?>
  1183. </ul>
  1184.  
  1185. <div class="tablenav">
  1186.  
  1187. <?php
  1188. $page_links = paginate_links( array(
  1189.     'base' => add_query_arg( 'paged', '%#%' ),
  1190.     'format' => '',
  1191.     'total' => ceil($wp_query->found_posts / 10),
  1192.     'current' => $_GET['paged']
  1193. ));
  1194.  
  1195. if ( $page_links )
  1196.     echo "<div class='tablenav-pages'>$page_links</div>";
  1197. ?>
  1198.  
  1199. <div class="alignleft">
  1200. <?php
  1201.  
  1202. $arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC";
  1203.  
  1204. $arc_result = $wpdb->get_results( $arc_query );
  1205.  
  1206. $month_count = count($arc_result);
  1207.  
  1208. if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
  1209. <select name='m'>
  1210. <option<?php selected( @$_GET['m'], 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
  1211. <?php
  1212. foreach ($arc_result as $arc_row) {
  1213.     if ( $arc_row->yyear == 0 )
  1214.         continue;
  1215.     $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
  1216.  
  1217.     if ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] )
  1218.         $default = ' selected="selected"';
  1219.     else
  1220.         $default = '';
  1221.  
  1222.     echo "<option$default value='" . attribute_escape( $arc_row->yyear . $arc_row->mmonth ) . "'>";
  1223.     echo wp_specialchars( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
  1224.     echo "</option>\n";
  1225. }
  1226. ?>
  1227. </select>
  1228. <?php } ?>
  1229.  
  1230. <input type="submit" id="post-query-submit" value="<?php echo attribute_escape( __( 'Filter »' ) ); ?>" class="button-secondary" />
  1231.  
  1232. </div>
  1233.  
  1234. <br class="clear" />
  1235. </div>
  1236. </form>
  1237.  
  1238. <form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form validate" id="library-form">
  1239.  
  1240. <?php wp_nonce_field('media-form'); ?>
  1241. <?php //media_upload_form( $errors ); ?>
  1242.  
  1243. <script type="text/javascript">
  1244. <!--
  1245. jQuery(function($){
  1246.     var preloaded = $(".media-item.preloaded");
  1247.     if ( preloaded.length > 0 ) {
  1248.         preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1249.         updateMediaForm();
  1250.     }
  1251. });
  1252. -->
  1253. </script>
  1254.  
  1255. <div id="media-items">
  1256. <?php echo get_media_items(null, $errors); ?>
  1257. </div>
  1258. <p class="ml-submit">
  1259. <input type="submit" class="button savebutton" name="save" value="<?php echo attribute_escape( __( 'Save all changes' ) ); ?>" />
  1260. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1261. </p>
  1262. </form>
  1263. <?php
  1264. }
  1265.  
  1266. function type_form_image() {
  1267.  
  1268.     if ( apply_filters( 'disable_captions', '' ) ) {
  1269.         $alt = __('Alternate Text');
  1270.         $alt_help = __('Alt text for the image, e.g. "The Mona Lisa"');
  1271.     } else {
  1272.         $alt = __('Image Caption');
  1273.         $alt_help = __('Also used as alternate text for the image');
  1274.     }
  1275.  
  1276.     return '
  1277.     <table class="describe"><tbody>
  1278.         <tr>
  1279.             <th valign="top" scope="row" class="label" style="width:120px;">
  1280.                 <span class="alignleft"><label for="src">' . __('Source') . '</label></span>
  1281.                 <span class="alignright"><img id="status_img" src="images/required.gif" title="required" alt="required" /></span>
  1282.             </th>
  1283.             <td class="field"><input id="src" name="src" value="" type="text" aria-required="true" onblur="addExtImage.getImageData()" /></td>
  1284.         </tr>
  1285.  
  1286.         <tr>
  1287.             <th valign="top" scope="row" class="label">
  1288.                 <span class="alignleft"><label for="title">' . __('Image Title') . '</label></span>
  1289.                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1290.             </th>
  1291.             <td class="field"><p><input id="title" name="title" value="" type="text" aria-required="true" /></p></td>
  1292.         </tr>
  1293.  
  1294.         <tr>
  1295.             <th valign="top" scope="row" class="label">
  1296.                 <span class="alignleft"><label for="alt">' . $alt . '</label></span>
  1297.             </th>
  1298.             <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
  1299.             <p class="help">' . $alt_help . '</p></td>
  1300.         </tr>
  1301.  
  1302.         <tr class="align">
  1303.             <th valign="top" scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
  1304.             <td class="field">
  1305.                 <input name="align" id="align-none" value="alignnone" onclick="addExtImage.align=this.value" type="radio" checked="checked" />
  1306.                 <label for="align-none" class="align image-align-none-label">' . __('None') . '</label>
  1307.                 <input name="align" id="align-left" value="alignleft" onclick="addExtImage.align=this.value" type="radio" />
  1308.                 <label for="align-left" class="align image-align-left-label">' . __('Left') . '</label>
  1309.                 <input name="align" id="align-center" value="aligncenter" onclick="addExtImage.align=this.value" type="radio" />
  1310.                 <label for="align-center" class="align image-align-center-label">' . __('Center') . '</label>
  1311.                 <input name="align" id="align-right" value="alignright" onclick="addExtImage.align=this.value" type="radio" />
  1312.                 <label for="align-right" class="align image-align-right-label">' . __('Right') . '</label>
  1313.             </td>
  1314.         </tr>
  1315.  
  1316.         <tr>
  1317.             <th valign="top" scope="row" class="label">
  1318.                 <span class="alignleft"><label for="url">' . __('Link URL') . '</label></span>
  1319.             </th>
  1320.             <td class="field"><input id="url" name="url" value="" type="text" /><br />
  1321.  
  1322.             <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __('None') . '</button>
  1323.             <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __('Link to image') . '</button>
  1324.             <p class="help">' . __('Enter a link URL or click above for presets.') . '</p></td>
  1325.         </tr>
  1326.  
  1327.         <tr>
  1328.             <td></td>
  1329.             <td>
  1330.                 <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . attribute_escape(__('Insert into Post')) . '" />
  1331.             </td>
  1332.         </tr>
  1333.     </tbody></table>
  1334. ';
  1335.  
  1336. }
  1337.  
  1338. function type_form_audio() {
  1339.     return '
  1340.     <table class="describe"><tbody>
  1341.         <tr>
  1342.             <th valign="top" scope="row" class="label">
  1343.                 <span class="alignleft"><label for="insertonly[href]">' . __('Audio File URL') . '</label></span>
  1344.                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1345.             </th>
  1346.             <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
  1347.         </tr>
  1348.         <tr>
  1349.             <th valign="top" scope="row" class="label">
  1350.                 <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
  1351.                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1352.             </th>
  1353.             <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
  1354.         </tr>
  1355.         <tr><td></td><td class="help">' . __('Link text, e.g. "Still Alive by Jonathan Coulton"') . '</td></tr>
  1356.         <tr>
  1357.             <td></td>
  1358.             <td>
  1359.                 <input type="submit" class="button" name="insertonlybutton" value="' . attribute_escape(__('Insert into Post')) . '" />
  1360.             </td>
  1361.         </tr>
  1362.     </tbody></table>
  1363. ';
  1364. }
  1365.  
  1366. function type_form_video() {
  1367.     return '
  1368.     <table class="describe"><tbody>
  1369.         <tr>
  1370.             <th valign="top" scope="row" class="label">
  1371.                 <span class="alignleft"><label for="insertonly[href]">' . __('Video URL') . '</label></span>
  1372.                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1373.             </th>
  1374.             <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
  1375.         </tr>
  1376.         <tr>
  1377.             <th valign="top" scope="row" class="label">
  1378.                 <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
  1379.                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1380.             </th>
  1381.             <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
  1382.         </tr>
  1383.         <tr><td></td><td class="help">' . __('Link text, e.g. "Lucy on YouTube"') . '</td></tr>
  1384.         <tr>
  1385.             <td></td>
  1386.             <td>
  1387.                 <input type="submit" class="button" name="insertonlybutton" value="' . attribute_escape(__('Insert into Post')) . '" />
  1388.             </td>
  1389.         </tr>
  1390.     </tbody></table>
  1391. ';
  1392. }
  1393.  
  1394. function type_form_file() {
  1395.     return '
  1396.     <table class="describe"><tbody>
  1397.         <tr>
  1398.             <th valign="top" scope="row" class="label">
  1399.                 <span class="alignleft"><label for="insertonly[href]">' . __('URL') . '</label></span>
  1400.                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1401.             </th>
  1402.             <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
  1403.         </tr>
  1404.         <tr>
  1405.             <th valign="top" scope="row" class="label">
  1406.                 <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
  1407.                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1408.             </th>
  1409.             <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
  1410.         </tr>
  1411.         <tr><td></td><td class="help">' . __('Link text, e.g. "Ransom Demands (PDF)"') . '</td></tr>
  1412.         <tr>
  1413.             <td></td>
  1414.             <td>
  1415.                 <input type="submit" class="button" name="insertonlybutton" value="' . attribute_escape(__('Insert into Post')) . '" />
  1416.             </td>
  1417.         </tr>
  1418.     </tbody></table>
  1419. ';
  1420. }
  1421.  
  1422. // support a GET parameter for disabling the flash uploader
  1423. function media_upload_use_flash($flash) {
  1424.         if ( array_key_exists('flash', $_REQUEST) )
  1425.                 $flash = !empty($_REQUEST['flash']);
  1426.         return $flash;
  1427. }
  1428.  
  1429. add_filter('flash_uploader', 'media_upload_use_flash');
  1430.  
  1431. function media_upload_flash_bypass() {
  1432.         echo '<p class="upload-flash-bypass">';
  1433.         printf( __('You are using the Flash uploader.  Problems?  Try the <a href="%s">Browser uploader</a> instead.'), clean_url(add_query_arg('flash', 0)) );
  1434.         echo '</p>';
  1435. }
  1436.  
  1437. add_action('post-flash-upload-ui', 'media_upload_flash_bypass');
  1438.  
  1439. function media_upload_html_bypass() {
  1440.         echo '<p class="upload-html-bypass">';
  1441.         if ( array_key_exists('flash', $_REQUEST) )
  1442.                 // the user manually selected the browser uploader, so let them switch back to Flash
  1443.                 printf( __('You are using the Browser uploader.  Try the <a href="%s">Flash uploader</a> instead.'), clean_url(add_query_arg('flash', 1)) );
  1444.         else
  1445.                 // the user probably doesn't have Flash
  1446.                 printf( __('You are using the Browser uploader.') );
  1447.  
  1448.         echo '</p>';
  1449. }
  1450.  
  1451. add_action('post-flash-upload-ui', 'media_upload_flash_bypass');
  1452. add_action('post-html-upload-ui', 'media_upload_html_bypass');
  1453.  
  1454. // make sure the GET parameter sticks when we submit a form
  1455. function media_upload_bypass_url($url) {
  1456.         if ( array_key_exists('flash', $_REQUEST) )
  1457.                 $url = add_query_arg('flash', intval($_REQUEST['flash']));
  1458.         return $url;
  1459. }
  1460.  
  1461. add_filter('media_upload_form_url', 'media_upload_bypass_url');
  1462.  
  1463.  
  1464.  
  1465. add_filter('async_upload_image', 'get_media_item', 10, 2);
  1466. add_filter('async_upload_audio', 'get_media_item', 10, 2);
  1467. add_filter('async_upload_video', 'get_media_item', 10, 2);
  1468. add_filter('async_upload_file', 'get_media_item', 10, 2);
  1469.  
  1470. add_action('media_upload_image', 'media_upload_image');
  1471. add_action('media_upload_audio', 'media_upload_audio');
  1472. add_action('media_upload_video', 'media_upload_video');
  1473. add_action('media_upload_file', 'media_upload_file');
  1474.  
  1475. add_filter('media_upload_gallery', 'media_upload_gallery');
  1476.  
  1477. add_filter('media_upload_library', 'media_upload_library');
  1478.  
  1479. ?>
  1480.