From c9ea3a7686542714d68d56c7679065cd94e1fbd9 Mon Sep 17 00:00:00 2001 From: dd32 Date: Sat, 13 Feb 2010 05:40:47 +0000 Subject: [PATCH] Convert Terms page to use WP_Ajax_Response(), hierarchical terms will now appear under their parent OR have the parents prefixed, Terms will be removed from tag cloud/parent list upon deletion, Terms will be added to Parent list in correct order upon ajax creation, Errors on term creation flow back to UI, clean up _tag_row() alternate class handling, Show None text in Category dropdown if empty and show_if_empty = true. See #11838 git-svn-id: http://svn.automattic.com/wordpress/trunk@13086 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-ajax.php | 35 +++++++++++++++++++++++------ wp-admin/includes/template.php | 25 ++++++++++++--------- wp-admin/js/tags.dev.js | 37 ++++++++++++++++++++++--------- wp-admin/js/tags.js | 2 +- wp-includes/category-template.php | 6 +++++ 5 files changed, 76 insertions(+), 29 deletions(-) diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index fbf3e3884e..d4632af2d2 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -538,30 +538,51 @@ case 'add-tag' : // From Manage->Tags $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag'; $tax = get_taxonomy($taxonomy); + $x = new WP_Ajax_Response(); + if ( !current_user_can( $tax->edit_cap ) ) die('-1'); $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST ); if ( !$tag || is_wp_error($tag) || (!$tag = get_term( $tag['term_id'], $taxonomy )) ) { - echo '

' . __('An error has occured. Please reload the page and try again.') . '

