First cut at Manage->Tags. Props jhodgdon. see #5684

git-svn-id: http://svn.automattic.com/wordpress/trunk@6660 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-01-25 19:29:01 +00:00
parent f529123061
commit 879f7f4766
9 changed files with 318 additions and 5 deletions

View File

@ -62,6 +62,15 @@ case 'delete-cat' :
die('1');
else die('0');
break;
case 'delete-tag' :
check_ajax_referer( "delete-tag_$id" );
if ( !current_user_can( 'manage_categories' ) )
die('-1');
if ( wp_delete_term($id, 'post_tag'))
die('1');
else die('0');
break;
case 'delete-link-cat' :
check_ajax_referer( "delete-link-category_$id" );
if ( !current_user_can( 'manage_categories' ) )
@ -302,6 +311,43 @@ case 'add-link-cat' : // From Blogroll -> Categories
) );
$x->send();
break;
case 'add-tag' : // From Manage->Tags
check_ajax_referer( 'add-tag' );
if ( !current_user_can( 'manage_categories' ) )
die('-1');
if ( '' === trim($_POST['name']) ) {
$x = new WP_Ajax_Response( array(
'what' => 'tag',
'id' => new WP_Error( 'name', __('You did not enter a tag name.') )
) );
$x->send();
}
$tag = wp_insert_term($_POST['name'], 'post_tag', $_POST );
if ( is_wp_error($tag) ) {
$x = new WP_Ajax_Response( array(
'what' => 'tag',
'id' => $tag
) );
$x->send();
}
if ( !$tag || (!$tag = get_term( $tag['term_id'], 'post_tag' )) )
die('0');
$tag_full_name = $tag->name;
$tag_full_name = attribute_escape($tag_full_name);
$x = new WP_Ajax_Response( array(
'what' => 'tag',
'id' => $tag->term_id,
'data' => _tag_row( $tag ),
'supplemental' => array('name' => $tag_full_name, 'show-link' => sprintf(__( 'Tag <a href="#%s">%s</a> added' ), "tag-$tag->term_id", $tag_full_name))
) );
$x->send();
break;
case 'add-comment' :
check_ajax_referer( $action );
if ( !current_user_can( 'edit_post', $id ) )

View File

@ -0,0 +1,39 @@
<?php
if ( ! empty($tag_ID) ) {
$heading = __('Edit Tag');
$submit_text = __('Edit Tag &raquo;');
$form = '<form name="edittag" id="edittag" method="post" action="edit-tags.php">';
$action = 'editedtag';
$nonce_action = 'update-tag_' . $tag_ID;
do_action('edit_tag_form_pre', $tag);
} else {
$heading = __('Add Tag');
$submit_text = __('Add Tag &raquo;');
$form = '<form name="addtag" id="addtag" method="post" action="edit-tags.php" class="add:the-list:">';
$action = 'addtag';
$nonce_action = 'add-tag';
do_action('add_tag_form_pre', $tag);
}
?>
<div class="wrap">
<h2><?php echo $heading ?></h2>
<div id="ajax-response"></div>
<?php echo $form ?>
<input type="hidden" name="action" value="<?php echo $action ?>" />
<input type="hidden" name="tag_ID" value="<?php echo $tag->term_id ?>" />
<?php wp_nonce_field($nonce_action); ?>
<table class="editform" width="100%" cellspacing="2" cellpadding="5">
<tr class="form-field form-required">
<th width="33%" scope="row" valign="top"><label for="name"><?php _e('Tag name:') ?></label></th>
<td width="67%"><input name="name" id="name" type="text" value="<?php echo attribute_escape($tag->name); ?>" size="40" /></td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="slug"><?php _e('Tag slug:') ?></label></th>
<td><input name="slug" id="slug" type="text" value="<?php echo attribute_escape($tag->slug); ?>" size="40" /></td>
</tr>
</table>
<p class="submit"><input type="submit" name="submit" value="<?php echo $submit_text ?>" /></p>
<?php do_action('edit_tag_form', $tag); ?>
</form>
</div>

140
wp-admin/edit-tags.php Normal file
View File

