2017-08-24 19:52:44 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Contains logic for deleting and adding tags.
|
2017-08-24 19:52:44 +02:00
|
|
|
*
|
2017-12-18 15:14:53 +01:00
|
|
|
* For deleting tags it makes a request to the server to delete the tag.
|
|
|
|
* For adding tags it makes a request to the server to add the tag.
|
2018-06-28 04:30:15 +02:00
|
|
|
*
|
|
|
|
* @output wp-admin/js/tags.js
|
2017-08-24 19:52:44 +02:00
|
|
|
*/
|
2013-11-15 06:02:10 +01:00
|
|
|
|
2018-06-28 04:30:15 +02:00
|
|
|
/* global ajaxurl, wpAjax, tagsl10n, showNotice, validateForm */
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
jQuery(document).ready(function($) {
|
|
|
|
|
2019-07-26 19:46:56 +02:00
|
|
|
var addingTerm = false;
|
|
|
|
|
2017-08-24 19:52:44 +02:00
|
|
|
/**
|
|
|
|
* Adds an event handler to the delete term link on the term overview page.
|
2017-12-18 15:14:53 +01:00
|
|
|
*
|
2017-08-24 19:52:44 +02:00
|
|
|
* Cancels default event handling and event bubbling.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {boolean} Always returns false to cancel the default event handling.
|
2017-08-24 19:52:44 +02:00
|
|
|
*/
|
2013-11-15 06:02:10 +01:00
|
|
|
$( '#the-list' ).on( 'click', '.delete-tag', function() {
|
2012-08-23 02:04:18 +02:00
|
|
|
var t = $(this), tr = t.parents('tr'), r = true, data;
|
2017-08-24 19:52:44 +02:00
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
if ( 'undefined' != showNotice )
|
|
|
|
r = showNotice.warn();
|
2017-08-24 19:52:44 +02:00
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
if ( r ) {
|
|
|
|
data = t.attr('href').replace(/[^?]*\?/, '').replace(/action=delete/, 'action=delete-tag');
|
2017-08-24 19:52:44 +02:00
|
|
|
|
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Makes a request to the server to delete the term that corresponds to the
|
|
|
|
* delete term button.
|
2017-08-24 19:52:44 +02:00
|
|
|
*
|
|
|
|
* @param {string} r The response from the server.
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-24 19:52:44 +02:00
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
$.post(ajaxurl, data, function(r){
|
|
|
|
if ( '1' == r ) {
|
|
|
|
$('#ajax-response').empty();
|
|
|
|
tr.fadeOut('normal', function(){ tr.remove(); });
|
2017-08-24 19:52:44 +02:00
|
|
|
|
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Removes the term from the parent box and the tag cloud.
|
2017-08-24 19:52:44 +02:00
|
|
|
*
|
|
|
|
* `data.match(/tag_ID=(\d+)/)[1]` matches the term id from the data variable.
|
|
|
|
* This term id is then used to select the relevant HTML elements:
|
|
|
|
* The parent box and the tag cloud.
|
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
$('select#parent option[value="' + data.match(/tag_ID=(\d+)/)[1] + '"]').remove();
|
|
|
|
$('a.tag-link-' + data.match(/tag_ID=(\d+)/)[1]).remove();
|
2017-08-24 19:52:44 +02:00
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
} else if ( '-1' == r ) {
|
|
|
|
$('#ajax-response').empty().append('<div class="error"><p>' + tagsl10n.noPerm + '</p></div>');
|
|
|
|
tr.children().css('backgroundColor', '');
|
2017-08-24 19:52:44 +02:00
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
} else {
|
|
|
|
$('#ajax-response').empty().append('<div class="error"><p>' + tagsl10n.broken + '</p></div>');
|
|
|
|
tr.children().css('backgroundColor', '');
|
|
|
|
}
|
|
|
|
});
|
2017-08-24 19:52:44 +02:00
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
tr.children().css('backgroundColor', '#f33');
|
|
|
|
}
|
2017-08-24 19:52:44 +02:00
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2017-08-24 19:52:44 +02:00
|
|
|
/**
|
|
|
|
* Adds a deletion confirmation when removing a tag.
|
|
|
|
*
|
|
|
|
* @since 4.8.0
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-24 19:52:44 +02:00
|
|
|
*/
|
2017-05-12 21:26:41 +02:00
|
|
|
$( '#edittag' ).on( 'click', '.delete', function( e ) {
|
|
|
|
if ( 'undefined' === typeof showNotice ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-24 19:52:44 +02:00
|
|
|
// Confirms the deletion, a negative response means the deletion must not be executed.
|
2017-05-12 21:26:41 +02:00
|
|
|
var response = showNotice.warn();
|
|
|
|
if ( ! response ) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-24 19:52:44 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Adds an event handler to the form submit on the term overview page.
|
2017-08-24 19:52:44 +02:00
|
|
|
*
|
|
|
|
* Cancels default event handling and event bubbling.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {boolean} Always returns false to cancel the default event handling.
|
2017-08-24 19:52:44 +02:00
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
$('#submit').click(function(){
|
|
|
|
var form = $(this).parents('form');
|
|
|
|
|
2013-11-15 06:02:10 +01:00
|
|
|
if ( ! validateForm( form ) )
|
2012-08-23 02:04:18 +02:00
|
|
|
return false;
|
|
|
|
|
2019-07-26 19:46:56 +02:00
|
|
|
if ( addingTerm ) {
|
|
|
|
// If we're adding a term, noop the button to avoid duplicate requests.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
addingTerm = true;
|
|
|
|
form.find( '.submit .spinner' ).addClass( 'is-active' );
|
|
|
|
|
2017-08-24 19:52:44 +02:00
|
|
|
/**
|
|
|
|
* Does a request to the server to add a new term to the database
|
|
|
|
*
|
|
|
|
* @param {string} r The response from the server.
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-24 19:52:44 +02:00
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
$.post(ajaxurl, $('#addtag').serialize(), function(r){
|
2013-11-15 06:02:10 +01:00
|
|
|
var res, parent, term, indent, i;
|
|
|
|
|
2019-07-26 19:46:56 +02:00
|
|
|
addingTerm = false;
|
|
|
|
form.find( '.submit .spinner' ).removeClass( 'is-active' );
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
$('#ajax-response').empty();
|
2013-11-15 06:02:10 +01:00
|
|
|
res = wpAjax.parseAjaxResponse( r, 'ajax-response' );
|
2012-08-23 02:04:18 +02:00
|
|
|
if ( ! res || res.errors )
|
|
|
|
return;
|
|
|
|
|
2013-11-15 06:02:10 +01:00
|
|
|
parent = form.find( 'select#parent' ).val();
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
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.
|
2013-11-15 06:02:10 +01:00
|
|
|
$( '.tags #tag-' + parent ).after( res.responses[0].supplemental.noparents ); // As the parent exists, Insert the version with - - - prefixed
|
2012-08-23 02:04:18 +02:00
|
|
|
else
|
2013-11-15 06:02:10 +01:00
|
|
|
$( '.tags' ).prepend( res.responses[0].supplemental.parents ); // As the parent is not visible, Insert the version with Parent - Child - ThisTerm
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
$('.tags .no-items').remove();
|
|
|
|
|
|
|
|
if ( form.find('select#parent') ) {
|
|
|
|
// Parents field exists, Add new term to the list.
|
2013-11-15 06:02:10 +01:00
|
|
|
term = res.responses[1].supplemental;
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
// Create an indent for the Parent field
|
2013-11-15 06:02:10 +01:00
|
|
|
indent = '';
|
|
|
|
for ( i = 0; i < res.responses[1].position; i++ )
|
2012-08-23 02:04:18 +02:00
|
|
|
indent += ' ';
|
|
|
|
|
2013-11-15 06:02:10 +01:00
|
|
|
form.find( 'select#parent option:selected' ).after( '<option value="' + term.term_id + '">' + indent + term.name + '</option>' );
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$('input[type="text"]:visible, textarea:visible', form).val('');
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|