common.js
2 weeks ago
rest.js
2 weeks ago
search.js
2 weeks ago
site-import.js
2 years ago
svg.js
2 weeks ago
rest.js
139 lines
| 1 | /* global tiobDash, jQuery, ajaxurl */ |
| 2 | /* eslint-disable no-console */ |
| 3 | |
| 4 | export const send = ( route, data, simple = false ) => { |
| 5 | return requestData( route, simple, data ); |
| 6 | }; |
| 7 | |
| 8 | export const get = ( route, simple = false, useNonce = true, signal = null ) => { |
| 9 | return requestData( route, simple, {}, 'GET', useNonce, signal ); |
| 10 | }; |
| 11 | |
| 12 | const requestData = async ( |
| 13 | route, |
| 14 | simple = false, |
| 15 | data = {}, |
| 16 | method = 'POST', |
| 17 | useNonce = true, |
| 18 | signal = null |
| 19 | ) => { |
| 20 | const options = { |
| 21 | method, |
| 22 | headers: { |
| 23 | Accept: 'application/json', |
| 24 | 'Content-Type': 'application/json', |
| 25 | }, |
| 26 | }; |
| 27 | |
| 28 | if ( tiobDash.params.site_url ) { |
| 29 | const url = new URL( route ); |
| 30 | url.searchParams.append( |
| 31 | 'site_url', |
| 32 | encodeURIComponent( tiobDash.params.site_url ) |
| 33 | ); |
| 34 | route = url; |
| 35 | } |
| 36 | |
| 37 | if ( useNonce ) { |
| 38 | options.headers[ 'x-wp-nonce' ] = tiobDash.nonce; |
| 39 | } |
| 40 | |
| 41 | // Optional AbortSignal so callers can cancel an in-flight request. |
| 42 | if ( signal ) { |
| 43 | options.signal = signal; |
| 44 | } |
| 45 | |
| 46 | if ( 'POST' === method ) { |
| 47 | options.body = JSON.stringify( data ); |
| 48 | } |
| 49 | |
| 50 | return await fetch( route, options ).then( ( response ) => { |
| 51 | return simple ? response : response.json(); |
| 52 | } ); |
| 53 | }; |
| 54 | |
| 55 | export const ajaxAction = async ( |
| 56 | route, |
| 57 | action = '', |
| 58 | useNonce = '', |
| 59 | data = {} |
| 60 | ) => { |
| 61 | const formData = new FormData(); |
| 62 | formData.append( 'nonce', useNonce ); |
| 63 | formData.append( 'action', action ); |
| 64 | if ( Object.keys( data ).length > 0 ) { |
| 65 | for ( const [ key, value ] of Object.entries( data ) ) { |
| 66 | formData.append( key, value ); |
| 67 | } |
| 68 | } |
| 69 | const options = { |
| 70 | method: 'POST', |
| 71 | headers: { |
| 72 | Accept: 'application/json', |
| 73 | }, |
| 74 | body: formData, |
| 75 | }; |
| 76 | |
| 77 | return await fetch( route, options ).then( () => { |
| 78 | return true; |
| 79 | } ); |
| 80 | }; |
| 81 | |
| 82 | export const track = async ( trackingId = '', data ) => { |
| 83 | try { |
| 84 | const response = await fetch( |
| 85 | 'https://api.themeisle.com/tracking/onboarding', |
| 86 | { |
| 87 | method: 'POST', |
| 88 | headers: { |
| 89 | 'Content-Type': 'application/json', |
| 90 | }, |
| 91 | body: JSON.stringify( { |
| 92 | _id: trackingId, |
| 93 | data, |
| 94 | } ), |
| 95 | } |
| 96 | ); |
| 97 | |
| 98 | if ( ! response.ok ) { |
| 99 | console.error( `HTTP error! Status: ${ response.status }` ); |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | const jsonResponse = await response.json(); |
| 104 | |
| 105 | const validCodes = [ 'success', 'invalid' ]; // Add valid codes to this array |
| 106 | if ( ! validCodes.includes( jsonResponse.code ) ) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | if ( jsonResponse.code === 'invalid' ) { |
| 111 | console.error( jsonResponse.message ); |
| 112 | return false; |
| 113 | } |
| 114 | const responseData = jsonResponse.data; |
| 115 | |
| 116 | return responseData.id || false; |
| 117 | } catch ( error ) { |
| 118 | console.error( error ); |
| 119 | return false; |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | /** |
| 124 | * Get logs from server using ajax. |
| 125 | * |
| 126 | * @param {Object} args - ajax arguments |
| 127 | */ |
| 128 | export const getLogsFromServer = ( args ) => { |
| 129 | jQuery.ajax( { |
| 130 | type: 'post', |
| 131 | url: ajaxurl, |
| 132 | data: { |
| 133 | action: 'tpc_get_logs', |
| 134 | nonce: tiobDash.nonce, |
| 135 | }, |
| 136 | ...args, |
| 137 | } ); |
| 138 | }; |
| 139 |