'; - exit; + $message = __('An error has occured. Please reload the page and try again.'); + if ( is_wp_error($tag) && $tag->get_error_message() ) + $message = $tag->get_error_message(); + + $x->add( array( + 'what' => 'taxonomy', + 'data' => new WP_Error('error', $message ) + ) ); + $x->send(); } $level = 0; $tag_full_name = false; + $tag_full_name = $tag->name; if ( is_taxonomy_hierarchical($taxonomy) ) { - $tag_full_name = $tag->name; $_tag = $tag; - while ( $_tag->parent ) { + while ( $_tag->parent ) { $_tag = get_term( $_tag->parent, $taxonomy ); $tag_full_name = $_tag->name . ' — ' . $tag_full_name; $level++; } - $tag_full_name = esc_attr($tag_full_name); } - echo _tag_row( $tag, $level, $tag_full_name, $taxonomy ); - exit; + if ( is_taxonomy_hierarchical($taxonomy) ) + $noparents = _tag_row( $tag, $level, $taxonomy ); + $tag->name = $tag_full_name; + $parents = _tag_row( $tag, 0, $taxonomy); + + $x->add( array( + 'what' => 'taxonomy', + 'supplemental' => compact('parents', 'noparents') + ) ); + $x->add( array( + 'what' => 'term', + 'position' => $level, + 'supplemental' => get_term( $tag->term_id, $taxonomy, ARRAY_A ) //Refetch as $tag has been contaminated by the full name. + ) ); + $x->send(); break; case 'get-tagcloud' : if ( !current_user_can( 'edit_posts' ) ) diff --git a/wp-admin/includes/template.php b/wp-admin/includes/template.php index f8367bfc87..fa9668c9da 100644 --- a/wp-admin/includes/template.php +++ b/wp-admin/includes/template.php @@ -482,7 +482,10 @@ function wp_link_category_checklist( $link_id = 0 ) { * @param unknown_type $class * @return unknown */ -function _tag_row( $tag, $level, $class = '', $taxonomy = 'post_tag' ) { +function _tag_row( $tag, $level, $taxonomy = 'post_tag' ) { + static $row_class = ''; + $row_class = ($row_class == '' ? ' class="alternate"' : ''); + $count = number_format_i18n( $tag->count ); if ( 'post_tag' == $taxonomy ) $tagsel = 'tag'; @@ -496,12 +499,13 @@ function _tag_row( $tag, $level, $class = '', $taxonomy = 'post_tag' ) { $count = ( $count > 0 ) ? "$count" : $count; $pad = str_repeat( '— ', max(0, $level) ); - $name = apply_filters( 'term_name', $pad . ' ' . $tag->name ); + $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag ); $qe_data = get_term($tag->term_id, $taxonomy, object, 'edit'); $edit_link = "edit-tags.php?action=edit&taxonomy=$taxonomy&tag_ID=$tag->term_id"; $out = ''; - $out .= ''; + $out .= ''; + $columns = get_column_headers('edit-tags'); $hidden = get_hidden_columns('edit-tags'); @@ -607,12 +611,12 @@ function tag_rows( $page = 1, $pagesize = 20, $searchterms = '', $taxonomy = 'po else $children = _get_term_hierarchy($taxonomy); - // Some funky recursion to get the job done is contained within, Skip it for non-hierarchical taxonomies for performance sake + // Some funky recursion to get the job done(Paging & parents mainly) is contained within, Skip it for non-hierarchical taxonomies for performance sake $out .= _term_rows($taxonomy, $terms, $children, $page, $pagesize, $count); } else { $terms = get_terms( $taxonomy, $args ); foreach( $terms as $term ) - $out .= _tag_row( $term, 0, ++$count % 2 ? ' class="alternate"' : '', $taxonomy ); + $out .= _tag_row( $term, 0, $taxonomy ); } echo $out; @@ -648,21 +652,20 @@ function _term_rows( $taxonomy, $terms, &$children, $page = 1, $per_page = 20, & unset($parent_ids); $num_parents = count($my_parents); - $count -= $num_parents; // Do not include parents in the per-page count, This is due to paging issues with unknown numbers of rows. while ( $my_parent = array_pop($my_parents) ) { - $output .= "\t" . _tag_row( $my_parent, $level - $num_parents, ++$count % 2 ? ' class="alternate"' : '', $taxonomy ); + $output .= "\t" . _tag_row( $my_parent, $level - $num_parents, $taxonomy ); $num_parents--; } } if ( $count >= $start ) - $output .= "\t" . _tag_row( $term, $level, ++$count % 2 ? ' class="alternate"' : '', $taxonomy ); - else - ++$count; + $output .= "\t" . _tag_row( $term, $level, $taxonomy ); + + ++$count; unset($terms[$key]); - if ( isset($children[$term->term_id]) ) + if ( isset($children[$term->term_id]) && empty($_GET['s']) ) $output .= _term_rows( $taxonomy, $terms, $children, $page, $per_page, $count, $term->term_id, $level + 1 ); } diff --git a/wp-admin/js/tags.dev.js b/wp-admin/js/tags.dev.js index de22409f1e..056cc25861 100644 --- a/wp-admin/js/tags.dev.js +++ b/wp-admin/js/tags.dev.js @@ -10,6 +10,9 @@ jQuery(document).ready(function($) { if ( '1' == r ) { $('#ajax-response').empty(); tr.fadeOut('normal', function(){ tr.remove(); }); + // Remove the term from the parent box and tag cloud + $('select#parent option[value=' + data.match(/tag_ID=(\d+)/)[1] + ']').remove(); + $('a.tag-link-' + data.match(/tag_ID=(\d+)/)[1]).remove(); } else if ( '-1' == r ) { $('#ajax-response').empty().append('

' + tagsl10n.noPerm + '

