PageRenderTime 77ms CodeModel.GetById 25ms 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
  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 symbol (*). */
  1411. sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) .
  1412. "</p></td></tr>\n";
  1413. $defaults = array(
  1414. 'input' => 'text',
  1415. 'required' => false,
  1416. 'value' => '',
  1417. 'extra_rows' => array(),
  1418. );
  1419. if ( $parsed_args['send'] ) {
  1420. $parsed_args['send'] = get_submit_button( __( 'Insert into Post' ), '', "send[$attachment_id]", false );
  1421. }
  1422. $delete = empty( $parsed_args['delete'] ) ? '' : $parsed_args['delete'];
  1423. if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
  1424. if ( ! EMPTY_TRASH_DAYS ) {
  1425. $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>';
  1426. } elseif ( ! MEDIA_TRASH ) {
  1427. $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
  1428. <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" .
  1429. /* translators: %s: File name. */
  1430. '<p>' . sprintf( __( 'You are about to delete %s.' ), '<strong>' . $filename . '</strong>' ) . "</p>
  1431. <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>
  1432. <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . '</a>
  1433. </div>';
  1434. } else {
  1435. $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>
  1436. <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>';
  1437. }
  1438. } else {
  1439. $delete = '';
  1440. }
  1441. $thumbnail = '';
  1442. $calling_post_id = 0;
  1443. if ( isset( $_GET['post_id'] ) ) {
  1444. $calling_post_id = absint( $_GET['post_id'] );
  1445. } elseif ( isset( $_POST ) && count( $_POST ) ) {// Like for async-upload where $_GET['post_id'] isn't set.
  1446. $calling_post_id = $post->post_parent;
  1447. }
  1448. if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
  1449. && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
  1450. $calling_post = get_post( $calling_post_id );
  1451. $calling_post_type_object = get_post_type_object( $calling_post->post_type );
  1452. $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
  1453. $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>';
  1454. }
  1455. if ( ( $parsed_args['send'] || $thumbnail || $delete ) && ! isset( $form_fields['buttons'] ) ) {
  1456. $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>" . $parsed_args['send'] . " $thumbnail $delete</td></tr>\n" );
  1457. }
  1458. $hidden_fields = array();
  1459. foreach ( $form_fields as $id => $field ) {
  1460. if ( '_' === $id[0] ) {
  1461. continue;
  1462. }
  1463. if ( ! empty( $field['tr'] ) ) {
  1464. $item .= $field['tr'];
  1465. continue;
  1466. }
  1467. $field = array_merge( $defaults, $field );
  1468. $name = "attachments[$attachment_id][$id]";
  1469. if ( 'hidden' === $field['input'] ) {
  1470. $hidden_fields[ $name ] = $field['value'];
  1471. continue;
  1472. }
  1473. $required = $field['required'] ? '<span class="required">*</span>' : '';
  1474. $required_attr = $field['required'] ? ' required' : '';
  1475. $class = $id;
  1476. $class .= $field['required'] ? ' form-required' : '';
  1477. $item .= "\t\t<tr class='$class'>\n\t\t\t<th scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}{$required}</span><br class='clear' /></label></th>\n\t\t\t<td class='field'>";
  1478. if ( ! empty( $field[ $field['input'] ] ) ) {
  1479. $item .= $field[ $field['input'] ];
  1480. } elseif ( 'textarea' === $field['input'] ) {
  1481. if ( 'post_content' == $id && user_can_richedit() ) {
  1482. // Sanitize_post() skips the post_content when user_can_richedit.
  1483. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1484. }
  1485. // Post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit().
  1486. $item .= "<textarea id='$name' name='$name'{$required_attr}>" . $field['value'] . '</textarea>';
  1487. } else {
  1488. $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "'{$required_attr} />";
  1489. }
  1490. if ( ! empty( $field['helps'] ) ) {
  1491. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1492. }
  1493. $item .= "</td>\n\t\t</tr>\n";
  1494. $extra_rows = array();
  1495. if ( ! empty( $field['errors'] ) ) {
  1496. foreach ( array_unique( (array) $field['errors'] ) as $error ) {
  1497. $extra_rows['error'][] = $error;
  1498. }
  1499. }
  1500. if ( ! empty( $field['extra_rows'] ) ) {
  1501. foreach ( $field['extra_rows'] as $class => $rows ) {
  1502. foreach ( (array) $rows as $html ) {
  1503. $extra_rows[ $class ][] = $html;
  1504. }
  1505. }
  1506. }
  1507. foreach ( $extra_rows as $class => $rows ) {
  1508. foreach ( $rows as $html ) {
  1509. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1510. }
  1511. }
  1512. }
  1513. if ( ! empty( $form_fields['_final'] ) ) {
  1514. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1515. }
  1516. $item .= "\t</tbody>\n";
  1517. $item .= "\t</table>\n";
  1518. foreach ( $hidden_fields as $name => $value ) {
  1519. $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
  1520. }
  1521. if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
  1522. $parent = (int) $_REQUEST['post_id'];
  1523. $parent_name = "attachments[$attachment_id][post_parent]";
  1524. $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
  1525. }
  1526. return $item;
  1527. }
  1528. /**
  1529. * @since 3.5.0
  1530. *
  1531. * @param int $attachment_id
  1532. * @param array $args
  1533. * @return array
  1534. */
  1535. function get_compat_media_markup( $attachment_id, $args = null ) {
  1536. $post = get_post( $attachment_id );
  1537. $default_args = array(
  1538. 'errors' => null,
  1539. 'in_modal' => false,
  1540. );
  1541. $user_can_edit = current_user_can( 'edit_post', $attachment_id );
  1542. $args = wp_parse_args( $args, $default_args );
  1543. /** This filter is documented in wp-admin/includes/media.php */
  1544. $args = apply_filters( 'get_media_item_args', $args );
  1545. $form_fields = array();
  1546. if ( $args['in_modal'] ) {
  1547. foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {
  1548. $t = (array) get_taxonomy( $taxonomy );
  1549. if ( ! $t['public'] || ! $t['show_ui'] ) {
  1550. continue;
  1551. }
  1552. if ( empty( $t['label'] ) ) {
  1553. $t['label'] = $taxonomy;
  1554. }
  1555. if ( empty( $t['args'] ) ) {
  1556. $t['args'] = array();
  1557. }
  1558. $terms = get_object_term_cache( $post->ID, $taxonomy );
  1559. if ( false === $terms ) {
  1560. $terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
  1561. }
  1562. $values = array();
  1563. foreach ( $terms as $term ) {
  1564. $values[] = $term->slug;
  1565. }
  1566. $t['value'] = join( ', ', $values );
  1567. $t['taxonomy'] = true;
  1568. $form_fields[ $taxonomy ] = $t;
  1569. }
  1570. }
  1571. /*
  1572. * Merge default fields with their errors, so any key passed with the error
  1573. * (e.g. 'error', 'helps', 'value') will replace the default.
  1574. * The recursive merge is easily traversed with array casting:
  1575. * foreach ( (array) $things as $thing )
  1576. */
  1577. $form_fields = array_merge_recursive( $form_fields, (array) $args['errors'] );
  1578. /** This filter is documented in wp-admin/includes/media.php */
  1579. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1580. unset(
  1581. $form_fields['image-size'],
  1582. $form_fields['align'],
  1583. $form_fields['image_alt'],
  1584. $form_fields['post_title'],
  1585. $form_fields['post_excerpt'],
  1586. $form_fields['post_content'],
  1587. $form_fields['url'],
  1588. $form_fields['menu_order'],
  1589. $form_fields['image_url']
  1590. );
  1591. /** This filter is documented in wp-admin/includes/media.php */
  1592. $media_meta = apply_filters( 'media_meta', '', $post );
  1593. $defaults = array(
  1594. 'input' => 'text',
  1595. 'required' => false,
  1596. 'value' => '',
  1597. 'extra_rows' => array(),
  1598. 'show_in_edit' => true,
  1599. 'show_in_modal' => true,
  1600. );
  1601. $hidden_fields = array();
  1602. $item = '';
  1603. foreach ( $form_fields as $id => $field ) {
  1604. if ( '_' === $id[0] ) {
  1605. continue;
  1606. }
  1607. $name = "attachments[$attachment_id][$id]";
  1608. $id_attr = "attachments-$attachment_id-$id";
  1609. if ( ! empty( $field['tr'] ) ) {
  1610. $item .= $field['tr'];
  1611. continue;
  1612. }
  1613. $field = array_merge( $defaults, $field );
  1614. if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) ) {
  1615. continue;
  1616. }
  1617. if ( 'hidden' === $field['input'] ) {
  1618. $hidden_fields[ $name ] = $field['value'];
  1619. continue;
  1620. }
  1621. $readonly = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
  1622. $required = $field['required'] ? '<span class="required">*</span>' : '';
  1623. $required_attr = $field['required'] ? ' required' : '';
  1624. $class = 'compat-field-' . $id;
  1625. $class .= $field['required'] ? ' form-required' : '';
  1626. $item .= "\t\t<tr class='$class'>";
  1627. $item .= "\t\t\t<th scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
  1628. $item .= "</th>\n\t\t\t<td class='field'>";
  1629. if ( ! empty( $field[ $field['input'] ] ) ) {
  1630. $item .= $field[ $field['input'] ];
  1631. } elseif ( 'textarea' === $field['input'] ) {
  1632. if ( 'post_content' == $id && user_can_richedit() ) {
  1633. // sanitize_post() skips the post_content when user_can_richedit.
  1634. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1635. }
  1636. $item .= "<textarea id='$id_attr' name='$name'{$required_attr}>" . $field['value'] . '</textarea>';
  1637. } else {
  1638. $item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly{$required_attr} />";
  1639. }
  1640. if ( ! empty( $field['helps'] ) ) {
  1641. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1642. }
  1643. $item .= "</td>\n\t\t</tr>\n";
  1644. $extra_rows = array();
  1645. if ( ! empty( $field['errors'] ) ) {
  1646. foreach ( array_unique( (array) $field['errors'] ) as $error ) {
  1647. $extra_rows['error'][] = $error;
  1648. }
  1649. }
  1650. if ( ! empty( $field['extra_rows'] ) ) {
  1651. foreach ( $field['extra_rows'] as $class => $rows ) {
  1652. foreach ( (array) $rows as $html ) {
  1653. $extra_rows[ $class ][] = $html;
  1654. }
  1655. }
  1656. }
  1657. foreach ( $extra_rows as $class => $rows ) {
  1658. foreach ( $rows as $html ) {
  1659. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1660. }
  1661. }
  1662. }
  1663. if ( ! empty( $form_fields['_final'] ) ) {
  1664. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1665. }
  1666. if ( $item ) {
  1667. $item = '<p class="media-types media-types-required-info">' .
  1668. /* translators: %s: Asterisk symbol (*). */
  1669. sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) .
  1670. '</p>' .
  1671. '<table class="compat-attachment-fields">' . $item . '</table>';
  1672. }
  1673. foreach ( $hidden_fields as $hidden_field => $value ) {
  1674. $item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n";
  1675. }
  1676. if ( $item ) {
  1677. $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr( $post->menu_order ) . '" />' . $item;
  1678. }
  1679. return array(
  1680. 'item' => $item,
  1681. 'meta' => $media_meta,
  1682. );
  1683. }
  1684. /**
  1685. * Outputs the legacy media upload header.
  1686. *
  1687. * @since 2.5.0
  1688. */
  1689. function media_upload_header() {
  1690. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1691. echo '<script type="text/javascript">post_id = ' . $post_id . ';</script>';
  1692. if ( empty( $_GET['chromeless'] ) ) {
  1693. echo '<div id="media-upload-header">';
  1694. the_media_upload_tabs();
  1695. echo '</div>';
  1696. }
  1697. }
  1698. /**
  1699. * Outputs the legacy media upload form.
  1700. *
  1701. * @since 2.5.0
  1702. *
  1703. * @global string $type
  1704. * @global string $tab
  1705. * @global bool $is_IE
  1706. * @global bool $is_opera
  1707. *
  1708. * @param array $errors
  1709. */
  1710. function media_upload_form( $errors = null ) {
  1711. global $type, $tab, $is_IE, $is_opera;
  1712. if ( ! _device_can_upload() ) {
  1713. echo '<p>' . sprintf(
  1714. /* translators: %s: https://apps.wordpress.org/ */
  1715. __( 'The web browser on your device cannot be used to upload files. You may be able to use the <a href="%s">native app for your device</a> instead.' ),
  1716. 'https://apps.wordpress.org/'
  1717. ) . '</p>';
  1718. return;
  1719. }
  1720. $upload_action_url = admin_url( 'async-upload.php' );
  1721. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1722. $_type = isset( $type ) ? $type : '';
  1723. $_tab = isset( $tab ) ? $tab : '';
  1724. $max_upload_size = wp_max_upload_size();
  1725. if ( ! $max_upload_size ) {
  1726. $max_upload_size = 0;
  1727. }
  1728. ?>
  1729. <div id="media-upload-notice">
  1730. <?php
  1731. if ( isset( $errors['upload_notice'] ) ) {
  1732. echo $errors['upload_notice'];
  1733. }
  1734. ?>
  1735. </div>
  1736. <div id="media-upload-error">
  1737. <?php
  1738. if ( isset( $errors['upload_error'] ) && is_wp_error( $errors['upload_error'] ) ) {
  1739. echo $errors['upload_error']->get_error_message();
  1740. }
  1741. ?>
  1742. </div>
  1743. <?php
  1744. if ( is_multisite() && ! is_upload_space_available() ) {
  1745. /**
  1746. * Fires when an upload will exceed the defined upload space quota for a network site.
  1747. *
  1748. * @since 3.5.0
  1749. */
  1750. do_action( 'upload_ui_over_quota' );
  1751. return;
  1752. }
  1753. /**
  1754. * Fires just before the legacy (pre-3.5.0) upload interface is loaded.
  1755. *
  1756. * @since 2.6.0
  1757. */
  1758. do_action( 'pre-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  1759. $post_params = array(
  1760. 'post_id' => $post_id,
  1761. '_wpnonce' => wp_create_nonce( 'media-form' ),
  1762. 'type' => $_type,
  1763. 'tab' => $_tab,
  1764. 'short' => '1',
  1765. );
  1766. /**
  1767. * Filters the media upload post parameters.
  1768. *
  1769. * @since 3.1.0 As 'swfupload_post_params'
  1770. * @since 3.3.0
  1771. *
  1772. * @param array $post_params An array of media upload parameters used by Plupload.
  1773. */
  1774. $post_params = apply_filters( 'upload_post_params', $post_params );
  1775. /*
  1776. * Since 4.9 the `runtimes` setting is hardcoded in our version of Plupload to `html5,html4`,
  1777. * and the `flash_swf_url` and `silverlight_xap_url` are not used.
  1778. */
  1779. $plupload_init = array(
  1780. 'browse_button' => 'plupload-browse-button',
  1781. 'container' => 'plupload-upload-ui',
  1782. 'drop_element' => 'drag-drop-area',
  1783. 'file_data_name' => 'async-upload',
  1784. 'url' => $upload_action_url,
  1785. 'filters' => array( 'max_file_size' => $max_upload_size . 'b' ),
  1786. 'multipart_params' => $post_params,
  1787. );
  1788. /*
  1789. * Currently only iOS Safari supports multiple files uploading,
  1790. * but iOS 7.x has a bug that prevents uploading of videos when enabled.
  1791. * See #29602.
  1792. */
  1793. if (
  1794. wp_is_mobile() &&
  1795. strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false &&
  1796. strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false
  1797. ) {
  1798. $plupload_init['multi_selection'] = false;
  1799. }
  1800. /**
  1801. * Filters the default Plupload settings.
  1802. *
  1803. * @since 3.3.0
  1804. *
  1805. * @param array $plupload_init An array of default settings used by Plupload.
  1806. */
  1807. $plupload_init = apply_filters( 'plupload_init', $plupload_init );
  1808. ?>
  1809. <script type="text/javascript">
  1810. <?php
  1811. // Verify size is an int. If not return default value.
  1812. $large_size_h = absint( get_option( 'large_size_h' ) );
  1813. if ( ! $large_size_h ) {
  1814. $large_size_h = 1024;
  1815. }
  1816. $large_size_w = absint( get_option( 'large_size_w' ) );
  1817. if ( ! $large_size_w ) {
  1818. $large_size_w = 1024;
  1819. }
  1820. ?>
  1821. var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
  1822. wpUploaderInit = <?php echo wp_json_encode( $plupload_init ); ?>;
  1823. </script>
  1824. <div id="plupload-upload-ui" class="hide-if-no-js">
  1825. <?php
  1826. /**
  1827. * Fires before the upload interface loads.
  1828. *
  1829. * @since 2.6.0 As 'pre-flash-upload-ui'
  1830. * @since 3.3.0
  1831. */
  1832. do_action( 'pre-plupload-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  1833. ?>
  1834. <div id="drag-drop-area">
  1835. <div class="drag-drop-inside">
  1836. <p class="drag-drop-info"><?php _e( 'Drop files to upload' ); ?></p>
  1837. <p><?php _ex( 'or', 'Uploader: Drop files here - or - Select Files' ); ?></p>
  1838. <p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e( 'Select Files' ); ?>" class="button" /></p>
  1839. </div>
  1840. </div>
  1841. <?php
  1842. /**
  1843. * Fires after the upload interface loads.
  1844. *
  1845. * @since 2.6.0 As 'post-flash-upload-ui'
  1846. * @since 3.3.0
  1847. */
  1848. do_action( 'post-plupload-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  1849. ?>
  1850. </div>
  1851. <div id="html-upload-ui" class="hide-if-js">
  1852. <?php
  1853. /**
  1854. * Fires before the upload button in the media upload interface.
  1855. *
  1856. * @since 2.6.0
  1857. */
  1858. do_action( 'pre-html-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  1859. ?>
  1860. <p id="async-upload-wrap">
  1861. <label class="screen-reader-text" for="async-upload"><?php _e( 'Upload' ); ?></label>
  1862. <input type="file" name="async-upload" id="async-upload" />
  1863. <?php submit_button( __( 'Upload' ), 'primary', 'html-upload', false ); ?>
  1864. <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e( 'Cancel' ); ?></a>
  1865. </p>
  1866. <div class="clear"></div>
  1867. <?php
  1868. /**
  1869. * Fires after the upload button in the media upload interface.
  1870. *
  1871. * @since 2.6.0
  1872. */
  1873. do_action( 'post-html-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  1874. ?>
  1875. </div>
  1876. <p class="max-upload-size">
  1877. <?php
  1878. /* translators: %s: Maximum allowed file size. */
  1879. printf( __( 'Maximum upload file size: %s.' ), esc_html( size_format( $max_upload_size ) ) );
  1880. ?>
  1881. </p>
  1882. <?php
  1883. /**
  1884. * Fires on the post upload UI screen.
  1885. *
  1886. * Legacy (pre-3.5.0) media workflow hook.
  1887. *
  1888. * @since 2.6.0
  1889. */
  1890. do_action( 'post-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  1891. }
  1892. /**
  1893. * Outputs the legacy media upload form for a given media type.
  1894. *
  1895. * @since 2.5.0
  1896. *
  1897. * @param string $type
  1898. * @param object $errors
  1899. * @param integer $id
  1900. */
  1901. function media_upload_type_form( $type = 'file', $errors = null, $id = null ) {
  1902. media_upload_header();
  1903. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1904. $form_action_url = admin_url( "media-upload.php?type=$type&tab=type&post_id=$post_id" );
  1905. /**
  1906. * Filters the media upload form action URL.
  1907. *
  1908. * @since 2.6.0
  1909. *
  1910. * @param string $form_action_url The media upload form action URL.
  1911. * @param string $type The type of media. Default 'file'.
  1912. */
  1913. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1914. $form_class = 'media-upload-form type-form validate';
  1915. if ( get_user_setting( 'uploader' ) ) {
  1916. $form_class .= ' html-uploader';
  1917. }
  1918. ?>
  1919. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
  1920. <?php submit_button( '', 'hidden', 'save', false ); ?>
  1921. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1922. <?php wp_nonce_field( 'media-form' ); ?>
  1923. <h3 class="media-title"><?php _e( 'Add media files from your computer' ); ?></h3>
  1924. <?php media_upload_form( $errors ); ?>
  1925. <script type="text/javascript">
  1926. jQuery(function($){
  1927. var preloaded = $(".media-item.preloaded");
  1928. if ( preloaded.length > 0 ) {
  1929. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1930. }
  1931. updateMediaForm();
  1932. });
  1933. </script>
  1934. <div id="media-items">
  1935. <?php
  1936. if ( $id ) {
  1937. if ( ! is_wp_error( $id ) ) {
  1938. add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 );
  1939. echo get_media_items( $id, $errors );
  1940. } else {
  1941. echo '<div id="media-upload-error">' . esc_html( $id->get_error_message() ) . '</div></div>';
  1942. exit;
  1943. }
  1944. }
  1945. ?>
  1946. </div>
  1947. <p class="savebutton ml-submit">
  1948. <?php submit_button( __( 'Save all changes' ), '', 'save', false ); ?>
  1949. </p>
  1950. </form>
  1951. <?php
  1952. }
  1953. /**
  1954. * Outputs the legacy media upload form for external media.
  1955. *
  1956. * @since 2.7.0
  1957. *
  1958. * @param string $type
  1959. * @param object $errors
  1960. * @param integer $id
  1961. */
  1962. function media_upload_type_url_form( $type = null, $errors = null, $id = null ) {
  1963. if ( null === $type ) {
  1964. $type = 'image';
  1965. }
  1966. media_upload_header();
  1967. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1968. $form_action_url = admin_url( "media-upload.php?type=$type&tab=type&post_id=$post_id" );
  1969. /** This filter is documented in wp-admin/includes/media.php */
  1970. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1971. $form_class = 'media-upload-form type-form validate';
  1972. if ( get_user_setting( 'uploader' ) ) {
  1973. $form_class .= ' html-uploader';
  1974. }
  1975. ?>
  1976. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
  1977. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1978. <?php wp_nonce_field( 'media-form' ); ?>
  1979. <h3 class="media-title"><?php _e( 'Insert media from another website' ); ?></h3>
  1980. <script type="text/javascript">
  1981. var addExtImage = {
  1982. width : '',
  1983. height : '',
  1984. align : 'alignnone',
  1985. insert : function() {
  1986. var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
  1987. if ( '' == f.src.value || '' == t.width )
  1988. return false;
  1989. if ( f.alt.value )
  1990. alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1991. <?php
  1992. /** This filter is documented in wp-admin/includes/media.php */
  1993. if ( ! apply_filters( 'disable_captions', '' ) ) {
  1994. ?>
  1995. if ( f.caption.value ) {
  1996. caption = f.caption.value.replace(/\r\n|\r/g, '\n');
  1997. caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
  1998. return a.replace(/[\r\n\t]+/, ' ');
  1999. });
  2000. caption = caption.replace(/\s*\n\s*/g, '<br />');
  2001. }
  2002. <?php
  2003. }
  2004. ?>
  2005. cls = caption ? '' : ' class="'+t.align+'"';
  2006. html = '<img alt="'+alt+'" src="'+f.src.value+'"'+cls+' width="'+t.width+'" height="'+t.height+'" />';
  2007. if ( f.url.value ) {
  2008. url = f.url.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  2009. html = '<a href="'+url+'">'+html+'</a>';
  2010. }
  2011. if ( caption )
  2012. html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
  2013. var win = window.dialogArguments || opener || parent || top;
  2014. win.send_to_editor(html);
  2015. return false;
  2016. },
  2017. resetImageData : function() {
  2018. var t = addExtImage;
  2019. t.width = t.height = '';
  2020. document.getElementById('go_button').style.color = '#bbb';
  2021. if ( ! document.forms[0].src.value )
  2022. document.getElementById('status_img').innerHTML = '';
  2023. else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
  2024. },
  2025. updateImageData : function() {
  2026. var t = addExtImage;
  2027. t.width = t.preloadImg.width;
  2028. t.height = t.preloadImg.height;
  2029. document.getElementById('go_button').style.color = '#333';
  2030. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
  2031. },
  2032. getImageData : function() {
  2033. if ( jQuery('table.describe').hasClass('not-image') )
  2034. return;
  2035. var t = addExtImage, src = document.forms[0].src.value;
  2036. if ( ! src ) {
  2037. t.resetImageData();
  2038. return false;
  2039. }
  2040. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" alt="" width="16" height="16" />';
  2041. t.preloadImg = new Image();
  2042. t.preloadImg.onload = t.updateImageData;
  2043. t.preloadImg.onerror = t.resetImageData;
  2044. t.preloadImg.src = src;
  2045. }
  2046. };
  2047. jQuery(document).ready( function($) {
  2048. $('.media-types input').click( function() {
  2049. $('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
  2050. });
  2051. });
  2052. </script>
  2053. <div id="media-items">
  2054. <div class="media-item media-blank">
  2055. <?php
  2056. /**
  2057. * Filters the insert media from URL form HTML.
  2058. *
  2059. * @since 3.3.0
  2060. *
  2061. * @param string $form_html The insert from URL form HTML.
  2062. */
  2063. echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) );
  2064. ?>
  2065. </div>
  2066. </div>
  2067. </form>
  2068. <?php
  2069. }
  2070. /**
  2071. * Adds gallery form to upload iframe
  2072. *
  2073. * @since 2.5.0
  2074. *
  2075. * @global string $redir_tab
  2076. * @global string $type
  2077. * @global string $tab
  2078. *
  2079. * @param array $errors
  2080. */
  2081. function media_upload_gallery_form( $errors ) {
  2082. global $redir_tab, $type;
  2083. $redir_tab = 'gallery';
  2084. media_upload_header();
  2085. $post_id = intval( $_REQUEST['post_id'] );
  2086. $form_action_url = admin_url( "media-upload.php?type=$type&tab=gallery&post_id=$post_id" );
  2087. /** This filter is documented in wp-admin/includes/media.php */
  2088. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  2089. $form_class = 'media-upload-form validate';
  2090. if ( get_user_setting( 'uploader' ) ) {
  2091. $form_class .= ' html-uploader';
  2092. }
  2093. ?>
  2094. <script type="text/javascript">
  2095. jQuery(function($){
  2096. var preloaded = $(".media-item.preloaded");
  2097. if ( preloaded.length > 0 ) {
  2098. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  2099. updateMediaForm();
  2100. }
  2101. });
  2102. </script>
  2103. <div id="sort-buttons" class="hide-if-no-js">
  2104. <span>
  2105. <?php _e( 'All Tabs:' ); ?>
  2106. <a href="#" id="showall"><?php _e( 'Show' ); ?></a>
  2107. <a href="#" id="hideall" style="display:none;"><?php _e( 'Hide' ); ?></a>
  2108. </span>
  2109. <?php _e( 'Sort Order:' ); ?>
  2110. <a href="#" id="asc"><?php _e( 'Ascending' ); ?></a> |
  2111. <a href="#" id="desc"><?php _e( 'Descending' ); ?></a> |
  2112. <a href="#" id="clear"><?php _ex( 'Clear', 'verb' ); ?></a>
  2113. </div>
  2114. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
  2115. <?php wp_nonce_field( 'media-form' ); ?>
  2116. <?php // media_upload_form( $errors ); ?>
  2117. <table class="widefat">
  2118. <thead><tr>
  2119. <th><?php _e( 'Media' ); ?></th>
  2120. <th class="order-head"><?php _e( 'Order' ); ?></th>
  2121. <th class="actions-head"><?php _e( 'Actions' ); ?></th>
  2122. </tr></thead>
  2123. </table>
  2124. <div id="media-items">
  2125. <?php add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 ); ?>
  2126. <?php echo get_media_items( $post_id, $errors ); ?>
  2127. </div>
  2128. <p class="ml-submit">
  2129. <?php
  2130. submit_button(
  2131. __( 'Save all changes' ),
  2132. 'savebutton',
  2133. 'save',
  2134. false,
  2135. array(
  2136. 'id' => 'save-all',
  2137. 'style' => 'display: none;',
  2138. )
  2139. );
  2140. ?>
  2141. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  2142. <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
  2143. <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
  2144. </p>
  2145. <div id="gallery-settings" style="display:none;">
  2146. <div class="title"><?php _e( 'Gallery Settings' ); ?></div>
  2147. <table id="basic" class="describe"><tbody>
  2148. <tr>
  2149. <th scope="row" class="label">
  2150. <label>
  2151. <span class="alignleft"><?php _e( 'Link thumbnails to:' ); ?></span>
  2152. </label>
  2153. </th>
  2154. <td class="field">
  2155. <input type="radio" name="linkto" id="linkto-file" value="file" />
  2156. <label for="linkto-file" class="radio"><?php _e( 'Image File' ); ?></label>
  2157. <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
  2158. <label for="linkto-post" class="radio"><?php _e( 'Attachment Page' ); ?></label>
  2159. </td>
  2160. </tr>
  2161. <tr>
  2162. <th scope="row" class="label">
  2163. <label>
  2164. <span class="alignleft"><?php _e( 'Order images by:' ); ?></span>
  2165. </label>
  2166. </th>
  2167. <td class="field">
  2168. <select id="orderby" name="orderby">
  2169. <option value="menu_order" selected="selected"><?php _e( 'Menu order' ); ?></option>
  2170. <option value="title"><?php _e( 'Title' ); ?></option>
  2171. <option value="post_date"><?php _e( 'Date/Time' ); ?></option>
  2172. <option value="rand"><?php _e( 'Random' ); ?></option>
  2173. </select>
  2174. </td>
  2175. </tr>
  2176. <tr>
  2177. <th scope="row" class="label">
  2178. <label>
  2179. <span class="alignleft"><?php _e( 'Order:' ); ?></span>
  2180. </label>
  2181. </th>
  2182. <td class="field">
  2183. <input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
  2184. <label for="order-asc" class="radio"><?php _e( 'Ascending' ); ?></label>
  2185. <input type="radio" name="order" id="order-desc" value="desc" />
  2186. <label for="order-desc" class="radio"><?php _e( 'Descending' ); ?></label>
  2187. </td>
  2188. </tr>
  2189. <tr>
  2190. <th scope="row" class="label">
  2191. <label>
  2192. <span class="alignleft"><?php _e( 'Gallery columns:' ); ?></span>
  2193. </label>
  2194. </th>
  2195. <td class="field">
  2196. <select id="columns" name="columns">
  2197. <option value="1">1</option>
  2198. <option value="2">2</option>
  2199. <option value="3" selected="selected">3</option>
  2200. <option value="4">4</option>
  2201. <option value="5">5</option>
  2202. <option value="6">6</option>
  2203. <option value="7">7</option>
  2204. <option value="8">8</option>
  2205. <option value="9">9</option>
  2206. </select>
  2207. </td>
  2208. </tr>
  2209. </tbody></table>
  2210. <p class="ml-submit">
  2211. <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
  2212. <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="update-gallery" id="update-gallery" value="<?php esc_attr_e( 'Update gallery settings' ); ?>" />
  2213. </p>
  2214. </div>
  2215. </form>
  2216. <?php
  2217. }
  2218. /**
  2219. * Outputs the legacy media upload form for the media library.
  2220. *
  2221. * @since 2.5.0
  2222. *
  2223. * @global wpdb $wpdb WordPress database abstraction object.
  2224. * @global WP_Query $wp_query WordPress Query object.
  2225. * @global WP_Locale $wp_locale WordPress date and time locale object.
  2226. * @global string $type
  2227. * @global string $tab
  2228. * @global array $post_mime_types
  2229. *
  2230. * @param array $errors
  2231. */
  2232. function media_upload_library_form( $errors ) {
  2233. global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
  2234. media_upload_header();
  2235. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  2236. $form_action_url = admin_url( "media-upload.php?type=$type&tab=library&post_id=$post_id" );
  2237. /** This filter is documented in wp-admin/includes/media.php */
  2238. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  2239. $form_class = 'media-upload-form validate';
  2240. if ( get_user_setting( 'uploader' ) ) {
  2241. $form_class .= ' html-uploader';
  2242. }
  2243. $q = $_GET;
  2244. $q['posts_per_page'] = 10;
  2245. $q['paged'] = isset( $q['paged'] ) ? intval( $q['paged'] ) : 0;
  2246. if ( $q['paged'] < 1 ) {
  2247. $q['paged'] = 1;
  2248. }
  2249. $q['offset'] = ( $q['paged'] - 1 ) * 10;
  2250. if ( $q['offset'] < 1 ) {
  2251. $q['offset'] = 0;
  2252. }
  2253. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query( $q );
  2254. ?>
  2255. <form id="filter" method="get">
  2256. <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
  2257. <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
  2258. <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
  2259. <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
  2260. <input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
  2261. <p id="media-search" class="search-box">
  2262. <label class="screen-reader-text" for="media-search-input"><?php _e( 'Search Media' ); ?>:</label>
  2263. <input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
  2264. <?php submit_button( __( 'Search Media' ), '', '', false ); ?>
  2265. </p>
  2266. <ul class="subsubsub">
  2267. <?php
  2268. $type_links = array();
  2269. $_num_posts = (array) wp_count_attachments();
  2270. $matches = wp_match_mime_types( array_keys( $post_mime_types ), array_keys( $_num_posts ) );
  2271. foreach ( $matches as $_type => $reals ) {
  2272. foreach ( $reals as $real ) {
  2273. if ( isset( $num_posts[ $_type ] ) ) {
  2274. $num_posts[ $_type ] += $_num_posts[ $real ];
  2275. } else {
  2276. $num_posts[ $_type ] = $_num_posts[ $real ];
  2277. }
  2278. }
  2279. }
  2280. // If available type specified by media button clicked, filter by that type.
  2281. if ( empty( $_GET['post_mime_type'] ) && ! empty( $num_posts[ $type ] ) ) {
  2282. $_GET['post_mime_type'] = $type;
  2283. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  2284. }
  2285. if ( empty( $_GET['post_mime_type'] ) || 'all' === $_GET['post_mime_type'] ) {
  2286. $class = ' class="current"';
  2287. } else {
  2288. $class = '';
  2289. }
  2290. $type_links[] = '<li><a href="' . esc_url(
  2291. add_query_arg(
  2292. array(
  2293. 'post_mime_type' => 'all',
  2294. 'paged' => false,
  2295. 'm' => false,
  2296. )
  2297. )
  2298. ) . '"' . $class . '>' . __( 'All Types' ) . '</a>';
  2299. foreach ( $post_mime_types as $mime_type => $label ) {
  2300. $class = '';
  2301. if ( ! wp_match_mime_types( $mime_type, $avail_post_mime_types ) ) {
  2302. continue;
  2303. }
  2304. if ( isset( $_GET['post_mime_type'] ) && wp_match_mime_types( $mime_type, $_GET['post_mime_type'] ) ) {
  2305. $class = ' class="current"';
  2306. }
  2307. $type_links[] = '<li><a href="' . esc_url(
  2308. add_query_arg(
  2309. array(
  2310. 'post_mime_type' => $mime_type,
  2311. 'paged' => false,
  2312. )
  2313. )
  2314. ) . '"' . $class . '>' . sprintf( translate_nooped_plural( $label[2], $num_posts[ $mime_type ] ), '<span id="' . $mime_type . '-counter">' . number_format_i18n( $num_posts[ $mime_type ] ) . '</span>' ) . '</a>';
  2315. }
  2316. /**
  2317. * Filters the media upload mime type list items.
  2318. *
  2319. * Returned values should begin with an `<li>` tag.
  2320. *
  2321. * @since 3.1.0
  2322. *
  2323. * @param string[] $type_links An array of list items containing mime type link HTML.
  2324. */
  2325. echo implode( ' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
  2326. unset( $type_links );
  2327. ?>
  2328. </ul>
  2329. <div class="tablenav">
  2330. <?php
  2331. $page_links = paginate_links(
  2332. array(
  2333. 'base' => add_query_arg( 'paged', '%#%' ),
  2334. 'format' => '',
  2335. 'prev_text' => __( '&laquo;' ),
  2336. 'next_text' => __( '&raquo;' ),
  2337. 'total' => ceil( $wp_query->found_posts / 10 ),
  2338. 'current' => $q['paged'],
  2339. )
  2340. );
  2341. if ( $page_links ) {
  2342. echo "<div class='tablenav-pages'>$page_links</div>";
  2343. }
  2344. ?>
  2345. <div class="alignleft actions">
  2346. <?php
  2347. $arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC";
  2348. $arc_result = $wpdb->get_results( $arc_query );
  2349. $month_count = count( $arc_result );
  2350. $selected_month = isset( $_GET['m'] ) ? $_GET['m'] : 0;
  2351. if ( $month_count && ! ( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) {
  2352. ?>
  2353. <select name='m'>
  2354. <option<?php selected( $selected_month, 0 ); ?> value='0'><?php _e( 'All dates' ); ?></option>
  2355. <?php
  2356. foreach ( $arc_result as $arc_row ) {
  2357. if ( 0 == $arc_row->yyear ) {
  2358. continue;
  2359. }
  2360. $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
  2361. if ( $arc_row->yyear . $arc_row->mmonth == $selected_month ) {
  2362. $default = ' selected="selected"';
  2363. } else {
  2364. $default = '';
  2365. }
  2366. echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
  2367. echo esc_html( $wp_locale->get_month( $arc_row->mmonth ) . " $arc_row->yyear" );
  2368. echo "</option>\n";
  2369. }
  2370. ?>
  2371. </select>
  2372. <?php } ?>
  2373. <?php submit_button( __( 'Filter &#187;' ), '', 'post-query-submit', false ); ?>
  2374. </div>
  2375. <br class="clear" />
  2376. </div>
  2377. </form>
  2378. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form">
  2379. <?php wp_nonce_field( 'media-form' ); ?>
  2380. <?php // media_upload_form( $errors ); ?>
  2381. <script type="text/javascript">
  2382. jQuery(function($){
  2383. var preloaded = $(".media-item.preloaded");
  2384. if ( preloaded.length > 0 ) {
  2385. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  2386. updateMediaForm();
  2387. }
  2388. });
  2389. </script>
  2390. <div id="media-items">
  2391. <?php add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 ); ?>
  2392. <?php echo get_media_items( null, $errors ); ?>
  2393. </div>
  2394. <p class="ml-submit">
  2395. <?php submit_button( __( 'Save all changes' ), 'savebutton', 'save', false ); ?>
  2396. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  2397. </p>
  2398. </form>
  2399. <?php
  2400. }
  2401. /**
  2402. * Creates the form for external url
  2403. *
  2404. * @since 2.7.0
  2405. *
  2406. * @param string $default_view
  2407. * @return string the form html
  2408. */
  2409. function wp_media_insert_url_form( $default_view = 'image' ) {
  2410. /** This filter is documented in wp-admin/includes/media.php */
  2411. if ( ! apply_filters( 'disable_captions', '' ) ) {
  2412. $caption = '
  2413. <tr class="image-only">
  2414. <th scope="row" class="label">
  2415. <label for="caption"><span class="alignleft">' . __( 'Image Caption' ) . '</span></label>
  2416. </th>
  2417. <td class="field"><textarea id="caption" name="caption"></textarea></td>
  2418. </tr>';
  2419. } else {
  2420. $caption = '';
  2421. }
  2422. $default_align = get_option( 'image_default_align' );
  2423. if ( empty( $default_align ) ) {
  2424. $default_align = 'none';
  2425. }
  2426. if ( 'image' == $default_view ) {
  2427. $view = 'image-only';
  2428. $table_class = '';
  2429. } else {
  2430. $view = 'not-image';
  2431. $table_class = $view;
  2432. }
  2433. return '
  2434. <p class="media-types"><label><input type="radio" name="media_type" value="image" id="image-only"' . checked( 'image-only', $view, false ) . ' /> ' . __( 'Image' ) . '</label> &nbsp; &nbsp; <label><input type="radio" name="media_type" value="generic" id="not-image"' . checked( 'not-image', $view, false ) . ' /> ' . __( 'Audio, Video, or Other File' ) . '</label></p>
  2435. <p class="media-types media-types-required-info">' .
  2436. /* translators: %s: Asterisk symbol (*). */
  2437. sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) .
  2438. '</p>
  2439. <table class="describe ' . $table_class . '"><tbody>
  2440. <tr>
  2441. <th scope="row" class="label" style="width:130px;">
  2442. <label for="src"><span class="alignleft">' . __( 'URL' ) . '</span> <span class="required">*</span></label>
  2443. <span class="alignright" id="status_img"></span>
  2444. </th>
  2445. <td class="field"><input id="src" name="src" value="" type="text" required onblur="addExtImage.getImageData()" /></td>
  2446. </tr>
  2447. <tr>
  2448. <th scope="row" class="label">
  2449. <label for="title"><span class="alignleft">' . __( 'Title' ) . '</span> <span class="required">*</span></label>
  2450. </th>
  2451. <td class="field"><input id="title" name="title" value="" type="text" required /></td>
  2452. </tr>
  2453. <tr class="not-image"><td></td><td><p class="help">' . __( 'Link text, e.g. &#8220;Ransom Demands (PDF)&#8221;' ) . '</p></td></tr>
  2454. <tr class="image-only">
  2455. <th scope="row" class="label">
  2456. <label for="alt"><span class="alignleft">' . __( 'Alternative Text' ) . '</span></label>
  2457. </th>
  2458. <td class="field"><input id="alt" name="alt" value="" type="text" required />
  2459. <p class="help">' . __( 'Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;' ) . '</p></td>
  2460. </tr>
  2461. ' . $caption . '
  2462. <tr class="align image-only">
  2463. <th scope="row" class="label"><p><label for="align">' . __( 'Alignment' ) . '</label></p></th>
  2464. <td class="field">
  2465. <input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'none' === $default_align ? ' checked="checked"' : '' ) . ' />
  2466. <label for="align-none" class="align image-align-none-label">' . __( 'None' ) . '</label>
  2467. <input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'left' === $default_align ? ' checked="checked"' : '' ) . ' />
  2468. <label for="align-left" class="align image-align-left-label">' . __( 'Left' ) . '</label>
  2469. <input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'center' === $default_align ? ' checked="checked"' : '' ) . ' />
  2470. <label for="align-center" class="align image-align-center-label">' . __( 'Center' ) . '</label>
  2471. <input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'right' === $default_align ? ' checked="checked"' : '' ) . ' />
  2472. <label for="align-right" class="align image-align-right-label">' . __( 'Right' ) . '</label>
  2473. </td>
  2474. </tr>
  2475. <tr class="image-only">
  2476. <th scope="row" class="label">
  2477. <label for="url"><span class="alignleft">' . __( 'Link Image To:' ) . '</span></label>
  2478. </th>
  2479. <td class="field"><input id="url" name="url" value="" type="text" /><br />
  2480. <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __( 'None' ) . '</button>
  2481. <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __( 'Link to image' ) . '</button>
  2482. <p class="help">' . __( 'Enter a link URL or click above for presets.' ) . '</p></td>
  2483. </tr>
  2484. <tr class="image-only">
  2485. <td></td>
  2486. <td>
  2487. <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . esc_attr__( 'Insert into Post' ) . '" />
  2488. </td>
  2489. </tr>
  2490. <tr class="not-image">
  2491. <td></td>
  2492. <td>
  2493. ' . get_submit_button( __( 'Insert into Post' ), '', 'insertonlybutton', false ) . '
  2494. </td>
  2495. </tr>
  2496. </tbody></table>';
  2497. }
  2498. /**
  2499. * Displays the multi-file uploader message.
  2500. *
  2501. * @since 2.6.0
  2502. *
  2503. * @global int $post_ID
  2504. */
  2505. function media_upload_flash_bypass() {
  2506. $browser_uploader = admin_url( 'media-new.php?browser-uploader' );
  2507. $post = get_post();
  2508. if ( $post ) {
  2509. $browser_uploader .= '&amp;post_id=' . intval( $post->ID );
  2510. } elseif ( ! empty( $GLOBALS['post_ID'] ) ) {
  2511. $browser_uploader .= '&amp;post_id=' . intval( $GLOBALS['post_ID'] );
  2512. }
  2513. ?>
  2514. <p class="upload-flash-bypass">
  2515. <?php
  2516. printf(
  2517. /* translators: 1: URL to browser uploader, 2: Additional link attributes. */
  2518. __( 'You are using the multi-file uploader. Problems? Try the <a href="%1$s" %2$s>browser uploader</a> instead.' ),
  2519. $browser_uploader,
  2520. 'target="_blank"'
  2521. );
  2522. ?>
  2523. </p>
  2524. <?php
  2525. }
  2526. /**
  2527. * Displays the browser's built-in uploader message.
  2528. *
  2529. * @since 2.6.0
  2530. */
  2531. function media_upload_html_bypass() {
  2532. ?>
  2533. <p class="upload-html-bypass hide-if-no-js">
  2534. <?php _e( 'You are using the browser&#8217;s built-in file uploader. The WordPress uploader includes multiple file selection and drag and drop capability. <a href="#">Switch to the multi-file uploader</a>.' ); ?>
  2535. </p>
  2536. <?php
  2537. }
  2538. /**
  2539. * Used to display a "After a file has been uploaded..." help message.
  2540. *
  2541. * @since 3.3.0
  2542. */
  2543. function media_upload_text_after() {}
  2544. /**
  2545. * Displays the checkbox to scale images.
  2546. *
  2547. * @since 3.3.0
  2548. */
  2549. function media_upload_max_image_resize() {
  2550. $checked = get_user_setting( 'upload_resize' ) ? ' checked="true"' : '';
  2551. $a = '';
  2552. $end = '';
  2553. if ( current_user_can( 'manage_options' ) ) {
  2554. $a = '<a href="' . esc_url( admin_url( 'options-media.php' ) ) . '" target="_blank">';
  2555. $end = '</a>';
  2556. }
  2557. ?>
  2558. <p class="hide-if-no-js"><label>
  2559. <input name="image_resize" type="checkbox" id="image_resize" value="true"<?php echo $checked; ?> />
  2560. <?php
  2561. /* translators: 1: Link start tag, 2: Link end tag, 3: Width, 4: Height. */
  2562. printf( __( 'Scale images to match the large size selected in %1$simage options%2$s (%3$d &times; %4$d).' ), $a, $end, (int) get_option( 'large_size_w', '1024' ), (int) get_option( 'large_size_h', '1024' ) );
  2563. ?>
  2564. </label></p>
  2565. <?php
  2566. }
  2567. /**
  2568. * Displays the out of storage quota message in Multisite.
  2569. *
  2570. * @since 3.5.0
  2571. */
  2572. function multisite_over_quota_message() {
  2573. echo '<p>' . sprintf(
  2574. /* translators: %s: Allowed space allocation. */
  2575. __( 'Sorry, you have used your space allocation of %s. Please delete some files to upload more files.' ),
  2576. size_format( get_space_allowed() * MB_IN_BYTES )
  2577. ) . '</p>';
  2578. }
  2579. /**
  2580. * Displays the image and editor in the post editor
  2581. *
  2582. * @since 3.5.0
  2583. *
  2584. * @param WP_Post $post A post object.
  2585. */
  2586. function edit_form_image_editor( $post ) {
  2587. $open = isset( $_GET['image-editor'] );
  2588. if ( $open ) {
  2589. require_once ABSPATH . 'wp-admin/includes/image-edit.php';
  2590. }
  2591. $thumb_url = false;
  2592. $attachment_id = intval( $post->ID );
  2593. if ( $attachment_id ) {
  2594. $thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
  2595. }
  2596. $alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
  2597. $att_url = wp_get_attachment_url( $post->ID );
  2598. ?>
  2599. <div class="wp_attachment_holder wp-clearfix">
  2600. <?php
  2601. if ( wp_attachment_is_image( $post->ID ) ) :
  2602. $image_edit_button = '';
  2603. if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  2604. $nonce = wp_create_nonce( "image_editor-$post->ID" );
  2605. $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>";
  2606. }
  2607. $open_style = '';
  2608. $not_open_style = '';
  2609. if ( $open ) {
  2610. $open_style = ' style="display:none"';
  2611. } else {
  2612. $not_open_style = ' style="display:none"';
  2613. }
  2614. ?>
  2615. <div class="imgedit-response" id="imgedit-response-<?php echo $attachment_id; ?>"></div>
  2616. <div<?php echo $open_style; ?> class="wp_attachment_image wp-clearfix" id="media-head-<?php echo $attachment_id; ?>">
  2617. <p id="thumbnail-head-<?php echo $attachment_id; ?>"><img class="thumbnail" src="<?php echo set_url_scheme( $thumb_url[0] ); ?>" style="max-width:100%" alt="" /></p>
  2618. <p><?php echo $image_edit_button; ?></p>
  2619. </div>
  2620. <div<?php echo $not_open_style; ?> class="image-editor" id="image-editor-<?php echo $attachment_id; ?>">
  2621. <?php
  2622. if ( $open ) {
  2623. wp_image_editor( $attachment_id );
  2624. }
  2625. ?>
  2626. </div>
  2627. <?php
  2628. elseif ( $attachment_id && wp_attachment_is( 'audio', $post ) ) :
  2629. wp_maybe_generate_attachment_metadata( $post );
  2630. echo wp_audio_shortcode( array( 'src' => $att_url ) );
  2631. elseif ( $attachment_id && wp_attachment_is( 'video', $post ) ) :
  2632. wp_maybe_generate_attachment_metadata( $post );
  2633. $meta = wp_get_attachment_metadata( $attachment_id );
  2634. $w = ! empty( $meta['width'] ) ? min( $meta['width'], 640 ) : 0;
  2635. $h = ! empty( $meta['height'] ) ? $meta['height'] : 0;
  2636. if ( $h && $w < $meta['width'] ) {
  2637. $h = round( ( $meta['height'] * $w ) / $meta['width'] );
  2638. }
  2639. $attr = array( 'src' => $att_url );
  2640. if ( ! empty( $w ) && ! empty( $h ) ) {
  2641. $attr['width'] = $w;
  2642. $attr['height'] = $h;
  2643. }
  2644. $thumb_id = get_post_thumbnail_id( $attachment_id );
  2645. if ( ! empty( $thumb_id ) ) {
  2646. $attr['poster'] = wp_get_attachment_url( $thumb_id );
  2647. }
  2648. echo wp_video_shortcode( $attr );
  2649. elseif ( isset( $thumb_url[0] ) ) :
  2650. ?>
  2651. <div class="wp_attachment_image wp-clearfix" id="media-head-<?php echo $attachment_id; ?>">
  2652. <p id="thumbnail-head-<?php echo $attachment_id; ?>">
  2653. <img class="thumbnail" src="<?php echo set_url_scheme( $thumb_url[0] ); ?>" style="max-width:100%" alt="" />
  2654. </p>
  2655. </div>
  2656. <?php
  2657. else :
  2658. /**
  2659. * Fires when an attachment type can't be rendered in the edit form.
  2660. *
  2661. * @since 4.6.0
  2662. *
  2663. * @param WP_Post $post A post object.
  2664. */
  2665. do_action( 'wp_edit_form_attachment_display', $post );
  2666. endif;
  2667. ?>
  2668. </div>
  2669. <div class="wp_attachment_details edit-form-section">
  2670. <?php if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) : ?>
  2671. <p class="attachment-alt-text">
  2672. <label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br />
  2673. <input type="text" class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" aria-describedby="alt-text-description" value="<?php echo esc_attr( $alt_text ); ?>" />
  2674. </p>
  2675. <p class="attachment-alt-text-description" id="alt-text-description">
  2676. <?php
  2677. printf(
  2678. /* translators: 1: Link to tutorial, 2: Additional link attributes, 3: Accessibility text. */
  2679. __( '<a href="%1$s" %2$s>Describe the purpose of the image%3$s</a>. Leave empty if the image is purely decorative.' ),
  2680. esc_url( 'https://www.w3.org/WAI/tutorials/images/decision-tree' ),
  2681. 'target="_blank" rel="noopener noreferrer"',
  2682. sprintf(
  2683. '<span class="screen-reader-text"> %s</span>',
  2684. /* translators: Accessibility text. */
  2685. __( '(opens in a new tab)' )
  2686. )
  2687. );
  2688. ?>
  2689. </p>
  2690. <?php endif; ?>
  2691. <p>
  2692. <label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong></label><br />
  2693. <textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea>
  2694. </p>
  2695. <?php
  2696. $quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
  2697. $editor_args = array(
  2698. 'textarea_name' => 'content',
  2699. 'textarea_rows' => 5,
  2700. 'media_buttons' => false,
  2701. 'tinymce' => false,
  2702. 'quicktags' => $quicktags_settings,
  2703. );
  2704. ?>
  2705. <label for="attachment_content" class="attachment-content-description"><strong><?php _e( 'Description' ); ?></strong>
  2706. <?php
  2707. if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  2708. echo ': ' . __( 'Displayed on attachment pages.' );
  2709. }
  2710. ?>
  2711. </label>
  2712. <?php wp_editor( $post->post_content, 'attachment_content', $editor_args ); ?>
  2713. </div>
  2714. <?php
  2715. $extras = get_compat_media_markup( $post->ID );
  2716. echo $extras['item'];
  2717. echo '<input type="hidden" id="image-edit-context" value="edit-attachment" />' . "\n";
  2718. }
  2719. /**
  2720. * Displays non-editable attachment metadata in the publish meta box.
  2721. *
  2722. * @since 3.5.0
  2723. */
  2724. function attachment_submitbox_metadata() {
  2725. $post = get_post();
  2726. $attachment_id = $post->ID;
  2727. $file = get_attached_file( $attachment_id );
  2728. $filename = esc_html( wp_basename( $file ) );
  2729. $media_dims = '';
  2730. $meta = wp_get_attachment_metadata( $attachment_id );
  2731. if ( isset( $meta['width'], $meta['height'] ) ) {
  2732. $media_dims .= "<span id='media-dims-$attachment_id'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  2733. }
  2734. /** This filter is documented in wp-admin/includes/media.php */
  2735. $media_dims = apply_filters( 'media_meta', $media_dims, $post );
  2736. $att_url = wp_get_attachment_url( $attachment_id );
  2737. ?>
  2738. <div class="misc-pub-section misc-pub-attachment">
  2739. <label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
  2740. <input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" id="attachment_url" value="<?php echo esc_attr( $att_url ); ?>" />
  2741. </div>
  2742. <div class="misc-pub-section misc-pub-filename">
  2743. <?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
  2744. </div>
  2745. <div class="misc-pub-section misc-pub-filetype">
  2746. <?php _e( 'File type:' ); ?>
  2747. <strong>
  2748. <?php
  2749. if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
  2750. echo esc_html( strtoupper( $matches[1] ) );
  2751. list( $mime_type ) = explode( '/', $post->post_mime_type );
  2752. if ( 'image' !== $mime_type && ! empty( $meta['mime_type'] ) ) {
  2753. if ( "$mime_type/" . strtolower( $matches[1] ) !== $meta['mime_type'] ) {
  2754. echo ' (' . $meta['mime_type'] . ')';
  2755. }
  2756. }
  2757. } else {
  2758. echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
  2759. }
  2760. ?>
  2761. </strong>
  2762. </div>
  2763. <?php
  2764. $file_size = false;
  2765. if ( isset( $meta['filesize'] ) ) {
  2766. $file_size = $meta['filesize'];
  2767. } elseif ( file_exists( $file ) ) {
  2768. $file_size = filesize( $file );
  2769. }
  2770. if ( ! empty( $file_size ) ) {
  2771. ?>
  2772. <div class="misc-pub-section misc-pub-filesize">
  2773. <?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong>
  2774. </div>
  2775. <?php
  2776. }
  2777. if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  2778. $fields = array(
  2779. 'length_formatted' => __( 'Length:' ),
  2780. 'bitrate' => __( 'Bitrate:' ),
  2781. );
  2782. /**
  2783. * Filters the audio and video metadata fields to be shown in the publish meta box.
  2784. *
  2785. * The key for each item in the array should correspond to an attachment
  2786. * metadata key, and the value should be the desired label.
  2787. *
  2788. * @since 3.7.0
  2789. * @since 4.9.0 Added the `$post` parameter.
  2790. *
  2791. * @param array $fields An array of the attachment metadata keys and labels.
  2792. * @param WP_Post $post WP_Post object for the current attachment.
  2793. */
  2794. $fields = apply_filters( 'media_submitbox_misc_sections', $fields, $post );
  2795. foreach ( $fields as $key => $label ) {
  2796. if ( empty( $meta[ $key ] ) ) {
  2797. continue;
  2798. }
  2799. ?>
  2800. <div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  2801. <?php echo $label; ?>
  2802. <strong>
  2803. <?php
  2804. switch ( $key ) {
  2805. case 'bitrate':
  2806. echo round( $meta['bitrate'] / 1000 ) . 'kb/s';
  2807. if ( ! empty( $meta['bitrate_mode'] ) ) {
  2808. echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) );
  2809. }
  2810. break;
  2811. default:
  2812. echo esc_html( $meta[ $key ] );
  2813. break;
  2814. }
  2815. ?>
  2816. </strong>
  2817. </div>
  2818. <?php
  2819. }
  2820. $fields = array(
  2821. 'dataformat' => __( 'Audio Format:' ),
  2822. 'codec' => __( 'Audio Codec:' ),
  2823. );
  2824. /**
  2825. * Filters the audio attachment metadata fields to be shown in the publish meta box.
  2826. *
  2827. * The key for each item in the array should correspond to an attachment
  2828. * metadata key, and the value should be the desired label.
  2829. *
  2830. * @since 3.7.0
  2831. * @since 4.9.0 Added the `$post` parameter.
  2832. *
  2833. * @param array $fields An array of the attachment metadata keys and labels.
  2834. * @param WP_Post $post WP_Post object for the current attachment.
  2835. */
  2836. $audio_fields = apply_filters( 'audio_submitbox_misc_sections', $fields, $post );
  2837. foreach ( $audio_fields as $key => $label ) {
  2838. if ( empty( $meta['audio'][ $key ] ) ) {
  2839. continue;
  2840. }
  2841. ?>
  2842. <div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  2843. <?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][ $key ] ); ?></strong>
  2844. </div>
  2845. <?php
  2846. }
  2847. }
  2848. if ( $media_dims ) {
  2849. ?>
  2850. <div class="misc-pub-section misc-pub-dimensions">
  2851. <?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
  2852. </div>
  2853. <?php
  2854. }
  2855. if ( ! empty( $meta['original_image'] ) ) {
  2856. ?>
  2857. <div class="misc-pub-section misc-pub-original-image">
  2858. <?php _e( 'Original image:' ); ?>
  2859. <a href="<?php echo esc_url( wp_get_original_image_url( $attachment_id ) ); ?>">
  2860. <?php echo esc_html( wp_basename( wp_get_original_image_path( $attachment_id ) ) ); ?>
  2861. </a>
  2862. </div>
  2863. <?php
  2864. }
  2865. }
  2866. /**
  2867. * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
  2868. *
  2869. * @since 3.6.0
  2870. *
  2871. * @param array $metadata An existing array with data
  2872. * @param array $data Data supplied by ID3 tags
  2873. */
  2874. function wp_add_id3_tag_data( &$metadata, $data ) {
  2875. foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
  2876. if ( ! empty( $data[ $version ]['comments'] ) ) {
  2877. foreach ( $data[ $version ]['comments'] as $key => $list ) {
  2878. if ( 'length' !== $key && ! empty( $list ) ) {
  2879. $metadata[ $key ] = wp_kses_post( reset( $list ) );
  2880. // Fix bug in byte stream analysis.
  2881. if ( 'terms_of_use' === $key && 0 === strpos( $metadata[ $key ], 'yright notice.' ) ) {
  2882. $metadata[ $key ] = 'Cop' . $metadata[ $key ];
  2883. }
  2884. }
  2885. }
  2886. break;
  2887. }
  2888. }
  2889. if ( ! empty( $data['id3v2']['APIC'] ) ) {
  2890. $image = reset( $data['id3v2']['APIC'] );
  2891. if ( ! empty( $image['data'] ) ) {
  2892. $metadata['image'] = array(
  2893. 'data' => $image['data'],
  2894. 'mime' => $image['image_mime'],
  2895. 'width' => $image['image_width'],
  2896. 'height' => $image['image_height'],
  2897. );
  2898. }
  2899. } elseif ( ! empty( $data['comments']['picture'] ) ) {
  2900. $image = reset( $data['comments']['picture'] );
  2901. if ( ! empty( $image['data'] ) ) {
  2902. $metadata['image'] = array(
  2903. 'data' => $image['data'],
  2904. 'mime' => $image['image_mime'],
  2905. );
  2906. }
  2907. }
  2908. }
  2909. /**
  2910. * Retrieve metadata from a video file's ID3 tags
  2911. *
  2912. * @since 3.6.0
  2913. *
  2914. * @param string $file Path to file.
  2915. * @return array|bool Returns array of metadata, if found.
  2916. */
  2917. function wp_read_video_metadata( $file ) {
  2918. if ( ! file_exists( $file ) ) {
  2919. return false;
  2920. }
  2921. $metadata = array();
  2922. if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
  2923. define( 'GETID3_TEMP_DIR', get_temp_dir() );
  2924. }
  2925. if ( ! class_exists( 'getID3', false ) ) {
  2926. require ABSPATH . WPINC . '/ID3/getid3.php';
  2927. }
  2928. $id3 = new getID3();
  2929. $data = $id3->analyze( $file );
  2930. if ( isset( $data['video']['lossless'] ) ) {
  2931. $metadata['lossless'] = $data['video']['lossless'];
  2932. }
  2933. if ( ! empty( $data['video']['bitrate'] ) ) {
  2934. $metadata['bitrate'] = (int) $data['video']['bitrate'];
  2935. }
  2936. if ( ! empty( $data['video']['bitrate_mode'] ) ) {
  2937. $metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
  2938. }
  2939. if ( ! empty( $data['filesize'] ) ) {
  2940. $metadata['filesize'] = (int) $data['filesize'];
  2941. }
  2942. if ( ! empty( $data['mime_type'] ) ) {
  2943. $metadata['mime_type'] = $data['mime_type'];
  2944. }
  2945. if ( ! empty( $data['playtime_seconds'] ) ) {
  2946. $metadata['length'] = (int) round( $data['playtime_seconds'] );
  2947. }
  2948. if ( ! empty( $data['playtime_string'] ) ) {
  2949. $metadata['length_formatted'] = $data['playtime_string'];
  2950. }
  2951. if ( ! empty( $data['video']['resolution_x'] ) ) {
  2952. $metadata['width'] = (int) $data['video']['resolution_x'];
  2953. }
  2954. if ( ! empty( $data['video']['resolution_y'] ) ) {
  2955. $metadata['height'] = (int) $data['video']['resolution_y'];
  2956. }
  2957. if ( ! empty( $data['fileformat'] ) ) {
  2958. $metadata['fileformat'] = $data['fileformat'];
  2959. }
  2960. if ( ! empty( $data['video']['dataformat'] ) ) {
  2961. $metadata['dataformat'] = $data['video']['dataformat'];
  2962. }
  2963. if ( ! empty( $data['video']['encoder'] ) ) {
  2964. $metadata['encoder'] = $data['video']['encoder'];
  2965. }
  2966. if ( ! empty( $data['video']['codec'] ) ) {
  2967. $metadata['codec'] = $data['video']['codec'];
  2968. }
  2969. if ( ! empty( $data['audio'] ) ) {
  2970. unset( $data['audio']['streams'] );
  2971. $metadata['audio'] = $data['audio'];
  2972. }
  2973. if ( empty( $metadata['created_timestamp'] ) ) {
  2974. $created_timestamp = wp_get_media_creation_timestamp( $data );
  2975. if ( false !== $created_timestamp ) {
  2976. $metadata['created_timestamp'] = $created_timestamp;
  2977. }
  2978. }
  2979. wp_add_id3_tag_data( $metadata, $data );
  2980. $file_format = isset( $metadata['fileformat'] ) ? $metadata['fileformat'] : null;
  2981. /**
  2982. * Filters the array of metadata retrieved from a video.
  2983. *
  2984. * In core, usually this selection is what is stored.
  2985. * More complete data can be parsed from the `$data` parameter.
  2986. *
  2987. * @since 4.9.0
  2988. *
  2989. * @param array $metadata Filtered Video metadata.
  2990. * @param string $file Path to video file.
  2991. * @param string $file_format File format of video, as analyzed by getID3.
  2992. * @param string $data Raw metadata from getID3.
  2993. */
  2994. return apply_filters( 'wp_read_video_metadata', $metadata, $file, $file_format, $data );
  2995. }
  2996. /**
  2997. * Retrieve metadata from an audio file's ID3 tags.
  2998. *
  2999. * @since 3.6.0
  3000. *
  3001. * @param string $file Path to file.
  3002. * @return array|bool Returns array of metadata, if found.
  3003. */
  3004. function wp_read_audio_metadata( $file ) {
  3005. if ( ! file_exists( $file ) ) {
  3006. return false;
  3007. }
  3008. $metadata = array();
  3009. if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
  3010. define( 'GETID3_TEMP_DIR', get_temp_dir() );
  3011. }
  3012. if ( ! class_exists( 'getID3', false ) ) {
  3013. require ABSPATH . WPINC . '/ID3/getid3.php';
  3014. }
  3015. $id3 = new getID3();
  3016. $data = $id3->analyze( $file );
  3017. if ( ! empty( $data['audio'] ) ) {
  3018. unset( $data['audio']['streams'] );
  3019. $metadata = $data['audio'];
  3020. }
  3021. if ( ! empty( $data['fileformat'] ) ) {
  3022. $metadata['fileformat'] = $data['fileformat'];
  3023. }
  3024. if ( ! empty( $data['filesize'] ) ) {
  3025. $metadata['filesize'] = (int) $data['filesize'];
  3026. }
  3027. if ( ! empty( $data['mime_type'] ) ) {
  3028. $metadata['mime_type'] = $data['mime_type'];
  3029. }
  3030. if ( ! empty( $data['playtime_seconds'] ) ) {
  3031. $metadata['length'] = (int) round( $data['playtime_seconds'] );
  3032. }
  3033. if ( ! empty( $data['playtime_string'] ) ) {
  3034. $metadata['length_formatted'] = $data['playtime_string'];
  3035. }
  3036. if ( empty( $metadata['created_timestamp'] ) ) {
  3037. $created_timestamp = wp_get_media_creation_timestamp( $data );
  3038. if ( false !== $created_timestamp ) {
  3039. $metadata['created_timestamp'] = $created_timestamp;
  3040. }
  3041. }
  3042. wp_add_id3_tag_data( $metadata, $data );
  3043. return $metadata;
  3044. }
  3045. /**
  3046. * Parse creation date from media metadata.
  3047. *
  3048. * The getID3 library doesn't have a standard method for getting creation dates,
  3049. * so the location of this data can vary based on the MIME type.
  3050. *
  3051. * @since 4.9.0
  3052. *
  3053. * @link https://github.com/JamesHeinrich/getID3/blob/master/structure.txt
  3054. *
  3055. * @param array $metadata The metadata returned by getID3::analyze().
  3056. * @return int|bool A UNIX timestamp for the media's creation date if available
  3057. * or a boolean FALSE if a timestamp could not be determined.
  3058. */
  3059. function wp_get_media_creation_timestamp( $metadata ) {
  3060. $creation_date = false;
  3061. if ( empty( $metadata['fileformat'] ) ) {
  3062. return $creation_date;
  3063. }
  3064. switch ( $metadata['fileformat'] ) {
  3065. case 'asf':
  3066. if ( isset( $metadata['asf']['file_properties_object']['creation_date_unix'] ) ) {
  3067. $creation_date = (int) $metadata['asf']['file_properties_object']['creation_date_unix'];
  3068. }
  3069. break;
  3070. case 'matroska':
  3071. case 'webm':
  3072. if ( isset( $metadata['matroska']['comments']['creation_time']['0'] ) ) {
  3073. $creation_date = strtotime( $metadata['matroska']['comments']['creation_time']['0'] );
  3074. } elseif ( isset( $metadata['matroska']['info']['0']['DateUTC_unix'] ) ) {
  3075. $creation_date = (int) $metadata['matroska']['info']['0']['DateUTC_unix'];
  3076. }
  3077. break;
  3078. case 'quicktime':
  3079. case 'mp4':
  3080. if ( isset( $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'] ) ) {
  3081. $creation_date = (int) $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'];
  3082. }
  3083. break;
  3084. }
  3085. return $creation_date;
  3086. }
  3087. /**
  3088. * Encapsulate logic for Attach/Detach actions
  3089. *
  3090. * @since 4.2.0
  3091. *
  3092. * @global wpdb $wpdb WordPress database abstraction object.
  3093. *
  3094. * @param int $parent_id Attachment parent ID.
  3095. * @param string $action Optional. Attach/detach action. Accepts 'attach' or 'detach'.
  3096. * Default 'attach'.
  3097. */
  3098. function wp_media_attach_action( $parent_id, $action = 'attach' ) {
  3099. global $wpdb;
  3100. if ( ! $parent_id ) {
  3101. return;
  3102. }
  3103. if ( ! current_user_can( 'edit_post', $parent_id ) ) {
  3104. wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
  3105. }
  3106. $ids = array();
  3107. foreach ( (array) $_REQUEST['media'] as $att_id ) {
  3108. $att_id = (int) $att_id;
  3109. if ( ! current_user_can( 'edit_post', $att_id ) ) {
  3110. continue;
  3111. }
  3112. $ids[] = $att_id;
  3113. }
  3114. if ( ! empty( $ids ) ) {
  3115. $ids_string = implode( ',', $ids );
  3116. if ( 'attach' === $action ) {
  3117. $result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $ids_string )", $parent_id ) );
  3118. } else {
  3119. $result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" );
  3120. }
  3121. foreach ( $ids as $att_id ) {
  3122. clean_attachment_cache( $att_id );
  3123. }
  3124. }
  3125. if ( isset( $result ) ) {
  3126. $location = 'upload.php';
  3127. $referer = wp_get_referer();
  3128. if ( $referer ) {
  3129. if ( false !== strpos( $referer, 'upload.php' ) ) {
  3130. $location = remove_query_arg( array( 'attached', 'detach' ), $referer );
  3131. }
  3132. }
  3133. $key = 'attach' === $action ? 'attached' : 'detach';
  3134. $location = add_query_arg( array( $key => $result ), $location );
  3135. wp_redirect( $location );
  3136. exit;
  3137. }
  3138. }