Skip to content

Commit 182721c

Browse files
Release 3.3.5
1 parent 6ff4ffe commit 182721c

File tree

35 files changed

+2284
-156
lines changed

35 files changed

+2284
-156
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This repository contains the PostFinance Checkout plugin that enables WooCommerc
1414

1515
## Documentation
1616

17-
* [Documentation](https://plugin-documentation.postfinance-checkout.ch/pfpayments/woocommerce/3.3.4/docs/en/documentation.html)
17+
* [Documentation](https://plugin-documentation.postfinance-checkout.ch/pfpayments/woocommerce/3.3.5/docs/en/documentation.html)
1818

1919
## Support
2020

@@ -31,7 +31,7 @@ ____________________________________________________________________________
3131

3232
## License
3333

34-
Please see the [license file](https://github.com/pfpayments/woocommerce/blob/3.3.4/LICENSE) for more information.
34+
Please see the [license file](https://github.com/pfpayments/woocommerce/blob/3.3.5/LICENSE) for more information.
3535

3636
## Privacy Policy
3737

assets/js/admin/order-statuses.js

+343
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
/* global postfinancecheckoutOrderStatusesLocalizeScript, ajaxurl */
2+
( function( $, data, wp, ajaxurl ) {
3+
$( function() {
4+
var $tbody = $('.postfinancecheckout-order-statuses-rows'),
5+
$save_button = $('.postfinancecheckout-order-status-save');
6+
7+
// Check if wp.template and templates exist before using them.
8+
var templatesExist = function(templateId) {
9+
return typeof wp.template === 'function' && document.getElementById('tmpl-' + templateId);
10+
};
11+
12+
var $row_template = templatesExist('postfinancecheckout-order-statuses-row') ? wp.template('postfinancecheckout-order-statuses-row') : null,
13+
$blank_template = templatesExist('postfinancecheckout-order-statuses-row-blank') ? wp.template('postfinancecheckout-order-statuses-row-blank') : null;
14+
15+
if (!$row_template || !$blank_template) {
16+
console.warn('Order statuses templates are not present in the DOM');
17+
return; // Exit if templates do not exist.
18+
}
19+
20+
// Ensure key field formatting.
21+
$(document).on('input', 'input[name="key"]', function() {
22+
var $charCount = $('#charCount');
23+
var maxLength = 17; // Character limit.
24+
var currentLength = this.value.length;
25+
var remaining = maxLength - this.value.length;
26+
27+
// Ensures that only lower case characters and no blank spaces are allowed.
28+
this.value = this.value.toLowerCase().replace(/\s+/g, '-').trim();
29+
30+
// If the text is longer than allowed, cut it down.
31+
if (currentLength > maxLength) {
32+
this.value = this.value.substring(0, maxLength);
33+
remaining = 0;
34+
}
35+
36+
// Updates the remaining character counter.
37+
if (currentLength === 0) {
38+
$charCount.hide();
39+
} else {
40+
$charCount.show().text(remaining + ' ' + data.strings.characters_remaining);
41+
}
42+
});
43+
44+
// Blocks entry of more characters when the limit is reached.
45+
$(document).on('keydown', 'input[name="key"]', function(event) {
46+
var maxLength = 17;
47+
if (this.value.length >= maxLength && event.key !== "Backspace" && event.key !== "Delete" && event.key.length === 1) {
48+
event.preventDefault();
49+
}
50+
});
51+
52+
// Backbone model.
53+
var OrderStatus = Backbone.Model.extend({
54+
save: function( changes ) {
55+
$.post( ajaxurl, {
56+
action : 'postfinancecheckout_custom_order_status_save_changes',
57+
postfinancecheckout_order_statuses_nonce : data.postfinancecheckout_order_statuses_nonce,
58+
changes,
59+
}, this.onSaveResponse, 'json' );
60+
},
61+
onSaveResponse: function( response, textStatus ) {
62+
if ( 'success' === textStatus ) {
63+
if ( response.success ) {
64+
// Get the new status data from the AJAX response.
65+
var newStatus = response.data.order_status;
66+
67+
// Add the new status to the beginning of the statuses array.
68+
var currentStatuses = orderStatus.get( 'statuses' );
69+
currentStatuses.unshift(newStatus); // Add new item at the start of the array.
70+
71+
// Update the model with the new array.
72+
orderStatus.set( 'statuses', currentStatuses );
73+
74+
// Trigger re-rendering to show the new row.
75+
orderStatus.trigger( 'saved:statuses' );
76+
} else if ( response.data ) {
77+
window.alert( response.data );
78+
} else {
79+
window.alert( data.strings.save_failed );
80+
}
81+
}
82+
orderStatusView.unblock();
83+
}
84+
} );
85+
86+
// Backbone view
87+
var OrderStatusView = Backbone.View.extend({
88+
rowTemplate: $row_template,
89+
initialize: function() {
90+
this.listenTo( this.model, 'saved:statuses', this.render );
91+
$( document.body ).on( 'click', '.postfinancecheckout-order-status-add-new', { view: this }, this.configureNewOrderStatus );
92+
$( document.body ).on( 'wc_backbone_modal_response', { view: this }, this.onConfigureOrderStatusSubmitted );
93+
$( document.body ).on( 'wc_backbone_modal_loaded', { view: this }, this.onLoadBackboneModal );
94+
$( document.body ).on( 'wc_backbone_modal_validation', this.validateFormArguments );
95+
},
96+
block: function() {
97+
$( this.el ).block({
98+
message: null,
99+
overlayCSS: {
100+
background: '#fff',
101+
opacity: 0.6
102+
}
103+
});
104+
},
105+
unblock: function() {
106+
$( this.el ).unblock();
107+
},
108+
render: function() {
109+
var statuses = _.indexBy( this.model.get( 'statuses' ), 'key' ),
110+
view = this;
111+
112+
this.$el.empty();
113+
this.unblock();
114+
115+
if ( _.size( statuses ) ) {
116+
// Sort statuses.
117+
statuses = _.sortBy( statuses, function( status ) {
118+
return status.key;
119+
} );
120+
121+
// Populate $tbody with the current statuses.
122+
$.each( statuses, function( id, rowData ) {
123+
view.renderRow( rowData );
124+
} );
125+
} else {
126+
view.$el.append( $blank_template );
127+
}
128+
},
129+
renderRow: function( rowData ) {
130+
var view = this;
131+
view.$el.append( view.rowTemplate( rowData ) );
132+
view.initRow( rowData );
133+
},
134+
initRow: function( rowData ) {
135+
var view = this;
136+
var $tr = view.$el.find( 'tr[data-id="' + rowData.key + '"]');
137+
138+
// Support select boxes.
139+
$tr.find( 'select' ).each( function() {
140+
var attribute = $( this ).data( 'attribute' );
141+
$( this ).find( 'option[value="' + rowData[ attribute ] + '"]' ).prop( 'selected', true );
142+
} );
143+
144+
// Check if buttons have data-type="core" and hide them
145+
var $actionsContainer = $tr.find('.actions-container');
146+
var options = $actionsContainer.data('type');
147+
148+
// Hide buttons if 'core'.
149+
if (options === 'core') {
150+
$actionsContainer.hide();
151+
}
152+
153+
// Make the rows function.
154+
$tr.find( '.view' ).show();
155+
$tr.find( '.edit' ).hide();
156+
$tr.find( '.postfinancecheckout-order-status-edit' ).on( 'click', { view: this }, this.onEditRow );
157+
$tr.find( '.postfinancecheckout-order-status-delete' ).on( 'click', { view: this }, this.onDeleteRow );
158+
},
159+
configureNewOrderStatus: function( event ) {
160+
event.preventDefault();
161+
const key = '';
162+
163+
$( this ).WCBackboneModal({
164+
template : 'postfinancecheckout-order-statuses-configure',
165+
variable : {
166+
key,
167+
action: 'create',
168+
},
169+
data : {
170+
key,
171+
action: 'create',
172+
}
173+
});
174+
},
175+
onConfigureOrderStatusSubmitted: function( event, target, posted_data ) {
176+
if ( target === 'postfinancecheckout-order-statuses-configure' ) {
177+
const view = event.data.view;
178+
const model = view.model;
179+
const isNewRow = posted_data.key.includes( 'postfinancecheckout_custom_order_status_' );
180+
const rowData = {
181+
...posted_data,
182+
};
183+
184+
if ( isNewRow ) {
185+
rowData.newRow = true;
186+
}
187+
188+
view.block();
189+
190+
model.save( {
191+
[ posted_data.key ]: rowData
192+
} );
193+
}
194+
},
195+
validateFormArguments: function( element, target, data ) {
196+
const requiredFields = [ 'key' ];
197+
const formIsComplete = Object.keys( data ).every( key => {
198+
if ( ! requiredFields.includes( key ) ) {
199+
return true;
200+
}
201+
if ( Array.isArray( data[ key ] ) ) {
202+
return data[ key ].length && !!data[ key ][ 0 ];
203+
}
204+
return !!data[ key ];
205+
} );
206+
const createButton = document.getElementById( 'btn-ok' );
207+
createButton.disabled = ! formIsComplete;
208+
createButton.classList.toggle( 'disabled', ! formIsComplete );
209+
},
210+
onEditRow: function( event ) {
211+
const key = $( this ).closest('tr').data('id');
212+
const model = event.data.view.model;
213+
const statuses = _.indexBy( model.get( 'statuses' ), 'key' );
214+
const rowData = statuses[ key ];
215+
216+
event.preventDefault();
217+
$( this ).WCBackboneModal({
218+
template : 'postfinancecheckout-order-statuses-configure',
219+
variable : {
220+
action: 'edit',
221+
...rowData
222+
},
223+
data : {
224+
action: 'edit',
225+
...rowData
226+
}
227+
});
228+
},
229+
onLoadBackboneModal: function( event, target ) {
230+
if ( 'postfinancecheckout-order-statuses-configure' === target ) {
231+
const modalContent = $('.wc-backbone-modal-content');
232+
const key = modalContent.data('id');
233+
const model = event.data.view.model;
234+
const statuses = _.indexBy( model.get( 'statuses' ), 'key' );
235+
const rowData = statuses[ key ];
236+
237+
if ( rowData ) {
238+
// Support select boxes.
239+
$('.wc-backbone-modal-content').find( 'select' ).each( function() {
240+
var attribute = $( this ).data( 'attribute' );
241+
$( this ).find( 'option[value="' + rowData[ attribute ] + '"]' ).prop( 'selected', true );
242+
} );
243+
}
244+
}
245+
246+
},
247+
removeRow: function( key ) {
248+
var $row = this.$el.find('tr[data-id="' + key + '"]');
249+
if ( $row.length ) {
250+
$row.fadeOut(300, function() {
251+
$(this).remove();
252+
});
253+
}
254+
},
255+
onDeleteRow: function( event ) {
256+
var view = event.data.view,
257+
model = view.model,
258+
key = $( this ).closest('tr').data('id');
259+
260+
event.preventDefault();
261+
262+
if ( !key ) {
263+
console.error('No key found for deletion');
264+
return;
265+
}
266+
267+
if ( ! confirm( data.strings.delete_confirmation_msg ) ) {
268+
return;
269+
}
270+
271+
view.block();
272+
273+
$.post( ajaxurl, {
274+
action: 'postfinancecheckout_custom_order_status_delete',
275+
postfinancecheckout_order_statuses_nonce: data.postfinancecheckout_order_statuses_nonce,
276+
key: key
277+
}, function( response ) {
278+
if ( response.success ) {
279+
// Remove the deleted item from the model.
280+
view.removeRow(key);
281+
282+
} else {
283+
alert( response.data.message || 'Error deleting status' );
284+
}
285+
}, 'json' )
286+
.fail(function(xhr, status, error) {
287+
view.showErrorModal(xhr.responseJSON?.data?.message);
288+
})
289+
.always(function() {
290+
view.unblock();
291+
});
292+
},
293+
} );
294+
var orderStatus = new OrderStatus({
295+
statuses: data.statuses
296+
} );
297+
var orderStatusView = new OrderStatusView({
298+
model: orderStatus,
299+
el: $tbody
300+
} );
301+
302+
orderStatusView.render();
303+
304+
// Function to show error modal
305+
OrderStatusView.prototype.showErrorModal = function(message) {
306+
if ( ! $.fn.WCBackboneModal ) {
307+
alert(message); // Fallback in case WCBackboneModal is not available
308+
return;
309+
}
310+
311+
var modalHtml = `
312+
<div id="postfinancecheckout-error-modal" class="wc-backbone-modal wc-modal-shipping-zone-method-settings" style="max-width: 400px; margin: auto;">
313+
<div class="wc-backbone-modal-content">
314+
<section class="wc-backbone-modal-main" role="main">
315+
<header class="wc-backbone-modal-header">
316+
<h1>${"Error"}</h1>
317+
<button class="modal-close modal-close-link dashicons dashicons-no-alt">
318+
<span class="screen-reader-text">Close</span>
319+
</button>
320+
</header>
321+
<article>
322+
<p>${message}</p>
323+
</article>
324+
<footer>
325+
<button class="button button-primary modal-close">OK</button>
326+
</footer>
327+
</section>
328+
</div>
329+
<div class="wc-backbone-modal-backdrop modal-close"></div>
330+
</div>`;
331+
332+
$('body').append(modalHtml);
333+
334+
// Show modal using WooCommerce's WCBackboneModal
335+
$('#postfinancecheckout-error-modal').WCBackboneModal();
336+
337+
// Remove modal from DOM when closed
338+
$(document).on('click', '.wc-backbone-modal .modal-close', function() {
339+
$('#postfinancecheckout-error-modal').remove();
340+
});
341+
};
342+
});
343+
})( jQuery, postfinancecheckoutOrderStatusesLocalizeScript, wp, ajaxurl );

assets/js/frontend/blocks/build/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)