PageRenderTime 32ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/media.php

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