PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/media.php

https://gitlab.com/rio.munas/gddc
PHP | 1554 lines | 1482 code | 18 blank | 54 comment | 14 complexity | f5247c8d7e185e4f16ae2fef42ddf145 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress Administration Media API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Defines the default media upload tabs
  10. *
  11. * @since 2.5.0
  12. *
  13. * @return array default tabs
  14. */
  15. function media_upload_tabs() {
  16. $_default_tabs = array(
  17. 'type' => __('From Computer'), // handler action suffix => tab text
  18. 'type_url' => __('From URL'),
  19. 'gallery' => __('Gallery'),
  20. 'library' => __('Media Library')
  21. );
  22. /**
  23. * Filter the available tabs in the legacy (pre-3.5.0) media popup.
  24. *
  25. * @since 2.5.0
  26. *
  27. * @param array $_default_tabs An array of media tabs.
  28. */
  29. return apply_filters( 'media_upload_tabs', $_default_tabs );
  30. }
  31. /**
  32. * Adds the gallery tab back to the tabs array if post has image attachments
  33. *
  34. * @since 2.5.0
  35. *
  36. * @global wpdb $wpdb 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 string $rel Optional. Image rel attribute. Default empty.
  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 = '', $size = 'medium', $alt = '' ) {
  106. $html = get_image_tag($id, $alt, '', $align, $size);
  107. if ( ! $rel ) {
  108. $rel = ' rel="attachment wp-att-' . esc_attr( $id ) . '"';
  109. } else {
  110. $rel = ' rel="' . esc_attr( $rel ) . '"';
  111. }
  112. if ( $url )
  113. $html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
  114. /**
  115. * Filter the image HTML markup to send to the editor.
  116. *
  117. * @since 2.5.0
  118. *
  119. * @param string $html The image HTML markup to send.
  120. * @param int $id The attachment id.
  121. * @param string $caption The image caption.
  122. * @param string $title The image title.
  123. * @param string $align The image alignment.
  124. * @param string $url The image source URL.
  125. * @param string|array $size Size of image. Image size or array of width and height values
  126. * (in that order). Default 'medium'.
  127. * @param string $alt The image alternative, or alt, text.
  128. */
  129. $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
  130. return $html;
  131. }
  132. /**
  133. * Adds image shortcode with caption to editor
  134. *
  135. * @since 2.6.0
  136. *
  137. * @param string $html
  138. * @param integer $id
  139. * @param string $caption image caption
  140. * @param string $alt image alt attribute
  141. * @param string $title image title attribute
  142. * @param string $align image css alignment property
  143. * @param string $url image src url
  144. * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
  145. * @return string
  146. */
  147. function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
  148. /**
  149. * Filter the caption text.
  150. *
  151. * Note: If the caption text is empty, the caption shortcode will not be appended
  152. * to the image HTML when inserted into the editor.
  153. *
  154. * Passing an empty value also prevents the {@see 'image_add_caption_shortcode'}
  155. * filter from being evaluated at the end of {@see image_add_caption()}.
  156. *
  157. * @since 4.1.0
  158. *
  159. * @param string $caption The original caption text.
  160. * @param int $id The attachment ID.
  161. */
  162. $caption = apply_filters( 'image_add_caption_text', $caption, $id );
  163. /**
  164. * Filter whether to disable captions.
  165. *
  166. * Prevents image captions from being appended to image HTML when inserted into the editor.
  167. *
  168. * @since 2.6.0
  169. *
  170. * @param bool $bool Whether to disable appending captions. Returning true to the filter
  171. * will disable captions. Default empty string.
  172. */
  173. if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
  174. return $html;
  175. $id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
  176. if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) )
  177. return $html;
  178. $width = $matches[1];
  179. $caption = str_replace( array("\r\n", "\r"), "\n", $caption);
  180. $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
  181. // Convert any remaining line breaks to <br>.
  182. $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
  183. $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
  184. if ( empty($align) )
  185. $align = 'none';
  186. $shcode = '[caption id="' . $id . '" align="align' . $align . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]';
  187. /**
  188. * Filter the image HTML markup including the caption shortcode.
  189. *
  190. * @since 2.6.0
  191. *
  192. * @param string $shcode The image HTML markup with caption shortcode.
  193. * @param string $html The image HTML markup.
  194. */
  195. return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
  196. }
  197. /**
  198. * Private preg_replace callback used in image_add_caption()
  199. *
  200. * @access private
  201. * @since 3.4.0
  202. */
  203. function _cleanup_image_add_caption( $matches ) {
  204. // Remove any line breaks from inside the tags.
  205. return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
  206. }
  207. /**
  208. * Adds image html to editor
  209. *
  210. * @since 2.5.0
  211. *
  212. * @param string $html
  213. */
  214. function media_send_to_editor($html) {
  215. ?>
  216. <script type="text/javascript">
  217. var win = window.dialogArguments || opener || parent || top;
  218. win.send_to_editor( <?php echo wp_json_encode( $html ); ?> );
  219. </script>
  220. <?php
  221. exit;
  222. }
  223. /**
  224. * Save a file submitted from a POST request and create an attachment post for it.
  225. *
  226. * @since 2.5.0
  227. *
  228. * @param string $file_id Index of the {@link $_FILES} array that the file was sent. Required.
  229. * @param int $post_id The post ID of a post to attach the media item to. Required, but can
  230. * be set to 0, creating a media item that has no relationship to a post.
  231. * @param array $post_data Overwrite some of the attachment. Optional.
  232. * @param array $overrides Override the {@link wp_handle_upload()} behavior. Optional.
  233. * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
  234. */
  235. function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) {
  236. $time = current_time('mysql');
  237. if ( $post = get_post($post_id) ) {
  238. if ( substr( $post->post_date, 0, 4 ) > 0 )
  239. $time = $post->post_date;
  240. }
  241. $name = $_FILES[$file_id]['name'];
  242. $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
  243. if ( isset($file['error']) )
  244. return new WP_Error( 'upload_error', $file['error'] );
  245. $name_parts = pathinfo($name);
  246. $name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );
  247. $url = $file['url'];
  248. $type = $file['type'];
  249. $file = $file['file'];
  250. $title = $name;
  251. $content = '';
  252. $excerpt = '';
  253. if ( preg_match( '#^audio#', $type ) ) {
  254. $meta = wp_read_audio_metadata( $file );
  255. if ( ! empty( $meta['title'] ) ) {
  256. $title = $meta['title'];
  257. }
  258. if ( ! empty( $title ) ) {
  259. if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
  260. /* translators: 1: audio track title, 2: album title, 3: artist name */
  261. $content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
  262. } elseif ( ! empty( $meta['album'] ) ) {
  263. /* translators: 1: audio track title, 2: album title */
  264. $content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
  265. } elseif ( ! empty( $meta['artist'] ) ) {
  266. /* translators: 1: audio track title, 2: artist name */
  267. $content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
  268. } else {
  269. $content .= sprintf( __( '"%s".' ), $title );
  270. }
  271. } elseif ( ! empty( $meta['album'] ) ) {
  272. if ( ! empty( $meta['artist'] ) ) {
  273. /* translators: 1: audio album title, 2: artist name */
  274. $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
  275. } else {
  276. $content .= $meta['album'] . '.';
  277. }
  278. } elseif ( ! empty( $meta['artist'] ) ) {
  279. $content .= $meta['artist'] . '.';
  280. }
  281. if ( ! empty( $meta['year'] ) )
  282. $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
  283. if ( ! empty( $meta['track_number'] ) ) {
  284. $track_number = explode( '/', $meta['track_number'] );
  285. if ( isset( $track_number[1] ) )
  286. $content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
  287. else
  288. $content .= ' ' . sprintf( __( 'Track %1$s.' ), number_format_i18n( $track_number[0] ) );
  289. }
  290. if ( ! empty( $meta['genre'] ) )
  291. $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
  292. // Use image exif/iptc data for title and caption defaults if possible.
  293. } elseif ( 0 === strpos( $type, 'image/' ) && $image_meta = @wp_read_image_metadata( $file ) ) {
  294. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
  295. $title = $image_meta['title'];
  296. }
  297. if ( trim( $image_meta['caption'] ) ) {
  298. $excerpt = $image_meta['caption'];
  299. }
  300. }
  301. // Construct the attachment array
  302. $attachment = array_merge( array(
  303. 'post_mime_type' => $type,
  304. 'guid' => $url,
  305. 'post_parent' => $post_id,
  306. 'post_title' => $title,
  307. 'post_content' => $content,
  308. 'post_excerpt' => $excerpt,
  309. ), $post_data );
  310. // This should never be set as it would then overwrite an existing attachment.
  311. unset( $attachment['ID'] );
  312. // Save the data
  313. $id = wp_insert_attachment($attachment, $file, $post_id);
  314. if ( !is_wp_error($id) ) {
  315. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  316. }
  317. return $id;
  318. }
  319. /**
  320. * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
  321. *
  322. * @since 2.6.0
  323. *
  324. * @param array $file_array Array similar to a {@link $_FILES} upload array
  325. * @param int $post_id The post ID the media is associated with
  326. * @param string $desc Description of the sideloaded file
  327. * @param array $post_data allows you to overwrite some of the attachment
  328. * @return int|object The ID of the attachment or a WP_Error on failure
  329. */
  330. function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
  331. $overrides = array('test_form'=>false);
  332. $time = current_time( 'mysql' );
  333. if ( $post = get_post( $post_id ) ) {
  334. if ( substr( $post->post_date, 0, 4 ) > 0 )
  335. $time = $post->post_date;
  336. }
  337. $file = wp_handle_sideload( $file_array, $overrides, $time );
  338. if ( isset($file['error']) )
  339. return new WP_Error( 'upload_error', $file['error'] );
  340. $url = $file['url'];
  341. $type = $file['type'];
  342. $file = $file['file'];
  343. $title = preg_replace('/\.[^.]+$/', '', basename($file));
  344. $content = '';
  345. // Use image exif/iptc data for title and caption defaults if possible.
  346. if ( $image_meta = @wp_read_image_metadata($file) ) {
  347. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
  348. $title = $image_meta['title'];
  349. if ( trim( $image_meta['caption'] ) )
  350. $content = $image_meta['caption'];
  351. }
  352. if ( isset( $desc ) )
  353. $title = $desc;
  354. // Construct the attachment array.
  355. $attachment = array_merge( array(
  356. 'post_mime_type' => $type,
  357. 'guid' => $url,
  358. 'post_parent' => $post_id,
  359. 'post_title' => $title,
  360. 'post_content' => $content,
  361. ), $post_data );
  362. // This should never be set as it would then overwrite an existing attachment.
  363. unset( $attachment['ID'] );
  364. // Save the attachment metadata
  365. $id = wp_insert_attachment($attachment, $file, $post_id);
  366. if ( !is_wp_error($id) )
  367. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  368. return $id;
  369. }
  370. /**
  371. * Adds the iframe to display content for the media upload page
  372. *
  373. * @since 2.5.0
  374. *
  375. * @global int $body_id
  376. *
  377. * @param string|callable $content_func
  378. */
  379. function wp_iframe($content_func /* ... */) {
  380. _wp_admin_html_begin();
  381. ?>
  382. <title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
  383. <?php
  384. wp_enqueue_style( 'colors' );
  385. // Check callback name for 'media'
  386. if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
  387. || ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
  388. wp_enqueue_style( 'media' );
  389. wp_enqueue_style( 'ie' );
  390. ?>
  391. <script type="text/javascript">
  392. 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();}}};
  393. var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
  394. isRtl = <?php echo (int) is_rtl(); ?>;
  395. </script>
  396. <?php
  397. /** This action is documented in wp-admin/admin-header.php */
  398. do_action( 'admin_enqueue_scripts', 'media-upload-popup' );
  399. /**
  400. * Fires when admin styles enqueued for the legacy (pre-3.5.0) media upload popup are printed.
  401. *
  402. * @since 2.9.0
  403. */
  404. do_action( 'admin_print_styles-media-upload-popup' );
  405. /** This action is documented in wp-admin/admin-header.php */
  406. do_action( 'admin_print_styles' );
  407. /**
  408. * Fires when admin scripts enqueued for the legacy (pre-3.5.0) media upload popup are printed.
  409. *
  410. * @since 2.9.0
  411. */
  412. do_action( 'admin_print_scripts-media-upload-popup' );
  413. /** This action is documented in wp-admin/admin-header.php */
  414. do_action( 'admin_print_scripts' );
  415. /**
  416. * Fires when scripts enqueued for the admin header for the legacy (pre-3.5.0)
  417. * media upload popup are printed.
  418. *
  419. * @since 2.9.0
  420. */
  421. do_action( 'admin_head-media-upload-popup' );
  422. /** This action is documented in wp-admin/admin-header.php */
  423. do_action( 'admin_head' );
  424. if ( is_string( $content_func ) ) {
  425. /**
  426. * Fires in the admin header for each specific form tab in the legacy
  427. * (pre-3.5.0) media upload popup.
  428. *
  429. * The dynamic portion of the hook, `$content_func`, refers to the form
  430. * callback for the media upload type. Possible values include
  431. * 'media_upload_type_form', 'media_upload_type_url_form', and
  432. * 'media_upload_library_form'.
  433. *
  434. * @since 2.5.0
  435. */
  436. do_action( "admin_head_{$content_func}" );
  437. }
  438. ?>
  439. </head>
  440. <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?> class="wp-core-ui no-js">
  441. <script type="text/javascript">
  442. document.body.className = document.body.className.replace('no-js', 'js');
  443. </script>
  444. <?php
  445. $args = func_get_args();
  446. $args = array_slice($args, 1);
  447. call_user_func_array($content_func, $args);
  448. /** This action is documented in wp-admin/admin-footer.php */
  449. do_action( 'admin_print_footer_scripts' );
  450. ?>
  451. <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
  452. </body>
  453. </html>
  454. <?php
  455. }
  456. /**
  457. * Adds the media button to the editor
  458. *
  459. * @since 2.5.0
  460. *
  461. * @global int $post_ID
  462. *
  463. * @staticvar int $instance
  464. *
  465. * @param string $editor_id
  466. */
  467. function media_buttons($editor_id = 'content') {
  468. static $instance = 0;
  469. $instance++;
  470. $post = get_post();
  471. if ( ! $post && ! empty( $GLOBALS['post_ID'] ) )
  472. $post = $GLOBALS['post_ID'];
  473. wp_enqueue_media( array(
  474. 'post' => $post
  475. ) );
  476. $img = '<span class="wp-media-buttons-icon"></span> ';
  477. $id_attribute = $instance === 1 ? ' id="insert-media-button"' : '';
  478. printf( '<button type="button"%s class="button insert-media add_media" data-editor="%s">%s</button>',
  479. $id_attribute,
  480. esc_attr( $editor_id ),
  481. $img . __( 'Add Media' )
  482. );
  483. /**
  484. * Filter the legacy (pre-3.5.0) media buttons.
  485. *
  486. * @since 2.5.0
  487. * @deprecated 3.5.0 Use 'media_buttons' action instead.
  488. *
  489. * @param string $string Media buttons context. Default empty.
  490. */
  491. $legacy_filter = apply_filters( 'media_buttons_context', '' );
  492. if ( $legacy_filter ) {
  493. // #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
  494. if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) )
  495. $legacy_filter .= '</a>';
  496. echo $legacy_filter;
  497. }
  498. }
  499. /**
  500. *
  501. * @global int $post_ID
  502. * @param string $type
  503. * @param int $post_id
  504. * @param string $tab
  505. * @return string
  506. */
  507. function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
  508. global $post_ID;
  509. if ( empty( $post_id ) )
  510. $post_id = $post_ID;
  511. $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
  512. if ( $type && 'media' != $type )
  513. $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
  514. if ( ! empty( $tab ) )
  515. $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
  516. /**
  517. * Filter the upload iframe source URL for a specific media type.
  518. *
  519. * The dynamic portion of the hook name, `$type`, refers to the type
  520. * of media uploaded.
  521. *
  522. * @since 3.0.0
  523. *
  524. * @param string $upload_iframe_src The upload iframe source URL by type.
  525. */
  526. $upload_iframe_src = apply_filters( $type . '_upload_iframe_src', $upload_iframe_src );
  527. return add_query_arg('TB_iframe', true, $upload_iframe_src);
  528. }
  529. /**
  530. * Handles form submissions for the legacy media uploader.
  531. *
  532. * @since 2.5.0
  533. *
  534. * @return mixed void|object WP_Error on failure
  535. */
  536. function media_upload_form_handler() {
  537. check_admin_referer('media-form');
  538. $errors = null;
  539. if ( isset($_POST['send']) ) {
  540. $keys = array_keys( $_POST['send'] );
  541. $send_id = (int) reset( $keys );
  542. }
  543. if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
  544. $post = $_post = get_post($attachment_id, ARRAY_A);
  545. if ( !current_user_can( 'edit_post', $attachment_id ) )
  546. continue;
  547. if ( isset($attachment['post_content']) )
  548. $post['post_content'] = $attachment['post_content'];
  549. if ( isset($attachment['post_title']) )
  550. $post['post_title'] = $attachment['post_title'];
  551. if ( isset($attachment['post_excerpt']) )
  552. $post['post_excerpt'] = $attachment['post_excerpt'];
  553. if ( isset($attachment['menu_order']) )
  554. $post['menu_order'] = $attachment['menu_order'];
  555. if ( isset($send_id) && $attachment_id == $send_id ) {
  556. if ( isset($attachment['post_parent']) )
  557. $post['post_parent'] = $attachment['post_parent'];
  558. }
  559. /**
  560. * Filter the attachment fields to be saved.
  561. *
  562. * @since 2.5.0
  563. *
  564. * @see wp_get_attachment_metadata()
  565. *
  566. * @param array $post An array of post data.
  567. * @param array $attachment An array of attachment metadata.
  568. */
  569. $post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
  570. if ( isset($attachment['image_alt']) ) {
  571. $image_alt = wp_unslash( $attachment['image_alt'] );
  572. if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) {
  573. $image_alt = wp_strip_all_tags( $image_alt, true );
  574. // Update_meta expects slashed.
  575. update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
  576. }
  577. }
  578. if ( isset($post['errors']) ) {
  579. $errors[$attachment_id] = $post['errors'];
  580. unset($post['errors']);
  581. }
  582. if ( $post != $_post )
  583. wp_update_post($post);
  584. foreach ( get_attachment_taxonomies($post) as $t ) {
  585. if ( isset($attachment[$t]) )
  586. wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
  587. }
  588. }
  589. if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
  590. <script type="text/javascript">
  591. var win = window.dialogArguments || opener || parent || top;
  592. win.tb_remove();
  593. </script>
  594. <?php
  595. exit;
  596. }
  597. if ( isset($send_id) ) {
  598. $attachment = wp_unslash( $_POST['attachments'][$send_id] );
  599. $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
  600. if ( !empty($attachment['url']) ) {
  601. $rel = '';
  602. if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
  603. $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
  604. $html = "<a href='{$attachment['url']}'$rel>$html</a>";
  605. }
  606. /**
  607. * Filter the HTML markup for a media item sent to the editor.
  608. *
  609. * @since 2.5.0
  610. *
  611. * @see wp_get_attachment_metadata()
  612. *
  613. * @param string $html HTML markup for a media item sent to the editor.
  614. * @param int $send_id The first key from the $_POST['send'] data.
  615. * @param array $attachment Array of attachment metadata.
  616. */
  617. $html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );
  618. return media_send_to_editor($html);
  619. }
  620. return $errors;
  621. }
  622. /**
  623. * Handles the process of uploading media.
  624. *
  625. * @since 2.5.0
  626. *
  627. * @return null|string
  628. */
  629. function wp_media_upload_handler() {
  630. $errors = array();
  631. $id = 0;
  632. if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
  633. check_admin_referer('media-form');
  634. // Upload File button was clicked
  635. $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
  636. unset($_FILES);
  637. if ( is_wp_error($id) ) {
  638. $errors['upload_error'] = $id;
  639. $id = false;
  640. }
  641. }
  642. if ( !empty($_POST['insertonlybutton']) ) {
  643. $src = $_POST['src'];
  644. if ( !empty($src) && !strpos($src, '://') )
  645. $src = "http://$src";
  646. if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
  647. $title = esc_html( wp_unslash( $_POST['title'] ) );
  648. if ( empty( $title ) )
  649. $title = esc_html( basename( $src ) );
  650. if ( $title && $src )
  651. $html = "<a href='" . esc_url($src) . "'>$title</a>";
  652. $type = 'file';
  653. if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
  654. && ( 'audio' == $ext_type || 'video' == $ext_type ) )
  655. $type = $ext_type;
  656. /**
  657. * Filter the URL sent to the editor for a specific media type.
  658. *
  659. * The dynamic portion of the hook name, `$type`, refers to the type
  660. * of media being sent.
  661. *
  662. * @since 3.3.0
  663. *
  664. * @param string $html HTML markup sent to the editor.
  665. * @param string $src Media source URL.
  666. * @param string $title Media title.
  667. */
  668. $html = apply_filters( $type . '_send_to_editor_url', $html, esc_url_raw( $src ), $title );
  669. } else {
  670. $align = '';
  671. $alt = esc_attr( wp_unslash( $_POST['alt'] ) );
  672. if ( isset($_POST['align']) ) {
  673. $align = esc_attr( wp_unslash( $_POST['align'] ) );
  674. $class = " class='align$align'";
  675. }
  676. if ( !empty($src) )
  677. $html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";
  678. /**
  679. * Filter the image URL sent to the editor.
  680. *
  681. * @since 2.8.0
  682. *
  683. * @param string $html HTML markup sent to the editor for an image.
  684. * @param string $src Image source URL.
  685. * @param string $alt Image alternate, or alt, text.
  686. * @param string $align The image alignment. Default 'alignnone'. Possible values include
  687. * 'alignleft', 'aligncenter', 'alignright', 'alignnone'.
  688. */
  689. $html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
  690. }
  691. return media_send_to_editor($html);
  692. }
  693. if ( isset( $_POST['save'] ) ) {
  694. $errors['upload_notice'] = __('Saved.');
  695. wp_enqueue_script( 'admin-gallery' );
  696. return wp_iframe( 'media_upload_gallery_form', $errors );
  697. } elseif ( ! empty( $_POST ) ) {
  698. $return = media_upload_form_handler();
  699. if ( is_string($return) )
  700. return $return;
  701. if ( is_array($return) )
  702. $errors = $return;
  703. }
  704. if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
  705. $type = 'image';
  706. if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
  707. $type = $_GET['type'];
  708. return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
  709. }
  710. return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
  711. }
  712. /**
  713. * Downloads an image from the specified URL and attaches it to a post.
  714. *
  715. * @since 2.6.0
  716. * @since 4.2.0 Introduced the `$return` parameter.
  717. *
  718. * @param string $file The URL of the image to download.
  719. * @param int $post_id The post ID the media is to be associated with.
  720. * @param string $desc Optional. Description of the image.
  721. * @param string $return Optional. Accepts 'html' (image tag html) or 'src' (URL). Default 'html'.
  722. * @return string|WP_Error Populated HTML img tag on success, WP_Error object otherwise.
  723. */
  724. function media_sideload_image( $file, $post_id, $desc = null, $return = 'html' ) {
  725. if ( ! empty( $file ) ) {
  726. // Set variables for storage, fix file filename for query strings.
  727. preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
  728. if ( ! $matches ) {
  729. return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL' ) );
  730. }
  731. $file_array = array();
  732. $file_array['name'] = basename( $matches[0] );
  733. // Download file to temp location.
  734. $file_array['tmp_name'] = download_url( $file );
  735. // If error storing temporarily, return the error.
  736. if ( is_wp_error( $file_array['tmp_name'] ) ) {
  737. return $file_array['tmp_name'];
  738. }
  739. // Do the validation and storage stuff.
  740. $id = media_handle_sideload( $file_array, $post_id, $desc );
  741. // If error storing permanently, unlink.
  742. if ( is_wp_error( $id ) ) {
  743. @unlink( $file_array['tmp_name'] );
  744. return $id;
  745. }
  746. $src = wp_get_attachment_url( $id );
  747. }
  748. // Finally, check to make sure the file has been saved, then return the HTML.
  749. if ( ! empty( $src ) ) {
  750. if ( $return === 'src' ) {
  751. return $src;
  752. }
  753. $alt = isset( $desc ) ? esc_attr( $desc ) : '';
  754. $html = "<img src='$src' alt='$alt' />";
  755. return $html;
  756. } else {
  757. return new WP_Error( 'image_sideload_failed' );
  758. }
  759. }
  760. /**
  761. * Retrieves the legacy media uploader form in an iframe.
  762. *
  763. * @since 2.5.0
  764. *
  765. * @return string|null
  766. */
  767. function media_upload_gallery() {
  768. $errors = array();
  769. if ( !empty($_POST) ) {
  770. $return = media_upload_form_handler();
  771. if ( is_string($return) )
  772. return $return;
  773. if ( is_array($return) )
  774. $errors = $return;
  775. }
  776. wp_enqueue_script('admin-gallery');
  777. return wp_iframe( 'media_upload_gallery_form', $errors );
  778. }
  779. /**
  780. * Retrieves the legacy media library form in an iframe.
  781. *
  782. * @since 2.5.0
  783. *
  784. * @return string|null
  785. */
  786. function media_upload_library() {
  787. $errors = array();
  788. if ( !empty($_POST) ) {
  789. $return = media_upload_form_handler();
  790. if ( is_string($return) )
  791. return $return;
  792. if ( is_array($return) )
  793. $errors = $return;
  794. }
  795. return wp_iframe( 'media_upload_library_form', $errors );
  796. }
  797. /**
  798. * Retrieve HTML for the image alignment radio buttons with the specified one checked.
  799. *
  800. * @since 2.7.0
  801. *
  802. * @param WP_Post $post
  803. * @param string $checked
  804. * @return string
  805. */
  806. function image_align_input_fields( $post, $checked = '' ) {
  807. if ( empty($checked) )
  808. $checked = get_user_setting('align', 'none');
  809. $alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
  810. if ( !array_key_exists( (string) $checked, $alignments ) )
  811. $checked = 'none';
  812. $out = array();
  813. foreach ( $alignments as $name => $label ) {
  814. $name = esc_attr($name);
  815. $out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
  816. ( $checked == $name ? " checked='checked'" : "" ) .
  817. " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
  818. }
  819. return join("\n", $out);
  820. }
  821. /**
  822. * Retrieve HTML for the size radio buttons with the specified one checked.
  823. *
  824. * @since 2.7.0
  825. *
  826. * @param WP_Post $post
  827. * @param bool|string $check
  828. * @return array
  829. */
  830. function image_size_input_fields( $post, $check = '' ) {
  831. /**
  832. * Filter the names and labels of the default image sizes.
  833. *
  834. * @since 3.3.0
  835. *
  836. * @param array $size_names Array of image sizes and their names. Default values
  837. * include 'Thumbnail', 'Medium', 'Large', 'Full Size'.
  838. */
  839. $size_names = apply_filters( 'image_size_names_choose', array(
  840. 'thumbnail' => __( 'Thumbnail' ),
  841. 'medium' => __( 'Medium' ),
  842. 'large' => __( 'Large' ),
  843. 'full' => __( 'Full Size' )
  844. ) );
  845. if ( empty( $check ) ) {
  846. $check = get_user_setting('imgsize', 'medium');
  847. }
  848. $out = array();
  849. foreach ( $size_names as $size => $label ) {
  850. $downsize = image_downsize( $post->ID, $size );
  851. $checked = '';
  852. // Is this size selectable?
  853. $enabled = ( $downsize[3] || 'full' == $size );
  854. $css_id = "image-size-{$size}-{$post->ID}";
  855. // If this size is the default but that's not available, don't select it.
  856. if ( $size == $check ) {
  857. if ( $enabled ) {
  858. $checked = " checked='checked'";
  859. } else {
  860. $check = '';
  861. }
  862. } elseif ( ! $check && $enabled && 'thumbnail' != $size ) {
  863. /*
  864. * If $check is not enabled, default to the first available size
  865. * that's bigger than a thumbnail.
  866. */
  867. $check = $size;
  868. $checked = " checked='checked'";
  869. }
  870. $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 />";
  871. $html .= "<label for='{$css_id}'>$label</label>";
  872. // Only show the dimensions if that choice is available.
  873. if ( $enabled ) {
  874. $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
  875. }
  876. $html .= '</div>';
  877. $out[] = $html;
  878. }
  879. return array(
  880. 'label' => __( 'Size' ),
  881. 'input' => 'html',
  882. 'html' => join( "\n", $out ),
  883. );
  884. }
  885. /**
  886. * Retrieve HTML for the Link URL buttons with the default link type as specified.
  887. *
  888. * @since 2.7.0
  889. *
  890. * @param WP_Post $post
  891. * @param string $url_type
  892. * @return string
  893. */
  894. function image_link_input_fields($post, $url_type = '') {
  895. $file = wp_get_attachment_url($post->ID);
  896. $link = get_attachment_link($post->ID);
  897. if ( empty($url_type) )
  898. $url_type = get_user_setting('urlbutton', 'post');
  899. $url = '';
  900. if ( $url_type == 'file' )
  901. $url = $file;
  902. elseif ( $url_type == 'post' )
  903. $url = $link;
  904. return "
  905. <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
  906. <button type='button' class='button urlnone' data-link-url=''>" . __('None') . "</button>
  907. <button type='button' class='button urlfile' data-link-url='" . esc_attr($file) . "'>" . __('File URL') . "</button>
  908. <button type='button' class='button urlpost' data-link-url='" . esc_attr($link) . "'>" . __('Attachment Post URL') . "</button>
  909. ";
  910. }
  911. /**
  912. * Output a textarea element for inputting an attachment caption.
  913. *
  914. * @since 3.4.0
  915. *
  916. * @param WP_Post $edit_post Attachment WP_Post object.
  917. * @return string HTML markup for the textarea element.
  918. */
  919. function wp_caption_input_textarea($edit_post) {
  920. // Post data is already escaped.
  921. $name = "attachments[{$edit_post->ID}][post_excerpt]";
  922. return '<textarea name="' . $name . '" id="' . $name . '">' . $edit_post->post_excerpt . '</textarea>';
  923. }
  924. /**
  925. * Retrieves the image attachment fields to edit form fields.
  926. *
  927. * @since 2.5.0
  928. *
  929. * @param array $form_fields
  930. * @param object $post
  931. * @return array
  932. */
  933. function image_attachment_fields_to_edit($form_fields, $post) {
  934. return $form_fields;
  935. }
  936. /**
  937. * Retrieves the single non-image attachment fields to edit form fields.
  938. *
  939. * @since 2.5.0
  940. *
  941. * @param array $form_fields An array of attachment form fields.
  942. * @param WP_Post $post The WP_Post attachment object.
  943. * @return array Filtered attachment form fields.
  944. */
  945. function media_single_attachment_fields_to_edit( $form_fields, $post ) {
  946. unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
  947. return $form_fields;
  948. }
  949. /**
  950. * Retrieves the post non-image attachment fields to edito form fields.
  951. *
  952. * @since 2.8.0
  953. *
  954. * @param array $form_fields An array of attachment form fields.
  955. * @param WP_Post $post The WP_Post attachment object.
  956. * @return array Filtered attachment form fields.
  957. */
  958. function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
  959. unset($form_fields['image_url']);
  960. return $form_fields;
  961. }
  962. /**
  963. * Filters input from media_upload_form_handler() and assigns a default
  964. * post_title from the file name if none supplied.
  965. *
  966. * Illustrates the use of the attachment_fields_to_save filter
  967. * which can be used to add default values to any field before saving to DB.
  968. *
  969. * @since 2.5.0
  970. *
  971. * @param array $post The WP_Post attachment object converted to an array.
  972. * @param array $attachment An array of attachment metadata.
  973. * @return array Filtered attachment post object.
  974. */
  975. function image_attachment_fields_to_save( $post, $attachment ) {
  976. if ( substr( $post['post_mime_type'], 0, 5 ) == 'image' ) {
  977. if ( strlen( trim( $post['post_title'] ) ) == 0 ) {
  978. $attachment_url = ( isset( $post['attachment_url'] ) ) ? $post['attachment_url'] : $post['guid'];
  979. $post['post_title'] = preg_replace( '/\.\w+$/', '', wp_basename( $attachment_url ) );
  980. $post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
  981. }
  982. }
  983. return $post;
  984. }
  985. /**
  986. * Retrieves the media element HTML to send to the editor.
  987. *
  988. * @since 2.5.0
  989. *
  990. * @param string $html
  991. * @param integer $attachment_id
  992. * @param array $attachment
  993. * @return string
  994. */
  995. function image_media_send_to_editor($html, $attachment_id, $attachment) {
  996. $post = get_post($attachment_id);
  997. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  998. $url = $attachment['url'];
  999. $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
  1000. $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
  1001. $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
  1002. $rel = ( $url == get_attachment_link($attachment_id) );
  1003. return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
  1004. }
  1005. return $html;
  1006. }
  1007. /**
  1008. * Retrieves the attachment fields to edit form fields.
  1009. *
  1010. * @since 2.5.0
  1011. *
  1012. * @param WP_Post $post
  1013. * @param array $errors
  1014. * @return array
  1015. */
  1016. function get_attachment_fields_to_edit($post, $errors = null) {
  1017. if ( is_int($post) )
  1018. $post = get_post($post);
  1019. if ( is_array($post) )
  1020. $post = new WP_Post( (object) $post );
  1021. $image_url = wp_get_attachment_url($post->ID);
  1022. $edit_post = sanitize_post($post, 'edit');
  1023. $form_fields = array(
  1024. 'post_title' => array(
  1025. 'label' => __('Title'),
  1026. 'value' => $edit_post->post_title
  1027. ),
  1028. 'image_alt' => array(),
  1029. 'post_excerpt' => array(
  1030. 'label' => __('Caption'),
  1031. 'input' => 'html',
  1032. 'html' => wp_caption_input_textarea($edit_post)
  1033. ),
  1034. 'post_content' => array(
  1035. 'label' => __('Description'),
  1036. 'value' => $edit_post->post_content,
  1037. 'input' => 'textarea'
  1038. ),
  1039. 'url' => array(
  1040. 'label' => __('Link URL'),
  1041. 'input' => 'html',
  1042. 'html' => image_link_input_fields($post, get_option('image_default_link_type')),
  1043. 'helps' => __('Enter a link URL or click above for presets.')
  1044. ),
  1045. 'menu_order' => array(
  1046. 'label' => __('Order'),
  1047. 'value' => $edit_post->menu_order
  1048. ),
  1049. 'image_url' => array(
  1050. 'label' => __('File URL'),
  1051. 'input' => 'html',
  1052. 'html' => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
  1053. 'value' => wp_get_attachment_url($post->ID),
  1054. 'helps' => __('Location of the uploaded file.')
  1055. )
  1056. );
  1057. foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  1058. $t = (array) get_taxonomy($taxonomy);
  1059. if ( ! $t['public'] || ! $t['show_ui'] )
  1060. continue;
  1061. if ( empty($t['label']) )
  1062. $t['label'] = $taxonomy;
  1063. if ( empty($t['args']) )
  1064. $t['args'] = array();
  1065. $terms = get_object_term_cache($post->ID, $taxonomy);
  1066. if ( false === $terms )
  1067. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  1068. $values = array();
  1069. foreach ( $terms as $term )
  1070. $values[] = $term->slug;
  1071. $t['value'] = join(', ', $values);
  1072. $form_fields[$taxonomy] = $t;
  1073. }
  1074. // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  1075. // The recursive merge is easily traversed with array casting: foreach ( (array) $things as $thing )
  1076. $form_fields = array_merge_recursive($form_fields, (array) $errors);
  1077. // This was formerly in image_attachment_fields_to_edit().
  1078. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  1079. $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
  1080. if ( empty($alt) )
  1081. $alt = '';
  1082. $form_fields['post_title']['required'] = true;
  1083. $form_fields['image_alt'] = array(
  1084. 'value' => $alt,
  1085. 'label' => __('Alternative Text'),
  1086. 'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
  1087. );
  1088. $form_fields['align'] = array(
  1089. 'label' => __('Alignment'),
  1090. 'input' => 'html',
  1091. 'html' => image_align_input_fields($post, get_option('image_default_align')),
  1092. );
  1093. $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
  1094. } else {
  1095. unset( $form_fields['image_alt'] );
  1096. }
  1097. /**
  1098. * Filter the attachment fields to edit.
  1099. *
  1100. * @since 2.5.0
  1101. *
  1102. * @param array $form_fields An array of attachment form fields.
  1103. * @param WP_Post $post The WP_Post attachment object.
  1104. */
  1105. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1106. return $form_fields;
  1107. }
  1108. /**
  1109. * Retrieve HTML for media items of post gallery.
  1110. *
  1111. * The HTML markup retrieved will be created for the progress of SWF Upload
  1112. * component. Will also create link for showing and hiding the form to modify
  1113. * the image attachment.
  1114. *
  1115. * @since 2.5.0
  1116. *
  1117. * @global WP_Query $wp_the_query
  1118. *
  1119. * @param int $post_id Optional. Post ID.
  1120. * @param array $errors Errors for attachment, if any.
  1121. * @return string
  1122. */
  1123. function get_media_items( $post_id, $errors ) {
  1124. $attachments = array();
  1125. if ( $post_id ) {
  1126. $post = get_post($post_id);
  1127. if ( $post && $post->post_type == 'attachment' )
  1128. $attachments = array($post->ID => $post);
  1129. else
  1130. $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
  1131. } else {
  1132. if ( is_array($GLOBALS['wp_the_query']->posts) )
  1133. foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
  1134. $attachments[$attachment->ID] = $attachment;
  1135. }
  1136. $output = '';
  1137. foreach ( (array) $attachments as $id => $attachment ) {
  1138. if ( $attachment->post_status == 'trash' )
  1139. continue;
  1140. if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
  1141. $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>";
  1142. }
  1143. return $output;
  1144. }
  1145. /**
  1146. * Retrieve HTML form for modifying the image attachment.
  1147. *
  1148. * @since 2.5.0
  1149. *
  1150. * @global string $redir_tab
  1151. *
  1152. * @param int $attachment_id Attachment ID for modification.
  1153. * @param string|array $args Optional. Override defaults.
  1154. * @return string HTML form for attachment.
  1155. */
  1156. function get_media_item( $attachment_id, $args = null ) {
  1157. global $redir_tab;
  1158. if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
  1159. $thumb_url = $thumb_url[0];
  1160. else
  1161. $thumb_url = false;
  1162. $post = get_post( $attachment_id );
  1163. $current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
  1164. $default_args = array(
  1165. 'errors' => null,
  1166. 'send' => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true,
  1167. 'delete' => true,
  1168. 'toggle' => true,
  1169. 'show_title' => true
  1170. );
  1171. $args = wp_parse_args( $args, $default_args );
  1172. /**
  1173. * Filter the arguments used to retrieve an image for the edit image form.
  1174. *
  1175. * @since 3.1.0
  1176. *
  1177. * @see get_media_item
  1178. *
  1179. * @param array $args An array of arguments.
  1180. */
  1181. $r = apply_filters( 'get_media_item_args', $args );
  1182. $toggle_on = __( 'Show' );
  1183. $toggle_off = __( 'Hide' );
  1184. $file = get_attached_file( $post->ID );
  1185. $filename = esc_html( wp_basename( $file ) );
  1186. $title = esc_attr( $post->post_title );
  1187. $post_mime_types = get_post_mime_types();
  1188. $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
  1189. $type = reset( $keys );
  1190. $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
  1191. $form_fields = get_attachment_fields_to_edit( $post, $r['errors'] );
  1192. if ( $r['toggle'] ) {
  1193. $class = empty( $r['errors'] ) ? 'startclosed' : 'startopen';
  1194. $toggle_links = "
  1195. <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
  1196. <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
  1197. } else {
  1198. $class = '';
  1199. $toggle_links = '';
  1200. }
  1201. $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
  1202. $display_title = $r['show_title'] ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '&hellip;' ) . "</span></div>" : '';
  1203. $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
  1204. $order = '';
  1205. foreach ( $form_fields as $key => $val ) {
  1206. if ( 'menu_order' == $key ) {
  1207. if ( $gallery )
  1208. $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>";
  1209. else
  1210. $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
  1211. unset( $form_fields['menu_order'] );
  1212. break;
  1213. }
  1214. }
  1215. $media_dims = '';
  1216. $meta = wp_get_attachment_metadata( $post->ID );
  1217. if ( isset( $meta['width'], $meta['height'] ) )
  1218. $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  1219. /**
  1220. * Filter the media metadata.
  1221. *
  1222. * @since 2.5.0
  1223. *
  1224. * @param string $media_dims The HTML markup containing the media dimensions.
  1225. * @param WP_Post $post The WP_Post attachment object.
  1226. */
  1227. $media_dims = apply_filters( 'media_meta', $media_dims, $post );
  1228. $image_edit_button = '';
  1229. if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  1230. $nonce = wp_create_nonce( "image_editor-$post->ID" );
  1231. $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>";
  1232. }
  1233. $attachment_url = get_permalink( $attachment_id );
  1234. $item = "
  1235. $type_html
  1236. $toggle_links
  1237. $order
  1238. $display_title
  1239. <table class='slidetoggle describe $class'>
  1240. <thead class='media-item-info' id='media-head-$post->ID'>
  1241. <tr>
  1242. <td class='A1B1' id='thumbnail-head-$post->ID'>
  1243. <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
  1244. <p>$image_edit_button</p>
  1245. </td>
  1246. <td>
  1247. <p><strong>" . __('File name:') . "</strong> $filename</p>
  1248. <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
  1249. <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( get_option('date_format'), $post->post_date ). '</p>';
  1250. if ( !empty( $media_dims ) )
  1251. $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
  1252. $item .= "</td></tr>\n";
  1253. $item .= "
  1254. </thead>
  1255. <tbody>
  1256. <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>
  1257. <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n";
  1258. $defaults = array(
  1259. 'input' => 'text',
  1260. 'required' => false,
  1261. 'value' => '',
  1262. 'extra_rows' => array(),
  1263. );
  1264. if ( $r['send'] ) {
  1265. $r['send'] = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
  1266. }
  1267. $delete = empty( $r['delete'] ) ? '' : $r['delete'];
  1268. if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
  1269. if ( !EMPTY_TRASH_DAYS ) {
  1270. $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>';
  1271. } elseif ( !MEDIA_TRASH ) {
  1272. $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
  1273. <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" .
  1274. /* translators: %s: file name */
  1275. '<p>' . sprintf( __( 'You are about to delete %s.' ), '<strong>' . $filename . '</strong>' ) . "</p>
  1276. <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>
  1277. <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
  1278. </div>";
  1279. } else {
  1280. $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>
  1281. <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>";
  1282. }
  1283. } else {
  1284. $delete = '';
  1285. }
  1286. $thumbnail = '';
  1287. $calling_post_id = 0;
  1288. if ( isset( $_GET['post_id'] ) ) {
  1289. $calling_post_id = absint( $_GET['post_id'] );
  1290. } elseif ( isset( $_POST ) && count( $_POST ) ) {// Like for async-upload where $_GET['post_id'] isn't set
  1291. $calling_post_id = $post->post_parent;
  1292. }
  1293. if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
  1294. && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
  1295. $calling_post = get_post( $calling_post_id );
  1296. $calling_post_type_object = get_post_type_object( $calling_post->post_type );
  1297. $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
  1298. $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>";
  1299. }
  1300. if ( ( $r['send'] || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) ) {
  1301. $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>" . $r['send'] . " $thumbnail $delete</td></tr>\n" );
  1302. }
  1303. $hidden_fields = array();
  1304. foreach ( $form_fields as $id => $field ) {
  1305. if ( $id[0] == '_' )
  1306. continue;
  1307. if ( !empty( $field['tr'] ) ) {
  1308. $item .= $field['tr'];
  1309. continue;
  1310. }
  1311. $field = array_merge( $defaults, $field );
  1312. $name = "attachments[$attachment_id][$id]";
  1313. if ( $field['input'] == 'hidden' ) {
  1314. $hidden_fields[$name] = $field['value'];
  1315. continue;
  1316. }
  1317. $required = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
  1318. $aria_required = $field['required'] ? " aria-required='true' " : '';
  1319. $class = $id;
  1320. $class .= $field['required'] ? ' form-required' : '';
  1321. $item .= "\t\t<tr class='$class'>\n\t\t\t<th scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label></th>\n\t\t\t<td class='field'>";
  1322. if ( !empty( $field[ $field['input'] ] ) )
  1323. $item .= $field[ $field['input'] ];
  1324. elseif ( $field['input'] == 'textarea' ) {
  1325. if ( 'post_content' == $id && user_can_richedit() ) {
  1326. // Sanitize_post() skips the post_content when user_can_richedit.
  1327. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1328. }
  1329. // Post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit().
  1330. $item .