1
1
// ALM Modules
2
2
import axios from 'axios' ;
3
- import '../scss/ajax-load-more.scss ';
3
+ import DOMPurify from 'dompurify ';
4
4
import { cacheCreateParams , getCache } from './addons/cache' ;
5
5
import { ctaCreateParams } from './addons/call-to-actions' ;
6
6
import { commentsCreateParams } from './addons/comments' ;
@@ -33,15 +33,16 @@ import * as resultsText from './modules/resultsText';
33
33
import setLocalizedVars from './modules/setLocalizedVars' ;
34
34
import { tableOfContents } from './modules/tableofcontents' ;
35
35
36
+ import '../scss/ajax-load-more.scss' ;
37
+
36
38
// External packages.
37
39
const qs = require ( 'qs' ) ;
38
40
const imagesLoaded = require ( 'imagesloaded' ) ;
39
41
40
42
// Axios Config.
41
43
axios . defaults . headers . common [ 'X-Requested-With' ] = 'XMLHttpRequest' ;
42
-
43
- // Axios Interceptor for nested data objects
44
44
axios . interceptors . request . use ( ( config ) => {
45
+ // Axios Interceptor for nested data objects
45
46
config . paramsSerializer = ( params ) => {
46
47
// Qs is already included in the Axios package
47
48
return qs . stringify ( params , {
@@ -144,15 +145,16 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
144
145
alm . trigger = alm . btnWrap [ alm . btnWrap . length - 1 ] ;
145
146
alm . button = alm ?. trigger ?. querySelector ( 'button.alm-load-more-btn' ) || null ;
146
147
148
+ // Button Labels.
147
149
alm . button_labels = {
148
- default : alm ?. listing ?. dataset ?. buttonLabel || alm_localize ?. button_label ,
149
- loading : alm ?. listing ?. dataset ?. buttonLoadingLabel || null ,
150
- done : alm ?. listing ?. dataset ?. buttonDoneLabel || null ,
150
+ default : DOMPurify . sanitize ( alm ?. listing ?. dataset ?. buttonLabel ) || DOMPurify . sanitize ( alm_localize ?. button_label ) ,
151
+ loading : DOMPurify . sanitize ( alm ?. listing ?. dataset ?. buttonLoadingLabel ) || null ,
152
+ done : DOMPurify . sanitize ( alm ?. listing ?. dataset ?. buttonDoneLabel ) || null ,
151
153
} ;
152
154
alm . prev_button_labels = {
153
- default : alm ?. listing ?. dataset ?. prevButtonLabel ,
154
- loading : alm ?. listing ?. dataset ?. prevButtonLoadingLabel || null ,
155
- done : alm ?. listing ?. dataset ?. prevButtonDoneLabel || null ,
155
+ default : DOMPurify . sanitize ( alm ?. listing ?. dataset ?. prevButtonLabel ) ,
156
+ loading : DOMPurify . sanitize ( alm ?. listing ?. dataset ?. prevButtonLoadingLabel ) || null ,
157
+ done : DOMPurify . sanitize ( alm ?. listing ?. dataset ?. prevButtonDoneLabel ) || null ,
156
158
} ;
157
159
158
160
alm . urls = alm ?. listing ?. dataset ?. urls === 'false' ? false : true ;
@@ -433,7 +435,7 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
433
435
params = '' ;
434
436
}
435
437
436
- // HTTP request via axios .
438
+ // Make API request .
437
439
const data = await axios
438
440
. get ( url , { params } )
439
441
. then ( function ( response ) {
@@ -449,7 +451,7 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
449
451
return response . data ; // Standard ALM.
450
452
} )
451
453
. catch ( function ( error ) {
452
- alm . AjaxLoadMore . error ( error , 'adminajax' ) ;
454
+ alm . AjaxLoadMore . error ( error ) ;
453
455
} ) ;
454
456
455
457
switch ( type ) {
@@ -518,7 +520,7 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
518
520
} )
519
521
. catch ( function ( error ) {
520
522
// Error
521
- alm . AjaxLoadMore . error ( error , 'restapi' ) ;
523
+ alm . AjaxLoadMore . error ( error ) ;
522
524
} ) ;
523
525
} ;
524
526
@@ -988,7 +990,7 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
988
990
} )
989
991
. catch ( function ( error ) {
990
992
// Error
991
- alm . AjaxLoadMore . error ( error , 'getSinglePost' ) ;
993
+ alm . AjaxLoadMore . error ( error ) ;
992
994
alm . fetchingPreviousPost = false ;
993
995
} ) ;
994
996
@@ -1531,37 +1533,18 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
1531
1533
} ;
1532
1534
1533
1535
/**
1534
- * Handle error messages .
1536
+ * Handle API errors by reseting the loading state and button text .
1535
1537
*
1536
- * @param {string } error The error message.
1537
- * @param {string } location The location the error occured.
1538
+ * @param {string } error The error message.
1538
1539
* @since 2.6.0
1539
1540
*/
1540
- alm . AjaxLoadMore . error = function ( error , location = null ) {
1541
+ alm . AjaxLoadMore . error = function ( error ) {
1542
+ console . error ( 'Ajax Load More: There was an error with the Ajax request.' , error ) ; //eslint-disable-line no-console
1541
1543
alm . loading = false ;
1542
1544
if ( ! alm . addons . paging ) {
1543
1545
alm . button . classList . remove ( 'loading' ) ;
1544
1546
alm . AjaxLoadMore . resetBtnText ( ) ;
1545
1547
}
1546
- console . warn ( 'Error: ' , error ) ;
1547
-
1548
- if ( error . response ) {
1549
- // The request was made and the server responded with a status code that falls out of the range of 2xx.
1550
- console . error ( 'Error Msg: ' , error . message ) ;
1551
- } else if ( error . request ) {
1552
- // The request was made but no response was received.
1553
- console . error ( error . request ) ;
1554
- } else {
1555
- // Something happened in setting up the request that triggered an Error.
1556
- console . error ( 'Error Msg: ' , error . message ) ;
1557
- }
1558
-
1559
- if ( location ) {
1560
- console . error ( 'ALM Error started in ' + location ) ;
1561
- }
1562
- if ( error . config ) {
1563
- console . error ( 'ALM Error Debug: ' , error . config ) ;
1564
- }
1565
1548
} ;
1566
1549
1567
1550
/**
0 commit comments