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

/wp-admin/includes/media.php

https://github.com/dipakdotyadav/WordPress
PHP | 2399 lines | 2340 code | 18 blank | 41 comment | 11 complexity | 56dec3757c6f2fd72482c2e9583e9f5c MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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, '', $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. $time = current_time( 'mysql' );
  228. if ( $post = get_post( $post_id ) ) {
  229. if ( substr( $post->post_date, 0, 4 ) > 0 )
  230. $time = $post->post_date;
  231. }
  232. $file = wp_handle_sideload( $file_array, $overrides, $time );
  233. if ( isset($file['error']) )
  234. return new WP_Error( 'upload_error', $file['error'] );
  235. $url = $file['url'];
  236. $type = $file['type'];
  237. $file = $file['file'];
  238. $title = preg_replace('/\.[^.]+$/', '', basename($file));
  239. $content = '';
  240. // use image exif/iptc data for title and caption defaults if possible
  241. if ( $image_meta = @wp_read_image_metadata($file) ) {
  242. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
  243. $title = $image_meta['title'];
  244. if ( trim( $image_meta['caption'] ) )
  245. $content = $image_meta['caption'];
  246. }
  247. if ( isset( $desc ) )
  248. $title = $desc;
  249. // Construct the attachment array
  250. $attachment = array_merge( array(
  251. 'post_mime_type' => $type,
  252. 'guid' => $url,
  253. 'post_parent' => $post_id,
  254. 'post_title' => $title,
  255. 'post_content' => $content,
  256. ), $post_data );
  257. // This should never be set as it would then overwrite an existing attachment.
  258. if ( isset( $attachment['ID'] ) )
  259. unset( $attachment['ID'] );
  260. // Save the attachment metadata
  261. $id = wp_insert_attachment($attachment, $file, $post_id);
  262. if ( !is_wp_error($id) )
  263. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  264. return $id;
  265. }
  266. /**
  267. * Adds the iframe to display content for the media upload page
  268. *
  269. * @since 2.5.0
  270. *
  271. * @param array $content_func
  272. */
  273. function wp_iframe($content_func /* ... */) {
  274. _wp_admin_html_begin();
  275. ?>
  276. <title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
  277. <?php
  278. wp_enqueue_style( 'colors' );
  279. // Check callback name for 'media'
  280. if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
  281. || ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
  282. wp_enqueue_style( 'media' );
  283. wp_enqueue_style( 'ie' );
  284. ?>
  285. <script type="text/javascript">
  286. //<![CDATA[
  287. 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();}}};
  288. 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(); ?>'};
  289. var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
  290. isRtl = <?php echo (int) is_rtl(); ?>;
  291. //]]>
  292. </script>
  293. <?php
  294. do_action('admin_enqueue_scripts', 'media-upload-popup');
  295. do_action('admin_print_styles-media-upload-popup');
  296. do_action('admin_print_styles');
  297. do_action('admin_print_scripts-media-upload-popup');
  298. do_action('admin_print_scripts');
  299. do_action('admin_head-media-upload-popup');
  300. do_action('admin_head');
  301. if ( is_string($content_func) )
  302. do_action( "admin_head_{$content_func}" );
  303. ?>
  304. </head>
  305. <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?> class="wp-core-ui no-js">
  306. <script type="text/javascript">
  307. document.body.className = document.body.className.replace('no-js', 'js');
  308. </script>
  309. <?php
  310. $args = func_get_args();
  311. $args = array_slice($args, 1);
  312. call_user_func_array($content_func, $args);
  313. do_action('admin_print_footer_scripts');
  314. ?>
  315. <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
  316. </body>
  317. </html>
  318. <?php
  319. }
  320. /**
  321. * Adds the media button to the editor
  322. *
  323. * @since 2.5.0
  324. *
  325. * @param string $editor_id
  326. */
  327. function media_buttons($editor_id = 'content') {
  328. $post = get_post();
  329. if ( ! $post && ! empty( $GLOBALS['post_ID'] ) )
  330. $post = $GLOBALS['post_ID'];
  331. wp_enqueue_media( array(
  332. 'post' => $post
  333. ) );
  334. $img = '<span class="wp-media-buttons-icon"></span> ';
  335. echo '<a href="#" id="insert-media-button" class="button insert-media add_media" data-editor="' . esc_attr( $editor_id ) . '" title="' . esc_attr__( 'Add Media' ) . '">' . $img . __( 'Add Media' ) . '</a>';
  336. // Don't use this filter. Want to add a button? Use the media_buttons action.
  337. $legacy_filter = apply_filters('media_buttons_context', ''); // deprecated
  338. if ( $legacy_filter ) {
  339. // #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
  340. if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) )
  341. $legacy_filter .= '</a>';
  342. echo $legacy_filter;
  343. }
  344. }
  345. add_action( 'media_buttons', 'media_buttons' );
  346. function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
  347. global $post_ID;
  348. if ( empty( $post_id ) )
  349. $post_id = $post_ID;
  350. $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
  351. if ( $type && 'media' != $type )
  352. $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
  353. if ( ! empty( $tab ) )
  354. $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
  355. $upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
  356. return add_query_arg('TB_iframe', true, $upload_iframe_src);
  357. }
  358. /**
  359. * {@internal Missing Short Description}}
  360. *
  361. * @since 2.5.0
  362. *
  363. * @return mixed void|object WP_Error on failure
  364. */
  365. function media_upload_form_handler() {
  366. check_admin_referer('media-form');
  367. $errors = null;
  368. if ( isset($_POST['send']) ) {
  369. $keys = array_keys($_POST['send']);
  370. $send_id = (int) array_shift($keys);
  371. }
  372. if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
  373. $post = $_post = get_post($attachment_id, ARRAY_A);
  374. $post_type_object = get_post_type_object( $post[ 'post_type' ] );
  375. if ( !current_user_can( $post_type_object->cap->edit_post, $attachment_id ) )
  376. continue;
  377. if ( isset($attachment['post_content']) )
  378. $post['post_content'] = $attachment['post_content'];
  379. if ( isset($attachment['post_title']) )
  380. $post['post_title'] = $attachment['post_title'];
  381. if ( isset($attachment['post_excerpt']) )
  382. $post['post_excerpt'] = $attachment['post_excerpt'];
  383. if ( isset($attachment['menu_order']) )
  384. $post['menu_order'] = $attachment['menu_order'];
  385. if ( isset($send_id) && $attachment_id == $send_id ) {
  386. if ( isset($attachment['post_parent']) )
  387. $post['post_parent'] = $attachment['post_parent'];
  388. }
  389. $post = apply_filters('attachment_fields_to_save', $post, $attachment);
  390. if ( isset($attachment['image_alt']) ) {
  391. $image_alt = wp_unslash( $attachment['image_alt'] );
  392. if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) {
  393. $image_alt = wp_strip_all_tags( $image_alt, true );
  394. // update_meta expects slashed
  395. update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
  396. }
  397. }
  398. if ( isset($post['errors']) ) {
  399. $errors[$attachment_id] = $post['errors'];
  400. unset($post['errors']);
  401. }
  402. if ( $post != $_post )
  403. wp_update_post($post);
  404. foreach ( get_attachment_taxonomies($post) as $t ) {
  405. if ( isset($attachment[$t]) )
  406. wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
  407. }
  408. }
  409. if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
  410. <script type="text/javascript">
  411. /* <![CDATA[ */
  412. var win = window.dialogArguments || opener || parent || top;
  413. win.tb_remove();
  414. /* ]]> */
  415. </script>
  416. <?php
  417. exit;
  418. }
  419. if ( isset($send_id) ) {
  420. $attachment = wp_unslash( $_POST['attachments'][$send_id] );
  421. $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
  422. if ( !empty($attachment['url']) ) {
  423. $rel = '';
  424. if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
  425. $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
  426. $html = "<a href='{$attachment['url']}'$rel>$html</a>";
  427. }
  428. $html = apply_filters('media_send_to_editor', $html, $send_id, $attachment);
  429. return media_send_to_editor($html);
  430. }
  431. return $errors;
  432. }
  433. /**
  434. * {@internal Missing Short Description}}
  435. *
  436. * @since 2.5.0
  437. *
  438. * @return mixed
  439. */
  440. function wp_media_upload_handler() {
  441. $errors = array();
  442. $id = 0;
  443. if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
  444. check_admin_referer('media-form');
  445. // Upload File button was clicked
  446. $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
  447. unset($_FILES);
  448. if ( is_wp_error($id) ) {
  449. $errors['upload_error'] = $id;
  450. $id = false;
  451. }
  452. }
  453. if ( !empty($_POST['insertonlybutton']) ) {
  454. $src = $_POST['src'];
  455. if ( !empty($src) && !strpos($src, '://') )
  456. $src = "http://$src";
  457. if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
  458. $title = esc_html( wp_unslash( $_POST['title'] ) );
  459. if ( empty( $title ) )
  460. $title = esc_html( basename( $src ) );
  461. if ( $title && $src )
  462. $html = "<a href='" . esc_url($src) . "'>$title</a>";
  463. $type = 'file';
  464. if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
  465. && ( 'audio' == $ext_type || 'video' == $ext_type ) )
  466. $type = $ext_type;
  467. $html = apply_filters( $type . '_send_to_editor_url', $html, esc_url_raw( $src ), $title );
  468. } else {
  469. $align = '';
  470. $alt = esc_attr( wp_unslash( $_POST['alt'] ) );
  471. if ( isset($_POST['align']) ) {
  472. $align = esc_attr( wp_unslash( $_POST['align'] ) );
  473. $class = " class='align$align'";
  474. }
  475. if ( !empty($src) )
  476. $html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";
  477. $html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
  478. }
  479. return media_send_to_editor($html);
  480. }
  481. if ( !empty($_POST) ) {
  482. $return = media_upload_form_handler();
  483. if ( is_string($return) )
  484. return $return;
  485. if ( is_array($return) )
  486. $errors = $return;
  487. }
  488. if ( isset($_POST['save']) ) {
  489. $errors['upload_notice'] = __('Saved.');
  490. return media_upload_gallery();
  491. }
  492. if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
  493. $type = 'image';
  494. if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
  495. $type = $_GET['type'];
  496. return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
  497. }
  498. return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
  499. }
  500. /**
  501. * Download an image from the specified URL and attach it to a post.
  502. *
  503. * @since 2.6.0
  504. *
  505. * @param string $file The URL of the image to download
  506. * @param int $post_id The post ID the media is to be associated with
  507. * @param string $desc Optional. Description of the image
  508. * @return string|WP_Error Populated HTML img tag on success
  509. */
  510. function media_sideload_image($file, $post_id, $desc = null) {
  511. if ( ! empty($file) ) {
  512. // Download file to temp location
  513. $tmp = download_url( $file );
  514. // Set variables for storage
  515. // fix file filename for query strings
  516. preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
  517. $file_array['name'] = basename($matches[0]);
  518. $file_array['tmp_name'] = $tmp;
  519. // If error storing temporarily, unlink
  520. if ( is_wp_error( $tmp ) ) {
  521. @unlink($file_array['tmp_name']);
  522. $file_array['tmp_name'] = '';
  523. }
  524. // do the validation and storage stuff
  525. $id = media_handle_sideload( $file_array, $post_id, $desc );
  526. // If error storing permanently, unlink
  527. if ( is_wp_error($id) ) {
  528. @unlink($file_array['tmp_name']);
  529. return $id;
  530. }
  531. $src = wp_get_attachment_url( $id );
  532. }
  533. // Finally check to make sure the file has been saved, then return the html
  534. if ( ! empty($src) ) {
  535. $alt = isset($desc) ? esc_attr($desc) : '';
  536. $html = "<img src='$src' alt='$alt' />";
  537. return $html;
  538. }
  539. }
  540. /**
  541. * {@internal Missing Short Description}}
  542. *
  543. * @since 2.5.0
  544. *
  545. * @return unknown
  546. */
  547. function media_upload_gallery() {
  548. $errors = array();
  549. if ( !empty($_POST) ) {
  550. $return = media_upload_form_handler();
  551. if ( is_string($return) )
  552. return $return;
  553. if ( is_array($return) )
  554. $errors = $return;
  555. }
  556. wp_enqueue_script('admin-gallery');
  557. return wp_iframe( 'media_upload_gallery_form', $errors );
  558. }
  559. /**
  560. * {@internal Missing Short Description}}
  561. *
  562. * @since 2.5.0
  563. *
  564. * @return unknown
  565. */
  566. function media_upload_library() {
  567. $errors = array();
  568. if ( !empty($_POST) ) {
  569. $return = media_upload_form_handler();
  570. if ( is_string($return) )
  571. return $return;
  572. if ( is_array($return) )
  573. $errors = $return;
  574. }
  575. return wp_iframe( 'media_upload_library_form', $errors );
  576. }
  577. /**
  578. * Retrieve HTML for the image alignment radio buttons with the specified one checked.
  579. *
  580. * @since 2.7.0
  581. *
  582. * @param object $post
  583. * @param string $checked
  584. * @return string
  585. */
  586. function image_align_input_fields( $post, $checked = '' ) {
  587. if ( empty($checked) )
  588. $checked = get_user_setting('align', 'none');
  589. $alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
  590. if ( !array_key_exists( (string) $checked, $alignments ) )
  591. $checked = 'none';
  592. $out = array();
  593. foreach ( $alignments as $name => $label ) {
  594. $name = esc_attr($name);
  595. $out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
  596. ( $checked == $name ? " checked='checked'" : "" ) .
  597. " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
  598. }
  599. return join("\n", $out);
  600. }
  601. /**
  602. * Retrieve HTML for the size radio buttons with the specified one checked.
  603. *
  604. * @since 2.7.0
  605. *
  606. * @param object $post
  607. * @param bool|string $check
  608. * @return array
  609. */
  610. function image_size_input_fields( $post, $check = '' ) {
  611. // get a list of the actual pixel dimensions of each possible intermediate version of this image
  612. $size_names = apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) );
  613. if ( empty($check) )
  614. $check = get_user_setting('imgsize', 'medium');
  615. foreach ( $size_names as $size => $label ) {
  616. $downsize = image_downsize($post->ID, $size);
  617. $checked = '';
  618. // is this size selectable?
  619. $enabled = ( $downsize[3] || 'full' == $size );
  620. $css_id = "image-size-{$size}-{$post->ID}";
  621. // if this size is the default but that's not available, don't select it
  622. if ( $size == $check ) {
  623. if ( $enabled )
  624. $checked = " checked='checked'";
  625. else
  626. $check = '';
  627. } elseif ( !$check && $enabled && 'thumbnail' != $size ) {
  628. // if $check is not enabled, default to the first available size that's bigger than a thumbnail
  629. $check = $size;
  630. $checked = " checked='checked'";
  631. }
  632. $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 />";
  633. $html .= "<label for='{$css_id}'>$label</label>";
  634. // only show the dimensions if that choice is available
  635. if ( $enabled )
  636. $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
  637. $html .= '</div>';
  638. $out[] = $html;
  639. }
  640. return array(
  641. 'label' => __('Size'),
  642. 'input' => 'html',
  643. 'html' => join("\n", $out),
  644. );
  645. }
  646. /**
  647. * Retrieve HTML for the Link URL buttons with the default link type as specified.
  648. *
  649. * @since 2.7.0
  650. *
  651. * @param object $post
  652. * @param string $url_type
  653. * @return string
  654. */
  655. function image_link_input_fields($post, $url_type = '') {
  656. $file = wp_get_attachment_url($post->ID);
  657. $link = get_attachment_link($post->ID);
  658. if ( empty($url_type) )
  659. $url_type = get_user_setting('urlbutton', 'post');
  660. $url = '';
  661. if ( $url_type == 'file' )
  662. $url = $file;
  663. elseif ( $url_type == 'post' )
  664. $url = $link;
  665. return "
  666. <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
  667. <button type='button' class='button urlnone' data-link-url=''>" . __('None') . "</button>
  668. <button type='button' class='button urlfile' data-link-url='" . esc_attr($file) . "'>" . __('File URL') . "</button>
  669. <button type='button' class='button urlpost' data-link-url='" . esc_attr($link) . "'>" . __('Attachment Post URL') . "</button>
  670. ";
  671. }
  672. function wp_caption_input_textarea($edit_post) {
  673. // post data is already escaped
  674. $name = "attachments[{$edit_post->ID}][post_excerpt]";
  675. return '<textarea name="' . $name . '" id="' . $name . '">' . $edit_post->post_excerpt . '</textarea>';
  676. }
  677. /**
  678. * {@internal Missing Short Description}}
  679. *
  680. * @since 2.5.0
  681. *
  682. * @param array $form_fields
  683. * @param object $post
  684. * @return array
  685. */
  686. function image_attachment_fields_to_edit($form_fields, $post) {
  687. return $form_fields;
  688. }
  689. /**
  690. * {@internal Missing Short Description}}
  691. *
  692. * @since 2.5.0
  693. *
  694. * @param array $form_fields
  695. * @param object $post {@internal $post not used}}
  696. * @return array
  697. */
  698. function media_single_attachment_fields_to_edit( $form_fields, $post ) {
  699. unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
  700. return $form_fields;
  701. }
  702. /**
  703. * {@internal Missing Short Description}}
  704. *
  705. * @since 2.8.0
  706. *
  707. * @param array $form_fields
  708. * @param object $post {@internal $post not used}}
  709. * @return array
  710. */
  711. function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
  712. unset($form_fields['image_url']);
  713. return $form_fields;
  714. }
  715. /**
  716. * Filters input from media_upload_form_handler() and assigns a default
  717. * post_title from the file name if none supplied.
  718. *
  719. * Illustrates the use of the attachment_fields_to_save filter
  720. * which can be used to add default values to any field before saving to DB.
  721. *
  722. * @since 2.5.0
  723. *
  724. * @param object $post
  725. * @param array $attachment {@internal $attachment not used}}
  726. * @return array
  727. */
  728. function image_attachment_fields_to_save($post, $attachment) {
  729. if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
  730. if ( strlen(trim($post['post_title'])) == 0 ) {
  731. $post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid']));
  732. $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
  733. }
  734. }
  735. return $post;
  736. }
  737. add_filter('attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2);
  738. /**
  739. * {@internal Missing Short Description}}
  740. *
  741. * @since 2.5.0
  742. *
  743. * @param string $html
  744. * @param integer $attachment_id
  745. * @param array $attachment
  746. * @return array
  747. */
  748. function image_media_send_to_editor($html, $attachment_id, $attachment) {
  749. $post = get_post($attachment_id);
  750. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  751. $url = $attachment['url'];
  752. $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
  753. $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
  754. $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
  755. $rel = ( $url == get_attachment_link($attachment_id) );
  756. return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
  757. }
  758. return $html;
  759. }
  760. add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
  761. /**
  762. * {@internal Missing Short Description}}
  763. *
  764. * @since 2.5.0
  765. *
  766. * @param object $post
  767. * @param array $errors
  768. * @return array
  769. */
  770. function get_attachment_fields_to_edit($post, $errors = null) {
  771. if ( is_int($post) )
  772. $post = get_post($post);
  773. if ( is_array($post) )
  774. $post = new WP_Post( (object) $post );
  775. $image_url = wp_get_attachment_url($post->ID);
  776. $edit_post = sanitize_post($post, 'edit');
  777. $form_fields = array(
  778. 'post_title' => array(
  779. 'label' => __('Title'),
  780. 'value' => $edit_post->post_title
  781. ),
  782. 'image_alt' => array(),
  783. 'post_excerpt' => array(
  784. 'label' => __('Caption'),
  785. 'input' => 'html',
  786. 'html' => wp_caption_input_textarea($edit_post)
  787. ),
  788. 'post_content' => array(
  789. 'label' => __('Description'),
  790. 'value' => $edit_post->post_content,
  791. 'input' => 'textarea'
  792. ),
  793. 'url' => array(
  794. 'label' => __('Link URL'),
  795. 'input' => 'html',
  796. 'html' => image_link_input_fields($post, get_option('image_default_link_type')),
  797. 'helps' => __('Enter a link URL or click above for presets.')
  798. ),
  799. 'menu_order' => array(
  800. 'label' => __('Order'),
  801. 'value' => $edit_post->menu_order
  802. ),
  803. 'image_url' => array(
  804. 'label' => __('File URL'),
  805. 'input' => 'html',
  806. 'html' => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
  807. 'value' => wp_get_attachment_url($post->ID),
  808. 'helps' => __('Location of the uploaded file.')
  809. )
  810. );
  811. foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  812. $t = (array) get_taxonomy($taxonomy);
  813. if ( ! $t['public'] || ! $t['show_ui'] )
  814. continue;
  815. if ( empty($t['label']) )
  816. $t['label'] = $taxonomy;
  817. if ( empty($t['args']) )
  818. $t['args'] = array();
  819. $terms = get_object_term_cache($post->ID, $taxonomy);
  820. if ( false === $terms )
  821. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  822. $values = array();
  823. foreach ( $terms as $term )
  824. $values[] = $term->slug;
  825. $t['value'] = join(', ', $values);
  826. $form_fields[$taxonomy] = $t;
  827. }
  828. // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  829. // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
  830. $form_fields = array_merge_recursive($form_fields, (array) $errors);
  831. // This was formerly in image_attachment_fields_to_edit().
  832. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  833. $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
  834. if ( empty($alt) )
  835. $alt = '';
  836. $form_fields['post_title']['required'] = true;
  837. $form_fields['image_alt'] = array(
  838. 'value' => $alt,
  839. 'label' => __('Alternative Text'),
  840. 'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
  841. );
  842. $form_fields['align'] = array(
  843. 'label' => __('Alignment'),
  844. 'input' => 'html',
  845. 'html' => image_align_input_fields($post, get_option('image_default_align')),
  846. );
  847. $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
  848. } else {
  849. unset( $form_fields['image_alt'] );
  850. }
  851. $form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post);
  852. return $form_fields;
  853. }
  854. /**
  855. * Retrieve HTML for media items of post gallery.
  856. *
  857. * The HTML markup retrieved will be created for the progress of SWF Upload
  858. * component. Will also create link for showing and hiding the form to modify
  859. * the image attachment.
  860. *
  861. * @since 2.5.0
  862. *
  863. * @param int $post_id Optional. Post ID.
  864. * @param array $errors Errors for attachment, if any.
  865. * @return string
  866. */
  867. function get_media_items( $post_id, $errors ) {
  868. $attachments = array();
  869. if ( $post_id ) {
  870. $post = get_post($post_id);
  871. if ( $post && $post->post_type == 'attachment' )
  872. $attachments = array($post->ID => $post);
  873. else
  874. $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
  875. } else {
  876. if ( is_array($GLOBALS['wp_the_query']->posts) )
  877. foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
  878. $attachments[$attachment->ID] = $attachment;
  879. }
  880. $output = '';
  881. foreach ( (array) $attachments as $id => $attachment ) {
  882. if ( $attachment->post_status == 'trash' )
  883. continue;
  884. if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
  885. $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>";
  886. }
  887. return $output;
  888. }
  889. /**
  890. * Retrieve HTML form for modifying the image attachment.
  891. *
  892. * @since 2.5.0
  893. *
  894. * @param int $attachment_id Attachment ID for modification.
  895. * @param string|array $args Optional. Override defaults.
  896. * @return string HTML form for attachment.
  897. */
  898. function get_media_item( $attachment_id, $args = null ) {
  899. global $redir_tab;
  900. if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
  901. $thumb_url = $thumb_url[0];
  902. else
  903. $thumb_url = false;
  904. $post = get_post( $attachment_id );
  905. $current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
  906. $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 );
  907. $args = wp_parse_args( $args, $default_args );
  908. $args = apply_filters( 'get_media_item_args', $args );
  909. extract( $args, EXTR_SKIP );
  910. $toggle_on = __( 'Show' );
  911. $toggle_off = __( 'Hide' );
  912. $filename = esc_html( wp_basename( $post->guid ) );
  913. $title = esc_attr( $post->post_title );
  914. if ( $_tags = get_the_tags( $attachment_id ) ) {
  915. foreach ( $_tags as $tag )
  916. $tags[] = $tag->name;
  917. $tags = esc_attr( join( ', ', $tags ) );
  918. }
  919. $post_mime_types = get_post_mime_types();
  920. $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
  921. $type = array_shift( $keys );
  922. $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
  923. $form_fields = get_attachment_fields_to_edit( $post, $errors );
  924. if ( $toggle ) {
  925. $class = empty( $errors ) ? 'startclosed' : 'startopen';
  926. $toggle_links = "
  927. <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
  928. <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
  929. } else {
  930. $class = '';
  931. $toggle_links = '';
  932. }
  933. $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
  934. $display_title = $show_title ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60 ) . "</span></div>" : '';
  935. $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
  936. $order = '';
  937. foreach ( $form_fields as $key => $val ) {
  938. if ( 'menu_order' == $key ) {
  939. if ( $gallery )
  940. $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>";
  941. else
  942. $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
  943. unset( $form_fields['menu_order'] );
  944. break;
  945. }
  946. }
  947. $media_dims = '';
  948. $meta = wp_get_attachment_metadata( $post->ID );
  949. if ( is_array( $meta ) && array_key_exists( 'width', $meta ) && array_key_exists( 'height', $meta ) )
  950. $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  951. $media_dims = apply_filters( 'media_meta', $media_dims, $post );
  952. $image_edit_button = '';
  953. if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  954. $nonce = wp_create_nonce( "image_editor-$post->ID" );
  955. $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>";
  956. }
  957. $attachment_url = get_permalink( $attachment_id );
  958. $item = "
  959. $type_html
  960. $toggle_links
  961. $order
  962. $display_title
  963. <table class='slidetoggle describe $class'>
  964. <thead class='media-item-info' id='media-head-$post->ID'>
  965. <tr valign='top'>
  966. <td class='A1B1' id='thumbnail-head-$post->ID'>
  967. <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
  968. <p>$image_edit_button</p>
  969. </td>
  970. <td>
  971. <p><strong>" . __('File name:') . "</strong> $filename</p>
  972. <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
  973. <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( get_option('date_format'), $post->post_date ). '</p>';
  974. if ( !empty( $media_dims ) )
  975. $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
  976. $item .= "</td></tr>\n";
  977. $item .= "
  978. </thead>
  979. <tbody>
  980. <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>
  981. <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n";
  982. $defaults = array(
  983. 'input' => 'text',
  984. 'required' => false,
  985. 'value' => '',
  986. 'extra_rows' => array(),
  987. );
  988. if ( $send )
  989. $send = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
  990. if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
  991. if ( !EMPTY_TRASH_DAYS ) {
  992. $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>';
  993. } elseif ( !MEDIA_TRASH ) {
  994. $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
  995. <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'><p>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' ), $filename ) . "</p>
  996. <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>
  997. <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
  998. </div>";
  999. } else {
  1000. $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>
  1001. <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>";
  1002. }
  1003. } else {
  1004. $delete = '';
  1005. }
  1006. $thumbnail = '';
  1007. $calling_post_id = 0;
  1008. if ( isset( $_GET['post_id'] ) )
  1009. $calling_post_id = absint( $_GET['post_id'] );
  1010. elseif ( isset( $_POST ) && count( $_POST ) ) // Like for async-upload where $_GET['post_id'] isn't set
  1011. $calling_post_id = $post->post_parent;
  1012. if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
  1013. && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
  1014. $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
  1015. $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>";
  1016. }
  1017. if ( ( $send || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) )
  1018. $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send $thumbnail $delete</td></tr>\n" );
  1019. $hidden_fields = array();
  1020. foreach ( $form_fields as $id => $field ) {
  1021. if ( $id[0] == '_' )
  1022. continue;
  1023. if ( !empty( $field['tr'] ) ) {
  1024. $item .= $field['tr'];
  1025. continue;
  1026. }
  1027. $field = array_merge( $defaults, $field );
  1028. $name = "attachments[$attachment_id][$id]";
  1029. if ( $field['input'] == 'hidden' ) {
  1030. $hidden_fields[$name] = $field['value'];
  1031. continue;
  1032. }
  1033. $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
  1034. $aria_required = $field['required'] ? " aria-required='true' " : '';
  1035. $class = $id;
  1036. $class .= $field['required'] ? ' form-required' : '';
  1037. $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'>";
  1038. if ( !empty( $field[ $field['input'] ] ) )
  1039. $item .= $field[ $field['input'] ];
  1040. elseif ( $field['input'] == 'textarea' ) {
  1041. if ( 'post_content' == $id && user_can_richedit() ) {
  1042. // sanitize_post() skips the post_content when user_can_richedit
  1043. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1044. }
  1045. // post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit()
  1046. $item .= "<textarea id='$name' name='$name' $aria_required>" . $field['value'] . '</textarea>';
  1047. } else {
  1048. $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
  1049. }
  1050. if ( !empty( $field['helps'] ) )
  1051. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1052. $item .= "</td>\n\t\t</tr>\n";
  1053. $extra_rows = array();
  1054. if ( !empty( $field['errors'] ) )
  1055. foreach ( array_unique( (array) $field['errors'] ) as $error )
  1056. $extra_rows['error'][] = $error;
  1057. if ( !empty( $field['extra_rows'] ) )
  1058. foreach ( $field['extra_rows'] as $class => $rows )
  1059. foreach ( (array) $rows as $html )
  1060. $extra_rows[$class][] = $html;
  1061. foreach ( $extra_rows as $class => $rows )
  1062. foreach ( $rows as $html )
  1063. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1064. }
  1065. if ( !empty( $form_fields['_final'] ) )
  1066. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1067. $item .= "\t</tbody>\n";
  1068. $item .= "\t</table>\n";
  1069. foreach ( $hidden_fields as $name => $value )
  1070. $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
  1071. if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
  1072. $parent = (int) $_REQUEST['post_id'];
  1073. $parent_name = "attachments[$attachment_id][post_parent]";
  1074. $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
  1075. }
  1076. return $item;
  1077. }
  1078. function get_compat_media_markup( $attachment_id, $args = null ) {
  1079. $post = get_post( $attachment_id );
  1080. $default_args = array(
  1081. 'errors' => null,
  1082. 'in_modal' => false,
  1083. );
  1084. $user_can_edit = current_user_can( 'edit_post', $attachment_id );
  1085. $args = wp_parse_args( $args, $default_args );
  1086. $args = apply_filters( 'get_media_item_args', $args );
  1087. $form_fields = array();
  1088. if ( $args['in_modal'] ) {
  1089. foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  1090. $t = (array) get_taxonomy($taxonomy);
  1091. if ( ! $t['public'] || ! $t['show_ui'] )
  1092. continue;
  1093. if ( empty($t['label']) )
  1094. $t['label'] = $taxonomy;
  1095. if ( empty($t['args']) )
  1096. $t['args'] = array();
  1097. $terms = get_object_term_cache($post->ID, $taxonomy);
  1098. if ( false === $terms )
  1099. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  1100. $values = array();
  1101. foreach ( $terms as $term )
  1102. $values[] = $term->slug;
  1103. $t['value'] = join(', ', $values);
  1104. $t['taxonomy'] = true;
  1105. $form_fields[$taxonomy] = $t;
  1106. }
  1107. }
  1108. // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  1109. // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
  1110. $form_fields = array_merge_recursive($form_fields, (array) $args['errors'] );
  1111. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1112. unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],
  1113. $form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],
  1114. $form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] );
  1115. $media_meta = apply_filters( 'media_meta', '', $post );
  1116. $defaults = array(
  1117. 'input' => 'text',
  1118. 'required' => false,
  1119. 'value' => '',
  1120. 'extra_rows' => array(),
  1121. 'show_in_edit' => true,
  1122. 'show_in_modal' => true,
  1123. );
  1124. $hidden_fields = array();
  1125. $item = '';
  1126. foreach ( $form_fields as $id => $field ) {
  1127. if ( $id[0] == '_' )
  1128. continue;
  1129. $name = "attachments[$attachment_id][$id]";
  1130. $id_attr = "attachments-$attachment_id-$id";
  1131. if ( !empty( $field['tr'] ) ) {
  1132. $item .= $field['tr'];
  1133. continue;
  1134. }
  1135. $field = array_merge( $defaults, $field );
  1136. if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) )
  1137. continue;
  1138. if ( $field['input'] == 'hidden' ) {
  1139. $hidden_fields[$name] = $field['value'];
  1140. continue;
  1141. }
  1142. $readonly = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
  1143. $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
  1144. $aria_required = $field['required'] ? " aria-required='true' " : '';
  1145. $class = 'compat-field-' . $id;
  1146. $class .= $field['required'] ? ' form-required' : '';
  1147. $item .= "\t\t<tr class='$class'>";
  1148. $item .= "\t\t\t<th valign='top' scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
  1149. $item .= "</th>\n\t\t\t<td class='field'>";
  1150. if ( !empty( $field[ $field['input'] ] ) )
  1151. $item .= $field[ $field['input'] ];
  1152. elseif ( $field['input'] == 'textarea' ) {
  1153. if ( 'post_content' == $id && user_can_richedit() ) {
  1154. // sanitize_post() skips the post_content when user_can_richedit
  1155. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1156. }
  1157. $item .= "<textarea id='$id_attr' name='$name' $aria_required>" . $field['value'] . '</textarea>';
  1158. } else {
  1159. $item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly $aria_required />";
  1160. }
  1161. if ( !empty( $field['helps'] ) )
  1162. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1163. $item .= "</td>\n\t\t</tr>\n";
  1164. $extra_rows = array();
  1165. if ( !empty( $field['errors'] ) )
  1166. foreach ( array_unique( (array) $field['errors'] ) as $error )
  1167. $extra_rows['error'][] = $error;
  1168. if ( !empty( $field['extra_rows'] ) )
  1169. foreach ( $field['extra_rows'] as $class => $rows )
  1170. foreach ( (array) $rows as $html )
  1171. $extra_rows[$class][] = $html;
  1172. foreach ( $extra_rows as $class => $rows )
  1173. foreach ( $rows as $html )
  1174. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1175. }
  1176. if ( !empty( $form_fields['_final'] ) )
  1177. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1178. if ( $item )
  1179. $item = '<table class="compat-attachment-fields">' . $item . '</table>';
  1180. foreach ( $hidden_fields as $hidden_field => $value ) {
  1181. $item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n";
  1182. }
  1183. if ( $item )
  1184. $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr( $post->menu_order ) . '" />' . $item;
  1185. return array(
  1186. 'item' => $item,
  1187. 'meta' => $media_meta,
  1188. );
  1189. }
  1190. /**
  1191. * {@internal Missing Short Description}}
  1192. *
  1193. * @since 2.5.0
  1194. */
  1195. function media_upload_header() {
  1196. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1197. echo '<script type="text/javascript">post_id = ' . $post_id . ";</script>\n";
  1198. if ( empty( $_GET['chromeless'] ) ) {
  1199. echo '<div id="media-upload-header">';
  1200. the_media_upload_tabs();
  1201. echo '</div>';
  1202. }
  1203. }
  1204. /**
  1205. * {@internal Missing Short Description}}
  1206. *
  1207. * @since 2.5.0
  1208. *
  1209. * @param unknown_type $errors
  1210. */
  1211. function media_upload_form( $errors = null ) {
  1212. global $type, $tab, $pagenow, $is_IE, $is_opera;
  1213. if ( ! _device_can_upload() ) {
  1214. 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>';
  1215. return;
  1216. }
  1217. $upload_action_url = admin_url('async-upload.php');
  1218. $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
  1219. $_type = isset($type) ? $type : '';
  1220. $_tab = isset($tab) ? $tab : '';
  1221. $upload_size_unit = $max_upload_size = wp_max_upload_size();
  1222. $sizes = array( 'KB', 'MB', 'GB' );
  1223. for ( $u = -1; $upload_size_unit > 1024 && $u < count( $sizes ) - 1; $u++ ) {
  1224. $upload_size_unit /= 1024;
  1225. }
  1226. if ( $u < 0 ) {
  1227. $upload_size_unit = 0;
  1228. $u = 0;
  1229. } else {
  1230. $upload_size_unit = (int) $upload_size_unit;
  1231. }
  1232. ?>
  1233. <div id="media-upload-notice"><?php
  1234. if (isset($errors['upload_notice']) )
  1235. echo $errors['upload_notice'];
  1236. ?></div>
  1237. <div id="media-upload-error"><?php
  1238. if (isset($errors['upload_error']) && is_wp_error($errors['upload_error']))
  1239. echo $errors['upload_error']->get_error_message();
  1240. ?></div>
  1241. <?php
  1242. if ( is_multisite() && !is_upload_space_available() ) {
  1243. do_action( 'upload_ui_over_quota' );
  1244. return;
  1245. }
  1246. do_action('pre-upload-ui');
  1247. $post_params = array(
  1248. "post_id" => $post_id,
  1249. "_wpnonce" => wp_create_nonce('media-form'),
  1250. "type" => $_type,
  1251. "tab" => $_tab,
  1252. "short" => "1",
  1253. );
  1254. $post_params = apply_filters( 'upload_post_params', $post_params ); // hook change! old name: 'swfupload_post_params'
  1255. $plupload_init = array(
  1256. 'runtimes' => 'html5,silverlight,flash,html4',
  1257. 'browse_button' => 'plupload-browse-button',
  1258. 'container' => 'plupload-upload-ui',
  1259. 'drop_element' => 'drag-drop-area',
  1260. 'file_data_name' => 'async-upload',
  1261. 'multiple_queues' => true,
  1262. 'max_file_size' => $max_upload_size . 'b',
  1263. 'url' => $upload_action_url,
  1264. 'flash_swf_url' => includes_url('js/plupload/plupload.flash.swf'),
  1265. 'silverlight_xap_url' => includes_url('js/plupload/plupload.silverlight.xap'),
  1266. 'filters' => array( array('title' => __( 'Allowed Files' ), 'extensions' => '*') ),
  1267. 'multipart' => true,
  1268. 'urlstream_upload' => true,
  1269. 'multipart_params' => $post_params
  1270. );
  1271. // Multi-file uploading doesn't currently work in iOS Safari,
  1272. // single-file allows the built-in camera to be used as source for images
  1273. if ( wp_is_mobile() )
  1274. $plupload_init['multi_selection'] = false;
  1275. $plupload_init = apply_filters( 'plupload_init', $plupload_init );
  1276. ?>
  1277. <script type="text/javascript">
  1278. <?php
  1279. // Verify size is an int. If not return default value.
  1280. $large_size_h = absint( get_option('large_size_h') );
  1281. if( !$large_size_h )
  1282. $large_size_h = 1024;
  1283. $large_size_w = absint( get_option('large_size_w') );
  1284. if( !$large_size_w )
  1285. $large_size_w = 1024;
  1286. ?>
  1287. var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
  1288. wpUploaderInit = <?php echo json_encode($plupload_init); ?>;
  1289. </script>
  1290. <div id="plupload-upload-ui" class="hide-if-no-js">
  1291. <?php do_action('pre-plupload-upload-ui'); // hook change, old name: 'pre-flash-upload-ui' ?>
  1292. <div id="drag-drop-area">
  1293. <div class="drag-drop-inside">
  1294. <p class="drag-drop-info"><?php _e('Drop files here'); ?></p>
  1295. <p><?php _ex('or', 'Uploader: Drop files here - or - Select Files'); ?></p>
  1296. <p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e('Select Files'); ?>" class="button" /></p>
  1297. </div>
  1298. </div>
  1299. <?php do_action('post-plupload-upload-ui'); // hook change, old name: 'post-flash-upload-ui' ?>
  1300. </div>
  1301. <div id="html-upload-ui" class="hide-if-js">
  1302. <?php do_action('pre-html-upload-ui'); ?>
  1303. <p id="async-upload-wrap">
  1304. <label class="screen-reader-text" for="async-upload"><?php _e('Upload'); ?></label>
  1305. <input type="file" name="async-upload" id="async-upload" />
  1306. <?php submit_button( __( 'Upload' ), 'button', 'html-upload', false ); ?>
  1307. <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e('Cancel'); ?></a>
  1308. </p>
  1309. <div class="clear"></div>
  1310. <?php do_action('post-html-upload-ui'); ?>
  1311. </div>
  1312. <span class="max-upload-size"><?php printf( __( 'Maximum upload file size: %d%s.' ), esc_html($upload_size_unit), esc_html($sizes[$u]) ); ?></span>
  1313. <?php
  1314. if ( ($is_IE || $is_opera) && $max_upload_size > 100 * 1024 * 1024 ) { ?>
  1315. <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>
  1316. <?php }
  1317. do_action('post-upload-ui');
  1318. }
  1319. /**
  1320. * {@internal Missing Short Description}}
  1321. *
  1322. * @since 2.5.0
  1323. *
  1324. * @param string $type
  1325. * @param object $errors
  1326. * @param integer $id
  1327. */
  1328. function media_upload_type_form($type = 'file', $errors = null, $id = null) {
  1329. media_upload_header();
  1330. $post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
  1331. $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
  1332. $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
  1333. $form_class = 'media-upload-form type-form validate';
  1334. if ( get_user_setting('uploader') )
  1335. $form_class .= ' html-uploader';
  1336. ?>
  1337. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
  1338. <?php submit_button( '', 'hidden', 'save', false ); ?>
  1339. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1340. <?php wp_nonce_field('media-form'); ?>
  1341. <h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
  1342. <?php media_upload_form( $errors ); ?>
  1343. <script type="text/javascript">
  1344. //<![CDATA[
  1345. jQuery(function($){
  1346. var preloaded = $(".media-item.preloaded");
  1347. if ( preloaded.length > 0 ) {
  1348. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1349. }
  1350. updateMediaForm();
  1351. });
  1352. //]]>
  1353. </script>
  1354. <div id="media-items"><?php
  1355. if ( $id ) {
  1356. if ( !is_wp_error($id) ) {
  1357. add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
  1358. echo get_media_items( $id, $errors );
  1359. } else {
  1360. echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div></div>';
  1361. exit;
  1362. }
  1363. }
  1364. ?></div>
  1365. <p class="savebutton ml-submit">
  1366. <?php submit_button( __( 'Save all changes' ), 'button', 'save', false ); ?>
  1367. </p>
  1368. </form>
  1369. <?php
  1370. }
  1371. /**
  1372. * {@internal Missing Short Description}}
  1373. *
  1374. * @since 2.7.0
  1375. *
  1376. * @param string $type
  1377. * @param object $errors
  1378. * @param integer $id
  1379. */
  1380. function media_upload_type_url_form($type = null, $errors = null, $id = null) {
  1381. if ( null === $type )
  1382. $type = 'image';
  1383. media_upload_header();
  1384. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1385. $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
  1386. $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
  1387. $form_class = 'media-upload-form type-form validate';
  1388. if ( get_user_setting('uploader') )
  1389. $form_class .= ' html-uploader';
  1390. ?>
  1391. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
  1392. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1393. <?php wp_nonce_field('media-form'); ?>
  1394. <h3 class="media-title"><?php _e('Insert media from another website'); ?></h3>
  1395. <script type="text/javascript">
  1396. //<![CDATA[
  1397. var addExtImage = {
  1398. width : '',
  1399. height : '',
  1400. align : 'alignnone',
  1401. insert : function() {
  1402. var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
  1403. if ( '' == f.src.value || '' == t.width )
  1404. return false;
  1405. if ( f.alt.value )
  1406. alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1407. <?php if ( ! apply_filters( 'disable_captions', '' ) ) { ?>
  1408. if ( f.caption.value ) {
  1409. caption = f.caption.value.replace(/\r\n|\r/g, '\n');
  1410. caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
  1411. return a.replace(/[\r\n\t]+/, ' ');
  1412. });
  1413. caption = caption.replace(/\s*\n\s*/g, '<br />');
  1414. }
  1415. <?php } ?>
  1416. cls = caption ? '' : ' class="'+t.align+'"';
  1417. html = '<img alt="'+alt+'" src="'+f.src.value+'"'+cls+' width="'+t.width+'" height="'+t.height+'" />';
  1418. if ( f.url.value ) {
  1419. url = f.url.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1420. html = '<a href="'+url+'">'+html+'</a>';
  1421. }
  1422. if ( caption )
  1423. html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
  1424. var win = window.dialogArguments || opener || parent || top;
  1425. win.send_to_editor(html);
  1426. return false;
  1427. },
  1428. resetImageData : function() {
  1429. var t = addExtImage;
  1430. t.width = t.height = '';
  1431. document.getElementById('go_button').style.color = '#bbb';
  1432. if ( ! document.forms[0].src.value )
  1433. document.getElementById('status_img').innerHTML = '*';
  1434. else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
  1435. },
  1436. updateImageData : function() {
  1437. var t = addExtImage;
  1438. t.width = t.preloadImg.width;
  1439. t.height = t.preloadImg.height;
  1440. document.getElementById('go_button').style.color = '#333';
  1441. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
  1442. },
  1443. getImageData : function() {
  1444. if ( jQuery('table.describe').hasClass('not-image') )
  1445. return;
  1446. var t = addExtImage, src = document.forms[0].src.value;
  1447. if ( ! src ) {
  1448. t.resetImageData();
  1449. return false;
  1450. }
  1451. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" width="16" />';
  1452. t.preloadImg = new Image();
  1453. t.preloadImg.onload = t.updateImageData;
  1454. t.preloadImg.onerror = t.resetImageData;
  1455. t.preloadImg.src = src;
  1456. }
  1457. }
  1458. jQuery(document).ready( function($) {
  1459. $('.media-types input').click( function() {
  1460. $('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
  1461. });
  1462. });
  1463. //]]>
  1464. </script>
  1465. <div id="media-items">
  1466. <div class="media-item media-blank">
  1467. <?php echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) ); ?>
  1468. </div>
  1469. </div>
  1470. </form>
  1471. <?php
  1472. }
  1473. /**
  1474. * Adds gallery form to upload iframe
  1475. *
  1476. * @since 2.5.0
  1477. *
  1478. * @param array $errors
  1479. */
  1480. function media_upload_gallery_form($errors) {
  1481. global $redir_tab, $type;
  1482. $redir_tab = 'gallery';
  1483. media_upload_header();
  1484. $post_id = intval($_REQUEST['post_id']);
  1485. $form_action_url = admin_url("media-upload.php?type=$type&tab=gallery&post_id=$post_id");
  1486. $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
  1487. $form_class = 'media-upload-form validate';
  1488. if ( get_user_setting('uploader') )
  1489. $form_class .= ' html-uploader';
  1490. ?>
  1491. <script type="text/javascript">
  1492. <!--
  1493. jQuery(function($){
  1494. var preloaded = $(".media-item.preloaded");
  1495. if ( preloaded.length > 0 ) {
  1496. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1497. updateMediaForm();
  1498. }
  1499. });
  1500. -->
  1501. </script>
  1502. <div id="sort-buttons" class="hide-if-no-js">
  1503. <span>
  1504. <?php _e('All Tabs:'); ?>
  1505. <a href="#" id="showall"><?php _e('Show'); ?></a>
  1506. <a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
  1507. </span>
  1508. <?php _e('Sort Order:'); ?>
  1509. <a href="#" id="asc"><?php _e('Ascending'); ?></a> |
  1510. <a href="#" id="desc"><?php _e('Descending'); ?></a> |
  1511. <a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
  1512. </div>
  1513. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
  1514. <?php wp_nonce_field('media-form'); ?>
  1515. <?php //media_upload_form( $errors ); ?>
  1516. <table class="widefat" cellspacing="0">
  1517. <thead><tr>
  1518. <th><?php _e('Media'); ?></th>
  1519. <th class="order-head"><?php _e('Order'); ?></th>
  1520. <th class="actions-head"><?php _e('Actions'); ?></th>
  1521. </tr></thead>
  1522. </table>
  1523. <div id="media-items">
  1524. <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
  1525. <?php echo get_media_items($post_id, $errors); ?>
  1526. </div>
  1527. <p class="ml-submit">
  1528. <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
  1529. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1530. <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
  1531. <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
  1532. </p>
  1533. <div id="gallery-settings" style="display:none;">
  1534. <div class="title"><?php _e('Gallery Settings'); ?></div>
  1535. <table id="basic" class="describe"><tbody>
  1536. <tr>
  1537. <th scope="row" class="label">
  1538. <label>
  1539. <span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
  1540. </label>
  1541. </th>
  1542. <td class="field">
  1543. <input type="radio" name="linkto" id="linkto-file" value="file" />
  1544. <label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
  1545. <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
  1546. <label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
  1547. </td>
  1548. </tr>
  1549. <tr>
  1550. <th scope="row" class="label">
  1551. <label>
  1552. <span class="alignleft"><?php _e('Order images by:'); ?></span>
  1553. </label>
  1554. </th>
  1555. <td class="field">
  1556. <select id="orderby" name="orderby">
  1557. <option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
  1558. <option value="title"><?php _e('Title'); ?></option>
  1559. <option value="post_date"><?php _e('Date/Time'); ?></option>
  1560. <option value="rand"><?php _e('Random'); ?></option>
  1561. </select>
  1562. </td>
  1563. </tr>
  1564. <tr>
  1565. <th scope="row" class="label">
  1566. <label>
  1567. <span class="alignleft"><?php _e('Order:'); ?></span>
  1568. </label>
  1569. </th>
  1570. <td class="field">
  1571. <input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
  1572. <label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
  1573. <input type="radio" name="order" id="order-desc" value="desc" />
  1574. <label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
  1575. </td>
  1576. </tr>
  1577. <tr>
  1578. <th scope="row" class="label">
  1579. <label>
  1580. <span class="alignleft"><?php _e('Gallery columns:'); ?></span>
  1581. </label>
  1582. </th>
  1583. <td class="field">
  1584. <select id="columns" name="columns">
  1585. <option value="1">1</option>
  1586. <option value="2">2</option>
  1587. <option value="3" selected="selected">3</option>
  1588. <option value="4">4</option>
  1589. <option value="5">5</option>
  1590. <option value="6">6</option>
  1591. <option value="7">7</option>
  1592. <option value="8">8</option>
  1593. <option value="9">9</option>
  1594. </select>
  1595. </td>
  1596. </tr>
  1597. </tbody></table>
  1598. <p class="ml-submit">
  1599. <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
  1600. <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="update-gallery" id="update-gallery" value="<?php esc_attr_e( 'Update gallery settings' ); ?>" />
  1601. </p>
  1602. </div>
  1603. </form>
  1604. <?php
  1605. }
  1606. /**
  1607. * {@internal Missing Short Description}}
  1608. *
  1609. * @since 2.5.0
  1610. *
  1611. * @param array $errors
  1612. */
  1613. function media_upload_library_form($errors) {
  1614. global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
  1615. media_upload_header();
  1616. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1617. $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
  1618. $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
  1619. $form_class = 'media-upload-form validate';
  1620. if ( get_user_setting('uploader') )
  1621. $form_class .= ' html-uploader';
  1622. $_GET['paged'] = isset( $_GET['paged'] ) ? intval($_GET['paged']) : 0;
  1623. if ( $_GET['paged'] < 1 )
  1624. $_GET['paged'] = 1;
  1625. $start = ( $_GET['paged'] - 1 ) * 10;
  1626. if ( $start < 1 )
  1627. $start = 0;
  1628. add_filter( 'post_limits', create_function( '$a', "return 'LIMIT $start, 10';" ) );
  1629. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  1630. ?>
  1631. <form id="filter" action="" method="get">
  1632. <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
  1633. <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
  1634. <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
  1635. <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
  1636. <input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
  1637. <p id="media-search" class="search-box">
  1638. <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
  1639. <input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
  1640. <?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
  1641. </p>
  1642. <ul class="subsubsub">
  1643. <?php
  1644. $type_links = array();
  1645. $_num_posts = (array) wp_count_attachments();
  1646. $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  1647. foreach ( $matches as $_type => $reals )
  1648. foreach ( $reals as $real )
  1649. if ( isset($num_posts[$_type]) )
  1650. $num_posts[$_type] += $_num_posts[$real];
  1651. else
  1652. $num_posts[$_type] = $_num_posts[$real];
  1653. // If available type specified by media button clicked, filter by that type
  1654. if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
  1655. $_GET['post_mime_type'] = $type;
  1656. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  1657. }
  1658. if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
  1659. $class = ' class="current"';
  1660. else
  1661. $class = '';
  1662. $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
  1663. foreach ( $post_mime_types as $mime_type => $label ) {
  1664. $class = '';
  1665. if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  1666. continue;
  1667. if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  1668. $class = ' class="current"';
  1669. $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false))) . "'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), "<span id='$mime_type-counter'>" . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
  1670. }
  1671. echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
  1672. unset($type_links);
  1673. ?>
  1674. </ul>
  1675. <div class="tablenav">
  1676. <?php
  1677. $page_links = paginate_links( array(
  1678. 'base' => add_query_arg( 'paged', '%#%' ),
  1679. 'format' => '',
  1680. 'prev_text' => __('&laquo;'),
  1681. 'next_text' => __('&raquo;'),
  1682. 'total' => ceil($wp_query->found_posts / 10),
  1683. 'current' => $_GET['paged']
  1684. ));
  1685. if ( $page_links )
  1686. echo "<div class='tablenav-pages'>$page_links</div>";
  1687. ?>
  1688. <div class="alignleft actions">
  1689. <?php
  1690. $arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC";
  1691. $arc_result = $wpdb->get_results( $arc_query );
  1692. $month_count = count($arc_result);
  1693. if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
  1694. <select name='m'>
  1695. <option<?php selected( @$_GET['m'], 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
  1696. <?php
  1697. foreach ($arc_result as $arc_row) {
  1698. if ( $arc_row->yyear == 0 )
  1699. continue;
  1700. $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
  1701. if ( isset($_GET['m']) && ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] ) )
  1702. $default = ' selected="selected"';
  1703. else
  1704. $default = '';
  1705. echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
  1706. echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
  1707. echo "</option>\n";
  1708. }
  1709. ?>
  1710. </select>
  1711. <?php } ?>
  1712. <?php submit_button( __( 'Filter &#187;' ), 'button', 'post-query-submit', false ); ?>
  1713. </div>
  1714. <br class="clear" />
  1715. </div>
  1716. </form>
  1717. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form">
  1718. <?php wp_nonce_field('media-form'); ?>
  1719. <?php //media_upload_form( $errors ); ?>
  1720. <script type="text/javascript">
  1721. <!--
  1722. jQuery(function($){
  1723. var preloaded = $(".media-item.preloaded");
  1724. if ( preloaded.length > 0 ) {
  1725. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1726. updateMediaForm();
  1727. }
  1728. });
  1729. -->
  1730. </script>
  1731. <div id="media-items">
  1732. <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
  1733. <?php echo get_media_items(null, $errors); ?>
  1734. </div>
  1735. <p class="ml-submit">
  1736. <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
  1737. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1738. </p>
  1739. </form>
  1740. <?php
  1741. }
  1742. /**
  1743. * Creates the form for external url
  1744. *
  1745. * @since 2.7.0
  1746. *
  1747. * @param string $default_view
  1748. * @return string the form html
  1749. */
  1750. function wp_media_insert_url_form( $default_view = 'image' ) {
  1751. if ( !apply_filters( 'disable_captions', '' ) ) {
  1752. $caption = '
  1753. <tr class="image-only">
  1754. <th valign="top" scope="row" class="label">
  1755. <label for="caption"><span class="alignleft">' . __('Image Caption') . '</span></label>
  1756. </th>
  1757. <td class="field"><textarea id="caption" name="caption"></textarea></td>
  1758. </tr>
  1759. ';
  1760. } else {
  1761. $caption = '';
  1762. }
  1763. $default_align = get_option('image_default_align');
  1764. if ( empty($default_align) )
  1765. $default_align = 'none';
  1766. if ( 'image' == $default_view ) {
  1767. $view = 'image-only';
  1768. $table_class = '';
  1769. } else {
  1770. $view = $table_class = 'not-image';
  1771. }
  1772. return '
  1773. <p class="media-types"><label><input type="radio" name="media_type" value="image" id="image-only"' . checked( 'image-only', $view, false ) . ' /> ' . __( 'Image' ) . '</label> &nbsp; &nbsp; <label><input type="radio" name="media_type" value="generic" id="not-image"' . checked( 'not-image', $view, false ) . ' /> ' . __( 'Audio, Video, or Other File' ) . '</label></p>
  1774. <table class="describe ' . $table_class . '"><tbody>
  1775. <tr>
  1776. <th valign="top" scope="row" class="label" style="width:130px;">
  1777. <label for="src"><span class="alignleft">' . __('URL') . '</span></label>
  1778. <span class="alignright"><abbr id="status_img" title="required" class="required">*</abbr></span>
  1779. </th>
  1780. <td class="field"><input id="src" name="src" value="" type="text" aria-required="true" onblur="addExtImage.getImageData()" /></td>
  1781. </tr>
  1782. <tr>
  1783. <th valign="top" scope="row" class="label">
  1784. <label for="title"><span class="alignleft">' . __('Title') . '</span></label>
  1785. <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1786. </th>
  1787. <td class="field"><input id="title" name="title" value="" type="text" aria-required="true" /></td>
  1788. </tr>
  1789. <tr class="not-image"><td></td><td><p class="help">' . __('Link text, e.g. &#8220;Ransom Demands (PDF)&#8221;') . '</p></td></tr>
  1790. <tr class="image-only">
  1791. <th valign="top" scope="row" class="label">
  1792. <label for="alt"><span class="alignleft">' . __('Alternative Text') . '</span></label>
  1793. </th>
  1794. <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
  1795. <p class="help">' . __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;') . '</p></td>
  1796. </tr>
  1797. ' . $caption . '
  1798. <tr class="align image-only">
  1799. <th valign="top" scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
  1800. <td class="field">
  1801. <input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'none' ? ' checked="checked"' : '').' />
  1802. <label for="align-none" class="align image-align-none-label">' . __('None') . '</label>
  1803. <input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'left' ? ' checked="checked"' : '').' />
  1804. <label for="align-left" class="align image-align-left-label">' . __('Left') . '</label>
  1805. <input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'center' ? ' checked="checked"' : '').' />
  1806. <label for="align-center" class="align image-align-center-label">' . __('Center') . '</label>
  1807. <input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'right' ? ' checked="checked"' : '').' />
  1808. <label for="align-right" class="align image-align-right-label">' . __('Right') . '</label>
  1809. </td>
  1810. </tr>
  1811. <tr class="image-only">
  1812. <th valign="top" scope="row" class="label">
  1813. <label for="url"><span class="alignleft">' . __('Link Image To:') . '</span></label>
  1814. </th>
  1815. <td class="field"><input id="url" name="url" value="" type="text" /><br />
  1816. <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __('None') . '</button>
  1817. <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __('Link to image') . '</button>
  1818. <p class="help">' . __('Enter a link URL or click above for presets.') . '</p></td>
  1819. </tr>
  1820. <tr class="image-only">
  1821. <td></td>
  1822. <td>
  1823. <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . esc_attr__('Insert into Post') . '" />
  1824. </td>
  1825. </tr>
  1826. <tr class="not-image">
  1827. <td></td>
  1828. <td>
  1829. ' . get_submit_button( __( 'Insert into Post' ), 'button', 'insertonlybutton', false ) . '
  1830. </td>
  1831. </tr>
  1832. </tbody></table>
  1833. ';
  1834. }
  1835. /**
  1836. * Displays the multi-file uploader message.
  1837. *
  1838. * @since 2.6.0
  1839. */
  1840. function media_upload_flash_bypass() {
  1841. $browser_uploader = admin_url( 'media-new.php?browser-uploader' );
  1842. if ( $post = get_post() )
  1843. $browser_uploader .= '&amp;post_id=' . intval( $post->ID );
  1844. elseif ( ! empty( $GLOBALS['post_ID'] ) )
  1845. $browser_uploader .= '&amp;post_id=' . intval( $GLOBALS['post_ID'] );
  1846. ?>
  1847. <p class="upload-flash-bypass">
  1848. <?php printf( __( 'You are using the multi-file uploader. Problems? Try the <a href="%1$s" target="%2$s">browser uploader</a> instead.' ), $browser_uploader, '_blank' ); ?>
  1849. </p>
  1850. <?php
  1851. }
  1852. add_action('post-plupload-upload-ui', 'media_upload_flash_bypass');
  1853. /**
  1854. * Displays the browser's built-in uploader message.
  1855. *
  1856. * @since 2.6.0
  1857. */
  1858. function media_upload_html_bypass() {
  1859. ?>
  1860. <p class="upload-html-bypass hide-if-no-js">
  1861. <?php _e('You are using the browser&#8217;s built-in file uploader. The WordPress uploader includes multiple file selection and drag and drop capability. <a href="#">Switch to the multi-file uploader</a>.'); ?>
  1862. </p>
  1863. <?php
  1864. }
  1865. add_action('post-html-upload-ui', 'media_upload_html_bypass');
  1866. /**
  1867. * Used to display a "After a file has been uploaded..." help message.
  1868. *
  1869. * @since 3.3.0
  1870. */
  1871. function media_upload_text_after() {}
  1872. /**
  1873. * Displays the checkbox to scale images.
  1874. *
  1875. * @since 3.3.0
  1876. */
  1877. function media_upload_max_image_resize() {
  1878. $checked = get_user_setting('upload_resize') ? ' checked="true"' : '';
  1879. $a = $end = '';
  1880. if ( current_user_can( 'manage_options' ) ) {
  1881. $a = '<a href="' . esc_url( admin_url( 'options-media.php' ) ) . '" target="_blank">';
  1882. $end = '</a>';
  1883. }
  1884. ?>
  1885. <p class="hide-if-no-js"><label>
  1886. <input name="image_resize" type="checkbox" id="image_resize" value="true"<?php echo $checked; ?> />
  1887. <?php
  1888. /* translators: %1$s is link start tag, %2$s is link end tag, %3$d is width, %4$d is height*/
  1889. printf( __( 'Scale images to match the large size selected in %1$simage options%2$s (%3$d &times; %4$d).' ), $a, $end, (int) get_option( 'large_size_w', '1024' ), (int) get_option( 'large_size_h', '1024' ) );
  1890. ?>
  1891. </label></p>
  1892. <?php
  1893. }
  1894. /**
  1895. * Displays the out of storage quota message in Multisite.
  1896. *
  1897. * @since 3.5.0
  1898. */
  1899. function multisite_over_quota_message() {
  1900. echo '<p>' . sprintf( __( 'Sorry, you have used all of your storage quota of %s MB.' ), get_space_allowed() ) . '</p>';
  1901. }
  1902. /**
  1903. * Displays the image and editor in the post editor
  1904. *
  1905. * @since 3.5.0
  1906. */
  1907. function edit_form_image_editor() {
  1908. $post = get_post();
  1909. $open = isset( $_GET['image-editor'] );
  1910. if ( $open )
  1911. require_once ABSPATH . 'wp-admin/includes/image-edit.php';
  1912. $thumb_url = false;
  1913. if ( $attachment_id = intval( $post->ID ) )
  1914. $thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
  1915. $filename = esc_html( basename( $post->guid ) );
  1916. $title = esc_attr( $post->post_title );
  1917. $alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
  1918. $att_url = wp_get_attachment_url( $post->ID );
  1919. if ( wp_attachment_is_image( $post->ID ) ) :
  1920. $image_edit_button = '';
  1921. if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  1922. $nonce = wp_create_nonce( "image_editor-$post->ID" );
  1923. $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>";
  1924. }
  1925. ?>
  1926. <div class="wp_attachment_holder">
  1927. <div class="imgedit-response" id="imgedit-response-<?php echo $attachment_id; ?>"></div>
  1928. <div<?php if ( $open ) echo ' style="display:none"'; ?> class="wp_attachment_image" id="media-head-<?php echo $attachment_id; ?>">
  1929. <p id="thumbnail-head-<?php echo $attachment_id; ?>"><img class="thumbnail" src="<?php echo set_url_scheme( $thumb_url[0] ); ?>" style="max-width:100%" alt="" /></p>
  1930. <p><?php echo $image_edit_button; ?></p>
  1931. </div>
  1932. <div<?php if ( ! $open ) echo ' style="display:none"'; ?> class="image-editor" id="image-editor-<?php echo $attachment_id; ?>">
  1933. <?php if ( $open ) wp_image_editor( $attachment_id ); ?>
  1934. </div>
  1935. </div>
  1936. <?php endif; ?>
  1937. <div class="wp_attachment_details edit-form-section">
  1938. <p>
  1939. <label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong></label><br />
  1940. <textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea>
  1941. </p>
  1942. <?php if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) : ?>
  1943. <p>
  1944. <label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br />
  1945. <input type="text" class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" value="<?php echo esc_attr( $alt_text ); ?>" />
  1946. </p>
  1947. <?php endif; ?>
  1948. <?php
  1949. $quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,spell,close' );
  1950. $editor_args = array(
  1951. 'textarea_name' => 'content',
  1952. 'textarea_rows' => 5,
  1953. 'media_buttons' => false,
  1954. 'tinymce' => false,
  1955. 'quicktags' => $quicktags_settings,
  1956. );
  1957. ?>
  1958. <label for="content"><strong><?php _e( 'Description' ); ?></strong></label>
  1959. <?php wp_editor( $post->post_content, 'attachment_content', $editor_args ); ?>
  1960. </div>
  1961. <?php
  1962. $extras = get_compat_media_markup( $post->ID );
  1963. echo $extras['item'];
  1964. echo '<input type="hidden" id="image-edit-context" value="edit-attachment" />' . "\n";
  1965. }
  1966. /**
  1967. * Displays non-editable attachment metadata in the publish metabox
  1968. *
  1969. * @since 3.5.0
  1970. */
  1971. function attachment_submitbox_metadata() {
  1972. $post = get_post();
  1973. $filename = esc_html( basename( $post->guid ) );
  1974. $media_dims = '';
  1975. $meta = wp_get_attachment_metadata( $post->ID );
  1976. if ( is_array( $meta ) && array_key_exists( 'width', $meta ) && array_key_exists( 'height', $meta ) )
  1977. $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  1978. $media_dims = apply_filters( 'media_meta', $media_dims, $post );
  1979. $att_url = wp_get_attachment_url( $post->ID );
  1980. ?>
  1981. <div class="misc-pub-section">
  1982. <label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
  1983. <input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" value="<?php echo esc_attr($att_url); ?>" />
  1984. </div>
  1985. <div class="misc-pub-section">
  1986. <?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
  1987. </div>
  1988. <div class="misc-pub-section">
  1989. <?php _e( 'File type:' ); ?> <strong><?php
  1990. if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) )
  1991. echo esc_html( strtoupper( $matches[1] ) );
  1992. else
  1993. echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
  1994. ?></strong>
  1995. </div>
  1996. <?php if ( $media_dims ) : ?>
  1997. <div class="misc-pub-section">
  1998. <?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
  1999. </div>
  2000. <?php
  2001. endif;
  2002. }
  2003. add_filter( 'async_upload_image', 'get_media_item', 10, 2 );
  2004. add_filter( 'async_upload_audio', 'get_media_item', 10, 2 );
  2005. add_filter( 'async_upload_video', 'get_media_item', 10, 2 );
  2006. add_filter( 'async_upload_file', 'get_media_item', 10, 2 );
  2007. add_action( 'media_upload_image', 'wp_media_upload_handler' );
  2008. add_action( 'media_upload_audio', 'wp_media_upload_handler' );
  2009. add_action( 'media_upload_video', 'wp_media_upload_handler' );
  2010. add_action( 'media_upload_file', 'wp_media_upload_handler' );
  2011. add_filter( 'media_upload_gallery', 'media_upload_gallery' );
  2012. add_filter( 'media_upload_library', 'media_upload_library' );
  2013. add_action( 'attachment_submitbox_misc_actions', 'attachment_submitbox_metadata' );