You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

3186 lines
101 KiB

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