adsense.js
60 lines
| 1 | export function authUrl() { |
| 2 | const { wizard } = advancedAds; |
| 3 | const params = new URLSearchParams({ |
| 4 | client_id: wizard.clientId, |
| 5 | redirect_uri: wizard.redirectUri, |
| 6 | state: wizard.state, |
| 7 | access_type: 'offline', |
| 8 | include_granted_scopes: 'true', |
| 9 | prompt: 'consent', |
| 10 | response_type: 'code', |
| 11 | }).toString(); |
| 12 | return `${wizard.authUrl}&${params}`; |
| 13 | } |
| 14 | |
| 15 | export function hasAuthCode() { |
| 16 | const params = new URLSearchParams(document.location.search); |
| 17 | return ( |
| 18 | params.get('code') && |
| 19 | 'adsense' === params.get('route') && |
| 20 | params.get('nonce') |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | export function submitCode() { |
| 25 | const params = new URLSearchParams(document.location.search); |
| 26 | return wp.ajax.post('advads_gadsense_mapi_confirm_code', { |
| 27 | nonce: params.get('nonce'), |
| 28 | code: params.get('code'), |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | export function getAccountDetails(tokenData) { |
| 33 | const params = new URLSearchParams(document.location.search); |
| 34 | return wp.ajax.post('advads_gadsense_mapi_get_details', { |
| 35 | nonce: params.get('nonce'), |
| 36 | token_data: tokenData, |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | export function getErrorMessage(response) { |
| 41 | let message = response.statusText; |
| 42 | try { |
| 43 | message = response.responseJSON.data.error; |
| 44 | } catch (e) { |
| 45 | try { |
| 46 | message = response.responseJSON.data.msg; |
| 47 | } catch (ee) { |
| 48 | try { |
| 49 | message = response.responseJSON.data.raw; |
| 50 | } catch (eee) { |
| 51 | try { |
| 52 | message = response.responseJSON.data.error_msg; |
| 53 | } catch (eeee) {} |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return message; |
| 59 | } |
| 60 |