PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / trunk
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses vtrunk
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / assets / src / js / admin / statistics / api.js

api.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses trunk, at assets/src/js/admin/statistics/api.js

76 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Statistics dashboard fetch wrapper + escaping helper.
3 *
4 * Every statistics request goes through lpStatsFetch so the localized globals
5 * (lpDataAdmin for REST root/nonce, lpAdminStatisticSettings for config) are
6 * read in exactly one file, and lpFetchAPI's blind spots are normalized here:
7 * it never rejects on HTTP error codes, so anything but status 'success'
8 * is routed to the error callback.
9 *
10 * @since 4.4.2
11 * @version 1.0.0
12 */
13
14 import * as lpUtils from 'lpAssetsJsPath/utils.js';
15 import { lpStatsState, LP_STATS_RANGE_RESOLVED } from './state.js';
16
17 export const getStatsConfig = () => window.lpAdminStatisticSettings || {};
18
19 export const getStatsI18n = ( key, fallback = '' ) => {
20 const { i18n = {} } = getStatsConfig();
21 return i18n[ key ] || fallback;
22 };
23
24 /**
25 * Fetch a statistics endpoint with the global filter state applied.
26 *
27 * @param {string} endpoint Route below the statistics namespace, e.g. 'filter-options'.
28 * @param {Object} extraArgs Query args merged over the state (tab-specific params).
29 * @param {Object} functions { before, success, error, completed } — success only
30 * fires on status 'success'; error receives an Error.
31 */
32 export const lpStatsFetch = ( endpoint, extraArgs = {}, functions = {} ) => {
33 const lpDataAdmin = window.lpDataAdmin || {};
34 const restNamespace = getStatsConfig().restNamespace || 'lp/v1/statistics';
35 const url = lpUtils.lpAddQueryArgs(
36 `${ lpDataAdmin.lp_rest_url || '/wp-json/' }${ restNamespace }/${ endpoint }`,
37 { ...lpStatsState.get(), ...extraArgs }
38 );
39
40 const onError =
41 'function' === typeof functions.error
42 ? functions.error
43 : ( err ) => console.error( 'LP Statistics:', err );
44
45 lpUtils.lpFetchAPI(
46 url,
47 { headers: { 'X-WP-Nonce': lpDataAdmin.nonce || '' } },
48 {
49 ...functions,
50 success: ( response ) => {
51 if ( response && 'success' === response.status ) {
52 // Broadcast the server-resolved range so the filter bar can
53 // reconcile its toggle label ( fixes the past-midnight case ).
54 const range = response.data && response.data.range;
55 if ( range && range.label ) {
56 document.dispatchEvent(
57 new CustomEvent( LP_STATS_RANGE_RESOLVED, { detail: range } )
58 );
59 }
60 if ( 'function' === typeof functions.success ) {
61 functions.success( response );
62 }
63 } else {
64 onError(
65 new Error(
66 ( response && response.message ) ||
67 getStatsI18n( 'loadError', 'Request failed.' )
68 )
69 );
70 }
71 },
72 error: onError,
73 }
74 );
75 };
76