| 1 | | <?php |
| 2 | | |
| 3 | | function wp_upload_display( $dims = false, $href = '' ) { |
| 4 | | global $post; |
| 5 | | $id = get_the_ID(); |
| 6 | | $attachment_data = wp_get_attachment_metadata( $id ); |
| 7 | | $is_image = (int) wp_attachment_is_image(); |
| 8 | | $filesystem_path = get_attached_file( $id ); |
| 9 | | if ( !isset($attachment_data['width']) && $is_image ) { |
| 10 | | if ( $image_data = getimagesize( $filesystem_path ) ) { |
| 11 | | $attachment_data['width'] = $image_data[0]; |
| 12 | | $attachment_data['height'] = $image_data[1]; |
| 13 | | wp_update_attachment_metadata( $id, $attachment_data ); |
| 14 | | } |
| 15 | | } |
| 16 | | if ( isset($attachment_data['width']) ) |
| 17 | | list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128); |
| 18 | | // check for extended metadata from exif/iptc |
| 19 | | if ( !isset($attachment_data['image_meta']) && $is_image ) { |
| 20 | | $image_meta = wp_read_image_metadata( $filesystem_path ); |
| 21 | | $attachment_data['image_meta'] = $image_meta; |
| 22 | | wp_update_attachment_metadata( $id, $attachment_data ); |
| 23 | | } |
| 24 | | |
| 25 | | $post_title = attribute_escape( the_title( '', '', false ) ); |
| 26 | | $post_content = attribute_escape(apply_filters( 'content_edit_pre', $post->post_content )); |
| 27 | | |
| 28 | | $class = 'text'; |
| 29 | | $innerHTML = get_attachment_innerHTML( $id, false, $dims ); |
| 30 | | if ( $image_src = get_attachment_icon_src() ) { |
| 31 | | $image_rel = wp_make_link_relative($image_src); |
| 32 | | $innerHTML = ' ' . str_replace($image_src, $image_rel, $innerHTML); |
| 33 | | $class = 'image'; |
| 34 | | } |
| 35 | | |
| 36 | | $src_base = wp_get_attachment_url(); |
| 37 | | $src = wp_make_link_relative( $src_base ); |
| 38 | | $src_base = str_replace($src, '', $src_base); |
| 39 | | |
| 40 | | if ( !trim($post_title) ) |
| 41 | | $post_title = basename($src); |
| 42 | | |
| 43 | | $r = ''; |
| 44 | | |
| 45 | | if ( $href ) |
| 46 | | $r .= "<a id='file-link-$id' href='$href' title='$post_title' class='file-link $class'>\n"; |
| 47 | | if ( $href || $image_src ) |
| 48 | | $r .= "\t\t\t$innerHTML"; |
| 49 | | if ( $href ) |
| 50 | | $r .= "</a>\n"; |
| 51 | | $size = @filesize($filesystem_path); |
| 52 | | if ( !empty($size) ) |
| 53 | | $r .= "\t\t\t\t<span class='upload-file-size'>".size_format($size)."</span>\n"; |
| 54 | | $r .= "\n\t\t<div class='upload-file-data'>\n\t\t\t<p>\n"; |
| 55 | | $r .= "\t\t\t\t<input type='hidden' name='attachment-url-$id' id='attachment-url-$id' value='$src' />\n"; |
| 56 | | $r .= "\t\t\t\t<input type='hidden' name='attachment-url-base-$id' id='attachment-url-base-$id' value='$src_base' />\n"; |
| 57 | | |
| 58 | | if ( !$thumb_base = wp_get_attachment_thumb_url() ) |
| 59 | | $thumb_base = wp_mime_type_icon(); |
| 60 | | if ( $thumb_base ) { |
| 61 | | $thumb_rel = wp_make_link_relative( $thumb_base ); |
| 62 | | $thumb_base = str_replace( $thumb_rel, '', $thumb_base ); |
| 63 | | $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-$id' id='attachment-thumb-url-$id' value='$thumb_rel' />\n"; |
| 64 | | $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-base-$id' id='attachment-thumb-url-base-$id' value='$thumb_base' />\n"; |
| 65 | | } |
| 66 | | |
| 67 | | $r .= "\t\t\t\t<input type='hidden' name='attachment-is-image-$id' id='attachment-is-image-$id' value='$is_image' />\n"; |
| 68 | | |
| 69 | | if ( isset($width) ) { |
| 70 | | $r .= "\t\t\t\t<input type='hidden' name='attachment-width-$id' id='attachment-width-$id' value='$width' />\n"; |
| 71 | | $r .= "\t\t\t\t<input type='hidden' name='attachment-height-$id' id='attachment-height-$id' value='$height' />\n"; |
| 72 | | } |
| 73 | | $r .= "\t\t\t\t<input type='hidden' name='attachment-page-url-$id' id='attachment-page-url-$id' value='" . get_attachment_link( $id ) . "' />\n"; |
| 74 | | $r .= "\t\t\t\t<input type='hidden' name='attachment-title-$id' id='attachment-title-$id' value='$post_title' />\n"; |
| 75 | | $r .= "\t\t\t\t<input type='hidden' name='attachment-description-$id' id='attachment-description-$id' value='$post_content' />\n"; |
| 76 | | $r .= "\t\t\t</p>\n\t\t</div>\n"; |
| 77 | | return $r; |
| 78 | | } |
| 79 | | |
| 80 | | function wp_upload_view() { |
| 81 | | global $style; |
| 82 | | $id = get_the_ID(); |
| 83 | | $attachment_data = wp_get_attachment_metadata( $id ); |
| 84 | | ?> |
| 85 | | <div id="upload-file"> |
| 86 | | <div id="file-title"> |
| 87 | | <h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style ) |
| 88 | | echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>"; |
| 89 | | the_title(); |
| 90 | | if ( !isset($attachment_data['width']) && 'inline' != $style ) |
| 91 | | echo '</a>'; |
| 92 | | ?></h2> |
| 93 | | <span><?php |
| 94 | | echo '[ '; |
| 95 | | echo '<a href="' . get_permalink() . '">' . __('view') . '</a>'; |
| 96 | | echo ' | '; |
| 97 | | echo '<a href="' . clean_url(add_query_arg('action', 'edit')) . '" title="' . __('Edit this file') . '">' . __('edit') . '</a>'; |
| 98 | | echo ' | '; |
| 99 | | echo '<a href="' . clean_url(remove_query_arg(array('action', 'ID'))) . '" title="' . __('Browse your files') . '">' . __('cancel') . '</a>'; |
| 100 | | echo ' ]'; ?></span> |
| 101 | | </div> |
| 102 | | |
| 103 | | <div id="upload-file-view" class="alignleft"> |
| 104 | | <?php if ( isset($attachment_data['width']) && 'inline' != $style ) |
| 105 | | echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>"; |
| 106 | | echo wp_upload_display( array(171, 128) ); |
| 107 | | if ( isset($attachment_data['width']) && 'inline' != $style ) |
| 108 | | echo '</a>'; ?> |
| 109 | | </div> |
| 110 | | <?php the_attachment_links( $id ); ?> |
| 111 | | </div> |
| 112 | | <?php echo "<form action='' id='browse-form'><input type='hidden' id='nonce-value' value='" . wp_create_nonce( 'inlineuploading' ) . "' /></form>\n"; |
| 113 | | } |
| 114 | | |
| 115 | | function wp_upload_form() { |
| 116 | | $id = get_the_ID(); |
| 117 | | global $post_id, $tab, $style; |
| 118 | | $enctype = $id ? '' : ' enctype="multipart/form-data"'; |
| 119 | | $post_id = (int) $post_id; |
| 120 | | ?> |
| 121 | | <form<?php echo $enctype; ?> id="upload-file" method="post" action="<?php echo get_option('siteurl') . '/wp-admin/upload.php?style=' . attribute_escape($style . '&tab=upload&post_id=' . $post_id); ?>"> |
| 122 | | <?php |
| 123 | | if ( $id ) : |
| 124 | | $attachment = get_post_to_edit( $id ); |
| 125 | | $attachment_data = wp_get_attachment_metadata( $id ); |
| 126 | | ?> |
| 127 | | <div id="file-title"> |
| 128 | | <h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style ) |
| 129 | | echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>"; |
| 130 | | the_title(); |
| 131 | | if ( !isset($attachment_data['width']) && 'inline' != $style ) |
| 132 | | echo '</a>'; |
| 133 | | ?></h2> |
| 134 | | <span><?php |
| 135 | | echo '[ '; |
| 136 | | echo '<a href="' . get_permalink() . '">' . __('view') . '</a>'; |
| 137 | | echo ' | '; |
| 138 | | echo '<a href="' . clean_url(add_query_arg('action', 'view')) . '">' . __('links') . '</a>'; |
| 139 | | echo ' | '; |
| 140 | | echo '<a href="' . clean_url(remove_query_arg(array('action','ID'))) . '" title="' . __('Browse your files') . '">' . __('cancel') . '</a>'; |
| 141 | | echo ' ]'; ?></span> |
| 142 | | </div> |
| 143 | | |
| 144 | | <div id="upload-file-view" class="alignleft"> |
| 145 | | <?php if ( isset($attachment_data['width']) && 'inline' != $style ) |
| 146 | | echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>"; |
| 147 | | echo wp_upload_display( array(171, 128) ); |
| 148 | | if ( isset($attachment_data['width']) && 'inline' != $style ) |
| 149 | | echo '</a>'; ?> |
| 150 | | </div> |
| 151 | | <?php endif; ?> |
| 152 | | <table><col /><col class="widefat" /> |
| 153 | | <?php if ( $id ): ?> |
| 154 | | <tr> |
| 155 | | <th scope="row"><label for="url"><?php _e('URL'); ?></label></th> |
| 156 | | <td><input type="text" id="url" class="readonly" value="<?php echo wp_get_attachment_url(); ?>" readonly="readonly" /></td> |
| 157 | | </tr> |
| 158 | | <?php else : ?> |
| 159 | | <tr> |
| 160 | | <th scope="row"><label for="upload"><?php _e('File'); ?></label></th> |
| 161 | | <td><input type="file" id="upload" name="image" /></td> |
| 162 | | </tr> |
| 163 | | <?php endif; ?> |
| 164 | | <tr> |
| 165 | | <th scope="row"><label for="post_title"><?php _e('Title'); ?></label></th> |
| 166 | | <td><input type="text" id="post_title" name="post_title" value="<?php echo $attachment->post_title; ?>" /></td> |
| 167 | | </tr> |
| 168 | | <tr> |
| 169 | | <th scope="row"><label for="post_content"><?php _e('Description'); ?></label></th> |
| 170 | | <td><textarea name="post_content" id="post_content"><?php echo $attachment->post_content; ?></textarea></td> |
| 171 | | </tr> |
| 172 | | <?php if (isset($attachment_data['image_meta'])) { ?> |
| 173 | | <tr> |
| 174 | | <th scope="row"><label for="url"><?php _e('Aperture'); ?></label></th> |
| 175 | | <td>f/<?php echo $attachment_data['image_meta']['aperture']; ?></td> |
| 176 | | </tr> |
| 177 | | <tr> |
| 178 | | <th scope="row"><label for="url"><?php _e('Credit'); ?></label></th> |
| 179 | | <td><?php echo $attachment_data['image_meta']['credit']; ?></td> |
| 180 | | </tr> |
| 181 | | <tr> |
| 182 | | <th scope="row"><label for="url"><?php _e('Camera'); ?></label></th> |
| 183 | | <td><?php echo $attachment_data['image_meta']['camera']; ?></td> |
| 184 | | </tr> |
| 185 | | <tr> |
| 186 | | <th scope="row"><label for="url"><?php _e('Created'); ?></label></th> |
| 187 | | <td><?php echo date_i18n(get_option('date_format').' '.get_option('time_format'), $attachment_data['image_meta']['created_timestamp']); ?></td> |
| 188 | | </tr> |
| 189 | | <tr> |
| 190 | | <th scope="row"><label for="url"><?php _e('Copyright'); ?></label></th> |
| 191 | | <td><?php echo $attachment_data['image_meta']['copyright']; ?></td> |
| 192 | | </tr> |
| 193 | | <tr> |
| 194 | | <th scope="row"><label for="url"><?php _e('Focal Length'); ?></label></th> |
| 195 | | <td><?php echo $attachment_data['image_meta']['focal_length']; ?>mm</td> |
| 196 | | </tr> |
| 197 | | <tr> |
| 198 | | <th scope="row"><label for="url"><?php _e('ISO'); ?></label></th> |
| 199 | | <td><?php echo $attachment_data['image_meta']['iso']; ?></td> |
| 200 | | </tr> |
| 201 | | <tr> |
| 202 | | <th scope="row"><label for="url"><?php _e('Shutter Speed'); ?></label></th> |
| 203 | | <td><?php $secs = $attachment_data['image_meta']['shutter_speed']; |
| 204 | | echo ($secs > 0.0 and $secs < 1.0) ? ("1/" . round(1/$secs)) : ($secs); ?>s</td> |
| 205 | | </tr> |
| 206 | | <tr> |
| 207 | | <th scope="row"><label for="url"><?php _e('Title'); ?></label></th> |
| 208 | | <td><?php echo $attachment_data['image_meta']['title']; ?></td> |
| 209 | | </tr> |
| 210 | | <?php } ?> |
| 211 | | <tr id="buttons" class="submit"> |
| 212 | | <td colspan='2'> |
| 213 | | <?php if ( $id ) : ?> |
| 214 | | <input type="submit" name="delete" id="delete" class="delete alignleft" value="<?php _e('Delete File'); ?>" /> |
| 215 | | <?php endif; ?> |
| 216 | | <input type="hidden" name="from_tab" value="<?php echo $tab; ?>" /> |
| 217 | | <input type="hidden" name="action" value="<?php echo $id ? 'save' : 'upload'; ?>" /> |
| 218 | | <?php if ( $post_id ) : ?> |
| 219 | | <input type="hidden" name="post_id" value="<?php echo $post_id; ?>" /> |
| 220 | | <?php endif; if ( $id ) : ?> |
| 221 | | <input type="hidden" name="ID" value="<?php echo $id; ?>" /> |
| 222 | | <?php endif; ?> |
| 223 | | <?php wp_nonce_field( 'inlineuploading' ); ?> |
| 224 | | <div class="submit"> |
| 225 | | <input type="submit" value="<?php $id ? _e('Save') : _e('Upload'); ?>" /> |
| 226 | | </div> |
| 227 | | </td> |
| 228 | | </tr> |
| 229 | | </table> |
| 230 | | </form> |
| 231 | | <?php |
| 232 | | } |
| 233 | | |
| 234 | | function wp_upload_tab_upload() { |
| 235 | | wp_upload_form(); |
| 236 | | } |
| 237 | | |
| 238 | | function wp_upload_tab_upload_action() { |
| 239 | | global $action; |
| 240 | | if ( isset($_POST['delete']) ) |
| 241 | | $action = 'delete'; |
| 242 | | |
| 243 | | switch ( $action ) : |
| 244 | | case 'upload' : |
| 245 | | global $from_tab, $post_id, $style; |
| 246 | | if ( !$from_tab ) |
| 247 | | $from_tab = 'upload'; |
| 248 | | |
| 249 | | check_admin_referer( 'inlineuploading' ); |
| 250 | | |
| 251 | | global $post_id, $post_title, $post_content; |
| 252 | | |
| 253 | | if ( !current_user_can( 'upload_files' ) ) |
| 254 | | wp_die( __('You are not allowed to upload files.') |
| 255 | | . " <a href='" . get_option('siteurl') . "/wp-admin/upload.php?style=" . attribute_escape($style . "&tab=browse-all&post_id=$post_id") . "'>" |
| 256 | | . __('Browse Files') . '</a>' |
| 257 | | ); |
| 258 | | |
| 259 | | $overrides = array('action'=>'upload'); |
| 260 | | |
| 261 | | $file = wp_handle_upload($_FILES['image'], $overrides); |
| 262 | | |
| 263 | | if ( isset($file['error']) ) |
| 264 | | wp_die($file['error'] . "<br /><a href='" . get_option('siteurl') |
| 265 | | . "/wp-admin/upload.php?style=" . attribute_escape($style . "&tab=$from_tab&post_id=$post_id") . "'>" . __('Back to Image Uploading') . '</a>' |
| 266 | | ); |
| 267 | | |
| 268 | | $url = $file['url']; |
| 269 | | $type = $file['type']; |
| 270 | | $file = $file['file']; |
| 271 | | |
| 272 | | // Construct the attachment array |
| 273 | | $attachment = array( |
| 274 | | 'post_title' => $post_title, |
| 275 | | 'post_content' => $post_content, |
| 276 | | 'post_type' => 'attachment', |
| 277 | | 'post_parent' => $post_id, |
| 278 | | 'post_mime_type' => $type, |
| 279 | | 'guid' => $url |
| 280 | | ); |
| 281 | | |
| 282 | | // Save the data |
| 283 | | $id = wp_insert_attachment($attachment, $file, $post_id); |
| 284 | | |
| 285 | | wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); |
| 286 | | |
| 287 | | wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=browse&action=view&ID=$id&post_id=$post_id"); |
| 288 | | die; |
| 289 | | break; |
| 290 | | |
| 291 | | case 'save' : |
| 292 | | global $from_tab, $post_id, $style; |
| 293 | | if ( !$from_tab ) |
| 294 | | $from_tab = 'upload'; |
| 295 | | check_admin_referer( 'inlineuploading' ); |
| 296 | | |
| 297 | | wp_update_post($_POST); |
| 298 | | wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=$from_tab&post_id=$post_id"); |
| 299 | | die; |
| 300 | | break; |
| 301 | | |
| 302 | | case 'delete' : |
| 303 | | global $ID, $post_id, $from_tab, $style; |
| 304 | | if ( !$from_tab ) |
| 305 | | $from_tab = 'upload'; |
| 306 | | |
| 307 | | check_admin_referer( 'inlineuploading' ); |
| 308 | | |
| 309 | | if ( !current_user_can('edit_post', (int) $ID) ) |
| 310 | | wp_die( __('You are not allowed to delete this attachment.') |
| 311 | | . " <a href='" . get_option('siteurl') . "/wp-admin/upload.php?style=" . attribute_escape($style . "&tab=$from_tab&post_id=$post_id") . "'>" |
| 312 | | . __('Go back') . '</a>' |
| 313 | | ); |
| 314 | | |
| 315 | | wp_delete_attachment($ID); |
| 316 | | |
| 317 | | wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=$from_tab&post_id=$post_id" ); |
| 318 | | die; |
| 319 | | break; |
| 320 | | |
| 321 | | endswitch; |
| 322 | | } |
| 323 | | |
| 324 | | add_action( 'upload_files_upload', 'wp_upload_tab_upload_action' ); |
| 325 | | |
| 326 | | function wp_upload_grab_attachments( $obj ) { |
| 327 | | $obj->is_attachment = true; |
| 328 | | } |
| 329 | | |
| 330 | | function wp_upload_posts_where( $where ) { |
| 331 | | global $post_id; |
| 332 | | return $where . " AND post_parent = '" . (int) $post_id . "'"; |
| 333 | | } |
| 334 | | |
| 335 | | function wp_upload_tab_browse() { |
| 336 | | global $action, $paged; |
| 337 | | $old_vars = compact( 'paged' ); |
| 338 | | |
| 339 | | switch ( $action ) : |
| 340 | | case 'edit' : |
| 341 | | case 'view' : |
| 342 | | global $ID; |
| 343 | | $attachments = query_posts("attachment_id=$ID"); |
| 344 | | if ( have_posts() ) : while ( have_posts() ) : the_post(); |
| 345 | | 'edit' == $action ? wp_upload_form() : wp_upload_view(); |
| 346 | | endwhile; endif; |
| 347 | | break; |
| 348 | | default : |
| 349 | | global $tab, $post_id, $style; |
| 350 | | add_action( 'pre_get_posts', 'wp_upload_grab_attachments' ); |
| 351 | | if ( 'browse' == $tab && $post_id ) |
| 352 | | add_filter( 'posts_where', 'wp_upload_posts_where' ); |
| 353 | | $attachments = query_posts("what_to_show=posts&post_status=any&posts_per_page=10&paged=$paged"); |
| 354 | | |
| 355 | | echo "<ul id='upload-files'>\n"; |
| 356 | | if ( have_posts() ) : while ( have_posts() ) : the_post(); |
| 357 | | $href = wp_specialchars( add_query_arg( array( |
| 358 | | 'action' => 'inline' == $style ? 'view' : 'edit', |
| 359 | | 'ID' => get_the_ID()) |
| 360 | | ), 1 ); |
| 361 | | |
| 362 | | echo "\t<li id='file-"; |
| 363 | | the_ID(); |
| 364 | | echo "' class='alignleft'>\n"; |
| 365 | | echo wp_upload_display( array(128,128), $href ); |
| 366 | | echo "\t</li>\n"; |
| 367 | | endwhile; |
| 368 | | else : |
| 369 | | echo "\t<li>" . __('There are no attachments to show.') . "</li>\n"; |
| 370 | | endif; |
| 371 | | echo "</ul>\n\n"; |
| 372 | | |
| 373 | | echo "<form action='' id='browse-form'><input type='hidden' id='nonce-value' value='" . wp_create_nonce( 'inlineuploading' ) . "' /></form>\n"; |
| 374 | | break; |
| 375 | | endswitch; |
| 376 | | |
| 377 | | extract($old_vars); |
| 378 | | } |
| 379 | | |
| 380 | | |
| 381 | | function wp_upload_tab_browse_action() { |
| 382 | | global $style; |
| 383 | | if ( 'inline' == $style ) |
| 384 | | wp_enqueue_script('upload'); |
| 385 | | } |
| 386 | | |
| 387 | | add_action( 'upload_files_browse', 'wp_upload_tab_browse_action' ); |
| 388 | | add_action( 'upload_files_browse-all', 'wp_upload_tab_browse_action' ); |
| 389 | | |
| 390 | | function wp_upload_admin_head() { |
| 391 | | wp_admin_css( 'css/upload' ); |
| 392 | | if ( 'inline' == @$_GET['style'] ) { |
| 393 | | echo "<style type='text/css' media='screen'>\n"; |
| 394 | | echo "\t#upload-menu { position: absolute; z-index: 2; }\n"; |
| 395 | | echo "\tbody > #upload-menu { position: fixed; }\n"; |
| 396 | | echo "\t#upload-content { top: 2em; }\n"; |
| 397 | | echo "\t#upload-file { position: absolute; top: 15px; }\n"; |
| 398 | | echo "</style>"; |
| 399 | | } |
| 400 | | } |
| 401 | | |
| 402 | | ?> |