1/** 2 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 * 4 * This source code is licensed under the license found in the 5 * LICENSE file in the root directory of this source tree. 6 * 7 * @package FacebookCommerce 8 */ 9 10/** 11 * Facebook for WooCommerce - Signals helper. 12 * 13 * Exposes `window.fbwcsignal` so a CMP or theme integration can hold or 14 * release signal delivery while keeping the plugin loaded. 15 * 16 * @package FacebookCommerce 17 */ 18( function () { 19 'use strict'; 20 21 var COOKIE_NAME = 'wc_facebook_signals_state'; 22 var STATE_ACTIVE = 'active'; 23 var STATE_HELD = 'held'; 24 25 /** 26 * Minimal cookie setter. 27 * 28 * @param {string} name 29 * @param {string} value 30 * @param {number} days 31 */ 32 function setCookie( name, value, days ) { 33 var d = new Date(); 34 d.setTime( d.getTime() + days * 86400000 ); 35 document.cookie = 36 name + 37 '=' + 38 encodeURIComponent( value ) + 39 ';expires=' + 40 d.toUTCString() + 41 ';path=/;SameSite=Lax'; 42 } 43 44 /** 45 * Read a cookie value. 46 * 47 * @param {string} name 48 * @return {string|null} 49 */ 50 function getCookie( name ) { 51 var match = document.cookie.match( 52 new RegExp( '(?:^|;\\s*)' + name + '=([^;]*)' ) 53 ); 54 return match ? decodeURIComponent( match[ 1 ] ) : null; 55 } 56 57 /** 58 * Update the stored signal state and sync it to PHP. 59 * 60 * @param {string} state `active` or `held`. 61 * @return {Promise} Resolves with the backend response. 62 */ 63 function updateState( state ) { 64 setCookie( COOKIE_NAME, state, 365 ); 65 66 var params = wc_facebook_signals_params; // localized by PHP. 67 68 return new Promise( function ( resolve, reject ) { 69 var xhr = new XMLHttpRequest(); 70 xhr.open( 'POST', params.ajax_url, true ); 71 xhr.setRequestHeader( 72 'Content-Type', 73 'application/x-www-form-urlencoded; charset=UTF-8' 74 ); 75 xhr.onload = function () { 76 if ( xhr.status >= 200 && xhr.status < 300 ) { 77 try { 78 var data = JSON.parse( xhr.responseText ); 79 resolve( data ); 80 } catch ( e ) { 81 reject( e ); 82 } 83 } else { 84 reject( new Error( 'AJAX failed: ' + xhr.status ) ); 85 } 86 }; 87 xhr.onerror = function () { 88 reject( new Error( 'Network error' ) ); 89 }; 90 xhr.send( 91 'action=' + 92 encodeURIComponent( params.action ) + 93 '&security=' + 94 encodeURIComponent( params.nonce ) + 95 '&state=' + 96 encodeURIComponent( state ) 97 ); 98 } ); 99 } 100 101 /** 102 * Hold browser/server signals for subsequent requests. 103 * 104 * @return {Promise} 105 */ 106 function hold() { 107 return updateState( STATE_HELD ); 108 } 109 110 /** 111 * Release held signals and flush any queued browser events. 112 * 113 * @return {Promise} 114 */ 115 function release() { 116 return updateState( STATE_ACTIVE ).then( function ( data ) { 117 if ( 118 window.FacebookSignals && 119 window.FacebookSignals._held 120 ) { 121 return window.FacebookSignals.release().then( 122 function () { 123 return data; 124 }, 125 function () { 126 return data; 127 } 128 ); 129 } 130 131 return data; 132 } ); 133 } 134 135 /** 136 * Read the current signal state from the cookie. 137 * 138 * @return {string|null} `active`, `held`, or `null`. 139 */ 140 function getState() { 141 var val = getCookie( COOKIE_NAME ); 142 if ( val === null ) { 143 return null; 144 } 145 146 return val === STATE_ACTIVE ? STATE_ACTIVE : STATE_HELD; 147 } 148 149 window.fbwcsignal = window.fbwcsignal || {}; 150 window.fbwcsignal.hold = hold; 151 window.fbwcsignal.release = release; 152 window.fbwcsignal.getState = getState; 153} )();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.