PageRenderTime 74ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/media.php

https://bitbucket.org/theshipswakecreative/psw
PHP | 2988 lines | 2921 code | 18 blank | 49 comment | 12 complexity | d3d2c0429f4fdfb40a902c36db781682 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0

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

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

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