diff --git a/includes/wizards/class-connections-wizard.php b/includes/wizards/class-connections-wizard.php index c5cb4293e9..ef5ed8f0ef 100644 --- a/includes/wizards/class-connections-wizard.php +++ b/includes/wizards/class-connections-wizard.php @@ -66,7 +66,6 @@ public function enqueue_scripts_and_styles() { 'can_connect_fivetran' => OAuth::is_proxy_configured( 'fivetran' ), 'can_use_webhooks' => defined( 'NEWSPACK_EXPERIMENTAL_WEBHOOKS' ) && NEWSPACK_EXPERIMENTAL_WEBHOOKS, 'can_use_everlit' => Everlit_Configuration_Manager::is_enabled(), - 'can_use_jetpack_sso' => class_exists( 'Jetpack' ) && defined( 'NEWSPACK_MANAGER_FILE' ), ] ); \wp_enqueue_script( 'newspack-connections-wizard' ); diff --git a/includes/wizards/newspack/class-newspack-settings.php b/includes/wizards/newspack/class-newspack-settings.php index 8044f89e5f..f647644f81 100644 --- a/includes/wizards/newspack/class-newspack-settings.php +++ b/includes/wizards/newspack/class-newspack-settings.php @@ -62,6 +62,11 @@ public function get_local_data() { 'googleOAuth' => OAuth::is_proxy_configured( 'google' ), ], ], + 'jetpack_sso' => [ + 'dependencies' => [ + 'jetpack_sso' => class_exists( 'Jetpack' ) && defined( 'NEWSPACK_MANAGER_FILE' ), + ], + ], 'recaptcha' => [], 'analytics' => [ 'editLink' => $google_site_kit_url, diff --git a/src/wizards/connections/views/main/index.js b/src/wizards/connections/views/main/index.js index 74c589a6c8..f17e15b1b6 100644 --- a/src/wizards/connections/views/main/index.js +++ b/src/wizards/connections/views/main/index.js @@ -14,7 +14,6 @@ import Plugins from './plugins'; import GoogleAuth from './google'; import Mailchimp from './mailchimp'; import FivetranConnection from './fivetran'; -import JetpackSSO from './jetpack-sso'; import Recaptcha from './recaptcha'; import Webhooks from './webhooks'; @@ -40,7 +39,6 @@ const Main = () => { /> ) } - { newspack_connections_data.can_use_webhooks && } diff --git a/src/wizards/newspack/views/settings/connections/index.tsx b/src/wizards/newspack/views/settings/connections/index.tsx index 0af37e0bf7..e212bc2ca8 100644 --- a/src/wizards/newspack/views/settings/connections/index.tsx +++ b/src/wizards/newspack/views/settings/connections/index.tsx @@ -15,6 +15,7 @@ import Plugins from './plugins'; import Webhooks from './webhooks'; import Analytics from './analytics'; import Recaptcha from './recaptcha'; +import JetpackSSO from './jetpack-sso'; import Mailchimp from './mailchimp'; import GoogleOAuth from './google-oauth'; import CustomEvents from './custom-events'; @@ -38,6 +39,13 @@ function Connections() { + { /* Jetpack SSO */ } + { connections.sections.jetpack_sso.dependencies?.jetpack_sso ? ( + + + + ) : null } + { /* reCAPTCHA */ } diff --git a/src/wizards/connections/views/main/jetpack-sso.js b/src/wizards/newspack/views/settings/connections/jetpack-sso.tsx similarity index 72% rename from src/wizards/connections/views/main/jetpack-sso.js rename to src/wizards/newspack/views/settings/connections/jetpack-sso.tsx index b6c5a3198d..78008b1764 100644 --- a/src/wizards/connections/views/main/jetpack-sso.js +++ b/src/wizards/newspack/views/settings/connections/jetpack-sso.tsx @@ -1,4 +1,3 @@ -/* globals newspack_connections_data */ /** * WordPress dependencies */ @@ -15,25 +14,31 @@ import { Button, Grid, Notice, - SectionHeader, SelectControl, -} from '../../../../components/src'; +} from '../../../../../components/src'; + +const isValidError = ( e: unknown ): e is WpRestApiError => { + return e instanceof Error && 'message' in e; +} const JetpackSSO = () => { - const [ error, setError ] = useState( null ); - const [ isLoading, setIsLoading ] = useState( false ); - const [ settings, setSettings ] = useState( {} ); - const [ settingsToUpdate, setSettingsToUpdate ] = useState( {} ); + const [ error, setError ] = useState( '' ); + const [ isLoading, setIsLoading ] = useState( false ); + const [ settings, setSettings ] = useState( {} ); + const [ settingsToUpdate, setSettingsToUpdate ] = useState( {} ); + + const getCapLabel = ( cap: JetpackSSOCaps ): string | undefined => + settings.available_caps ? settings.available_caps[ cap ] : undefined; useEffect( () => { const fetchSettings = async () => { setIsLoading( true ); try { - const fetchedSettings = await apiFetch( { path: '/newspack-manager/v1/jetpack-sso' } ); + const fetchedSettings = await apiFetch( { path: '/newspack-manager/v1/jetpack-sso' } ); setSettings( fetchedSettings ); setSettingsToUpdate( fetchedSettings ); - } catch ( e ) { - setError( e.message || __( 'Error fetching settings.', 'newspack-plugin' ) ); + } catch ( e: unknown ) { + setError( isValidError( e ) ? e.message : __( 'Error fetching settings.', 'newspack-plugin' ) ); } finally { setIsLoading( false ); } @@ -41,29 +46,25 @@ const JetpackSSO = () => { fetchSettings(); }, [] ); - const updateSettings = async data => { - setError( null ); + const updateSettings = async ( data: JetpackSSOSettings ) => { + setError( '' ); setIsLoading( true ); try { - const newSettings = await apiFetch( { + const newSettings = await apiFetch( { path: '/newspack-manager/v1/jetpack-sso', method: 'POST', data, } ); setSettings( newSettings ); setSettingsToUpdate( newSettings ); - } catch ( e ) { - setError( e?.message || __( 'Error updating settings.', 'newspack-plugin' ) ); + } catch ( e: unknown ) { + setError( isValidError( e ) ? e.message : __( 'Error updating settings.', 'newspack-plugin' ) ); } finally { setIsLoading( false ); } }; - if ( ! newspack_connections_data.can_use_jetpack_sso ) { - return null; - } return ( <> - { label={ __( 'Capability', 'newspack-plugin' ) } hideLabelFromVision value={ settingsToUpdate?.force_2fa_cap || '' } - onChange={ value => + onChange={ ( value: JetpackSSOCaps ) => setSettingsToUpdate( { ...settingsToUpdate, force_2fa_cap: value } ) } options={ - Object.keys( settings.available_caps || {} ).map( cap => ( { - label: settings.available_caps[ cap ], + Object.keys( settings.available_caps || {} ).map( ( cap: string ) => ( { + label: getCapLabel( cap as JetpackSSOCaps ), value: cap, } ) ) } diff --git a/src/wizards/newspack/views/settings/types.d.ts b/src/wizards/newspack/views/settings/types.d.ts index 3627ca1a53..fe0ecd9113 100644 --- a/src/wizards/newspack/views/settings/types.d.ts +++ b/src/wizards/newspack/views/settings/types.d.ts @@ -92,3 +92,15 @@ type RssData = { module_enabled_rss: boolean; 'module_enabled_media-partners': boolean; }; + +/** Jetpack SSO Caps */ +type JetpackSSOCaps = 'edit_posts' | 'publish_posts' | 'edit_others_posts' | 'manage_options'; + +/** Jetpack SSO Settings */ +type JetpackSSOSettings = Partial<{ + jetpack_sso_force_2fa: boolean; + force_2fa: boolean; + force_2fa_cap: JetpackSSOCaps; + obfuscate_account: boolean; + available_caps: { [key in JetpackSSOCaps]?: string }; +}>; diff --git a/src/wizards/types/index.d.ts b/src/wizards/types/index.d.ts index b62b046d1e..af7466d505 100644 --- a/src/wizards/types/index.d.ts +++ b/src/wizards/types/index.d.ts @@ -14,6 +14,18 @@ type WizardApiFetch< T = {} > = ( callbacks?: ApiFetchCallbacks< any > ) => Promise< T >; +/** + * WP REST API Error. + */ +type WpRestApiError = { + code: string; + message: string; + data: { + status: number; + params: Record< string, string >; + }; +}; + /** * Attachment object interface. */