PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
automatorwp / assets / js / automatorwp-admin.js

automatorwp-admin.js in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.3, at assets/js/automatorwp-admin.js

2,173 lines 78.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function ( $ ) {
2
3 // Selector Control
4 $('.automatorwp-selector select').each(function() { automatorwp_selector( $(this) ); });
5
6 // Post Selector Control
7 $('.automatorwp-post-selector select').each(function() { automatorwp_post_selector( $(this) ); });
8
9 // Term Selector Control
10 $('.automatorwp-term-selector select').each(function() { automatorwp_term_selector( $(this) ); });
11
12 // Taxonomy Selector Control
13 $('.automatorwp-taxonomy-selector select').each(function() { automatorwp_selector( $(this) ); });
14
15 // User Selector Control
16 $('.automatorwp-user-selector select').each(function() { automatorwp_user_selector( $(this) ); });
17
18 // Object Selector Control
19 $('.automatorwp-object-selector select').each(function() { automatorwp_object_selector( $(this) ); });
20
21 // Ajax Selector Control
22 $('.automatorwp-ajax-selector select').each(function() { automatorwp_ajax_selector( $(this) ); });
23
24 // Filter selector
25 $('.automatorwp-filter-selector select').each(function() { automatorwp_filter_selector( $(this) ); });
26
27 // Update items positions at init to avoid any order error
28 automatorwp_update_items_position( $('.automatorwp-automation-items.automatorwp-triggers') );
29 automatorwp_update_items_position( $('.automatorwp-automation-items.automatorwp-actions') );
30
31 // Save expiration (just to update its preview to "Never Expires")
32 $('body').on('click', '.cmb2-id-expiration + #cmb-field-js-controls-expiration-after #cmb-field-js-controls-expiration-save', function(e) {
33 var field_row = $(this).parent().prev('.cmb-row');
34 var field = $($(this).attr('href') + '_date');
35 if( field.val() === '' ) {
36 var value_container = field_row.prev('.cmb-field-js-controls-before').find('.cmb-field-js-controls-value');
37 value_container.text( automatorwp_admin.never_expires_text );
38 }
39 });
40
41 // Save and activate
42 $('body').on('click', '.automatorwp-save-and-activate input', function(e) {
43 e.preventDefault();
44
45 if( $('select#status').length ) {
46 $('select#status').val('active');
47 } else if( $('input#status1').length ) {
48 $('input#status1').prop('checked', true);
49 $('input#status2').prop('checked', false);
50 }
51
52 $('#ct-save').trigger('click');
53 });
54
55 // Tooltip
56 $('body').on('mouseover', '.automatorwp-tooltip', function(e) {
57 $(this).prev('.cmb2-metabox-description').addClass('automatorwp-tooltip-dialog').fadeIn('fast');
58 });
59
60 $('body').on('mouseout', '.automatorwp-tooltip', function(e) {
61 $(this).prev('.cmb2-metabox-description').fadeOut('fast');
62 });
63
64 // Make automation items sortable
65 $('.automatorwp-automation-items').sortable({
66 cursor: 'grabbing',
67 cancel: '.automatorwp-option-form-container, .automatorwp-no-grab',
68 // When the items order is updated
69 update : function ( e, ui ) {
70
71 // Event target refers to .automatorwp-triggers or .automatorwp-actions
72 automatorwp_update_items_position( $(e.target) );
73
74 },
75 stop: function( e, ui ) {
76
77 // Refresh wysiwyg editors (after drag and drop forms with editors, is required to refresh them to make them work again)
78 ui.item.find( '.cmb-type-wysiwyg' ).each( function() {
79
80 var $this = $(this);
81
82 var textarea_id = $this.find('textarea').attr('id');
83 var textarea = $this.find('textarea').clone();
84
85 // Remove CMB2 HTML and add a single textarea to be converted as editor
86 $this.find('.wp-editor-wrap').remove();
87 $this.find('.cmb-td').append(textarea);
88
89 automatorwp_initialize_wysiwyg_editor( textarea_id );
90
91 } );
92
93 if ( 'undefined' !== typeof window.QTags ) {
94 window.QTags._buttonsInit();
95 }
96
97 }
98 });
99
100 // Toggleable options list
101 $('body').on('click', '.automatorwp-toggleable-options-list-toggle', function(e) {
102 e.preventDefault();
103
104 var $this = $(this);
105 var show_text = $this.data( 'show-text' );
106 var hide_text = $this.data( 'hide-text' );
107 var list = $this.closest('.cmb-td').find('.automatorwp-toggleable-options-list');
108
109 // Toggle options list visitibility and change toggle text
110 if( $this.text() === show_text ) {
111 $this.text(hide_text);
112 list.slideDown('fast');
113 } else {
114 $this.text(show_text);
115 list.slideUp('fast');
116 }
117
118 });
119
120 // Sequential
121 $('body').on('change', 'input#sequential', function(e) {
122
123 var selector = '.automatorwp-triggers .automatorwp-automation-item:not(.automatorwp-automation-item-filter) .automatorwp-automation-item-position';
124
125 if( $(this).prop('checked') ) {
126 $(selector).show();
127 } else {
128 $(selector).hide();
129 }
130
131 });
132
133 // Move Up/Down
134 $('body').on('click', '.automatorwp-automation-item-action-move-up, .automatorwp-automation-item-action-move-down', function(e) {
135
136 var item = $(this).closest('.automatorwp-automation-item');
137 var type_class = '.automatorwp-' + ( item.hasClass('automatorwp-trigger') ? 'trigger' : 'action' );
138 var items_list = $(this).closest('.automatorwp-automation-items');
139 var direction = ( $(this).hasClass('automatorwp-automation-item-action-move-up') ? 'up' : 'down' );
140 var sibling_item;
141
142 if( direction === 'up' ) {
143 sibling_item = item.prev('.automatorwp-automation-item');
144 } else {
145 sibling_item = item.next('.automatorwp-automation-item');
146 }
147
148 if( ! sibling_item.length ) {
149 return;
150 }
151
152 if( direction === 'up' ) {
153 $('#' + item.attr('id') + type_class).insertBefore( '#' + sibling_item.attr('id') + type_class );
154 } else {
155 $('#' + item.attr('id') + type_class).insertAfter( '#' + sibling_item.attr('id') + type_class );
156 }
157
158 // Update items position
159 automatorwp_update_items_position( items_list );
160 });
161
162 // Delete item
163 $('body').on('click', '.automatorwp-automation-item-action-delete', function(e) {
164
165 var item = $(this).closest('.automatorwp-automation-item');
166 var items_list = $(this).closest('.automatorwp-automation-items');
167 var item_type = item.hasClass('automatorwp-trigger') ? 'trigger' : 'action';
168 var id = item.find('input.id').val();
169
170 // Hide this item
171 item.slideUp('fast', function() {
172 // Removes this item
173 item.remove();
174
175 // Update items position
176 automatorwp_update_items_position( items_list );
177 });
178
179 // If is an anonymous automation, add the add trigger form again
180 if( item_type === 'trigger' && $('body.edit-anonymous-automation').length ) {
181 // Show add trigger form
182 $('<div class="automatorwp-automation-item automatorwp-trigger" style="display: none;">'
183 + $('#automatorwp_triggers').find('.automatorwp-add-item-form').html()
184 + '</div>').appendTo('.automatorwp-triggers').slideDown('fast');
185 }
186
187 $.ajax({
188 url: ajaxurl,
189 method: 'POST',
190 data: {
191 action: 'automatorwp_delete_automation_item',
192 nonce: automatorwp_admin.nonce,
193 id: id,
194 item_type: item_type
195 },
196 success: function( response ) {
197
198 if( response.success ) {
199
200 // Remove trigger and action tags
201 if( item_type === 'trigger' || item_type === 'action' ) {
202 $('.automatorwp-automation-tag-selector optgroup[data-id="' + id + '"]').remove();
203 }
204
205 } else {
206
207 }
208 },
209 error: function( response ) {
210
211 }
212 });
213
214
215 });
216
217 // Close
218 $('body').on('click', '.automatorwp-automation-item-action-close', function(e) {
219
220 var item = $(this).closest('.automatorwp-automation-item');
221 var items_list = $(this).closest('.automatorwp-automation-items');
222 var item_type = item.hasClass('automatorwp-trigger') ? 'trigger' : 'action';
223 var id = item.find('input.id').val();
224
225 // Hide this item
226 item.slideUp('fast', function() {
227 // Removes this item
228 item.remove();
229 });
230
231 });
232
233 // Update controls depending of automation type
234 if( $('body.edit-anonymous-automation').length ) {
235
236 if( $('#automatorwp_triggers .automatorwp-automation-item').length === 0 ) {
237 // Show add trigger form
238 $('<div class="automatorwp-automation-item automatorwp-trigger">'
239 + $('#automatorwp_triggers').find('.automatorwp-add-item-form').html()
240 + '</div>').appendTo('.automatorwp-triggers');
241 }
242
243 // Remove elements and inputs not allowed
244 $('.automatorwp-add-trigger').remove();
245 $('#automatorwp_triggers .automatorwp-add-filter').remove();
246 $('.automatorwp-sequential-field').remove();
247 $('#cmb-field-js-controls-times_per_user-before').remove();
248 $('.cmb2-id-times-per-user').remove();
249 $('#cmb-field-js-controls-times_per_user-after').remove();
250
251 } else {
252 // Remove anonymous info
253 $('.automatorwp-automation-ui-anonymous-notice').remove();
254 }
255
256 // -----------------------------------------------
257 // Anonymous user action
258 // -----------------------------------------------
259
260 // Anonymous user action choices
261 $('body').on('click', '.automatorwp-action-automatorwp-anonymous-user .automatorwp-anonymous-user-choice', function(e) {
262
263 var item = $(this).closest('.automatorwp-automation-item');
264 var option_form = item.find('.automatorwp-option-form-container[data-option="user"]');
265 var choice = $(this).data('choice');
266
267 // Update the select value
268 option_form.find('.cmb2-id-run-actions-on select').val( choice ).trigger('change');
269
270 // Hide the choices
271 $(this).closest('.automatorwp-anonymous-user-choices').slideUp('fast');
272
273 // Show this option form
274 option_form.addClass('automatorwp-option-form-active').slideDown('fast');
275
276 });
277
278 // Anonymous user action change button
279 $('body').on('click', '.automatorwp-action-automatorwp-anonymous-user .automatorwp-anonymous-user-change-button', function(e) {
280 var item = $(this).closest('.automatorwp-automation-item');
281
282 // Hide resume
283 $(this).closest('.automatorwp-anonymous-user-resume').slideUp('fast');
284
285 // Show the choices
286 item.find('.automatorwp-anonymous-user-choices').slideDown('fast');
287 });
288
289 // Anonymous user action resume buttons
290 $('body').on('click', '.automatorwp-action-automatorwp-anonymous-user .automatorwp-anonymous-user-resume strong', function(e) {
291 var item = $(this).closest('.automatorwp-automation-item');
292 var option_form = item.find('.automatorwp-option-form-container[data-option="user"]');
293
294 // Trigger change on select
295 option_form.find('.cmb2-id-run-actions-on select').trigger('change');
296
297 // Hide resume
298 $(this).closest('.automatorwp-anonymous-user-resume').slideUp('fast');
299
300 // Show this option form
301 option_form.addClass('automatorwp-option-form-active').slideDown('fast');
302 });
303
304 // Anonymous user action fields visibility
305 $('body').on('change', '.automatorwp-action-automatorwp-anonymous-user .cmb2-id-run-actions-on select', function(e) {
306 var choice = $(this).val();
307 var form = $(this).closest('.cmb2-metabox');
308 var target = undefined;
309 var checked = undefined;
310
311 if( choice === 'existing_user' ) {
312
313 checked = form.find('.cmb2-id-existing-user-not-exists input:checked');
314
315 // Check if, for existing user, wants to create a new user
316 if( checked.length && checked.val() === 'new_user' ) {
317
318 target = undefined;
319
320 // Show all fields
321 form.find('> .cmb-row').show();
322
323 // Hide new user search fields (and run actions on)
324 form.find('.cmb2-id-run-actions-on,' +
325 '.cmb2-id-new-user-exists,' +
326 ' .cmb2-id-new-user-search-field').hide();
327
328 } else {
329 // Target to all existing fields
330 target = form.find('.cmb2-id-search-field,' +
331 ' .cmb2-id-search-field-value,' +
332 ' .cmb2-id-existing-user-not-exists');
333 }
334 } else if( choice === 'new_user' ) {
335
336 checked = form.find('.cmb2-id-new-user-exists input:checked');
337
338 // Check if, for new user, wants to assign to a existing user
339 if( checked.length && checked.val() === 'existing_user' ) {
340
341 // Target to all new user fields
342 target = form.find('.cmb2-id-user-login,' +
343 ' .cmb2-id-user-email,' +
344 ' .cmb2-id-first-name,' +
345 ' .cmb2-id-last-name,' +
346 ' .cmb2-id-user-url,' +
347 ' .cmb2-id-user-pass,' +
348 ' .cmb2-id-role,' +
349 ' .cmb2-id-send-user-notification,' +
350 ' .cmb2-id-user-meta,' +
351 ' .cmb2-id-new-user-exists,' +
352 ' .cmb2-id-new-user-search-field');
353
354 } else {
355
356 // target new user fields excepts the search field
357 target = form.find('.cmb2-id-user-login,' +
358 ' .cmb2-id-user-email,' +
359 ' .cmb2-id-first-name,' +
360 ' .cmb2-id-last-name,' +
361 ' .cmb2-id-user-url,' +
362 ' .cmb2-id-user-pass,' +
363 ' .cmb2-id-role,' +
364 ' .cmb2-id-send-user-notification,' +
365 ' .cmb2-id-user-meta,' +
366 ' .cmb2-id-new-user-exists');
367
368 }
369 }
370
371 if( target !== undefined ) {
372 // Hide all fields
373 form.find('> .cmb-row').hide();
374
375 // Only show target fields
376 target.show();
377 }
378
379 });
380
381 // Anonymous user action existing user not exists
382 $('body').on('change', '.automatorwp-action-automatorwp-anonymous-user .cmb2-id-existing-user-not-exists input', function(e) {
383 var $this = $(this);
384 var fields = $this.closest('.cmb2-metabox').find(
385 '.cmb2-id-user-login,' +
386 ' .cmb2-id-user-email,' +
387 ' .cmb2-id-first-name,' +
388 ' .cmb2-id-last-name,' +
389 ' .cmb2-id-user-url,' +
390 ' .cmb2-id-user-pass,' +
391 ' .cmb2-id-role,' +
392 ' .cmb2-id-send-user-notification,' +
393 ' .cmb2-id-user-meta');
394
395 if( $this.prop('checked') && $this.val() === 'new_user' ) {
396 fields.slideDown('fast');
397 } else {
398 fields.slideUp('fast');
399 }
400 });
401
402 // Anonymous user action new user exists
403 $('body').on('change', '.automatorwp-action-automatorwp-anonymous-user .cmb2-id-new-user-exists input', function(e) {
404 var $this = $(this);
405 var fields = $this.closest('.cmb2-metabox').find('.cmb2-id-new-user-search-field');
406
407 if( $this.prop('checked') && $this.val() === 'existing_user' ) {
408 fields.slideDown('fast');
409 } else {
410 fields.slideUp('fast');
411 }
412 });
413
414 // Anonymous user action cancel
415 $('body').on('click', '.automatorwp-action-automatorwp-anonymous-user .automatorwp-cancel-option-form', function(e) {
416
417 var item = $(this).closest('.automatorwp-automation-item');
418 var option_form = item.find('.automatorwp-option-form-container[data-option="user"]');
419
420 // Show the choices again
421 $('.automatorwp-anonymous-user-choices').slideDown('fast');
422
423 // Update the select value
424 option_form.find('.cmb2-id-run-actions-on select').val('').trigger('change');
425 });
426
427 // -----------------------------------------------
428 // Add Trigger/Action
429 // -----------------------------------------------
430
431 // Add trigger/action
432 $('body').on('click', '.automatorwp-add-trigger, .automatorwp-add-action', function(e) {
433 e.preventDefault();
434
435 var type = $(this).hasClass('automatorwp-add-trigger') ? 'trigger' : 'action';
436 var new_item = $('<div class="automatorwp-automation-item automatorwp-' + type + '" style="display: none;">'
437 + $(this).closest('.inside').find('.automatorwp-add-item-form').html()
438 + '</div>');
439
440 new_item.appendTo('.automatorwp-' + type + 's');
441
442 automatorwp_filter_selector( new_item.find('.automatorwp-all-integrations-choices') );
443
444 new_item.slideDown('fast');
445 });
446
447 // Select an integration
448 $('body').on('click', '.automatorwp-automation-item .automatorwp-select-integration .automatorwp-integration', function(e) {
449 e.preventDefault();
450
451 var item = $(this).closest('.automatorwp-automation-item');
452 var all_items_select = item.find('select.automatorwp-all-integrations-choices');
453
454 // Bail if all items select is already adding an item
455 if( all_items_select.prop('disabled') ) {
456 return;
457 }
458
459 var select = item.find('.automatorwp-integration-choices[data-integration="' + $(this).data('integration') + '"]');
460
461 // Hide the integration select
462 item.find('.automatorwp-select-integration').hide();
463
464 // Update integration icon and label
465 item.find('.automatorwp-automation-item-details .automatorwp-integration-icon').append('<img src="' + $(this).data('icon') + '" />');
466 item.find('.automatorwp-automation-item-content').prepend('<div class="automatorwp-integration-label">' + $(this).data('label') + '</div>');
467
468 // Show the trigger select
469 select.show();
470 item.find('.automatorwp-integration-choices-container').show();
471
472 $('<div class="automatorwp-select2-dropdown-container"></div>').insertAfter(select);
473
474 var dropdown_container = select.next('.automatorwp-select2-dropdown-container');
475
476 select.automatorwp_select2( {
477 theme: 'default automatorwp-select2',
478 placeholder: select.data('placeholder'),
479 allowClear: false,
480 multiple: false,
481 closeOnSelect: false,
482 dropdownParent: dropdown_container,
483 matcher: automatorwp_select2_words_matcher,
484 escapeMarkup: function(markup) { return markup; },
485 templateResult: function(data) {
486 // Custom template result to allow HTML markup on select items
487 var text = data.text;
488
489 if( data.element !== undefined ) {
490
491 // Check if option has something on data-text attribute
492 var custom_text = $(data.element).data('text');
493
494 if( custom_text !== undefined && custom_text.length ) {
495 text = custom_text;
496 }
497
498 }
499
500 return text;
501 },
502 templateSelection: function(data) { return data.text; }
503 } ).on('select2:open', function(e) {
504 // Set the search input placeholder attribute
505 dropdown_container.find('input.select2-search__field').attr('placeholder', select.data('placeholder'));
506 }).on('select2:closing', function(e) {
507 // Prevent to close dropdown
508 e.preventDefault();
509 }).on('select2:closed', function(e) {
510 // Prevent to close dropdown
511 select.select2('open');
512 }).on('select2:selecting', function (e) {
513 // Prevent change value if disabled
514 if ( select.prop('disabled') ) {
515 e.preventDefault();
516 }
517 });
518
519 select.automatorwp_select2( 'open' );
520 });
521
522 // Select a trigger/action from all integrations input
523 $('body').on('change', '.automatorwp-automation-item .automatorwp-all-integrations-choices', function(e) {
524 e.preventDefault();
525 automatorwp_add_new_item_from_select( $(this) );
526 });
527
528 // Select a trigger/action
529 $('body').on('change', '.automatorwp-automation-item .automatorwp-integration-choices', function(e) {
530 e.preventDefault();
531 automatorwp_add_new_item_from_select( $(this) );
532 });
533
534 // Cancel select a trigger/action
535 $('body').on('click', '.automatorwp-automation-item .automatorwp-cancel-choice-select', function(e) {
536 e.preventDefault();
537
538 var item = $(this).closest('.automatorwp-automation-item');
539 var type = ( item.hasClass('automatorwp-trigger') ? 'trigger' : 'action' );
540
541 if( type === 'trigger' && $('body.edit-anonymous-automation').length ) {
542 // Show the integration select
543 item.find('.automatorwp-select-integration').show();
544
545 // Update integration icon and label
546 item.find('.automatorwp-automation-item-details .automatorwp-integration-icon img').remove();
547 item.find('.automatorwp-automation-item-content .automatorwp-integration-label').remove();
548
549 // Hide the trigger select
550 item.find('.automatorwp-integration-choices-container').hide();
551
552 // Remove choices dropdown
553 $('.automatorwp-select2-dropdown-container').remove();
554
555 return;
556 }
557
558 item.slideUp('fast', function() {
559 item.remove();
560 });
561 });
562
563 // On click recommended integrations
564 $('body').on('click', '.automatorwp-recommended-integrations-label a', function(e) {
565 e.preventDefault();
566
567 $(this).closest('.automatorwp-recommended-integrations').find('.automatorwp-integrations').slideToggle('fast');
568 });
569
570 // -----------------------------------------------
571 // Add Filter
572 // -----------------------------------------------
573
574 $('body').on('click', '.automatorwp-add-filter', function(e) {
575
576 var button = $(this);
577 var box = button.closest('.postbox');
578 var items_list = box.find('.automatorwp-automation-items');
579 var automation_id = $('input#object_id').val();
580 var item_type = ( box.attr('id') === 'automatorwp_triggers' ? 'trigger' : 'action' );
581 var type = 'filter';
582
583 var last_position = items_list.find('.automatorwp-automation-item input.position').last();
584 var position = 0;
585
586 if( last_position.length ) {
587 position = parseInt( last_position.val() ) + 1;
588 } else {
589 position = parseInt( items_list.find('.automatorwp-automation-item').length - 1 );
590 }
591
592 // Disable button
593 button.prop('disabled', true);
594 // Show spinner
595 box.find('.automatorwp-box-spinner').show();
596
597 $.ajax({
598 url: ajaxurl,
599 method: 'POST',
600 data: {
601 action: 'automatorwp_add_automation_item',
602 nonce: automatorwp_admin.nonce,
603 automation_id: automation_id,
604 item_type: item_type,
605 type: type,
606 position: position
607 },
608 success: function( response ) {
609
610 // Enable button
611 button.prop('disabled', false);
612 // Hide spinner
613 box.find('.automatorwp-box-spinner').hide();
614
615 if( response.success ) {
616 var new_item = $( response.data.edit_html );
617
618 // Append the new item
619 new_item.appendTo( items_list );
620
621 // If sequential enabled, show the item position
622 if( item_type === 'trigger' && $('#sequential').prop('checked') ) {
623 // Update items position
624 automatorwp_update_items_position( items_list );
625
626 items_list.find('.automatorwp-automation-item:not(.automatorwp-automation-item-filter) .automatorwp-automation-item-position').show();
627 }
628
629 // Add tags to all tags selectors
630 if( (item_type === 'trigger' || item_type === 'action') && response.data.tags_html.length ) {
631 $( response.data.tags_html ).appendTo('.automatorwp-automation-tag-selector');
632 }
633
634 // Make javascript features work on the new item
635 automatorwp_initialize_form_fields( new_item );
636
637 } else {
638
639 }
640 },
641 error: function( response ) {
642
643 }
644 });
645 });
646
647 // -----------------------------------------------
648 // Options
649 // -----------------------------------------------
650
651 // Click on an option
652 $('body').on('click', '.automatorwp-automation-item-label > .automatorwp-option', function(e) {
653
654 var item = $(this).closest('.automatorwp-automation-item');
655 var option = $(this).data('option');
656 var option_form = item.find('.automatorwp-option-form-container[data-option="' + option + '"]');
657
658 if( option_form.hasClass('automatorwp-option-form-active') ) {
659
660 // Hide this option form
661 option_form.removeClass('automatorwp-option-form-active').slideUp('fast');
662
663 } else {
664
665 // Hide any other option form
666 item.find('.automatorwp-option-form-active').removeClass('automatorwp-option-form-active').slideUp('fast');
667
668 // Show this option form
669 option_form.addClass('automatorwp-option-form-active').slideDown('fast');
670
671 }
672
673 });
674
675 // Save option
676 $('body').on('click', '.automatorwp-save-option-form', function(e) {
677
678 var button = $(this);
679
680 if( button.prop('disabled') ) {
681 return;
682 }
683
684 var item = button.closest('.automatorwp-automation-item');
685 var form = button.closest('.automatorwp-option-form-container');
686 var id = item.find('input.id').val();
687 var item_type = item.hasClass('automatorwp-trigger') ? 'trigger' : 'action';
688
689 // Show spinner
690 form.find('.automatorwp-spinner').show();
691
692 // Disable save and cancel button
693 button.prop('disabled', true);
694 button.next('.automatorwp-cancel-option-form').prop('disabled', true);
695
696 // Get the option form values
697 var data = automatorwp_get_option_form_values( form );
698
699 // Append the request data
700 data.action = 'automatorwp_update_item_option';
701 data.nonce = automatorwp_admin.nonce;
702 data.id = id;
703 data.item_type = item_type;
704 data.option_name = form.data('option');
705
706 $.ajax({
707 url: ajaxurl,
708 method: 'POST',
709 data: data,
710 success: function( response ) {
711
712 if( response.success ) {
713
714 // Hide the option form
715 form.removeClass('automatorwp-option-form-active').slideUp('fast');
716
717 // Override the item content HTML (used for filters)
718 if( response.data.item_html !== undefined ) {
719
720 if( item.hasClass('automatorwp-automation-item-filter') && item.data('update-content') === 'true' ) {
721
722 var item_content = $(response.data.item_html).filter('.automatorwp-automation-item').find('.automatorwp-automation-item-content').html();
723
724 item.find('.automatorwp-automation-item-content').html( item_content );
725
726 // Make javascript features work on the new item
727 automatorwp_initialize_form_fields( item );
728
729 item.data('update-content', 'false');
730 }
731 }
732
733 // Update the item label
734 item.find('.automatorwp-automation-item-label').html( response.data.edit_html );
735
736 if( item_type === 'trigger' || item_type === 'action' ) {
737 // Update the option display on tags selector
738 $('.automatorwp-automation-tag-selector optgroup[data-id="' + id + '"]').attr('label', response.data.edit_html );
739
740 // Update option tags
741 if( response.data.tags_html.length ) {
742 $('.automatorwp-automation-tag-selector optgroup[data-id="' + id + '"]').html( $(response.data.tags_html).html() );
743
744 // To get the options correctly updated and working is required to destroy all Select2 and reinitialize them
745 $('.automatorwp-automation-tag-selector').each(function() {
746 $(this).automatorwp_select2('destroy');
747
748 automatorwp_tags_selector( $(this) );
749 });
750 }
751 }
752
753 }
754
755 // Hide the spinner
756 form.find('.automatorwp-spinner').hide();
757
758 // Enable save and cancel button
759 button.prop('disabled', false);
760 button.next('.automatorwp-cancel-option-form').prop('disabled', false);
761
762 },
763 error: function( response ) {
764
765 }
766 });
767
768 });
769
770 // Cancel option edit
771 $('body').on('click', '.automatorwp-cancel-option-form', function(e) {
772
773 // Hide the option form
774 $(this).closest('.automatorwp-option-form-container').removeClass('automatorwp-option-form-active').slideUp('fast');
775
776 });
777
778 // On change filter field
779 $('body').on('change', '.automatorwp-automation-item-filter .cmb2-id-filter select', function(e) {
780
781 var item = $(this).closest('.automatorwp-automation-item-filter');
782
783 item.data('update-content', 'true' );
784 });
785
786 // -----------------------------------------------
787 // Options fields
788 // -----------------------------------------------
789
790 // Make radios work through javascript
791 $('body').on('change', '.automatorwp-option-form input[type="radio"]', function(e) {
792 $(this).closest('ul').find('input[type="radio"]:not([value="' + $(this).val() + '"])').prop('checked', false);
793 });
794
795 // Fix radio label click
796 $('body').on('click', '.automatorwp-option-form input[type="radio"] + label', function(e) {
797 e.preventDefault();
798 $(this).prev('input').prop('checked', true).trigger('change');
799 });
800
801 // Fix checkbox label click
802 $('body').on('click', '.automatorwp-option-form input[type="checkbox"] + label', function(e) {
803 e.preventDefault();
804 $(this).prev('input').prop( 'checked', ( ! $(this).prev('input').prop('checked') ) ).trigger('change');
805 });
806
807 // Fix repeatable fields selector
808 $('.automatorwp-option-form .cmb-repeat-table').each(function() {
809 var item_id = $(this).closest('.automatorwp-automation-item').find('input.id').val();
810 var new_id = $(this).attr('id') + '-' + item_id;
811
812 $(this).attr('id', new_id);
813 $(this).parent().find('.cmb-add-row-button').attr('data-selector', new_id);
814 });
815
816 // Fix group fields selector
817 $('.automatorwp-option-form .cmb-repeatable-group').each(function() {
818 var item_id = $(this).closest('.automatorwp-automation-item').find('input.id').val();
819 var new_id = $(this).attr('id') + '-' + item_id;
820
821 $(this).attr('id', new_id);
822 $(this).find('.cmb-add-group-row').attr('data-selector', new_id);
823 $(this).find('.cmb-remove-group-row').attr('data-selector', new_id);
824 });
825
826 // On add a new group row, reinitialize fields
827 $('body').on('cmb2_add_row', '.cmb-repeatable-group', function( e, row, cmb ) {
828
829 // Remove Select2 element
830 row.find('.select2').remove();
831
832 // Find all Select2 elements
833 var select2_elements = row.find('.select2-hidden-accessible');
834
835 // Reset Select2 data on options
836 select2_elements.find('optgroup, option')
837 .removeAttr('id')
838 .removeAttr('data-select2-id'); // For options and group options, select2 assigns this attribute as id
839
840 // Reset Select2 data on the input element
841 select2_elements
842 .removeClass('select2-hidden-accessible')
843 .removeAttr('id')
844 .removeAttr('data-select2-id'); // For fields without id, select2 assigns this attribute as id
845
846 automatorwp_initialize_form_fields( row );
847 });
848
849 // -----------------------------------------------
850 // Tags
851 // -----------------------------------------------
852
853 // Tags Selector Control
854 $('.automatorwp-automation-tag-selector').each(function() { automatorwp_tags_selector( $(this) ); });
855
856 // On focus an action option input
857 $('body').on('focus', '.automatorwp-action input, .automatorwp-action textarea', function(e) {
858 $(this).data('focused', true);
859 });
860
861 // On change tag selector
862 $('body').on('change', '.automatorwp-automation-tag-selector', function(e) {
863 var $this = $(this);
864 var input = $this.parent().find( 'input, textarea' );
865 var value = $this.val();
866
867 if( value && value.length ) {
868 value = '{' + value + '}';
869
870 // Support for wysiwyg field
871 if( $this.closest('.cmb-row').hasClass('cmb-type-wysiwyg') ) {
872
873 input = $this.parent().find( 'textarea.wp-editor-area' );
874
875 var editor = window.tinyMCE.get( input.attr('id') );
876
877 if ( editor && ! editor.isHidden() ) {
878 // Insert content to the HTML editor
879 editor.execCommand( 'mceInsertContent', false, value );
880
881 return;
882 } else if ( typeof QTags !== 'undefined' ) {
883 // If quick tags are available, insert the HTML into its content.
884 QTags.insertContent( value );
885
886 return;
887 }
888 }
889
890 // if input hasn't been focused yet, then set caret at end
891 if( input.data('focused') !== true ) {
892 automatorwp_set_caret_at_end( input );
893 }
894
895 // Insert content at caret
896 automatorwp_insert_at_caret( input, value );
897
898 // Restore tag selector value to allow select again the same option
899 $this.val(null).trigger('change');
900
901 }
902 });
903
904 // Make tags selector dropdown option groups toggleables
905 $('body').on('click', '.automatorwp-tags-select2 .select2-dropdown li[role="group"] > strong', function(e) {
906 $(this).next('ul').slideToggle('fast');
907 });
908
909 // -----------------------------------------------
910 // Taxonomy Selector
911 // -----------------------------------------------
912
913 // Change the taxonomy
914 $('body').on('change', '.automatorwp-taxonomy-selector select', function(e) {
915 var row = $(this).closest('.cmb-row');
916 var term_row = row.next('.automatorwp-term-selector');
917
918 // Bail if next field is not a term selector
919 if( term_row === undefined ) {
920 return;
921 }
922
923 var taxonomy = $(this).val();
924 var first_change = row.hasClass('is-option-change');
925
926 if( taxonomy === 'any' || taxonomy === '' ) {
927 // Hide the term selector
928 if( first_change ) {
929 term_row.hide();
930 } else {
931 term_row.slideUp('fast');
932 }
933 } else {
934 var term_selector = term_row.find('select.select2-hidden-accessible');
935 var taxonomy_label = automatorwp_get_taxonomy_label( taxonomy );
936
937 if( ! taxonomy_label.length ) {
938 taxonomy_label = 'Category';
939 }
940
941 // Remove Select2 element
942 term_selector.next('.select2').remove();
943
944 // Update the taxonomy and placeholder
945 term_selector.data( 'taxonomy', taxonomy );
946 term_selector.data( 'placeholder', automatorwp_admin_functions.taxonomy_selector_placeholder_pattern.replace('%s', taxonomy_label.toLowerCase() ) );
947
948 if( ! first_change ) {
949 // Update the the term value
950 term_selector.val('');
951 }
952
953 // Reset the selector
954 term_selector.removeAttr('data-select2-id');
955
956 // Init it again
957 automatorwp_term_selector( term_selector );
958
959 // Show the term selector
960 if( first_change ) {
961 term_row.show();
962 } else {
963 term_row.slideDown('fast');
964 }
965 }
966
967 row.removeClass('is-option-change');
968 });
969
970 // On click on an option, check if form contains the taxonomy selector
971 $('body').on('click', '.automatorwp-automation-item-label > .automatorwp-option', function(e) {
972
973 var item = $(this).closest('.automatorwp-automation-item');
974 var option = $(this).data('option');
975 var option_form = item.find('.automatorwp-option-form-container[data-option="' + option + '"]');
976 var taxonomy_selector = option_form.find('.automatorwp-taxonomy-selector');
977
978 if( taxonomy_selector !== undefined ) {
979 taxonomy_selector.addClass('is-option-change');
980 taxonomy_selector.find('select.select2-hidden-accessible').trigger('change');
981 }
982
983 });
984
985 // -----------------------------------------------
986 // Selector Custom Input
987 // -----------------------------------------------
988
989 // Change the selector
990 $('body').on('change', 'select', function(e) {
991
992 var row = $(this).closest('.cmb-row');
993 var custom_input_row = row.next('.automatorwp-selector-custom-input');
994
995 // Bail if next field is not a term selector
996 if( custom_input_row === undefined ) {
997 return;
998 }
999
1000 var first_change = row.hasClass('is-option-change');
1001
1002 var value = $(this).val();
1003
1004 if( value === 'custom' || ( Array.isArray( value ) && value.includes( 'custom' ) ) ) {
1005 // Show the custom input
1006 if( first_change ) {
1007 custom_input_row.show();
1008 } else {
1009 custom_input_row.slideDown('fast');
1010 }
1011 } else {
1012 // Hide the custom input
1013 if( first_change ) {
1014 custom_input_row.hide();
1015 } else {
1016 custom_input_row.slideUp('fast');
1017 }
1018 }
1019
1020 row.removeClass('is-option-change');
1021 });
1022
1023 // On click on an option, check if form contains a selector custom input
1024 $('body').on('click', '.automatorwp-automation-item-label > .automatorwp-option', function(e) {
1025
1026 var item = $(this).closest('.automatorwp-automation-item');
1027 var option = $(this).data('option');
1028 var option_form = item.find('.automatorwp-option-form-container[data-option="' + option + '"]');
1029 var custom_input_row = option_form.find('.automatorwp-selector-custom-input');
1030
1031 if( custom_input_row !== undefined ) {
1032
1033 var selector_row = custom_input_row.prev('.cmb-row');
1034
1035 if( selector_row !== undefined ) {
1036 selector_row.addClass('is-option-change');
1037 selector_row.find('select.select2-hidden-accessible').trigger('change');
1038 }
1039 }
1040
1041 });
1042
1043 // Initial status for filters (where form is always visible)
1044 $('.automatorwp-selector-custom-input').each(function() {
1045 var selector_row = $(this).prev('.cmb-row');
1046
1047 if( selector_row.find('select').val() === 'custom' ) {
1048 $(this).show();
1049 }
1050 });
1051
1052 // -----------------------------------------------
1053 // Dialog
1054 // -----------------------------------------------
1055
1056 // Dialog cancel button
1057 $('body').on('click', '.automatorwp-dialog-cancel', function(e) {
1058
1059 var target = $(this).data('dialog');
1060
1061 if( target.length ) {
1062 $('.' + target).dialog('close');
1063 }
1064 });
1065
1066 // -----------------------------------------------
1067 // Automation type dialog
1068 // -----------------------------------------------
1069
1070 // Show automation type dialog on new automation links
1071 $('body').on('click', 'a[href$="admin.php?page=add_automatorwp_automations"]', function(e) {
1072 e.preventDefault();
1073
1074 $('.automatorwp-automation-type-dialog').dialog({
1075 dialogClass: 'automatorwp-dialog',
1076 closeText: '',
1077 show: { effect: 'fadeIn', duration: 200 },
1078 hide: { effect: 'fadeOut', duration: 200 },
1079 resizable: false,
1080 height: 'auto',
1081 width: 800,
1082 modal: true,
1083 draggable: false,
1084 closeOnEscape: false,
1085 });
1086
1087 });
1088
1089 // Select automation type
1090 $('body').on('click', '.automatorwp-automation-type-dialog .automatorwp-automation-type', function(e) {
1091 $('.automatorwp-automation-type-dialog .automatorwp-automation-type-selected').removeClass('automatorwp-automation-type-selected');
1092 $(this).addClass('automatorwp-automation-type-selected');
1093 });
1094
1095 // Automation type dialog confirm button
1096 $('body').on('click', '.automatorwp-automation-type-dialog .automatorwp-automation-type-dialog-confirm', function(e) {
1097
1098 var $this = $(this);
1099
1100 if( $this.hasClass('disabled') ) {
1101 return;
1102 }
1103
1104 // Disable the button
1105 $this.addClass('disabled');
1106
1107 $this.closest('.automatorwp-dialog-buttons').prepend('<span class="spinner is-active" style="float: none;"></span>');
1108
1109 var type = 'user';
1110 var selected = $('.automatorwp-automation-type-dialog .automatorwp-automation-type-selected');
1111
1112 if( selected.length ) {
1113 type = selected.data('type');
1114 }
1115
1116 var url = window.location.href.split('admin.php')[0];
1117
1118 // Redirect to the add new automation
1119 window.location.href = url + 'admin.php?page=add_automatorwp_automations&type=' + type;
1120
1121 });
1122
1123 // -----------------------------------------------
1124 // Automation URL export dialog
1125 // -----------------------------------------------
1126
1127 var url_export_id = 0;
1128 var url_export_url = '';
1129
1130 // Show automation url export dialog on export links
1131 $('body').on('click', 'a.automatorwp-url-export-automation', function(e) {
1132 e.preventDefault();
1133
1134 url_export_id = automatorwp_get_url_param( $(this).attr('href'), 'id', 0 );
1135 url_export_url = '';
1136
1137 // Hide the URL
1138 $('.automatorwp-automation-url-export-dialog input').val('');
1139 $('.automatorwp-automation-url-export-dialog .automatorwp-automation-url-export-dialog-url').hide();
1140 $('.automatorwp-automation-url-export-dialog .automatorwp-automation-url-export-dialog-input .automatorwp-notice-error').hide();
1141
1142 // Update confirm button label
1143 $('.automatorwp-automation-url-export-dialog-confirm .automatorwp-automation-url-export-dialog-confirm-default-text').show();
1144 $('.automatorwp-automation-url-export-dialog-confirm .automatorwp-automation-url-export-dialog-confirm-generating-text').hide();
1145 $('.automatorwp-automation-url-export-dialog-confirm .automatorwp-automation-url-export-dialog-confirm-success-text').hide();
1146
1147 $('.automatorwp-automation-url-export-dialog').dialog({
1148 dialogClass: 'automatorwp-dialog',
1149 closeText: '',
1150 show: { effect: 'fadeIn', duration: 200 },
1151 hide: { effect: 'fadeOut', duration: 200 },
1152 resizable: false,
1153 height: 'auto',
1154 width: 600,
1155 modal: true,
1156 draggable: false,
1157 closeOnEscape: false,
1158 });
1159
1160 });
1161
1162 $('body').on('keypress', '.automatorwp-automation-url-export-dialog-input input', function(e) {
1163
1164 var keycode = ( e.keyCode ? e.keyCode : e.which );
1165
1166 if( parseInt( keycode ) === 13 ) {
1167 $('.automatorwp-automation-url-export-dialog-confirm').trigger('click');
1168 }
1169 });
1170
1171 // Automation url export confirm button
1172 $('body').on('click', '.automatorwp-automation-url-export-dialog-confirm', function(e) {
1173
1174 var button = $(this);
1175 var dialog = button.closest('.automatorwp-automation-url-export-dialog');
1176 dialog.find('.automatorwp-automation-url-export-dialog-input .automatorwp-notice-error').slideUp('fast');
1177
1178 // Close dialog if button display the success text
1179 if( button.find('.automatorwp-automation-url-export-dialog-confirm-success-text').is(":visible") ) {
1180 dialog.dialog('close');
1181 return;
1182 }
1183
1184 // Bail if button is disabled
1185 if( button.prop( 'disabled' ) ) {
1186 return;
1187 }
1188
1189 // Bail if no automation ID provided
1190 if( url_export_id === 0 ) {
1191 return;
1192 }
1193
1194 // Check the URL
1195 var url = dialog.find('.automatorwp-automation-url-export-dialog-input input').val();
1196
1197 // Only check url if not is a local or development URL
1198 if( ! ( url.includes('dev') || url.includes('127.0.0.') || url.includes('localhost') || url.includes(':8888') ) ) {
1199 if( ! automatorwp_is_valid_url( url ) ) {
1200 dialog.find('.automatorwp-automation-url-export-dialog-input .automatorwp-notice-error').slideDown('fast');
1201 return;
1202 }
1203 }
1204
1205 if( ! url.startsWith('http') ) {
1206 url = 'https://' + url;
1207 }
1208
1209 button.prop( 'disabled', true );
1210
1211 // Update confirm button label
1212 button.find('.automatorwp-automation-url-export-dialog-confirm-default-text').hide();
1213 button.find('.automatorwp-automation-url-export-dialog-confirm-generating-text').show();
1214 button.find('.automatorwp-automation-url-export-dialog-confirm-success-text').hide();
1215
1216 $.ajax({
1217 url: ajaxurl,
1218 method: 'POST',
1219 data: {
1220 action: 'automatorwp_get_automation_export_url',
1221 nonce: automatorwp_admin.nonce,
1222 automation_id: url_export_id,
1223 },
1224 success: function( response ) {
1225
1226 // Update the URL
1227 if( ! url.endsWith('/') ) {
1228 url += '/';
1229 }
1230
1231 url_export_url = response.data;
1232
1233 url += url_export_url;
1234
1235 var url_input = dialog.find('.automatorwp-automation-url-export-dialog-url input');
1236
1237 url_input.val(url);
1238
1239 // Show the URL input
1240 dialog.find('.automatorwp-automation-url-export-dialog-url').slideDown('fast', function() {
1241 url_input.trigger('focus');
1242 url_input.trigger('select');
1243 url_input[0].setSelectionRange(0, 99999);
1244 });
1245
1246 button.prop( 'disabled', false );
1247
1248 // Update confirm button label
1249 button.find('.automatorwp-automation-url-export-dialog-confirm-default-text').hide();
1250 button.find('.automatorwp-automation-url-export-dialog-confirm-generating-text').hide();
1251 button.find('.automatorwp-automation-url-export-dialog-confirm-success-text').show();
1252
1253 },
1254 error: function( response ) {
1255
1256 button.prop( 'disabled', false );
1257
1258 // Update confirm button label
1259 button.find('.automatorwp-automation-url-export-dialog-confirm-default-text').show();
1260 button.find('.automatorwp-automation-url-export-dialog-confirm-generating-text').hide();
1261 button.find('.automatorwp-automation-url-export-dialog-confirm-success-text').hide();
1262
1263 }
1264 });
1265
1266 });
1267
1268 // Force select on click on the URL input
1269 $('body').on('click', '.automatorwp-automation-url-export-dialog-url input', function(e) {
1270 $(this).trigger('select');
1271 $(this)[0].setSelectionRange(0, 99999); // For mobile devices
1272 });
1273
1274 // Copy URL
1275 $('body').on('click', '.automatorwp-automation-url-export-dialog-url-copy', function(e) {
1276 var input = $('.automatorwp-automation-url-export-dialog-url input');
1277
1278 input.trigger('select');
1279 input[0].setSelectionRange(0, 99999); // For mobile devices
1280
1281 document.execCommand("copy");
1282
1283 $(this).find('.automatorwp-automation-url-export-dialog-url-copy-text-copy').hide();
1284 $(this).find('.automatorwp-automation-url-export-dialog-url-copy-text-copied').show();
1285 });
1286
1287 // -----------------------------------------------
1288 // All users automation
1289 // -----------------------------------------------
1290
1291 // Toggle visibility on schedule run checkbox change
1292 $('body').on('change', '#automatorwp-automations-execution-options input#schedule_run', function(e) {
1293 var form = $(this).closest('.cmb2-metabox');
1294 var target = form.find('.cmb2-id-schedule-run-datetime');
1295
1296 if( $(this).prop('checked') ) {
1297 // Force uncheck recurring run checkbox
1298 if( form.find('.cmb2-id-recurring-run input').prop('checked') ) {
1299 form.find('.cmb2-id-recurring-run input').prop( 'checked', false ).trigger('change');
1300 }
1301
1302 target.slideDown('fast');
1303 } else {
1304 target.slideUp('fast');
1305 }
1306 });
1307
1308 if( ! $('#automatorwp-automations-execution-options input#schedule_run').prop('checked') ) {
1309 $('#automatorwp-automations-execution-options .cmb2-id-schedule-run-datetime').hide();
1310 }
1311
1312 // Toggle visibility on recurring run checkbox change
1313 $('body').on('change', '#automatorwp-automations-execution-options input#recurring_run', function(e) {
1314 var form = $(this).closest('.cmb2-metabox');
1315 var target = form.find('.automatorwp-recurring-run-day-label, .cmb2-id-recurring-run-period, .cmb2-id-recurring-run-time');
1316
1317 form.find('.cmb2-id-recurring-run-period select').trigger('change');
1318
1319 if( $(this).prop('checked') ) {
1320 // Force uncheck schedule run checkbox
1321 if( form.find('.cmb2-id-schedule-run input').prop('checked') ) {
1322 form.find('.cmb2-id-schedule-run input').prop( 'checked', false ).trigger('change');
1323 }
1324
1325 target.slideDown('fast');
1326 } else {
1327 target.slideUp('fast');
1328 }
1329 });
1330
1331 if( ! $('#automatorwp-automations-execution-options input#recurring_run').prop('checked') ) {
1332 $('#automatorwp-automations-execution-options').find('.automatorwp-recurring-run-day-label, .cmb2-id-recurring-run-period, .cmb2-id-recurring-run-time').hide();
1333 }
1334
1335 // Toggle visibility on recurring run period change
1336 $('body').on('change', '#automatorwp-automations-execution-options select#recurring_run_period', function(e) {
1337 var form = $(this).closest('.cmb2-metabox');
1338 var target = form.find('.cmb2-id-recurring-run-day');
1339
1340 // Bail if recurring run not checked
1341 if( ! form.find('input#recurring_run').prop('checked') ) {
1342 target.slideUp('fast');
1343 return;
1344 }
1345
1346 if( $(this).val() !== 'day' ) {
1347 target.slideDown('fast');
1348 } else {
1349 target.slideUp('fast');
1350 }
1351
1352 // Update maximum allowed values for the day input
1353 var day_input = form.find('.cmb2-id-recurring-run-day input#recurring_run_day')
1354 var max_day = 365;
1355
1356 switch( $(this).val() ) {
1357 case 'week':
1358 max_day = 7;
1359 break;
1360 case 'month':
1361 max_day = 31;
1362 break;
1363 case 'year':
1364 default:
1365 max_day = 365;
1366 break;
1367 }
1368
1369 if( parseInt( day_input.val() ) > max_day ) {
1370 day_input.val( max_day );
1371 }
1372
1373 day_input.attr( 'max', max_day );
1374 });
1375
1376 if( $('#automatorwp-automations-execution-options select#recurring_run_period').val() === 'day' ) {
1377 $('#automatorwp-automations-execution-options .cmb2-id-recurring-run-day').hide();
1378 }
1379
1380 $('#automatorwp-automations-execution-options select#recurring_run_period').trigger('change');
1381
1382 // Run automation button
1383 $('body').on('click', '.automatorwp-run-automation button', function(e) {
1384
1385 var $this = $(this);
1386
1387 if ( $this.prop('disabled') ) {
1388 return;
1389 }
1390
1391 var automation_id = $('input#object_id').val();
1392 var automation_type = $('select#type').val();
1393
1394 // Used for automation loop
1395 var items_per_loop = 0;
1396 var items_per_loop_input = $('input#items_per_loop');
1397
1398 if( items_per_loop_input.length ) {
1399 items_per_loop = items_per_loop_input.val();
1400 }
1401
1402 // Disable the button and switch labels
1403 $this.prop('disabled', true);
1404 $this.find('.automatorwp-run-automation-run-label').hide();
1405 $this.find('.automatorwp-run-automation-running-label').show();
1406 $('.automatorwp-run-automation').addClass('automatorwp-is-running');
1407
1408 // Show the cancel button
1409 $('.automatorwp-cancel-automation-run').show();
1410 // Update the cancel automation run flag
1411 automatorwp_cancel_automation_run = false;
1412
1413 // Remove any error notice
1414 $this.closest('#major-publishing-actions').find('.automatorwp-notice-error').remove();
1415
1416 // Reset the progress
1417 $('.automatorwp-run-automation-progress').removeClass('automatorwp-run-automation-progress-completed');
1418 $('.automatorwp-run-automation-progress .automatorwp-run-automation-progress-current-progress').css({ width: '0%' });
1419 $('.automatorwp-run-automation-progress-text').text('0/0');
1420
1421 // Show the progress
1422 $('.automatorwp-run-automation-progress').slideDown('fast');
1423
1424 automatorwp_run_automation( automation_id, items_per_loop );
1425 });
1426
1427 if( $('.automatorwp-run-automation.automatorwp-is-running').length ) {
1428 var automation_id = $('input#object_id').val();
1429 var automation_type = $('select#type').val();
1430
1431 // Used for automation loop
1432 var items_per_loop = 0;
1433 var items_per_loop_input = $('input#items_per_loop');
1434
1435 if( items_per_loop_input.length ) {
1436 items_per_loop = items_per_loop_input.val();
1437 }
1438
1439 automatorwp_run_automation( automation_id, items_per_loop );
1440 }
1441
1442 // Cancel automation run button
1443 $('body').on('click', '.automatorwp-cancel-automation-run button', function(e) {
1444
1445 var $this = $(this);
1446
1447 if ( $this.prop('disabled') ) {
1448 return;
1449 }
1450
1451 // Disable the button and switch labels
1452 $this.prop('disabled', true);
1453 $this.find('.automatorwp-cancel-automation-run-cancel-label').hide();
1454 $this.find('.automatorwp-cancel-automation-run-cancelling-label').show();
1455
1456 automatorwp_cancel_automation_run = true;
1457
1458 });
1459
1460 })( jQuery );
1461
1462 /**
1463 * Add new item from select
1464 *
1465 * @since 1.0.0
1466 *
1467 * @param {Object} select
1468 */
1469 function automatorwp_add_new_item_from_select( select ) {
1470
1471 var $ = $ || jQuery;
1472
1473 if( select.prop('disabled') ) {
1474 return;
1475 }
1476
1477 var item = select.closest('.automatorwp-automation-item');
1478 var items_list = item.closest('.automatorwp-automation-items');
1479 var automation_id = $('input#object_id').val();
1480 var type = select.val();
1481 var item_type = item.hasClass('automatorwp-trigger') ? 'trigger' : 'action';
1482
1483 if( ! type.length ) {
1484 return;
1485 }
1486
1487 var last_position = items_list.find('.automatorwp-automation-item input.position').last();
1488 var position = 0;
1489
1490 if( last_position.length ) {
1491 position = parseInt( last_position.val() ) + 1;
1492 } else {
1493 position = parseInt( items_list.find('.automatorwp-automation-item').length - 1 );
1494 }
1495
1496 // Disable the dropdown
1497 select.prop('disabled', true);
1498
1499 // Show spinner
1500 items_list.siblings('.automatorwp-spinner').show();
1501
1502 $.ajax({
1503 url: ajaxurl,
1504 method: 'POST',
1505 data: {
1506 action: 'automatorwp_add_automation_item',
1507 nonce: automatorwp_admin.nonce,
1508 automation_id: automation_id,
1509 item_type: item_type,
1510 type: type,
1511 position: position
1512 },
1513 success: function( response ) {
1514
1515 // Hide spinner
1516 items_list.siblings('.automatorwp-spinner').hide();
1517
1518 // Remove the new item form
1519 item.remove();
1520
1521 if( response.success ) {
1522
1523 var new_item = $( response.data.edit_html );
1524
1525 // Append the new item
1526 new_item.appendTo( items_list );
1527
1528 // If sequential enabled, show the item position
1529 if( item_type === 'trigger' && $('#sequential').prop('checked') ) {
1530 // Update items position
1531 automatorwp_update_items_position( items_list );
1532
1533 items_list.find('.automatorwp-automation-item:not(.automatorwp-automation-item-filter) .automatorwp-automation-item-position').show();
1534 }
1535
1536 // Add tags to all tags selectors
1537 if( (item_type === 'trigger' || item_type === 'action') && response.data.tags_html.length ) {
1538 $( response.data.tags_html ).appendTo('.automatorwp-automation-tag-selector');
1539 }
1540
1541 // Make javascript features work on the new item
1542 automatorwp_initialize_form_fields( new_item );
1543
1544 } else {
1545
1546 }
1547 },
1548 error: function( response ) {
1549
1550 }
1551 });
1552
1553 }
1554
1555 /**
1556 * Update elements positions
1557 *
1558 * @since 1.0.0
1559 *
1560 * @param {Object} list
1561 */
1562 function automatorwp_update_items_position( list ) {
1563
1564 var $ = $ || jQuery;
1565
1566 var items = list.find('.automatorwp-automation-item');
1567 var items_order = {};
1568 var current_position = 0;
1569
1570 // Loop through each item
1571 items.each(function( index, value ) {
1572
1573 // Write it's current position to our hidden input value
1574 $(this).find('input.position').val( index );
1575
1576 if( ! $(this).hasClass('automatorwp-automation-item-filter') ) {
1577 // Update item position
1578 $(this).find('.automatorwp-automation-item-position').html( ( current_position + 1 ) + '.');
1579
1580 current_position++;
1581 }
1582
1583 items_order[$(this).find('input.id').val()] = index;
1584
1585 });
1586
1587 if( Object.entries(items_order).length ) {
1588
1589 var item_type = list.hasClass('automatorwp-triggers') ? 'trigger' : 'action';
1590
1591 // Update automation items order trough ajax
1592 $.ajax({
1593 url: ajaxurl,
1594 method: 'POST',
1595 data: {
1596 action: 'automatorwp_update_automation_items_order',
1597 nonce: automatorwp_admin.nonce,
1598 items_order: items_order,
1599 item_type: item_type,
1600 },
1601 success: function( response ) {
1602
1603 if( response.success ) {
1604
1605 } else {
1606
1607 }
1608 },
1609 error: function( response ) {
1610
1611 }
1612 });
1613
1614 }
1615
1616 // Update the tags order
1617 automatorwp_update_tags_selector_order();
1618
1619 }
1620
1621 /**
1622 * Update tags selector order
1623 *
1624 * @since 1.0.0
1625 */
1626 function automatorwp_update_tags_selector_order() {
1627
1628 var $ = $ || jQuery;
1629
1630 var items = $('.automatorwp-automation-item');
1631 var items_order = {};
1632 var triggers_sep = false;
1633 var actions_sep = false;
1634 var tags_index = 0;
1635 var prefix = 't:';
1636
1637 // Loop through each item
1638 items.each(function( index, value ) {
1639
1640 if( $(this).hasClass('automatorwp-trigger') ) {
1641 prefix = 't:';
1642 } else {
1643 prefix = 'a:';
1644 }
1645
1646 // Triggers separator
1647 if( ! triggers_sep && $(this).hasClass('automatorwp-trigger') ) {
1648 triggers_sep = true;
1649 items_order[tags_index] = 'triggers_sep';
1650 tags_index++;
1651 }
1652
1653 // Actions separator
1654 if( ! actions_sep && $(this).hasClass('automatorwp-action') ) {
1655 actions_sep = true;
1656 items_order[tags_index] = 'actions_sep';
1657 tags_index++;
1658 }
1659
1660 // Set up a custom array for tags
1661 items_order[tags_index] = prefix + $(this).find('input.id').val();
1662 tags_index++;
1663 });
1664
1665 if( Object.entries(items_order).length ) {
1666
1667 // Update tags selector order
1668 $.each(items_order, function( index, id ) {
1669
1670 $('.automatorwp-automation-tag-selector').each(function() {
1671
1672 if( id === 'triggers_sep' || id === 'actions_sep' ) {
1673 $(this).find('option[value="' + id + '"]').appendTo(this);
1674 } else {
1675 $(this).find('optgroup[data-id="' + id + '"]').appendTo(this);
1676 }
1677
1678 });
1679
1680 });
1681
1682 }
1683
1684 }
1685
1686 /**
1687 * Turns the given element into a tags selector
1688 *
1689 * @since 1.0.0
1690 *
1691 * @param {Object} element
1692 */
1693 function automatorwp_tags_selector( element ) {
1694
1695 var $ = $ || jQuery;
1696
1697 element.automatorwp_select2({
1698 theme: 'default automatorwp-select2 automatorwp-tags-select2',
1699 escapeMarkup: function(markup) { return markup; },
1700 templateResult: function(data) {
1701 // Custom template result to allow HTML markup on select items
1702 var text = data.text;
1703
1704 if( data.element !== undefined ) {
1705 // Check if option has something on label attribute
1706 var label = $(data.element).attr('label');
1707
1708 if( label !== undefined && label.length ) {
1709 text = label;
1710 }
1711
1712 // Check if option has something on data-text attribute
1713 var custom_text = $(data.element).data('text');
1714
1715 if( custom_text !== undefined && custom_text.length ) {
1716 text = custom_text;
1717 }
1718
1719 // Check if option has an icon
1720 var icon = $(data.element).data('icon');
1721
1722 if( icon !== undefined ) {
1723 text = '<img src="' + icon + '" />' + text;
1724 }
1725
1726 }
1727
1728 return text;
1729 },
1730 matcher: automatorwp_select2_optgroup_matcher,
1731 dropdownPosition: 'below',
1732 });
1733
1734 element.val(null).trigger('change');
1735
1736 }
1737
1738 /**
1739 * Turns the given element into a filter selector
1740 *
1741 * @since 1.0.0
1742 *
1743 * @param {Object} element
1744 */
1745 function automatorwp_filter_selector( element ) {
1746
1747 var $ = $ || jQuery;
1748
1749 element.automatorwp_select2({
1750 theme: 'default automatorwp-select2 automatorwp-filter-select2',
1751 escapeMarkup: function(markup) { return markup; },
1752 templateResult: function(data) {
1753 // Custom template result to allow HTML markup on select items
1754 var text = data.text;
1755
1756 if( data.element !== undefined ) {
1757 // Check if option has something on label attribute
1758 var label = $(data.element).attr('label');
1759
1760 if( label !== undefined && label.length ) {
1761 text = label;
1762 }
1763
1764 // Check if option has something on data-text attribute
1765 var custom_text = $(data.element).data('text');
1766
1767 if( custom_text !== undefined && custom_text.length ) {
1768 text = custom_text;
1769 }
1770
1771 // Check if option has an icon
1772 var icon = $(data.element).data('icon');
1773
1774 if( icon !== undefined ) {
1775 text = '<img src="' + icon + '" />' + text;
1776 }
1777
1778 }
1779
1780 return text;
1781 },
1782 matcher: automatorwp_select2_optgroup_matcher,
1783 });
1784
1785 }
1786
1787 /**
1788 * Get form fields values
1789 *
1790 * @since 1.0.0
1791 *
1792 * @param {Object} form
1793 *
1794 * @return {Object}
1795 */
1796 function automatorwp_get_option_form_values( form ) {
1797
1798 var $ = $ || jQuery;
1799 var data = {};
1800
1801 form.find('input:not([type="button"]):not([type="submit"]), select, textarea').each(function() {
1802
1803 var option = $(this).data('option');
1804 var value = $(this).val();
1805
1806 // Skip pattern fields
1807 if( $(this).closest('.cmb-row.empty-row').length ) {
1808 return;
1809 }
1810
1811 if( $(this).attr('type') === 'checkbox' || $(this).attr('type') === 'radio' ) {
1812
1813 if( $(this).closest('.cmb2-checkbox-list').length ) {
1814 // Multicheck
1815 value = [];
1816 $(this).closest('.cmb2-checkbox-list').find('input:checked').each(function() {
1817 value.push($(this).val());
1818 });
1819 } else if( ! $(this).prop('checked') ) {
1820 // Checkbox and radio
1821 return;
1822 }
1823
1824 } else if( $(this).closest('.cmb-row.cmb-type-text-datetime-timestamp').length ) {
1825 // Datetime Picker
1826
1827 value = {
1828 'date': $(this).closest('.cmb-row.cmb-type-text-datetime-timestamp').find('input.cmb2-datepicker').val(),
1829 'time': $(this).closest('.cmb-row.cmb-type-text-datetime-timestamp').find('input.cmb2-timepicker').val(),
1830 };
1831
1832 } else if( $(this).hasClass('wp-editor-area') ) {
1833 // Wysiwyg
1834
1835 option = $(this).attr('name');
1836
1837 // Force save tinyMCE editor to get textarea value updated
1838 if( window.tinyMCE.editors.length && window.tinyMCE.editors[$(this).attr('id')] ) {
1839 window.tinyMCE.editors[$(this).attr('id')].save();
1840 value = $(this).val();
1841 }
1842 } else if( $(this).hasClass('cmb2-upload-file') || $(this).hasClass('cmb2-upload-file-id') ) {
1843 // File field
1844 value = $(this).closest('.cmb-td').find('.cmb2-upload-file').val();
1845 }
1846
1847 if( option !== undefined ) {
1848
1849 // Repeatable and group fields
1850 if( option.indexOf('[') !== -1 ) {
1851 var parts = option.split('[');
1852 option = parts[0];
1853
1854 if( data[option] === undefined ) {
1855 data[option] = []
1856 }
1857
1858 if( parts[1] !== ']' ) {
1859
1860 var option_key = parts[1].replace(']', '');
1861 var index = $(this).closest('.cmb-repeatable-grouping').data('iterator');
1862
1863 if( data[option][index] === undefined ) {
1864 data[option][index] = {}
1865 }
1866
1867 // Group field
1868 data[option][index][option_key] = value;
1869 } else {
1870 // Repeatable
1871 data[option].push(value);
1872 }
1873
1874 } else {
1875 // Single option value
1876 data[option] = value;
1877 }
1878 }
1879 });
1880
1881 return data;
1882
1883 }
1884
1885 /**
1886 * Initialize form fields
1887 *
1888 * @since 1.0.0
1889 *
1890 * @param {Object} form
1891 */
1892 function automatorwp_initialize_form_fields( form ) {
1893
1894 var $ = $ || jQuery;
1895
1896 // Selector Control
1897 form.find('.automatorwp-selector select').each(function() { automatorwp_selector( $(this) ); });
1898
1899 // Post Selector Control
1900 form.find('.automatorwp-post-selector select').each(function() { automatorwp_post_selector( $(this) ); });
1901
1902 // Term Selector Control
1903 form.find('.automatorwp-term-selector select').each(function() { automatorwp_term_selector( $(this) ); });
1904
1905 // Taxonomy Selector Control
1906 form.find('.automatorwp-taxonomy-selector select').each(function() { automatorwp_selector( $(this) ); });
1907
1908 // User Selector Control
1909 form.find('.automatorwp-user-selector select').each(function() { automatorwp_user_selector( $(this) ); });
1910
1911 // Object Selector Control
1912 form.find('.automatorwp-object-selector select').each(function() { automatorwp_object_selector( $(this) ); });
1913
1914 // Ajax Selector Control
1915 form.find('.automatorwp-ajax-selector select').each(function() { automatorwp_ajax_selector( $(this) ); });
1916
1917 // Tags selector
1918 form.find('.automatorwp-automation-tag-selector').each(function() { automatorwp_tags_selector( $(this) ); });
1919
1920 // Filter selector
1921 form.find('.automatorwp-filter-selector select').each(function() { automatorwp_filter_selector( $(this) ); });
1922
1923 // Fix repeatable fields selector
1924 form.find('.cmb-repeat-table').each(function() {
1925 var item_id = $(this).closest('.automatorwp-automation-item').find('input.id').val();
1926 var new_id = $(this).attr('id') + '-' + item_id;
1927
1928 $(this).attr('id', new_id);
1929 $(this).parent().find('.cmb-add-row-button').attr('data-selector', new_id);
1930 });
1931
1932 // Fix group fields selector
1933 form.find('.cmb-repeatable-group').each(function() {
1934 var item_id = $(this).closest('.automatorwp-automation-item').find('input.id').val();
1935 var new_id = $(this).attr('id') + '-' + item_id;
1936
1937 $(this).attr('id', new_id);
1938 $(this).find('.cmb-add-group-row').attr('data-selector', new_id);
1939 $(this).find('.cmb-remove-group-row').attr('data-selector', new_id);
1940 });
1941
1942 // Init CMB2 callbacks
1943 form
1944 // Repeatable content
1945 .on( 'click', '.cmb-add-group-row', function(e) {
1946 window.CMB2.addGroupRow.apply(this, [e] );
1947 })
1948 .on( 'click', '.cmb-add-row-button', function(e) {
1949 window.CMB2.addAjaxRow.apply(this, [e] );
1950 })
1951 .on( 'click', '.cmb-remove-group-row', function(e) {
1952 window.CMB2.removeGroupRow.apply(this, [e] );
1953 })
1954 .on( 'click', '.cmb-remove-row-button', function(e) {
1955 window.CMB2.removeAjaxRow.apply(this, [e] );
1956 })
1957 // Reset titles when removing a row
1958 .on( 'cmb2_remove_row', '.cmb-repeatable-group', function(e) {
1959 window.CMB2.resetTitlesAndIterator.apply(this, [e] );
1960 })
1961 .on( 'click', '.cmbhandle, .cmbhandle + .cmbhandle-title', function(e) {
1962 window.CMB2.toggleHandle.apply(this, [e] );
1963 })
1964 .on( 'click', '.cmb2-upload-button', function(e) {
1965 window.CMB2.handleMedia.apply(this, [e] );
1966 })
1967
1968 // Init time/date/color pickers
1969 window.CMB2.initPickers( form.find('input[type="text"].cmb2-timepicker'), form.find('input[type="text"].cmb2-datepicker'), form.find('input[type="text"].cmb2-colorpicker') );
1970
1971
1972 // Init wysiwyg editors
1973 form.find( '.cmb-type-wysiwyg' ).each( function() {
1974
1975 var $this = $(this);
1976
1977 var textarea_id = $this.find('textarea').attr('id');
1978 var textarea = $this.find('textarea').clone();
1979
1980 // Remove CMB2 HTML and add a single textarea to be converted as editor
1981 $this.find('.wp-editor-wrap').remove();
1982 $this.find('.cmb-td').append(textarea);
1983
1984 automatorwp_initialize_wysiwyg_editor( textarea_id );
1985
1986 } );
1987
1988 if ( 'undefined' !== typeof window.QTags ) {
1989 window.QTags._buttonsInit();
1990 }
1991
1992 }
1993
1994
1995 /**
1996 * Initialize a wysiwyg editor
1997 *
1998 * @since 1.0.0
1999 *
2000 * @param {string} textarea_id
2001 */
2002 function automatorwp_initialize_wysiwyg_editor( textarea_id ) {
2003
2004 if( window.tinyMCE.editors.length && window.tinyMCE.editors[textarea_id] ) {
2005 window.tinyMCE.editors[textarea_id].remove();
2006 }
2007
2008 wp.editor.initialize( textarea_id, {
2009 mediaButtons: true,
2010 tinymce: {
2011 wpautop: true,
2012 toolbar1: 'formatselect bold italic bullist numlist blockquote alignleft aligncenter alignright link unlink wp_more fullscreen wp_adv',
2013 toolbar2: 'strikethrough hr forecolor pastetext removeformat charmap outdent indent undo redo wp_help'
2014 },
2015 quicktags: {
2016 buttons: 'strong,em,link,block,del,ins,img,ul,ol,li,,code,more,close'
2017 }
2018 });
2019
2020 }
2021
2022 var automatorwp_original_automation_status = '';
2023 var automatorwp_cancel_automation_run = false;
2024
2025 /**
2026 * Run an automation through ajax
2027 *
2028 * @since 1.0.0
2029 *
2030 * @param {int} automation_id
2031 * @param {int} items_per_loop
2032 */
2033 function automatorwp_run_automation( automation_id, items_per_loop ) {
2034
2035 var $ = $ || jQuery;
2036
2037 if( automatorwp_original_automation_status === '' ) {
2038 automatorwp_original_automation_status = $('select#status').val();
2039
2040 if( automatorwp_original_automation_status === 'in-progress' ) {
2041 automatorwp_original_automation_status = $('.automatorwp-run-automation').data('original-status');
2042 }
2043
2044 // Switch status to in-progress
2045 $('select#status').val('in-progress').trigger('change');
2046 $('#cmb-field-js-controls-status-value .cmb2-id-status').html($('select#status option[value="in-progress"]').html());
2047 }
2048
2049 $.ajax({
2050 url: ajaxurl,
2051 method: 'POST',
2052 data: {
2053 action: 'automatorwp_run_automation',
2054 nonce: automatorwp_admin.nonce,
2055 automation_id: automation_id,
2056 items_per_loop: items_per_loop,
2057 },
2058 success: function( response ) {
2059
2060 if( response.success ) {
2061
2062 if( automatorwp_cancel_automation_run === true ) {
2063
2064 // Send an ajax request to cancel run
2065 $.ajax({
2066 url: ajaxurl,
2067 method: 'POST',
2068 data: {
2069 action: 'automatorwp_cancel_automation_run',
2070 nonce: automatorwp_admin.nonce,
2071 automation_id: automation_id,
2072 },
2073 success: function( response ) {
2074 // Show cancel done text
2075 $('.automatorwp-cancel-automation-run button .automatorwp-cancel-automation-run-cancelling-label').hide();
2076 $('.automatorwp-cancel-automation-run button .automatorwp-cancel-automation-run-done-label').show();
2077
2078 // Update the cancel automation run flag
2079 automatorwp_cancel_automation_run = false;
2080
2081 setTimeout(function() {
2082 // Restore the run automation elements
2083 automatorwp_restore_run_automation_elements( response );
2084 }, 2000);
2085 },
2086 error: function( response ) {
2087 // Restore the run automation elements
2088 automatorwp_restore_run_automation_elements( response );
2089 }
2090 });
2091
2092 return;
2093 }
2094
2095 // Update progress
2096 $('.automatorwp-run-automation-progress .automatorwp-run-automation-progress-current-progress').css({ width: response.data.percentage + '%' });
2097 $('.automatorwp-run-automation-progress-text').text(response.data.processed + '/' + response.data.count);
2098
2099 if( response.data.percentage === 100 ) {
2100 $('.automatorwp-run-automation-progress').addClass('automatorwp-run-automation-progress-completed');
2101 }
2102
2103 if( response.data.run_again ) {
2104 // Get the details
2105 automatorwp_run_automation( automation_id, items_per_loop );
2106 } else {
2107
2108 // Show run automation done text
2109 $('.automatorwp-run-automation button .automatorwp-run-automation-running-label').hide();
2110 $('.automatorwp-run-automation button .automatorwp-run-automation-done-label').show();
2111
2112 // Hide the cancel button
2113 $('.automatorwp-cancel-automation-run').hide();
2114
2115 setTimeout(function() {
2116 // Restore the run automation elements
2117 automatorwp_restore_run_automation_elements( response );
2118 }, 2000);
2119 }
2120
2121 } else {
2122 // Restore the run automation elements
2123 automatorwp_restore_run_automation_elements( response );
2124 }
2125 },
2126 error: function( response ) {
2127 // Restore the run automation elements
2128 automatorwp_restore_run_automation_elements( response );
2129 }
2130 });
2131
2132 }
2133
2134 /**
2135 * Helper fuction to restore the elements disabled or hidden during the run automation process
2136 *
2137 * @since 1.0.0
2138 *
2139 * @param {Object} response
2140 */
2141 function automatorwp_restore_run_automation_elements( response ) {
2142
2143 var $ = $ || jQuery;
2144
2145 // Enable run automation button, switch labels and restore status
2146 $('.automatorwp-run-automation button').prop('disabled', false);
2147 $('.automatorwp-run-automation button .automatorwp-run-automation-run-label').show();
2148 $('.automatorwp-run-automation button .automatorwp-run-automation-running-label').hide();
2149 $('.automatorwp-run-automation button .automatorwp-run-automation-done-label').hide();
2150 $('.automatorwp-run-automation').removeClass('automatorwp-is-running');
2151 $('select#status').val(automatorwp_original_automation_status).trigger('change');
2152 $('#cmb-field-js-controls-status-value .cmb2-id-status').html($('select#status option[value="' + automatorwp_original_automation_status + '"]').html());
2153
2154 // Hide the progress
2155 $('.automatorwp-run-automation-progress').slideUp('fast');
2156
2157 // Restore the cancel button
2158 $('.automatorwp-cancel-automation-run').hide();
2159 $('.automatorwp-cancel-automation-run button').prop('disabled', false);
2160 $('.automatorwp-cancel-automation-run button .automatorwp-cancel-automation-run-cancel-label').show();
2161 $('.automatorwp-cancel-automation-run button .automatorwp-cancel-automation-run-cancelling-label').hide();
2162 $('.automatorwp-cancel-automation-run button .automatorwp-cancel-automation-run-done-label').hide();
2163
2164 // Update the cancel automation run flag
2165 automatorwp_cancel_automation_run = false;
2166
2167 // Show a message if required
2168 if( response !== undefined && response.data !== undefined && response.data.message !== undefined ) {
2169 $('.automatorwp-run-automation button').closest('#major-publishing-actions').append('<div class="automatorwp-notice-error" style="display: none;">' + response.data.message + '</div>');
2170 $('.automatorwp-run-automation button').closest('#major-publishing-actions').find('.automatorwp-notice-error').slideDown('fast');
2171 }
2172
2173 }