PageRenderTime 66ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/wp-admin/includes/media.php

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

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