licenses-api.js
273 lines
| 1 | import apiFetch from '@wordpress/api-fetch'; |
| 2 | import { dispatch, select } from '@wordpress/data'; |
| 3 | |
| 4 | import { STORE_NAME } from '@admin/store'; |
| 5 | |
| 6 | const API_PATH = '/advanced-ads/v1/licenses'; |
| 7 | const AUTOUPDATE_API_PATH = '/advanced-ads/v1/plugin-autoupdate'; |
| 8 | const API_URL = `${ advancedAds.endpoints.siteUrl }/wp-json${ API_PATH }`; |
| 9 | |
| 10 | export function extractApiMessage( payload ) { |
| 11 | if ( |
| 12 | ! payload || |
| 13 | typeof payload !== 'object' || |
| 14 | Array.isArray( payload ) |
| 15 | ) { |
| 16 | return null; |
| 17 | } |
| 18 | |
| 19 | const message = payload.message ?? payload.data?.message; |
| 20 | |
| 21 | if ( message === undefined || message === null ) { |
| 22 | return null; |
| 23 | } |
| 24 | |
| 25 | const text = String( message ).trim(); |
| 26 | if ( text === '' ) { |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | return text; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Pull a user-facing message from a failed apiFetch / REST error. |
| 35 | * |
| 36 | * @param {unknown} err Error from apiFetch. |
| 37 | * @return {string|null} Message text or null when none is available. |
| 38 | */ |
| 39 | export function extractApiErrorMessage( err ) { |
| 40 | if ( ! err || typeof err !== 'object' ) { |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | const dataMessage = err.data?.message; |
| 45 | if ( dataMessage !== undefined && dataMessage !== null ) { |
| 46 | const text = String( dataMessage ).trim(); |
| 47 | if ( text !== '' ) { |
| 48 | return text; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if ( err instanceof Error ) { |
| 53 | const text = String( err.message ?? '' ).trim(); |
| 54 | if ( text !== '' ) { |
| 55 | return text; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | const message = err.message; |
| 60 | if ( message !== undefined && message !== null ) { |
| 61 | const text = String( message ).trim(); |
| 62 | if ( text !== '' ) { |
| 63 | return text; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | function safeObject( val ) { |
| 71 | return val && typeof val === 'object' ? val : {}; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Normalize GET/POST licenses REST payload (array legacy or object with map). |
| 76 | * |
| 77 | * @param {Array|Object} payload REST licenses payload. |
| 78 | * @return {{ licenses: Array, appliedAddonKeyMap: Object, autoUpdateStates: Object, addonInstallStates: Object }} Normalized licenses response object. |
| 79 | */ |
| 80 | export function normalizeLicensesResponse( payload ) { |
| 81 | if ( Array.isArray( payload ) ) { |
| 82 | return { |
| 83 | licenses: payload, |
| 84 | appliedAddonKeyMap: {}, |
| 85 | autoUpdateStates: {}, |
| 86 | addonInstallStates: {}, |
| 87 | lastSyncAt: 0, |
| 88 | expiryNoticeFlags: {}, |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | return { |
| 93 | licenses: Array.isArray( payload?.licenses ) ? payload.licenses : [], |
| 94 | appliedAddonKeyMap: safeObject( payload?.appliedAddonKeyMap ), |
| 95 | autoUpdateStates: safeObject( payload?.autoUpdateStates ), |
| 96 | addonInstallStates: safeObject( payload?.addonInstallStates ), |
| 97 | lastSyncAt: payload?.lastSyncAt ?? 0, |
| 98 | expiryNoticeFlags: safeObject( payload?.expiryNoticeFlags ), |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | function applyLicensesPayloadToStore( payload ) { |
| 103 | const { |
| 104 | licenses, |
| 105 | appliedAddonKeyMap, |
| 106 | autoUpdateStates, |
| 107 | addonInstallStates, |
| 108 | lastSyncAt, |
| 109 | expiryNoticeFlags, |
| 110 | } = normalizeLicensesResponse( payload ); |
| 111 | dispatch( STORE_NAME ).setLicenses( |
| 112 | licenses, |
| 113 | appliedAddonKeyMap, |
| 114 | autoUpdateStates, |
| 115 | addonInstallStates, |
| 116 | lastSyncAt, |
| 117 | expiryNoticeFlags |
| 118 | ); |
| 119 | return licenses; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Toggle per-plugin auto-update (advanced-ads-{addon}-autoupdate). |
| 124 | * |
| 125 | * @param {string} addonId Short id (pro, tracking, …) or "main" for base plugin. |
| 126 | * @param {string} state "on" or "off". |
| 127 | */ |
| 128 | export async function togglePluginAutoUpdate( addonId, state ) { |
| 129 | const response = await apiFetch( { |
| 130 | path: AUTOUPDATE_API_PATH, |
| 131 | method: 'POST', |
| 132 | data: { |
| 133 | addonId: addonId || 'main', |
| 134 | state, |
| 135 | }, |
| 136 | } ); |
| 137 | |
| 138 | if ( |
| 139 | response?.autoUpdateStates && |
| 140 | typeof response.autoUpdateStates === 'object' |
| 141 | ) { |
| 142 | dispatch( STORE_NAME ).setAutoUpdateStates( response.autoUpdateStates ); |
| 143 | } |
| 144 | |
| 145 | return response; |
| 146 | } |
| 147 | |
| 148 | // ----------- Internal Api call --------------- // |
| 149 | |
| 150 | export function fetchLicenses() { |
| 151 | return apiFetch( { url: API_URL, method: 'GET' } ); |
| 152 | } |
| 153 | |
| 154 | export function saveLicenses( |
| 155 | licenses, |
| 156 | { |
| 157 | activate = false, |
| 158 | activatingLicenseKey = '', |
| 159 | activatingAddonId = '', |
| 160 | installOnly = false, |
| 161 | deactivatingAddonId = '', |
| 162 | deactivatingLicenseKey = '', |
| 163 | signal = undefined, |
| 164 | } = {} |
| 165 | ) { |
| 166 | return apiFetch( { |
| 167 | url: API_URL, |
| 168 | method: 'POST', |
| 169 | signal, |
| 170 | data: { |
| 171 | licenses: Array.isArray( licenses ) ? licenses : [], |
| 172 | activate, |
| 173 | activatingLicenseKey: activatingLicenseKey || '', |
| 174 | activatingAddonId: activatingAddonId || '', |
| 175 | installOnly, |
| 176 | deactivatingAddonId: deactivatingAddonId || '', |
| 177 | deactivatingLicenseKey: deactivatingLicenseKey || '', |
| 178 | }, |
| 179 | } ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Remove one license from the current site (Tracking, Pro, All Access, …). |
| 184 | * |
| 185 | * @param {string} licenseKey License key for the card being deactivated. |
| 186 | * @param {Array} licenses Current license list for POST body. |
| 187 | */ |
| 188 | export async function deactivateLicenseOnSite( licenseKey, licenses ) { |
| 189 | const saved = await saveLicenses( licenses, { |
| 190 | deactivatingLicenseKey: licenseKey, |
| 191 | } ); |
| 192 | applyLicensesPayloadToStore( saved ); |
| 193 | return saved; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Apply license and activate one All Access add-on on this site. |
| 198 | * |
| 199 | * @param {string} licenseKey All Access license key. |
| 200 | * @param {string} addonId Short add-on id (pro, tracking, …). |
| 201 | * @param {Array} licenses Current license list for POST body. |
| 202 | */ |
| 203 | export async function activateAddonFromLicense( |
| 204 | licenseKey, |
| 205 | addonId, |
| 206 | licenses |
| 207 | ) { |
| 208 | const saved = await saveLicenses( licenses, { |
| 209 | activatingLicenseKey: licenseKey, |
| 210 | activatingAddonId: addonId, |
| 211 | } ); |
| 212 | applyLicensesPayloadToStore( saved ); |
| 213 | return saved; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Deactivate one All Access add-on plugin on this site (keeps package on disk). |
| 218 | * |
| 219 | * @param {string} addonId Short add-on id (pro, tracking, …). |
| 220 | * @param {Array} licenses Current license list for POST body. |
| 221 | */ |
| 222 | export async function deactivateAddonFromLicense( addonId, licenses ) { |
| 223 | const saved = await saveLicenses( licenses, { |
| 224 | deactivatingAddonId: addonId, |
| 225 | } ); |
| 226 | applyLicensesPayloadToStore( saved ); |
| 227 | return saved; |
| 228 | } |
| 229 | |
| 230 | // ----------- Store Helpers --------------- // |
| 231 | |
| 232 | export async function initLicenses() { |
| 233 | const payload = await fetchLicenses(); |
| 234 | applyLicensesPayloadToStore( payload ); |
| 235 | } |
| 236 | |
| 237 | export async function setLicensesAndSave( licenses, options = {} ) { |
| 238 | dispatch( STORE_NAME ).setLicenses( licenses ); |
| 239 | const saved = await saveLicenses( licenses, options ); |
| 240 | applyLicensesPayloadToStore( saved ); |
| 241 | } |
| 242 | |
| 243 | export async function addLicenseAndSave( license ) { |
| 244 | dispatch( STORE_NAME ).addLicense( license ); |
| 245 | const current = select( STORE_NAME ).getLicenses(); |
| 246 | const saved = await saveLicenses( current ); |
| 247 | applyLicensesPayloadToStore( saved ); |
| 248 | } |
| 249 | |
| 250 | export async function removeLicenseAndSave( licenseId ) { |
| 251 | dispatch( STORE_NAME ).removeLicense( licenseId ); |
| 252 | const current = select( STORE_NAME ).getLicenses(); |
| 253 | const saved = await saveLicenses( current ); |
| 254 | applyLicensesPayloadToStore( saved ); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Activate a license on the shop via plugin REST (shop-first on the server). |
| 259 | * |
| 260 | * @param {string} licenseKey License key. |
| 261 | * @param {AbortSignal} [signal] Optional abort signal. |
| 262 | * @return {Object} Licenses REST payload. |
| 263 | */ |
| 264 | export async function activateLicenseOnShop( licenseKey, signal ) { |
| 265 | const licenses = select( STORE_NAME ).getLicenses(); |
| 266 | const saved = await saveLicenses( licenses, { |
| 267 | activatingLicenseKey: licenseKey, |
| 268 | signal, |
| 269 | } ); |
| 270 | applyLicensesPayloadToStore( saved ); |
| 271 | return saved; |
| 272 | } |
| 273 |