1/* global affwp_debug_vars */ 2jQuery(document).ready( function($) { 3 4 var ref_cookie = $.cookie( affwp_debug_vars.ref_cookie ); 5 var visit_cookie = $.cookie( affwp_debug_vars.visit_cookie ); 6 var campaign_cookie = $.cookie( affwp_debug_vars.campaign_cookie ); 7 var credit_last = AFFWP.referral_credit_last; 8 9 /* 10 * Debug data 11 */ 12 13 if ( 1 === AFFWP.debug ) { 14 15 /** 16 * Helpful utilities and data for debugging the JavaScript environment. 17 * 18 * @type {prototype Object} AFFWP.debug_utility An AFFWP.debug_utility object. 19 * 20 * @since 2.0 21 * @since 2.0.1 Return false instead of null. 22 * @var item The cookie item name. 23 */ 24 function affwp_get_cookie_item( item ) { 25 var re = new RegExp(item + "=([^;]+)"); 26 var value = re.exec(document.cookie); 27 return (value != null) ? unescape(value[1]) : false; 28 } 29 30 // Short-circuiting, and saving a parse operation 31 32 /** 33 * Checks whether a given value is an integer, with support for floating-point. 34 * For simple positive integer checks, 35 * use jQuery isNumeric instead (such as within tracking the ref below). 36 * 37 * @since 2.0 38 * 39 * @param {int} value The value to check. 40 * @return {boolean} True if value is an integer, otherwise false. 41 */ 42 function affwp_debug_is_int( value ) { 43 var i; 44 45 if ( isNaN( value ) ) { 46 return false; 47 } 48 49 i = parseFloat( value ); 50 51 return ( i | 0 ) === i; 52 } 53 54 /** 55 * Assert two values are equal. 56 * 57 * @since 2.0 58 * 59 * @param {mixed string|int} a String or integer. 60 * @param {mixed string|int} b String or integer. 61 * @return void 62 */ 63 function affwp_debug_tests_assert_equals( a, b ) { 64 if ( 1 !== AFFWP.debug ) { 65 return false; 66 } 67 68 console.assert( a === b, 'AffiliateWP: Assertion failed, values not equal.' ); 69 } 70 71 /** 72 * Assert two values are not equal. 73 * 74 * @since 2.0 75 * 76 * @param {mixed string|int} a String or integer. 77 * @param {mixed string|int} b String or integer. 78 * 79 * @return void 80 */ 81 function affwp_debug_tests_assert_not_equals( a, b ) { 82 if ( 1 !== AFFWP.debug ) { 83 return false; 84 } 85 86 console.assert( a !== b, 'AffiliateWP: Assertion failed, values are equal.' ); 87 } 88 89 /** 90 * Asserts value a is greater than value b. 91 * 92 * @since 2.0 93 * 94 * @param {int} a Integer. 95 * @param {int} b Integer. 96 * 97 * @return void 98 */ 99 function affwp_debug_tests_assert_greater_than( a, b ) { 100 if ( 1 !== AFFWP.debug || ! affwp_debug_is_int( a ) || ! affwp_debug_is_int( b ) ) { 101 return false; 102 } 103 104 console.assert( a > b, 'AffiliateWP: Assertion failed, primary value is less than secondary value.' ); 105 } 106 107 /** 108 * Asserts value a is less than value b. 109 * 110 * @since 2.0 111 * 112 * @param {int} a Integer. 113 * @param {int} b Integer. 114 * 115 * @return void 116 */ 117 function affwp_debug_tests_assert_less_than( a, b ) { 118 if ( 1 !== AFFWP.debug || ! affwp_debug_is_int( a ) || ! affwp_debug_is_int( b ) ) { 119 return false; 120 } 121 122 console.assert( a < b, 'AffiliateWP: Assertion failed, primary value is greater than secondary value.' ); 123 } 124 125 /** 126 * Various data pertaining to AffiliateWP (if available). 127 * 128 * @since 2.0 129 * @return {array} An array of debug variables. 130 */ 131 function affwp_debug_inline_vars() { 132 var vars = { 133 ajax_url : affwp_scripts.ajaxurl.length ? JSON.stringify( affwp_scripts.ajaxurl ) : 'unavailable', 134 ref : JSON.stringify( AFFWP.referral_var ? AFFWP.referral_var : affwp_get_cookie_item( affwp_debug_vars.ref_cookie ) ), 135 visit_cookie : affwp_get_cookie_item( affwp_debug_vars.visit_cookie ) ? JSON.stringify( affwp_get_cookie_item( affwp_debug_vars.visit_cookie ) ) : 'unavailable', 136 credit_last : AFFWP.referral_credit_last ? JSON.stringify( AFFWP.referral_credit_last ) : 'unavailable', 137 campaign : JSON.stringify( affwp_get_query_vars()['campaign'] ? affwp_get_query_vars()['campaign'] : affwp_get_cookie_item( affwp_debug_vars.campaign_cookie ) ), 138 currency : affwp_debug_vars.currency.length ? JSON.stringify( affwp_debug_vars.currency ) : 'unavailable', 139 version : affwp_debug_vars.version.length ? JSON.stringify( affwp_debug_vars.version ) : 'unavailable' 140 } 141 142 return vars; 143 } 144 145 /** 146 * Get all active integrations. 147 * 148 * @since 2.0 149 * 150 * @return {array} All active AffiliateWP integrations on the site. 151 */ 152 function affwp_debug_get_integrations() { 153 154 if ( typeof affwp_debug_vars !== 'undefined' ) { 155 return affwp_debug_vars.integrations; 156 } else { 157 return false;
158 } 159 160 } 161 162 /** 163 * Returns the current time via the performance timing API. 164 * 165 * @since 2.0 166 * @return {int} The current browser client time. 167 */ 168 function affwp_debug_print_time() { 169 return performance.now(); 170 } 171 172 /** 173 * Halts all JavaScript at the given callable of AFFWP.debug_utility.halt(). 174 * 175 * - Useful in implementing step-debuggers, and breakpoints. 176 * 177 * @since 2.0 178 * 179 * @param {string} errorMessage Error message. Optional. 180 * 181 * @return void 182 */ 183 function affwp_debug_halt( errorMessage ) { 184 if ( errorMessage ) { 185 console.affwp( errorMessage ); 186 console.log( '\n' ); 187 } 188 189 console.affwp( 'Halting at ' + affwp_debug_print_time() ); 190 191 debugger; 192 } 193 194 /** 195 * Outputs any available debug data 196 * 197 * @since 2.0 198 * 199 * @param {string} heading Optional title heading. Renders before the tabular data output. 200 * @param {array} debugData Available debug data. 201 * 202 * @return void 203 */ 204 function affwp_debug_output() { 205 206 console.affwp( 'The following data is provided from AffiliateWP debug mode. To disable this information, please deactivate debug mode in AffiliateWP.' ); 207 208 console.affwp( 'Available debug data: ' + '\n' + JSON.stringify( 209 Object( 210 affwp_debug_inline_vars() 211 ) 212 ) 213 ); 214 215 console.affwp( 'Integrations' + '\n' + JSON.stringify( 216 Object( 217 affwp_debug_get_integrations() 218 ) 219 ) 220 ); 221 } 222 223 /** 224 * Defines styles for AffiliateWP-related console output. 225 * 226 * @since 2.0 227 * @see console.affwp 228 */ 229 var affwpConsoleStyles = [ 230 'background: transparent' 231 , 'border-bottom: 2px solid #E34F43' 232 , 'color: black' 233 , 'display: block' 234 , 'line-height: 18px' 235 , 'text-align: left' 236 , 'margin: 4px' 237 , 'font-weight: bold' 238 ].join( ';' ); 239 240 /** 241 * An extension of the console.log prototype. 242 * 243 * Usage: 244 * - Callable with `console.affwp( "The error or message" )` 245 * - Disambiguates the source of the error or message. 246 * 247 * @since 2.0 248 * 249 * @return void 250 */ 251 console.affwp = function(){ 252 Array.prototype.unshift.call( 253 arguments, '%c' + ' * AffiliateWP: ', affwpConsoleStyles + ' *' ); 254 console.log.apply( console, arguments ); 255 }; 256 257 /** 258 * Print debug info to the console. 259 */ 260 affwp_debug_output(); 261 } 262 263 /* 264 * End debug data 265 */ 266 267 if( '1' != credit_last && ref_cookie ) { 268 return; 269 } 270 271 var ref = affwp_get_query_vars()[AFFWP.referral_var]; 272 var campaign = affwp_get_query_vars()['campaign']; 273 274 if( typeof ref == 'undefined' || $.isFunction( ref ) ) { 275 276 // See if we are using a pretty affiliate URL 277 var path = window.location.pathname.split( '/' ); 278 $.each( path, function( key, value ) { 279 if( AFFWP.referral_var == value ) { 280 ref = path[ key + 1 ]; 281 } 282 }); 283 284 } 285 286 if( $.isFunction( ref ) ) { 287 return; 288 } 289 290 if( typeof ref != 'undefined' && ! $.isNumeric( ref ) ) { 291 292 // If a username was provided instead of an affiliate ID number, we need to retrieve the ID 293 $.ajax({ 294 type: "POST", 295 data: { 296 action: 'affwp_get_affiliate_id', 297 affiliate: ref 298 }, 299 url: affwp_scripts.ajaxurl, 300 success: function (response) { 301 if( '1' == response.data.success ) { 302 303 if( '1' == credit_last && ref_cookie && ref_cookie != response.data.affiliate_id ) { 304 $.removeCookie( affwp_debug_vars.ref_cookie ); 305 } 306 307 if( ( '1' == credit_last && ref_cookie && ref_cookie != response.data.affiliate_id ) || ! ref_cookie ) { 308 affwp_track_visit( response.data.affiliate_id, campaign ); 309 } 310 311 } 312 } 313 314 }).fail(function (response) { 315 if ( window.console && window.console.log ) { 316 console.log( response ); 317 } 318 }); 319 320 } else { 321 322 // If a referral var is present and a referral cookie is not already set 323 if( ref && ! ref_cookie ) { 324 affwp_track_visit( ref, campaign ); 325 } else if( '1' == credit_last && ref && ref_cookie && ref !== ref_cookie ) { 326 $.removeCookie( affwp_debug_vars.ref_cookie ); 327 affwp_track_visit( ref, campaign ); 328 } 329 330 } 331 332 /** 333 * Tracks an affiliate visit. 334 * 335 * @since 1.0 336 * 337 * @param {int} affiliate_id The affiliate ID. 338 * @param {string} url_campaign The campaign, if provided. 339 * 340 * @return void 341 */ 342 function affwp_track_visit( affiliate_id, url_campaign ) { 343 344 // Set the cookie and expire it after 24 hours 345 affwp_set_cookie( affwp_debug_vars.ref_cookie, affiliate_id ); 346 347 // Fire an ajax request to log the hit 348 $.ajax({ 349 type: "POST", 350 data: { 351 action: 'affwp_track_visit', 352 affiliate: affiliate_id, 353 campaign: url_campaign, 354 url: document.URL, 355 referrer: document.referrer 356 }, 357 url: affwp_scripts.ajaxurl, 358 success: function (response) { 359 affwp_set_cookie( affwp_debug_vars.visit_cookie, response ); 360 affwp_set_cookie( affwp_debug_vars.campaign_cookie, url_campaign ); 361 } 362 363 }).fail(function (response) { 364 if ( window.console && window.console.log ) { 365 console.log( response ); 366 } 367 }); 368 369 } 370
371 /** 372 * Set a cookie, with optional domain if set. Note that providing *any* domain will 373 * set the cookie domain with a leading dot, indicating it should be sent to sub-domains. 374 * 375 * example: host.tld 376 * 377 * - $.cookie( 'some_cookie', ...) = cookie domain: host.tld 378 * - $.cookie ('some_cookie', ... domain: 'host.tld' ) = .host.tld 379 * 380 * @since 2.1.10 381 * 382 * @param {string} name cookie name, e.g. affwp_ref 383 * @param {string} value cookie value 384 */ 385 function affwp_set_cookie( name, value ) { 386 387 if ( 'cookie_domain' in AFFWP ) { 388 $.cookie( name, value, { expires: AFFWP.expiration, path: '/', domain: AFFWP.cookie_domain } ); 389 } else { 390 $.cookie( name, value, { expires: AFFWP.expiration, path: '/' } ); 391 } 392 } 393 394 /** 395 * Gets url query variables from the current URL. 396 * 397 * @since 1.0 398 * 399 * @return {array} vars The url query variables in the current site url, if present. 400 */ 401 function affwp_get_query_vars() { 402 var vars = [], hash; 403 var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 404 for(var i = 0; i < hashes.length; i++) { 405 hash = hashes[i].split('='); 406 vars.push(hash[0]); 407 408 var key = typeof hash[1] == 'undefined' ? 0 : 1; 409 410 // Remove fragment identifiers 411 var n = hash[key].indexOf('#'); 412 hash[key] = hash[key].substring(0, n != -1 ? n : hash[key].length); 413 vars[hash[0]] = hash[key]; 414 } 415 return vars; 416 } 417 418});
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.