installed plugin Easy Digital Downloads version 3.1.0.3

This commit is contained in:
2022-11-27 15:03:07 +00:00
committed by Gitium
parent 555673545b
commit c5dce2cec6
1200 changed files with 238970 additions and 0 deletions

View File

@ -0,0 +1,24 @@
/* global Backbone */
/**
* Internal dependencies.
*/
import TaxRate from './../models/tax-rate.js';
/**
* A collection of multiple tax rates.
*/
const TaxRates = Backbone.Collection.extend( {
// Map the model.
model: TaxRate,
/**
* Set initial state.
*/
initialize: function() {
this.showAll = false;
this.selected = [];
},
} );
export default TaxRates;

View File

@ -0,0 +1,47 @@
/* global _, eddTaxRates */
/**
* Internal dependencies.
*/
import TaxRate from './models/tax-rate.js';
import TaxRates from './collections/tax-rates.js';
import Manager from './views/manager.js';
import { jQueryReady } from 'utils/jquery.js';
/**
* DOM ready.
*/
jQueryReady( () => {
// Show notice if taxes are not enabled.
const noticeEl = document.getElementById( 'edd-tax-disabled-notice' );
if ( noticeEl ) {
noticeEl.classList.add( 'notice' );
noticeEl.classList.add( 'notice-warning' );
}
// Start manager with a blank collection.
const manager = new Manager( {
collection: new TaxRates(),
} );
const rates = [];
// Normalize rate data.
_.each( eddTaxRates.rates, ( rate ) => rates.push( {
id: rate.id,
country: rate.name,
region: rate.description,
global: 'country' === rate.scope,
amount: rate.amount,
status: rate.status,
} ) );
// Add initial rates.
manager.collection.set( rates, {
silent: true,
} );
// Render manager.
manager.render();
} );

View File

@ -0,0 +1,34 @@
/* global Backbone */
/**
* Model a tax rate.
*/
const TaxRate = Backbone.Model.extend( {
defaults: {
id: '',
country: '',
region: '',
global: true,
amount: 0,
status: 'active',
unsaved: false,
selected: false,
},
/**
* Format a rate amount (adds a %)
*
* @todo This should support dynamic decimal types.
*/
formattedAmount: function() {
let amount = 0;
if ( this.get( 'amount' ) ) {
amount = parseFloat( this.get( 'amount' ) ).toFixed( 2 );
}
return `${ amount }%`;
},
} );
export default TaxRate;

View File

@ -0,0 +1,55 @@
/* global wp, _ */
/**
* Apply bulk actions.
*/
const BulkActions = wp.Backbone.View.extend( {
// See https://codex.wordpress.org/Javascript_Reference/wp.template
template: wp.template( 'edd-admin-tax-rates-table-bulk-actions' ),
// Watch events.
events: {
'click .edd-admin-tax-rates-table-filter': 'filter',
'change .edd-admin-tax-rates-table-hide input': 'showHide',
},
/**
* Bulk actions for selected items.
*
* Currently only supports changing the status.
*
* @param {Object} event Event.
*/
filter: function( event ) {
event.preventDefault();
// @hack - need to access the DOM directly here because the dropdown is not tied to the button event.
const status = document.getElementById( 'edd-admin-tax-rates-table-bulk-actions' );
_.each( this.collection.selected, ( cid ) => {
const model = this.collection.get( {
cid: cid,
} );
model.set( 'status', status.value );
} );
this.collection.trigger( 'filtered' );
},
/**
* Toggle show active/inactive rates.
*
* @param {Object} event Event.
*/
showHide: function( event ) {
this.collection.showAll = event.target.checked;
// @hack -- shouldn't access this table directly.
document.getElementById( 'edd_tax_rates' ).classList.toggle( 'has-inactive', this.collection.showAll );
this.collection.trigger( 'filtered' );
},
} );
export default BulkActions;

View File

