PageRenderTime 295ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/public/wp-admin/includes/media.php

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

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