first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
;(function ( $, window, document ) {
'use strict';
var $wc_ppec = {
init: function() {
window.paypalCheckoutReady = function() {
paypal.checkout.setup(
wc_ppec_context.payer_id,
{
environment: wc_ppec_context.environment,
button: ['woo_pp_ec_button', 'woo_pp_ppc_button'],
locale: wc_ppec_context.locale,
container: ['woo_pp_ec_button', 'woo_pp_ppc_button']
}
);
}
}
}
var costs_updated = false;
$( '#woo_pp_ec_button' ).click( function( event ) {
if ( costs_updated ) {
costs_updated = false;
return;
}
event.stopPropagation();
var data = {
'nonce': wc_ppec_context.update_shipping_costs_nonce,
};
var href = $(this).attr( 'href' );
$.ajax( {
type: 'POST',
data: data,
url: wc_ppec_context.ajaxurl,
success: function( response ) {
costs_updated = true;
$( '#woo_pp_ec_button' ).click();
}
} );
} );
if ( wc_ppec_context.show_modal ) {
$wc_ppec.init();
}
})( jQuery, window, document );

View File

@@ -0,0 +1,126 @@
/* global wc_ppec_generate_cart_context */
;(function( $, window, document ) {
'use strict';
// This button state is only applicable to non-SPB click handler below.
var button_enabled = true;
$( '#woo_pp_ec_button_product' )
.on( 'enable.legacy', function() {
button_enabled = true;
} )
.on( 'disable.legacy', function() {
button_enabled = false;
} );
$( '#woo_pp_ec_button_product' )
.on( 'enable', function() {
$( '#woo_pp_ec_button_product' ).css( {
'cursor': '',
'-webkit-filter': '', // Safari 6.0 - 9.0
'filter': '',
} );
$( '#woo_pp_ec_button_product > *' ).css( 'pointer-events', '' );
} )
.on( 'disable', function() {
$( '#woo_pp_ec_button_product' ).css( {
'cursor': 'not-allowed',
'-webkit-filter': 'grayscale( 100% )', // Safari 6.0 - 9.0
'filter': 'grayscale( 100% )',
} );
$( '#woo_pp_ec_button_product > *' ).css( 'pointer-events', 'none' );
} );
// True if the product is simple or the user selected a valid variation. False on variable product without a valid variation selected
var variation_valid = true;
// True if all the fields of the product form are valid (such as required fields configured by Product Add-Ons). False otherwise
var fields_valid = true;
var form = $( 'form.cart' );
var update_button = function() {
$( '#woo_pp_ec_button_product' ).trigger( ( variation_valid && fields_valid ) ? 'enable' : 'disable' );
};
var validate_form = function() {
fields_valid = form.get( 0 ).checkValidity();
update_button();
};
// It's a variations form, button availability should depend on its events
if ( $( '.variations_form' ).length ) {
variation_valid = false;
$( '.variations_form' )
.on( 'show_variation', function( event, form, purchasable ) {
variation_valid = purchasable;
update_button();
} )
.on( 'hide_variation', function() {
variation_valid = false;
update_button();
} );
}
// Disable the button if there are invalid fields in the product page (like required fields from Product Addons)
form.on( 'change', 'select, input, textarea', function() {
// Hack: IE11 uses the previous field value for the checkValidity() check if it's called in the onChange handler
setTimeout( validate_form, 0 );
} );
validate_form();
var generate_cart = function( callback ) {
var data = {
'nonce': wc_ppec_generate_cart_context.generate_cart_nonce,
'attributes': {},
};
var field_pairs = form.serializeArray();
for ( var i = 0; i < field_pairs.length; i++ ) {
// Prevent the default WooCommerce PHP form handler from recognizing this as an "add to cart" call
if ( 'add-to-cart' === field_pairs[ i ].name ) {
field_pairs[ i ].name = 'ppec-add-to-cart';
}
// Save attributes as a separate prop in `data` object,
// so that `attributes` can be used later on when adding a variable product to cart
if ( -1 !== field_pairs[ i ].name.indexOf( 'attribute_' ) ) {
data.attributes[ field_pairs[ i ].name ] = field_pairs[ i ].value;
continue;
}
data[ field_pairs[ i ].name ] = field_pairs[ i ].value;
}
// If this is a simple product, the "Submit" button has the product ID as "value", we need to include it explicitly
data[ 'ppec-add-to-cart' ] = $( '[name=add-to-cart]' ).val();
$.ajax( {
type: 'POST',
data: data,
url: wc_ppec_generate_cart_context.ajaxurl,
success: callback,
} );
};
window.wc_ppec_generate_cart = generate_cart;
// Non-SPB mode click handler, namespaced as 'legacy' as it's replaced by `payment` callback of Button API.
$( '#woo_pp_ec_button_product' ).on( 'click.legacy', function( event ) {
event.preventDefault();
if ( ! button_enabled ) {
return;
}
$( '#woo_pp_ec_button_product' ).trigger( 'disable' );
var href = $(this).attr( 'href' );
generate_cart( function() {
window.location.href = href;
} );
} );
})( jQuery, window, document );

