diff --git a/projects/js-packages/publicize-components/changelog/update-social-schema-front-end-changes b/projects/js-packages/publicize-components/changelog/update-social-schema-front-end-changes new file mode 100644 index 0000000000000..e3a61d622179e --- /dev/null +++ b/projects/js-packages/publicize-components/changelog/update-social-schema-front-end-changes @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Updated the shape of connections object following the schema update diff --git a/projects/js-packages/publicize-components/src/components/connection-management/connection-info.tsx b/projects/js-packages/publicize-components/src/components/connection-management/connection-info.tsx index ebd65c1ccea8a..8b4f1bf73b6f5 100644 --- a/projects/js-packages/publicize-components/src/components/connection-management/connection-info.tsx +++ b/projects/js-packages/publicize-components/src/components/connection-management/connection-info.tsx @@ -27,7 +27,7 @@ export function ConnectionInfo( { connection, service }: ConnectionInfoProps ) {
diff --git a/projects/js-packages/publicize-components/src/components/connection-management/connection-name.tsx b/projects/js-packages/publicize-components/src/components/connection-management/connection-name.tsx index 7bdd740496e47..d28420646ea29 100644 --- a/projects/js-packages/publicize-components/src/components/connection-management/connection-name.tsx +++ b/projects/js-packages/publicize-components/src/components/connection-management/connection-name.tsx @@ -27,12 +27,10 @@ export function ConnectionName( { connection }: ConnectionNameProps ) { return (
{ ! connection.profile_link ? ( - - { connection.display_name || connection.external_name } - + { connection.display_name } ) : ( - { connection.display_name || connection.external_display || connection.external_name } + { connection.display_name } ) } { isUpdating ? ( diff --git a/projects/js-packages/publicize-components/src/components/connection-management/reconnect.tsx b/projects/js-packages/publicize-components/src/components/connection-management/reconnect.tsx index 22bf30e2e64d2..889798f31cd9b 100644 --- a/projects/js-packages/publicize-components/src/components/connection-management/reconnect.tsx +++ b/projects/js-packages/publicize-components/src/components/connection-management/reconnect.tsx @@ -63,7 +63,7 @@ export function Reconnect( { connection, service, variant = 'link' }: ReconnectP const formData = new FormData(); if ( service.ID === 'mastodon' ) { - formData.set( 'instance', connection.external_display ); + formData.set( 'instance', connection.external_handle ); } if ( service.ID === 'bluesky' ) { diff --git a/projects/js-packages/publicize-components/src/components/form/broken-connections-notice.tsx b/projects/js-packages/publicize-components/src/components/form/broken-connections-notice.tsx index ffb87494effae..fc142cfe131c3 100644 --- a/projects/js-packages/publicize-components/src/components/form/broken-connections-notice.tsx +++ b/projects/js-packages/publicize-components/src/components/form/broken-connections-notice.tsx @@ -1,30 +1,18 @@ import { Button } from '@automattic/jetpack-components'; import { ExternalLink } from '@wordpress/components'; -import { useDispatch } from '@wordpress/data'; +import { useDispatch, useSelect } from '@wordpress/data'; import { createInterpolateElement, Fragment } from '@wordpress/element'; import { __, _x } from '@wordpress/i18n'; import usePublicizeConfig from '../../hooks/use-publicize-config'; -import useSocialMediaConnections from '../../hooks/use-social-media-connections'; import { store } from '../../social-store'; import { Connection } from '../../social-store/types'; -import { checkConnectionCode } from '../../utils/connections'; import { getSocialScriptData } from '../../utils/script-data'; import Notice from '../notice'; import { useServiceLabel } from '../services/use-service-label'; import styles from './styles.module.scss'; export const BrokenConnectionsNotice: React.FC = () => { - const { connections } = useSocialMediaConnections(); - - const brokenConnections = connections.filter( connection => { - return ( - connection.status === 'broken' || - // This is a legacy check for connections that are not healthy. - // TODO remove this check when we are sure that all connections have - // the status property (same schema for connections endpoints), e.g. on Simple/Atomic sites - checkConnectionCode( connection, 'broken' ) - ); - } ); + const brokenConnections = useSelect( select => select( store ).getBrokenConnections(), [] ); const { connectionsPageUrl } = usePublicizeConfig(); @@ -87,11 +75,9 @@ export const BrokenConnectionsNotice: React.FC = () => { { // Since Intl.ListFormat is not allowed in Jetpack yet, // we join the connections with a comma and space - connectionsList.map( ( { display_name, external_display, id }, i ) => ( - - - { display_name || external_display } - + connectionsList.map( ( { display_name, connection_id }, i ) => ( + + { display_name } { i < connectionsList.length - 1 && _x( ',', diff --git a/projects/js-packages/publicize-components/src/components/form/connections-list.tsx b/projects/js-packages/publicize-components/src/components/form/connections-list.tsx index c2c5e5c51b4dc..e15950ad91771 100644 --- a/projects/js-packages/publicize-components/src/components/form/connections-list.tsx +++ b/projects/js-packages/publicize-components/src/components/form/connections-list.tsx @@ -35,18 +35,17 @@ export const ConnectionsList: React.FC = () => {
    { connections.map( conn => { - const { display_name, id, service_name, profile_picture, connection_id } = conn; - const currentId = connection_id ? connection_id : id; + const { display_name, service_name, profile_picture, connection_id } = conn; return ( ); diff --git a/projects/js-packages/publicize-components/src/components/form/unsupported-connections-notice.tsx b/projects/js-packages/publicize-components/src/components/form/unsupported-connections-notice.tsx index fd64817250ccc..778db75f9b53f 100644 --- a/projects/js-packages/publicize-components/src/components/form/unsupported-connections-notice.tsx +++ b/projects/js-packages/publicize-components/src/components/form/unsupported-connections-notice.tsx @@ -3,16 +3,20 @@ import { createInterpolateElement } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { usePublicizeConfig } from '../../..'; import useSocialMediaConnections from '../../hooks/use-social-media-connections'; -import { checkConnectionCode } from '../../utils/connections'; import Notice from '../notice'; +import { useService } from '../services/use-service'; export const UnsupportedConnectionsNotice: React.FC = () => { const { connections } = useSocialMediaConnections(); const { connectionsPageUrl } = usePublicizeConfig(); - const unsupportedConnections = connections.filter( connection => - checkConnectionCode( connection, 'unsupported' ) + const getServices = useService(); + + const unsupportedConnections = connections.filter( + connection => + // If getServices returns falsy, it means the service is unsupported. + ! getServices( connection.service_name ) ); return ( diff --git a/projects/js-packages/publicize-components/src/components/form/use-connection-state.ts b/projects/js-packages/publicize-components/src/components/form/use-connection-state.ts index e994363e19d4a..7f3a5bc4daaa6 100644 --- a/projects/js-packages/publicize-components/src/components/form/use-connection-state.ts +++ b/projects/js-packages/publicize-components/src/components/form/use-connection-state.ts @@ -31,17 +31,16 @@ export const useConnectionState = () => { */ const isInGoodShape = useCallback( ( connection: Connection ) => { - const { id, is_healthy, connection_id, status } = connection; - const currentId = connection_id ? connection_id : id; + const { connection_id: id, status } = connection; // 1. Be healthy - const isHealthy = false !== is_healthy && status !== 'broken'; + const isHealthy = status !== 'broken'; // 2. Have no validation errors - const hasValidationErrors = validationErrors[ currentId ] !== undefined && ! isConvertible; + const hasValidationErrors = validationErrors[ id ] !== undefined && ! isConvertible; // 3. Not have a NO_MEDIA_ERROR when media is required - const hasNoMediaError = validationErrors[ currentId ] === NO_MEDIA_ERROR; + const hasNoMediaError = validationErrors[ id ] === NO_MEDIA_ERROR; return isHealthy && ! hasValidationErrors && ! hasNoMediaError; }, diff --git a/projects/js-packages/publicize-components/src/components/manage-connections-modal/confirmation-form/index.tsx b/projects/js-packages/publicize-components/src/components/manage-connections-modal/confirmation-form/index.tsx index f99e92f74e942..a07f892d38a56 100644 --- a/projects/js-packages/publicize-components/src/components/manage-connections-modal/confirmation-form/index.tsx +++ b/projects/js-packages/publicize-components/src/components/manage-connections-modal/confirmation-form/index.tsx @@ -162,7 +162,7 @@ export function ConfirmationForm( { keyringResult, onComplete, isAdmin }: Confir display_name: accountInfo?.label, profile_picture: accountInfo?.profile_picture, service_name: service.ID, - external_id: external_user_ID, + external_id: external_user_ID.toString(), } ); onComplete(); diff --git a/projects/js-packages/publicize-components/src/components/services/connect-form.tsx b/projects/js-packages/publicize-components/src/components/services/connect-form.tsx index a700da6a8514a..a530834148449 100644 --- a/projects/js-packages/publicize-components/src/components/services/connect-form.tsx +++ b/projects/js-packages/publicize-components/src/components/services/connect-form.tsx @@ -24,7 +24,7 @@ type ConnectFormProps = { * * @param {ConnectFormProps} props - Component props * - * @return {import('react').ReactNode} Connect form component + * @return Connect form component */ export function ConnectForm( { service, diff --git a/projects/js-packages/publicize-components/src/components/services/custom-inputs.tsx b/projects/js-packages/publicize-components/src/components/services/custom-inputs.tsx index 90f61a1350056..e2f114f412ba4 100644 --- a/projects/js-packages/publicize-components/src/components/services/custom-inputs.tsx +++ b/projects/js-packages/publicize-components/src/components/services/custom-inputs.tsx @@ -73,7 +73,7 @@ export function CustomInputs( { service }: CustomInputsProps ) { name="handle" defaultValue={ reconnectingAccount?.service_name === 'bluesky' - ? reconnectingAccount?.external_name + ? reconnectingAccount?.external_handle : undefined } autoComplete="off" diff --git a/projects/js-packages/publicize-components/src/components/social-post-modal/post-preview.tsx b/projects/js-packages/publicize-components/src/components/social-post-modal/post-preview.tsx index 8876dcec74516..8484cf5886719 100644 --- a/projects/js-packages/publicize-components/src/components/social-post-modal/post-preview.tsx +++ b/projects/js-packages/publicize-components/src/components/social-post-modal/post-preview.tsx @@ -33,9 +33,9 @@ export type PostPreviewProps = { export function PostPreview( { connection }: PostPreviewProps ) { const user = useMemo( () => ( { - displayName: connection.display_name || connection.external_display, + displayName: connection.display_name, profileImage: connection.profile_picture, - externalName: connection.external_name, + externalName: connection.external_handle, } ), [ connection ] ); diff --git a/projects/js-packages/publicize-components/src/components/social-post-modal/preview-section.tsx b/projects/js-packages/publicize-components/src/components/social-post-modal/preview-section.tsx index 18e656f04082b..cbb0bef242bd5 100644 --- a/projects/js-packages/publicize-components/src/components/social-post-modal/preview-section.tsx +++ b/projects/js-packages/publicize-components/src/components/social-post-modal/preview-section.tsx @@ -33,7 +33,7 @@ export function PreviewSection() { // to avoid errors for old connections like Twitter .filter( ( { service_name } ) => getService( service_name ) ) .map( connection => { - const title = connection.display_name || connection.external_display; + const title = connection.display_name; const name = `${ connection.service_name }-${ connection.connection_id }`; const icon = ( - conn.connection_id - ? conn.connection_id === freshConnection.connection_id - : conn.id === freshConnection.id + const prevConnection = prevConnections.find( + conn => conn.connection_id === freshConnection.connection_id ); const connection = { ...defaults, ...prevConnection, ...freshConnection, - shared: prevConnection?.shared, - is_healthy: freshConnection.test_success, }; connections.push( connection ); } @@ -378,7 +372,7 @@ export function createConnection( data, optimisticData = {} ) { sprintf( /* translators: %s is the name of the social media platform e.g. "Facebook" */ __( '%s account connected successfully.', 'jetpack-publicize-components' ), - connection.label + connection.service_label ), { type: 'snackbar', diff --git a/projects/js-packages/publicize-components/src/social-store/actions/test/connection-data.js b/projects/js-packages/publicize-components/src/social-store/actions/test/connection-data.js index 86b84bc27c9b3..2e6efa6235e99 100644 --- a/projects/js-packages/publicize-components/src/social-store/actions/test/connection-data.js +++ b/projects/js-packages/publicize-components/src/social-store/actions/test/connection-data.js @@ -119,22 +119,14 @@ describe( 'Social store actions: connectionData', () => { const freshConnections = connections.map( connection => ( { ...connection, - test_success: false, + status: 'broken', } ) ); registry.dispatch( socialStore ).mergeConnections( freshConnections ); const connectionsAfterMerge = registry.select( socialStore ).getConnections(); - expect( connectionsAfterMerge ).toEqual( - freshConnections.map( connection => ( { - ...connection, - // These 3 are added while merging - done: false, - toggleable: true, - is_healthy: false, - } ) ) - ); + expect( connectionsAfterMerge ).toEqual( freshConnections ); } ); } ); @@ -156,10 +148,7 @@ describe( 'Social store actions: connectionData', () => { if ( path.startsWith( refreshConnections ) ) { return connections.map( connection => ( { ...connection, - can_refresh: false, - refresh_url: '', - test_message: 'Some message', - test_success: true, + status: 'broken', } ) ); } @@ -184,14 +173,7 @@ describe( 'Social store actions: connectionData', () => { expect( connectionsAfterRefresh ).toEqual( connections.map( connection => ( { ...connection, - can_refresh: false, - refresh_url: '', - test_message: 'Some message', - test_success: true, - // These 3 are added while merging - done: false, - toggleable: true, - is_healthy: true, + status: 'broken', } ) ) ); diff --git a/projects/js-packages/publicize-components/src/social-store/reducer/connection-data.ts b/projects/js-packages/publicize-components/src/social-store/reducer/connection-data.ts index 29fd84f077177..237d93331c0ea 100644 --- a/projects/js-packages/publicize-components/src/social-store/reducer/connection-data.ts +++ b/projects/js-packages/publicize-components/src/social-store/reducer/connection-data.ts @@ -133,11 +133,7 @@ const connectionData = ( state: ConnectionData = { connections: [] }, action ) = return { ...state, connections: state.connections.map( connection => { - // If the connection has a connection_id, then give it priority. - // Otherwise, use the id. - const isTargetConnection = connection.connection_id - ? connection.connection_id === action.connectionId - : connection.id === action.connectionId; + const isTargetConnection = connection.connection_id === action.connectionId; if ( isTargetConnection ) { return { diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/connection-data.js b/projects/js-packages/publicize-components/src/social-store/selectors/connection-data.ts similarity index 82% rename from projects/js-packages/publicize-components/src/social-store/selectors/connection-data.js rename to projects/js-packages/publicize-components/src/social-store/selectors/connection-data.ts index 25b675d301c4d..2eeeb115ed404 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/connection-data.js +++ b/projects/js-packages/publicize-components/src/social-store/selectors/connection-data.ts @@ -1,5 +1,5 @@ -import { checkConnectionCode } from '../../utils/connections'; import { REQUEST_TYPE_DEFAULT } from '../actions/constants'; +import { SocialStoreState } from '../types'; /** * Returns the connections list from the store. @@ -8,7 +8,7 @@ import { REQUEST_TYPE_DEFAULT } from '../actions/constants'; * * @return {Array} The connections list */ -export function getConnections( state ) { +export function getConnections( state: SocialStoreState ) { return state.connectionData?.connections ?? []; } @@ -32,13 +32,7 @@ export function getConnectionById( state, connectionId ) { */ export function getBrokenConnections( state ) { return getConnections( state ).filter( connection => { - return ( - connection.status === 'broken' || - // This is a legacy check for connections that are not healthy. - // TODO remove this check when we are sure that all connections have - // the status property (same schema for connections endpoints), e.g. on Simple/Atomic sites - checkConnectionCode( connection, 'broken' ) - ); + return connection.status === 'broken'; } ); } @@ -48,7 +42,7 @@ export function getBrokenConnections( state ) { * @param {import("../types").SocialStoreState} state - State object. * @param {string} serviceName - The service name. * - * @return {Array} The connections. + * @return {Array} The connections. */ export function getConnectionsByService( state, serviceName ) { return getConnections( state ).filter( ( { service_name } ) => service_name === serviceName ); @@ -72,7 +66,7 @@ export function hasConnections( state ) { export function getFailedConnections( state ) { const connections = getConnections( state ); - return connections.filter( connection => false === connection.test_success ); + return connections.filter( connection => 'broken' === connection.status ); } /** @@ -85,7 +79,7 @@ export function getFailedConnections( state ) { export function getMustReauthConnections( state ) { const connections = getConnections( state ); return connections - .filter( connection => 'must_reauth' === connection.test_success ) + .filter( connection => 'broken' === connection.status ) .map( connection => connection.service_name ); } @@ -132,22 +126,11 @@ export function getConnectionProfileDetails( state, service, { forceDefaults = f ); if ( connection ) { - const { - display_name, - profile_display_name, - profile_picture, - external_display, - external_name, - } = connection; - - displayName = 'twitter' === service ? profile_display_name : display_name || external_display; - username = 'twitter' === service ? display_name : connection.username; - profileImage = profile_picture; + const { display_name, profile_picture, external_handle } = connection; - // Connections schema is a mess - if ( 'bluesky' === service ) { - username = external_name; - } + displayName = display_name; + username = external_handle; + profileImage = profile_picture; } } @@ -199,14 +182,14 @@ export function getAbortControllers( state, requestType = REQUEST_TYPE_DEFAULT ) /** * Whether a mastodon account is already connected. * - * @param {import("../types").SocialStoreState} state - State object. - * @param {string} username - The mastodon username. + * @param {import("../types").SocialStoreState} state - State object. + * @param {string} handle - The mastodon handle. * * @return {boolean} Whether the mastodon account is already connected. */ -export function isMastodonAccountAlreadyConnected( state, username ) { +export function isMastodonAccountAlreadyConnected( state, handle ) { return getConnectionsByService( state, 'mastodon' ).some( connection => { - return connection.external_display === username; + return connection.external_handle === handle; } ); } @@ -220,7 +203,7 @@ export function isMastodonAccountAlreadyConnected( state, username ) { */ export function isBlueskyAccountAlreadyConnected( state, handle ) { return getConnectionsByService( state, 'bluesky' ).some( connection => { - return connection.external_name === handle; + return connection.external_handle === handle; } ); } diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/test/connection-data.test.js b/projects/js-packages/publicize-components/src/social-store/selectors/test/connection-data.test.js index b73f4d62f1c49..7cfce26a5feaf 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/test/connection-data.test.js +++ b/projects/js-packages/publicize-components/src/social-store/selectors/test/connection-data.test.js @@ -12,34 +12,31 @@ const state = { connectionData: { connections: [ { - id: '123456789', service_name: 'facebook', display_name: 'Some name', profile_picture: 'https://wordpress.com/some-url-of-a-picture', - username: 'username', + external_handle: 'external_handle', enabled: false, connection_id: '987654321', - test_success: true, + status: 'ok', }, { - id: '234567891', service_name: 'tumblr', display_name: 'Some name', profile_picture: 'https://wordpress.com/some-url-of-another-picture', - username: 'username', + external_handle: 'external_handle', enabled: true, connection_id: '198765432', - test_success: false, + status: 'broken', }, { - id: '345678912', service_name: 'mastodon', display_name: 'somename', profile_picture: 'https://wordpress.com/some-url-of-one-more-picture', - username: '@somename@mastodon.social', + external_handle: '@somename@mastodon.social', enabled: false, connection_id: '219876543', - test_success: 'must_reauth', + status: 'ok', }, ], }, @@ -93,7 +90,7 @@ describe( 'Social store selectors: connectionData', () => { it( 'should return must reauth connections', () => { const mustReauthConnections = getMustReauthConnections( state ); expect( mustReauthConnections ).toEqual( [ - state.connectionData.connections[ 2 ].service_name, + state.connectionData.connections[ 1 ].service_name, ] ); } ); } ); @@ -140,7 +137,7 @@ describe( 'Social store selectors: connectionData', () => { expect( getConnectionProfileDetails( state, 'facebook' ) ).toEqual( { displayName: connection.display_name, profileImage: connection.profile_picture, - username: connection.username, + username: connection.external_handle, } ); } ); diff --git a/projects/js-packages/publicize-components/src/social-store/types.ts b/projects/js-packages/publicize-components/src/social-store/types.ts index fb5701967342a..e74a354fa53e1 100644 --- a/projects/js-packages/publicize-components/src/social-store/types.ts +++ b/projects/js-packages/publicize-components/src/social-store/types.ts @@ -1,25 +1,64 @@ export type ConnectionStatus = 'ok' | 'broken'; export type Connection = { - id: string; - service_name: string; - label?: string; + connection_id: string; display_name: string; - external_display?: string; - external_id: string; - external_name?: string; - username: string; enabled: boolean; - done: boolean; - toggleable: boolean; - connection_id: string; - is_healthy?: boolean; - error_code?: string; - can_disconnect: boolean; - profile_picture: string; + external_handle: string; + external_id: string; profile_link: string; + profile_picture: string; + service_label: string; + service_name: string; shared: boolean; status: ConnectionStatus; + user_id: number; + + /* DEPRECATED FIELDS */ + /** + * @deprecated + */ + can_disconnect?: boolean; + /** + * @deprecated + */ + done?: boolean; + /** + * @deprecated Use `status` instead. + */ + error_code?: string; + /** + * @deprecated Use `display_name` instead. + */ + external_display?: string; + /** + * @deprecated Use `external_handle` instead. + */ + external_name?: string; + /** + * @deprecated Use `connection_id` instead. + */ + id?: string; + /** + * @deprecated Use `status` instead. + */ + is_healthy?: boolean; + /** + * @deprecated Use `service_label` instead. + */ + label?: string; + /** + * @deprecated Use `status` instead. + */ + test_success?: boolean; + /** + * @deprecated + */ + toggleable?: boolean; + /** + * @deprecated Use `external_handle` instead. + */ + username?: string; }; export type ConnectionData = { diff --git a/projects/js-packages/publicize-components/src/utils/connections.ts b/projects/js-packages/publicize-components/src/utils/connections.ts deleted file mode 100644 index 5ca5e3daf8266..0000000000000 --- a/projects/js-packages/publicize-components/src/utils/connections.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { Connection } from '../social-store/types'; - -export const checkConnectionCode = ( connection: Connection, code: string ) => { - return false === connection.is_healthy && code === ( connection.error_code ?? 'broken' ); -}; diff --git a/projects/js-packages/publicize-components/src/utils/test-utils.js b/projects/js-packages/publicize-components/src/utils/test-utils.js index fa2f326be7bc6..b502817d8f05c 100644 --- a/projects/js-packages/publicize-components/src/utils/test-utils.js +++ b/projects/js-packages/publicize-components/src/utils/test-utils.js @@ -33,34 +33,28 @@ export const testPost = { export const connections = [ { - id: '123456789', service_name: 'facebook', display_name: 'Some name', profile_picture: 'https://wordpress.com/some-url-of-a-picture', - username: 'username', + external_handle: 'username', enabled: false, connection_id: '987654321', - test_success: true, }, { - id: '234567891', service_name: 'tumblr', display_name: 'Some name', profile_picture: 'https://wordpress.com/some-url-of-another-picture', - username: 'username', + external_handle: 'username', enabled: false, connection_id: '198765432', - test_success: false, }, { - id: '345678912', service_name: 'mastodon', display_name: 'somename', profile_picture: 'https://wordpress.com/some-url-of-one-more-picture', - username: '@somename@mastodon.social', + external_handle: '@somename@mastodon.social', enabled: false, connection_id: '219876543', - test_success: 'must_reauth', }, ]; diff --git a/projects/packages/publicize/changelog/update-unify-social-connections-schema b/projects/packages/publicize/changelog/update-unify-social-connections-schema new file mode 100644 index 0000000000000..feff6a2e9064f --- /dev/null +++ b/projects/packages/publicize/changelog/update-unify-social-connections-schema @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Social: Use connections REST endpoint for initial state diff --git a/projects/packages/publicize/src/class-connections.php b/projects/packages/publicize/src/class-connections.php new file mode 100644 index 0000000000000..36de3b630cdf8 --- /dev/null +++ b/projects/packages/publicize/src/class-connections.php @@ -0,0 +1,78 @@ +is_wpcom_simple(); + + if ( $is_wpcom ) { + // We don't need to cache connections for simple sites. + return Connections_Controller::get_connections( $run_tests ); + } + + $clear_cache = $args['clear_cache'] ?? false; + + if ( $clear_cache || $run_tests ) { + self::clear_transient(); + } + + $connections = get_transient( self::CONNECTIONS_TRANSIENT ); + + // This can be an empty array, so we need to check for false. + if ( false === $connections ) { + $connections = self::fetch_and_cache_connections( $run_tests ); + } + + return $connections; + } + + /** + * Fetch connections from the REST API and cache them. + * + * @param bool $run_tests Whether to run connection tests. + * + * @return array + */ + public static function fetch_and_cache_connections( $run_tests = false ) { + $connections = Connections_Controller::get_connections( $run_tests ); + + if ( is_array( $connections ) ) { + set_transient( self::CONNECTIONS_TRANSIENT, $connections, HOUR_IN_SECONDS * 4 ); + } + + return $connections; + } + + /** + * Delete the transient. + */ + public static function clear_transient() { + delete_transient( self::CONNECTIONS_TRANSIENT ); + } +} diff --git a/projects/packages/publicize/src/class-publicize-script-data.php b/projects/packages/publicize/src/class-publicize-script-data.php index b4e7dded6b62d..63ca3c726944a 100644 --- a/projects/packages/publicize/src/class-publicize-script-data.php +++ b/projects/packages/publicize/src/class-publicize-script-data.php @@ -21,8 +21,6 @@ */ class Publicize_Script_Data { - const SERVICES_TRANSIENT = 'jetpack_social_services_list'; - /** * Get the publicize instance - properly typed * @@ -160,8 +158,7 @@ public static function get_store_initial_state() { return array( 'connectionData' => array( - // We do not have this method on WPCOM Publicize class yet. - 'connections' => ! $is_wpcom ? self::publicize()->get_all_connections_for_user() : array(), + 'connections' => Connections::get_all(), ), 'shareStatus' => $share_status, ); @@ -238,17 +235,24 @@ public static function get_api_paths() { $is_simple_site = ( new Host() )->is_wpcom_simple(); + $commom_paths = array( + 'refreshConnections' => '/wpcom/v2/publicize/connections?test_connections=1', + ); + + $specific_paths = array(); + if ( $is_simple_site ) { - return array( - 'refreshConnections' => '/wpcom/v2/publicize/connection-test-results', - 'resharePost' => '/wpcom/v2/posts/{postId}/publicize', + + $specific_paths = array( + 'resharePost' => '/wpcom/v2/posts/{postId}/publicize', + ); + } else { + $specific_paths = array( + 'resharePost' => '/jetpack/v4/publicize/{postId}', ); } - return array( - 'refreshConnections' => '/jetpack/v4/publicize/connections?test_connections=1', - 'resharePost' => '/jetpack/v4/publicize/{postId}', - ); + return array_merge( $commom_paths, $specific_paths ); } /**