11// ALM Modules
22import axios from 'axios' ;
3- import '../scss/ajax-load-more.scss ';
3+ import DOMPurify from 'dompurify ';
44import { cacheCreateParams , getCache } from './addons/cache' ;
55import { ctaCreateParams } from './addons/call-to-actions' ;
66import { commentsCreateParams } from './addons/comments' ;
@@ -33,15 +33,16 @@ import * as resultsText from './modules/resultsText';
3333import setLocalizedVars from './modules/setLocalizedVars' ;
3434import { tableOfContents } from './modules/tableofcontents' ;
3535
36+ import '../scss/ajax-load-more.scss' ;
37+
3638// External packages.
3739const qs = require ( 'qs' ) ;
3840const imagesLoaded = require ( 'imagesloaded' ) ;
3941
4042// Axios Config.
4143axios . defaults . headers . common [ 'X-Requested-With' ] = 'XMLHttpRequest' ;
42-
43- // Axios Interceptor for nested data objects
4444axios . interceptors . request . use ( ( config ) => {
45+ // Axios Interceptor for nested data objects
4546 config . paramsSerializer = ( params ) => {
4647 // Qs is already included in the Axios package
4748 return qs . stringify ( params , {
@@ -144,15 +145,16 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
144145 alm . trigger = alm . btnWrap [ alm . btnWrap . length - 1 ] ;
145146 alm . button = alm ?. trigger ?. querySelector ( 'button.alm-load-more-btn' ) || null ;
146147
148+ // Button Labels.
147149 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 ,
151153 } ;
152154 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 ,
156158 } ;
157159
158160 alm . urls = alm ?. listing ?. dataset ?. urls === 'false' ? false : true ;
@@ -433,7 +435,7 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
433435 params = '' ;
434436 }
435437
436- // HTTP request via axios .
438+ // Make API request .
437439 const data = await axios
438440 . get ( url , { params } )
439441 . then ( function ( response ) {
@@ -449,7 +451,7 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
449451 return response . data ; // Standard ALM.
450452 } )
451453 . catch ( function ( error ) {
452- alm . AjaxLoadMore . error ( error , 'adminajax' ) ;
454+ alm . AjaxLoadMore . error ( error ) ;
453455 } ) ;
454456
455457 switch ( type ) {
@@ -518,7 +520,7 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
518520 } )
519521 . catch ( function ( error ) {
520522 // Error
521- alm . AjaxLoadMore . error ( error , 'restapi' ) ;
523+ alm . AjaxLoadMore . error ( error ) ;
522524 } ) ;
523525 } ;
524526
@@ -988,7 +990,7 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
988990 } )
989991 . catch ( function ( error ) {
990992 // Error
991- alm . AjaxLoadMore . error ( error , 'getSinglePost' ) ;
993+ alm . AjaxLoadMore . error ( error ) ;
992994 alm . fetchingPreviousPost = false ;
993995 } ) ;
994996
@@ -1531,37 +1533,18 @@ const isBlockEditor = document.body.classList.contains('wp-admin');
15311533 } ;
15321534
15331535 /**
1534- * Handle error messages .
1536+ * Handle API errors by reseting the loading state and button text .
15351537 *
1536- * @param {string } error The error message.
1537- * @param {string } location The location the error occured.
1538+ * @param {string } error The error message.
15381539 * @since 2.6.0
15391540 */
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
15411543 alm . loading = false ;
15421544 if ( ! alm . addons . paging ) {
15431545 alm . button . classList . remove ( 'loading' ) ;
15441546 alm . AjaxLoadMore . resetBtnText ( ) ;
15451547 }
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- }
15651548 } ;
15661549
15671550 /**
0 commit comments