PageRenderTime 66ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/media.php

https://github.com/younggive/WordPress
PHP | 2177 lines | 2118 code | 18 blank | 41 comment | 11 complexity | aeb285698ae66e4dbc877ead41476c64 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

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

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