Skip to content

Instantly share code, notes, and snippets.

@iniznet
Created October 1, 2022 06:13
Show Gist options
  • Save iniznet/52ba16916c2c3d9a583fd1676714cace to your computer and use it in GitHub Desktop.
Save iniznet/52ba16916c2c3d9a583fd1676714cace to your computer and use it in GitHub Desktop.
MyCRED sell content vanilla version
(function($) {
var buying = false;
$( '.mycred-sell-this-wrapper' ).on( 'click', '.mycred-buy-this-content-button', function(){
if ( buying === true ) return false;
buying = true;
var button = $(this);
var post_id = button.data( 'pid' );
var point_type = button.data( 'type' );
var buttonlabel = button.html();
var content_for_sale = $( '#mycred-buy-content' + post_id );
$.ajax({
type : "POST",
data : {
action : 'mycred-buy-content',
token : myCREDBuyContent.token,
postid : post_id,
ctype : point_type
},
dataType : "JSON",
url : myCREDBuyContent.ajaxurl,
beforeSend : function() {
button.attr( 'disabled', 'disabled' ).html( myCREDBuyContent.working );
},
success : function( response ) {
if ( response.success === undefined || ( response.success === true && myCREDBuyContent.reload === '1' ) )
location.reload();
else {
if ( response.success ) {
content_for_sale.fadeOut(function(){
content_for_sale.removeClass( 'mycred-sell-this-wrapper mycred-sell-entire-content mycred-sell-partial-content' ).empty().append( response.data ).fadeIn();
});
}
else {
button.removeAttr( 'disabled' ).html( buttonlabel );
if ( response.data != '' )
alert( response.data );
}
}
console.log( response );
},
complete : function(){
buying = false;
}
});
});
})( jQuery );
/**
* rewrite jquery above into vanilla js
*/
const init = () => {
const buying = false;
const mycredSellThisWrapper = document.querySelector('.mycred-sell-this-wrapper');
if (mycredSellThisWrapper) {
mycredSellThisWrapper.addEventListener('click', (e) => {
if (e.target.classList.contains('mycred-buy-this-content-button')) {
if (buying === true) return false;
buying = true;
const button = e.target;
const post_id = button.dataset.pid;
const point_type = button.dataset.type;
const buttonlabel = button.innerHTML;
const content_for_sale = document.querySelector('#mycred-buy-content' + post_id);
const xhr = new XMLHttpRequest();
xhr.open('POST', myCREDBuyContent.ajaxurl, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
const response = JSON.parse(xhr.responseText);
if (response.success === undefined || (response.success === true && myCREDBuyContent.reload === '1')) {
location.reload();
} else {
if (response.success) {
content_for_sale.style.display = 'none';
content_for_sale.classList.remove('mycred-sell-this-wrapper', 'mycred-sell-entire-content', 'mycred-sell-partial-content');
content_for_sale.innerHTML = response.data;
content_for_sale.style.display = 'block';
} else {
button.removeAttribute('disabled');
button.innerHTML = buttonlabel;
if (response.data !== '') {
alert(response.data);
}
}
}
console.log(response);
}
};
xhr.send(`action=mycred-buy-content&token=${myCREDBuyContent.token}&postid=${post_id}&ctype=${point_type}`);
buying = false;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment