| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
// Fetch settings from the API |
| 4 |
export const fetchSettings = async () => { |
| 5 |
try { |
| 6 |
const response = await apiFetch( { |
| 7 |
path: '/suremails/v1/get-settings', |
| 8 |
method: 'POST', |
| 9 |
headers: { |
| 10 |
'Content-Type': 'application/json', |
| 11 |
'X-WP-Nonce': suremails.nonce, |
| 12 |
}, |
| 13 |
} ); |
| 14 |
|
| 15 |
if ( typeof response !== 'object' ) { |
| 16 |
throw new Error( 'Invalid JSON response' ); |
| 17 |
} |
| 18 |
return response; |
| 19 |
} catch ( error ) { |
| 20 |
throw new Error( error.message || 'Error fetching settings' ); |
| 21 |
} |
| 22 |
}; |
| 23 |
|
| 24 |
// Save settings to the API |
| 25 |
export const saveSettings = async ( updatedSettings ) => { |
| 26 |
try { |
| 27 |
const response = await apiFetch( { |
| 28 |
path: '/suremails/v1/set-settings', |
| 29 |
method: 'POST', |
| 30 |
headers: { |
| 31 |
'X-WP-Nonce': suremails.nonce, |
| 32 |
'Content-Type': 'application/json', |
| 33 |
}, |
| 34 |
body: JSON.stringify( { |
| 35 |
settings: { |
| 36 |
delete_email_logs_after: |
| 37 |
updatedSettings.settings.delete_email_logs_after, |
| 38 |
log_emails: updatedSettings.settings.log_emails, |
| 39 |
email_simulation: updatedSettings.settings.email_simulation, |
| 40 |
default_connection: |
| 41 |
updatedSettings.settings.default_connection, |
| 42 |
emailSummaryActive: |
| 43 |
updatedSettings.settings.emailSummaryActive, |
| 44 |
emailSummaryDay: updatedSettings.settings.emailSummaryDay, |
| 45 |
showInSidebar: updatedSettings.settings.showInSidebar, |
| 46 |
analytics: updatedSettings.settings.analytics, |
| 47 |
}, |
| 48 |
} ), |
| 49 |
} ); |
| 50 |
return response; |
| 51 |
} catch ( error ) { |
| 52 |
throw new Error( error.message || 'Error saving settings' ); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
/** |
| 57 |
* Set the Reputation Shield (Content Guard) activation status. |
| 58 |
* |
| 59 |
* When a boolean `status` is passed, the backend is instructed to set the |
| 60 |
* option to that exact value. If omitted, the endpoint preserves its legacy |
| 61 |
* toggle behavior for backward compatibility. |
| 62 |
* |
| 63 |
* @param {boolean} [status] Desired activation state. |
| 64 |
* @return {Object} The response |
| 65 |
*/ |
| 66 |
export const activateContentGuard = async ( status ) => { |
| 67 |
try { |
| 68 |
const hasStatus = typeof status === 'boolean'; |
| 69 |
return await apiFetch( { |
| 70 |
path: '/suremails/v1/content-guard/activate', |
| 71 |
method: hasStatus ? 'POST' : 'GET', |
| 72 |
headers: { |
| 73 |
'Content-Type': 'application/json', |
| 74 |
'X-WP-Nonce': suremails.nonce, |
| 75 |
}, |
| 76 |
...( hasStatus && { |
| 77 |
data: { status: status ? 'yes' : 'no' }, |
| 78 |
} ), |
| 79 |
} ); |
| 80 |
} catch ( error ) { |
| 81 |
throw new Error( |
| 82 |
error?.data?.message || |
| 83 |
__( 'Error activating Reputation Shield', 'suremails' ) |
| 84 |
); |
| 85 |
} |
| 86 |
}; |
| 87 |
|
| 88 |
/** |
| 89 |
* Save user details for Content Guard |
| 90 |
* |
| 91 |
* @param {Object} userDetails - The user details |
| 92 |
* @return {Object} The response |
| 93 |
*/ |
| 94 |
export const saveUserDetails = async ( userDetails ) => { |
| 95 |
try { |
| 96 |
return await apiFetch( { |
| 97 |
path: '/suremails/v1/content-guard/user-details', |
| 98 |
method: 'POST', |
| 99 |
headers: { |
| 100 |
'X-WP-Nonce': suremails.nonce, |
| 101 |
'Content-Type': 'application/json', |
| 102 |
}, |
| 103 |
body: JSON.stringify( userDetails ), |
| 104 |
} ); |
| 105 |
} catch ( error ) { |
| 106 |
throw new Error( error.data.message || 'Error saving user details' ); |
| 107 |
} |
| 108 |
}; |
| 109 |
|