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

/branches/2.1/wp-admin/upload-functions.php

#
PHP | 357 lines | 308 code | 47 blank | 2 comment | 68 complexity | eba2404f5d61d964dee69a618b7993f6 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. function wp_upload_display( $dims = false, $href = '' ) {
  3. global $post;
  4. $id = get_the_ID();
  5. $attachment_data = wp_get_attachment_metadata( $id );
  6. $is_image = (int) wp_attachment_is_image();
  7. if ( !isset($attachment_data['width']) && $is_image ) {
  8. if ( $image_data = getimagesize( get_attached_file( $id ) ) ) {
  9. $attachment_data['width'] = $image_data[0];
  10. $attachment_data['height'] = $image_data[1];
  11. wp_update_attachment_metadata( $id, $attachment_data );
  12. }
  13. }
  14. if ( isset($attachment_data['width']) )
  15. list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128);
  16. ob_start();
  17. the_title();
  18. $post_title = attribute_escape(ob_get_contents());
  19. ob_end_clean();
  20. $post_content = apply_filters( 'content_edit_pre', $post->post_content );
  21. $class = 'text';
  22. $innerHTML = get_attachment_innerHTML( $id, false, $dims );
  23. if ( $image_src = get_attachment_icon_src() ) {
  24. $image_rel = wp_make_link_relative($image_src);
  25. $innerHTML = '&nbsp;' . str_replace($image_src, $image_rel, $innerHTML);
  26. $class = 'image';
  27. }
  28. $src_base = wp_get_attachment_url();
  29. $src = wp_make_link_relative( $src_base );
  30. $src_base = str_replace($src, '', $src_base);
  31. $r = '';
  32. if ( $href )
  33. $r .= "<a id='file-link-$id' href='" . clean_url($href) ."' title='$post_title' class='file-link $class'>\n";
  34. if ( $href || $image_src )
  35. $r .= "\t\t\t$innerHTML";
  36. if ( $href )
  37. $r .= "</a>\n";
  38. $r .= "\n\t\t<div class='upload-file-data'>\n\t\t\t<p>\n";
  39. $r .= "\t\t\t\t<input type='hidden' name='attachment-url-$id' id='attachment-url-$id' value='$src' />\n";
  40. $r .= "\t\t\t\t<input type='hidden' name='attachment-url-base-$id' id='attachment-url-base-$id' value='$src_base' />\n";
  41. if ( !$thumb_base = wp_get_attachment_thumb_url() )
  42. $thumb_base = wp_mime_type_icon();
  43. if ( $thumb_base ) {
  44. $thumb_rel = wp_make_link_relative( $thumb_base );
  45. $thumb_base = str_replace( $thumb_rel, '', $thumb_base );
  46. $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-$id' id='attachment-thumb-url-$id' value='$thumb_rel' />\n";
  47. $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-base-$id' id='attachment-thumb-url-base-$id' value='$thumb_base' />\n";
  48. }
  49. $r .= "\t\t\t\t<input type='hidden' name='attachment-is-image-$id' id='attachment-is-image-$id' value='$is_image' />\n";
  50. if ( isset($width) ) {
  51. $r .= "\t\t\t\t<input type='hidden' name='attachment-width-$id' id='attachment-width-$id' value='$width' />\n";
  52. $r .= "\t\t\t\t<input type='hidden' name='attachment-height-$id' id='attachment-height-$id' value='$height' />\n";
  53. }
  54. $r .= "\t\t\t\t<input type='hidden' name='attachment-page-url-$id' id='attachment-page-url-$id' value='" . get_attachment_link( $id ) . "' />\n";
  55. $r .= "\t\t\t\t<input type='hidden' name='attachment-title-$id' id='attachment-title-$id' value='$post_title' />\n";
  56. $r .= "\t\t\t\t<input type='hidden' name='attachment-description-$id' id='attachment-description-$id' value='$post_content' />\n";
  57. $r .= "\t\t\t</p>\n\t\t</div>\n";
  58. return $r;
  59. }
  60. function wp_upload_view() {
  61. global $style, $post_id, $style;
  62. $id = get_the_ID();
  63. $attachment_data = wp_get_attachment_metadata( $id );
  64. ?>
  65. <div id="upload-file">
  66. <div id="file-title">
  67. <h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style )
  68. echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
  69. the_title();
  70. if ( !isset($attachment_data['width']) && 'inline' != $style )
  71. echo '</a>';
  72. ?></h2>
  73. <span><?php
  74. echo '[&nbsp;';
  75. echo '<a href="' . get_permalink() . '">' . __('view') . '</a>';
  76. echo '&nbsp;|&nbsp;';
  77. echo '<a href="' . clean_url(add_query_arg('action', 'edit')) . '" title="' . __('Edit this file') . '">' . __('edit') . '</a>';
  78. echo '&nbsp;|&nbsp;';
  79. echo '<a href="' . clean_url(remove_query_arg(array('action', 'ID'))) . '" title="' . __('Browse your files') . '">' . __('cancel') . '</a>';
  80. echo '&nbsp;]'; ?></span>
  81. </div>
  82. <div id="upload-file-view" class="alignleft">
  83. <?php if ( isset($attachment_data['width']) && 'inline' != $style )
  84. echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
  85. echo wp_upload_display( array(171, 128) );
  86. if ( isset($attachment_data['width']) && 'inline' != $style )
  87. echo '</a>'; ?>
  88. </div>
  89. <?php the_attachment_links( $id ); ?>
  90. </div>
  91. <?php echo "<form action='' id='browse-form'><input type='hidden' id='nonce-value' value='" . wp_create_nonce( 'inlineuploading' ) . "' /></form>\n";
  92. }
  93. function wp_upload_form() {
  94. $id = get_the_ID();
  95. global $post_id, $tab, $style;
  96. $enctype = $id ? '' : ' enctype="multipart/form-data"';
  97. ?>
  98. <form<?php echo $enctype; ?> id="upload-file" method="post" action="<?php echo get_option('siteurl') . "/wp-admin/upload.php?style=$style&amp;tab=upload&amp;post_id=$post_id"; ?>">
  99. <?php
  100. if ( $id ) :
  101. $attachment = get_post_to_edit( $id );
  102. $attachment_data = wp_get_attachment_metadata( $id );
  103. ?>
  104. <div id="file-title">
  105. <h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style )
  106. echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
  107. the_title();
  108. if ( !isset($attachment_data['width']) && 'inline' != $style )
  109. echo '</a>';
  110. ?></h2>
  111. <span><?php
  112. echo '[&nbsp;';
  113. echo '<a href="' . get_permalink() . '">' . __('view') . '</a>';
  114. echo '&nbsp;|&nbsp;';
  115. echo '<a href="' . clean_url(add_query_arg('action', 'view')) . '">' . __('links') . '</a>';
  116. echo '&nbsp;|&nbsp;';
  117. echo '<a href="' . clean_url(remove_query_arg(array('action','ID'))) . '" title="' . __('Browse your files') . '">' . __('cancel') . '</a>';
  118. echo '&nbsp;]'; ?></span>
  119. </div>
  120. <div id="upload-file-view" class="alignleft">
  121. <?php if ( isset($attachment_data['width']) && 'inline' != $style )
  122. echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
  123. echo wp_upload_display( array(171, 128) );
  124. if ( isset($attachment_data['width']) && 'inline' != $style )
  125. echo '</a>'; ?>
  126. </div>
  127. <?php endif; ?>
  128. <table><col /><col class="widefat" />
  129. <?php if ( $id ): ?>
  130. <tr>
  131. <th scope="row"><label for="url"><?php _e('URL'); ?></label></th>
  132. <td><input type="text" id="url" class="readonly" value="<?php echo wp_get_attachment_url(); ?>" readonly="readonly" /></td>
  133. </tr>
  134. <?php else : ?>
  135. <tr>
  136. <th scope="row"><label for="upload"><?php _e('File'); ?></label></th>
  137. <td><input type="file" id="upload" name="image" /></td>
  138. </tr>
  139. <?php endif; ?>
  140. <tr>
  141. <th scope="row"><label for="post_title"><?php _e('Title'); ?></label></th>
  142. <td><input type="text" id="post_title" name="post_title" value="<?php echo $attachment->post_title; ?>" /></td>
  143. </tr>
  144. <tr>
  145. <th scope="row"><label for="post_content"><?php _e('Description'); ?></label></th>
  146. <td><textarea name="post_content" id="post_content"><?php echo $attachment->post_content; ?></textarea></td>
  147. </tr>
  148. <tr id="buttons" class="submit">
  149. <td colspan='2'>
  150. <?php if ( $id ) : ?>
  151. <input type="submit" name="delete" id="delete" class="delete alignleft" value="<?php _e('Delete File'); ?>" />
  152. <?php endif; ?>
  153. <input type="hidden" name="from_tab" value="<?php echo $tab; ?>" />
  154. <input type="hidden" name="action" value="<?php echo $id ? 'save' : 'upload'; ?>" />
  155. <?php if ( $post_id ) : ?>
  156. <input type="hidden" name="post_id" value="<?php echo $post_id; ?>" />
  157. <?php endif; if ( $id ) : ?>
  158. <input type="hidden" name="ID" value="<?php echo $id; ?>" />
  159. <?php endif; ?>
  160. <?php wp_nonce_field( 'inlineuploading' ); ?>
  161. <div class="submit">
  162. <input type="submit" value="<?php $id ? _e('Save') : _e('Upload'); ?> &raquo;" />
  163. </div>
  164. </td>
  165. </tr>
  166. </table>
  167. </form>
  168. <?php
  169. }
  170. function wp_upload_tab_upload() {
  171. wp_upload_form();
  172. }
  173. function wp_upload_tab_upload_action() {
  174. global $action;
  175. if ( isset($_POST['delete']) )
  176. $action = 'delete';
  177. switch ( $action ) :
  178. case 'upload' :
  179. global $from_tab, $post_id, $style;
  180. if ( !$from_tab )
  181. $from_tab = 'upload';
  182. check_admin_referer( 'inlineuploading' );
  183. global $post_id, $post_title, $post_content;
  184. if ( !current_user_can( 'upload_files' ) )
  185. wp_die( __('You are not allowed to upload files.')
  186. . " <a href='" . get_option('siteurl') . "/wp-admin/upload.php?style=$style&amp;tab=browse-all&amp;post_id=$post_id'>"
  187. . __('Browse Files') . '</a>'
  188. );
  189. $overrides = array('action'=>'upload');
  190. $file = wp_handle_upload($_FILES['image'], $overrides);
  191. if ( isset($file['error']) )
  192. wp_die($file['error'] . "<br /><a href='" . get_option('siteurl')
  193. . "/wp-admin/upload.php?style=$style&amp;tab=$from_tab&amp;post_id=$post_id'>" . __('Back to Image Uploading') . '</a>'
  194. );
  195. $url = $file['url'];
  196. $type = $file['type'];
  197. $file = $file['file'];
  198. $filename = basename($file);
  199. // Construct the attachment array
  200. $attachment = array(
  201. 'post_title' => $post_title ? $post_title : $filename,
  202. 'post_content' => $post_content,
  203. 'post_type' => 'attachment',
  204. 'post_parent' => $post_id,
  205. 'post_mime_type' => $type,
  206. 'guid' => $url
  207. );
  208. // Save the data
  209. $id = wp_insert_attachment($attachment, $file, $post_id);
  210. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  211. wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=browse&action=view&ID=$id&post_id=$post_id");
  212. die;
  213. break;
  214. case 'save' :
  215. global $from_tab, $post_id, $style;
  216. if ( !$from_tab )
  217. $from_tab = 'upload';
  218. check_admin_referer( 'inlineuploading' );
  219. wp_update_post($_POST);
  220. wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=$from_tab&post_id=$post_id");
  221. die;
  222. break;
  223. case 'delete' :
  224. global $ID, $post_id, $from_tab, $style;
  225. if ( !$from_tab )
  226. $from_tab = 'upload';
  227. check_admin_referer( 'inlineuploading' );
  228. if ( !current_user_can('edit_post', (int) $ID) )
  229. wp_die( __('You are not allowed to delete this attachment.')
  230. . " <a href='" . get_option('siteurl') . "/wp-admin/upload.php?style=$style&amp;tab=$from_tab&amp;post_id=$post_id'>"
  231. . __('Go back') . '</a>'
  232. );
  233. wp_delete_attachment($ID);
  234. wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=$from_tab&post_id=$post_id" );
  235. die;
  236. break;
  237. endswitch;
  238. }
  239. add_action( 'upload_files_upload', 'wp_upload_tab_upload_action' );
  240. function wp_upload_grab_attachments( $obj ) {
  241. $obj->is_attachment = true;
  242. }
  243. function wp_upload_posts_where( $where ) {
  244. global $post_id;
  245. return $where . " AND post_parent = '" . (int) $post_id . "'";
  246. }
  247. function wp_upload_tab_browse() {
  248. global $wpdb, $action, $paged;
  249. $old_vars = compact( 'paged' );
  250. switch ( $action ) :
  251. case 'edit' :
  252. case 'view' :
  253. global $ID;
  254. $attachments = query_posts("attachment_id=$ID");
  255. if ( have_posts() ) : while ( have_posts() ) : the_post();
  256. 'edit' == $action ? wp_upload_form() : wp_upload_view();
  257. endwhile; endif;
  258. break;
  259. default :
  260. global $tab, $post_id, $style;
  261. add_action( 'pre_get_posts', 'wp_upload_grab_attachments' );
  262. if ( 'browse' == $tab && $post_id )
  263. add_filter( 'posts_where', 'wp_upload_posts_where' );
  264. $attachments = query_posts("what_to_show=posts&posts_per_page=10&paged=$paged");
  265. $count_query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'attachment'";
  266. if ( $post_id )
  267. $count_query .= " AND post_parent = '$post_id'";
  268. $total = $wpdb->get_var($count_query);
  269. echo "<ul id='upload-files'>\n";
  270. if ( have_posts() ) : while ( have_posts() ) : the_post();
  271. $href = wp_specialchars( add_query_arg( array(
  272. 'action' => 'inline' == $style ? 'view' : 'edit',
  273. 'ID' => get_the_ID())
  274. ), 1 );
  275. echo "\t<li id='file-";
  276. the_ID();
  277. echo "' class='alignleft'>\n";
  278. echo wp_upload_display( array(128,128), $href );
  279. echo "\t</li>\n";
  280. endwhile;
  281. else :
  282. echo "\t<li>" . __('There are no attachments to show.') . "</li>\n";
  283. endif;
  284. echo "</ul>\n\n";
  285. echo "<form action='' id='browse-form'><input type='hidden' id='nonce-value' value='" . wp_create_nonce( 'inlineuploading' ) . "' /></form>\n";
  286. break;
  287. endswitch;
  288. extract($old_vars);
  289. }
  290. function wp_upload_tab_browse_action() {
  291. global $style;
  292. if ( 'inline' == $style )
  293. wp_enqueue_script('upload');
  294. }
  295. add_action( 'upload_files_browse', 'wp_upload_tab_browse_action' );
  296. add_action( 'upload_files_browse-all', 'wp_upload_tab_browse_action' );
  297. function wp_upload_admin_head() {
  298. global $wp_locale;
  299. echo "<link rel='stylesheet' href='" . get_option('siteurl') . '/wp-admin/upload.css?version=' . get_bloginfo('version') . "a' type='text/css' />\n";
  300. if ( 'rtl' == $wp_locale->text_direction )
  301. echo "<link rel='stylesheet' href='" . get_option('siteurl') . '/wp-admin/upload-rtl.css?version=' . get_bloginfo('version') . "a' type='text/css' />\n";
  302. if ( 'inline' == @$_GET['style'] ) {
  303. echo "<style type='text/css' media='screen'>\n";
  304. echo "\t#upload-menu { position: absolute; z-index: 2; }\n";
  305. echo "\tbody > #upload-menu { position: fixed; }\n";
  306. echo "\t#upload-content { top: 2em; }\n";
  307. echo "\t#upload-file { position: absolute; top: 15px; }\n";
  308. echo "</style>";
  309. }
  310. }