PageRenderTime 66ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/media.php

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