| 1 |
const endpoint = wpupg_public.api_endpoint; |
| 2 |
|
| 3 |
export default { |
| 4 |
loadItems( body ) { |
| 5 |
let loadEndpoint = endpoint; |
| 6 |
|
| 7 |
// WP Extended Search compatibility for text search filter. |
| 8 |
if ( body.hasOwnProperty( 'args' ) && body.args.hasOwnProperty( 'wpes' ) && 0 < body.args.wpes ) { |
| 9 |
loadEndpoint += `?wpessid=${ body.args.wpes }`; |
| 10 |
} |
| 11 |
|
| 12 |
let headers = { |
| 13 |
'Accept': 'application/json', |
| 14 |
'Content-Type': 'application/json', |
| 15 |
// Don't cache API calls. |
| 16 |
'Cache-Control': 'no-cache, no-store, must-revalidate', |
| 17 |
'Pragma': 'no-cache', |
| 18 |
'Expires': 0, |
| 19 |
}; |
| 20 |
|
| 21 |
if ( 0 < parseInt( wpupg_public.user ) ) { |
| 22 |
headers['X-WP-Nonce'] = wpupg_public.api_nonce; |
| 23 |
} |
| 24 |
|
| 25 |
const args = { |
| 26 |
method: 'POST', |
| 27 |
headers, |
| 28 |
credentials: 'same-origin', |
| 29 |
body: JSON.stringify( body ), |
| 30 |
}; |
| 31 |
|
| 32 |
return fetch( loadEndpoint, args ).then( function ( response ) { |
| 33 |
if ( response.ok ) { |
| 34 |
return response.json(); |
| 35 |
} else { |
| 36 |
// Log errors in console and try to get as much debug information as possible. |
| 37 |
console.log(loadEndpoint, args); |
| 38 |
console.log(response); |
| 39 |
|
| 40 |
try { |
| 41 |
response.text().then(text => { |
| 42 |
console.log(text); |
| 43 |
}) |
| 44 |
} catch(e) { |
| 45 |
console.log(e); |
| 46 |
} |
| 47 |
|
| 48 |
return false; |
| 49 |
} |
| 50 |
} ); |
| 51 |
}, |
| 52 |
}; |
| 53 |
|