'); tr.children().css('backgroundColor', ''); @@ -30,17 +33,31 @@ jQuery(document).ready(function($) { return false; $.post(ajaxurl, $('#addtag').serialize(), function(r){ - if ( r.indexOf('
0 && $('#tag-' + parent ).length > 0 ) // If the parent exists on this page, insert it below. Else insert it at the top of the list. - $('#the-list #tag-' + parent).after(r); - else - $('#the-list').prepend(r); - $('input[type="text"]:visible, textarea:visible', form).val(''); + $('#ajax-response').empty(); + var res = wpAjax.parseAjaxResponse(r, 'ajax-response'); + if ( ! res ) + return; + + var parent = form.find('select#parent').val(); + + if ( parent > 0 && $('#tag-' + parent ).length > 0 ) // If the parent exists on this page, insert it below. Else insert it at the top of the list. + $('#the-list #tag-' + parent).after( res.responses[0].supplemental['noparents'] ); // As the parent exists, Insert the version with - - - prefixed + else + $('#the-list').prepend( res.responses[0].supplemental['parents'] ); // As the parent is not visible, Insert the version with Parent - Child - ThisTerm + + if ( form.find('select#parent') ) { + // Parents field exists, Add new term to the list. + var term = res.responses[1].supplemental; + + // Create an indent for the Parent field + var indent = ''; + for ( var i = 0; i < res.responses[1].position; i++ ) + indent += '   '; + + form.find('select#parent option:selected').after(''); } + + $('input[type="text"]:visible, textarea:visible', form).val(''); }); return false; diff --git a/wp-admin/js/tags.js b/wp-admin/js/tags.js index 1928515652..08349dc2af 100644 --- a/wp-admin/js/tags.js +++ b/wp-admin/js/tags.js @@ -1 +1 @@ -jQuery(document).ready(function($){$(".delete-tag").live("click",function(e){var t=$(this),tr=t.parents("tr"),r=true,data;if("undefined"!=showNotice){r=showNotice.warn()}if(r){data=t.attr("href").replace(/[^?]*\?/,"").replace(/action=delete/,"action=delete-tag");$.post(ajaxurl,data,function(r){if("1"==r){$("#ajax-response").empty();tr.fadeOut("normal",function(){tr.remove()})}else{if("-1"==r){$("#ajax-response").empty().append('

'+tagsl10n.noPerm+"

");tr.children().css("backgroundColor","")}else{$("#ajax-response").empty().append('

'+tagsl10n.broken+"

");tr.children().css("backgroundColor","")}}});tr.children().css("backgroundColor","#f33")}return false});$("#submit").click(function(){var form=$(this).parents("form");if(!validateForm(form)){return false}$.post(ajaxurl,$("#addtag").serialize(),function(r){if(r.indexOf('
0&&$("#tag-"+parent).length>0){$("#the-list #tag-"+parent).after(r)}else{$("#the-list").prepend(r)}$('input[type="text"]:visible, textarea:visible',form).val("")}});return false})}); \ No newline at end of file +jQuery(document).ready(function($){$(".delete-tag").live("click",function(e){var t=$(this),tr=t.parents("tr"),r=true,data;if("undefined"!=showNotice){r=showNotice.warn()}if(r){data=t.attr("href").replace(/[^?]*\?/,"").replace(/action=delete/,"action=delete-tag");$.post(ajaxurl,data,function(r){if("1"==r){$("#ajax-response").empty();tr.fadeOut("normal",function(){tr.remove()});$("select#parent option[value="+data.match(/tag_ID=(\d+)/)[1]+"]").remove();$("a.tag-link-"+data.match(/tag_ID=(\d+)/)[1]).remove()}else{if("-1"==r){$("#ajax-response").empty().append('

'+tagsl10n.noPerm+"

");tr.children().css("backgroundColor","")}else{$("#ajax-response").empty().append('

'+tagsl10n.broken+"

");tr.children().css("backgroundColor","")}}});tr.children().css("backgroundColor","#f33")}return false});$("#submit").click(function(){var form=$(this).parents("form");if(!validateForm(form)){return false}$.post(ajaxurl,$("#addtag").serialize(),function(r){$("#ajax-response").empty();var res=wpAjax.parseAjaxResponse(r,"ajax-response");if(!res){return}var parent=form.find("select#parent").val();if(parent>0&&$("#tag-"+parent).length>0){$("#the-list #tag-"+parent).after(res.responses[0].supplemental.noparents)}else{$("#the-list").prepend(res.responses[0].supplemental.parents)}if(form.find("select#parent")){var term=res.responses[1].supplemental;var indent="";for(var i=0;i'+indent+term.name+"")}$('input[type="text"]:visible, textarea:visible',form).val("")});return false})}); \ No newline at end of file diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index 4d8713e648..6719f3e33c 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -364,6 +364,12 @@ function wp_dropdown_categories( $args = '' ) { $output = "