PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/msw/dev/wp-admin/includes/media.php

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

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