PageRenderTime 1168ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/media.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 2350 lines | 2294 code | 16 blank | 40 comment | 11 complexity | 9afd8ae95e7d59fc65f5f9ed371864b5 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  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 : "<?php echo esc_attr( $flash_action_url ); ?>",
  1295. flash_url : "<?php echo includes_url('js/swfupload/swfupload.swf'); ?>",
  1296. file_post_name: "async-upload",
  1297. file_types: "<?php echo apply_filters('upload_file_glob', '*.*'); ?>",
  1298. post_params : {
  1299. <?php echo $post_params_str; ?>
  1300. },
  1301. file_size_limit : "<?php echo $max_upload_size; ?>b",
  1302. file_dialog_start_handler : fileDialogStart,
  1303. file_queued_handler : fileQueued,
  1304. upload_start_handler : uploadStart,
  1305. upload_progress_handler : uploadProgress,
  1306. upload_error_handler : uploadError,
  1307. upload_success_handler : <?php echo apply_filters( 'swfupload_success_handler', 'uploadSuccess' ); ?>,
  1308. upload_complete_handler : uploadComplete,
  1309. file_queue_error_handler : fileQueueError,
  1310. file_dialog_complete_handler : fileDialogComplete,
  1311. swfupload_pre_load_handler: swfuploadPreLoad,
  1312. swfupload_load_failed_handler: swfuploadLoadFailed,
  1313. custom_settings : {
  1314. degraded_element_id : "html-upload-ui", // id of the element displayed when swfupload is unavailable
  1315. swfupload_element_id : "flash-upload-ui" // id of the element displayed when swfupload is available
  1316. },
  1317. debug: false
  1318. };
  1319. swfu = new SWFUpload(settings);
  1320. };
  1321. //]]>
  1322. </script>
  1323. <div id="flash-upload-ui" class="hide-if-no-js">
  1324. <?php do_action('pre-flash-upload-ui'); ?>
  1325. <div>
  1326. <?php _e( 'Choose files to upload' ); ?>
  1327. <div id="flash-browse-button"></div>
  1328. <span><input id="cancel-upload" disabled="disabled" onclick="cancelUpload()" type="button" value="<?php esc_attr_e('Cancel Upload'); ?>" class="button" /></span>
  1329. </div>
  1330. <p class="media-upload-size"><?php printf( __( 'Maximum upload file size: %d%s' ), $upload_size_unit, $sizes[$u] ); ?></p>
  1331. <?php do_action('post-flash-upload-ui'); ?>
  1332. <p class="howto"><?php _e('After a file has been uploaded, you can add titles and descriptions.'); ?></p>
  1333. </div>
  1334. <?php endif; // $flash ?>
  1335. <div id="html-upload-ui" <?php if ( $flash ) echo 'class="hide-if-js"'; ?>>
  1336. <?php do_action('pre-html-upload-ui'); ?>
  1337. <p id="async-upload-wrap">
  1338. <label class="screen-reader-text" for="async-upload"><?php _e('Upload'); ?></label>
  1339. <input type="file" name="async-upload" id="async-upload" />
  1340. <?php submit_button( __( 'Upload' ), 'button', 'html-upload', false ); ?>
  1341. <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e('Cancel'); ?></a>
  1342. </p>
  1343. <div class="clear"></div>
  1344. <p class="media-upload-size"><?php printf( __( 'Maximum upload file size: %d%s' ), $upload_size_unit, $sizes[$u] ); ?></p>
  1345. <?php if ( is_lighttpd_before_150() ): ?>
  1346. <p><?php _e('If you want to use all capabilities of the uploader, like uploading multiple files at once, please update to lighttpd 1.5.'); ?></p>
  1347. <?php endif;?>
  1348. <?php do_action('post-html-upload-ui', $flash); ?>
  1349. </div>
  1350. <?php do_action('post-upload-ui'); ?>
  1351. <?php
  1352. }
  1353. /**
  1354. * {@internal Missing Short Description}}
  1355. *
  1356. * @since 2.5.0
  1357. *
  1358. * @param unknown_type $type
  1359. * @param unknown_type $errors
  1360. * @param unknown_type $id
  1361. */
  1362. function media_upload_type_form($type = 'file', $errors = null, $id = null) {
  1363. media_upload_header();
  1364. $post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
  1365. $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
  1366. $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
  1367. ?>
  1368. <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form type-form validate" id="<?php echo $type; ?>-form">
  1369. <?php submit_button( '', 'hidden', 'save', false ); ?>
  1370. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1371. <?php wp_nonce_field('media-form'); ?>
  1372. <h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
  1373. <?php media_upload_form( $errors ); ?>
  1374. <script type="text/javascript">
  1375. //<![CDATA[
  1376. jQuery(function($){
  1377. var preloaded = $(".media-item.preloaded");
  1378. if ( preloaded.length > 0 ) {
  1379. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1380. }
  1381. updateMediaForm();
  1382. });
  1383. //]]>
  1384. </script>
  1385. <div id="media-items">
  1386. <?php
  1387. if ( $id ) {
  1388. if ( !is_wp_error($id) ) {
  1389. add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
  1390. echo get_media_items( $id, $errors );
  1391. } else {
  1392. echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div>';
  1393. exit;
  1394. }
  1395. }
  1396. ?>
  1397. </div>
  1398. <p class="savebutton ml-submit">
  1399. <?php submit_button( __( 'Save all changes' ), 'button', 'save', false ); ?>
  1400. </p>
  1401. </form>
  1402. <?php
  1403. }
  1404. /**
  1405. * {@internal Missing Short Description}}
  1406. *
  1407. * @since 2.7.0
  1408. *
  1409. * @param unknown_type $type
  1410. * @param unknown_type $errors
  1411. * @param unknown_type $id
  1412. */
  1413. function media_upload_type_url_form($type = 'file', $errors = null, $id = null) {
  1414. media_upload_header();
  1415. $post_id = intval($_REQUEST['post_id']);
  1416. $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
  1417. $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
  1418. $callback = "type_url_form_$type";
  1419. ?>
  1420. <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form type-form validate" id="<?php echo $type; ?>-form">
  1421. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1422. <?php wp_nonce_field('media-form'); ?>
  1423. <?php if ( is_callable($callback) ) { ?>
  1424. <h3 class="media-title"><?php _e('Add media file from URL'); ?></h3>
  1425. <script type="text/javascript">
  1426. //<![CDATA[
  1427. var addExtImage = {
  1428. width : '',
  1429. height : '',
  1430. align : 'alignnone',
  1431. insert : function() {
  1432. var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
  1433. if ( '' == f.src.value || '' == t.width )
  1434. return false;
  1435. if ( f.title.value ) {
  1436. title = f.title.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1437. title = ' title="'+title+'"';
  1438. }
  1439. if ( f.alt.value )
  1440. alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1441. <?php if ( ! apply_filters( 'disable_captions', '' ) ) { ?>
  1442. if ( f.caption.value )
  1443. caption = f.caption.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1444. <?php } ?>
  1445. cls = caption ? '' : ' class="'+t.align+'"';
  1446. html = '<img alt="'+alt+'" src="'+f.src.value+'"'+title+cls+' width="'+t.width+'" height="'+t.height+'" />';
  1447. if ( f.url.value )
  1448. html = '<a href="'+f.url.value+'">'+html+'</a>';
  1449. if ( caption )
  1450. html = '[caption id="" align="'+t.align+'" width="'+t.width+'" caption="'+caption+'"]'+html+'[/caption]';
  1451. var win = window.dialogArguments || opener || parent || top;
  1452. win.send_to_editor(html);
  1453. return false;
  1454. },
  1455. resetImageData : function() {
  1456. var t = addExtImage;
  1457. t.width = t.height = '';
  1458. document.getElementById('go_button').style.color = '#bbb';
  1459. if ( ! document.forms[0].src.value )
  1460. document.getElementById('status_img').innerHTML = '*';
  1461. else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
  1462. },
  1463. updateImageData : function() {
  1464. var t = addExtImage;
  1465. t.width = t.preloadImg.width;
  1466. t.height = t.preloadImg.height;
  1467. document.getElementById('go_button').style.color = '#333';
  1468. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
  1469. },
  1470. getImageData : function() {
  1471. var t = addExtImage, src = document.forms[0].src.value;
  1472. if ( ! src ) {
  1473. t.resetImageData();
  1474. return false;
  1475. }
  1476. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />';
  1477. t.preloadImg = new Image();
  1478. t.preloadImg.onload = t.updateImageData;
  1479. t.preloadImg.onerror = t.resetImageData;
  1480. t.preloadImg.src = src;
  1481. }
  1482. }
  1483. //]]>
  1484. </script>
  1485. <div id="media-items">
  1486. <div class="media-item media-blank">
  1487. <?php echo apply_filters($callback, call_user_func($callback)); ?>
  1488. </div>
  1489. </div>
  1490. </form>
  1491. <?php
  1492. } else {
  1493. wp_die( __('Unknown action.') );
  1494. }
  1495. }
  1496. /**
  1497. * {@internal Missing Short Description}}
  1498. *
  1499. * @since 2.5.0
  1500. *
  1501. * @param unknown_type $errors
  1502. */
  1503. function media_upload_gallery_form($errors) {
  1504. global $redir_tab, $type;
  1505. $redir_tab = 'gallery';
  1506. media_upload_header();
  1507. $post_id = intval($_REQUEST['post_id']);
  1508. $form_action_url = admin_url("media-upload.php?type=$type&tab=gallery&post_id=$post_id");
  1509. $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
  1510. ?>
  1511. <script type="text/javascript">
  1512. <!--
  1513. jQuery(function($){
  1514. var preloaded = $(".media-item.preloaded");
  1515. if ( preloaded.length > 0 ) {
  1516. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1517. updateMediaForm();
  1518. }
  1519. });
  1520. -->
  1521. </script>
  1522. <div id="sort-buttons" class="hide-if-no-js">
  1523. <span>
  1524. <?php _e('All Tabs:'); ?>
  1525. <a href="#" id="showall"><?php _e('Show'); ?></a>
  1526. <a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
  1527. </span>
  1528. <?php _e('Sort Order:'); ?>
  1529. <a href="#" id="asc"><?php _e('Ascending'); ?></a> |
  1530. <a href="#" id="desc"><?php _e('Descending'); ?></a> |
  1531. <a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
  1532. </div>
  1533. <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form validate" id="gallery-form">
  1534. <?php wp_nonce_field('media-form'); ?>
  1535. <?php //media_upload_form( $errors ); ?>
  1536. <table class="widefat" cellspacing="0">
  1537. <thead><tr>
  1538. <th><?php _e('Media'); ?></th>
  1539. <th class="order-head"><?php _e('Order'); ?></th>
  1540. <th class="actions-head"><?php _e('Actions'); ?></th>
  1541. </tr></thead>
  1542. </table>
  1543. <div id="media-items">
  1544. <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
  1545. <?php echo get_media_items($post_id, $errors); ?>
  1546. </div>
  1547. <p class="ml-submit">
  1548. <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
  1549. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1550. <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
  1551. <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
  1552. </p>
  1553. <div id="gallery-settings" style="display:none;">
  1554. <div class="title"><?php _e('Gallery Settings'); ?></div>
  1555. <table id="basic" class="describe"><tbody>
  1556. <tr>
  1557. <th scope="row" class="label">
  1558. <label>
  1559. <span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
  1560. </label>
  1561. </th>
  1562. <td class="field">
  1563. <input type="radio" name="linkto" id="linkto-file" value="file" />
  1564. <label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
  1565. <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
  1566. <label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
  1567. </td>
  1568. </tr>
  1569. <tr>
  1570. <th scope="row" class="label">
  1571. <label>
  1572. <span class="alignleft"><?php _e('Order images by:'); ?></span>
  1573. </label>
  1574. </th>
  1575. <td class="field">
  1576. <select id="orderby" name="orderby">
  1577. <option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
  1578. <option value="title"><?php _e('Title'); ?></option>
  1579. <option value="ID"><?php _e('Date/Time'); ?></option>
  1580. <option value="rand"><?php _e('Random'); ?></option>
  1581. </select>
  1582. </td>
  1583. </tr>
  1584. <tr>
  1585. <th scope="row" class="label">
  1586. <label>
  1587. <span class="alignleft"><?php _e('Order:'); ?></span>
  1588. </label>
  1589. </th>
  1590. <td class="field">
  1591. <input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
  1592. <label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
  1593. <input type="radio" name="order" id="order-desc" value="desc" />
  1594. <label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
  1595. </td>
  1596. </tr>
  1597. <tr>
  1598. <th scope="row" class="label">
  1599. <label>
  1600. <span class="alignleft"><?php _e('Gallery columns:'); ?></span>
  1601. </label>
  1602. </th>
  1603. <td class="field">
  1604. <select id="columns" name="columns">
  1605. <option value="1">1</option>
  1606. <option value="2">2</option>
  1607. <option value="3" selected="selected">3</option>
  1608. <option value="4">4</option>
  1609. <option value="5">5</option>
  1610. <option value="6">6</option>
  1611. <option value="7">7</option>
  1612. <option value="8">8</option>
  1613. <option value="9">9</option>
  1614. </select>
  1615. </td>
  1616. </tr>
  1617. </tbody></table>
  1618. <p class="ml-submit">
  1619. <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
  1620. <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="update-gallery" id="update-gallery" value="<?php esc_attr_e( 'Update gallery settings' ); ?>" />
  1621. </p>
  1622. </div>
  1623. </form>
  1624. <?php
  1625. }
  1626. /**
  1627. * {@internal Missing Short Description}}
  1628. *
  1629. * @since 2.5.0
  1630. *
  1631. * @param unknown_type $errors
  1632. */
  1633. function media_upload_library_form($errors) {
  1634. global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
  1635. media_upload_header();
  1636. $post_id = intval($_REQUEST['post_id']);
  1637. $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
  1638. $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
  1639. $_GET['paged'] = isset( $_GET['paged'] ) ? intval($_GET['paged']) : 0;
  1640. if ( $_GET['paged'] < 1 )
  1641. $_GET['paged'] = 1;
  1642. $start = ( $_GET['paged'] - 1 ) * 10;
  1643. if ( $start < 1 )
  1644. $start = 0;
  1645. add_filter( 'post_limits', create_function( '$a', "return 'LIMIT $start, 10';" ) );
  1646. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  1647. ?>
  1648. <form id="filter" action="" method="get">
  1649. <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
  1650. <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
  1651. <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
  1652. <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
  1653. <p id="media-search" class="search-box">
  1654. <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
  1655. <input type="text" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
  1656. <?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
  1657. </p>
  1658. <ul class="subsubsub">
  1659. <?php
  1660. $type_links = array();
  1661. $_num_posts = (array) wp_count_attachments();
  1662. $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  1663. foreach ( $matches as $_type => $reals )
  1664. foreach ( $reals as $real )
  1665. if ( isset($num_posts[$_type]) )
  1666. $num_posts[$_type] += $_num_posts[$real];
  1667. else
  1668. $num_posts[$_type] = $_num_posts[$real];
  1669. // If available type specified by media button clicked, filter by that type
  1670. if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
  1671. $_GET['post_mime_type'] = $type;
  1672. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  1673. }
  1674. if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
  1675. $class = ' class="current"';
  1676. else
  1677. $class = '';
  1678. $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
  1679. foreach ( $post_mime_types as $mime_type => $label ) {
  1680. $class = '';
  1681. if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  1682. continue;
  1683. if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  1684. $class = ' class="current"';
  1685. $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false))) . "'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), "<span id='$mime_type-counter'>" . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
  1686. }
  1687. echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
  1688. unset($type_links);
  1689. ?>
  1690. </ul>
  1691. <div class="tablenav">
  1692. <?php
  1693. $page_links = paginate_links( array(
  1694. 'base' => add_query_arg( 'paged', '%#%' ),
  1695. 'format' => '',
  1696. 'prev_text' => __('&laquo;'),
  1697. 'next_text' => __('&raquo;'),
  1698. 'total' => ceil($wp_query->found_posts / 10),
  1699. 'current' => $_GET['paged']
  1700. ));
  1701. if ( $page_links )
  1702. echo "<div class='tablenav-pages'>$page_links</div>";
  1703. ?>
  1704. <div class="alignleft actions">
  1705. <?php
  1706. $arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC";
  1707. $arc_result = $wpdb->get_results( $arc_query );
  1708. $month_count = count($arc_result);
  1709. if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
  1710. <select name='m'>
  1711. <option<?php selected( @$_GET['m'], 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
  1712. <?php
  1713. foreach ($arc_result as $arc_row) {
  1714. if ( $arc_row->yyear == 0 )
  1715. continue;
  1716. $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
  1717. if ( isset($_GET['m']) && ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] ) )
  1718. $default = ' selected="selected"';
  1719. else
  1720. $default = '';
  1721. echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
  1722. echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
  1723. echo "</option>\n";
  1724. }
  1725. ?>
  1726. </select>
  1727. <?php } ?>
  1728. <?php submit_button( __( 'Filter &#187;' ), 'secondary', 'post-query-submit', false ); ?>
  1729. </div>
  1730. <br class="clear" />
  1731. </div>
  1732. </form>
  1733. <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form validate" id="library-form">
  1734. <?php wp_nonce_field('media-form'); ?>
  1735. <?php //media_upload_form( $errors ); ?>
  1736. <script type="text/javascript">
  1737. <!--
  1738. jQuery(function($){
  1739. var preloaded = $(".media-item.preloaded");
  1740. if ( preloaded.length > 0 ) {
  1741. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1742. updateMediaForm();
  1743. }
  1744. });
  1745. -->
  1746. </script>
  1747. <div id="media-items">
  1748. <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
  1749. <?php echo get_media_items(null, $errors); ?>
  1750. </div>
  1751. <p class="ml-submit">
  1752. <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
  1753. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1754. </p>
  1755. </form>
  1756. <?php
  1757. }
  1758. /**
  1759. * {@internal Missing Short Description}}
  1760. *
  1761. * @since 2.7.0
  1762. *
  1763. * @return unknown
  1764. */
  1765. function type_url_form_image() {
  1766. if ( !apply_filters( 'disable_captions', '' ) ) {
  1767. $caption = '
  1768. <tr>
  1769. <th valign="top" scope="row" class="label">
  1770. <span class="alignleft"><label for="caption">' . __('Image Caption') . '</label></span>
  1771. </th>
  1772. <td class="field"><input id="caption" name="caption" value="" type="text" /></td>
  1773. </tr>
  1774. ';
  1775. } else {
  1776. $caption = '';
  1777. }
  1778. $default_align = get_option('image_default_align');
  1779. if ( empty($default_align) )
  1780. $default_align = 'none';
  1781. return '
  1782. <h4 class="media-sub-title">' . __('Insert an image from another web site') . '</h4>
  1783. <table class="describe"><tbody>
  1784. <tr>
  1785. <th valign="top" scope="row" class="label" style="width:130px;">
  1786. <span class="alignleft"><label for="src">' . __('Image URL') . '</label></span>
  1787. <span class="alignright"><abbr id="status_img" title="required" class="required">*</abbr></span>
  1788. </th>
  1789. <td class="field"><input id="src" name="src" value="" type="text" aria-required="true" onblur="addExtImage.getImageData()" /></td>
  1790. </tr>
  1791. <tr>
  1792. <th valign="top" scope="row" class="label">
  1793. <span class="alignleft"><label for="title">' . __('Image Title') . '</label></span>
  1794. <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1795. </th>
  1796. <td class="field"><input id="title" name="title" value="" type="text" aria-required="true" /></td>
  1797. </tr>
  1798. <tr>
  1799. <th valign="top" scope="row" class="label">
  1800. <span class="alignleft"><label for="alt">' . __('Alternate Text') . '</label></span>
  1801. </th>
  1802. <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
  1803. <p class="help">' . __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;') . '</p></td>
  1804. </tr>
  1805. ' . $caption . '
  1806. <tr class="align">
  1807. <th valign="top" scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
  1808. <td class="field">
  1809. <input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'none' ? ' checked="checked"' : '').' />
  1810. <label for="align-none" class="align image-align-none-label">' . __('None') . '</label>
  1811. <input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'left' ? ' checked="checked"' : '').' />
  1812. <label for="align-left" class="align image-align-left-label">' . __('Left') . '</label>
  1813. <input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'center' ? ' checked="checked"' : '').' />
  1814. <label for="align-center" class="align image-align-center-label">' . __('Center') . '</label>
  1815. <input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'right' ? ' checked="checked"' : '').' />
  1816. <label for="align-right" class="align image-align-right-label">' . __('Right') . '</label>
  1817. </td>
  1818. </tr>
  1819. <tr>
  1820. <th valign="top" scope="row" class="label">
  1821. <span class="alignleft"><label for="url">' . __('Link Image To:') . '</label></span>
  1822. </th>
  1823. <td class="field"><input id="url" name="url" value="" type="text" /><br />
  1824. <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __('None') . '</button>
  1825. <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __('Link to image') . '</button>
  1826. <p class="help">' . __('Enter a link URL or click above for presets.') . '</p></td>
  1827. </tr>
  1828. ' . _insert_into_post_button('image') . '
  1829. </tbody></table>
  1830. ';
  1831. }
  1832. /**
  1833. * {@internal Missing Short Description}}
  1834. *
  1835. * @since 2.7.0
  1836. *
  1837. * @return unknown
  1838. */
  1839. function type_url_form_audio() {
  1840. return '
  1841. <table class="describe"><tbody>
  1842. <tr>
  1843. <th valign="top" scope="row" class="label">
  1844. <span class="alignleft"><label for="insertonly[href]">' . __('Audio File URL') . '</label></span>
  1845. <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1846. </th>
  1847. <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
  1848. </tr>
  1849. <tr>
  1850. <th valign="top" scope="row" class="label">
  1851. <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
  1852. <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1853. </th>
  1854. <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
  1855. </tr>
  1856. <tr><td></td><td class="help">' . __('Link text, e.g. &#8220;Still Alive by Jonathan Coulton&#8221;') . '</td></tr>
  1857. ' . _insert_into_post_button('audio') . '
  1858. </tbody></table>
  1859. ';
  1860. }
  1861. /**
  1862. * {@internal Missing Short Description}}
  1863. *
  1864. * @since 2.7.0
  1865. *
  1866. * @return unknown
  1867. */
  1868. function type_url_form_video() {
  1869. return '
  1870. <table class="describe"><tbody>
  1871. <tr>
  1872. <th valign="top" scope="row" class="label">
  1873. <span class="alignleft"><label for="insertonly[href]">' . __('Video URL') . '</label></span>
  1874. <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1875. </th>
  1876. <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
  1877. </tr>
  1878. <tr>
  1879. <th valign="top" scope="row" class="label">
  1880. <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
  1881. <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1882. </th>
  1883. <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
  1884. </tr>
  1885. <tr><td></td><td class="help">' . __('Link text, e.g. &#8220;Lucy on YouTube&#8221;') . '</td></tr>
  1886. ' . _insert_into_post_button('video') . '
  1887. </tbody></table>
  1888. ';
  1889. }
  1890. /**
  1891. * {@internal Missing Short Description}}
  1892. *
  1893. * @since 2.7.0
  1894. *
  1895. * @return unknown
  1896. */
  1897. function type_url_form_file() {
  1898. return '
  1899. <table class="describe"><tbody>
  1900. <tr>
  1901. <th valign="top" scope="row" class="label">
  1902. <span class="alignleft"><label for="insertonly[href]">' . __('URL') . '</label></span>
  1903. <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1904. </th>
  1905. <td class="field"><input id="insertonly[href]" name="insertonly[href]" value="" type="text" aria-required="true"></td>
  1906. </tr>
  1907. <tr>
  1908. <th valign="top" scope="row" class="label">
  1909. <span class="alignleft"><label for="insertonly[title]">' . __('Title') . '</label></span>
  1910. <span class="alignright"><abbr title="required" class="required">*</abbr></span>
  1911. </th>
  1912. <td class="field"><input id="insertonly[title]" name="insertonly[title]" value="" type="text" aria-required="true"></td>
  1913. </tr>
  1914. <tr><td></td><td class="help">' . __('Link text, e.g. &#8220;Ransom Demands (PDF)&#8221;') . '</td></tr>
  1915. ' . _insert_into_post_button('file') . '
  1916. </tbody></table>
  1917. ';
  1918. }
  1919. function _insert_into_post_button($type) {
  1920. if ( !post_type_supports(get_post_type($_GET['post_id']), 'editor') )
  1921. return '';
  1922. if ( 'image' == $type )
  1923. return '
  1924. <tr>
  1925. <td></td>
  1926. <td>
  1927. <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . esc_attr__('Insert into Post') . '" />
  1928. </td>
  1929. </tr>
  1930. ';
  1931. return '
  1932. <tr>
  1933. <td></td>
  1934. <td>
  1935. ' . get_submit_button( __( 'Insert into Post' ), 'button', 'insertonlybutton', false ) . '
  1936. </td>
  1937. </tr>
  1938. ';
  1939. }
  1940. /**
  1941. * {@internal Missing Short Description}}
  1942. *
  1943. * Support a GET parameter for disabling the flash uploader.
  1944. *
  1945. * @since 2.6.0
  1946. *
  1947. * @param unknown_type $flash
  1948. * @return unknown
  1949. */
  1950. function media_upload_use_flash($flash) {
  1951. if ( array_key_exists('flash', $_REQUEST) )
  1952. $flash = !empty($_REQUEST['flash']);
  1953. return $flash;
  1954. }
  1955. add_filter('flash_uploader', 'media_upload_use_flash');
  1956. /**
  1957. * {@internal Missing Short Description}}
  1958. *
  1959. * @since 2.6.0
  1960. */
  1961. function media_upload_flash_bypass() {
  1962. echo '<p class="upload-flash-bypass">';
  1963. printf( __('You are using the Flash uploader. Problems? Try the <a href="%s">Browser uploader</a> instead.'), esc_url(add_query_arg('flash', 0)) );
  1964. echo '</p>';
  1965. }
  1966. /**
  1967. * {@internal Missing Short Description}}
  1968. *
  1969. * @since 2.6.0
  1970. */
  1971. function media_upload_html_bypass($flash = true) {
  1972. echo '<p class="upload-html-bypass hide-if-no-js">';
  1973. _e('You are using the Browser uploader.');
  1974. if ( $flash ) {
  1975. // the user manually selected the browser uploader, so let them switch back to Flash
  1976. echo ' ';
  1977. printf( __('Try the <a href="%s">Flash uploader</a> instead.'), esc_url(add_query_arg('flash', 1)) );
  1978. }
  1979. echo "</p>\n";
  1980. }
  1981. add_action('post-flash-upload-ui', 'media_upload_flash_bypass');
  1982. add_action('post-html-upload-ui', 'media_upload_html_bypass');
  1983. /**
  1984. * {@internal Missing Short Description}}
  1985. *
  1986. * Make sure the GET parameter sticks when we submit a form.
  1987. *
  1988. * @since 2.6.0
  1989. *
  1990. * @param unknown_type $url
  1991. * @return unknown
  1992. */
  1993. function media_upload_bypass_url($url) {
  1994. if ( array_key_exists('flash', $_REQUEST) )
  1995. $url = add_query_arg('flash', intval($_REQUEST['flash']));
  1996. return $url;
  1997. }
  1998. add_filter('media_upload_form_url', 'media_upload_bypass_url');
  1999. add_filter('async_upload_image', 'get_media_item', 10, 2);
  2000. add_filter('async_upload_audio', 'get_media_item', 10, 2);
  2001. add_filter('async_upload_video', 'get_media_item', 10, 2);
  2002. add_filter('async_upload_file', 'get_media_item', 10, 2);
  2003. add_action('media_upload_image', 'media_upload_image');
  2004. add_action('media_upload_audio', 'media_upload_audio');
  2005. add_action('media_upload_video', 'media_upload_video');
  2006. add_action('media_upload_file', 'media_upload_file');
  2007. add_filter('media_upload_gallery', 'media_upload_gallery');
  2008. add_filter('media_upload_library', 'media_upload_library');