| 1 |
jQuery( document ).ready( function( $ ) { |
| 2 |
$.post( |
| 3 |
cartflowsPointerData.ajaxurl, |
| 4 |
{ |
| 5 |
action: 'cartflows_pointer_should_show', |
| 6 |
nonce: cartflowsPointerData.nonce, |
| 7 |
}, |
| 8 |
function( response ) { |
| 9 |
if ( ! response || ! response.show ) { |
| 10 |
return; |
| 11 |
} |
| 12 |
|
| 13 |
let $target = $( cartflowsPointerData.target_selector ); |
| 14 |
if ( ! $target.length ) { |
| 15 |
$target = $( cartflowsPointerData.fallback_target ); |
| 16 |
} |
| 17 |
|
| 18 |
// Build pointer content safely. |
| 19 |
// Note: content uses .html() as it contains intentional HTML from server (sanitized via wp_kses_post). |
| 20 |
const pointerContentHtml = $( '<div>' ) |
| 21 |
.append( $( '<h3>' ).text( response.title ) ) |
| 22 |
.append( $( '<p>' ).html( response.content ) ); |
| 23 |
const pointerContent = pointerContentHtml.html(); |
| 24 |
|
| 25 |
let pointerClosedBy = null; |
| 26 |
|
| 27 |
$target |
| 28 |
.pointer( { |
| 29 |
content: pointerContent, |
| 30 |
position: cartflowsPointerData.position, |
| 31 |
buttons: function( event, t ) { |
| 32 |
const dismissBtn = $( '<a class="close" href="#"></a>' ) |
| 33 |
.text( response.dismiss ) |
| 34 |
.on( 'click.pointer', function( e ) { |
| 35 |
e.preventDefault(); |
| 36 |
pointerClosedBy = 'dismiss'; |
| 37 |
t.element.pointer( 'close' ); |
| 38 |
$.post( cartflowsPointerData.ajaxurl, { |
| 39 |
action: 'cartflows_pointer_dismiss', |
| 40 |
nonce: cartflowsPointerData.nonce, |
| 41 |
} ); |
| 42 |
} ); |
| 43 |
|
| 44 |
// CTA button. |
| 45 |
let safeUrl = '#'; |
| 46 |
if ( response.button_url && /^https?:\/\//i.test( response.button_url ) ) { |
| 47 |
safeUrl = response.button_url; |
| 48 |
} |
| 49 |
|
| 50 |
const ctaBtn = $( '<a class="button button-primary"></a>' ) |
| 51 |
.attr( 'href', safeUrl ) |
| 52 |
.text( response.button_text ) |
| 53 |
.on( 'click.pointer', function() { |
| 54 |
pointerClosedBy = 'cta'; |
| 55 |
t.element.pointer( 'close' ); |
| 56 |
$.post( cartflowsPointerData.ajaxurl, { |
| 57 |
action: 'cartflows_pointer_accept', |
| 58 |
nonce: cartflowsPointerData.nonce, |
| 59 |
} ); |
| 60 |
} ); |
| 61 |
|
| 62 |
return $( '<div style="display:flex;justify-content:space-between;align-items:center;width:100%"></div>' ) |
| 63 |
.append( ctaBtn ) |
| 64 |
.append( dismissBtn ); |
| 65 |
}, |
| 66 |
close: function() { |
| 67 |
if ( ! pointerClosedBy ) { |
| 68 |
$.post( cartflowsPointerData.ajaxurl, { |
| 69 |
action: 'cartflows_pointer_dismiss', |
| 70 |
nonce: cartflowsPointerData.nonce, |
| 71 |
} ); |
| 72 |
} |
| 73 |
}, |
| 74 |
} ) |
| 75 |
.pointer( 'open' ); |
| 76 |
} |
| 77 |
); |
| 78 |
} ); |
| 79 |
|