/**
* Created by Nabeel on 26-Oct-16.
*/
(function ( $, win, undefined ) {
$( function () {
// add expire link elements
$( '.coupon:not(.inactive)' ).find( '.post-edit-link' ).each( function ( index, element ) {
// get edit post arguments
var query_args = getQueryParameters( element.href.split( '?' )[ 1 ] );
if ( 'post' in query_args ) {
// create element
$( '
' + kortingvandedag_expired_coupons.i18n.mark + '' )
// insert it after edit link
.insertAfter( $( element ) );
}
} );
// expire link click
$( '.container' ).on( 'click', '.post-expire-link', function ( e ) {
e.preventDefault();
if ( e.currentTarget.className.indexOf( 'loading' ) > -1 ) {
return true;
}
// set link loading status
var $link = $( e.currentTarget ).addClass( 'loading' ).text( kortingvandedag_expired_coupons.i18n.loading );
// send ajax request
$.post( kortingvandedag_expired_coupons.ajax_url, {
action : 'mark_coupon_expired',
post_id: $link.data( 'post' )
}, function ( response ) {
if ( response.success ) {
// success
$link.text( kortingvandedag_expired_coupons.i18n.expired );
// set coupon UI also
$link.closest( '.coupon' ).addClass( 'inactive' );
} else {
// error
alert( response.data );
$link.text( kortingvandedag_expired_coupons.i18n.mark );
}
}, 'json' ).always( function () {
// link loading status
$link.removeClass( 'loading' );
} );
} );
function getQueryParameters( str ) {
return (str || document.location.search).replace( /(^\?)/, '' ).split( "&" ).map( function ( n ) {
// split
n = n.split( "=" );
// assign
this[ n[ 0 ] ] = n[ 1 ];
return this;
}.bind( {} ) )[ 0 ];
}
} );
})( jQuery, window );