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 / csv.js

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

96 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Client-side CSV export ( Blob + BOM ).
3 *
4 * RFC-4180 escaping plus a CSV-injection guard: values starting with
5 * = + - @ get a leading apostrophe so Excel never executes them.
6 *
7 * @since 4.4.2
8 * @version 1.0.0
9 */
10
11 import { lpStatsState } from './state.js';
12
13 const sanitizeSegment = ( segment ) => {
14 const clean = String( segment ?? '' )
15 .toLowerCase()
16 .replace( /[^a-z0-9-]+/g, '-' )
17 .replace( /^-+|-+$/g, '' );
18
19 return clean || 'data';
20 };
21
22 /**
23 * `learnpress-{tab}-{table}-{filtertype}.csv`, all segments sanitized.
24 *
25 * @param {string} tab
26 * @param {string} table
27 * @return {string} Filename.
28 */
29 export const buildCsvFilename = ( tab, table ) => {
30 const { filtertype } = lpStatsState.get();
31
32 return `learnpress-${ sanitizeSegment( tab ) }-${ sanitizeSegment(
33 table
34 ) }-${ sanitizeSegment( filtertype ) }.csv`;
35 };
36
37 const escapeCell = ( value ) => {
38 let str = null == value ? '' : String( value );
39
40 if ( /^[=+\-@]/.test( str ) ) {
41 str = `'${ str }`;
42 }
43
44 if ( /[",\n\r]/.test( str ) ) {
45 str = `"${ str.replace( /"/g, '""' ) }"`;
46 }
47
48 return str;
49 };
50
51 /**
52 * Build and download a CSV from a data-table handle.
53 *
54 * @param {string} filename Full filename (see buildCsvFilename).
55 * @param {Array} columns Column definitions ({ key, label, csv? }).
56 * @param {Array} rows Row objects.
57 */
58 export const exportCsv = ( filename, columns = [], rows = [] ) => {
59 if ( ! columns.length ) {
60 return;
61 }
62
63 const lines = [
64 columns.map( ( column ) => escapeCell( column.label ) ).join( ',' ),
65 ];
66
67 rows.forEach( ( row ) => {
68 lines.push(
69 columns
70 .map( ( column ) => {
71 const value =
72 'function' === typeof column.csv
73 ? column.csv( row )
74 : row[ column.key ];
75
76 return escapeCell( value );
77 } )
78 .join( ',' )
79 );
80 } );
81
82 // BOM keeps Excel reading UTF-8 (Vietnamese titles etc.).
83 const blob = new Blob( [ '\u{FEFF}' + lines.join( '\r\n' ) ], {
84 type: 'text/csv;charset=utf-8;',
85 } );
86 const url = URL.createObjectURL( blob );
87
88 const link = document.createElement( 'a' );
89 link.href = url;
90 link.download = filename;
91 document.body.appendChild( link );
92 link.click();
93 document.body.removeChild( link );
94 URL.revokeObjectURL( url );
95 };
96