PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/media.php

https://gitlab.com/geyson/geyson
PHP | 3110 lines | 3039 code | 18 blank | 53 comment | 12 complexity | a9764366a8f34e66cc39e71c68ecd951 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0

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

Large files files are truncated, but you can click here to view the full file