Make WordPress Core

Changeset 13086


Ignore:
Timestamp:
02/13/2010 05:40:47 AM (15 years ago)
Author:
dd32
Message:

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

Location:
trunk
Files:
5 edited

Legend:

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

    r13046 r13086  
    539539    $tax = get_taxonomy($taxonomy);
    540540
     541    $x = new WP_Ajax_Response();
     542
    541543    if ( !current_user_can( $tax->edit_cap ) )
    542544        die('-1');
     
    545547
    546548    if ( !$tag || is_wp_error($tag) || (!$tag = get_term( $tag['term_id'], $taxonomy )) ) {
    547         echo '<div class="error"><p>' . __('An error has occured. Please reload the page and try again.') . '</p></div>';
    548         exit;
     549        $message = __('An error has occured. Please reload the page and try again.');
     550        if ( is_wp_error($tag) && $tag->get_error_message() )
     551            $message = $tag->get_error_message();
     552
     553        $x->add( array(
     554            'what' => 'taxonomy',
     555            'data' => new WP_Error('error', $message )
     556        ) );
     557        $x->send();
    549558    }
    550559
    551560    $level = 0;
    552561    $tag_full_name = false;
     562    $tag_full_name = $tag->name;
    553563    if ( is_taxonomy_hierarchical($taxonomy) ) {
    554         $tag_full_name = $tag->name;
    555564        $_tag = $tag;
    556         while ( $_tag->parent ) {
     565        while ( $_tag->parent  ) {
    557566            $_tag = get_term( $_tag->parent, $taxonomy );
    558567            $tag_full_name = $_tag->name . ' &#8212; ' . $tag_full_name;
    559568            $level++;
    560569        }
    561         $tag_full_name = esc_attr($tag_full_name);
    562     }
    563     echo _tag_row( $tag, $level, $tag_full_name, $taxonomy );
    564     exit;
     570    }
     571    if ( is_taxonomy_hierarchical($taxonomy) )
     572        $noparents = _tag_row( $tag, $level, $taxonomy );
     573    $tag->name = $tag_full_name;
     574    $parents = _tag_row( $tag, 0, $taxonomy);
     575
     576    $x->add( array(
     577        'what' => 'taxonomy',
     578        'supplemental' => compact('parents', 'noparents')
     579        ) );
     580    $x->add( array(
     581        'what' => 'term',
     582        'position' => $level,
     583        'supplemental' => get_term( $tag->term_id, $taxonomy, ARRAY_A ) //Refetch as $tag has been contaminated by the full name.
     584        ) );
     585    $x->send();
    565586    break;
    566587case 'get-tagcloud' :
  • trunk/wp-admin/includes/template.php

    r13081 r13086  
    483483 * @return unknown
    484484 */
    485 function _tag_row( $tag, $level, $class = '', $taxonomy = 'post_tag' ) {
     485function _tag_row( $tag, $level, $taxonomy = 'post_tag' ) {
     486        static $row_class = '';
     487        $row_class = ($row_class == '' ? ' class="alternate"' : '');
     488
    486489        $count = number_format_i18n( $tag->count );
    487490        if ( 'post_tag' == $taxonomy )
     
    497500
    498501        $pad = str_repeat( '&#8212; ', max(0, $level) );
    499         $name = apply_filters( 'term_name', $pad . ' ' . $tag->name );
     502        $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
    500503        $qe_data = get_term($tag->term_id, $taxonomy, object, 'edit');
    501504        $edit_link = "edit-tags.php?action=edit&amp;taxonomy=$taxonomy&amp;tag_ID=$tag->term_id";
    502505
    503506        $out = '';
    504         $out .= '<tr id="tag-' . $tag->term_id . '"' . $class . '>';
     507        $out .= '<tr id="tag-' . $tag->term_id . '"' . $row_class . '>';
     508
    505509
    506510        $columns = get_column_headers('edit-tags');
     
    608612            $children = _get_term_hierarchy($taxonomy);
    609613
    610         // Some funky recursion to get the job done is contained within, Skip it for non-hierarchical taxonomies for performance sake
     614        // Some funky recursion to get the job done(Paging & parents mainly) is contained within, Skip it for non-hierarchical taxonomies for performance sake
    611615        $out .= _term_rows($taxonomy, $terms, $children, $page, $pagesize, $count);
    612616    } else {
    613617        $terms = get_terms( $taxonomy, $args );
    614618        foreach( $terms as $term )
    615             $out .= _tag_row( $term, 0, ++$count % 2 ? ' class="alternate"' : '', $taxonomy );
     619            $out .= _tag_row( $term, 0, $taxonomy );
    616620    }
    617621
     
    649653
    650654            $num_parents = count($my_parents);
    651             $count -= $num_parents; // Do not include parents in the per-page count, This is due to paging issues with unknown numbers of rows.
    652655            while ( $my_parent = array_pop($my_parents) ) {
    653                 $output .=  "\t" . _tag_row( $my_parent, $level - $num_parents, ++$count % 2 ? ' class="alternate"' : '', $taxonomy );
     656                $output .=  "\t" . _tag_row( $my_parent, $level - $num_parents, $taxonomy );
    654657                $num_parents--;
    655658            }
     
    657660
    658661        if ( $count >= $start )
    659             $output .= "\t" . _tag_row( $term, $level, ++$count % 2 ? ' class="alternate"' : '', $taxonomy );
    660         else
    661             ++$count;
     662            $output .= "\t" . _tag_row( $term, $level, $taxonomy );
     663
     664        ++$count;
    662665
    663666        unset($terms[$key]);
    664667
    665         if ( isset($children[$term->term_id]) )
     668        if ( isset($children[$term->term_id]) && empty($_GET['s']) )
    666669            $output .= _term_rows( $taxonomy, $terms, $children, $page, $per_page, $count, $term->term_id, $level + 1 );
    667670    }
  • trunk/wp-admin/js/tags.dev.js

    r12818 r13086  
    1111                    $('#ajax-response').empty();
    1212                    tr.fadeOut('normal', function(){ tr.remove(); });
     13                    // Remove the term from the parent box and tag cloud
     14                    $('select#parent option[value=' + data.match(/tag_ID=(\d+)/)[1] + ']').remove();
     15                    $('a.tag-link-' + data.match(/tag_ID=(\d+)/)[1]).remove();
    1316                } else if ( '-1' == r ) {
    1417                    $('#ajax-response').empty().append('<div class="error"><p>' + tagsl10n.noPerm + '</p></div>');
     
    3134
    3235        $.post(ajaxurl, $('#addtag').serialize(), function(r){
    33             if ( r.indexOf('<div class="error"') === 0 ) {
    34                 $('#ajax-response').append(r);
    35             } else {
    36                 $('#ajax-response').empty();
    37                 var parent = form.find('select#parent').val();
    38                 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.
    39                     $('#the-list #tag-' + parent).after(r);
    40                 else
    41                     $('#the-list').prepend(r);
    42                 $('input[type="text"]:visible, textarea:visible', form).val('');
     36           $('#ajax-response').empty();
     37            var res = wpAjax.parseAjaxResponse(r, 'ajax-response');
     38            if ( ! res )
     39                return;
     40
     41            var parent = form.find('select#parent').val(); 
     42
     43            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.
     44                $('#the-list #tag-' + parent).after( res.responses[0].supplemental['noparents'] ); // As the parent exists, Insert the version with - - - prefixed
     45            else
     46                $('#the-list').prepend( res.responses[0].supplemental['parents'] ); // As the parent is not visible, Insert the version with Parent - Child - ThisTerm
     47
     48            if ( form.find('select#parent') ) {
     49                // Parents field exists, Add new term to the list.
     50                var term = res.responses[1].supplemental;
     51
     52                // Create an indent for the Parent field
     53                var indent = '';
     54                for ( var i = 0; i < res.responses[1].position; i++ )
     55                    indent += '&nbsp;&nbsp;&nbsp;';
     56
     57                form.find('select#parent option:selected').after('<option value="' + term['term_id'] + '">' + indent + term['name'] + '</option>');
    4358            }
     59
     60            $('input[type="text"]:visible, textarea:visible', form).val('');
    4461        });
    4562
  • trunk/wp-admin/js/tags.js

    r12818 r13086  
    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('<div class="error"><p>'+tagsl10n.noPerm+"</p></div>");tr.children().css("backgroundColor","")}else{$("#ajax-response").empty().append('<div class="error"><p>'+tagsl10n.broken+"</p></div>");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('<div class="error"')===0){$("#ajax-response").append(r)}else{$("#ajax-response").empty();var parent=form.find("select#parent").val();if(parent>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})});
     1jQuery(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('<div class="error"><p>'+tagsl10n.noPerm+"</p></div>");tr.children().css("backgroundColor","")}else{$("#ajax-response").empty().append('<div class="error"><p>'+tagsl10n.broken+"</p></div>");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<res.responses[1].position;i++){indent+="&nbsp;&nbsp;&nbsp;"}form.find("select#parent option:selected").after('<option value="'+term.term_id+'">'+indent+term.name+"</option>")}$('input[type="text"]:visible, textarea:visible',form).val("")});return false})});
  • trunk/wp-includes/category-template.php

    r12798 r13086  
    365365    else
    366366        $output = '';
     367   
     368    if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
     369        $show_option_none = apply_filters( 'list_cats', $show_option_none );
     370        $output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
     371    }
     372   
    367373    if ( ! empty( $categories ) ) {
    368374
Note: See TracChangeset for help on using the changeset viewer.