View File

@@ -0,0 +1,17 @@
;(function ( $, window, document ) {
'use strict';
$( 'form.checkout' ).on( 'click', 'input[name="payment_method"]', function() {
// Avoid toggling submit button if on confirmation screen
if ( $( '#payment' ).find( '.wc-gateway-ppec-cancel' ).length ) {
return;
}
var isPPEC = $( this ).is( '#payment_method_ppec_paypal' );
var togglePPEC = isPPEC ? 'show' : 'hide';
var toggleSubmit = isPPEC ? 'hide' : 'show';
$( '#woo_pp_ec_button_checkout' ).animate( { opacity: togglePPEC, height: togglePPEC, padding: togglePPEC }, 230 );
$( '#place_order' ).animate( { opacity: toggleSubmit, height: toggleSubmit, padding: toggleSubmit }, 230 );
} );
})( jQuery, window, document );

View File

@@ -0,0 +1,96 @@
;(function ( $, window, document ) {
'use strict';
var uploadField = {
frames: [],
init: function() {
$( 'button.image_upload' )
.on( 'click', this.onClickUploadButton );
$( 'button.image_remove' )
.on( 'click', this.removeProductImage );
},
onClickUploadButton: function( event ) {
event.preventDefault();
var data = $( event.target ).data();
// If the media frame already exists, reopen it.
if ( 'undefined' !== typeof uploadField.frames[ data.fieldId ] ) {
// Open frame.
uploadField.frames[ data.fieldId ].open();
return false;
}
// Create the media frame.
uploadField.frames[ data.fieldId ] = wp.media( {
title: data.mediaFrameTitle,
button: {
text: data.mediaFrameButton
},
multiple: false // Set to true to allow multiple files to be selected
} );
// When an image is selected, run a callback.
var context = {
fieldId: data.fieldId,
};
uploadField.frames[ data.fieldId ]
.on( 'select', uploadField.onSelectAttachment, context );
// Finally, open the modal.
uploadField.frames[ data.fieldId ].open();
},
onSelectAttachment: function() {
// We set multiple to false so only get one image from the uploader.
var attachment = uploadField.frames[ this.fieldId ]
.state()
.get( 'selection' )
.first()
.toJSON();
var $field = $( '#' + this.fieldId );
var $img = $( '<img />' )
.attr( 'src', getAttachmentUrl( attachment ) );
$field.siblings( '.image-preview-wrapper' )
.html( $img );
$field.val( attachment.id );
$field.siblings( 'button.image_remove' ).show();
$field.siblings( 'button.image_upload' ).hide();
},
removeProductImage: function( event ) {
event.preventDefault();
var $button = $( event.target );
var data = $button.data();
var $field = $( '#' + data.fieldId );
//update fields
$field.val( '' );
$field.siblings( '.image-preview-wrapper' ).html( ' ' );
$button.hide();
$field.siblings( 'button.image_upload' ).show();
},
};
function getAttachmentUrl( attachment ) {
if ( attachment.sizes && attachment.sizes.medium ) {
return attachment.sizes.medium.url;
}
if ( attachment.sizes && attachment.sizes.thumbnail ) {
return attachment.sizes.thumbnail.url;
}
return attachment.url;
}
function run() {
uploadField.init();
}
$( run );
}( jQuery ) );

View File

