From 85b6b9e5408db3af72790fda42273b8d5e2aebfd Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Mon, 6 Jan 2025 14:50:15 +1300 Subject: [PATCH 1/5] fix toggles --- .../_inc/client/traffic/site-stats.jsx | 51 +++++++------------ 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx b/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx index a4eb2d52298a2..869997c017ff2 100644 --- a/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx +++ b/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx @@ -43,8 +43,23 @@ class SiteStatsComponent extends React.Component { }; if ( roles ) { - this.addCustomCountRolesState( countRoles ); - this.addCustomRolesState( roles ); + roles.forEach( role => { + if ( + ! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes( role ) + ) { + this.state[ `roles_${ role }` ] = includes( roles, role, false ); + } + } ); + } + + if ( countRoles ) { + countRoles.forEach( role => { + if ( + ! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes( role ) + ) { + this.state[ `count_roles_${ role }` ] = includes( countRoles, role, false ); + } + } ); } } @@ -107,38 +122,6 @@ class SiteStatsComponent extends React.Component { return () => this.updateOptions( role, setting ); }; - /** - * Allows for custom roles 'count logged in page views' stats settings to be added to the current state. - * - * @param {Array} countRoles - All roles (including custom) that have 'count logged in page views' enabled. - */ - addCustomCountRolesState( countRoles ) { - countRoles.forEach( role => { - if ( - ! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes( - countRoles - ) - ) { - this.setState( { [ `count_roles_${ role }` ]: includes( countRoles, role, false ) } ); - } - } ); - } - - /** - * Allows for custom roles 'allow stats reports' stats settings to be added to the current state. - * - * @param {Array} roles - All roles (including custom) that have 'allow stats reports' enabled. - */ - addCustomRolesState( roles ) { - roles.forEach( role => { - if ( - ! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes( role ) - ) { - this.setState( { [ `roles_${ role }` ]: includes( roles, role, false ) } ); - } - } ); - } - handleStatsOptionToggle( option_slug ) { return () => this.props.updateFormStateModuleOption( 'stats', option_slug ); } From 78ac5536493c15c87ad37c0e5c9b6758bdb8e496 Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Mon, 6 Jan 2025 14:51:09 +1300 Subject: [PATCH 2/5] changelog --- .../plugins/jetpack/changelog/fix-stats-setting-role-toggles | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/plugins/jetpack/changelog/fix-stats-setting-role-toggles diff --git a/projects/plugins/jetpack/changelog/fix-stats-setting-role-toggles b/projects/plugins/jetpack/changelog/fix-stats-setting-role-toggles new file mode 100644 index 0000000000000..eee7a6f4e22e0 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-stats-setting-role-toggles @@ -0,0 +1,4 @@ +Significance: patch +Type: bugfix + +Fix custom roles settings are not sticking for Jetpack Stats From ad5841c7e42d32971375bb94c091fd3cd9147a59 Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Mon, 6 Jan 2025 15:07:28 +1300 Subject: [PATCH 3/5] includes is not necessary --- projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx b/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx index 869997c017ff2..fd860ba52dc6d 100644 --- a/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx +++ b/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx @@ -47,7 +47,7 @@ class SiteStatsComponent extends React.Component { if ( ! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes( role ) ) { - this.state[ `roles_${ role }` ] = includes( roles, role, false ); + this.state[ `roles_${ role }` ] = true; } } ); } @@ -57,7 +57,7 @@ class SiteStatsComponent extends React.Component { if ( ! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes( role ) ) { - this.state[ `count_roles_${ role }` ] = includes( countRoles, role, false ); + this.state[ `count_roles_${ role }` ] = true; } } ); } From 7ebf1eac8eaa07124e04e51f53709d99be83a679 Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Mon, 6 Jan 2025 15:08:48 +1300 Subject: [PATCH 4/5] make the condition check more robust --- projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx b/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx index fd860ba52dc6d..7225d3f914d18 100644 --- a/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx +++ b/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx @@ -42,7 +42,7 @@ class SiteStatsComponent extends React.Component { wpcom_reader_views_enabled: props.getOptionValue( 'wpcom_reader_views_enabled' ), }; - if ( roles ) { + if ( roles?.length > 0 ) { roles.forEach( role => { if ( ! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes( role ) @@ -52,7 +52,7 @@ class SiteStatsComponent extends React.Component { } ); } - if ( countRoles ) { + if ( countRoles?.length > 0 ) { countRoles.forEach( role => { if ( ! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes( role ) From 22f546eecc359e1dcc2c752698becd27b02f2eab Mon Sep 17 00:00:00 2001 From: Jasper Kang Date: Tue, 7 Jan 2025 09:38:33 +1300 Subject: [PATCH 5/5] use a const array --- .../plugins/jetpack/_inc/client/traffic/site-stats.jsx | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx b/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx index 7225d3f914d18..dde495ddf45fb 100644 --- a/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx +++ b/projects/plugins/jetpack/_inc/client/traffic/site-stats.jsx @@ -42,11 +42,11 @@ class SiteStatsComponent extends React.Component { wpcom_reader_views_enabled: props.getOptionValue( 'wpcom_reader_views_enabled' ), }; + const defaultRoles = [ 'administrator', 'editor', 'author', 'contributor', 'subscriber' ]; + if ( roles?.length > 0 ) { roles.forEach( role => { - if ( - ! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes( role ) - ) { + if ( ! defaultRoles.includes( role ) ) { this.state[ `roles_${ role }` ] = true; } } ); @@ -54,9 +54,7 @@ class SiteStatsComponent extends React.Component { if ( countRoles?.length > 0 ) { countRoles.forEach( role => { - if ( - ! [ 'administrator', 'editor', 'author', 'subscriber', 'contributor' ].includes( role ) - ) { + if ( ! defaultRoles.includes( role ) ) { this.state[ `count_roles_${ role }` ] = true; } } );