@ -0,0 +1,140 @@
<?php
require_once('admin.php');
$title = __('Tags');
$parent_file = 'edit.php';
wp_reset_vars(array('action', 'tag'));
switch($action) {
case 'addtag':
check_admin_referer('add-tag');
if ( !current_user_can('manage_categories') )
wp_die(__('Cheatin&#8217; uh?'));
$ret = wp_insert_term($_POST['name'], 'post_tag', $_POST);
if ( $ret && !is_wp_error( $ret ) ) {
wp_redirect('edit-tags.php?message=1#addtag');
} else {
wp_redirect('edit-tags.php?message=4#addtag');
}
exit;
break;
case 'delete':
$tag_ID = (int) $_GET['tag_ID'];
check_admin_referer('delete-tag_' . $tag_ID);
if ( !current_user_can('manage_categories') )
wp_die(__('Cheatin&#8217; uh?'));
wp_delete_term( $tag_ID, 'post_tag');
wp_redirect('edit-tags.php?message=2');
exit;
break;
case 'edit':
require_once ('admin-header.php');
$tag_ID = (int) $_GET['tag_ID'];
$tag = get_term($tag_ID, 'post_tag', OBJECT, 'edit');
include('edit-tag-form.php');
break;
case 'editedtag':
$tag_ID = (int) $_POST['tag_ID'];
check_admin_referer('update-tag_' . $tag_ID);
if ( !current_user_can('manage_categories') )
wp_die(__('Cheatin&#8217; uh?'));
$ret = wp_update_term($tag_ID, 'post_tag', $_POST);
if( $ret && !is_wp_error( $ret ) ) {
wp_redirect('edit-tags.php?message=3');
} else {
wp_redirect('edit-tags.php?message=5');
}
exit;
break;
default:
wp_enqueue_script( 'admin-tags' );
require_once ('admin-header.php');
$messages[1] = __('Tag added.');
$messages[2] = __('Tag deleted.');
$messages[3] = __('Tag updated.');
$messages[4] = __('Tag not added.');
$messages[5] = __('Tag not updated.');
?>
<?php if (isset($_GET['message'])) : ?>
<div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
<?php endif; ?>
<div class="wrap">
<?php if ( current_user_can('manage_categories') ) : ?>
<h2><?php printf(__('Tags (<a href="%s">add new</a>)'), '#addtag') ?> </h2>
<?php else : ?>
<h2><?php _e('Tags') ?> </h2>
<?php endif; ?>
<table class="widefat">
<thead>
<tr>
<th scope="col" style="text-align: center"><?php _e('ID') ?></th>
<th scope="col"><?php _e('Name') ?></th>
<th scope="col" width="90" style="text-align: center"><?php _e('Posts') ?></th>
<th colspan="2" style="text-align: center"><?php _e('Action') ?></th>
</tr>
</thead>
<tbody id="the-list" class="list:tag">
<?php
$pagenum = absint( $_GET['pagenum'] );
if( !$tagsperpage || $tagsperpage < 0 ) {
$tagsperpage = 20;
}
$count = tag_rows( $pagenum, $tagsperpage );
?>
</tbody>
</table>
<?php
$baseurl = get_bloginfo( 'home') . '/wp-admin/edit-tags.php?pagenum=';
if( $pagenum >= 1 ) {
echo '<a href="' . $baseurl . ($pagenum - 1 ) . '">&lt;&lt;' . __('Previous Tags') . '</a>';
if( $count == $tagsperpage ) {
echo ' | ';
}
}
if( $count == $tagsperpage ) {
echo '<a href="' . $baseurl . ($pagenum + 1 ) . '">' . __('Next Tags') . '&gt;&gt;</a>';
}
?>
</div>
<?php if ( current_user_can('manage_categories') ) : ?>
<br />
<?php include('edit-tag-form.php'); ?>
<?php endif; ?>
<?php
break;
}
include('admin-footer.php');
?>

View File

@ -48,8 +48,9 @@ if ( is_single() ) {
}
$h2_search = isset($_GET['s']) && $_GET['s'] ? ' ' . sprintf(__('matching &#8220;%s&#8221;'), wp_specialchars( get_search_query() ) ) : '';
$h2_cat = isset($_GET['cat']) && $_GET['cat'] ? ' ' . sprintf( __('in &#8220;%s&#8221;'), single_cat_title('', false) ) : '';
$h2_tag = isset($_GET['tag']) && $_GET['tag'] ? ' ' . sprintf( __('tagged with &#8220;%s&#8221;'), single_tag_title('', false) ) : '';
$h2_month = isset($_GET['m']) && $_GET['m'] ? ' ' . sprintf( __('during %s'), single_month_title(' ', false) ) : '';
printf( _c( '%1$s%2$s%3$s%4$s%5$s|You can reorder these: 1: Posts, 2: by {s}, 3: matching {s}, 4: in {s}, 5: during {s}' ), $h2_noun, $h2_author, $h2_search, $h2_cat, $h2_month );
printf( _c( '%1$s%2$s%3$s%4$s%5$s%6$s|You can reorder these: 1: Posts, 2: by {s}, 3: matching {s}, 4: in {s}, 5: tagged with {s}, 6: during {s}' ), $h2_noun, $h2_author, $h2_search, $h2_cat, $h2_tag, $h2_month );
}
?></h2>

View File