@ -0,0 +1,65 @@
/* global wp */
/**
* Internal dependencies.
*/
import Table from './table.js';
import BulkActions from './bulk-actions.js';
/**
* Manage tax rates.
*/
const Manager = wp.Backbone.View.extend( {
// Append to this element.
el: '#edd-admin-tax-rates',
/**
* Set bind changes to collection.
*/
initialize: function() {
this.listenTo( this.collection, 'add change', this.makeDirty );
// Clear unload confirmation when submitting parent form.
document.querySelector( '.edd-settings-form #submit' ).addEventListener( 'click', this.makeClean );
},
/**
* Output the manager.
*/
render: function() {
this.views.add( new BulkActions( {
collection: this.collection,
} ) );
this.views.add( new Table( {
collection: this.collection,
} ) );
},
/**
* Collection has changed so warn the user before exiting.
*/
makeDirty: function() {
window.onbeforeunload = this.confirmUnload;
},
/**
* When submitting the main form remove the dirty check.
*/
makeClean: function() {
window.onbeforeunload = null;
},
/**
* Confirm page unload.
*
* @param {Object} event Close event.
*/
confirmUnload: function( event ) {
event.preventDefault();
return '';
},
} );
export default Manager;

View File

@ -0,0 +1,38 @@
/* global wp, _ */
/**
* Internal dependencies.
*/
import { getChosenVars } from 'utils/chosen.js';
const RegionField = wp.Backbone.View.extend( {
/**
* Bind passed arguments.
*
* @param {Object} options Extra options passed.
*/
initialize: function( options ) {
_.extend( this, options );
},
/**
* Create a list of options.
*/
render: function() {
if ( this.global ) {
return;
}
if ( 'nostates' === this.states ) {
this.setElement( '<input type="text" id="tax_rate_region" />' );
} else {
this.$el.html( this.states );
this.$el.find( 'select' ).each( function() {
const el = $( this );
el.chosen( getChosenVars( el ) );
} );
}
},
} );
export default RegionField;

View File

@ -0,0 +1,236 @@
/* global wp */
/**
* Internal dependencies.
*/
import TaxRate from './../models/tax-rate.js';
import RegionField from './../views/region-field.js';
import { getChosenVars } from 'utils/chosen.js';
/**
* Add a new rate "form".
*/
const TableAdd = wp.Backbone.View.extend( {
// Use <tfoot>
tagName: 'tfoot',
// Set class.
className: 'add-new',
// See https://codex.wordpress.org/Javascript_Reference/wp.template
template: wp.template( 'edd-admin-tax-rates-table-add' ),
// Watch events.
events: {
'click button': 'addTaxRate',
'keypress': 'maybeAddTaxRate',
'change #tax_rate_country': 'setCountry',
// Can be select or input.
'keyup #tax_rate_region': 'setRegion',
'change #tax_rate_region': 'setRegion',
'change input[type="checkbox"]': 'setGlobal',
// Can be click increase or keyboard.
'keyup #tax_rate_amount': 'setAmount',
'change #tax_rate_amount': 'setAmount',
},
/**
* Set initial state and bind changes to model.
*/
initialize: function() {
this.model = new TaxRate( {
global: true,
unsaved: true,
} );
this.listenTo( this.model, 'change:country', this.updateRegion );
this.listenTo( this.model, 'change:global', this.updateRegion );
},
/**
* Render. Only overwritten so we can reinit chosen once cleared.
*/
render: function() {
wp.Backbone.View.prototype.render.apply( this, arguments );
this.$el.find( 'select' ).each( function() {
const el = $( this );
el.chosen( getChosenVars( el ) );
} );
return this;
},
/**
* Show a list of states or an input field.
*/
updateRegion: function() {
const self = this;
const data = {
action: 'edd_get_shop_states',
country: this.model.get( 'country' ),
nonce: eddTaxRates.nonce,
field_name: 'tax_rate_region',
};
$.post( ajaxurl, data, function( response ) {
self.views.set( '#tax_rate_region_wrapper', new RegionField( {
states: response,
global: self.model.get( 'global' ),
} ) );
} );
},
/**
* Set a country value.
*
* @param {Object} event Event.
*/
setCountry: function( event ) {
let country = event.target.options[ event.target.selectedIndex ].value;
let regionGlobalCheckbox = document.getElementById( "tax_rate_region_global" );
if ( 'all' === country ) {
country = '*';
regionGlobalCheckbox.checked = true;
this.model.set( 'region', '' );
this.model.set( 'global', true );
regionGlobalCheckbox.readOnly = true;
regionGlobalCheckbox.disabled = true;
} else {
regionGlobalCheckbox.disabled = false;
regionGlobalCheckbox.readOnly = false;
}
this.model.set( 'country', country );
},
/**
* Set a region value.
*
* @param {Object} event Event.
*/
setRegion: function( event ) {
let value = false;
if ( event.target.value ) {
value = event.target.value;
} else {
value = event.target.options[ event.target.selectedIndex ].value;
}
this.model.set( 'region', value );
},
/**
* Set a global scope.
*
* @param {Object} event Event.
*/
setGlobal: function( event ) {
let isChecked = event.target.checked;
this.model.set( 'global', isChecked );
if ( true === isChecked ) {
this.model.set( 'region', '' );
}
},
/**
* Set an amount value.
*
* @param {Object} event Event.
*/
setAmount: function( event ) {
this.model.set( 'amount', event.target.value );
},
/**
* Monitors keyepress for "Enter" key.
*
* We cannot use the `submit` event because we cannot nest <form>
* elements inside the settings API.
*
* @param {Object} event Keypress event.
*/
maybeAddTaxRate: function( event ) {
if ( 13 !== event.keyCode ) {
return;
}
this.addTaxRate( event );
},
/**
* Add a single rate when the "form" is submitted.
*
* @param {Object} event Event.
*/
addTaxRate: function( event ) {
event.preventDefault();
const { i18n } = eddTaxRates;
if ( ! this.model.get( 'country' ) ) {
alert( i18n.emptyCountry );
return;
}
let addingRegion = this.model.get( 'region' );
let addingCountry = this.model.get( 'country' );
let addingGlobal = '' === this.model.get( 'region' );
// For the purposes of this query, the * is really an empty query.
if ( '*' === addingCountry ) {
addingCountry = '';
addingRegion = '';
addingGlobal = false;
}
const existingCountryWide = this.collection.where( {
region: addingRegion,
country: addingCountry,
global: addingGlobal,
status: 'active',
} );
if ( existingCountryWide.length > 0 ) {
const countryString = '' === addingCountry
? '*'
: addingCountry;
const regionString = '' === addingRegion
? ''
: ': ' + addingRegion;
const taxRateString = countryString + regionString;
alert( i18n.duplicateRate.replace( '%s', `"${ taxRateString }"` ) );
return;
}
if ( this.model.get( 'amount' ) <= 0 ) {
alert( i18n.emptyTax );
return;
}
// Merge cid as ID to make this a unique model.
this.collection.add( _.extend(
this.model.attributes,
{
id: this.model.cid,
}
) );
this.render();
this.initialize();
},
} );
export default TableAdd;

