PageRenderTime 71ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/media.php

https://bitbucket.org/Wallynm/iptb
PHP | 2105 lines | 2046 code | 19 blank | 40 comment | 13 complexity | 81a0c14bd10b6e4165bebebfe45592bc MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, GPL-3.0

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

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