@ -232,6 +232,65 @@ function dropdown_link_categories( $default = 0 ) {
}
}
// Tag stuff
// Returns a single tag row (see tag_rows below)
// Note: this is also used in admin-ajax.php!
function _tag_row( $tag, $class = '' ) {
$count = number_format_i18n( $tag->count );
$count = ( $count > 0 ) ? "<a href='edit.php?tag=$tag->slug'>$count</a>" : $count;
$out = '';
$out .= '<tr id="tag-' . $tag->term_id . '"' . $class . '>';
$out .= '<th scope="row">' . $tag->term_id . '</th>';
$out .= '<td>' . apply_filters( 'term_name', $tag->name ) . '</td>';
$out .= "<td>$count</td>";
$out .= '<td><a href="edit-tags.php?action=edit&amp;tag_ID=' . $tag->term_id . '" class="edit">' .
__( 'Edit' ) . "</a></td>" .
'<td><a href="' . wp_nonce_url( "edit-tags.php?action=delete&amp;tag_ID=$tag->term_id",
'delete-tag_' . $tag->term_id ) .
'" class="delete:the-list:tag-' . $tag->term_id . ' delete">' .
__( 'Delete' ) . "</a></td>";
$out .= '</tr>';
return $out;
}
// Outputs appropriate rows for the Nth page of the Tag Management screen,
// assuming M tags displayed at a time on the page
// Returns the number of tags displayed
function tag_rows( $page = 0, $pagesize = 20 ) {
// Get a page worth of tags
$start = $page * $pagesize;
$tags = get_terms( 'post_tag', "offset=$start&number=$pagesize&hide_empty=0" );
// convert it to table rows
$out = '';
$class = '';
$i = 0;
$count = 0;
foreach( $tags as $tag ) {
if( $i ) {
$i = 0;
$class = ' class="alternate"';
} else {
$i = 1;
$class = '';
}
$out .= _tag_row( $tag, $class );
$count++;
}
// filter and send to screen
$out = apply_filters('tag_rows', $out);
echo $out;
return $count;
}
// define the columns to display, the syntax is 'internal name' => 'display name'
function wp_manage_posts_columns() {
$posts_columns = array();

21
wp-admin/js/tags.js Normal file
View File

@ -0,0 +1,21 @@
jQuery(function($) {
var options = false
var addAfter = function( r, settings ) {
var name = $("<span>" + $('name', r).text() + "</span>").html();
var id = $('tag', r).attr('id');
options[options.length] = new Option(name, id);
}
var delAfter = function( r, settings ) {
var id = $('tag', r).attr('id');
for ( var o = 0; o < options.length; o++ )
if ( id == options[o].value )
options[o] = null;
}
if ( options )
$('#the-list').wpList( { addAfter: addAfter, delAfter: delAfter } );
else
$('#the-list').wpList();
});

View File

@ -40,6 +40,7 @@ $submenu['edit.php'][5] = array(__('Posts'), 'edit_posts', 'edit.php');
$submenu['edit.php'][10] = array(__('Pages'), 'edit_pages', 'edit-pages.php');
$submenu['edit.php'][15] = array(__('Links'), 'manage_links', 'link-manager.php');
$submenu['edit.php'][20] = array(__('Categories'), 'manage_categories', 'categories.php');
$submenu['edit.php'][21] = array(__('Tags'), 'manage_categories', 'edit-tags.php');
$submenu['edit.php'][25] = array(__('Media Library'), 'upload_files', 'upload.php');
$submenu['edit.php'][30] = array(__('Import'), 'import', 'import.php');
$submenu['edit.php'][35] = array(__('Export'), 'import', 'export.php');

View File

@ -116,6 +116,7 @@ class WP_Scripts {
'how' => __('Separate multiple categories with commas.')
) );
$this->add( 'admin-categories', '/wp-admin/js/categories.js', array('wp-lists'), '20071031' );
$this->add( 'admin-tags', '/wp-admin/js/tags.js', array('wp-lists'), '20071031' );
$this->add( 'admin-custom-fields', '/wp-admin/js/custom-fields.js', array('wp-lists'), '20070823' );
$this->add( 'password-strength-meter', '/wp-admin/js/password-strength-meter.js', array('jquery'), '20070405' );
$this->localize( 'password-strength-meter', 'pwsL10n', array(

View File

@ -525,9 +525,10 @@ function &get_terms($taxonomies, $args = '') {
'hide_empty' => true, 'exclude' => '', 'include' => '',
'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',
'pad_counts' => false);
'pad_counts' => false, 'offset' => '');
$args = wp_parse_args( $args, $defaults );
$args['number'] = absint( $args['number'] );
$args['offset'] = absint( $args['offset'] );
if ( !$single_taxonomy || !is_taxonomy_hierarchical($taxonomies[0]) ||
'' != $args['parent'] ) {
$args['child_of'] = 0;
@ -625,9 +626,13 @@ function &get_terms($taxonomies, $args = '') {
if ( $hide_empty && !$hierarchical )
$where .= ' AND tt.count > 0';
if ( !empty($number) )
$number = 'LIMIT ' . $number;
else
if ( !empty($number) ) {
if( $offset )
$number = 'LIMIT ' . $offset . ',' . $number;
else
$number = 'LIMIT ' . $number;
} else
$number = '';
if ( 'all' == $fields )