PageRenderTime 69ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/AFelipeTrujillo/goblog
PHP | 2986 lines | 2919 code | 18 blank | 49 comment | 12 complexity | cbb0aa412eff4676b73624c816c082d5 MD5 | raw file
Possible License(s): GPL-2.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. /**
  23. * Filter the available tabs in the legacy (pre-3.5.0) media popup.
  24. *
  25. * @since 2.5.0
  26. *
  27. * @param array $_default_tabs An array of media tabs.
  28. */
  29. return apply_filters( 'media_upload_tabs', $_default_tabs );
  30. }
  31. /**
  32. * Adds the gallery tab back to the tabs array if post has image attachments
  33. *
  34. * @since 2.5.0
  35. *
  36. * @param array $tabs
  37. * @return array $tabs with gallery if post has image attachment
  38. */
  39. function update_gallery_tab($tabs) {
  40. global $wpdb;
  41. if ( !isset($_REQUEST['post_id']) ) {
  42. unset($tabs['gallery']);
  43. return $tabs;
  44. }
  45. $post_id = intval($_REQUEST['post_id']);
  46. if ( $post_id )
  47. $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 ) ) );
  48. if ( empty($attachments) ) {
  49. unset($tabs['gallery']);
  50. return $tabs;
  51. }
  52. $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
  53. return $tabs;
  54. }
  55. add_filter('media_upload_tabs', 'update_gallery_tab');
  56. /**
  57. * {@internal Missing Short Description}}
  58. *
  59. * @since 2.5.0
  60. */
  61. function the_media_upload_tabs() {
  62. global $redir_tab;
  63. $tabs = media_upload_tabs();
  64. $default = 'type';
  65. if ( !empty($tabs) ) {
  66. echo "<ul id='sidemenu'>\n";
  67. if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) ) {
  68. $current = $redir_tab;
  69. } elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) ) {
  70. $current = $_GET['tab'];
  71. } else {
  72. /** This filter is documented in wp-admin/media-upload.php */
  73. $current = apply_filters( 'media_upload_default_tab', $default );
  74. }
  75. foreach ( $tabs as $callback => $text ) {
  76. $class = '';
  77. if ( $current == $callback )
  78. $class = " class='current'";
  79. $href = add_query_arg(array('tab' => $callback, 's' => false, 'paged' => false, 'post_mime_type' => false, 'm' => false));
  80. $link = "<a href='" . esc_url($href) . "'$class>$text</a>";
  81. echo "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
  82. }
  83. echo "</ul>\n";
  84. }
  85. }
  86. /**
  87. * {@internal Missing Short Description}}
  88. *
  89. * @since 2.5.0
  90. *
  91. * @param integer $id image attachment id
  92. * @param string $caption image caption
  93. * @param string $alt image alt attribute
  94. * @param string $title image title attribute
  95. * @param string $align image css alignment property
  96. * @param string $url image src url
  97. * @param string|bool $rel image rel attribute
  98. * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
  99. * @return string the html to insert into editor
  100. */
  101. function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
  102. $html = get_image_tag($id, $alt, '', $align, $size);
  103. $rel = $rel ? ' rel="attachment wp-att-' . esc_attr($id).'"' : '';
  104. if ( $url )
  105. $html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
  106. /**
  107. * Filter the image HTML markup to send to the editor.
  108. *
  109. * @since 2.5.0
  110. *
  111. * @param string $html The image HTML markup to send.
  112. * @param int $id The attachment id.
  113. * @param string $caption The image caption.
  114. * @param string $title The image title.
  115. * @param string $align The image alignment.
  116. * @param string $url The image source URL.
  117. * @param string $size The image size.
  118. * @param string $alt The image alternative, or alt, text.
  119. */
  120. $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
  121. return $html;
  122. }
  123. /**
  124. * Adds image shortcode with caption to editor
  125. *
  126. * @since 2.6.0
  127. *
  128. * @param string $html
  129. * @param integer $id
  130. * @param string $caption image caption
  131. * @param string $alt image alt attribute
  132. * @param string $title image title attribute
  133. * @param string $align image css alignment property
  134. * @param string $url image src url
  135. * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
  136. * @return string
  137. */
  138. function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
  139. /**
  140. * Filter whether to disable captions.
  141. *
  142. * Prevents image captions from being appended to image HTML when inserted into the editor.
  143. *
  144. * @since 2.6.0
  145. *
  146. * @param bool $bool Whether to disable appending captions. Returning true to the filter
  147. * will disable captions. Default empty string.
  148. */
  149. if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
  150. return $html;
  151. $id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
  152. if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) )
  153. return $html;
  154. $width = $matches[1];
  155. $caption = str_replace( array("\r\n", "\r"), "\n", $caption);
  156. $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
  157. // convert any remaining line breaks to <br>
  158. $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
  159. $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
  160. if ( empty($align) )
  161. $align = 'none';
  162. $shcode = '[caption id="' . $id . '" align="align' . $align . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]';
  163. /**
  164. * Filter the image HTML markup including the caption shortcode.
  165. *
  166. * @since 2.6.0
  167. *
  168. * @param string $shcode The image HTML markup with caption shortcode.
  169. * @param string $html The image HTML markup.
  170. */
  171. return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
  172. }
  173. add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
  174. /**
  175. * Private preg_replace callback used in image_add_caption()
  176. *
  177. * @access private
  178. * @since 3.4.0
  179. */
  180. function _cleanup_image_add_caption( $matches ) {
  181. // remove any line breaks from inside the tags
  182. return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
  183. }
  184. /**
  185. * Adds image html to editor
  186. *
  187. * @since 2.5.0
  188. *
  189. * @param string $html
  190. */
  191. function media_send_to_editor($html) {
  192. ?>
  193. <script type="text/javascript">
  194. /* <![CDATA[ */
  195. var win = window.dialogArguments || opener || parent || top;
  196. win.send_to_editor('<?php echo addslashes($html); ?>');
  197. /* ]]> */
  198. </script>
  199. <?php
  200. exit;
  201. }
  202. /**
  203. * This handles the file upload POST itself, creating the attachment post.
  204. *
  205. * @since 2.5.0
  206. *
  207. * @param string $file_id Index into the {@link $_FILES} array of the upload
  208. * @param int $post_id The post ID the media is associated with
  209. * @param array $post_data allows you to overwrite some of the attachment
  210. * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
  211. * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
  212. */
  213. function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) {
  214. $time = current_time('mysql');
  215. if ( $post = get_post($post_id) ) {
  216. if ( substr( $post->post_date, 0, 4 ) > 0 )
  217. $time = $post->post_date;
  218. }
  219. $name = $_FILES[$file_id]['name'];
  220. $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
  221. if ( isset($file['error']) )
  222. return new WP_Error( 'upload_error', $file['error'] );
  223. $name_parts = pathinfo($name);
  224. $name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );
  225. $url = $file['url'];
  226. $type = $file['type'];
  227. $file = $file['file'];
  228. $title = $name;
  229. $content = '';
  230. if ( preg_match( '#^audio#', $type ) ) {
  231. $meta = wp_read_audio_metadata( $file );
  232. if ( ! empty( $meta['title'] ) )
  233. $title = $meta['title'];
  234. $content = '';
  235. if ( ! empty( $title ) ) {
  236. if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
  237. /* translators: 1: audio track title, 2: album title, 3: artist name */
  238. $content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
  239. } else if ( ! empty( $meta['album'] ) ) {
  240. /* translators: 1: audio track title, 2: album title */
  241. $content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
  242. } else if ( ! empty( $meta['artist'] ) ) {
  243. /* translators: 1: audio track title, 2: artist name */
  244. $content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
  245. } else {
  246. $content .= sprintf( __( '"%s".' ), $title );
  247. }
  248. } else if ( ! empty( $meta['album'] ) ) {
  249. if ( ! empty( $meta['artist'] ) ) {
  250. /* translators: 1: audio album title, 2: artist name */
  251. $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
  252. } else {
  253. $content .= $meta['album'] . '.';
  254. }
  255. } else if ( ! empty( $meta['artist'] ) ) {
  256. $content .= $meta['artist'] . '.';
  257. }
  258. if ( ! empty( $meta['year'] ) )
  259. $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
  260. if ( ! empty( $meta['track_number'] ) ) {
  261. $track_number = explode( '/', $meta['track_number'] );
  262. if ( isset( $track_number[1] ) )
  263. $content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
  264. else
  265. $content .= ' ' . sprintf( __( 'Track %1$s.' ), number_format_i18n( $track_number[0] ) );
  266. }
  267. if ( ! empty( $meta['genre'] ) )
  268. $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
  269. // use image exif/iptc data for title and caption defaults if possible
  270. } elseif ( $image_meta = @wp_read_image_metadata( $file ) ) {
  271. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
  272. $title = $image_meta['title'];
  273. if ( trim( $image_meta['caption'] ) )
  274. $content = $image_meta['caption'];
  275. }
  276. // Construct the attachment array
  277. $attachment = array_merge( array(
  278. 'post_mime_type' => $type,
  279. 'guid' => $url,
  280. 'post_parent' => $post_id,
  281. 'post_title' => $title,
  282. 'post_content' => $content,
  283. ), $post_data );
  284. // This should never be set as it would then overwrite an existing attachment.
  285. if ( isset( $attachment['ID'] ) )
  286. unset( $attachment['ID'] );
  287. // Save the data
  288. $id = wp_insert_attachment($attachment, $file, $post_id);
  289. if ( !is_wp_error($id) ) {
  290. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  291. }
  292. return $id;
  293. }
  294. /**
  295. * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
  296. *
  297. * @since 2.6.0
  298. *
  299. * @param array $file_array Array similar to a {@link $_FILES} upload array
  300. * @param int $post_id The post ID the media is associated with
  301. * @param string $desc Description of the sideloaded file
  302. * @param array $post_data allows you to overwrite some of the attachment
  303. * @return int|object The ID of the attachment or a WP_Error on failure
  304. */
  305. function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
  306. $overrides = array('test_form'=>false);
  307. $time = current_time( 'mysql' );
  308. if ( $post = get_post( $post_id ) ) {
  309. if ( substr( $post->post_date, 0, 4 ) > 0 )
  310. $time = $post->post_date;
  311. }
  312. $file = wp_handle_sideload( $file_array, $overrides, $time );
  313. if ( isset($file['error']) )
  314. return new WP_Error( 'upload_error', $file['error'] );
  315. $url = $file['url'];
  316. $type = $file['type'];
  317. $file = $file['file'];
  318. $title = preg_replace('/\.[^.]+$/', '', basename($file));
  319. $content = '';
  320. // use image exif/iptc data for title and caption defaults if possible
  321. if ( $image_meta = @wp_read_image_metadata($file) ) {
  322. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
  323. $title = $image_meta['title'];
  324. if ( trim( $image_meta['caption'] ) )
  325. $content = $image_meta['caption'];
  326. }
  327. if ( isset( $desc ) )
  328. $title = $desc;
  329. // Construct the attachment array
  330. $attachment = array_merge( array(
  331. 'post_mime_type' => $type,
  332. 'guid' => $url,
  333. 'post_parent' => $post_id,
  334. 'post_title' => $title,
  335. 'post_content' => $content,
  336. ), $post_data );
  337. // This should never be set as it would then overwrite an existing attachment.
  338. if ( isset( $attachment['ID'] ) )
  339. unset( $attachment['ID'] );
  340. // Save the attachment metadata
  341. $id = wp_insert_attachment($attachment, $file, $post_id);
  342. if ( !is_wp_error($id) )
  343. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  344. return $id;
  345. }
  346. /**
  347. * Adds the iframe to display content for the media upload page
  348. *
  349. * @since 2.5.0
  350. *
  351. * @param array $content_func
  352. */
  353. function wp_iframe($content_func /* ... */) {
  354. _wp_admin_html_begin();
  355. ?>
  356. <title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
  357. <?php
  358. wp_enqueue_style( 'colors' );
  359. // Check callback name for 'media'
  360. if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
  361. || ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
  362. wp_enqueue_style( 'media' );
  363. wp_enqueue_style( 'ie' );
  364. ?>
  365. <script type="text/javascript">
  366. //<![CDATA[
  367. 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();}}};
  368. var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
  369. isRtl = <?php echo (int) is_rtl(); ?>;
  370. //]]>
  371. </script>
  372. <?php
  373. /** This action is documented in wp-admin/admin-header.php */
  374. do_action( 'admin_enqueue_scripts', 'media-upload-popup' );
  375. /**
  376. * Fires when admin styles enqueued for the legacy (pre-3.5.0) media upload popup are printed.
  377. *
  378. * @since 2.9.0
  379. */
  380. do_action( 'admin_print_styles-media-upload-popup' );
  381. /** This action is documented in wp-admin/admin-header.php */
  382. do_action( 'admin_print_styles' );
  383. /**
  384. * Fires when admin scripts enqueued for the legacy (pre-3.5.0) media upload popup are printed.
  385. *
  386. * @since 2.9.0
  387. */
  388. do_action( 'admin_print_scripts-media-upload-popup' );
  389. /** This action is documented in wp-admin/admin-header.php */
  390. do_action( 'admin_print_scripts' );
  391. /**
  392. * Fires when scripts enqueued for the admin header for the legacy (pre-3.5.0)
  393. * media upload popup are printed.
  394. *
  395. * @since 2.9.0
  396. */
  397. do_action( 'admin_head-media-upload-popup' );
  398. /** This action is documented in wp-admin/admin-header.php */
  399. do_action( 'admin_head' );
  400. if ( is_string( $content_func ) ) {
  401. /**
  402. * Fires in the admin header for each specific form tab in the legacy
  403. * (pre-3.5.0) media upload popup.
  404. *
  405. * The dynamic portion of the hook, $content_func, refers to the form
  406. * callback for the media upload type. Possible values include
  407. * 'media_upload_type_form', 'media_upload_type_url_form', and
  408. * 'media_upload_library_form'.
  409. *
  410. * @since 2.5.0
  411. */
  412. do_action( "admin_head_{$content_func}" );
  413. }
  414. ?>
  415. </head>
  416. <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?> class="wp-core-ui no-js">
  417. <script type="text/javascript">
  418. document.body.className = document.body.className.replace('no-js', 'js');
  419. </script>
  420. <?php
  421. $args = func_get_args();
  422. $args = array_slice($args, 1);
  423. call_user_func_array($content_func, $args);
  424. /** This action is documented in wp-admin/admin-footer.php */
  425. do_action( 'admin_print_footer_scripts' );
  426. ?>
  427. <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
  428. </body>
  429. </html>
  430. <?php
  431. }
  432. /**
  433. * Adds the media button to the editor
  434. *
  435. * @since 2.5.0
  436. *
  437. * @param string $editor_id
  438. */
  439. function media_buttons($editor_id = 'content') {
  440. $post = get_post();
  441. if ( ! $post && ! empty( $GLOBALS['post_ID'] ) )
  442. $post = $GLOBALS['post_ID'];
  443. wp_enqueue_media( array(
  444. 'post' => $post
  445. ) );
  446. $img = '<span class="wp-media-buttons-icon"></span> ';
  447. 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>';
  448. /**
  449. * Filter the legacy (pre-3.5.0) media buttons.
  450. *
  451. * @since 2.5.0
  452. * @deprecated 3.5.0 Use 'media_buttons' action instead.
  453. *
  454. * @param string $string Media buttons context. Default empty.
  455. */
  456. $legacy_filter = apply_filters( 'media_buttons_context', '' );
  457. if ( $legacy_filter ) {
  458. // #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
  459. if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) )
  460. $legacy_filter .= '</a>';
  461. echo $legacy_filter;
  462. }
  463. }
  464. add_action( 'media_buttons', 'media_buttons' );
  465. function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
  466. global $post_ID;
  467. if ( empty( $post_id ) )
  468. $post_id = $post_ID;
  469. $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
  470. if ( $type && 'media' != $type )
  471. $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
  472. if ( ! empty( $tab ) )
  473. $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
  474. /**
  475. * Filter the upload iframe source URL for a specific media type.
  476. *
  477. * The dynamic portion of the hook name, $type, refers to the type
  478. * of media uploaded.
  479. *
  480. * @since 3.0.0
  481. *
  482. * @param string $upload_iframe_src The upload iframe source URL by type.
  483. */
  484. $upload_iframe_src = apply_filters( $type . '_upload_iframe_src', $upload_iframe_src );
  485. return add_query_arg('TB_iframe', true, $upload_iframe_src);
  486. }
  487. /**
  488. * {@internal Missing Short Description}}
  489. *
  490. * @since 2.5.0
  491. *
  492. * @return mixed void|object WP_Error on failure
  493. */
  494. function media_upload_form_handler() {
  495. check_admin_referer('media-form');
  496. $errors = null;
  497. if ( isset($_POST['send']) ) {
  498. $keys = array_keys($_POST['send']);
  499. $send_id = (int) array_shift($keys);
  500. }
  501. if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
  502. $post = $_post = get_post($attachment_id, ARRAY_A);
  503. if ( !current_user_can( 'edit_post', $attachment_id ) )
  504. continue;
  505. if ( isset($attachment['post_content']) )
  506. $post['post_content'] = $attachment['post_content'];
  507. if ( isset($attachment['post_title']) )
  508. $post['post_title'] = $attachment['post_title'];
  509. if ( isset($attachment['post_excerpt']) )
  510. $post['post_excerpt'] = $attachment['post_excerpt'];
  511. if ( isset($attachment['menu_order']) )
  512. $post['menu_order'] = $attachment['menu_order'];
  513. if ( isset($send_id) && $attachment_id == $send_id ) {
  514. if ( isset($attachment['post_parent']) )
  515. $post['post_parent'] = $attachment['post_parent'];
  516. }
  517. /**
  518. * Filter the attachment fields to be saved.
  519. *
  520. * @since 2.5.0
  521. *
  522. * @see wp_get_attachment_metadata()
  523. *
  524. * @param WP_Post $post The WP_Post object.
  525. * @param array $attachment An array of attachment metadata.
  526. */
  527. $post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
  528. if ( isset($attachment['image_alt']) ) {
  529. $image_alt = wp_unslash( $attachment['image_alt'] );
  530. if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) {
  531. $image_alt = wp_strip_all_tags( $image_alt, true );
  532. // update_meta expects slashed
  533. update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
  534. }
  535. }
  536. if ( isset($post['errors']) ) {
  537. $errors[$attachment_id] = $post['errors'];
  538. unset($post['errors']);
  539. }
  540. if ( $post != $_post )
  541. wp_update_post($post);
  542. foreach ( get_attachment_taxonomies($post) as $t ) {
  543. if ( isset($attachment[$t]) )
  544. wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
  545. }
  546. }
  547. if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
  548. <script type="text/javascript">
  549. /* <![CDATA[ */
  550. var win = window.dialogArguments || opener || parent || top;
  551. win.tb_remove();
  552. /* ]]> */
  553. </script>
  554. <?php
  555. exit;
  556. }
  557. if ( isset($send_id) ) {
  558. $attachment = wp_unslash( $_POST['attachments'][$send_id] );
  559. $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
  560. if ( !empty($attachment['url']) ) {
  561. $rel = '';
  562. if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
  563. $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
  564. $html = "<a href='{$attachment['url']}'$rel>$html</a>";
  565. }
  566. /**
  567. * Filter the HTML markup for a media item sent to the editor.
  568. *
  569. * @since 2.5.0
  570. *
  571. * @see wp_get_attachment_metadata()
  572. *
  573. * @param string $html HTML markup for a media item sent to the editor.
  574. * @param int $send_id The first key from the $_POST['send'] data.
  575. * @param array $attachment Array of attachment metadata.
  576. */
  577. $html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );
  578. return media_send_to_editor($html);
  579. }
  580. return $errors;
  581. }
  582. /**
  583. * {@internal Missing Short Description}}
  584. *
  585. * @since 2.5.0
  586. *
  587. * @return mixed
  588. */
  589. function wp_media_upload_handler() {
  590. $errors = array();
  591. $id = 0;
  592. if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
  593. check_admin_referer('media-form');
  594. // Upload File button was clicked
  595. $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
  596. unset($_FILES);
  597. if ( is_wp_error($id) ) {
  598. $errors['upload_error'] = $id;
  599. $id = false;
  600. }
  601. }
  602. if ( !empty($_POST['insertonlybutton']) ) {
  603. $src = $_POST['src'];
  604. if ( !empty($src) && !strpos($src, '://') )
  605. $src = "http://$src";
  606. if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
  607. $title = esc_html( wp_unslash( $_POST['title'] ) );
  608. if ( empty( $title ) )
  609. $title = esc_html( basename( $src ) );
  610. if ( $title && $src )
  611. $html = "<a href='" . esc_url($src) . "'>$title</a>";
  612. $type = 'file';
  613. if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
  614. && ( 'audio' == $ext_type || 'video' == $ext_type ) )
  615. $type = $ext_type;
  616. /**
  617. * Filter the URL sent to the editor for a specific media type.
  618. *
  619. * The dynamic portion of the hook name, $type, refers to the type
  620. * of media being sent.
  621. *
  622. * @since 3.3.0
  623. *
  624. * @param string $html HTML markup sent to the editor.
  625. * @param string $src Media source URL.
  626. * @param string $title Media title.
  627. */
  628. $html = apply_filters( $type . '_send_to_editor_url', $html, esc_url_raw( $src ), $title );
  629. } else {
  630. $align = '';
  631. $alt = esc_attr( wp_unslash( $_POST['alt'] ) );
  632. if ( isset($_POST['align']) ) {
  633. $align = esc_attr( wp_unslash( $_POST['align'] ) );
  634. $class = " class='align$align'";
  635. }
  636. if ( !empty($src) )
  637. $html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";
  638. /**
  639. * Filter the image URL sent to the editor.
  640. *
  641. * @since 2.8.0
  642. *
  643. * @param string $html HTML markup sent to the editor for an image.
  644. * @param string $src Image source URL.
  645. * @param string $alt Image alternate, or alt, text.
  646. * @param string $align The image alignment. Default 'alignnone'. Possible values include
  647. * 'alignleft', 'aligncenter', 'alignright', 'alignnone'.
  648. */
  649. $html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
  650. }
  651. return media_send_to_editor($html);
  652. }
  653. if ( !empty($_POST) ) {
  654. $return = media_upload_form_handler();
  655. if ( is_string($return) )
  656. return $return;
  657. if ( is_array($return) )
  658. $errors = $return;
  659. }
  660. if ( isset($_POST['save']) ) {
  661. $errors['upload_notice'] = __('Saved.');
  662. return media_upload_gallery();
  663. }
  664. if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
  665. $type = 'image';
  666. if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
  667. $type = $_GET['type'];
  668. return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
  669. }
  670. return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
  671. }
  672. /**
  673. * Download an image from the specified URL and attach it to a post.
  674. *
  675. * @since 2.6.0
  676. *
  677. * @param string $file The URL of the image to download
  678. * @param int $post_id The post ID the media is to be associated with
  679. * @param string $desc Optional. Description of the image
  680. * @return string|WP_Error Populated HTML img tag on success
  681. */
  682. function media_sideload_image($file, $post_id, $desc = null) {
  683. if ( ! empty($file) ) {
  684. // Download file to temp location
  685. $tmp = download_url( $file );
  686. // Set variables for storage
  687. // fix file filename for query strings
  688. preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
  689. $file_array['name'] = basename($matches[0]);
  690. $file_array['tmp_name'] = $tmp;
  691. // If error storing temporarily, unlink
  692. if ( is_wp_error( $tmp ) ) {
  693. @unlink($file_array['tmp_name']);
  694. $file_array['tmp_name'] = '';
  695. }
  696. // do the validation and storage stuff
  697. $id = media_handle_sideload( $file_array, $post_id, $desc );
  698. // If error storing permanently, unlink
  699. if ( is_wp_error($id) ) {
  700. @unlink($file_array['tmp_name']);
  701. return $id;
  702. }
  703. $src = wp_get_attachment_url( $id );
  704. }
  705. // Finally check to make sure the file has been saved, then return the html
  706. if ( ! empty($src) ) {
  707. $alt = isset($desc) ? esc_attr($desc) : '';
  708. $html = "<img src='$src' alt='$alt' />";
  709. return $html;
  710. }
  711. }
  712. /**
  713. * {@internal Missing Short Description}}
  714. *
  715. * @since 2.5.0
  716. *
  717. * @return unknown
  718. */
  719. function media_upload_gallery() {
  720. $errors = array();
  721. if ( !empty($_POST) ) {
  722. $return = media_upload_form_handler();
  723. if ( is_string($return) )
  724. return $return;
  725. if ( is_array($return) )
  726. $errors = $return;
  727. }
  728. wp_enqueue_script('admin-gallery');
  729. return wp_iframe( 'media_upload_gallery_form', $errors );
  730. }
  731. /**
  732. * {@internal Missing Short Description}}
  733. *
  734. * @since 2.5.0
  735. *
  736. * @return unknown
  737. */
  738. function media_upload_library() {
  739. $errors = array();
  740. if ( !empty($_POST) ) {
  741. $return = media_upload_form_handler();
  742. if ( is_string($return) )
  743. return $return;
  744. if ( is_array($return) )
  745. $errors = $return;
  746. }
  747. return wp_iframe( 'media_upload_library_form', $errors );
  748. }
  749. /**
  750. * Retrieve HTML for the image alignment radio buttons with the specified one checked.
  751. *
  752. * @since 2.7.0
  753. *
  754. * @param object $post
  755. * @param string $checked
  756. * @return string
  757. */
  758. function image_align_input_fields( $post, $checked = '' ) {
  759. if ( empty($checked) )
  760. $checked = get_user_setting('align', 'none');
  761. $alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
  762. if ( !array_key_exists( (string) $checked, $alignments ) )
  763. $checked = 'none';
  764. $out = array();
  765. foreach ( $alignments as $name => $label ) {
  766. $name = esc_attr($name);
  767. $out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
  768. ( $checked == $name ? " checked='checked'" : "" ) .
  769. " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
  770. }
  771. return join("\n", $out);
  772. }
  773. /**
  774. * Retrieve HTML for the size radio buttons with the specified one checked.
  775. *
  776. * @since 2.7.0
  777. *
  778. * @param object $post
  779. * @param bool|string $check
  780. * @return array
  781. */
  782. function image_size_input_fields( $post, $check = '' ) {
  783. /**
  784. * Filter the names and labels of the default image sizes.
  785. *
  786. * @since 3.3.0
  787. *
  788. * @param array $size_names Array of image sizes and their names. Default values
  789. * include 'Thumbnail', 'Medium', 'Large', 'Full Size'.
  790. */
  791. $size_names = apply_filters( 'image_size_names_choose', array(
  792. 'thumbnail' => __( 'Thumbnail' ),
  793. 'medium' => __( 'Medium' ),
  794. 'large' => __( 'Large' ),
  795. 'full' => __( 'Full Size' )
  796. ) );
  797. if ( empty($check) )
  798. $check = get_user_setting('imgsize', 'medium');
  799. foreach ( $size_names as $size => $label ) {
  800. $downsize = image_downsize($post->ID, $size);
  801. $checked = '';
  802. // is this size selectable?
  803. $enabled = ( $downsize[3] || 'full' == $size );
  804. $css_id = "image-size-{$size}-{$post->ID}";
  805. // if this size is the default but that's not available, don't select it
  806. if ( $size == $check ) {
  807. if ( $enabled )
  808. $checked = " checked='checked'";
  809. else
  810. $check = '';
  811. } elseif ( !$check && $enabled && 'thumbnail' != $size ) {
  812. // if $check is not enabled, default to the first available size that's bigger than a thumbnail
  813. $check = $size;
  814. $checked = " checked='checked'";
  815. }
  816. $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 />";
  817. $html .= "<label for='{$css_id}'>$label</label>";
  818. // only show the dimensions if that choice is available
  819. if ( $enabled )
  820. $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
  821. $html .= '</div>';
  822. $out[] = $html;
  823. }
  824. return array(
  825. 'label' => __('Size'),
  826. 'input' => 'html',
  827. 'html' => join("\n", $out),
  828. );
  829. }
  830. /**
  831. * Retrieve HTML for the Link URL buttons with the default link type as specified.
  832. *
  833. * @since 2.7.0
  834. *
  835. * @param object $post
  836. * @param string $url_type
  837. * @return string
  838. */
  839. function image_link_input_fields($post, $url_type = '') {
  840. $file = wp_get_attachment_url($post->ID);
  841. $link = get_attachment_link($post->ID);
  842. if ( empty($url_type) )
  843. $url_type = get_user_setting('urlbutton', 'post');
  844. $url = '';
  845. if ( $url_type == 'file' )
  846. $url = $file;
  847. elseif ( $url_type == 'post' )
  848. $url = $link;
  849. return "
  850. <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
  851. <button type='button' class='button urlnone' data-link-url=''>" . __('None') . "</button>
  852. <button type='button' class='button urlfile' data-link-url='" . esc_attr($file) . "'>" . __('File URL') . "</button>
  853. <button type='button' class='button urlpost' data-link-url='" . esc_attr($link) . "'>" . __('Attachment Post URL') . "</button>
  854. ";
  855. }
  856. function wp_caption_input_textarea($edit_post) {
  857. // post data is already escaped
  858. $name = "attachments[{$edit_post->ID}][post_excerpt]";
  859. return '<textarea name="' . $name . '" id="' . $name . '">' . $edit_post->post_excerpt . '</textarea>';
  860. }
  861. /**
  862. * {@internal Missing Short Description}}
  863. *
  864. * @since 2.5.0
  865. *
  866. * @param array $form_fields
  867. * @param object $post
  868. * @return array
  869. */
  870. function image_attachment_fields_to_edit($form_fields, $post) {
  871. return $form_fields;
  872. }
  873. /**
  874. * {@internal Missing Short Description}}
  875. *
  876. * @since 2.5.0
  877. *
  878. * @param array $form_fields
  879. * @param object $post {@internal $post not used}}
  880. * @return array
  881. */
  882. function media_single_attachment_fields_to_edit( $form_fields, $post ) {
  883. unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
  884. return $form_fields;
  885. }
  886. /**
  887. * {@internal Missing Short Description}}
  888. *
  889. * @since 2.8.0
  890. *
  891. * @param array $form_fields
  892. * @param object $post {@internal $post not used}}
  893. * @return array
  894. */
  895. function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
  896. unset($form_fields['image_url']);
  897. return $form_fields;
  898. }
  899. /**
  900. * Filters input from media_upload_form_handler() and assigns a default
  901. * post_title from the file name if none supplied.
  902. *
  903. * Illustrates the use of the attachment_fields_to_save filter
  904. * which can be used to add default values to any field before saving to DB.
  905. *
  906. * @since 2.5.0
  907. *
  908. * @param object $post
  909. * @param array $attachment {@internal $attachment not used}}
  910. * @return array
  911. */
  912. function image_attachment_fields_to_save( $post, $attachment ) {
  913. if ( substr( $post['post_mime_type'], 0, 5 ) == 'image' ) {
  914. if ( strlen( trim( $post['post_title'] ) ) == 0 ) {
  915. $attachment_url = ( isset( $post['attachment_url'] ) ) ? $post['attachment_url'] : $post['guid'];
  916. $post['post_title'] = preg_replace( '/\.\w+$/', '', wp_basename( $attachment_url ) );
  917. $post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
  918. }
  919. }
  920. return $post;
  921. }
  922. add_filter( 'attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2 );
  923. /**
  924. * {@internal Missing Short Description}}
  925. *
  926. * @since 2.5.0
  927. *
  928. * @param string $html
  929. * @param integer $attachment_id
  930. * @param array $attachment
  931. * @return array
  932. */
  933. function image_media_send_to_editor($html, $attachment_id, $attachment) {
  934. $post = get_post($attachment_id);
  935. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  936. $url = $attachment['url'];
  937. $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
  938. $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
  939. $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
  940. $rel = ( $url == get_attachment_link($attachment_id) );
  941. return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
  942. }
  943. return $html;
  944. }
  945. add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
  946. /**
  947. * {@internal Missing Short Description}}
  948. *
  949. * @since 2.5.0
  950. *
  951. * @param object $post
  952. * @param array $errors
  953. * @return array
  954. */
  955. function get_attachment_fields_to_edit($post, $errors = null) {
  956. if ( is_int($post) )
  957. $post = get_post($post);
  958. if ( is_array($post) )
  959. $post = new WP_Post( (object) $post );
  960. $image_url = wp_get_attachment_url($post->ID);
  961. $edit_post = sanitize_post($post, 'edit');
  962. $form_fields = array(
  963. 'post_title' => array(
  964. 'label' => __('Title'),
  965. 'value' => $edit_post->post_title
  966. ),
  967. 'image_alt' => array(),
  968. 'post_excerpt' => array(
  969. 'label' => __('Caption'),
  970. 'input' => 'html',
  971. 'html' => wp_caption_input_textarea($edit_post)
  972. ),
  973. 'post_content' => array(
  974. 'label' => __('Description'),
  975. 'value' => $edit_post->post_content,
  976. 'input' => 'textarea'
  977. ),
  978. 'url' => array(
  979. 'label' => __('Link URL'),
  980. 'input' => 'html',
  981. 'html' => image_link_input_fields($post, get_option('image_default_link_type')),
  982. 'helps' => __('Enter a link URL or click above for presets.')
  983. ),
  984. 'menu_order' => array(
  985. 'label' => __('Order'),
  986. 'value' => $edit_post->menu_order
  987. ),
  988. 'image_url' => array(
  989. 'label' => __('File URL'),
  990. 'input' => 'html',
  991. 'html' => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
  992. 'value' => wp_get_attachment_url($post->ID),
  993. 'helps' => __('Location of the uploaded file.')
  994. )
  995. );
  996. foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  997. $t = (array) get_taxonomy($taxonomy);
  998. if ( ! $t['public'] || ! $t['show_ui'] )
  999. continue;
  1000. if ( empty($t['label']) )
  1001. $t['label'] = $taxonomy;
  1002. if ( empty($t['args']) )
  1003. $t['args'] = array();
  1004. $terms = get_object_term_cache($post->ID, $taxonomy);
  1005. if ( false === $terms )
  1006. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  1007. $values = array();
  1008. foreach ( $terms as $term )
  1009. $values[] = $term->slug;
  1010. $t['value'] = join(', ', $values);
  1011. $form_fields[$taxonomy] = $t;
  1012. }
  1013. // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  1014. // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
  1015. $form_fields = array_merge_recursive($form_fields, (array) $errors);
  1016. // This was formerly in image_attachment_fields_to_edit().
  1017. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  1018. $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
  1019. if ( empty($alt) )
  1020. $alt = '';
  1021. $form_fields['post_title']['required'] = true;
  1022. $form_fields['image_alt'] = array(
  1023. 'value' => $alt,
  1024. 'label' => __('Alternative Text'),
  1025. 'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
  1026. );
  1027. $form_fields['align'] = array(
  1028. 'label' => __('Alignment'),
  1029. 'input' => 'html',
  1030. 'html' => image_align_input_fields($post, get_option('image_default_align')),
  1031. );
  1032. $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
  1033. } else {
  1034. unset( $form_fields['image_alt'] );
  1035. }
  1036. /**
  1037. * Filter the attachment fields to edit.
  1038. *
  1039. * @since 2.5.0
  1040. *
  1041. * @param array $form_fields An array of attachment form fields.
  1042. * @param WP_Post $post The WP_Post attachment object.
  1043. */
  1044. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1045. return $form_fields;
  1046. }
  1047. /**
  1048. * Retrieve HTML for media items of post gallery.
  1049. *
  1050. * The HTML markup retrieved will be created for the progress of SWF Upload
  1051. * component. Will also create link for showing and hiding the form to modify
  1052. * the image attachment.
  1053. *
  1054. * @since 2.5.0
  1055. *
  1056. * @param int $post_id Optional. Post ID.
  1057. * @param array $errors Errors for attachment, if any.
  1058. * @return string
  1059. */
  1060. function get_media_items( $post_id, $errors ) {
  1061. $attachments = array();
  1062. if ( $post_id ) {
  1063. $post = get_post($post_id);
  1064. if ( $post && $post->post_type == 'attachment' )
  1065. $attachments = array($post->ID => $post);
  1066. else
  1067. $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
  1068. } else {
  1069. if ( is_array($GLOBALS['wp_the_query']->posts) )
  1070. foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
  1071. $attachments[$attachment->ID] = $attachment;
  1072. }
  1073. $output = '';
  1074. foreach ( (array) $attachments as $id => $attachment ) {
  1075. if ( $attachment->post_status == 'trash' )
  1076. continue;
  1077. if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
  1078. $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>";
  1079. }
  1080. return $output;
  1081. }
  1082. /**
  1083. * Retrieve HTML form for modifying the image attachment.
  1084. *
  1085. * @since 2.5.0
  1086. *
  1087. * @param int $attachment_id Attachment ID for modification.
  1088. * @param string|array $args Optional. Override defaults.
  1089. * @return string HTML form for attachment.
  1090. */
  1091. function get_media_item( $attachment_id, $args = null ) {
  1092. global $redir_tab;
  1093. if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
  1094. $thumb_url = $thumb_url[0];
  1095. else
  1096. $thumb_url = false;
  1097. $post = get_post( $attachment_id );
  1098. $current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
  1099. $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 );
  1100. $args = wp_parse_args( $args, $default_args );
  1101. /**
  1102. * Filter the arguments used to retrieve an image for the edit image form.
  1103. *
  1104. * @since 3.1.0
  1105. *
  1106. * @see get_media_item
  1107. *
  1108. * @param array $args An array of arguments.
  1109. */
  1110. $args = apply_filters( 'get_media_item_args', $args );
  1111. extract( $args, EXTR_SKIP );
  1112. $toggle_on = __( 'Show' );
  1113. $toggle_off = __( 'Hide' );
  1114. $filename = esc_html( wp_basename( $post->guid ) );
  1115. $title = esc_attr( $post->post_title );
  1116. if ( $_tags = get_the_tags( $attachment_id ) ) {
  1117. foreach ( $_tags as $tag )
  1118. $tags[] = $tag->name;
  1119. $tags = esc_attr( join( ', ', $tags ) );
  1120. }
  1121. $post_mime_types = get_post_mime_types();
  1122. $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
  1123. $type = array_shift( $keys );
  1124. $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
  1125. $form_fields = get_attachment_fields_to_edit( $post, $errors );
  1126. if ( $toggle ) {
  1127. $class = empty( $errors ) ? 'startclosed' : 'startopen';
  1128. $toggle_links = "
  1129. <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
  1130. <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
  1131. } else {
  1132. $class = '';
  1133. $toggle_links = '';
  1134. }
  1135. $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
  1136. $display_title = $show_title ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '&hellip;' ) . "</span></div>" : '';
  1137. $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
  1138. $order = '';
  1139. foreach ( $form_fields as $key => $val ) {
  1140. if ( 'menu_order' == $key ) {
  1141. if ( $gallery )
  1142. $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>";
  1143. else
  1144. $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
  1145. unset( $form_fields['menu_order'] );
  1146. break;
  1147. }
  1148. }
  1149. $media_dims = '';
  1150. $meta = wp_get_attachment_metadata( $post->ID );
  1151. if ( isset( $meta['width'], $meta['height'] ) )
  1152. $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  1153. /**
  1154. * Filter the media metadata.
  1155. *
  1156. * @since 2.5.0
  1157. *
  1158. * @param string $media_dims The HTML markup containing the media dimensions.
  1159. * @param WP_Post $post The WP_Post attachment object.
  1160. */
  1161. $media_dims = apply_filters( 'media_meta', $media_dims, $post );
  1162. $image_edit_button = '';
  1163. if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  1164. $nonce = wp_create_nonce( "image_editor-$post->ID" );
  1165. $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>";
  1166. }
  1167. $attachment_url = get_permalink( $attachment_id );
  1168. $item = "
  1169. $type_html
  1170. $toggle_links
  1171. $order
  1172. $display_title
  1173. <table class='slidetoggle describe $class'>
  1174. <thead class='media-item-info' id='media-head-$post->ID'>
  1175. <tr>
  1176. <td class='A1B1' id='thumbnail-head-$post->ID'>
  1177. <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
  1178. <p>$image_edit_button</p>
  1179. </td>
  1180. <td>
  1181. <p><strong>" . __('File name:') . "</strong> $filename</p>
  1182. <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
  1183. <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( get_option('date_format'), $post->post_date ). '</p>';
  1184. if ( !empty( $media_dims ) )
  1185. $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
  1186. $item .= "</td></tr>\n";
  1187. $item .= "
  1188. </thead>
  1189. <tbody>
  1190. <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>
  1191. <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n";
  1192. $defaults = array(
  1193. 'input' => 'text',
  1194. 'required' => false,
  1195. 'value' => '',
  1196. 'extra_rows' => array(),
  1197. );
  1198. if ( $send )
  1199. $send = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
  1200. if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
  1201. if ( !EMPTY_TRASH_DAYS ) {
  1202. $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>';
  1203. } elseif ( !MEDIA_TRASH ) {
  1204. $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
  1205. <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'><p>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' ), $filename ) . "</p>
  1206. <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>
  1207. <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
  1208. </div>";
  1209. } else {
  1210. $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>
  1211. <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>";
  1212. }
  1213. } else {
  1214. $delete = '';
  1215. }
  1216. $thumbnail = '';
  1217. $calling_post_id = 0;
  1218. if ( isset( $_GET['post_id'] ) )
  1219. $calling_post_id = absint( $_GET['post_id'] );
  1220. elseif ( isset( $_POST ) && count( $_POST ) ) // Like for async-upload where $_GET['post_id'] isn't set
  1221. $calling_post_id = $post->post_parent;
  1222. if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
  1223. && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
  1224. $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
  1225. $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>";
  1226. }
  1227. if ( ( $send || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) )
  1228. $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send $thumbnail $delete</td></tr>\n" );
  1229. $hidden_fields = array();
  1230. foreach ( $form_fields as $id => $field ) {
  1231. if ( $id[0] == '_' )
  1232. continue;
  1233. if ( !empty( $field['tr'] ) ) {
  1234. $item .= $field['tr'];
  1235. continue;
  1236. }
  1237. $field = array_merge( $defaults, $field );
  1238. $name = "attachments[$attachment_id][$id]";
  1239. if ( $field['input'] == 'hidden' ) {
  1240. $hidden_fields[$name] = $field['value'];
  1241. continue;
  1242. }
  1243. $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
  1244. $aria_required = $field['required'] ? " aria-required='true' " : '';
  1245. $class = $id;
  1246. $class .= $field['required'] ? ' form-required' : '';
  1247. $item .= "\t\t<tr class='$class'>\n\t\t\t<th 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'>";
  1248. if ( !empty( $field[ $field['input'] ] ) )
  1249. $item .= $field[ $field['input'] ];
  1250. elseif ( $field['input'] == 'textarea' ) {
  1251. if ( 'post_content' == $id && user_can_richedit() ) {
  1252. // sanitize_post() skips the post_content when user_can_richedit
  1253. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1254. }
  1255. // post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit()
  1256. $item .= "<textarea id='$name' name='$name' $aria_required>" . $field['value'] . '</textarea>';
  1257. } else {
  1258. $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
  1259. }
  1260. if ( !empty( $field['helps'] ) )
  1261. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1262. $item .= "</td>\n\t\t</tr>\n";
  1263. $extra_rows = array();
  1264. if ( !empty( $field['errors'] ) )
  1265. foreach ( array_unique( (array) $field['errors'] ) as $error )
  1266. $extra_rows['error'][] = $error;
  1267. if ( !empty( $field['extra_rows'] ) )
  1268. foreach ( $field['extra_rows'] as $class => $rows )
  1269. foreach ( (array) $rows as $html )
  1270. $extra_rows[$class][] = $html;
  1271. foreach ( $extra_rows as $class => $rows )
  1272. foreach ( $rows as $html )
  1273. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1274. }
  1275. if ( !empty( $form_fields['_final'] ) )
  1276. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1277. $item .= "\t</tbody>\n";
  1278. $item .= "\t</table>\n";
  1279. foreach ( $hidden_fields as $name => $value )
  1280. $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
  1281. if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
  1282. $parent = (int) $_REQUEST['post_id'];
  1283. $parent_name = "attachments[$attachment_id][post_parent]";
  1284. $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
  1285. }
  1286. return $item;
  1287. }
  1288. function get_compat_media_markup( $attachment_id, $args = null ) {
  1289. $post = get_post( $attachment_id );
  1290. $default_args = array(
  1291. 'errors' => null,
  1292. 'in_modal' => false,
  1293. );
  1294. $user_can_edit = current_user_can( 'edit_post', $attachment_id );
  1295. $args = wp_parse_args( $args, $default_args );
  1296. /** This filter is documented in wp-admin/includes/media.php */
  1297. $args = apply_filters( 'get_media_item_args', $args );
  1298. $form_fields = array();
  1299. if ( $args['in_modal'] ) {
  1300. foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  1301. $t = (array) get_taxonomy($taxonomy);
  1302. if ( ! $t['public'] || ! $t['show_ui'] )
  1303. continue;
  1304. if ( empty($t['label']) )
  1305. $t['label'] = $taxonomy;
  1306. if ( empty($t['args']) )
  1307. $t['args'] = array();
  1308. $terms = get_object_term_cache($post->ID, $taxonomy);
  1309. if ( false === $terms )
  1310. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  1311. $values = array();
  1312. foreach ( $terms as $term )
  1313. $values[] = $term->slug;
  1314. $t['value'] = join(', ', $values);
  1315. $t['taxonomy'] = true;
  1316. $form_fields[$taxonomy] = $t;
  1317. }
  1318. }
  1319. // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  1320. // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
  1321. $form_fields = array_merge_recursive($form_fields, (array) $args['errors'] );
  1322. /** This filter is documented in wp-admin/includes/media.php */
  1323. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1324. unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],
  1325. $form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],
  1326. $form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] );
  1327. /** This filter is documented in wp-admin/includes/media.php */
  1328. $media_meta = apply_filters( 'media_meta', '', $post );
  1329. $defaults = array(
  1330. 'input' => 'text',
  1331. 'required' => false,
  1332. 'value' => '',
  1333. 'extra_rows' => array(),
  1334. 'show_in_edit' => true,
  1335. 'show_in_modal' => true,
  1336. );
  1337. $hidden_fields = array();
  1338. $item = '';
  1339. foreach ( $form_fields as $id => $field ) {
  1340. if ( $id[0] == '_' )
  1341. continue;
  1342. $name = "attachments[$attachment_id][$id]";
  1343. $id_attr = "attachments-$attachment_id-$id";
  1344. if ( !empty( $field['tr'] ) ) {
  1345. $item .= $field['tr'];
  1346. continue;
  1347. }
  1348. $field = array_merge( $defaults, $field );
  1349. if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) )
  1350. continue;
  1351. if ( $field['input'] == 'hidden' ) {
  1352. $hidden_fields[$name] = $field['value'];
  1353. continue;
  1354. }
  1355. $readonly = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
  1356. $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
  1357. $aria_required = $field['required'] ? " aria-required='true' " : '';
  1358. $class = 'compat-field-' . $id;
  1359. $class .= $field['required'] ? ' form-required' : '';
  1360. $item .= "\t\t<tr class='$class'>";
  1361. $item .= "\t\t\t<th scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
  1362. $item .= "</th>\n\t\t\t<td class='field'>";
  1363. if ( !empty( $field[ $field['input'] ] ) )
  1364. $item .= $field[ $field['input'] ];
  1365. elseif ( $field['input'] == 'textarea' ) {
  1366. if ( 'post_content' == $id && user_can_richedit() ) {
  1367. // sanitize_post() skips the post_content when user_can_richedit
  1368. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1369. }
  1370. $item .= "<textarea id='$id_attr' name='$name' $aria_required>" . $field['value'] . '</textarea>';
  1371. } else {
  1372. $item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly $aria_required />";
  1373. }
  1374. if ( !empty( $field['helps'] ) )
  1375. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1376. $item .= "</td>\n\t\t</tr>\n";
  1377. $extra_rows = array();
  1378. if ( !empty( $field['errors'] ) )
  1379. foreach ( array_unique( (array) $field['errors'] ) as $error )
  1380. $extra_rows['error'][] = $error;
  1381. if ( !empty( $field['extra_rows'] ) )
  1382. foreach ( $field['extra_rows'] as $class => $rows )
  1383. foreach ( (array) $rows as $html )
  1384. $extra_rows[$class][] = $html;
  1385. foreach ( $extra_rows as $class => $rows )
  1386. foreach ( $rows as $html )
  1387. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1388. }
  1389. if ( !empty( $form_fields['_final'] ) )
  1390. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1391. if ( $item )
  1392. $item = '<table class="compat-attachment-fields">' . $item . '</table>';
  1393. foreach ( $hidden_fields as $hidden_field => $value ) {
  1394. $item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n";
  1395. }
  1396. if ( $item )
  1397. $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr( $post->menu_order ) . '" />' . $item;
  1398. return array(
  1399. 'item' => $item,
  1400. 'meta' => $media_meta,
  1401. );
  1402. }
  1403. /**
  1404. * {@internal Missing Short Description}}
  1405. *
  1406. * @since 2.5.0
  1407. */
  1408. function media_upload_header() {
  1409. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1410. echo '<script type="text/javascript">post_id = ' . $post_id . ";</script>\n";
  1411. if ( empty( $_GET['chromeless'] ) ) {
  1412. echo '<div id="media-upload-header">';
  1413. the_media_upload_tabs();
  1414. echo '</div>';
  1415. }
  1416. }
  1417. /**
  1418. * {@internal Missing Short Description}}
  1419. *
  1420. * @since 2.5.0
  1421. *
  1422. * @param unknown_type $errors
  1423. */
  1424. function media_upload_form( $errors = null ) {
  1425. global $type, $tab, $is_IE, $is_opera;
  1426. if ( ! _device_can_upload() ) {
  1427. 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.'), 'https://wordpress.org/mobile/' ) . '</p>';
  1428. return;
  1429. }
  1430. $upload_action_url = admin_url('async-upload.php');
  1431. $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
  1432. $_type = isset($type) ? $type : '';
  1433. $_tab = isset($tab) ? $tab : '';
  1434. $upload_size_unit = $max_upload_size = wp_max_upload_size();
  1435. $sizes = array( 'KB', 'MB', 'GB' );
  1436. for ( $u = -1; $upload_size_unit > 1024 && $u < count( $sizes ) - 1; $u++ ) {
  1437. $upload_size_unit /= 1024;
  1438. }
  1439. if ( $u < 0 ) {
  1440. $upload_size_unit = 0;
  1441. $u = 0;
  1442. } else {
  1443. $upload_size_unit = (int) $upload_size_unit;
  1444. }
  1445. ?>
  1446. <div id="media-upload-notice"><?php
  1447. if (isset($errors['upload_notice']) )
  1448. echo $errors['upload_notice'];
  1449. ?></div>
  1450. <div id="media-upload-error"><?php
  1451. if (isset($errors['upload_error']) && is_wp_error($errors['upload_error']))
  1452. echo $errors['upload_error']->get_error_message();
  1453. ?></div>
  1454. <?php
  1455. if ( is_multisite() && !is_upload_space_available() ) {
  1456. /**
  1457. * Fires when an upload will exceed the defined upload space quota for a network site.
  1458. *
  1459. * @since 3.5.0
  1460. */
  1461. do_action( 'upload_ui_over_quota' );
  1462. return;
  1463. }
  1464. /**
  1465. * Fires just before the legacy (pre-3.5.0) upload interface is loaded.
  1466. *
  1467. * @since 2.6.0
  1468. */
  1469. do_action( 'pre-upload-ui' );
  1470. $post_params = array(
  1471. "post_id" => $post_id,
  1472. "_wpnonce" => wp_create_nonce('media-form'),
  1473. "type" => $_type,
  1474. "tab" => $_tab,
  1475. "short" => "1",
  1476. );
  1477. /**
  1478. * Filter the media upload post parameters.
  1479. *
  1480. * @since 3.1.0 As 'swfupload_post_params'
  1481. * @since 3.3.0
  1482. *
  1483. * @param array $post_params An array of media upload parameters used by Plupload.
  1484. */
  1485. $post_params = apply_filters( 'upload_post_params', $post_params );
  1486. $plupload_init = array(
  1487. 'runtimes' => 'html5,flash,silverlight,html4',
  1488. 'browse_button' => 'plupload-browse-button',
  1489. 'container' => 'plupload-upload-ui',
  1490. 'drop_element' => 'drag-drop-area',
  1491. 'file_data_name' => 'async-upload',
  1492. 'url' => $upload_action_url,
  1493. 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),
  1494. 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
  1495. 'filters' => array(
  1496. 'max_file_size' => $max_upload_size . 'b',
  1497. ),
  1498. 'multipart_params' => $post_params,
  1499. );
  1500. // Multi-file uploading doesn't currently work in iOS Safari,
  1501. // single-file allows the built-in camera to be used as source for images
  1502. if ( wp_is_mobile() )
  1503. $plupload_init['multi_selection'] = false;
  1504. /**
  1505. * Filter the default Plupload settings.
  1506. *
  1507. * @since 3.3.0
  1508. *
  1509. * @param array $plupload_init An array of default settings used by Plupload.
  1510. */
  1511. $plupload_init = apply_filters( 'plupload_init', $plupload_init );
  1512. ?>
  1513. <script type="text/javascript">
  1514. <?php
  1515. // Verify size is an int. If not return default value.
  1516. $large_size_h = absint( get_option('large_size_h') );
  1517. if( !$large_size_h )
  1518. $large_size_h = 1024;
  1519. $large_size_w = absint( get_option('large_size_w') );
  1520. if( !$large_size_w )
  1521. $large_size_w = 1024;
  1522. ?>
  1523. var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
  1524. wpUploaderInit = <?php echo json_encode($plupload_init); ?>;
  1525. </script>
  1526. <div id="plupload-upload-ui" class="hide-if-no-js">
  1527. <?php
  1528. /**
  1529. * Fires before the upload interface loads.
  1530. *
  1531. * @since 2.6.0 As 'pre-flash-upload-ui'
  1532. * @since 3.3.0
  1533. */
  1534. do_action( 'pre-plupload-upload-ui' ); ?>
  1535. <div id="drag-drop-area">
  1536. <div class="drag-drop-inside">
  1537. <p class="drag-drop-info"><?php _e('Drop files here'); ?></p>
  1538. <p><?php _ex('or', 'Uploader: Drop files here - or - Select Files'); ?></p>
  1539. <p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e('Select Files'); ?>" class="button" /></p>
  1540. </div>
  1541. </div>
  1542. <?php
  1543. /**
  1544. * Fires after the upload interface loads.
  1545. *
  1546. * @since 2.6.0 As 'post-flash-upload-ui'
  1547. * @since 3.3.0
  1548. */
  1549. do_action( 'post-plupload-upload-ui' ); ?>
  1550. </div>
  1551. <div id="html-upload-ui" class="hide-if-js">
  1552. <?php
  1553. /**
  1554. * Fires before the upload button in the media upload interface.
  1555. *
  1556. * @since 2.6.0
  1557. */
  1558. do_action( 'pre-html-upload-ui' );
  1559. ?>
  1560. <p id="async-upload-wrap">
  1561. <label class="screen-reader-text" for="async-upload"><?php _e('Upload'); ?></label>
  1562. <input type="file" name="async-upload" id="async-upload" />
  1563. <?php submit_button( __( 'Upload' ), 'button', 'html-upload', false ); ?>
  1564. <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e('Cancel'); ?></a>
  1565. </p>
  1566. <div class="clear"></div>
  1567. <?php
  1568. /**
  1569. * Fires after the upload button in the media upload interface.
  1570. *
  1571. * @since 2.6.0
  1572. */
  1573. do_action( 'post-html-upload-ui' );
  1574. ?>
  1575. </div>
  1576. <span class="max-upload-size"><?php printf( __( 'Maximum upload file size: %d%s.' ), esc_html($upload_size_unit), esc_html($sizes[$u]) ); ?></span>
  1577. <?php
  1578. /**
  1579. * Fires on the post upload UI screen.
  1580. *
  1581. * Legacy (pre-3.5.0) media workflow hook.
  1582. *
  1583. * @since 2.6.0
  1584. */
  1585. do_action( 'post-upload-ui' );
  1586. }
  1587. /**
  1588. * {@internal Missing Short Description}}
  1589. *
  1590. * @since 2.5.0
  1591. *
  1592. * @param string $type
  1593. * @param object $errors
  1594. * @param integer $id
  1595. */
  1596. function media_upload_type_form($type = 'file', $errors = null, $id = null) {
  1597. media_upload_header();
  1598. $post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
  1599. $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
  1600. /**
  1601. * Filter the media upload form action URL.
  1602. *
  1603. * @since 2.6.0
  1604. *
  1605. * @param string $form_action_url The media upload form action URL.
  1606. * @param string $type The type of media. Default 'file'.
  1607. */
  1608. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1609. $form_class = 'media-upload-form type-form validate';
  1610. if ( get_user_setting('uploader') )
  1611. $form_class .= ' html-uploader';
  1612. ?>
  1613. <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">
  1614. <?php submit_button( '', 'hidden', 'save', false ); ?>
  1615. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1616. <?php wp_nonce_field('media-form'); ?>
  1617. <h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
  1618. <?php media_upload_form( $errors ); ?>
  1619. <script type="text/javascript">
  1620. //<![CDATA[
  1621. jQuery(function($){
  1622. var preloaded = $(".media-item.preloaded");
  1623. if ( preloaded.length > 0 ) {
  1624. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1625. }
  1626. updateMediaForm();
  1627. });
  1628. //]]>
  1629. </script>
  1630. <div id="media-items"><?php
  1631. if ( $id ) {
  1632. if ( !is_wp_error($id) ) {
  1633. add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
  1634. echo get_media_items( $id, $errors );
  1635. } else {
  1636. echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div></div>';
  1637. exit;
  1638. }
  1639. }
  1640. ?></div>
  1641. <p class="savebutton ml-submit">
  1642. <?php submit_button( __( 'Save all changes' ), 'button', 'save', false ); ?>
  1643. </p>
  1644. </form>
  1645. <?php
  1646. }
  1647. /**
  1648. * {@internal Missing Short Description}}
  1649. *
  1650. * @since 2.7.0
  1651. *
  1652. * @param string $type
  1653. * @param object $errors
  1654. * @param integer $id
  1655. */
  1656. function media_upload_type_url_form($type = null, $errors = null, $id = null) {
  1657. if ( null === $type )
  1658. $type = 'image';
  1659. media_upload_header();
  1660. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1661. $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
  1662. /** This filter is documented in wp-admin/includes/media.php */
  1663. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1664. $form_class = 'media-upload-form type-form validate';
  1665. if ( get_user_setting('uploader') )
  1666. $form_class .= ' html-uploader';
  1667. ?>
  1668. <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">
  1669. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1670. <?php wp_nonce_field('media-form'); ?>
  1671. <h3 class="media-title"><?php _e('Insert media from another website'); ?></h3>
  1672. <script type="text/javascript">
  1673. //<![CDATA[
  1674. var addExtImage = {
  1675. width : '',
  1676. height : '',
  1677. align : 'alignnone',
  1678. insert : function() {
  1679. var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
  1680. if ( '' == f.src.value || '' == t.width )
  1681. return false;
  1682. if ( f.alt.value )
  1683. alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1684. <?php
  1685. /** This filter is documented in wp-admin/includes/media.php */
  1686. if ( ! apply_filters( 'disable_captions', '' ) ) {
  1687. ?>
  1688. if ( f.caption.value ) {
  1689. caption = f.caption.value.replace(/\r\n|\r/g, '\n');
  1690. caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
  1691. return a.replace(/[\r\n\t]+/, ' ');
  1692. });
  1693. caption = caption.replace(/\s*\n\s*/g, '<br />');
  1694. }
  1695. <?php } ?>
  1696. cls = caption ? '' : ' class="'+t.align+'"';
  1697. html = '<img alt="'+alt+'" src="'+f.src.value+'"'+cls+' width="'+t.width+'" height="'+t.height+'" />';
  1698. if ( f.url.value ) {
  1699. url = f.url.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1700. html = '<a href="'+url+'">'+html+'</a>';
  1701. }
  1702. if ( caption )
  1703. html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
  1704. var win = window.dialogArguments || opener || parent || top;
  1705. win.send_to_editor(html);
  1706. return false;
  1707. },
  1708. resetImageData : function() {
  1709. var t = addExtImage;
  1710. t.width = t.height = '';
  1711. document.getElementById('go_button').style.color = '#bbb';
  1712. if ( ! document.forms[0].src.value )
  1713. document.getElementById('status_img').innerHTML = '*';
  1714. else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
  1715. },
  1716. updateImageData : function() {
  1717. var t = addExtImage;
  1718. t.width = t.preloadImg.width;
  1719. t.height = t.preloadImg.height;
  1720. document.getElementById('go_button').style.color = '#333';
  1721. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
  1722. },
  1723. getImageData : function() {
  1724. if ( jQuery('table.describe').hasClass('not-image') )
  1725. return;
  1726. var t = addExtImage, src = document.forms[0].src.value;
  1727. if ( ! src ) {
  1728. t.resetImageData();
  1729. return false;
  1730. }
  1731. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" width="16" />';
  1732. t.preloadImg = new Image();
  1733. t.preloadImg.onload = t.updateImageData;
  1734. t.preloadImg.onerror = t.resetImageData;
  1735. t.preloadImg.src = src;
  1736. }
  1737. }
  1738. jQuery(document).ready( function($) {
  1739. $('.media-types input').click( function() {
  1740. $('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
  1741. });
  1742. });
  1743. //]]>
  1744. </script>
  1745. <div id="media-items">
  1746. <div class="media-item media-blank">
  1747. <?php
  1748. /**
  1749. * Filter the insert media from URL form HTML.
  1750. *
  1751. * @since 3.3.0
  1752. *
  1753. * @param string $form_html The insert from URL form HTML.
  1754. */
  1755. echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) );
  1756. ?>
  1757. </div>
  1758. </div>
  1759. </form>
  1760. <?php
  1761. }
  1762. /**
  1763. * Adds gallery form to upload iframe
  1764. *
  1765. * @since 2.5.0
  1766. *
  1767. * @param array $errors
  1768. */
  1769. function media_upload_gallery_form($errors) {
  1770. global $redir_tab, $type;
  1771. $redir_tab = 'gallery';
  1772. media_upload_header();
  1773. $post_id = intval($_REQUEST['post_id']);
  1774. $form_action_url = admin_url("media-upload.php?type=$type&tab=gallery&post_id=$post_id");
  1775. /** This filter is documented in wp-admin/includes/media.php */
  1776. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1777. $form_class = 'media-upload-form validate';
  1778. if ( get_user_setting('uploader') )
  1779. $form_class .= ' html-uploader';
  1780. ?>
  1781. <script type="text/javascript">
  1782. <!--
  1783. jQuery(function($){
  1784. var preloaded = $(".media-item.preloaded");
  1785. if ( preloaded.length > 0 ) {
  1786. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1787. updateMediaForm();
  1788. }
  1789. });
  1790. -->
  1791. </script>
  1792. <div id="sort-buttons" class="hide-if-no-js">
  1793. <span>
  1794. <?php _e('All Tabs:'); ?>
  1795. <a href="#" id="showall"><?php _e('Show'); ?></a>
  1796. <a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
  1797. </span>
  1798. <?php _e('Sort Order:'); ?>
  1799. <a href="#" id="asc"><?php _e('Ascending'); ?></a> |
  1800. <a href="#" id="desc"><?php _e('Descending'); ?></a> |
  1801. <a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
  1802. </div>
  1803. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
  1804. <?php wp_nonce_field('media-form'); ?>
  1805. <?php //media_upload_form( $errors ); ?>
  1806. <table class="widefat">
  1807. <thead><tr>
  1808. <th><?php _e('Media'); ?></th>
  1809. <th class="order-head"><?php _e('Order'); ?></th>
  1810. <th class="actions-head"><?php _e('Actions'); ?></th>
  1811. </tr></thead>
  1812. </table>
  1813. <div id="media-items">
  1814. <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
  1815. <?php echo get_media_items($post_id, $errors); ?>
  1816. </div>
  1817. <p class="ml-submit">
  1818. <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
  1819. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1820. <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
  1821. <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
  1822. </p>
  1823. <div id="gallery-settings" style="display:none;">
  1824. <div class="title"><?php _e('Gallery Settings'); ?></div>
  1825. <table id="basic" class="describe"><tbody>
  1826. <tr>
  1827. <th scope="row" class="label">
  1828. <label>
  1829. <span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
  1830. </label>
  1831. </th>
  1832. <td class="field">
  1833. <input type="radio" name="linkto" id="linkto-file" value="file" />
  1834. <label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
  1835. <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
  1836. <label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
  1837. </td>
  1838. </tr>
  1839. <tr>
  1840. <th scope="row" class="label">
  1841. <label>
  1842. <span class="alignleft"><?php _e('Order images by:'); ?></span>
  1843. </label>
  1844. </th>
  1845. <td class="field">
  1846. <select id="orderby" name="orderby">
  1847. <option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
  1848. <option value="title"><?php _e('Title'); ?></option>
  1849. <option value="post_date"><?php _e('Date/Time'); ?></option>
  1850. <option value="rand"><?php _e('Random'); ?></option>
  1851. </select>
  1852. </td>
  1853. </tr>
  1854. <tr>
  1855. <th scope="row" class="label">
  1856. <label>
  1857. <span class="alignleft"><?php _e('Order:'); ?></span>
  1858. </label>
  1859. </th>
  1860. <td class="field">
  1861. <input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
  1862. <label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
  1863. <input type="radio" name="order" id="order-desc" value="desc" />
  1864. <label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
  1865. </td>
  1866. </tr>
  1867. <tr>
  1868. <th scope="row" class="label">
  1869. <label>
  1870. <span class="alignleft"><?php _e('Gallery columns:'); ?></span>
  1871. </label>
  1872. </th>
  1873. <td class="field">
  1874. <select id="columns" name="columns">
  1875. <option value="1">1</option>
  1876. <option value="2">2</option>
  1877. <option value="3" selected="selected">3</option>
  1878. <option value="4">4</option>
  1879. <option value="5">5</option>
  1880. <option value="6">6</option>
  1881. <option value="7">7</option>
  1882. <option value="8">8</option>
  1883. <option value="9">9</option>
  1884. </select>
  1885. </td>
  1886. </tr>
  1887. </tbody></table>
  1888. <p class="ml-submit">
  1889. <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
  1890. <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' ); ?>" />
  1891. </p>
  1892. </div>
  1893. </form>
  1894. <?php
  1895. }
  1896. /**
  1897. * {@internal Missing Short Description}}
  1898. *
  1899. * @since 2.5.0
  1900. *
  1901. * @param array $errors
  1902. */
  1903. function media_upload_library_form($errors) {
  1904. global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
  1905. media_upload_header();
  1906. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1907. $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
  1908. /** This filter is documented in wp-admin/includes/media.php */
  1909. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1910. $form_class = 'media-upload-form validate';
  1911. if ( get_user_setting('uploader') )
  1912. $form_class .= ' html-uploader';
  1913. $q = $_GET;
  1914. $q['posts_per_page'] = 10;
  1915. $q['paged'] = isset( $q['paged'] ) ? intval( $q['paged'] ) : 0;
  1916. if ( $q['paged'] < 1 ) {
  1917. $q['paged'] = 1;
  1918. }
  1919. $q['offset'] = ( $q['paged'] - 1 ) * 10;
  1920. if ( $q['offset'] < 1 ) {
  1921. $q['offset'] = 0;
  1922. }
  1923. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query( $q );
  1924. ?>
  1925. <form id="filter" action="" method="get">
  1926. <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
  1927. <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
  1928. <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
  1929. <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
  1930. <input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
  1931. <p id="media-search" class="search-box">
  1932. <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
  1933. <input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
  1934. <?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
  1935. </p>
  1936. <ul class="subsubsub">
  1937. <?php
  1938. $type_links = array();
  1939. $_num_posts = (array) wp_count_attachments();
  1940. $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  1941. foreach ( $matches as $_type => $reals )
  1942. foreach ( $reals as $real )
  1943. if ( isset($num_posts[$_type]) )
  1944. $num_posts[$_type] += $_num_posts[$real];
  1945. else
  1946. $num_posts[$_type] = $_num_posts[$real];
  1947. // If available type specified by media button clicked, filter by that type
  1948. if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
  1949. $_GET['post_mime_type'] = $type;
  1950. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  1951. }
  1952. if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
  1953. $class = ' class="current"';
  1954. else
  1955. $class = '';
  1956. $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
  1957. foreach ( $post_mime_types as $mime_type => $label ) {
  1958. $class = '';
  1959. if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  1960. continue;
  1961. if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  1962. $class = ' class="current"';
  1963. $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>';
  1964. }
  1965. /**
  1966. * Filter the media upload mime type list items.
  1967. *
  1968. * Returned values should begin with an <li> tag.
  1969. *
  1970. * @since 3.1.0
  1971. *
  1972. * @param array $type_links An array of list items containing mime type link HTML.
  1973. */
  1974. echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
  1975. unset($type_links);
  1976. ?>
  1977. </ul>
  1978. <div class="tablenav">
  1979. <?php
  1980. $page_links = paginate_links( array(
  1981. 'base' => add_query_arg( 'paged', '%#%' ),
  1982. 'format' => '',
  1983. 'prev_text' => __('&laquo;'),
  1984. 'next_text' => __('&raquo;'),
  1985. 'total' => ceil($wp_query->found_posts / 10),
  1986. 'current' => $q['paged'],
  1987. ));
  1988. if ( $page_links )
  1989. echo "<div class='tablenav-pages'>$page_links</div>";
  1990. ?>
  1991. <div class="alignleft actions">
  1992. <?php
  1993. $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";
  1994. $arc_result = $wpdb->get_results( $arc_query );
  1995. $month_count = count($arc_result);
  1996. $selected_month = isset( $_GET['m'] ) ? $_GET['m'] : 0;
  1997. if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
  1998. <select name='m'>
  1999. <option<?php selected( $selected_month, 0 ); ?> value='0'><?php _e( 'All dates' ); ?></option>
  2000. <?php
  2001. foreach ($arc_result as $arc_row) {
  2002. if ( $arc_row->yyear == 0 )
  2003. continue;
  2004. $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
  2005. if ( $arc_row->yyear . $arc_row->mmonth == $selected_month )
  2006. $default = ' selected="selected"';
  2007. else
  2008. $default = '';
  2009. echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
  2010. echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
  2011. echo "</option>\n";
  2012. }
  2013. ?>
  2014. </select>
  2015. <?php } ?>
  2016. <?php submit_button( __( 'Filter &#187;' ), 'button', 'post-query-submit', false ); ?>
  2017. </div>
  2018. <br class="clear" />
  2019. </div>
  2020. </form>
  2021. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form">
  2022. <?php wp_nonce_field('media-form'); ?>
  2023. <?php //media_upload_form( $errors ); ?>
  2024. <script type="text/javascript">
  2025. <!--
  2026. jQuery(function($){
  2027. var preloaded = $(".media-item.preloaded");
  2028. if ( preloaded.length > 0 ) {
  2029. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  2030. updateMediaForm();
  2031. }
  2032. });
  2033. -->
  2034. </script>
  2035. <div id="media-items">
  2036. <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
  2037. <?php echo get_media_items(null, $errors); ?>
  2038. </div>
  2039. <p class="ml-submit">
  2040. <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
  2041. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  2042. </p>
  2043. </form>
  2044. <?php
  2045. }
  2046. /**
  2047. * Creates the form for external url
  2048. *
  2049. * @since 2.7.0
  2050. *
  2051. * @param string $default_view
  2052. * @return string the form html
  2053. */
  2054. function wp_media_insert_url_form( $default_view = 'image' ) {
  2055. /** This filter is documented in wp-admin/includes/media.php */
  2056. if ( ! apply_filters( 'disable_captions', '' ) ) {
  2057. $caption = '
  2058. <tr class="image-only">
  2059. <th scope="row" class="label">
  2060. <label for="caption"><span class="alignleft">' . __('Image Caption') . '</span></label>
  2061. </th>
  2062. <td class="field"><textarea id="caption" name="caption"></textarea></td>
  2063. </tr>
  2064. ';
  2065. } else {
  2066. $caption = '';
  2067. }
  2068. $default_align = get_option('image_default_align');
  2069. if ( empty($default_align) )
  2070. $default_align = 'none';
  2071. if ( 'image' == $default_view ) {
  2072. $view = 'image-only';
  2073. $table_class = '';
  2074. } else {
  2075. $view = $table_class = 'not-image';
  2076. }
  2077. return '
  2078. <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>
  2079. <table class="describe ' . $table_class . '"><tbody>
  2080. <tr>
  2081. <th scope="row" class="label" style="width:130px;">
  2082. <label for="src"><span class="alignleft">' . __('URL') . '</span></label>
  2083. <span class="alignright"><abbr id="status_img" title="required" class="required">*</abbr></span>
  2084. </th>
  2085. <td class="field"><input id="src" name="src" value="" type="text" aria-required="true" onblur="addExtImage.getImageData()" /></td>
  2086. </tr>
  2087. <tr>
  2088. <th scope="row" class="label">
  2089. <label for="title"><span class="alignleft">' . __('Title') . '</span></label>
  2090. <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  2091. </th>
  2092. <td class="field"><input id="title" name="title" value="" type="text" aria-required="true" /></td>
  2093. </tr>
  2094. <tr class="not-image"><td></td><td><p class="help">' . __('Link text, e.g. &#8220;Ransom Demands (PDF)&#8221;') . '</p></td></tr>
  2095. <tr class="image-only">
  2096. <th scope="row" class="label">
  2097. <label for="alt"><span class="alignleft">' . __('Alternative Text') . '</span></label>
  2098. </th>
  2099. <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
  2100. <p class="help">' . __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;') . '</p></td>
  2101. </tr>
  2102. ' . $caption . '
  2103. <tr class="align image-only">
  2104. <th scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
  2105. <td class="field">
  2106. <input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'none' ? ' checked="checked"' : '').' />
  2107. <label for="align-none" class="align image-align-none-label">' . __('None') . '</label>
  2108. <input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'left' ? ' checked="checked"' : '').' />
  2109. <label for="align-left" class="align image-align-left-label">' . __('Left') . '</label>
  2110. <input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'center' ? ' checked="checked"' : '').' />
  2111. <label for="align-center" class="align image-align-center-label">' . __('Center') . '</label>
  2112. <input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'right' ? ' checked="checked"' : '').' />
  2113. <label for="align-right" class="align image-align-right-label">' . __('Right') . '</label>
  2114. </td>
  2115. </tr>
  2116. <tr class="image-only">
  2117. <th scope="row" class="label">
  2118. <label for="url"><span class="alignleft">' . __('Link Image To:') . '</span></label>
  2119. </th>
  2120. <td class="field"><input id="url" name="url" value="" type="text" /><br />
  2121. <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __('None') . '</button>
  2122. <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __('Link to image') . '</button>
  2123. <p class="help">' . __('Enter a link URL or click above for presets.') . '</p></td>
  2124. </tr>
  2125. <tr class="image-only">
  2126. <td></td>
  2127. <td>
  2128. <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . esc_attr__('Insert into Post') . '" />
  2129. </td>
  2130. </tr>
  2131. <tr class="not-image">
  2132. <td></td>
  2133. <td>
  2134. ' . get_submit_button( __( 'Insert into Post' ), 'button', 'insertonlybutton', false ) . '
  2135. </td>
  2136. </tr>
  2137. </tbody></table>
  2138. ';
  2139. }
  2140. /**
  2141. * Displays the multi-file uploader message.
  2142. *
  2143. * @since 2.6.0
  2144. */
  2145. function media_upload_flash_bypass() {
  2146. $browser_uploader = admin_url( 'media-new.php?browser-uploader' );
  2147. if ( $post = get_post() )
  2148. $browser_uploader .= '&amp;post_id=' . intval( $post->ID );
  2149. elseif ( ! empty( $GLOBALS['post_ID'] ) )
  2150. $browser_uploader .= '&amp;post_id=' . intval( $GLOBALS['post_ID'] );
  2151. ?>
  2152. <p class="upload-flash-bypass">
  2153. <?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' ); ?>
  2154. </p>
  2155. <?php
  2156. }
  2157. add_action('post-plupload-upload-ui', 'media_upload_flash_bypass');
  2158. /**
  2159. * Displays the browser's built-in uploader message.
  2160. *
  2161. * @since 2.6.0
  2162. */
  2163. function media_upload_html_bypass() {
  2164. ?>
  2165. <p class="upload-html-bypass hide-if-no-js">
  2166. <?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>.'); ?>
  2167. </p>
  2168. <?php
  2169. }
  2170. add_action('post-html-upload-ui', 'media_upload_html_bypass');
  2171. /**
  2172. * Used to display a "After a file has been uploaded..." help message.
  2173. *
  2174. * @since 3.3.0
  2175. */
  2176. function media_upload_text_after() {}
  2177. /**
  2178. * Displays the checkbox to scale images.
  2179. *
  2180. * @since 3.3.0
  2181. */
  2182. function media_upload_max_image_resize() {
  2183. $checked = get_user_setting('upload_resize') ? ' checked="true"' : '';
  2184. $a = $end = '';
  2185. if ( current_user_can( 'manage_options' ) ) {
  2186. $a = '<a href="' . esc_url( admin_url( 'options-media.php' ) ) . '" target="_blank">';
  2187. $end = '</a>';
  2188. }
  2189. ?>
  2190. <p class="hide-if-no-js"><label>
  2191. <input name="image_resize" type="checkbox" id="image_resize" value="true"<?php echo $checked; ?> />
  2192. <?php
  2193. /* translators: %1$s is link start tag, %2$s is link end tag, %3$d is width, %4$d is height*/
  2194. 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' ) );
  2195. ?>
  2196. </label></p>
  2197. <?php
  2198. }
  2199. /**
  2200. * Displays the out of storage quota message in Multisite.
  2201. *
  2202. * @since 3.5.0
  2203. */
  2204. function multisite_over_quota_message() {
  2205. echo '<p>' . sprintf( __( 'Sorry, you have used all of your storage quota of %s MB.' ), get_space_allowed() ) . '</p>';
  2206. }
  2207. /**
  2208. * Displays the image and editor in the post editor
  2209. *
  2210. * @since 3.5.0
  2211. */
  2212. function edit_form_image_editor( $post ) {
  2213. $open = isset( $_GET['image-editor'] );
  2214. if ( $open )
  2215. require_once ABSPATH . 'wp-admin/includes/image-edit.php';
  2216. $thumb_url = false;
  2217. if ( $attachment_id = intval( $post->ID ) )
  2218. $thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
  2219. $filename = esc_html( basename( $post->guid ) );
  2220. $title = esc_attr( $post->post_title );
  2221. $alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
  2222. $att_url = wp_get_attachment_url( $post->ID ); ?>
  2223. <div class="wp_attachment_holder">
  2224. <?php
  2225. if ( wp_attachment_is_image( $post->ID ) ) :
  2226. $image_edit_button = '';
  2227. if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  2228. $nonce = wp_create_nonce( "image_editor-$post->ID" );
  2229. $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>";
  2230. }
  2231. ?>
  2232. <div class="imgedit-response" id="imgedit-response-<?php echo $attachment_id; ?>"></div>
  2233. <div<?php if ( $open ) echo ' style="display:none"'; ?> class="wp_attachment_image" id="media-head-<?php echo $attachment_id; ?>">
  2234. <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>
  2235. <p><?php echo $image_edit_button; ?></p>
  2236. </div>
  2237. <div<?php if ( ! $open ) echo ' style="display:none"'; ?> class="image-editor" id="image-editor-<?php echo $attachment_id; ?>">
  2238. <?php if ( $open ) wp_image_editor( $attachment_id ); ?>
  2239. </div>
  2240. <?php
  2241. elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'audio/' ) ):
  2242. wp_maybe_generate_attachment_metadata( $post );
  2243. echo wp_audio_shortcode( array( 'src' => $att_url ) );
  2244. elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'video/' ) ):
  2245. wp_maybe_generate_attachment_metadata( $post );
  2246. $meta = wp_get_attachment_metadata( $attachment_id );
  2247. $w = ! empty( $meta['width'] ) ? min( $meta['width'], 640 ) : 0;
  2248. $h = ! empty( $meta['height'] ) ? $meta['height'] : 0;
  2249. if ( $h && $w < $meta['width'] ) {
  2250. $h = round( ( $meta['height'] * $w ) / $meta['width'] );
  2251. }
  2252. $attr = array( 'src' => $att_url );
  2253. if ( ! empty( $w ) && ! empty( $h ) ) {
  2254. $attr['width'] = $w;
  2255. $attr['height'] = $h;
  2256. }
  2257. echo wp_video_shortcode( $attr );
  2258. endif; ?>
  2259. </div>
  2260. <div class="wp_attachment_details edit-form-section">
  2261. <p>
  2262. <label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong></label><br />
  2263. <textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea>
  2264. </p>
  2265. <?php if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) : ?>
  2266. <p>
  2267. <label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br />
  2268. <input type="text" class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" value="<?php echo esc_attr( $alt_text ); ?>" />
  2269. </p>
  2270. <?php endif; ?>
  2271. <?php
  2272. $quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
  2273. $editor_args = array(
  2274. 'textarea_name' => 'content',
  2275. 'textarea_rows' => 5,
  2276. 'media_buttons' => false,
  2277. 'tinymce' => false,
  2278. 'quicktags' => $quicktags_settings,
  2279. );
  2280. ?>
  2281. <label for="content"><strong><?php _e( 'Description' ); ?></strong><?php
  2282. if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  2283. echo ': ' . __( 'Displayed on attachment pages.' );
  2284. } ?></label>
  2285. <?php wp_editor( $post->post_content, 'attachment_content', $editor_args ); ?>
  2286. </div>
  2287. <?php
  2288. $extras = get_compat_media_markup( $post->ID );
  2289. echo $extras['item'];
  2290. echo '<input type="hidden" id="image-edit-context" value="edit-attachment" />' . "\n";
  2291. }
  2292. /**
  2293. * Displays non-editable attachment metadata in the publish metabox
  2294. *
  2295. * @since 3.5.0
  2296. */
  2297. function attachment_submitbox_metadata() {
  2298. $post = get_post();
  2299. $filename = esc_html( wp_basename( $post->guid ) );
  2300. $media_dims = '';
  2301. $meta = wp_get_attachment_metadata( $post->ID );
  2302. if ( isset( $meta['width'], $meta['height'] ) )
  2303. $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  2304. /** This filter is documented in wp-admin/includes/media.php */
  2305. $media_dims = apply_filters( 'media_meta', $media_dims, $post );
  2306. $att_url = wp_get_attachment_url( $post->ID );
  2307. ?>
  2308. <div class="misc-pub-section misc-pub-attachment">
  2309. <label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
  2310. <input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" value="<?php echo esc_attr($att_url); ?>" />
  2311. </div>
  2312. <div class="misc-pub-section misc-pub-filename">
  2313. <?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
  2314. </div>
  2315. <div class="misc-pub-section misc-pub-filetype">
  2316. <?php _e( 'File type:' ); ?> <strong><?php
  2317. if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
  2318. echo esc_html( strtoupper( $matches[1] ) );
  2319. list( $mime_type ) = explode( '/', $post->post_mime_type );
  2320. if ( $mime_type !== 'image' && ! empty( $meta['mime_type'] ) ) {
  2321. if ( $meta['mime_type'] !== "$mime_type/" . strtolower( $matches[1] ) ) {
  2322. echo ' (' . $meta['mime_type'] . ')';
  2323. }
  2324. }
  2325. } else {
  2326. echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
  2327. }
  2328. ?></strong>
  2329. </div>
  2330. <?php
  2331. $file = get_attached_file( $post->ID );
  2332. $file_size = false;
  2333. if ( isset( $meta['filesize'] ) )
  2334. $file_size = $meta['filesize'];
  2335. elseif ( file_exists( $file ) )
  2336. $file_size = filesize( $file );
  2337. if ( ! empty( $file_size ) ) : ?>
  2338. <div class="misc-pub-section misc-pub-filesize">
  2339. <?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong>
  2340. </div>
  2341. <?php
  2342. endif;
  2343. if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  2344. /**
  2345. * Filter the audio and video metadata fields to be shown in the publish meta box.
  2346. *
  2347. * The key for each item in the array should correspond to an attachment
  2348. * metadata key, and the value should be the desired label.
  2349. *
  2350. * @since 3.7.0
  2351. *
  2352. * @param array $fields An array of the attachment metadata keys and labels.
  2353. */
  2354. $fields = apply_filters( 'media_submitbox_misc_sections', array(
  2355. 'length_formatted' => __( 'Length:' ),
  2356. 'bitrate' => __( 'Bitrate:' ),
  2357. ) );
  2358. foreach ( $fields as $key => $label ) {
  2359. if ( empty( $meta[ $key ] ) ) {
  2360. continue;
  2361. }
  2362. ?>
  2363. <div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  2364. <?php echo $label ?> <strong><?php
  2365. switch ( $key ) {
  2366. case 'bitrate' :
  2367. echo round( $meta['bitrate'] / 1000 ) . 'kb/s';
  2368. if ( ! empty( $meta['bitrate_mode'] ) ) {
  2369. echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) );
  2370. }
  2371. break;
  2372. default:
  2373. echo esc_html( $meta[ $key ] );
  2374. break;
  2375. }
  2376. ?></strong>
  2377. </div>
  2378. <?php
  2379. }
  2380. /**
  2381. * Filter the audio attachment metadata fields to be shown in the publish meta box.
  2382. *
  2383. * The key for each item in the array should correspond to an attachment
  2384. * metadata key, and the value should be the desired label.
  2385. *
  2386. * @since 3.7.0
  2387. *
  2388. * @param array $fields An array of the attachment metadata keys and labels.
  2389. */
  2390. $audio_fields = apply_filters( 'audio_submitbox_misc_sections', array(
  2391. 'dataformat' => __( 'Audio Format:' ),
  2392. 'codec' => __( 'Audio Codec:' )
  2393. ) );
  2394. foreach ( $audio_fields as $key => $label ) {
  2395. if ( empty( $meta['audio'][ $key ] ) ) {
  2396. continue;
  2397. }
  2398. ?>
  2399. <div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  2400. <?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][$key] ); ?></strong>
  2401. </div>
  2402. <?php
  2403. }
  2404. }
  2405. if ( $media_dims ) : ?>
  2406. <div class="misc-pub-section misc-pub-dimensions">
  2407. <?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
  2408. </div>
  2409. <?php
  2410. endif;
  2411. }
  2412. add_filter( 'async_upload_image', 'get_media_item', 10, 2 );
  2413. add_filter( 'async_upload_audio', 'get_media_item', 10, 2 );
  2414. add_filter( 'async_upload_video', 'get_media_item', 10, 2 );
  2415. add_filter( 'async_upload_file', 'get_media_item', 10, 2 );
  2416. add_action( 'media_upload_image', 'wp_media_upload_handler' );
  2417. add_action( 'media_upload_audio', 'wp_media_upload_handler' );
  2418. add_action( 'media_upload_video', 'wp_media_upload_handler' );
  2419. add_action( 'media_upload_file', 'wp_media_upload_handler' );
  2420. add_filter( 'media_upload_gallery', 'media_upload_gallery' );
  2421. add_filter( 'media_upload_library', 'media_upload_library' );
  2422. add_action( 'attachment_submitbox_misc_actions', 'attachment_submitbox_metadata' );
  2423. /**
  2424. * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
  2425. *
  2426. * @since 3.6.0
  2427. *
  2428. * @param array $metadata An existing array with data
  2429. * @param array $data Data supplied by ID3 tags
  2430. */
  2431. function wp_add_id3_tag_data( &$metadata, $data ) {
  2432. foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
  2433. if ( ! empty( $data[$version]['comments'] ) ) {
  2434. foreach ( $data[$version]['comments'] as $key => $list ) {
  2435. if ( ! empty( $list ) ) {
  2436. $metadata[$key] = reset( $list );
  2437. // fix bug in byte stream analysis
  2438. if ( 'terms_of_use' === $key && 0 === strpos( $metadata[$key], 'yright notice.' ) )
  2439. $metadata[$key] = 'Cop' . $metadata[$key];
  2440. }
  2441. }
  2442. break;
  2443. }
  2444. }
  2445. if ( ! empty( $data['id3v2']['APIC'] ) ) {
  2446. $image = reset( $data['id3v2']['APIC']);
  2447. if ( ! empty( $image['data'] ) ) {
  2448. $metadata['image'] = array(
  2449. 'data' => $image['data'],
  2450. 'mime' => $image['image_mime'],
  2451. 'width' => $image['image_width'],
  2452. 'height' => $image['image_height']
  2453. );
  2454. }
  2455. } elseif ( ! empty( $data['comments']['picture'] ) ) {
  2456. $image = reset( $data['comments']['picture'] );
  2457. if ( ! empty( $image['data'] ) ) {
  2458. $metadata['image'] = array(
  2459. 'data' => $image['data'],
  2460. 'mime' => $image['image_mime']
  2461. );
  2462. }
  2463. }
  2464. }
  2465. /**
  2466. * Retrieve metadata from a video file's ID3 tags
  2467. *
  2468. * @since 3.6.0
  2469. *
  2470. * @param string $file Path to file.
  2471. * @return array|boolean Returns array of metadata, if found.
  2472. */
  2473. function wp_read_video_metadata( $file ) {
  2474. if ( ! file_exists( $file ) )
  2475. return false;
  2476. $metadata = array();
  2477. if ( ! class_exists( 'getID3' ) )
  2478. require( ABSPATH . WPINC . '/ID3/getid3.php' );
  2479. $id3 = new getID3();
  2480. $data = $id3->analyze( $file );
  2481. if ( isset( $data['video']['lossless'] ) )
  2482. $metadata['lossless'] = $data['video']['lossless'];
  2483. if ( ! empty( $data['video']['bitrate'] ) )
  2484. $metadata['bitrate'] = (int) $data['video']['bitrate'];
  2485. if ( ! empty( $data['video']['bitrate_mode'] ) )
  2486. $metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
  2487. if ( ! empty( $data['filesize'] ) )
  2488. $metadata['filesize'] = (int) $data['filesize'];
  2489. if ( ! empty( $data['mime_type'] ) )
  2490. $metadata['mime_type'] = $data['mime_type'];
  2491. if ( ! empty( $data['playtime_seconds'] ) )
  2492. $metadata['length'] = (int) ceil( $data['playtime_seconds'] );
  2493. if ( ! empty( $data['playtime_string'] ) )
  2494. $metadata['length_formatted'] = $data['playtime_string'];
  2495. if ( ! empty( $data['video']['resolution_x'] ) )
  2496. $metadata['width'] = (int) $data['video']['resolution_x'];
  2497. if ( ! empty( $data['video']['resolution_y'] ) )
  2498. $metadata['height'] = (int) $data['video']['resolution_y'];
  2499. if ( ! empty( $data['fileformat'] ) )
  2500. $metadata['fileformat'] = $data['fileformat'];
  2501. if ( ! empty( $data['video']['dataformat'] ) )
  2502. $metadata['dataformat'] = $data['video']['dataformat'];
  2503. if ( ! empty( $data['video']['encoder'] ) )
  2504. $metadata['encoder'] = $data['video']['encoder'];
  2505. if ( ! empty( $data['video']['codec'] ) )
  2506. $metadata['codec'] = $data['video']['codec'];
  2507. if ( ! empty( $data['audio'] ) ) {
  2508. unset( $data['audio']['streams'] );
  2509. $metadata['audio'] = $data['audio'];
  2510. }
  2511. wp_add_id3_tag_data( $metadata, $data );
  2512. return $metadata;
  2513. }
  2514. /**
  2515. * Retrieve metadata from a audio file's ID3 tags
  2516. *
  2517. * @since 3.6.0
  2518. *
  2519. * @param string $file Path to file.
  2520. * @return array|boolean Returns array of metadata, if found.
  2521. */
  2522. function wp_read_audio_metadata( $file ) {
  2523. if ( ! file_exists( $file ) )
  2524. return false;
  2525. $metadata = array();
  2526. if ( ! class_exists( 'getID3' ) )
  2527. require( ABSPATH . WPINC . '/ID3/getid3.php' );
  2528. $id3 = new getID3();
  2529. $data = $id3->analyze( $file );
  2530. if ( ! empty( $data['audio'] ) ) {
  2531. unset( $data['audio']['streams'] );
  2532. $metadata = $data['audio'];
  2533. }
  2534. if ( ! empty( $data['fileformat'] ) )
  2535. $metadata['fileformat'] = $data['fileformat'];
  2536. if ( ! empty( $data['filesize'] ) )
  2537. $metadata['filesize'] = (int) $data['filesize'];
  2538. if ( ! empty( $data['mime_type'] ) )
  2539. $metadata['mime_type'] = $data['mime_type'];
  2540. if ( ! empty( $data['playtime_seconds'] ) )
  2541. $metadata['length'] = (int) ceil( $data['playtime_seconds'] );
  2542. if ( ! empty( $data['playtime_string'] ) )
  2543. $metadata['length_formatted'] = $data['playtime_string'];
  2544. wp_add_id3_tag_data( $metadata, $data );
  2545. return $metadata;
  2546. }