PageRenderTime 84ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/media.php

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