PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/media.php

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