PluginProbe ʕ •ᴥ•ʔ
Starter Sites & Templates by Neve / trunk
Starter Sites & Templates by Neve vtrunk
1.4.0 1.3.0 1.2.29 1.2.28 1.2.6 1.2.7 1.2.8 1.2.9 trunk 1.0.10 1.0.11 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.27 1.1.28 1.1.29 1.1.3 1.1.30 1.1.31 1.1.32 1.1.33 1.1.34 1.1.35 1.1.36 1.1.37 1.1.38 1.1.39 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.3 1.2.4 1.2.5
templates-patterns-collection / onboarding / src / utils / rest.js
templates-patterns-collection / onboarding / src / utils Last commit date
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