PageRenderTime 431ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/media.php

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