Skip to content

Commit 5919329

Browse files
committed
Add the option to update the term count once term is added or deleted
1 parent 9156dc3 commit 5919329

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/js/_enqueues/admin/tags.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ jQuery( function($) {
5454
$('select#parent option[value="' + data.match(/tag_ID=(\d+)/)[1] + '"]').remove();
5555
$('a.tag-link-' + data.match(/tag_ID=(\d+)/)[1]).remove();
5656

57+
/**
58+
* Updates the term count on deletion.
59+
*/
60+
var termCountWrapper = $( '#posts-filter .displaying-num' );
61+
if ( termCountWrapper.length ) {
62+
termCountWrapper.each(function () {
63+
$( this ).text( $( this ).text().replace( /^\d+/, function ( match ) {
64+
return parseInt( match, 10 ) - 1;
65+
} ) );
66+
} );
67+
}
5768
} else if ( '-1' == r ) {
5869
$('#ajax-response').empty().append('<div class="notice notice-error"><p>' + wp.i18n.__( 'Sorry, you are not allowed to do that.' ) + '</p></div>');
5970
tr.children().css('backgroundColor', '');
@@ -117,7 +128,7 @@ jQuery( function($) {
117128
* @return {void}
118129
*/
119130
$.post(ajaxurl, $('#addtag').serialize(), function(r){
120-
var res, parent, term, indent, i;
131+
var res, parent, term, indent, i, termCountWrapper, termCount;
121132

122133
addingTerm = false;
123134
form.find( '.submit .spinner' ).removeClass( 'is-active' );
@@ -158,6 +169,19 @@ jQuery( function($) {
158169
form.find( 'select#parent option:selected' ).after( '<option value="' + term.term_id + '">' + indent + term.name + '</option>' );
159170
}
160171

172+
termCountWrapper = $( '#posts-filter .displaying-num' );
173+
termCount = res.responses[ 2 ].supplemental.count ? res.responses[ 2 ].supplemental.count : null;
174+
175+
/**
176+
* Updates the term count if we get count and the term count wrapper exists.
177+
*/
178+
if ( termCountWrapper.length && null !== termCount ) {
179+
termCountWrapper.each(function () {
180+
console.log( $( this ).text().match( /^\d+/ ) );
181+
$( this ).text( $( this ).text().replace( /^\d+/, termCount ) );
182+
} );
183+
}
184+
161185
$('input:not([type="checkbox"]):not([type="radio"]):not([type="button"]):not([type="submit"]):not([type="reset"]):visible, textarea:visible', form).val('');
162186
});
163187

src/wp-admin/includes/ajax-actions.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,14 @@ function wp_ajax_add_tag() {
11521152
$message = $messages['_item'][1];
11531153
}
11541154

1155+
// Get the new term count after the new term has been added.
1156+
$term_count = wp_count_terms(
1157+
array(
1158+
'taxonomy' => $taxonomy,
1159+
'hide_empty' => false,
1160+
)
1161+
);
1162+
11551163
$x->add(
11561164
array(
11571165
'what' => 'taxonomy',
@@ -1172,6 +1180,16 @@ function wp_ajax_add_tag() {
11721180
)
11731181
);
11741182

1183+
$x->add(
1184+
array(
1185+
'what' => 'term-count',
1186+
'supplemental' => array(
1187+
'taxonomy' => $taxonomy,
1188+
'count' => $term_count,
1189+
)
1190+
)
1191+
);
1192+
11751193
$x->send();
11761194
}
11771195

0 commit comments

Comments
 (0)