PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/media.php

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

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