diff --git a/projects/js-packages/publicize-components/changelog/update-social-initial-state-migrate-api-paths b/projects/js-packages/publicize-components/changelog/update-social-initial-state-migrate-api-paths new file mode 100644 index 0000000000000..76095067a3bf0 --- /dev/null +++ b/projects/js-packages/publicize-components/changelog/update-social-initial-state-migrate-api-paths @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Social; Migrated the API paths from initial state to the new script data diff --git a/projects/js-packages/publicize-components/src/hooks/use-share-post/index.js b/projects/js-packages/publicize-components/src/hooks/use-share-post/index.js index 17f865ccce260..0b94ee04a8844 100644 --- a/projects/js-packages/publicize-components/src/hooks/use-share-post/index.js +++ b/projects/js-packages/publicize-components/src/hooks/use-share-post/index.js @@ -1,4 +1,3 @@ -import { getJetpackData } from '@automattic/jetpack-shared-extension-utils'; import apiFetch from '@wordpress/api-fetch'; import { useSelect } from '@wordpress/data'; import { store as editorStore } from '@wordpress/editor'; @@ -6,6 +5,7 @@ import { useState, useCallback } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import useSocialMediaConnections from '../../hooks/use-social-media-connections'; import useSocialMediaMessage from '../../hooks/use-social-media-message'; +import { getSocialScriptData } from '../../utils/script-data'; /** * Takes an error object and returns a more meaningful error message. @@ -79,9 +79,7 @@ export default function useSharePost( postId ) { postId = postId || currentPostId; const [ data, setData ] = useState( { data: [], error: {} } ); - const path = ( - getJetpackData()?.social?.resharePath ?? '/wpcom/v2/posts/{postId}/publicize' - ).replace( '{postId}', postId ); + const path = getSocialScriptData().api_paths.resharePost.replace( '{postId}', postId ); const doPublicize = useCallback( function () { diff --git a/projects/js-packages/publicize-components/src/social-store/actions/connection-data.js b/projects/js-packages/publicize-components/src/social-store/actions/connection-data.js index 1594de17cb502..8c23c207ad21d 100644 --- a/projects/js-packages/publicize-components/src/social-store/actions/connection-data.js +++ b/projects/js-packages/publicize-components/src/social-store/actions/connection-data.js @@ -3,6 +3,7 @@ import apiFetch from '@wordpress/api-fetch'; import { dispatch as coreDispatch } from '@wordpress/data'; import { store as editorStore } from '@wordpress/editor'; import { __, sprintf } from '@wordpress/i18n'; +import { getSocialScriptData } from '../../utils/script-data'; import { ADD_CONNECTION, DELETE_CONNECTION, @@ -177,7 +178,7 @@ export function abortRefreshConnectionsRequest() { export function refreshConnectionTestResults( syncToMeta = false ) { return async function ( { dispatch, select } ) { try { - const path = select.connectionRefreshPath() || '/wpcom/v2/publicize/connection-test-results'; + const path = getSocialScriptData().api_paths.refreshConnections; // Wait until all connections are done updating/deleting. while ( 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 e4ebbc87cbde1..86b84bc27c9b3 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 @@ -139,10 +139,21 @@ describe( 'Social store actions: connectionData', () => { } ); describe( 'refreshConnectionTestResults', () => { + const refreshConnections = '/wpcom/v2/publicize/connection-test-results'; + beforeAll( () => { + global.JetpackScriptData = { + social: { + api_paths: { + refreshConnections, + }, + }, + }; + } ); + it( 'should refresh connection test results', async () => { // Mock apiFetch response. apiFetch.setFetchHandler( async ( { path } ) => { - if ( path.startsWith( '/wpcom/v2/publicize/connection-test-results' ) ) { + if ( path.startsWith( refreshConnections ) ) { return connections.map( connection => ( { ...connection, can_refresh: false, diff --git a/projects/js-packages/publicize-components/src/social-store/reducer/index.js b/projects/js-packages/publicize-components/src/social-store/reducer/index.js index e330313125616..6bb4ca08a345b 100644 --- a/projects/js-packages/publicize-components/src/social-store/reducer/index.js +++ b/projects/js-packages/publicize-components/src/social-store/reducer/index.js @@ -16,7 +16,6 @@ const reducer = combineReducers( { useAdminUiV1: ( state = false ) => state, featureFlags: ( state = false ) => state, hasPaidFeatures: ( state = false ) => state, - connectionRefreshPath: ( state = '' ) => state, } ); export default reducer; diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/index.js b/projects/js-packages/publicize-components/src/social-store/selectors/index.js index ad6608112a2fe..00b33e2c69533 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/index.js +++ b/projects/js-packages/publicize-components/src/social-store/selectors/index.js @@ -14,7 +14,6 @@ const selectors = { useAdminUiV1: state => state.useAdminUiV1, featureFlags: state => state.featureFlags, hasPaidFeatures: state => state.hasPaidFeatures, - connectionRefreshPath: state => state.connectionRefreshPath, }; export default selectors; diff --git a/projects/js-packages/publicize-components/src/types/types.ts b/projects/js-packages/publicize-components/src/types/types.ts index 10abe2931230d..4134cea695179 100644 --- a/projects/js-packages/publicize-components/src/types/types.ts +++ b/projects/js-packages/publicize-components/src/types/types.ts @@ -12,7 +12,13 @@ export type ConnectionService = { multiple_external_user_ID_support?: boolean; }; +export interface ApiPaths { + refreshConnections: string; + resharePost: string; +} + export interface SocialScriptData { + api_paths: ApiPaths; is_publicize_enabled: boolean; feature_flags: FeatureFlags; supported_services: Array< ConnectionService >; diff --git a/projects/packages/publicize/changelog/update-social-initial-state-migrate-api-paths b/projects/packages/publicize/changelog/update-social-initial-state-migrate-api-paths new file mode 100644 index 0000000000000..76095067a3bf0 --- /dev/null +++ b/projects/packages/publicize/changelog/update-social-initial-state-migrate-api-paths @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Social; Migrated the API paths from initial state to the new script data diff --git a/projects/packages/publicize/src/class-publicize-script-data.php b/projects/packages/publicize/src/class-publicize-script-data.php index 77f5a11ef3016..ba9d6631e41cb 100644 --- a/projects/packages/publicize/src/class-publicize-script-data.php +++ b/projects/packages/publicize/src/class-publicize-script-data.php @@ -11,6 +11,7 @@ use Automattic\Jetpack\Connection\Manager; use Automattic\Jetpack\Current_Plan; use Automattic\Jetpack\Publicize\Publicize_Utils as Utils; +use Automattic\Jetpack\Status\Host; /** * Publicize_Script_Data class. @@ -30,6 +31,11 @@ public static function publicize() { */ global $publicize; + if ( ! $publicize && function_exists( 'publicize_init' ) ) { + // @phan-suppress-next-line PhanUndeclaredFunction - phan is dumb not to see the function_exists check + publicize_init(); + } + return $publicize; } @@ -49,7 +55,7 @@ public static function set_admin_script_data( $data ) { $data['social'] = self::get_admin_script_data(); - if ( empty( $data['site']['plan'] ) ) { + if ( empty( $data['site']['plan']['product_slug'] ) ) { $data['site']['plan'] = Current_Plan::get(); } @@ -74,18 +80,27 @@ public static function get_admin_script_data() { } $basic_data = array( + 'api_paths' => array(), 'is_publicize_enabled' => Utils::is_publicize_active(), 'feature_flags' => self::get_feature_flags(), 'supported_services' => array(), ); - if ( ! Utils::is_publicize_active() || ! Utils::is_connected() ) { + if ( ! Utils::is_publicize_active() ) { + return $basic_data; + } + + // Simple sites don't have a user connection. + $is_publicize_configured = ( new Host() )->is_wpcom_simple() || Utils::is_connected(); + + if ( ! $is_publicize_configured ) { return $basic_data; } return array_merge( $basic_data, array( + 'api_paths' => self::get_api_paths(), 'supported_services' => self::get_supported_services(), /** * 'store' => self::get_store_script_data(), @@ -166,4 +181,26 @@ function ( $service ) { ) ); } + + /** + * Get the API paths. + * + * @return array + */ + public static function get_api_paths() { + + $is_simple_site = ( new Host() )->is_wpcom_simple(); + + if ( $is_simple_site ) { + return array( + 'refreshConnections' => '/wpcom/v2/publicize/connection-test-results', + 'resharePost' => '/wpcom/v2/posts/{postId}/publicize', + ); + } + + return array( + 'refreshConnections' => '/jetpack/v4/publicize/connections?test_connections=1', + 'resharePost' => '/jetpack/v4/publicize/{postId}', + ); + } }