@@ -0,0 +1,171 @@
/* global wc_ppec_context */
;( function ( $, window, document ) {
'use strict';
// Show error notice at top of checkout form, or else within button container
var showError = function( errorMessage, selector ) {
var $container = $( '.woocommerce-notices-wrapper, form.checkout' );
if ( ! $container || ! $container.length ) {
$( selector ).prepend( errorMessage );
return;
} else {
$container = $container.first();
}
// Adapted from https://github.com/woocommerce/woocommerce/blob/ea9aa8cd59c9fa735460abf0ebcb97fa18f80d03/assets/js/frontend/checkout.js#L514-L529
$( '.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message' ).remove();
$container.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + errorMessage + '</div>' );
$container.find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).blur();
var scrollElement = $( '.woocommerce-NoticeGroup-checkout' );
if ( ! scrollElement.length ) {
scrollElement = $container;
}
if ( $.scroll_to_notices ) {
$.scroll_to_notices( scrollElement );
} else {
// Compatibility with WC <3.3
$( 'html, body' ).animate( {
scrollTop: ( $container.offset().top - 100 )
}, 1000 );
}
$( document.body ).trigger( 'checkout_error' );
}
// Map funding method settings to enumerated options provided by PayPal.
var getFundingMethods = function( methods ) {
if ( ! methods ) {
return undefined;
}
var paypal_funding_methods = [];
for ( var i = 0; i < methods.length; i++ ) {
var method = paypal.FUNDING[ methods[ i ] ];
if ( method ) {
paypal_funding_methods.push( method );
}
}
return paypal_funding_methods;
}
var render = function( isMiniCart ) {
var prefix = isMiniCart ? 'mini_cart_' : '';
var button_size = wc_ppec_context[ prefix + 'button_size' ];
var button_layout = wc_ppec_context[ prefix + 'button_layout' ];
var button_label = wc_ppec_context[ prefix + 'button_label' ];
var allowed = wc_ppec_context[ prefix + 'allowed_methods' ];
var disallowed = wc_ppec_context[ prefix + 'disallowed_methods' ];
var selector = isMiniCart ? '#woo_pp_ec_button_mini_cart' : '#woo_pp_ec_button_' + wc_ppec_context.page;
var fromCheckout = 'checkout' === wc_ppec_context.page && ! isMiniCart;
// Don't render if already rendered in DOM.
if ( $( selector ).children().length ) {
return;
}
paypal.Button.render( {
env: wc_ppec_context.environment,
locale: wc_ppec_context.locale,
commit: fromCheckout,
funding: {
allowed: getFundingMethods( allowed ),
disallowed: getFundingMethods( disallowed ),
},
style: {
color: wc_ppec_context.button_color,
shape: wc_ppec_context.button_shape,
layout: button_layout,
size: button_size,
label: button_label,
branding: true,
tagline: false,
},
validate: function( actions ) {
// Only enable on variable product page if purchasable variation selected.
$( '#woo_pp_ec_button_product' ).off( '.legacy' )
.on( 'enable', actions.enable )
.on( 'disable', actions.disable );
},
payment: function() {
// Clear any errors from previous attempt.
$( '.woocommerce-error', selector ).remove();
return new paypal.Promise( function( resolve, reject ) {
// First, generate cart if triggered from single product.
if ( 'product' === wc_ppec_context.page && ! isMiniCart ) {
window.wc_ppec_generate_cart( resolve );
} else {
resolve();
}
} ).then( function() {
// Make PayPal Checkout initialization request.
var data = $( selector ).closest( 'form' )
.add( $( '<input type="hidden" name="nonce" /> ' )
.attr( 'value', wc_ppec_context.start_checkout_nonce )
)
.add( $( '<input type="hidden" name="from_checkout" /> ' )
.attr( 'value', fromCheckout ? 'yes' : 'no' )
)
.serialize();
return paypal.request( {
method: 'post',
url: wc_ppec_context.start_checkout_url,
body: data,
} ).then( function( response ) {
if ( ! response.success ) {
var messageItems = response.data.messages.map( function( message ) {
return '<li>' + message + '</li>';
} ).join( '' );
showError( '<ul class="woocommerce-error" role="alert">' + messageItems + '</ul>', selector );
return null;
}
return response.data.token;
} );
} );
},
onAuthorize: function( data, actions ) {
if ( fromCheckout ) {
// Pass data necessary for authorizing payment to back-end.
$( 'form.checkout' )
.append( $( '<input type="hidden" name="paymentToken" /> ' ).attr( 'value', data.paymentToken ) )
.append( $( '<input type="hidden" name="payerID" /> ' ).attr( 'value', data.payerID ) )
.submit();
} else {
// Navigate to order confirmation URL specified in original request to PayPal from back-end.
return actions.redirect();
}
},
}, selector );
};
// Render cart, single product, or checkout buttons.
if ( wc_ppec_context.page ) {
if ( 'checkout' !== wc_ppec_context.page ) {
render();
}
$( document.body ).on( 'updated_cart_totals updated_checkout', render.bind( this, false ) );
}
// Render buttons in mini-cart if present.
$( document.body ).on( 'wc_fragments_loaded wc_fragments_refreshed', function() {
var $button = $( '.widget_shopping_cart #woo_pp_ec_button_mini_cart' );
if ( $button.length ) {
// Clear any existing button in container, and render.
$button.empty();
render( true );
}
} );
} )( jQuery, window, document );