PageRenderTime 55ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests
PHP | 1517 lines | 1458 code | 18 blank | 41 comment | 11 complexity | d70fff25c62e2e2604e6acf125db741f MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress Administration Media API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Defines the default media upload tabs
  10. *
  11. * @since 2.5.0
  12. *
  13. * @return array default tabs
  14. */
  15. function media_upload_tabs() {
  16. $_default_tabs = array(
  17. 'type' => __('From Computer'), // handler action suffix => tab text
  18. 'type_url' => __('From URL'),
  19. 'gallery' => __('Gallery'),
  20. 'library' => __('Media Library')
  21. );
  22. return apply_filters('media_upload_tabs', $_default_tabs);
  23. }
  24. /**
  25. * Adds the gallery tab back to the tabs array if post has image attachments
  26. *
  27. * @since 2.5.0
  28. *
  29. * @param array $tabs
  30. * @return array $tabs with gallery if post has image attachment
  31. */
  32. function update_gallery_tab($tabs) {
  33. global $wpdb;
  34. if ( !isset($_REQUEST['post_id']) ) {
  35. unset($tabs['gallery']);
  36. return $tabs;
  37. }
  38. $post_id = intval($_REQUEST['post_id']);
  39. if ( $post_id )
  40. $attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) );
  41. if ( empty($attachments) ) {
  42. unset($tabs['gallery']);
  43. return $tabs;
  44. }
  45. $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
  46. return $tabs;
  47. }
  48. add_filter('media_upload_tabs', 'update_gallery_tab');
  49. /**
  50. * {@internal Missing Short Description}}
  51. *
  52. * @since 2.5.0
  53. */
  54. function the_media_upload_tabs() {
  55. global $redir_tab;
  56. $tabs = media_upload_tabs();
  57. $default = 'type';
  58. if ( !empty($tabs) ) {
  59. echo "<ul id='sidemenu'>\n";
  60. if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) )
  61. $current = $redir_tab;
  62. elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) )
  63. $current = $_GET['tab'];
  64. else
  65. $current = apply_filters('media_upload_default_tab', $default);
  66. foreach ( $tabs as $callback => $text ) {
  67. $class = '';
  68. if ( $current == $callback )
  69. $class = " class='current'";
  70. $href = add_query_arg(array('tab' => $callback, 's' => false, 'paged' => false, 'post_mime_type' => false, 'm' => false));
  71. $link = "<a href='" . esc_url($href) . "'$class>$text</a>";
  72. echo "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
  73. }
  74. echo "</ul>\n";
  75. }
  76. }
  77. /**
  78. * {@internal Missing Short Description}}
  79. *
  80. * @since 2.5.0
  81. *
  82. * @param integer $id image attachment id
  83. * @param string $caption image caption
  84. * @param string $alt image alt attribute
  85. * @param string $title image title attribute
  86. * @param string $align image css alignment property
  87. * @param string $url image src url
  88. * @param string|bool $rel image rel attribute
  89. * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
  90. * @return string the html to insert into editor
  91. */
  92. function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
  93. $html = get_image_tag($id, $alt, '', $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. if ( preg_match( '#^audio#', $type ) ) {
  190. $meta = wp_read_audio_metadata( $file );
  191. if ( ! empty( $meta['title'] ) )
  192. $title = $meta['title'];
  193. $content = '';
  194. if ( ! empty( $title ) ) {
  195. if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
  196. /* translators: 1: audio track title, 2: album title, 3: artist name */
  197. $content .= sprintf( __( '&#8220;%1$s&#8221; from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
  198. } else if ( ! empty( $meta['album'] ) ) {
  199. /* translators: 1: audio track title, 2: album title */
  200. $content .= sprintf( __( '&#8220;%1$s&#8221; from %2$s.' ), $title, $meta['album'] );
  201. } else if ( ! empty( $meta['artist'] ) ) {
  202. /* translators: 1: audio track title, 2: artist name */
  203. $content .= sprintf( __( '&#8220;%1$s&#8221; by %2$s.' ), $title, $meta['artist'] );
  204. } else {
  205. $content .= sprintf( __( '&#8220;%s&#8221;.' ), $title );
  206. }
  207. } else if ( ! empty( $meta['album'] ) ) {
  208. if ( ! empty( $meta['artist'] ) ) {
  209. /* translators: 1: audio album title, 2: artist name */
  210. $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
  211. } else {
  212. $content .= $meta['album'] . '.';
  213. }
  214. } else if ( ! empty( $meta['artist'] ) ) {
  215. $content .= $meta['artist'] . '.';
  216. }
  217. if ( ! empty( $meta['year'] ) )
  218. $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
  219. if ( ! empty( $meta['track_number'] ) ) {
  220. $track_number = explode( '/', $meta['track_number'] );
  221. if ( isset( $track_number[1] ) )
  222. $content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
  223. else
  224. $content .= ' ' . sprintf( __( 'Track %1$s.' ), number_format_i18n( $track_number[0] ) );
  225. }
  226. if ( ! empty( $meta['genre'] ) )
  227. $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
  228. // use image exif/iptc data for title and caption defaults if possible
  229. } elseif ( $image_meta = @wp_read_image_metadata( $file ) ) {
  230. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
  231. $title = $image_meta['title'];
  232. if ( trim( $image_meta['caption'] ) )
  233. $content = $image_meta['caption'];
  234. }
  235. // Construct the attachment array
  236. $attachment = array_merge( array(
  237. 'post_mime_type' => $type,
  238. 'guid' => $url,
  239. 'post_parent' => $post_id,
  240. 'post_title' => $title,
  241. 'post_content' => $content,
  242. ), $post_data );
  243. // This should never be set as it would then overwrite an existing attachment.
  244. if ( isset( $attachment['ID'] ) )
  245. unset( $attachment['ID'] );
  246. // Save the data
  247. $id = wp_insert_attachment($attachment, $file, $post_id);
  248. if ( !is_wp_error($id) ) {
  249. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  250. }
  251. return $id;
  252. }
  253. /**
  254. * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
  255. *
  256. * @since 2.6.0
  257. *
  258. * @param array $file_array Array similar to a {@link $_FILES} upload array
  259. * @param int $post_id The post ID the media is associated with
  260. * @param string $desc Description of the sideloaded file
  261. * @param array $post_data allows you to overwrite some of the attachment
  262. * @return int|object The ID of the attachment or a WP_Error on failure
  263. */
  264. function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
  265. $overrides = array('test_form'=>false);
  266. $time = current_time( 'mysql' );
  267. if ( $post = get_post( $post_id ) ) {
  268. if ( substr( $post->post_date, 0, 4 ) > 0 )
  269. $time = $post->post_date;
  270. }
  271. $file = wp_handle_sideload( $file_array, $overrides, $time );
  272. if ( isset($file['error']) )
  273. return new WP_Error( 'upload_error', $file['error'] );
  274. $url = $file['url'];
  275. $type = $file['type'];
  276. $file = $file['file'];
  277. $title = preg_replace('/\.[^.]+$/', '', basename($file));
  278. $content = '';
  279. // use image exif/iptc data for title and caption defaults if possible
  280. if ( $image_meta = @wp_read_image_metadata($file) ) {
  281. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
  282. $title = $image_meta['title'];
  283. if ( trim( $image_meta['caption'] ) )
  284. $content = $image_meta['caption'];
  285. }
  286. if ( isset( $desc ) )
  287. $title = $desc;
  288. // Construct the attachment array
  289. $attachment = array_merge( array(
  290. 'post_mime_type' => $type,
  291. 'guid' => $url,
  292. 'post_parent' => $post_id,
  293. 'post_title' => $title,
  294. 'post_content' => $content,
  295. ), $post_data );
  296. // This should never be set as it would then overwrite an existing attachment.
  297. if ( isset( $attachment['ID'] ) )
  298. unset( $attachment['ID'] );
  299. // Save the attachment metadata
  300. $id = wp_insert_attachment($attachment, $file, $post_id);
  301. if ( !is_wp_error($id) )
  302. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  303. return $id;
  304. }
  305. /**
  306. * Adds the iframe to display content for the media upload page
  307. *
  308. * @since 2.5.0
  309. *
  310. * @param array $content_func
  311. */
  312. function wp_iframe($content_func /* ... */) {
  313. _wp_admin_html_begin();
  314. ?>
  315. <title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
  316. <?php
  317. wp_enqueue_style( 'colors' );
  318. // Check callback name for 'media'
  319. if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
  320. || ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
  321. wp_enqueue_style( 'media' );
  322. wp_enqueue_style( 'ie' );
  323. ?>
  324. <script type="text/javascript">
  325. //<![CDATA[
  326. 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();}}};
  327. var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
  328. isRtl = <?php echo (int) is_rtl(); ?>;
  329. //]]>
  330. </script>
  331. <?php
  332. do_action('admin_enqueue_scripts', 'media-upload-popup');
  333. do_action('admin_print_styles-media-upload-popup');
  334. do_action('admin_print_styles');
  335. do_action('admin_print_scripts-media-upload-popup');
  336. do_action('admin_print_scripts');
  337. do_action('admin_head-media-upload-popup');
  338. do_action('admin_head');
  339. if ( is_string($content_func) )
  340. do_action( "admin_head_{$content_func}" );
  341. ?>
  342. </head>
  343. <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?> class="wp-core-ui no-js">
  344. <script type="text/javascript">
  345. document.body.className = document.body.className.replace('no-js', 'js');
  346. </script>
  347. <?php
  348. $args = func_get_args();
  349. $args = array_slice($args, 1);
  350. call_user_func_array($content_func, $args);
  351. do_action('admin_print_footer_scripts');
  352. ?>
  353. <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
  354. </body>
  355. </html>
  356. <?php
  357. }
  358. /**
  359. * Adds the media button to the editor
  360. *
  361. * @since 2.5.0
  362. *
  363. * @param string $editor_id
  364. */
  365. function media_buttons($editor_id = 'content') {
  366. $post = get_post();
  367. if ( ! $post && ! empty( $GLOBALS['post_ID'] ) )
  368. $post = $GLOBALS['post_ID'];
  369. wp_enqueue_media( array(
  370. 'post' => $post
  371. ) );
  372. $img = '<span class="wp-media-buttons-icon"></span> ';
  373. 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>';
  374. // Don't use this filter. Want to add a button? Use the media_buttons action.
  375. $legacy_filter = apply_filters('media_buttons_context', ''); // deprecated
  376. if ( $legacy_filter ) {
  377. // #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
  378. if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) )
  379. $legacy_filter .= '</a>';
  380. echo $legacy_filter;
  381. }
  382. }
  383. add_action( 'media_buttons', 'media_buttons' );
  384. function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
  385. global $post_ID;
  386. if ( empty( $post_id ) )
  387. $post_id = $post_ID;
  388. $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
  389. if ( $type && 'media' != $type )
  390. $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
  391. if ( ! empty( $tab ) )
  392. $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
  393. $upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
  394. return add_query_arg('TB_iframe', true, $upload_iframe_src);
  395. }
  396. /**
  397. * {@internal Missing Short Description}}
  398. *
  399. * @since 2.5.0
  400. *
  401. * @return mixed void|object WP_Error on failure
  402. */
  403. function media_upload_form_handler() {
  404. check_admin_referer('media-form');
  405. $errors = null;
  406. if ( isset($_POST['send']) ) {
  407. $keys = array_keys($_POST['send']);
  408. $send_id = (int) array_shift($keys);
  409. }
  410. if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
  411. $post = $_post = get_post($attachment_id, ARRAY_A);
  412. if ( !current_user_can( 'edit_post', $attachment_id ) )
  413. continue;
  414. if ( isset($attachment['post_content']) )
  415. $post['post_content'] = $attachment['post_content'];
  416. if ( isset($attachment['post_title']) )
  417. $post['post_title'] = $attachment['post_title'];
  418. if ( isset($attachment['post_excerpt']) )
  419. $post['post_excerpt'] = $attachment['post_excerpt'];
  420. if ( isset($attachment['menu_order']) )
  421. $post['menu_order'] = $attachment['menu_order'];
  422. if ( isset($send_id) && $attachment_id == $send_id ) {
  423. if ( isset($attachment['post_parent']) )
  424. $post['post_parent'] = $attachment['post_parent'];
  425. }
  426. $post = apply_filters('attachment_fields_to_save', $post, $attachment);
  427. if ( isset($attachment['image_alt']) ) {
  428. $image_alt = wp_unslash( $attachment['image_alt'] );
  429. if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) {
  430. $image_alt = wp_strip_all_tags( $image_alt, true );
  431. // update_meta expects slashed
  432. update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
  433. }
  434. }
  435. if ( isset($post['errors']) ) {
  436. $errors[$attachment_id] = $post['errors'];
  437. unset($post['errors']);
  438. }
  439. if ( $post != $_post )
  440. wp_update_post($post);
  441. foreach ( get_attachment_taxonomies($post) as $t ) {
  442. if ( isset($attachment[$t]) )
  443. wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
  444. }
  445. }
  446. if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
  447. <script type="text/javascript">
  448. /* <![CDATA[ */
  449. var win = window.dialogArguments || opener || parent || top;
  450. win.tb_remove();
  451. /* ]]> */
  452. </script>
  453. <?php
  454. exit;
  455. }
  456. if ( isset($send_id) ) {
  457. $attachment = wp_unslash( $_POST['attachments'][$send_id] );
  458. $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
  459. if ( !empty($attachment['url']) ) {
  460. $rel = '';
  461. if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
  462. $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
  463. $html = "<a href='{$attachment['url']}'$rel>$html</a>";
  464. }
  465. $html = apply_filters('media_send_to_editor', $html, $send_id, $attachment);
  466. return media_send_to_editor($html);
  467. }
  468. return $errors;
  469. }
  470. /**
  471. * {@internal Missing Short Description}}
  472. *
  473. * @since 2.5.0
  474. *
  475. * @return mixed
  476. */
  477. function wp_media_upload_handler() {
  478. $errors = array();
  479. $id = 0;
  480. if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
  481. check_admin_referer('media-form');
  482. // Upload File button was clicked
  483. $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
  484. unset($_FILES);
  485. if ( is_wp_error($id) ) {
  486. $errors['upload_error'] = $id;
  487. $id = false;
  488. }
  489. }
  490. if ( !empty($_POST['insertonlybutton']) ) {
  491. $src = $_POST['src'];
  492. if ( !empty($src) && !strpos($src, '://') )
  493. $src = "http://$src";
  494. if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
  495. $title = esc_html( wp_unslash( $_POST['title'] ) );
  496. if ( empty( $title ) )
  497. $title = esc_html( basename( $src ) );
  498. if ( $title && $src )
  499. $html = "<a href='" . esc_url($src) . "'>$title</a>";
  500. $type = 'file';
  501. if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
  502. && ( 'audio' == $ext_type || 'video' == $ext_type ) )
  503. $type = $ext_type;
  504. $html = apply_filters( $type . '_send_to_editor_url', $html, esc_url_raw( $src ), $title );
  505. } else {
  506. $align = '';
  507. $alt = esc_attr( wp_unslash( $_POST['alt'] ) );
  508. if ( isset($_POST['align']) ) {
  509. $align = esc_attr( wp_unslash( $_POST['align'] ) );
  510. $class = " class='align$align'";
  511. }
  512. if ( !empty($src) )
  513. $html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";
  514. $html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
  515. }
  516. return media_send_to_editor($html);
  517. }
  518. if ( !empty($_POST) ) {
  519. $return = media_upload_form_handler();
  520. if ( is_string($return) )
  521. return $return;
  522. if ( is_array($return) )
  523. $errors = $return;
  524. }
  525. if ( isset($_POST['save']) ) {
  526. $errors['upload_notice'] = __('Saved.');
  527. return media_upload_gallery();
  528. }
  529. if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
  530. $type = 'image';
  531. if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
  532. $type = $_GET['type'];
  533. return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
  534. }
  535. return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
  536. }
  537. /**
  538. * Download an image from the specified URL and attach it to a post.
  539. *
  540. * @since 2.6.0
  541. *
  542. * @param string $file The URL of the image to download
  543. * @param int $post_id The post ID the media is to be associated with
  544. * @param string $desc Optional. Description of the image
  545. * @return string|WP_Error Populated HTML img tag on success
  546. */
  547. function media_sideload_image($file, $post_id, $desc = null) {
  548. if ( ! empty($file) ) {
  549. // Download file to temp location
  550. $tmp = download_url( $file );
  551. // Set variables for storage
  552. // fix file filename for query strings
  553. preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
  554. $file_array['name'] = basename($matches[0]);
  555. $file_array['tmp_name'] = $tmp;
  556. // If error storing temporarily, unlink
  557. if ( is_wp_error( $tmp ) ) {
  558. @unlink($file_array['tmp_name']);
  559. $file_array['tmp_name'] = '';
  560. }
  561. // do the validation and storage stuff
  562. $id = media_handle_sideload( $file_array, $post_id, $desc );
  563. // If error storing permanently, unlink
  564. if ( is_wp_error($id) ) {
  565. @unlink($file_array['tmp_name']);
  566. return $id;
  567. }
  568. $src = wp_get_attachment_url( $id );
  569. }
  570. // Finally check to make sure the file has been saved, then return the html
  571. if ( ! empty($src) ) {
  572. $alt = isset($desc) ? esc_attr($desc) : '';
  573. $html = "<img src='$src' alt='$alt' />";
  574. return $html;
  575. }
  576. }
  577. /**
  578. * {@internal Missing Short Description}}
  579. *
  580. * @since 2.5.0
  581. *
  582. * @return unknown
  583. */
  584. function media_upload_gallery() {
  585. $errors = array();
  586. if ( !empty($_POST) ) {
  587. $return = media_upload_form_handler();
  588. if ( is_string($return) )
  589. return $return;
  590. if ( is_array($return) )
  591. $errors = $return;
  592. }
  593. wp_enqueue_script('admin-gallery');
  594. return wp_iframe( 'media_upload_gallery_form', $errors );
  595. }
  596. /**
  597. * {@internal Missing Short Description}}
  598. *
  599. * @since 2.5.0
  600. *
  601. * @return unknown
  602. */
  603. function media_upload_library() {
  604. $errors = array();
  605. if ( !empty($_POST) ) {
  606. $return = media_upload_form_handler();
  607. if ( is_string($return) )
  608. return $return;
  609. if ( is_array($return) )
  610. $errors = $return;
  611. }
  612. return wp_iframe( 'media_upload_library_form', $errors );
  613. }
  614. /**
  615. * Retrieve HTML for the image alignment radio buttons with the specified one checked.
  616. *
  617. * @since 2.7.0
  618. *
  619. * @param object $post
  620. * @param string $checked
  621. * @return string
  622. */
  623. function image_align_input_fields( $post, $checked = '' ) {
  624. if ( empty($checked) )
  625. $checked = get_user_setting('align', 'none');
  626. $alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
  627. if ( !array_key_exists( (string) $checked, $alignments ) )
  628. $checked = 'none';
  629. $out = array();
  630. foreach ( $alignments as $name => $label ) {
  631. $name = esc_attr($name);
  632. $out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
  633. ( $checked == $name ? " checked='checked'" : "" ) .
  634. " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
  635. }
  636. return join("\n", $out);
  637. }
  638. /**
  639. * Retrieve HTML for the size radio buttons with the specified one checked.
  640. *
  641. * @since 2.7.0
  642. *
  643. * @param object $post
  644. * @param bool|string $check
  645. * @return array
  646. */
  647. function image_size_input_fields( $post, $check = '' ) {
  648. // get a list of the actual pixel dimensions of each possible intermediate version of this image
  649. $size_names = apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) );
  650. if ( empty($check) )
  651. $check = get_user_setting('imgsize', 'medium');
  652. foreach ( $size_names as $size => $label ) {
  653. $downsize = image_downsize($post->ID, $size);
  654. $checked = '';
  655. // is this size selectable?
  656. $enabled = ( $downsize[3] || 'full' == $size );
  657. $css_id = "image-size-{$size}-{$post->ID}";
  658. // if this size is the default but that's not available, don't select it
  659. if ( $size == $check ) {
  660. if ( $enabled )
  661. $checked = " checked='checked'";
  662. else
  663. $check = '';
  664. } elseif ( !$check && $enabled && 'thumbnail' != $size ) {
  665. // if $check is not enabled, default to the first available size that's bigger than a thumbnail
  666. $check = $size;
  667. $checked = " checked='checked'";
  668. }
  669. $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 />";
  670. $html .= "<label for='{$css_id}'>$label</label>";
  671. // only show the dimensions if that choice is available
  672. if ( $enabled )
  673. $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
  674. $html .= '</div>';
  675. $out[] = $html;
  676. }
  677. return array(
  678. 'label' => __('Size'),
  679. 'input' => 'html',
  680. 'html' => join("\n", $out),
  681. );
  682. }
  683. /**
  684. * Retrieve HTML for the Link URL buttons with the default link type as specified.
  685. *
  686. * @since 2.7.0
  687. *
  688. * @param object $post
  689. * @param string $url_type
  690. * @return string
  691. */
  692. function image_link_input_fields($post, $url_type = '') {
  693. $file = wp_get_attachment_url($post->ID);
  694. $link = get_attachment_link($post->ID);
  695. if ( empty($url_type) )
  696. $url_type = get_user_setting('urlbutton', 'post');
  697. $url = '';
  698. if ( $url_type == 'file' )
  699. $url = $file;
  700. elseif ( $url_type == 'post' )
  701. $url = $link;
  702. return "
  703. <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
  704. <button type='button' class='button urlnone' data-link-url=''>" . __('None') . "</button>
  705. <button type='button' class='button urlfile' data-link-url='" . esc_attr($file) . "'>" . __('File URL') . "</button>
  706. <button type='button' class='button urlpost' data-link-url='" . esc_attr($link) . "'>" . __('Attachment Post URL') . "</button>
  707. ";
  708. }
  709. function wp_caption_input_textarea($edit_post) {
  710. // post data is already escaped
  711. $name = "attachments[{$edit_post->ID}][post_excerpt]";
  712. return '<textarea name="' . $name . '" id="' . $name . '">' . $edit_post->post_excerpt . '</textarea>';
  713. }
  714. /**
  715. * {@internal Missing Short Description}}
  716. *
  717. * @since 2.5.0
  718. *
  719. * @param array $form_fields
  720. * @param object $post
  721. * @return array
  722. */
  723. function image_attachment_fields_to_edit($form_fields, $post) {
  724. return $form_fields;
  725. }
  726. /**
  727. * {@internal Missing Short Description}}
  728. *
  729. * @since 2.5.0
  730. *
  731. * @param array $form_fields
  732. * @param object $post {@internal $post not used}}
  733. * @return array
  734. */
  735. function media_single_attachment_fields_to_edit( $form_fields, $post ) {
  736. unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
  737. return $form_fields;
  738. }
  739. /**
  740. * {@internal Missing Short Description}}
  741. *
  742. * @since 2.8.0
  743. *
  744. * @param array $form_fields
  745. * @param object $post {@internal $post not used}}
  746. * @return array
  747. */
  748. function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
  749. unset($form_fields['image_url']);
  750. return $form_fields;
  751. }
  752. /**
  753. * Filters input from media_upload_form_handler() and assigns a default
  754. * post_title from the file name if none supplied.
  755. *
  756. * Illustrates the use of the attachment_fields_to_save filter
  757. * which can be used to add default values to any field before saving to DB.
  758. *
  759. * @since 2.5.0
  760. *
  761. * @param object $post
  762. * @param array $attachment {@internal $attachment not used}}
  763. * @return array
  764. */
  765. function image_attachment_fields_to_save($post, $attachment) {
  766. if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
  767. if ( strlen(trim($post['post_title'])) == 0 ) {
  768. $post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid']));
  769. $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
  770. }
  771. }
  772. return $post;
  773. }
  774. add_filter('attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2);
  775. /**
  776. * {@internal Missing Short Description}}
  777. *
  778. * @since 2.5.0
  779. *
  780. * @param string $html
  781. * @param integer $attachment_id
  782. * @param array $attachment
  783. * @return array
  784. */
  785. function image_media_send_to_editor($html, $attachment_id, $attachment) {
  786. $post = get_post($attachment_id);
  787. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  788. $url = $attachment['url'];
  789. $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
  790. $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
  791. $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
  792. $rel = ( $url == get_attachment_link($attachment_id) );
  793. return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
  794. }
  795. return $html;
  796. }
  797. add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
  798. /**
  799. * {@internal Missing Short Description}}
  800. *
  801. * @since 2.5.0
  802. *
  803. * @param object $post
  804. * @param array $errors
  805. * @return array
  806. */
  807. function get_attachment_fields_to_edit($post, $errors = null) {
  808. if ( is_int($post) )
  809. $post = get_post($post);
  810. if ( is_array($post) )
  811. $post = new WP_Post( (object) $post );
  812. $image_url = wp_get_attachment_url($post->ID);
  813. $edit_post = sanitize_post($post, 'edit');
  814. $form_fields = array(
  815. 'post_title' => array(
  816. 'label' => __('Title'),
  817. 'value' => $edit_post->post_title
  818. ),
  819. 'image_alt' => array(),
  820. 'post_excerpt' => array(
  821. 'label' => __('Caption'),
  822. 'input' => 'html',
  823. 'html' => wp_caption_input_textarea($edit_post)
  824. ),
  825. 'post_content' => array(
  826. 'label' => __('Description'),
  827. 'value' => $edit_post->post_content,
  828. 'input' => 'textarea'
  829. ),
  830. 'url' => array(
  831. 'label' => __('Link URL'),
  832. 'input' => 'html',
  833. 'html' => image_link_input_fields($post, get_option('image_default_link_type')),
  834. 'helps' => __('Enter a link URL or click above for presets.')
  835. ),
  836. 'menu_order' => array(
  837. 'label' => __('Order'),
  838. 'value' => $edit_post->menu_order
  839. ),
  840. 'image_url' => array(
  841. 'label' => __('File URL'),
  842. 'input' => 'html',
  843. 'html' => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
  844. 'value' => wp_get_attachment_url($post->ID),
  845. 'helps' => __('Location of the uploaded file.')
  846. )
  847. );
  848. foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  849. $t = (array) get_taxonomy($taxonomy);
  850. if ( ! $t['public'] || ! $t['show_ui'] )
  851. continue;
  852. if ( empty($t['label']) )
  853. $t['label'] = $taxonomy;
  854. if ( empty($t['args']) )
  855. $t['args'] = array();
  856. $terms = get_object_term_cache($post->ID, $taxonomy);
  857. if ( false === $terms )
  858. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  859. $values = array();
  860. foreach ( $terms as $term )
  861. $values[] = $term->slug;
  862. $t['value'] = join(', ', $values);
  863. $form_fields[$taxonomy] = $t;
  864. }
  865. // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  866. // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
  867. $form_fields = array_merge_recursive($form_fields, (array) $errors);
  868. // This was formerly in image_attachment_fields_to_edit().
  869. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  870. $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
  871. if ( empty($alt) )
  872. $alt = '';
  873. $form_fields['post_title']['required'] = true;
  874. $form_fields['image_alt'] = array(
  875. 'value' => $alt,
  876. 'label' => __('Alternative Text'),
  877. 'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
  878. );
  879. $form_fields['align'] = array(
  880. 'label' => __('Alignment'),
  881. 'input' => 'html',
  882. 'html' => image_align_input_fields($post, get_option('image_default_align')),
  883. );
  884. $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
  885. } else {
  886. unset( $form_fields['image_alt'] );
  887. }
  888. $form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post);
  889. return $form_fields;
  890. }
  891. /**
  892. * Retrieve HTML for media items of post gallery.
  893. *
  894. * The HTML markup retrieved will be created for the progress of SWF Upload
  895. * component. Will also create link for showing and hiding the form to modify
  896. * the image attachment.
  897. *
  898. * @since 2.5.0
  899. *
  900. * @param int $post_id Optional. Post ID.
  901. * @param array $errors Errors for attachment, if any.
  902. * @return string
  903. */
  904. function get_media_items( $post_id, $errors ) {
  905. $attachments = array();
  906. if ( $post_id ) {
  907. $post = get_post($post_id);
  908. if ( $post && $post->post_type == 'attachment' )
  909. $attachments = array($post->ID => $post);
  910. else
  911. $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
  912. } else {
  913. if ( is_array($GLOBALS['wp_the_query']->posts) )
  914. foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
  915. $attachments[$attachment->ID] = $attachment;
  916. }
  917. $output = '';
  918. foreach ( (array) $attachments as $id => $attachment ) {
  919. if ( $attachment->post_status == 'trash' )
  920. continue;
  921. if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
  922. $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>";
  923. }
  924. return $output;
  925. }
  926. /**
  927. * Retrieve HTML form for modifying the image attachment.
  928. *
  929. * @since 2.5.0
  930. *
  931. * @param int $attachment_id Attachment ID for modification.
  932. * @param string|array $args Optional. Override defaults.
  933. * @return string HTML form for attachment.
  934. */
  935. function get_media_item( $attachment_id, $args = null ) {
  936. global $redir_tab;
  937. if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
  938. $thumb_url = $thumb_url[0];
  939. else
  940. $thumb_url = false;
  941. $post = get_post( $attachment_id );
  942. $current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
  943. $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 );
  944. $args = wp_parse_args( $args, $default_args );
  945. $args = apply_filters( 'get_media_item_args', $args );
  946. extract( $args, EXTR_SKIP );
  947. $toggle_on = __( 'Show' );
  948. $toggle_off = __( 'Hide' );
  949. $filename = esc_html( wp_basename( $post->guid ) );
  950. $title = esc_attr( $post->post_title );
  951. if ( $_tags = get_the_tags( $attachment_id ) ) {
  952. foreach ( $_tags as $tag )
  953. $tags[] = $tag->name;
  954. $tags = esc_attr( join( ', ', $tags ) );
  955. }
  956. $post_mime_types = get_post_mime_types();
  957. $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
  958. $type = array_shift( $keys );
  959. $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
  960. $form_fields = get_attachment_fields_to_edit( $post, $errors );
  961. if ( $toggle ) {
  962. $class = empty( $errors ) ? 'startclosed' : 'startopen';
  963. $toggle_links = "
  964. <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
  965. <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
  966. } else {
  967. $class = '';
  968. $toggle_links = '';
  969. }
  970. $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
  971. $display_title = $show_title ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '&hellip;' ) . "</span></div>" : '';
  972. $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
  973. $order = '';
  974. foreach ( $form_fields as $key => $val ) {
  975. if ( 'menu_order' == $key ) {
  976. if ( $gallery )
  977. $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>";
  978. else
  979. $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
  980. unset( $form_fields['menu_order'] );
  981. break;
  982. }
  983. }
  984. $media_dims = '';
  985. $meta = wp_get_attachment_metadata( $post->ID );
  986. if ( isset( $meta['width'], $meta['height'] ) )
  987. $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  988. $media_dims = apply_filters( 'media_meta', $media_dims, $post );
  989. $image_edit_button = '';
  990. if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  991. $nonce = wp_create_nonce( "image_editor-$post->ID" );
  992. $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>";
  993. }
  994. $attachment_url = get_permalink( $attachment_id );
  995. $item = "
  996. $type_html
  997. $toggle_links
  998. $order
  999. $display_title
  1000. <table class='slidetoggle describe $class'>
  1001. <thead class='media-item-info' id='media-head-$post->ID'>
  1002. <tr valign='top'>
  1003. <td class='A1B1' id='thumbnail-head-$post->ID'>
  1004. <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
  1005. <p>$image_edit_button</p>
  1006. </td>
  1007. <td>
  1008. <p><strong>" . __('File name:') . "</strong> $filename</p>
  1009. <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
  1010. <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( get_option('date_format'), $post->post_date ). '</p>';
  1011. if ( !empty( $media_dims ) )
  1012. $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
  1013. $item .= "</td></tr>\n";
  1014. $item .= "
  1015. </thead>
  1016. <tbody>
  1017. <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>
  1018. <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n";
  1019. $defaults = array(
  1020. 'input' => 'text',
  1021. 'required' => false,
  1022. 'value' => '',
  1023. 'extra_rows' => array(),
  1024. );
  1025. if ( $send )
  1026. $send = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
  1027. if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
  1028. if ( !EMPTY_TRASH_DAYS ) {
  1029. $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>';
  1030. } elseif ( !MEDIA_TRASH ) {
  1031. $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
  1032. <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'><p>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' ), $filename ) . "</p>
  1033. <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>
  1034. <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
  1035. </div>";
  1036. } else {
  1037. $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>
  1038. <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>";
  1039. }
  1040. } else {
  1041. $delete = '';
  1042. }
  1043. $thumbnail = '';
  1044. $calling_post_id = 0;
  1045. if ( isset( $_GET['post_id'] ) )
  1046. $calling_post_id = absint( $_GET['post_id'] );
  1047. elseif ( isset( $_POST ) && count( $_POST ) ) // Like for async-upload where $_GET['post_id'] isn't set
  1048. $calling_post_id = $post->post_parent;
  1049. if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
  1050. && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
  1051. $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
  1052. $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>";
  1053. }
  1054. if ( ( $send || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) )
  1055. $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send $thumbnail $delete</td></tr>\n" );
  1056. $hidden_fields = array();
  1057. foreach ( $form_fields as $id => $field ) {
  1058. if ( $id[0] == '_' )
  1059. continue;
  1060. if ( !empty( $field['tr'] ) ) {
  1061. $item .= $field['tr'];
  1062. continue;
  1063. }
  1064. $field = array_merge( $defaults, $field );
  1065. $name = "attachments[$attachment_id][$id]";
  1066. if ( $field['input'] == 'hidden' ) {
  1067. $hidden_fields[$name] = $field['value'];
  1068. continue;
  1069. }
  1070. $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
  1071. $aria_required = $field['required'] ? " aria-required='true' " : '';
  1072. $class = $id;
  1073. $class .= $field['required'] ? ' form-required' : '';
  1074. $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'>";
  1075. if ( !empty( $field[ $field['input'] ] ) )
  1076. $item .= $field[ $field['input'] ];
  1077. elseif ( $field['input'] == 'textarea' ) {
  1078. if ( 'post_content' == $id && user_can_richedit() ) {
  1079. // sanitize_post() skips the post_content when user_can_richedit
  1080. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1081. }
  1082. // post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit()
  1083. $item .= "<textarea id='$name' name='$name' $aria_required>" . $field['value'] . '</textarea>';
  1084. } else {
  1085. $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
  1086. }
  1087. if ( !empty( $field['helps'] ) )
  1088. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1089. $item .= "</td>\n\t\t</tr>\n";
  1090. $extra_rows = array();
  1091. if ( !empty( $field['errors'] ) )
  1092. foreach ( array_unique( (array) $field['errors'] ) as $error )
  1093. $extra_rows['error'][] = $error;
  1094. if ( !empty( $field['extra_rows'] ) )
  1095. foreach ( $field['extra_rows'] as $class => $rows )
  1096. foreach ( (array) $rows as $html )
  1097. $extra_rows[$class][] = $html;
  1098. foreach ( $extra_rows as $class => $rows )
  1099. foreach ( $rows as $html )
  1100. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1101. }
  1102. if ( !empty( $form_fields['_final'] ) )
  1103. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1104. $item .= "\t</tbody>\n";
  1105. $item .= "\t</table>\n";
  1106. foreach ( $hidden_fields as $name => $value )
  1107. $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
  1108. if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
  1109. $parent = (int) $_REQUEST['post_id'];
  1110. $parent_name = "attachments[$attachment_id][post_parent]";
  1111. $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
  1112. }
  1113. return $item;
  1114. }
  1115. function get_compat_media_markup( $attachment_id, $args = null ) {
  1116. $post = get_post( $attachment_id );
  1117. $default_args = array(
  1118. 'errors' => null,
  1119. 'in_modal' => false,
  1120. );
  1121. $user_can_edit = current_user_can( 'edit_post', $attachment_id );
  1122. $args = wp_parse_args( $args, $default_args );
  1123. $args = apply_filters( 'get_media_item_args', $args );
  1124. $form_fields = array();
  1125. if ( $args['in_modal'] ) {
  1126. foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  1127. $t = (array) get_taxonomy($taxonomy);
  1128. if ( ! $t['public'] || ! $t['show_ui'] )
  1129. continue;
  1130. if ( empty($t['label']) )
  1131. $t['label'] = $taxonomy;
  1132. if ( empty($t['args']) )
  1133. $t['args'] = array();
  1134. $terms = get_object_term_cache($post->ID, $taxonomy);
  1135. if ( false === $terms )
  1136. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  1137. $values = array();
  1138. foreach ( $terms as $term )
  1139. $values[] = $term->slug;
  1140. $t['value'] = join(', ', $values);
  1141. $t['taxonomy'] = true;
  1142. $form_fields[$taxonomy] = $t;
  1143. }
  1144. }
  1145. // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  1146. // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
  1147. $form_fields = array_merge_recursive($form_fields, (array) $args['errors'] );
  1148. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1149. unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],
  1150. $form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],
  1151. $form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] );
  1152. $media_meta = apply_filters( 'media_meta', '', $post );
  1153. $defaults = array(
  1154. 'input' => 'text',
  1155. 'required' => false,
  1156. 'value' => '',
  1157. 'extra_rows' => array(),
  1158. 'show_in_edit' => true,
  1159. 'show_in_modal' => true,
  1160. );
  1161. $hidden_fields = array();
  1162. $item = '';
  1163. foreach ( $form_fields as $id => $field ) {
  1164. if ( $id[0] == '_' )
  1165. continue;
  1166. $name = "attachments[$attachment_id][$id]";
  1167. $id_attr = "attachments-$attachment_id-$id";
  1168. if ( !empty( $field['tr'] ) ) {
  1169. $item .= $field['tr'];
  1170. continue;
  1171. }
  1172. $field = array_merge( $defaults, $field );
  1173. if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) )
  1174. continue;
  1175. if ( $field['input'] == 'hidden' ) {
  1176. $hidden_fields[$name] = $field['value'];
  1177. continue;
  1178. }
  1179. $readonly = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
  1180. $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
  1181. $aria_required = $field['required'] ? " aria-required='true' " : '';
  1182. $class = 'compat-field-' . $id;
  1183. $class .= $field['required'] ? ' form-required' : '';
  1184. $item .= "\t\t<tr class='$class'>";
  1185. $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>";
  1186. $item .= "</th>\n\t\t\t<td class='field'>";
  1187. if ( !empty( $field[ $field['input'] ] ) )
  1188. $item .= $field[ $field['input'] ];
  1189. elseif ( $field['input'] == 'textarea' ) {
  1190. if ( 'post_content' == $id && user_can_richedit() ) {
  1191. // sanitize_post() skips the post_content when user_can_richedit
  1192. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1193. }
  1194. $item .= "<textarea id='$id_attr' name='$name' $aria_required>" . $field['value'] . '</textarea>';
  1195. } else {
  1196. $item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly $aria_required />";
  1197. }
  1198. if ( !empty( $field['helps'] ) )
  1199. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1200. $item .= "</td>\n\t\t</tr>\n";
  1201. $extra_rows = array();
  1202. if ( !empty( $field['errors'] ) )
  1203. foreach ( array_unique( (array) $field['errors'] ) as $error )
  1204. $extra_rows['error'][] = $error;
  1205. if ( !empty( $field['extra_rows'] ) )
  1206. foreach ( $field['extra_rows'] as $class => $rows )
  1207. foreach ( (array) $rows as $html )
  1208. $extra_rows[$class][] = $html;
  1209. foreach ( $extra_rows as $class => $rows )
  1210. foreach ( $rows as $html )
  1211. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1212. }
  1213. if ( !empty( $form_fields['_final'] ) )
  1214. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1215. if ( $item )
  1216. $item = '<table class="compat-attachment-fields">' . $item . '</table>';
  1217. foreach ( $hidden_fields as $hidden_field => $value ) {
  1218. $item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n";
  1219. }
  1220. if ( $item )
  1221. $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr( $post->menu_order ) . '" />' . $item;
  1222. return array(
  1223. 'item' => $item,
  1224. 'meta' => $media_meta,
  1225. );
  1226. }
  1227. /**
  1228. * {@internal Missing Short Description}}
  1229. *
  1230. * @since 2.5.0
  1231. */
  1232. function media_upload_header() {
  1233. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1234. echo '<script type="text/javascript">post_id = ' . $post_id . ";</script>\n";
  1235. if ( empty( $_GET['chromeless'] ) ) {
  1236. echo '<div id="media-upload-header">';
  1237. the_media_upload_tabs();
  1238. echo '</div>';
  1239. }
  1240. }
  1241. /**
  1242. * {@internal Missing Short Description}}
  1243. *
  1244. * @since 2.5.0
  1245. *
  1246. * @param unknown_type $errors
  1247. */
  1248. function media_upload_form( $errors = null ) {
  1249. global $type, $tab, $pagenow, $is_IE, $is_opera;
  1250. if ( ! _device_can_upload() ) {
  1251. echo '<p>' . sprintf( __('The web browser on your device cannot be used to upload files. You may be able to use the <a href="%s">native app for your device</a> instead.'), 'http://wordpress.org/mobile/' ) . '</p>';
  1252. return;
  1253. }
  1254. $upload_action_url = admin_url('async-upload.php');
  1255. $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
  1256. $_type = isset($type) ? $type : '';
  1257. $_tab = isset($tab) ? $tab : '';
  1258. $upload_size_unit = $max_upload_size = wp_max_upload_size();
  1259. $sizes = array( 'KB', 'MB', 'GB' );
  1260. for ( $u = -1; $upload_si