Make WordPress Core


Ignore:
Timestamp:
09/16/2008 04:24:28 AM (17 years ago)
Author:
azaozz
Message:

Reattaching of orphan attachments and choose post/page ajaxed search box

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/upload.php

    r8894 r8901  
    1111add_thickbox();
    1212wp_enqueue_script( 'media-upload' );
     13wp_enqueue_script( 'wp-ajax-response' );
     14wp_enqueue_script( 'jquery-ui-draggable' );
     15wp_enqueue_script( 'jquery-ui-resizable' );
    1316
    1417if (!current_user_can('upload_files'))
    1518    wp_die(__('You do not have permission to upload files.'));
    1619
    17 // Handle bulk deletes
    18 if ( isset($_GET['action']) && isset($_GET['media']) ) {
     20if ( isset($_GET['find_detached'] ) ) {
     21    check_admin_referer('bulk-media');
     22
     23    if ( ! current_user_can('edit_posts') )
     24        wp_die( __('You are not allowed to scan for lost attachments.') );
     25
     26    $all_posts = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'post' OR post_type = 'page'");
     27    $all_att = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'attachment'");
     28
     29    $lost = array();
     30    foreach ( (array) $all_att as $att ) {
     31        if ( $att->post_parent > 0 && ! in_array($att->post_parent, $all_posts) )
     32            $lost[] = $att->ID;
     33    }
     34    $_GET['detached'] = 1;
     35
     36} elseif ( isset($_GET['found_post_id']) && isset($_GET['media']) ) {
     37    check_admin_referer('bulk-media');
     38
     39    if ( ! ( $parent_id = (int) $_GET['found_post_id'] ) )
     40        return;
     41
     42    $parent = &get_post($parent_id);
     43    if ( !current_user_can('edit_post', $parent_id) )
     44        wp_die( __('You are not allowed to edit this post.') );
     45
     46    $attach = array();
     47    foreach( (array) $_GET['media'] as $att_id ) {
     48        $att_id = (int) $att_id;
     49
     50        if ( !current_user_can('edit_post', $att_id) )
     51            continue;
     52
     53        $attach[] = $att_id;
     54    }
     55
     56    if ( ! empty($attach) ) {
     57        $attach = implode(',', $attach);
     58        $attached = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ($attach)", $parent_id) );
     59
     60        $message = sprintf( __ngettext('Added %1$s attachment to <strong>%2$s</strong>', 'Added %1$s attachments to <strong>%2$s</strong>', $attached, apply_filters( "the_title", $parent->post_title ) ) , $attached, apply_filters( "the_title", $parent->post_title ) );
     61    }
     62
     63    $_GET['detached'] = 1;
     64
     65} elseif ( isset($_GET['action']) && isset($_GET['media']) ) {
    1966    check_admin_referer('bulk-media');
    2067    if ( $_GET['action'] == 'delete' ) {
     
    5198wp_enqueue_script('media');
    5299
    53 list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
     100if ( ! isset( $_GET['paged'] ) || $_GET['paged'] < 1 )
     101    $_GET['paged'] = 1;
     102
     103if ( isset($_GET['detached']) ) {
     104
     105    if ( isset($lost) ) {
     106        $start = ( $_GET['paged'] - 1 ) * 50;
     107        $page_links_total = ceil(count($lost) / 50);
     108        $lost = implode(',', $lost);
     109
     110        $orphans = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type = 'attachment' AND ID IN ($lost) LIMIT $start, 50" );
     111    } else {
     112        $start = ( $_GET['paged'] - 1 ) * 25;
     113        $orphans = $wpdb->get_results( "SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent < 1 LIMIT $start, 25" );
     114        $page_links_total = ceil($wpdb->get_var( "SELECT FOUND_ROWS()" ) / 25);
     115    }
     116
     117    $post_mime_types = array(
     118                'image' => array(__('Images'), __('Manage Images'), __ngettext_noop('Image (%s)', 'Images (%s)')),
     119                'audio' => array(__('Audio'), __('Manage Audio'), __ngettext_noop('Audio (%s)', 'Audio (%s)')),
     120                'video' => array(__('Video'), __('Manage Video'), __ngettext_noop('Video (%s)', 'Video (%s)')),
     121            );
     122    $post_mime_types = apply_filters('post_mime_types', $post_mime_types);
     123
     124    $avail_post_mime_types = get_available_post_mime_types('attachment');
     125
     126    if ( isset($_GET['post_mime_type']) && !array_intersect( (array) $_GET['post_mime_type'], array_keys($post_mime_types) ) )
     127        unset($_GET['post_mime_type']);
     128
     129} else {
     130    list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
     131}
    54132
    55133if ( is_singular() ) {
     
    58136}
    59137
    60 require_once('admin-header.php');
    61 
    62 if ( !isset( $_GET['paged'] ) )
    63     $_GET['paged'] = 1;
    64 
    65 ?>
     138require_once('admin-header.php'); ?>
    66139
    67140<form class="search-form" action="" method="get">
     
    90163
    91164<h2><?php
    92 if ( is_singular() ) {
    93     printf(__('Comments on %s'), apply_filters( "the_title", $post->post_title));
     165if ( isset($_GET['detached']) ) {
     166    _e('Unattached Media');
     167} elseif ( is_singular() ) {
     168    printf( __('Comments on %s'), apply_filters("the_title", $post->post_title) );
    94169} else {
    95170    $post_mime_type_label = _c('Media|manage media header');
     
    97172        $post_mime_type_label = $post_mime_types[$_GET['post_mime_type']][1];
    98173    //TODO: Unreachable code: $post_listing_pageable is undefined, Similar code in edit.php
    99     //if ( $post_listing_pageable && !is_archive() && !is_search() ) 
     174    //if ( $post_listing_pageable && !is_archive() && !is_search() )
    100175    //  $h2_noun = is_paged() ? sprintf(__( 'Previous %s' ), $post_mime_type_label) : sprintf(__('Latest %s'), $post_mime_type_label);
    101176    //else
     
    128203    foreach ( $reals as $real )
    129204        $num_posts[$type] += $_num_posts[$real];
    130 $class = empty($_GET['post_mime_type']) ? ' class="current"' : '';
     205
     206$class = empty($_GET['post_mime_type']) && ! isset($_GET['detached']) ? ' class="current"' : '';
    131207$type_links[] = "<li><a href=\"upload.php\"$class>".__('All Types')."</a>";
    132208foreach ( $post_mime_types as $mime_type => $label ) {
     
    142218    sprintf(__ngettext($label[2][0], $label[2][1], $num_posts[$mime_type]), number_format_i18n( $num_posts[$mime_type] )) . '</a>';
    143219}
     220$class = isset($_GET['detached']) ? ' class="current"' : '';
     221$type_links[] = '<li><a href="upload.php?detached=1"' . $class . '>' . __('Unattached') . '</a>';
     222
    144223echo implode(' | </li>', $type_links) . '</li>';
    145224unset($type_links);
     
    156235$messages[2] = __('Media deleted.');
    157236
    158 if (isset($_GET['message'])) : ?>
    159 <div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
     237if ( isset($_GET['message']) && (int) $_GET['message'] )
     238    $message = $messages[$_GET['message']];
     239
     240if ( $message ) { ?>
     241<div id="message" class="updated fade"><p><?php echo $message; ?></p></div>
    160242<?php $_SERVER['REQUEST_URI'] = remove_query_arg(array('message'), $_SERVER['REQUEST_URI']);
    161 endif;
     243}
    162244?>
    163245
     
    167249
    168250<?php
     251if ( ! isset($page_links_total) )
     252    $page_links_total =  $wp_query->max_num_pages;
     253
    169254$page_links = paginate_links( array(
    170255    'base' => add_query_arg( 'paged', '%#%' ),
    171256    'format' => '',
    172     'total' => $wp_query->max_num_pages,
     257    'total' => $page_links_total,
    173258    'current' => $_GET['paged']
    174259));
     
    179264
    180265<div class="alignleft">
    181 <select name="action">
     266<select name="action" id="select-action">
    182267<option value="" selected><?php _e('Actions'); ?></option>
    183268<option value="delete"><?php _e('Delete'); ?></option>
     269<?php if ( isset($orphans) ) { ?>
     270<option value="attach"><?php _e('Attach to a post'); ?></option>
     271<?php } ?>
    184272</select>
    185 <input type="submit" value="<?php _e('Apply'); ?>" name="doaction" class="button-secondary action" />
     273<input type="submit" id="submit" value="<?php _e('Apply'); ?>" name="doaction" class="button-secondary action" />
    186274<?php wp_nonce_field('bulk-media'); ?>
    187275<?php
    188276
    189 if ( !is_singular() ) :
     277if ( ! is_singular() && ! isset($_GET['detached']) ) {
    190278    $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";
    191279
     
    218306<input type="submit" id="post-query-submit" value="<?php _e('Filter'); ?>" class="button-secondary" />
    219307
    220 <?php endif; // is_singular ?>
     308<?php } // ! is_singular ?>
     309
     310<?php if ( isset($_GET['detached']) ) { ?>
     311    <input type="submit" id="find_detached" name="find_detached" value="<?php _e('Scan for lost attachments'); ?>" class="button-secondary" />
     312<?php } ?>
    221313
    222314</div>
     
    226318
    227319<br class="clear" />
    228 
    229 <?php include( 'edit-attachment-rows.php' ); ?>
    230 
    231320<?php wp_nonce_field( 'hiddencolumns', 'hiddencolumnsnonce', false ); ?>
     321
     322<?php if ( isset($orphans) ) { ?>
     323<table class="widefat">
     324<thead>
     325<tr>
     326    <th scope="col" class="check-column"><input type="checkbox" /></th>
     327    <th scope="col"></th>
     328    <th scope="col"><?php echo _c('Media|media column header'); ?></th>
     329    <th scope="col"><?php echo _c('Date Added|media column header'); ?></th>
     330</tr>
     331</thead>
     332
     333<tbody id="the-list" class="list:post">
     334<?php
     335    if ( $orphans ) {
     336        foreach ( $orphans as $post ) {
     337            $class = 'alternate' == $class ? '' : 'alternate';
     338            $att_title = empty($post->post_title) ? __('(no title)') : wp_specialchars( apply_filters('the_title', $post->post_title) );
     339?>
     340    <tr id='post-<?php echo $post->ID; ?>' class='<?php echo $class; ?>' valign="top">
     341        <th scope="row" class="check-column"><input type="checkbox" name="media[]" value="<?php echo $post->ID; ?>" /></th>
     342
     343        <td class="media-icon"><?php
     344        if ( $thumb = wp_get_attachment_image( $post->ID, array(80, 60), true ) ) { ?>
     345            <a href="media.php?action=edit&amp;attachment_id=<?php echo $post->ID; ?>" title="<?php echo attribute_escape(sprintf(__('Edit "%s"'), $att_title)); ?>"><?php echo $thumb; ?></a>
     346<?php   } ?></td>
     347
     348        <td><strong><a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php echo attribute_escape(sprintf(__('Edit "%s"'), $att_title)); ?>"><?php echo $att_title; ?></a></strong><br />
     349        <?php echo strtoupper(preg_replace('/^.*?\.(\w+)$/', '$1', get_attached_file($post->ID))); ?>
     350
     351        <p>
     352        <?php
     353        $actions = array();
     354        $actions['edit'] = '<a href="' . get_edit_post_link($post->ID, true) . '">' . __('Edit') . '</a>';
     355        $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url("post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID) . "' onclick=\"if ( confirm('" . js_escape(sprintf( ('draft' == $post->post_status) ? __("You are about to delete this attachment '%s'\n  'Cancel' to stop, 'OK' to delete.") : __("You are about to delete this attachment '%s'\n  'Cancel' to stop, 'OK' to delete."), $post->post_title )) . "') ) { return true;}return false;\">" . __('Delete') . "</a>";
     356        $actions['view'] = '<a href="' . get_permalink($post->ID) . '" title="' . attribute_escape(sprintf(__('View "%s"'), $title)) . '" rel="permalink">' . __('View') . '</a>';
     357        $actions['attach'] = '<a href="#the-list" onclick="findPosts.open(\'media[]\',\''.$post->ID.'\');return false;">'.__('Attach').'</a>';
     358        $action_count = count($actions);
     359        $i = 0;
     360        foreach ( $actions as $action => $link ) {
     361            ++$i;
     362            ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
     363            echo "<span class='$action'>$link$sep</span>";
     364        } ?>
     365        </p></td>
     366
     367<?php   if ( '0000-00-00 00:00:00' == $post->post_date && 'date' == $column_name ) {
     368            $t_time = $h_time = __('Unpublished');
     369        } else {
     370            $t_time = get_the_time(__('Y/m/d g:i:s A'));
     371            $m_time = $post->post_date;
     372            $time = get_post_time( 'G', true );
     373            if ( ( abs($t_diff = time() - $time) ) < 86400 ) {
     374                if ( $t_diff < 0 )
     375                    $h_time = sprintf( __('%s from now'), human_time_diff( $time ) );
     376                else
     377                    $h_time = sprintf( __('%s ago'), human_time_diff( $time ) );
     378            } else {
     379                $h_time = mysql2date(__('Y/m/d'), $m_time);
     380            }
     381        } ?>
     382        <td><?php echo $h_time ?></td>
     383    </tr>
     384<?php   }
     385
     386    } else { ?>
     387    <tr><td colspan="5"><?php _e('No posts found.') ?></td></tr>
     388<?php } ?>
     389</tbody>
     390</table>
     391<script type="text/javascript">
     392    (function($){
     393        $('#submit').click(function(e) {
     394            if ( 'attach' == $('#select-action').val() ) {
     395                e.preventDefault();
     396                findPosts.open();
     397            }
     398        });
     399    })(jQuery);
     400</script>
     401<?php find_posts_div();
     402
     403} else {
     404    include( 'edit-attachment-rows.php' );
     405} ?>
    232406
    233407</form>
     
    285459</div>
    286460
    287 <?php 
     461<?php
    288462
    289463include('admin-footer.php');
Note: See TracChangeset for help on using the changeset viewer.