| 1 |
var automatorwp_redirect_in_progress = false; |
| 2 |
|
| 3 |
/** |
| 4 |
* Server side check to meet if user should get redirected |
| 5 |
* |
| 6 |
* @since 1.4.3 |
| 7 |
*/ |
| 8 |
function automatorwp_check_for_redirect() { |
| 9 |
|
| 10 |
var $ = jQuery; |
| 11 |
|
| 12 |
var user_id = parseInt( automatorwp_redirect.user_id ); |
| 13 |
|
| 14 |
if( user_id === 0 || isNaN( user_id ) ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
if( automatorwp_redirect_in_progress ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
automatorwp_redirect_in_progress = true; |
| 23 |
|
| 24 |
$.ajax({ |
| 25 |
url: automatorwp_redirect.ajaxurl, |
| 26 |
method: 'POST', |
| 27 |
data: { |
| 28 |
action: 'automatorwp_check_for_redirect', |
| 29 |
nonce: automatorwp_redirect.nonce, |
| 30 |
user_id: automatorwp_redirect.user_id, |
| 31 |
}, |
| 32 |
success: function( response ) { |
| 33 |
|
| 34 |
// Sanitization check |
| 35 |
if( ! automatorwp_is_response_valid_for_redirect( response ) ) { |
| 36 |
automatorwp_redirect_in_progress = false; |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// Try to redirect |
| 41 |
if( ! automatorwp_redirect_to_url( response.data.redirect_url ) ) { |
| 42 |
automatorwp_redirect_in_progress = false; |
| 43 |
} |
| 44 |
|
| 45 |
}, |
| 46 |
error: function( response ) { |
| 47 |
|
| 48 |
// Sanitization check |
| 49 |
if( ! automatorwp_is_response_valid_for_redirect( response ) ) { |
| 50 |
automatorwp_redirect_in_progress = false; |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
// Try to redirect |
| 55 |
if( ! automatorwp_redirect_to_url( response.data.redirect_url ) ) { |
| 56 |
automatorwp_redirect_in_progress = false; |
| 57 |
} |
| 58 |
|
| 59 |
} |
| 60 |
}); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Check if the response object has all required properties |
| 66 |
* |
| 67 |
* @since 2.4.1 |
| 68 |
* |
| 69 |
* @param {object} response |
| 70 |
*/ |
| 71 |
function automatorwp_is_response_valid_for_redirect( response ) { |
| 72 |
|
| 73 |
if( response === undefined ) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
if( response.data === undefined ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
if( response.data.redirect_url === undefined ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
return true; |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Server side check to meet if user should get redirected |
| 91 |
* |
| 92 |
* @since 1.4.3 |
| 93 |
* |
| 94 |
* @param {string} url |
| 95 |
*/ |
| 96 |
function automatorwp_redirect_to_url( url ) { |
| 97 |
|
| 98 |
if( url === undefined ) { |
| 99 |
url = ''; |
| 100 |
} |
| 101 |
|
| 102 |
if( ! url.length ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
document.location.href = url; |
| 107 |
|
| 108 |
return true; |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Check if request URL or its data should be excluded from the redirect check |
| 114 |
* |
| 115 |
* @since 1.6.9 |
| 116 |
* |
| 117 |
* @param {string} url |
| 118 |
* @param {string} data |
| 119 |
* |
| 120 |
* @return {boolean} |
| 121 |
*/ |
| 122 |
function automatorwp_redirect_is_url_excluded( url, data ) { |
| 123 |
|
| 124 |
if( url === undefined ) { |
| 125 |
url = ''; |
| 126 |
} |
| 127 |
|
| 128 |
if( data === undefined ) { |
| 129 |
data = url; |
| 130 |
} |
| 131 |
|
| 132 |
if ( typeof data !== 'string' ) { |
| 133 |
data = url; |
| 134 |
} |
| 135 |
|
| 136 |
// Check for excluded urls |
| 137 |
var excluded_url = false; |
| 138 |
|
| 139 |
automatorwp_redirect.excluded_urls.forEach( function ( to_match ) { |
| 140 |
if( url !== undefined && url.includes( to_match ) || url === to_match ) { |
| 141 |
excluded_url = true; |
| 142 |
} |
| 143 |
} ); |
| 144 |
|
| 145 |
if( excluded_url ) { |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
// Check for excluded data |
| 150 |
var excluded_data = false; |
| 151 |
|
| 152 |
automatorwp_redirect.excluded_data.forEach( function ( to_match ) { |
| 153 |
if( data !== undefined && data.includes( to_match ) ) { |
| 154 |
excluded_data = true; |
| 155 |
} |
| 156 |
} ); |
| 157 |
|
| 158 |
if( excluded_data ) { |
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
// If is an ajax call, check for excluded ajax actions |
| 163 |
if( url !== undefined && url.includes('admin-ajax.php') ) { |
| 164 |
|
| 165 |
var excluded_action = false; |
| 166 |
|
| 167 |
automatorwp_redirect.excluded_ajax_actions.forEach( function ( to_match ) { |
| 168 |
if( data !== undefined && data.includes( 'action=' + to_match ) ) { |
| 169 |
excluded_action = true; |
| 170 |
} |
| 171 |
|
| 172 |
if( url !== undefined && url.includes( 'action=' + to_match ) ) { |
| 173 |
excluded_action = true; |
| 174 |
} |
| 175 |
} ); |
| 176 |
|
| 177 |
if( excluded_action ) { |
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
return false; |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
(function ( $ ) { |
| 188 |
|
| 189 |
// Listen for any ajax success |
| 190 |
$(document).ajaxSuccess( function ( event, request, settings ) { |
| 191 |
|
| 192 |
// Bail if URL is excluded |
| 193 |
if( automatorwp_redirect_is_url_excluded( settings.url, settings.data ) ) { |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
var status = parseInt( request.status ); |
| 198 |
|
| 199 |
if ( status === 200 ) { |
| 200 |
automatorwp_check_for_redirect(); |
| 201 |
} |
| 202 |
|
| 203 |
}) ; |
| 204 |
|
| 205 |
})( jQuery ); |