View File

@ -0,0 +1,33 @@
/* global wp, _ */
/**
* Output a table header and footer.
*/
const TableMeta = wp.Backbone.View.extend( {
// See https://codex.wordpress.org/Javascript_Reference/wp.template
template: wp.template( 'edd-admin-tax-rates-table-meta' ),
// Watch events.
events: {
'change [type="checkbox"]': 'selectAll',
},
/**
* Select all items in the collection.
*
* @param {Object} event Event.
*/
selectAll: function( event ) {
const checked = event.target.checked;
_.each( this.collection.models, ( model ) => {
// Check individual models.
model.set( 'selected', checked );
// Add to global selection.
this.collection.selected.push( model.cid );
} );
},
} );
export default TableMeta;

View File

@ -0,0 +1,17 @@
/* global wp */
/**
* Empty tax rates table.
*/
const TableRowEmpty = wp.Backbone.View.extend( {
// Insert as a <tr>
tagName: 'tr',
// Set class.
className: 'edd-tax-rate-row edd-tax-rate-row--is-empty',
// See https://codex.wordpress.org/Javascript_Reference/wp.template
template: wp.template( 'edd-admin-tax-rates-table-row-empty' ),
} );
export default TableRowEmpty;

View File

@ -0,0 +1,119 @@
/* global wp, _ */
/**
* A row inside a table of rates.
*/
const TableRow = wp.Backbone.View.extend( {
// Insert as a <tr>
tagName: 'tr',
// Set class.
className: function() {
return 'edd-tax-rate-row edd-tax-rate-row--' + this.model.get( 'status' );
},
// See https://codex.wordpress.org/Javascript_Reference/wp.template
template: wp.template( 'edd-admin-tax-rates-table-row' ),
// Watch events.
events: {
'click .remove': 'removeRow',
'click .activate': 'activateRow',
'click .deactivate': 'deactivateRow',
'change [type="checkbox"]': 'selectRow',
},
/**
* Bind model to view.
*/
initialize: function() {
this.listenTo( this.model, 'change', this.render );
},
/**
* Render
*/
render: function() {
this.$el.html( this.template( {
...this.model.toJSON(),
formattedAmount: this.model.formattedAmount(),
} ) );
// Ensure the wrapper class has the new name.
this.$el.attr( 'class', _.result( this, 'className' ) );
},
/**
* Remove a rate (can only be done if it has not been saved to the database).
*
* Don't use this.model.destroy() to avoid sending a DELETE request.
*
* @param {Object} event Event.
*/
removeRow: function( event ) {
event.preventDefault();
this.collection.remove( this.model );
},
/**
* Activate a rate.
*
* @param {Object} event Event.
*/
activateRow: function( event ) {
event.preventDefault();
const { i18n } = eddTaxRates;
const existingCountryWide = this.collection.where( {
region: this.model.get( 'region' ),
country: this.model.get( 'country' ),
global: '' === this.model.get( 'region' ),
status: 'active',
} );
if ( existingCountryWide.length > 0 ) {
const regionString = '' === this.model.get( 'region' )
? ''
: ': ' + this.model.get( 'region' );
const taxRateString = this.model.get( 'country' ) + regionString;
alert( i18n.duplicateRate.replace( '%s', `"${ taxRateString }"` ) );
return;
}
this.model.set( 'status', 'active' );
},
/**
* Deactivate a rate.
*
* @param {Object} event Event.
*/
deactivateRow: function( event ) {
event.preventDefault();
this.model.set( 'status', 'inactive' );
},
/**
* Select or deselect for bulk actions.
*
* @param {Object} event Event.
*/
selectRow: function( event ) {
const checked = event.target.checked;
if ( ! checked ) {
this.collection.selected = _.reject( this.collection.selected, ( cid ) => {
return cid === this.model.cid;
} );
} else {
this.collection.selected.push( this.model.cid );
}
},
} );
export default TableRow;

