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

/wp-admin/includes/media.php

http://github.com/wordpress/wordpress
PHP | 3746 lines | 2288 code | 603 blank | 855 comment | 409 complexity | 09ae324ec8b5507fecbab7b407eda489 MD5 | raw file
Possible License(s): 0BSD

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

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