@@ -84,7 +84,7 @@ document.addEventListener('DOMContentLoaded', () => {
8484 *
8585 * @param e
8686 */
87- toggleOption : function ( e ) {
87+ toggleOption : async function ( e ) {
8888 /**
8989 * Make sure event target is a toggle.
9090 */
@@ -109,11 +109,13 @@ document.addEventListener('DOMContentLoaded', () => {
109109 // Toggle: off
110110 button . classList . replace ( 'bg-indigo-600' , 'bg-gray-200' ) ;
111111 toggle . classList . replace ( 'translate-x-5' , 'translate-x-0' ) ;
112+ button . dataset . status = 'off' ;
112113 toggleStatus = '' ;
113114 } else {
114115 // Toggle: on
115116 button . classList . replace ( 'bg-gray-200' , 'bg-indigo-600' ) ;
116117 toggle . classList . replace ( 'translate-x-0' , 'translate-x-5' ) ;
118+ button . dataset . status = 'on' ;
117119 toggleStatus = 'on' ;
118120 }
119121
@@ -126,7 +128,13 @@ document.addEventListener('DOMContentLoaded', () => {
126128 form . append ( 'is_list' , button . dataset . list ) ;
127129 form . append ( '_nonce' , plausible . nonce ) ;
128130
129- plausible . ajax ( form ) ;
131+ let data = await plausible . ajax ( form ) ;
132+
133+ if ( data . capabilities === undefined ) {
134+ return ;
135+ }
136+
137+ plausible . maybeDisableOptions ( data . capabilities ) ;
130138 } ,
131139
132140 /**
@@ -160,6 +168,33 @@ document.addEventListener('DOMContentLoaded', () => {
160168 plausible . ajax ( form , button ) ;
161169 } ,
162170
171+ /**
172+ * Disable options based on the capabilities retrieved from the API.
173+ *
174+ * @param capabilities
175+ */
176+ maybeDisableOptions : function ( capabilities ) {
177+ let options = document . querySelectorAll ( 'button[data-caps]' ) ;
178+
179+ options . forEach ( function ( option ) {
180+ let caps = option . dataset . caps . split ( ',' ) ;
181+ let disable = false ;
182+
183+ caps . forEach ( function ( cap ) {
184+ if ( capabilities [ cap ] === false ) {
185+ disable = true ;
186+ }
187+ } ) ;
188+
189+ if ( disable === true ) {
190+ // Trigger a click to make sure the option is disabled.
191+ if ( option . dataset . status === 'on' ) {
192+ option . dispatchEvent ( new Event ( 'click' , { bubbles : true } ) ) ;
193+ }
194+ }
195+ } ) ;
196+ } ,
197+
163198 /**
164199 * Currently only validates the domain_name input, but can be used in the future for other custom input validations.
165200 *
@@ -375,7 +410,8 @@ document.addEventListener('DOMContentLoaded', () => {
375410 }
376411 }
377412
378- if ( response . status === 200 ) {
413+ // We still want the data, if it's a Payment Required error.
414+ if ( response . status === 200 || response . status === 402 ) {
379415 return response . json ( ) ;
380416 }
381417
@@ -478,6 +514,7 @@ document.addEventListener('DOMContentLoaded', () => {
478514 } , 2000 ) ;
479515 }
480516 } ,
517+
481518 /**
482519 * Renders a HTML box containing additional information about the enabled option.
483520 *
0 commit comments