View File

@ -0,0 +1,65 @@
/* global wp, _ */
/**
* Internal dependencies.
*/
import TableRowEmpty from './table-row-empty.js';
import TableRow from './table-row.js';
/**
* A bunch of rows inside a table of rates.
*/
const TableRows = wp.Backbone.View.extend( {
// Insert as a <tbody>
tagName: 'tbody',
/**
* Bind events to collection.
*/
initialize: function() {
this.listenTo( this.collection, 'add', this.render );
this.listenTo( this.collection, 'remove', this.render );
this.listenTo( this.collection, 'filtered change', this.filtered );
},
/**
* Render a collection of rows.
*/
render: function() {
// Clear to handle sorting.
this.views.remove();
// Show empty placeholder.
if ( 0 === this.collection.models.length ) {
return this.views.add( new TableRowEmpty() );
}
// Add items.
_.each( this.collection.models, ( rate ) => {
this.views.add( new TableRow( {
collection: this.collection,
model: rate,
} ) );
} );
},
/**
* Show an empty state if all items are deactivated.
*/
filtered: function() {
const disabledRates = this.collection.where( {
status: 'inactive',
} );
// Check if all rows are invisible, and show the "No Items" row if so
if ( disabledRates.length === this.collection.models.length && ! this.collection.showAll ) {
this.views.add( new TableRowEmpty() );
// Possibly re-render the view
} else {
this.render();
}
},
} );
export default TableRows;

View File

@ -0,0 +1,50 @@
/* global wp */
/**
* Internal dependencies.
*/
import TableMeta from './table-meta.js';
import TableRows from './table-rows.js';
import TableAdd from './table-add.js';
/**
* Manage the tax rate rows in a table.
*/
const Table = wp.Backbone.View.extend( {
// Render as a <table> tag.
tagName: 'table',
// Set class.
className: 'wp-list-table widefat fixed tax-rates',
// Set ID.
id: 'edd_tax_rates',
/**
* Output a table with a header, body, and footer.
*/
render: function() {
this.views.add( new TableMeta( {
tagName: 'thead',
collection: this.collection,
} ) );
this.views.add( new TableRows( {
collection: this.collection,
} ) );
this.views.add( new TableAdd( {
collection: this.collection,
} ) );
this.views.add( new TableMeta( {
tagName: 'tfoot',
collection: this.collection,
} ) );
// Trigger the `filtered` action to show/hide rows accordingly
this.collection.trigger( 'filtered' );
},
} );
export default Table;