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

/wp-admin/includes/media.php

https://github.com/davodey/WordPress
PHP | 2973 lines | 2906 code | 18 blank | 49 comment | 12 complexity | 6c0d12ed59dfdb08b1bc622d6d62f6fc MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * WordPress Administration Media API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Defines the default media upload tabs
  10. *
  11. * @since 2.5.0
  12. *
  13. * @return array default tabs
  14. */
  15. function media_upload_tabs() {
  16. $_default_tabs = array(
  17. 'type' => __('From Computer'), // handler action suffix => tab text
  18. 'type_url' => __('From URL'),
  19. 'gallery' => __('Gallery'),
  20. 'library' => __('Media Library')
  21. );
  22. /**
  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. // Set variables for storage
  685. // fix file filename for query strings
  686. preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
  687. $file_array['name'] = basename( $matches[0] );
  688. // Download file to temp location
  689. $file_array['tmp_name'] = download_url( $file );
  690. // If error storing temporarily, return the error.
  691. if ( is_wp_error( $file_array['tmp_name'] ) ) {
  692. return $file_array['tmp_name'];
  693. }
  694. // do the validation and storage stuff
  695. $id = media_handle_sideload( $file_array, $post_id, $desc );
  696. // If error storing permanently, unlink
  697. if ( is_wp_error( $id ) ) {
  698. @unlink( $file_array['tmp_name'] );
  699. return $id;
  700. }
  701. $src = wp_get_attachment_url( $id );
  702. }
  703. // Finally check to make sure the file has been saved, then return the html
  704. if ( ! empty( $src ) ) {
  705. $alt = isset( $desc ) ? esc_attr( $desc ) : '';
  706. $html = "<img src='$src' alt='$alt' />";
  707. return $html;
  708. }
  709. }
  710. /**
  711. * {@internal Missing Short Description}}
  712. *
  713. * @since 2.5.0
  714. *
  715. * @return unknown
  716. */
  717. function media_upload_gallery() {
  718. $errors = array();
  719. if ( !empty($_POST) ) {
  720. $return = media_upload_form_handler();
  721. if ( is_string($return) )
  722. return $return;
  723. if ( is_array($return) )
  724. $errors = $return;
  725. }
  726. wp_enqueue_script('admin-gallery');
  727. return wp_iframe( 'media_upload_gallery_form', $errors );
  728. }
  729. /**
  730. * {@internal Missing Short Description}}
  731. *
  732. * @since 2.5.0
  733. *
  734. * @return unknown
  735. */
  736. function media_upload_library() {
  737. $errors = array();
  738. if ( !empty($_POST) ) {
  739. $return = media_upload_form_handler();
  740. if ( is_string($return) )
  741. return $return;
  742. if ( is_array($return) )
  743. $errors = $return;
  744. }
  745. return wp_iframe( 'media_upload_library_form', $errors );
  746. }
  747. /**
  748. * Retrieve HTML for the image alignment radio buttons with the specified one checked.
  749. *
  750. * @since 2.7.0
  751. *
  752. * @param object $post
  753. * @param string $checked
  754. * @return string
  755. */
  756. function image_align_input_fields( $post, $checked = '' ) {
  757. if ( empty($checked) )
  758. $checked = get_user_setting('align', 'none');
  759. $alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
  760. if ( !array_key_exists( (string) $checked, $alignments ) )
  761. $checked = 'none';
  762. $out = array();
  763. foreach ( $alignments as $name => $label ) {
  764. $name = esc_attr($name);
  765. $out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
  766. ( $checked == $name ? " checked='checked'" : "" ) .
  767. " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
  768. }
  769. return join("\n", $out);
  770. }
  771. /**
  772. * Retrieve HTML for the size radio buttons with the specified one checked.
  773. *
  774. * @since 2.7.0
  775. *
  776. * @param object $post
  777. * @param bool|string $check
  778. * @return array
  779. */
  780. function image_size_input_fields( $post, $check = '' ) {
  781. /**
  782. * Filter the names and labels of the default image sizes.
  783. *
  784. * @since 3.3.0
  785. *
  786. * @param array $size_names Array of image sizes and their names. Default values
  787. * include 'Thumbnail', 'Medium', 'Large', 'Full Size'.
  788. */
  789. $size_names = apply_filters( 'image_size_names_choose', array(
  790. 'thumbnail' => __( 'Thumbnail' ),
  791. 'medium' => __( 'Medium' ),
  792. 'large' => __( 'Large' ),
  793. 'full' => __( 'Full Size' )
  794. ) );
  795. if ( empty($check) )
  796. $check = get_user_setting('imgsize', 'medium');
  797. foreach ( $size_names as $size => $label ) {
  798. $downsize = image_downsize($post->ID, $size);
  799. $checked = '';
  800. // is this size selectable?
  801. $enabled = ( $downsize[3] || 'full' == $size );
  802. $css_id = "image-size-{$size}-{$post->ID}";
  803. // if this size is the default but that's not available, don't select it
  804. if ( $size == $check ) {
  805. if ( $enabled )
  806. $checked = " checked='checked'";
  807. else
  808. $check = '';
  809. } elseif ( !$check && $enabled && 'thumbnail' != $size ) {
  810. // if $check is not enabled, default to the first available size that's bigger than a thumbnail
  811. $check = $size;
  812. $checked = " checked='checked'";
  813. }
  814. $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 />";
  815. $html .= "<label for='{$css_id}'>$label</label>";
  816. // only show the dimensions if that choice is available
  817. if ( $enabled )
  818. $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
  819. $html .= '</div>';
  820. $out[] = $html;
  821. }
  822. return array(
  823. 'label' => __('Size'),
  824. 'input' => 'html',
  825. 'html' => join("\n", $out),
  826. );
  827. }
  828. /**
  829. * Retrieve HTML for the Link URL buttons with the default link type as specified.
  830. *
  831. * @since 2.7.0
  832. *
  833. * @param object $post
  834. * @param string $url_type
  835. * @return string
  836. */
  837. function image_link_input_fields($post, $url_type = '') {
  838. $file = wp_get_attachment_url($post->ID);
  839. $link = get_attachment_link($post->ID);
  840. if ( empty($url_type) )
  841. $url_type = get_user_setting('urlbutton', 'post');
  842. $url = '';
  843. if ( $url_type == 'file' )
  844. $url = $file;
  845. elseif ( $url_type == 'post' )
  846. $url = $link;
  847. return "
  848. <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
  849. <button type='button' class='button urlnone' data-link-url=''>" . __('None') . "</button>
  850. <button type='button' class='button urlfile' data-link-url='" . esc_attr($file) . "'>" . __('File URL') . "</button>
  851. <button type='button' class='button urlpost' data-link-url='" . esc_attr($link) . "'>" . __('Attachment Post URL') . "</button>
  852. ";
  853. }
  854. function wp_caption_input_textarea($edit_post) {
  855. // post data is already escaped
  856. $name = "attachments[{$edit_post->ID}][post_excerpt]";
  857. return '<textarea name="' . $name . '" id="' . $name . '">' . $edit_post->post_excerpt . '</textarea>';
  858. }
  859. /**
  860. * {@internal Missing Short Description}}
  861. *
  862. * @since 2.5.0
  863. *
  864. * @param array $form_fields
  865. * @param object $post
  866. * @return array
  867. */
  868. function image_attachment_fields_to_edit($form_fields, $post) {
  869. return $form_fields;
  870. }
  871. /**
  872. * {@internal Missing Short Description}}
  873. *
  874. * @since 2.5.0
  875. *
  876. * @param array $form_fields An array of attachment form fields.
  877. * @param WP_Post $post The WP_Post attachment object.
  878. * @return array Filtered attachment form fields.
  879. */
  880. function media_single_attachment_fields_to_edit( $form_fields, $post ) {
  881. unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
  882. return $form_fields;
  883. }
  884. /**
  885. * {@internal Missing Short Description}}
  886. *
  887. * @since 2.8.0
  888. *
  889. * @param array $form_fields An array of attachment form fields.
  890. * @param WP_Post $post The WP_Post attachment object.
  891. * @return array Filtered attachment form fields.
  892. */
  893. function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
  894. unset($form_fields['image_url']);
  895. return $form_fields;
  896. }
  897. /**
  898. * Filters input from media_upload_form_handler() and assigns a default
  899. * post_title from the file name if none supplied.
  900. *
  901. * Illustrates the use of the attachment_fields_to_save filter
  902. * which can be used to add default values to any field before saving to DB.
  903. *
  904. * @since 2.5.0
  905. *
  906. * @param WP_Post $post The WP_Post attachment object.
  907. * @param array $attachment An array of attachment metadata.
  908. * @return array Filtered attachment post object.
  909. */
  910. function image_attachment_fields_to_save( $post, $attachment ) {
  911. if ( substr( $post['post_mime_type'], 0, 5 ) == 'image' ) {
  912. if ( strlen( trim( $post['post_title'] ) ) == 0 ) {
  913. $attachment_url = ( isset( $post['attachment_url'] ) ) ? $post['attachment_url'] : $post['guid'];
  914. $post['post_title'] = preg_replace( '/\.\w+$/', '', wp_basename( $attachment_url ) );
  915. $post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
  916. }
  917. }
  918. return $post;
  919. }
  920. add_filter( 'attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2 );
  921. /**
  922. * {@internal Missing Short Description}}
  923. *
  924. * @since 2.5.0
  925. *
  926. * @param string $html
  927. * @param integer $attachment_id
  928. * @param array $attachment
  929. * @return array
  930. */
  931. function image_media_send_to_editor($html, $attachment_id, $attachment) {
  932. $post = get_post($attachment_id);
  933. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  934. $url = $attachment['url'];
  935. $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
  936. $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
  937. $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
  938. $rel = ( $url == get_attachment_link($attachment_id) );
  939. return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
  940. }
  941. return $html;
  942. }
  943. add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
  944. /**
  945. * {@internal Missing Short Description}}
  946. *
  947. * @since 2.5.0
  948. *
  949. * @param object $post
  950. * @param array $errors
  951. * @return array
  952. */
  953. function get_attachment_fields_to_edit($post, $errors = null) {
  954. if ( is_int($post) )
  955. $post = get_post($post);
  956. if ( is_array($post) )
  957. $post = new WP_Post( (object) $post );
  958. $image_url = wp_get_attachment_url($post->ID);
  959. $edit_post = sanitize_post($post, 'edit');
  960. $form_fields = array(
  961. 'post_title' => array(
  962. 'label' => __('Title'),
  963. 'value' => $edit_post->post_title
  964. ),
  965. 'image_alt' => array(),
  966. 'post_excerpt' => array(
  967. 'label' => __('Caption'),
  968. 'input' => 'html',
  969. 'html' => wp_caption_input_textarea($edit_post)
  970. ),
  971. 'post_content' => array(
  972. 'label' => __('Description'),
  973. 'value' => $edit_post->post_content,
  974. 'input' => 'textarea'
  975. ),
  976. 'url' => array(
  977. 'label' => __('Link URL'),
  978. 'input' => 'html',
  979. 'html' => image_link_input_fields($post, get_option('image_default_link_type')),
  980. 'helps' => __('Enter a link URL or click above for presets.')
  981. ),
  982. 'menu_order' => array(
  983. 'label' => __('Order'),
  984. 'value' => $edit_post->menu_order
  985. ),
  986. 'image_url' => array(
  987. 'label' => __('File URL'),
  988. 'input' => 'html',
  989. 'html' => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
  990. 'value' => wp_get_attachment_url($post->ID),
  991. 'helps' => __('Location of the uploaded file.')
  992. )
  993. );
  994. foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  995. $t = (array) get_taxonomy($taxonomy);
  996. if ( ! $t['public'] || ! $t['show_ui'] )
  997. continue;
  998. if ( empty($t['label']) )
  999. $t['label'] = $taxonomy;
  1000. if ( empty($t['args']) )
  1001. $t['args'] = array();
  1002. $terms = get_object_term_cache($post->ID, $taxonomy);
  1003. if ( false === $terms )
  1004. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  1005. $values = array();
  1006. foreach ( $terms as $term )
  1007. $values[] = $term->slug;
  1008. $t['value'] = join(', ', $values);
  1009. $form_fields[$taxonomy] = $t;
  1010. }
  1011. // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  1012. // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
  1013. $form_fields = array_merge_recursive($form_fields, (array) $errors);
  1014. // This was formerly in image_attachment_fields_to_edit().
  1015. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  1016. $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
  1017. if ( empty($alt) )
  1018. $alt = '';
  1019. $form_fields['post_title']['required'] = true;
  1020. $form_fields['image_alt'] = array(
  1021. 'value' => $alt,
  1022. 'label' => __('Alternative Text'),
  1023. 'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
  1024. );
  1025. $form_fields['align'] = array(
  1026. 'label' => __('Alignment'),
  1027. 'input' => 'html',
  1028. 'html' => image_align_input_fields($post, get_option('image_default_align')),
  1029. );
  1030. $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
  1031. } else {
  1032. unset( $form_fields['image_alt'] );
  1033. }
  1034. /**
  1035. * Filter the attachment fields to edit.
  1036. *
  1037. * @since 2.5.0
  1038. *
  1039. * @param array $form_fields An array of attachment form fields.
  1040. * @param WP_Post $post The WP_Post attachment object.
  1041. */
  1042. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1043. return $form_fields;
  1044. }
  1045. /**
  1046. * Retrieve HTML for media items of post gallery.
  1047. *
  1048. * The HTML markup retrieved will be created for the progress of SWF Upload
  1049. * component. Will also create link for showing and hiding the form to modify
  1050. * the image attachment.
  1051. *
  1052. * @since 2.5.0
  1053. *
  1054. * @param int $post_id Optional. Post ID.
  1055. * @param array $errors Errors for attachment, if any.
  1056. * @return string
  1057. */
  1058. function get_media_items( $post_id, $errors ) {
  1059. $attachments = array();
  1060. if ( $post_id ) {
  1061. $post = get_post($post_id);
  1062. if ( $post && $post->post_type == 'attachment' )
  1063. $attachments = array($post->ID => $post);
  1064. else
  1065. $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
  1066. } else {
  1067. if ( is_array($GLOBALS['wp_the_query']->posts) )
  1068. foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
  1069. $attachments[$attachment->ID] = $attachment;
  1070. }
  1071. $output = '';
  1072. foreach ( (array) $attachments as $id => $attachment ) {
  1073. if ( $attachment->post_status == 'trash' )
  1074. continue;
  1075. if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
  1076. $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>";
  1077. }
  1078. return $output;
  1079. }
  1080. /**
  1081. * Retrieve HTML form for modifying the image attachment.
  1082. *
  1083. * @since 2.5.0
  1084. *
  1085. * @param int $attachment_id Attachment ID for modification.
  1086. * @param string|array $args Optional. Override defaults.
  1087. * @return string HTML form for attachment.
  1088. */
  1089. function get_media_item( $attachment_id, $args = null ) {
  1090. global $redir_tab;
  1091. if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
  1092. $thumb_url = $thumb_url[0];
  1093. else
  1094. $thumb_url = false;
  1095. $post = get_post( $attachment_id );
  1096. $current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
  1097. $default_args = array(
  1098. 'errors' => null,
  1099. 'send' => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true,
  1100. 'delete' => true,
  1101. 'toggle' => true,
  1102. 'show_title' => true
  1103. );
  1104. $args = wp_parse_args( $args, $default_args );
  1105. /**
  1106. * Filter the arguments used to retrieve an image for the edit image form.
  1107. *
  1108. * @since 3.1.0
  1109. *
  1110. * @see get_media_item
  1111. *
  1112. * @param array $args An array of arguments.
  1113. */
  1114. $r = apply_filters( 'get_media_item_args', $args );
  1115. $toggle_on = __( 'Show' );
  1116. $toggle_off = __( 'Hide' );
  1117. $filename = esc_html( wp_basename( $post->guid ) );
  1118. $title = esc_attr( $post->post_title );
  1119. $post_mime_types = get_post_mime_types();
  1120. $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
  1121. $type = array_shift( $keys );
  1122. $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
  1123. $form_fields = get_attachment_fields_to_edit( $post, $r['errors'] );
  1124. if ( $r['toggle'] ) {
  1125. $class = empty( $r['errors'] ) ? 'startclosed' : 'startopen';
  1126. $toggle_links = "
  1127. <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
  1128. <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
  1129. } else {
  1130. $class = '';
  1131. $toggle_links = '';
  1132. }
  1133. $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
  1134. $display_title = $r['show_title'] ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '&hellip;' ) . "</span></div>" : '';
  1135. $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
  1136. $order = '';
  1137. foreach ( $form_fields as $key => $val ) {
  1138. if ( 'menu_order' == $key ) {
  1139. if ( $gallery )
  1140. $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>";
  1141. else
  1142. $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
  1143. unset( $form_fields['menu_order'] );
  1144. break;
  1145. }
  1146. }
  1147. $media_dims = '';
  1148. $meta = wp_get_attachment_metadata( $post->ID );
  1149. if ( isset( $meta['width'], $meta['height'] ) )
  1150. $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  1151. /**
  1152. * Filter the media metadata.
  1153. *
  1154. * @since 2.5.0
  1155. *
  1156. * @param string $media_dims The HTML markup containing the media dimensions.
  1157. * @param WP_Post $post The WP_Post attachment object.
  1158. */
  1159. $media_dims = apply_filters( 'media_meta', $media_dims, $post );
  1160. $image_edit_button = '';
  1161. if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  1162. $nonce = wp_create_nonce( "image_editor-$post->ID" );
  1163. $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>";
  1164. }
  1165. $attachment_url = get_permalink( $attachment_id );
  1166. $item = "
  1167. $type_html
  1168. $toggle_links
  1169. $order
  1170. $display_title
  1171. <table class='slidetoggle describe $class'>
  1172. <thead class='media-item-info' id='media-head-$post->ID'>
  1173. <tr>
  1174. <td class='A1B1' id='thumbnail-head-$post->ID'>
  1175. <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
  1176. <p>$image_edit_button</p>
  1177. </td>
  1178. <td>
  1179. <p><strong>" . __('File name:') . "</strong> $filename</p>
  1180. <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
  1181. <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( get_option('date_format'), $post->post_date ). '</p>';
  1182. if ( !empty( $media_dims ) )
  1183. $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
  1184. $item .= "</td></tr>\n";
  1185. $item .= "
  1186. </thead>
  1187. <tbody>
  1188. <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>
  1189. <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n";
  1190. $defaults = array(
  1191. 'input' => 'text',
  1192. 'required' => false,
  1193. 'value' => '',
  1194. 'extra_rows' => array(),
  1195. );
  1196. if ( $r['send'] ) {
  1197. $r['send'] = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
  1198. }
  1199. $delete = empty( $r['delete'] ) ? '' : $r['delete'];
  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. }
  1223. if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
  1224. && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
  1225. $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
  1226. $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>";
  1227. }
  1228. if ( ( $r['send'] || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) ) {
  1229. $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>" . $r['send'] . " $thumbnail $delete</td></tr>\n" );
  1230. }
  1231. $hidden_fields = array();
  1232. foreach ( $form_fields as $id => $field ) {
  1233. if ( $id[0] == '_' )
  1234. continue;
  1235. if ( !empty( $field['tr'] ) ) {
  1236. $item .= $field['tr'];
  1237. continue;
  1238. }
  1239. $field = array_merge( $defaults, $field );
  1240. $name = "attachments[$attachment_id][$id]";
  1241. if ( $field['input'] == 'hidden' ) {
  1242. $hidden_fields[$name] = $field['value'];
  1243. continue;
  1244. }
  1245. $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
  1246. $aria_required = $field['required'] ? " aria-required='true' " : '';
  1247. $class = $id;
  1248. $class .= $field['required'] ? ' form-required' : '';
  1249. $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'>";
  1250. if ( !empty( $field[ $field['input'] ] ) )
  1251. $item .= $field[ $field['input'] ];
  1252. elseif ( $field['input'] == 'textarea' ) {
  1253. if ( 'post_content' == $id && user_can_richedit() ) {
  1254. // sanitize_post() skips the post_content when user_can_richedit
  1255. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1256. }
  1257. // post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit()
  1258. $item .= "<textarea id='$name' name='$name' $aria_required>" . $field['value'] . '</textarea>';
  1259. } else {
  1260. $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
  1261. }
  1262. if ( !empty( $field['helps'] ) )
  1263. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1264. $item .= "</td>\n\t\t</tr>\n";
  1265. $extra_rows = array();
  1266. if ( !empty( $field['errors'] ) )
  1267. foreach ( array_unique( (array) $field['errors'] ) as $error )
  1268. $extra_rows['error'][] = $error;
  1269. if ( !empty( $field['extra_rows'] ) )
  1270. foreach ( $field['extra_rows'] as $class => $rows )
  1271. foreach ( (array) $rows as $html )
  1272. $extra_rows[$class][] = $html;
  1273. foreach ( $extra_rows as $class => $rows )
  1274. foreach ( $rows as $html )
  1275. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1276. }
  1277. if ( !empty( $form_fields['_final'] ) )
  1278. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1279. $item .= "\t</tbody>\n";
  1280. $item .= "\t</table>\n";
  1281. foreach ( $hidden_fields as $name => $value )
  1282. $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
  1283. if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
  1284. $parent = (int) $_REQUEST['post_id'];
  1285. $parent_name = "attachments[$attachment_id][post_parent]";
  1286. $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
  1287. }
  1288. return $item;
  1289. }
  1290. function get_compat_media_markup( $attachment_id, $args = null ) {
  1291. $post = get_post( $attachment_id );
  1292. $default_args = array(
  1293. 'errors' => null,
  1294. 'in_modal' => false,
  1295. );
  1296. $user_can_edit = current_user_can( 'edit_post', $attachment_id );
  1297. $args = wp_parse_args( $args, $default_args );
  1298. /** This filter is documented in wp-admin/includes/media.php */
  1299. $args = apply_filters( 'get_media_item_args', $args );
  1300. $form_fields = array();
  1301. if ( $args['in_modal'] ) {
  1302. foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  1303. $t = (array) get_taxonomy($taxonomy);
  1304. if ( ! $t['public'] || ! $t['show_ui'] )
  1305. continue;
  1306. if ( empty($t['label']) )
  1307. $t['label'] = $taxonomy;
  1308. if ( empty($t['args']) )
  1309. $t['args'] = array();
  1310. $terms = get_object_term_cache($post->ID, $taxonomy);
  1311. if ( false === $terms )
  1312. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  1313. $values = array();
  1314. foreach ( $terms as $term )
  1315. $values[] = $term->slug;
  1316. $t['value'] = join(', ', $values);
  1317. $t['taxonomy'] = true;
  1318. $form_fields[$taxonomy] = $t;
  1319. }
  1320. }
  1321. // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  1322. // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
  1323. $form_fields = array_merge_recursive($form_fields, (array) $args['errors'] );
  1324. /** This filter is documented in wp-admin/includes/media.php */
  1325. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1326. unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],
  1327. $form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],
  1328. $form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] );
  1329. /** This filter is documented in wp-admin/includes/media.php */
  1330. $media_meta = apply_filters( 'media_meta', '', $post );
  1331. $defaults = array(
  1332. 'input' => 'text',
  1333. 'required' => false,
  1334. 'value' => '',
  1335. 'extra_rows' => array(),
  1336. 'show_in_edit' => true,
  1337. 'show_in_modal' => true,
  1338. );
  1339. $hidden_fields = array();
  1340. $item = '';
  1341. foreach ( $form_fields as $id => $field ) {
  1342. if ( $id[0] == '_' )
  1343. continue;
  1344. $name = "attachments[$attachment_id][$id]";
  1345. $id_attr = "attachments-$attachment_id-$id";
  1346. if ( !empty( $field['tr'] ) ) {
  1347. $item .= $field['tr'];
  1348. continue;
  1349. }
  1350. $field = array_merge( $defaults, $field );
  1351. if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) )
  1352. continue;
  1353. if ( $field['input'] == 'hidden' ) {
  1354. $hidden_fields[$name] = $field['value'];
  1355. continue;
  1356. }
  1357. $readonly = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
  1358. $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
  1359. $aria_required = $field['required'] ? " aria-required='true' " : '';
  1360. $class = 'compat-field-' . $id;
  1361. $class .= $field['required'] ? ' form-required' : '';
  1362. $item .= "\t\t<tr class='$class'>";
  1363. $item .= "\t\t\t<th scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
  1364. $item .= "</th>\n\t\t\t<td class='field'>";
  1365. if ( !empty( $field[ $field['input'] ] ) )
  1366. $item .= $field[ $field['input'] ];
  1367. elseif ( $field['input'] == 'textarea' ) {
  1368. if ( 'post_content' == $id && user_can_richedit() ) {
  1369. // sanitize_post() skips the post_content when user_can_richedit
  1370. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1371. }
  1372. $item .= "<textarea id='$id_attr' name='$name' $aria_required>" . $field['value'] . '</textarea>';
  1373. } else {
  1374. $item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly $aria_required />";
  1375. }
  1376. if ( !empty( $field['helps'] ) )
  1377. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1378. $item .= "</td>\n\t\t</tr>\n";
  1379. $extra_rows = array();
  1380. if ( !empty( $field['errors'] ) )
  1381. foreach ( array_unique( (array) $field['errors'] ) as $error )
  1382. $extra_rows['error'][] = $error;
  1383. if ( !empty( $field['extra_rows'] ) )
  1384. foreach ( $field['extra_rows'] as $class => $rows )
  1385. foreach ( (array) $rows as $html )
  1386. $extra_rows[$class][] = $html;
  1387. foreach ( $extra_rows as $class => $rows )
  1388. foreach ( $rows as $html )
  1389. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1390. }
  1391. if ( !empty( $form_fields['_final'] ) )
  1392. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1393. if ( $item )
  1394. $item = '<table class="compat-attachment-fields">' . $item . '</table>';
  1395. foreach ( $hidden_fields as $hidden_field => $value ) {
  1396. $item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n";
  1397. }
  1398. if ( $item )
  1399. $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr( $post->menu_order ) . '" />' . $item;
  1400. return array(
  1401. 'item' => $item,
  1402. 'meta' => $media_meta,
  1403. );
  1404. }
  1405. /**
  1406. * {@internal Missing Short Description}}
  1407. *
  1408. * @since 2.5.0
  1409. */
  1410. function media_upload_header() {
  1411. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1412. echo '<script type="text/javascript">post_id = ' . $post_id . ";</script>\n";
  1413. if ( empty( $_GET['chromeless'] ) ) {
  1414. echo '<div id="media-upload-header">';
  1415. the_media_upload_tabs();
  1416. echo '</div>';
  1417. }
  1418. }
  1419. /**
  1420. * {@internal Missing Short Description}}
  1421. *
  1422. * @since 2.5.0
  1423. *
  1424. * @param unknown_type $errors
  1425. */
  1426. function media_upload_form( $errors = null ) {
  1427. global $type, $tab, $is_IE, $is_opera;
  1428. if ( ! _device_can_upload() ) {
  1429. 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>';
  1430. return;
  1431. }
  1432. $upload_action_url = admin_url('async-upload.php');
  1433. $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
  1434. $_type = isset($type) ? $type : '';
  1435. $_tab = isset($tab) ? $tab : '';
  1436. $max_upload_size = wp_max_upload_size();
  1437. if ( ! $max_upload_size ) {
  1438. $max_upload_size = 0;
  1439. }
  1440. ?>
  1441. <div id="media-upload-notice"><?php
  1442. if (isset($errors['upload_notice']) )
  1443. echo $errors['upload_notice'];
  1444. ?></div>
  1445. <div id="media-upload-error"><?php
  1446. if (isset($errors['upload_error']) && is_wp_error($errors['upload_error']))
  1447. echo $errors['upload_error']->get_error_message();
  1448. ?></div>
  1449. <?php
  1450. if ( is_multisite() && !is_upload_space_available() ) {
  1451. /**
  1452. * Fires when an upload will exceed the defined upload space quota for a network site.
  1453. *
  1454. * @since 3.5.0
  1455. */
  1456. do_action( 'upload_ui_over_quota' );
  1457. return;
  1458. }
  1459. /**
  1460. * Fires just before the legacy (pre-3.5.0) upload interface is loaded.
  1461. *
  1462. * @since 2.6.0
  1463. */
  1464. do_action( 'pre-upload-ui' );
  1465. $post_params = array(
  1466. "post_id" => $post_id,
  1467. "_wpnonce" => wp_create_nonce('media-form'),
  1468. "type" => $_type,
  1469. "tab" => $_tab,
  1470. "short" => "1",
  1471. );
  1472. /**
  1473. * Filter the media upload post parameters.
  1474. *
  1475. * @since 3.1.0 As 'swfupload_post_params'
  1476. * @since 3.3.0
  1477. *
  1478. * @param array $post_params An array of media upload parameters used by Plupload.
  1479. */
  1480. $post_params = apply_filters( 'upload_post_params', $post_params );
  1481. $plupload_init = array(
  1482. 'runtimes' => 'html5,flash,silverlight,html4',
  1483. 'browse_button' => 'plupload-browse-button',
  1484. 'container' => 'plupload-upload-ui',
  1485. 'drop_element' => 'drag-drop-area',
  1486. 'file_data_name' => 'async-upload',
  1487. 'url' => $upload_action_url,
  1488. 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),
  1489. 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
  1490. 'filters' => array(
  1491. 'max_file_size' => $max_upload_size . 'b',
  1492. ),
  1493. 'multipart_params' => $post_params,
  1494. );
  1495. // Multi-file uploading doesn't currently work in iOS Safari,
  1496. // single-file allows the built-in camera to be used as source for images
  1497. if ( wp_is_mobile() )
  1498. $plupload_init['multi_selection'] = false;
  1499. /**
  1500. * Filter the default Plupload settings.
  1501. *
  1502. * @since 3.3.0
  1503. *
  1504. * @param array $plupload_init An array of default settings used by Plupload.
  1505. */
  1506. $plupload_init = apply_filters( 'plupload_init', $plupload_init );
  1507. ?>
  1508. <script type="text/javascript">
  1509. <?php
  1510. // Verify size is an int. If not return default value.
  1511. $large_size_h = absint( get_option('large_size_h') );
  1512. if( !$large_size_h )
  1513. $large_size_h = 1024;
  1514. $large_size_w = absint( get_option('large_size_w') );
  1515. if( !$large_size_w )
  1516. $large_size_w = 1024;
  1517. ?>
  1518. var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
  1519. wpUploaderInit = <?php echo json_encode($plupload_init); ?>;
  1520. </script>
  1521. <div id="plupload-upload-ui" class="hide-if-no-js">
  1522. <?php
  1523. /**
  1524. * Fires before the upload interface loads.
  1525. *
  1526. * @since 2.6.0 As 'pre-flash-upload-ui'
  1527. * @since 3.3.0
  1528. */
  1529. do_action( 'pre-plupload-upload-ui' ); ?>
  1530. <div id="drag-drop-area">
  1531. <div class="drag-drop-inside">
  1532. <p class="drag-drop-info"><?php _e('Drop files here'); ?></p>
  1533. <p><?php _ex('or', 'Uploader: Drop files here - or - Select Files'); ?></p>
  1534. <p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e('Select Files'); ?>" class="button" /></p>
  1535. </div>
  1536. </div>
  1537. <?php
  1538. /**
  1539. * Fires after the upload interface loads.
  1540. *
  1541. * @since 2.6.0 As 'post-flash-upload-ui'
  1542. * @since 3.3.0
  1543. */
  1544. do_action( 'post-plupload-upload-ui' ); ?>
  1545. </div>
  1546. <div id="html-upload-ui" class="hide-if-js">
  1547. <?php
  1548. /**
  1549. * Fires before the upload button in the media upload interface.
  1550. *
  1551. * @since 2.6.0
  1552. */
  1553. do_action( 'pre-html-upload-ui' );
  1554. ?>
  1555. <p id="async-upload-wrap">
  1556. <label class="screen-reader-text" for="async-upload"><?php _e('Upload'); ?></label>
  1557. <input type="file" name="async-upload" id="async-upload" />
  1558. <?php submit_button( __( 'Upload' ), 'button', 'html-upload', false ); ?>
  1559. <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e('Cancel'); ?></a>
  1560. </p>
  1561. <div class="clear"></div>
  1562. <?php
  1563. /**
  1564. * Fires after the upload button in the media upload interface.
  1565. *
  1566. * @since 2.6.0
  1567. */
  1568. do_action( 'post-html-upload-ui' );
  1569. ?>
  1570. </div>
  1571. <span class="max-upload-size"><?php printf( __( 'Maximum upload file size: %s.' ), esc_html( size_format( $max_upload_size ) ) ); ?></span>
  1572. <?php
  1573. /**
  1574. * Fires on the post upload UI screen.
  1575. *
  1576. * Legacy (pre-3.5.0) media workflow hook.
  1577. *
  1578. * @since 2.6.0
  1579. */
  1580. do_action( 'post-upload-ui' );
  1581. }
  1582. /**
  1583. * {@internal Missing Short Description}}
  1584. *
  1585. * @since 2.5.0
  1586. *
  1587. * @param string $type
  1588. * @param object $errors
  1589. * @param integer $id
  1590. */
  1591. function media_upload_type_form($type = 'file', $errors = null, $id = null) {
  1592. media_upload_header();
  1593. $post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
  1594. $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
  1595. /**
  1596. * Filter the media upload form action URL.
  1597. *
  1598. * @since 2.6.0
  1599. *
  1600. * @param string $form_action_url The media upload form action URL.
  1601. * @param string $type The type of media. Default 'file'.
  1602. */
  1603. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1604. $form_class = 'media-upload-form type-form validate';
  1605. if ( get_user_setting('uploader') )
  1606. $form_class .= ' html-uploader';
  1607. ?>
  1608. <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">
  1609. <?php submit_button( '', 'hidden', 'save', false ); ?>
  1610. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1611. <?php wp_nonce_field('media-form'); ?>
  1612. <h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
  1613. <?php media_upload_form( $errors ); ?>
  1614. <script type="text/javascript">
  1615. //<![CDATA[
  1616. jQuery(function($){
  1617. var preloaded = $(".media-item.preloaded");
  1618. if ( preloaded.length > 0 ) {
  1619. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1620. }
  1621. updateMediaForm();
  1622. });
  1623. //]]>
  1624. </script>
  1625. <div id="media-items"><?php
  1626. if ( $id ) {
  1627. if ( !is_wp_error($id) ) {
  1628. add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
  1629. echo get_media_items( $id, $errors );
  1630. } else {
  1631. echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div></div>';
  1632. exit;
  1633. }
  1634. }
  1635. ?></div>
  1636. <p class="savebutton ml-submit">
  1637. <?php submit_button( __( 'Save all changes' ), 'button', 'save', false ); ?>
  1638. </p>
  1639. </form>
  1640. <?php
  1641. }
  1642. /**
  1643. * {@internal Missing Short Description}}
  1644. *
  1645. * @since 2.7.0
  1646. *
  1647. * @param string $type
  1648. * @param object $errors
  1649. * @param integer $id
  1650. */
  1651. function media_upload_type_url_form($type = null, $errors = null, $id = null) {
  1652. if ( null === $type )
  1653. $type = 'image';
  1654. media_upload_header();
  1655. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1656. $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
  1657. /** This filter is documented in wp-admin/includes/media.php */
  1658. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1659. $form_class = 'media-upload-form type-form validate';
  1660. if ( get_user_setting('uploader') )
  1661. $form_class .= ' html-uploader';
  1662. ?>
  1663. <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">
  1664. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1665. <?php wp_nonce_field('media-form'); ?>
  1666. <h3 class="media-title"><?php _e('Insert media from another website'); ?></h3>
  1667. <script type="text/javascript">
  1668. //<![CDATA[
  1669. var addExtImage = {
  1670. width : '',
  1671. height : '',
  1672. align : 'alignnone',
  1673. insert : function() {
  1674. var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
  1675. if ( '' == f.src.value || '' == t.width )
  1676. return false;
  1677. if ( f.alt.value )
  1678. alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1679. <?php
  1680. /** This filter is documented in wp-admin/includes/media.php */
  1681. if ( ! apply_filters( 'disable_captions', '' ) ) {
  1682. ?>
  1683. if ( f.caption.value ) {
  1684. caption = f.caption.value.replace(/\r\n|\r/g, '\n');
  1685. caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
  1686. return a.replace(/[\r\n\t]+/, ' ');
  1687. });
  1688. caption = caption.replace(/\s*\n\s*/g, '<br />');
  1689. }
  1690. <?php } ?>
  1691. cls = caption ? '' : ' class="'+t.align+'"';
  1692. html = '<img alt="'+alt+'" src="'+f.src.value+'"'+cls+' width="'+t.width+'" height="'+t.height+'" />';
  1693. if ( f.url.value ) {
  1694. url = f.url.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1695. html = '<a href="'+url+'">'+html+'</a>';
  1696. }
  1697. if ( caption )
  1698. html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
  1699. var win = window.dialogArguments || opener || parent || top;
  1700. win.send_to_editor(html);
  1701. return false;
  1702. },
  1703. resetImageData : function() {
  1704. var t = addExtImage;
  1705. t.width = t.height = '';
  1706. document.getElementById('go_button').style.color = '#bbb';
  1707. if ( ! document.forms[0].src.value )
  1708. document.getElementById('status_img').innerHTML = '*';
  1709. else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
  1710. },
  1711. updateImageData : function() {
  1712. var t = addExtImage;
  1713. t.width = t.preloadImg.width;
  1714. t.height = t.preloadImg.height;
  1715. document.getElementById('go_button').style.color = '#333';
  1716. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
  1717. },
  1718. getImageData : function() {
  1719. if ( jQuery('table.describe').hasClass('not-image') )
  1720. return;
  1721. var t = addExtImage, src = document.forms[0].src.value;
  1722. if ( ! src ) {
  1723. t.resetImageData();
  1724. return false;
  1725. }
  1726. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" width="16" />';
  1727. t.preloadImg = new Image();
  1728. t.preloadImg.onload = t.updateImageData;
  1729. t.preloadImg.onerror = t.resetImageData;
  1730. t.preloadImg.src = src;
  1731. }
  1732. }
  1733. jQuery(document).ready( function($) {
  1734. $('.media-types input').click( function() {
  1735. $('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
  1736. });
  1737. });
  1738. //]]>
  1739. </script>
  1740. <div id="media-items">
  1741. <div class="media-item media-blank">
  1742. <?php
  1743. /**
  1744. * Filter the insert media from URL form HTML.
  1745. *
  1746. * @since 3.3.0
  1747. *
  1748. * @param string $form_html The insert from URL form HTML.
  1749. */
  1750. echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) );
  1751. ?>
  1752. </div>
  1753. </div>
  1754. </form>
  1755. <?php
  1756. }
  1757. /**
  1758. * Adds gallery form to upload iframe
  1759. *
  1760. * @since 2.5.0
  1761. *
  1762. * @param array $errors
  1763. */
  1764. function media_upload_gallery_form($errors) {
  1765. global $redir_tab, $type;
  1766. $redir_tab = 'gallery';
  1767. media_upload_header();
  1768. $post_id = intval($_REQUEST['post_id']);
  1769. $form_action_url = admin_url("media-upload.php?type=$type&tab=gallery&post_id=$post_id");
  1770. /** This filter is documented in wp-admin/includes/media.php */
  1771. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1772. $form_class = 'media-upload-form validate';
  1773. if ( get_user_setting('uploader') )
  1774. $form_class .= ' html-uploader';
  1775. ?>
  1776. <script type="text/javascript">
  1777. jQuery(function($){
  1778. var preloaded = $(".media-item.preloaded");
  1779. if ( preloaded.length > 0 ) {
  1780. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1781. updateMediaForm();
  1782. }
  1783. });
  1784. </script>
  1785. <div id="sort-buttons" class="hide-if-no-js">
  1786. <span>
  1787. <?php _e('All Tabs:'); ?>
  1788. <a href="#" id="showall"><?php _e('Show'); ?></a>
  1789. <a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
  1790. </span>
  1791. <?php _e('Sort Order:'); ?>
  1792. <a href="#" id="asc"><?php _e('Ascending'); ?></a> |
  1793. <a href="#" id="desc"><?php _e('Descending'); ?></a> |
  1794. <a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
  1795. </div>
  1796. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
  1797. <?php wp_nonce_field('media-form'); ?>
  1798. <?php //media_upload_form( $errors ); ?>
  1799. <table class="widefat">
  1800. <thead><tr>
  1801. <th><?php _e('Media'); ?></th>
  1802. <th class="order-head"><?php _e('Order'); ?></th>
  1803. <th class="actions-head"><?php _e('Actions'); ?></th>
  1804. </tr></thead>
  1805. </table>
  1806. <div id="media-items">
  1807. <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
  1808. <?php echo get_media_items($post_id, $errors); ?>
  1809. </div>
  1810. <p class="ml-submit">
  1811. <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
  1812. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1813. <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
  1814. <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
  1815. </p>
  1816. <div id="gallery-settings" style="display:none;">
  1817. <div class="title"><?php _e('Gallery Settings'); ?></div>
  1818. <table id="basic" class="describe"><tbody>
  1819. <tr>
  1820. <th scope="row" class="label">
  1821. <label>
  1822. <span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
  1823. </label>
  1824. </th>
  1825. <td class="field">
  1826. <input type="radio" name="linkto" id="linkto-file" value="file" />
  1827. <label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
  1828. <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
  1829. <label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
  1830. </td>
  1831. </tr>
  1832. <tr>
  1833. <th scope="row" class="label">
  1834. <label>
  1835. <span class="alignleft"><?php _e('Order images by:'); ?></span>
  1836. </label>
  1837. </th>
  1838. <td class="field">
  1839. <select id="orderby" name="orderby">
  1840. <option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
  1841. <option value="title"><?php _e('Title'); ?></option>
  1842. <option value="post_date"><?php _e('Date/Time'); ?></option>
  1843. <option value="rand"><?php _e('Random'); ?></option>
  1844. </select>
  1845. </td>
  1846. </tr>
  1847. <tr>
  1848. <th scope="row" class="label">
  1849. <label>
  1850. <span class="alignleft"><?php _e('Order:'); ?></span>
  1851. </label>
  1852. </th>
  1853. <td class="field">
  1854. <input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
  1855. <label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
  1856. <input type="radio" name="order" id="order-desc" value="desc" />
  1857. <label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
  1858. </td>
  1859. </tr>
  1860. <tr>
  1861. <th scope="row" class="label">
  1862. <label>
  1863. <span class="alignleft"><?php _e('Gallery columns:'); ?></span>
  1864. </label>
  1865. </th>
  1866. <td class="field">
  1867. <select id="columns" name="columns">
  1868. <option value="1">1</option>
  1869. <option value="2">2</option>
  1870. <option value="3" selected="selected">3</option>
  1871. <option value="4">4</option>
  1872. <option value="5">5</option>
  1873. <option value="6">6</option>
  1874. <option value="7">7</option>
  1875. <option value="8">8</option>
  1876. <option value="9">9</option>
  1877. </select>
  1878. </td>
  1879. </tr>
  1880. </tbody></table>
  1881. <p class="ml-submit">
  1882. <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
  1883. <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' ); ?>" />
  1884. </p>
  1885. </div>
  1886. </form>
  1887. <?php
  1888. }
  1889. /**
  1890. * {@internal Missing Short Description}}
  1891. *
  1892. * @since 2.5.0
  1893. *
  1894. * @param array $errors
  1895. */
  1896. function media_upload_library_form($errors) {
  1897. global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
  1898. media_upload_header();
  1899. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1900. $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
  1901. /** This filter is documented in wp-admin/includes/media.php */
  1902. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1903. $form_class = 'media-upload-form validate';
  1904. if ( get_user_setting('uploader') )
  1905. $form_class .= ' html-uploader';
  1906. $q = $_GET;
  1907. $q['posts_per_page'] = 10;
  1908. $q['paged'] = isset( $q['paged'] ) ? intval( $q['paged'] ) : 0;
  1909. if ( $q['paged'] < 1 ) {
  1910. $q['paged'] = 1;
  1911. }
  1912. $q['offset'] = ( $q['paged'] - 1 ) * 10;
  1913. if ( $q['offset'] < 1 ) {
  1914. $q['offset'] = 0;
  1915. }
  1916. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query( $q );
  1917. ?>
  1918. <form id="filter" action="" method="get">
  1919. <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
  1920. <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
  1921. <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
  1922. <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
  1923. <input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
  1924. <p id="media-search" class="search-box">
  1925. <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
  1926. <input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
  1927. <?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
  1928. </p>
  1929. <ul class="subsubsub">
  1930. <?php
  1931. $type_links = array();
  1932. $_num_posts = (array) wp_count_attachments();
  1933. $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  1934. foreach ( $matches as $_type => $reals )
  1935. foreach ( $reals as $real )
  1936. if ( isset($num_posts[$_type]) )
  1937. $num_posts[$_type] += $_num_posts[$real];
  1938. else
  1939. $num_posts[$_type] = $_num_posts[$real];
  1940. // If available type specified by media button clicked, filter by that type
  1941. if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
  1942. $_GET['post_mime_type'] = $type;
  1943. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  1944. }
  1945. if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
  1946. $class = ' class="current"';
  1947. else
  1948. $class = '';
  1949. $type_links[] = '<li><a href="' . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . '"' . $class . '>' . __('All Types') . '</a>';
  1950. foreach ( $post_mime_types as $mime_type => $label ) {
  1951. $class = '';
  1952. if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  1953. continue;
  1954. if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  1955. $class = ' class="current"';
  1956. $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>';
  1957. }
  1958. /**
  1959. * Filter the media upload mime type list items.
  1960. *
  1961. * Returned values should begin with an <li> tag.
  1962. *
  1963. * @since 3.1.0
  1964. *
  1965. * @param array $type_links An array of list items containing mime type link HTML.
  1966. */
  1967. echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
  1968. unset($type_links);
  1969. ?>
  1970. </ul>
  1971. <div class="tablenav">
  1972. <?php
  1973. $page_links = paginate_links( array(
  1974. 'base' => add_query_arg( 'paged', '%#%' ),
  1975. 'format' => '',
  1976. 'prev_text' => __('&laquo;'),
  1977. 'next_text' => __('&raquo;'),
  1978. 'total' => ceil($wp_query->found_posts / 10),
  1979. 'current' => $q['paged'],
  1980. ));
  1981. if ( $page_links )
  1982. echo "<div class='tablenav-pages'>$page_links</div>";
  1983. ?>
  1984. <div class="alignleft actions">
  1985. <?php
  1986. $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";
  1987. $arc_result = $wpdb->get_results( $arc_query );
  1988. $month_count = count($arc_result);
  1989. $selected_month = isset( $_GET['m'] ) ? $_GET['m'] : 0;
  1990. if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
  1991. <select name='m'>
  1992. <option<?php selected( $selected_month, 0 ); ?> value='0'><?php _e( 'All dates' ); ?></option>
  1993. <?php
  1994. foreach ($arc_result as $arc_row) {
  1995. if ( $arc_row->yyear == 0 )
  1996. continue;
  1997. $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
  1998. if ( $arc_row->yyear . $arc_row->mmonth == $selected_month )
  1999. $default = ' selected="selected"';
  2000. else
  2001. $default = '';
  2002. echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
  2003. echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
  2004. echo "</option>\n";
  2005. }
  2006. ?>
  2007. </select>
  2008. <?php } ?>
  2009. <?php submit_button( __( 'Filter &#187;' ), 'button', 'post-query-submit', false ); ?>
  2010. </div>
  2011. <br class="clear" />
  2012. </div>
  2013. </form>
  2014. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form">
  2015. <?php wp_nonce_field('media-form'); ?>
  2016. <?php //media_upload_form( $errors ); ?>
  2017. <script type="text/javascript">
  2018. <!--
  2019. jQuery(function($){
  2020. var preloaded = $(".media-item.preloaded");
  2021. if ( preloaded.length > 0 ) {
  2022. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  2023. updateMediaForm();
  2024. }
  2025. });
  2026. -->
  2027. </script>
  2028. <div id="media-items">
  2029. <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
  2030. <?php echo get_media_items(null, $errors); ?>
  2031. </div>
  2032. <p class="ml-submit">
  2033. <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
  2034. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  2035. </p>
  2036. </form>
  2037. <?php
  2038. }
  2039. /**
  2040. * Creates the form for external url
  2041. *
  2042. * @since 2.7.0
  2043. *
  2044. * @param string $default_view
  2045. * @return string the form html
  2046. */
  2047. function wp_media_insert_url_form( $default_view = 'image' ) {
  2048. /** This filter is documented in wp-admin/includes/media.php */
  2049. if ( ! apply_filters( 'disable_captions', '' ) ) {
  2050. $caption = '
  2051. <tr class="image-only">
  2052. <th scope="row" class="label">
  2053. <label for="caption"><span class="alignleft">' . __('Image Caption') . '</span></label>
  2054. </th>
  2055. <td class="field"><textarea id="caption" name="caption"></textarea></td>
  2056. </tr>
  2057. ';
  2058. } else {
  2059. $caption = '';
  2060. }
  2061. $default_align = get_option('image_default_align');
  2062. if ( empty($default_align) )
  2063. $default_align = 'none';
  2064. if ( 'image' == $default_view ) {
  2065. $view = 'image-only';
  2066. $table_class = '';
  2067. } else {
  2068. $view = $table_class = 'not-image';
  2069. }
  2070. return '
  2071. <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>
  2072. <table class="describe ' . $table_class . '"><tbody>
  2073. <tr>
  2074. <th scope="row" class="label" style="width:130px;">
  2075. <label for="src"><span class="alignleft">' . __('URL') . '</span></label>
  2076. <span class="alignright"><abbr id="status_img" title="required" class="required">*</abbr></span>
  2077. </th>
  2078. <td class="field"><input id="src" name="src" value="" type="text" aria-required="true" onblur="addExtImage.getImageData()" /></td>
  2079. </tr>
  2080. <tr>
  2081. <th scope="row" class="label">
  2082. <label for="title"><span class="alignleft">' . __('Title') . '</span></label>
  2083. <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  2084. </th>
  2085. <td class="field"><input id="title" name="title" value="" type="text" aria-required="true" /></td>
  2086. </tr>
  2087. <tr class="not-image"><td></td><td><p class="help">' . __('Link text, e.g. &#8220;Ransom Demands (PDF)&#8221;') . '</p></td></tr>
  2088. <tr class="image-only">
  2089. <th scope="row" class="label">
  2090. <label for="alt"><span class="alignleft">' . __('Alternative Text') . '</span></label>
  2091. </th>
  2092. <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
  2093. <p class="help">' . __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;') . '</p></td>
  2094. </tr>
  2095. ' . $caption . '
  2096. <tr class="align image-only">
  2097. <th scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
  2098. <td class="field">
  2099. <input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'none' ? ' checked="checked"' : '').' />
  2100. <label for="align-none" class="align image-align-none-label">' . __('None') . '</label>
  2101. <input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'left' ? ' checked="checked"' : '').' />
  2102. <label for="align-left" class="align image-align-left-label">' . __('Left') . '</label>
  2103. <input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'center' ? ' checked="checked"' : '').' />
  2104. <label for="align-center" class="align image-align-center-label">' . __('Center') . '</label>
  2105. <input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'right' ? ' checked="checked"' : '').' />
  2106. <label for="align-right" class="align image-align-right-label">' . __('Right') . '</label>
  2107. </td>
  2108. </tr>
  2109. <tr class="image-only">
  2110. <th scope="row" class="label">
  2111. <label for="url"><span class="alignleft">' . __('Link Image To:') . '</span></label>
  2112. </th>
  2113. <td class="field"><input id="url" name="url" value="" type="text" /><br />
  2114. <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __('None') . '</button>
  2115. <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __('Link to image') . '</button>
  2116. <p class="help">' . __('Enter a link URL or click above for presets.') . '</p></td>
  2117. </tr>
  2118. <tr class="image-only">
  2119. <td></td>
  2120. <td>
  2121. <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . esc_attr__('Insert into Post') . '" />
  2122. </td>
  2123. </tr>
  2124. <tr class="not-image">
  2125. <td></td>
  2126. <td>
  2127. ' . get_submit_button( __( 'Insert into Post' ), 'button', 'insertonlybutton', false ) . '
  2128. </td>
  2129. </tr>
  2130. </tbody></table>
  2131. ';
  2132. }
  2133. /**
  2134. * Displays the multi-file uploader message.
  2135. *
  2136. * @since 2.6.0
  2137. */
  2138. function media_upload_flash_bypass() {
  2139. $browser_uploader = admin_url( 'media-new.php?browser-uploader' );
  2140. if ( $post = get_post() )
  2141. $browser_uploader .= '&amp;post_id=' . intval( $post->ID );
  2142. elseif ( ! empty( $GLOBALS['post_ID'] ) )
  2143. $browser_uploader .= '&amp;post_id=' . intval( $GLOBALS['post_ID'] );
  2144. ?>
  2145. <p class="upload-flash-bypass">
  2146. <?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' ); ?>
  2147. </p>
  2148. <?php
  2149. }
  2150. add_action('post-plupload-upload-ui', 'media_upload_flash_bypass');
  2151. /**
  2152. * Displays the browser's built-in uploader message.
  2153. *
  2154. * @since 2.6.0
  2155. */
  2156. function media_upload_html_bypass() {
  2157. ?>
  2158. <p class="upload-html-bypass hide-if-no-js">
  2159. <?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>.'); ?>
  2160. </p>
  2161. <?php
  2162. }
  2163. add_action('post-html-upload-ui', 'media_upload_html_bypass');
  2164. /**
  2165. * Used to display a "After a file has been uploaded..." help message.
  2166. *
  2167. * @since 3.3.0
  2168. */
  2169. function media_upload_text_after() {}
  2170. /**
  2171. * Displays the checkbox to scale images.
  2172. *
  2173. * @since 3.3.0
  2174. */
  2175. function media_upload_max_image_resize() {
  2176. $checked = get_user_setting('upload_resize') ? ' checked="true"' : '';
  2177. $a = $end = '';
  2178. if ( current_user_can( 'manage_options' ) ) {
  2179. $a = '<a href="' . esc_url( admin_url( 'options-media.php' ) ) . '" target="_blank">';
  2180. $end = '</a>';
  2181. }
  2182. ?>
  2183. <p class="hide-if-no-js"><label>
  2184. <input name="image_resize" type="checkbox" id="image_resize" value="true"<?php echo $checked; ?> />
  2185. <?php
  2186. /* translators: %1$s is link start tag, %2$s is link end tag, %3$d is width, %4$d is height*/
  2187. 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' ) );
  2188. ?>
  2189. </label></p>
  2190. <?php
  2191. }
  2192. /**
  2193. * Displays the out of storage quota message in Multisite.
  2194. *
  2195. * @since 3.5.0
  2196. */
  2197. function multisite_over_quota_message() {
  2198. echo '<p>' . sprintf( __( 'Sorry, you have used all of your storage quota of %s MB.' ), get_space_allowed() ) . '</p>';
  2199. }
  2200. /**
  2201. * Displays the image and editor in the post editor
  2202. *
  2203. * @since 3.5.0
  2204. */
  2205. function edit_form_image_editor( $post ) {
  2206. $open = isset( $_GET['image-editor'] );
  2207. if ( $open )
  2208. require_once ABSPATH . 'wp-admin/includes/image-edit.php';
  2209. $thumb_url = false;
  2210. if ( $attachment_id = intval( $post->ID ) )
  2211. $thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
  2212. $alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
  2213. $att_url = wp_get_attachment_url( $post->ID ); ?>
  2214. <div class="wp_attachment_holder">
  2215. <?php
  2216. if ( wp_attachment_is_image( $post->ID ) ) :
  2217. $image_edit_button = '';
  2218. if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  2219. $nonce = wp_create_nonce( "image_editor-$post->ID" );
  2220. $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>";
  2221. }
  2222. ?>
  2223. <div class="imgedit-response" id="imgedit-response-<?php echo $attachment_id; ?>"></div>
  2224. <div<?php if ( $open ) echo ' style="display:none"'; ?> class="wp_attachment_image" id="media-head-<?php echo $attachment_id; ?>">
  2225. <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>
  2226. <p><?php echo $image_edit_button; ?></p>
  2227. </div>
  2228. <div<?php if ( ! $open ) echo ' style="display:none"'; ?> class="image-editor" id="image-editor-<?php echo $attachment_id; ?>">
  2229. <?php if ( $open ) wp_image_editor( $attachment_id ); ?>
  2230. </div>
  2231. <?php
  2232. elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'audio/' ) ):
  2233. wp_maybe_generate_attachment_metadata( $post );
  2234. echo wp_audio_shortcode( array( 'src' => $att_url ) );
  2235. elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'video/' ) ):
  2236. wp_maybe_generate_attachment_metadata( $post );
  2237. $meta = wp_get_attachment_metadata( $attachment_id );
  2238. $w = ! empty( $meta['width'] ) ? min( $meta['width'], 640 ) : 0;
  2239. $h = ! empty( $meta['height'] ) ? $meta['height'] : 0;
  2240. if ( $h && $w < $meta['width'] ) {
  2241. $h = round( ( $meta['height'] * $w ) / $meta['width'] );
  2242. }
  2243. $attr = array( 'src' => $att_url );
  2244. if ( ! empty( $w ) && ! empty( $h ) ) {
  2245. $attr['width'] = $w;
  2246. $attr['height'] = $h;
  2247. }
  2248. echo wp_video_shortcode( $attr );
  2249. endif; ?>
  2250. </div>
  2251. <div class="wp_attachment_details edit-form-section">
  2252. <p>
  2253. <label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong></label><br />
  2254. <textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea>
  2255. </p>
  2256. <?php if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) : ?>
  2257. <p>
  2258. <label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br />
  2259. <input type="text" class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" value="<?php echo esc_attr( $alt_text ); ?>" />
  2260. </p>
  2261. <?php endif; ?>
  2262. <?php
  2263. $quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
  2264. $editor_args = array(
  2265. 'textarea_name' => 'content',
  2266. 'textarea_rows' => 5,
  2267. 'media_buttons' => false,
  2268. 'tinymce' => false,
  2269. 'quicktags' => $quicktags_settings,
  2270. );
  2271. ?>
  2272. <label for="content"><strong><?php _e( 'Description' ); ?></strong><?php
  2273. if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  2274. echo ': ' . __( 'Displayed on attachment pages.' );
  2275. } ?></label>
  2276. <?php wp_editor( $post->post_content, 'attachment_content', $editor_args ); ?>
  2277. </div>
  2278. <?php
  2279. $extras = get_compat_media_markup( $post->ID );
  2280. echo $extras['item'];
  2281. echo '<input type="hidden" id="image-edit-context" value="edit-attachment" />' . "\n";
  2282. }
  2283. /**
  2284. * Displays non-editable attachment metadata in the publish metabox
  2285. *
  2286. * @since 3.5.0
  2287. */
  2288. function attachment_submitbox_metadata() {
  2289. $post = get_post();
  2290. $filename = esc_html( wp_basename( $post->guid ) );
  2291. $media_dims = '';
  2292. $meta = wp_get_attachment_metadata( $post->ID );
  2293. if ( isset( $meta['width'], $meta['height'] ) )
  2294. $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  2295. /** This filter is documented in wp-admin/includes/media.php */
  2296. $media_dims = apply_filters( 'media_meta', $media_dims, $post );
  2297. $att_url = wp_get_attachment_url( $post->ID );
  2298. ?>
  2299. <div class="misc-pub-section misc-pub-attachment">
  2300. <label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
  2301. <input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" value="<?php echo esc_attr($att_url); ?>" />
  2302. </div>
  2303. <div class="misc-pub-section misc-pub-filename">
  2304. <?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
  2305. </div>
  2306. <div class="misc-pub-section misc-pub-filetype">
  2307. <?php _e( 'File type:' ); ?> <strong><?php
  2308. if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
  2309. echo esc_html( strtoupper( $matches[1] ) );
  2310. list( $mime_type ) = explode( '/', $post->post_mime_type );
  2311. if ( $mime_type !== 'image' && ! empty( $meta['mime_type'] ) ) {
  2312. if ( $meta['mime_type'] !== "$mime_type/" . strtolower( $matches[1] ) ) {
  2313. echo ' (' . $meta['mime_type'] . ')';
  2314. }
  2315. }
  2316. } else {
  2317. echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
  2318. }
  2319. ?></strong>
  2320. </div>
  2321. <?php
  2322. $file = get_attached_file( $post->ID );
  2323. $file_size = false;
  2324. if ( isset( $meta['filesize'] ) )
  2325. $file_size = $meta['filesize'];
  2326. elseif ( file_exists( $file ) )
  2327. $file_size = filesize( $file );
  2328. if ( ! empty( $file_size ) ) : ?>
  2329. <div class="misc-pub-section misc-pub-filesize">
  2330. <?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong>
  2331. </div>
  2332. <?php
  2333. endif;
  2334. if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  2335. /**
  2336. * Filter the audio and video metadata fields to be shown in the publish meta box.
  2337. *
  2338. * The key for each item in the array should correspond to an attachment
  2339. * metadata key, and the value should be the desired label.
  2340. *
  2341. * @since 3.7.0
  2342. *
  2343. * @param array $fields An array of the attachment metadata keys and labels.
  2344. */
  2345. $fields = apply_filters( 'media_submitbox_misc_sections', array(
  2346. 'length_formatted' => __( 'Length:' ),
  2347. 'bitrate' => __( 'Bitrate:' ),
  2348. ) );
  2349. foreach ( $fields as $key => $label ) {
  2350. if ( empty( $meta[ $key ] ) ) {
  2351. continue;
  2352. }
  2353. ?>
  2354. <div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  2355. <?php echo $label ?> <strong><?php
  2356. switch ( $key ) {
  2357. case 'bitrate' :
  2358. echo round( $meta['bitrate'] / 1000 ) . 'kb/s';
  2359. if ( ! empty( $meta['bitrate_mode'] ) ) {
  2360. echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) );
  2361. }
  2362. break;
  2363. default:
  2364. echo esc_html( $meta[ $key ] );
  2365. break;
  2366. }
  2367. ?></strong>
  2368. </div>
  2369. <?php
  2370. }
  2371. /**
  2372. * Filter the audio attachment metadata fields to be shown in the publish meta box.
  2373. *
  2374. * The key for each item in the array should correspond to an attachment
  2375. * metadata key, and the value should be the desired label.
  2376. *
  2377. * @since 3.7.0
  2378. *
  2379. * @param array $fields An array of the attachment metadata keys and labels.
  2380. */
  2381. $audio_fields = apply_filters( 'audio_submitbox_misc_sections', array(
  2382. 'dataformat' => __( 'Audio Format:' ),
  2383. 'codec' => __( 'Audio Codec:' )
  2384. ) );
  2385. foreach ( $audio_fields as $key => $label ) {
  2386. if ( empty( $meta['audio'][ $key ] ) ) {
  2387. continue;
  2388. }
  2389. ?>
  2390. <div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  2391. <?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][$key] ); ?></strong>
  2392. </div>
  2393. <?php
  2394. }
  2395. }
  2396. if ( $media_dims ) : ?>
  2397. <div class="misc-pub-section misc-pub-dimensions">
  2398. <?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
  2399. </div>
  2400. <?php
  2401. endif;
  2402. }
  2403. add_filter( 'async_upload_image', 'get_media_item', 10, 2 );
  2404. add_filter( 'async_upload_audio', 'get_media_item', 10, 2 );
  2405. add_filter( 'async_upload_video', 'get_media_item', 10, 2 );
  2406. add_filter( 'async_upload_file', 'get_media_item', 10, 2 );
  2407. add_action( 'media_upload_image', 'wp_media_upload_handler' );
  2408. add_action( 'media_upload_audio', 'wp_media_upload_handler' );
  2409. add_action( 'media_upload_video', 'wp_media_upload_handler' );
  2410. add_action( 'media_upload_file', 'wp_media_upload_handler' );
  2411. add_filter( 'media_upload_gallery', 'media_upload_gallery' );
  2412. add_filter( 'media_upload_library', 'media_upload_library' );
  2413. add_action( 'attachment_submitbox_misc_actions', 'attachment_submitbox_metadata' );
  2414. /**
  2415. * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
  2416. *
  2417. * @since 3.6.0
  2418. *
  2419. * @param array $metadata An existing array with data
  2420. * @param array $data Data supplied by ID3 tags
  2421. */
  2422. function wp_add_id3_tag_data( &$metadata, $data ) {
  2423. foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
  2424. if ( ! empty( $data[$version]['comments'] ) ) {
  2425. foreach ( $data[$version]['comments'] as $key => $list ) {
  2426. if ( ! empty( $list ) ) {
  2427. $metadata[$key] = reset( $list );
  2428. // fix bug in byte stream analysis
  2429. if ( 'terms_of_use' === $key && 0 === strpos( $metadata[$key], 'yright notice.' ) )
  2430. $metadata[$key] = 'Cop' . $metadata[$key];
  2431. }
  2432. }
  2433. break;
  2434. }
  2435. }
  2436. if ( ! empty( $data['id3v2']['APIC'] ) ) {
  2437. $image = reset( $data['id3v2']['APIC']);
  2438. if ( ! empty( $image['data'] ) ) {
  2439. $metadata['image'] = array(
  2440. 'data' => $image['data'],
  2441. 'mime' => $image['image_mime'],
  2442. 'width' => $image['image_width'],
  2443. 'height' => $image['image_height']
  2444. );
  2445. }
  2446. } elseif ( ! empty( $data['comments']['picture'] ) ) {
  2447. $image = reset( $data['comments']['picture'] );
  2448. if ( ! empty( $image['data'] ) ) {
  2449. $metadata['image'] = array(
  2450. 'data' => $image['data'],
  2451. 'mime' => $image['image_mime']
  2452. );
  2453. }
  2454. }
  2455. }
  2456. /**
  2457. * Retrieve metadata from a video file's ID3 tags
  2458. *
  2459. * @since 3.6.0
  2460. *
  2461. * @param string $file Path to file.
  2462. * @return array|boolean Returns array of metadata, if found.
  2463. */
  2464. function wp_read_video_metadata( $file ) {
  2465. if ( ! file_exists( $file ) )
  2466. return false;
  2467. $metadata = array();
  2468. if ( ! class_exists( 'getID3' ) )
  2469. require( ABSPATH . WPINC . '/ID3/getid3.php' );
  2470. $id3 = new getID3();
  2471. $data = $id3->analyze( $file );
  2472. if ( isset( $data['video']['lossless'] ) )
  2473. $metadata['lossless'] = $data['video']['lossless'];
  2474. if ( ! empty( $data['video']['bitrate'] ) )
  2475. $metadata['bitrate'] = (int) $data['video']['bitrate'];
  2476. if ( ! empty( $data['video']['bitrate_mode'] ) )
  2477. $metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
  2478. if ( ! empty( $data['filesize'] ) )
  2479. $metadata['filesize'] = (int) $data['filesize'];
  2480. if ( ! empty( $data['mime_type'] ) )
  2481. $metadata['mime_type'] = $data['mime_type'];
  2482. if ( ! empty( $data['playtime_seconds'] ) )
  2483. $metadata['length'] = (int) ceil( $data['playtime_seconds'] );
  2484. if ( ! empty( $data['playtime_string'] ) )
  2485. $metadata['length_formatted'] = $data['playtime_string'];
  2486. if ( ! empty( $data['video']['resolution_x'] ) )
  2487. $metadata['width'] = (int) $data['video']['resolution_x'];
  2488. if ( ! empty( $data['video']['resolution_y'] ) )
  2489. $metadata['height'] = (int) $data['video']['resolution_y'];
  2490. if ( ! empty( $data['fileformat'] ) )
  2491. $metadata['fileformat'] = $data['fileformat'];
  2492. if ( ! empty( $data['video']['dataformat'] ) )
  2493. $metadata['dataformat'] = $data['video']['dataformat'];
  2494. if ( ! empty( $data['video']['encoder'] ) )
  2495. $metadata['encoder'] = $data['video']['encoder'];
  2496. if ( ! empty( $data['video']['codec'] ) )
  2497. $metadata['codec'] = $data['video']['codec'];
  2498. if ( ! empty( $data['audio'] ) ) {
  2499. unset( $data['audio']['streams'] );
  2500. $metadata['audio'] = $data['audio'];
  2501. }
  2502. wp_add_id3_tag_data( $metadata, $data );
  2503. return $metadata;
  2504. }
  2505. /**
  2506. * Retrieve metadata from a audio file's ID3 tags
  2507. *
  2508. * @since 3.6.0
  2509. *
  2510. * @param string $file Path to file.
  2511. * @return array|boolean Returns array of metadata, if found.
  2512. */
  2513. function wp_read_audio_metadata( $file ) {
  2514. if ( ! file_exists( $file ) )
  2515. return false;
  2516. $metadata = array();
  2517. if ( ! class_exists( 'getID3' ) )
  2518. require( ABSPATH . WPINC . '/ID3/getid3.php' );
  2519. $id3 = new getID3();
  2520. $data = $id3->analyze( $file );
  2521. if ( ! empty( $data['audio'] ) ) {
  2522. unset( $data['audio']['streams'] );
  2523. $metadata = $data['audio'];
  2524. }
  2525. if ( ! empty( $data['fileformat'] ) )
  2526. $metadata['fileformat'] = $data['fileformat'];
  2527. if ( ! empty( $data['filesize'] ) )
  2528. $metadata['filesize'] = (int) $data['filesize'];
  2529. if ( ! empty( $data['mime_type'] ) )
  2530. $metadata['mime_type'] = $data['mime_type'];
  2531. if ( ! empty( $data['playtime_seconds'] ) )
  2532. $metadata['length'] = (int) ceil( $data['playtime_seconds'] );
  2533. if ( ! empty( $data['playtime_string'] ) )
  2534. $metadata['length_formatted'] = $data['playtime_string'];
  2535. wp_add_id3_tag_data( $metadata, $data );
  2536. return $metadata;
  2537. }