PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/media.php

https://github.com/schr/wordpress
PHP | 2079 lines | 1789 code | 141 blank | 149 comment | 88 complexity | cb53b509fb55e24c40d173bdd23f2701 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file