Skip to content

Commit

Permalink
Social | initial state: Migrate API paths to new script data (#38962)
Browse files Browse the repository at this point in the history
* Social | initial state: Migrate API paths to new script data

* Fix site plan data

* Phan is dumb

* Fix JS tests
  • Loading branch information
manzoorwanijk authored Aug 21, 2024
1 parent accb8c6 commit c4f01da
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Social; Migrated the API paths from initial state to the new script data
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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';
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.
Expand Down Expand Up @@ -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 () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions projects/js-packages/publicize-components/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 >;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Social; Migrated the API paths from initial state to the new script data
41 changes: 39 additions & 2 deletions projects/packages/publicize/src/class-publicize-script-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

Expand All @@ -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();
}

Expand All @@ -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(),
Expand Down Expand Up @@ -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}',
);
}
}

0 comments on commit c4f01da

Please sign in to comment.