File tree 8 files changed +64
-49
lines changed
toggles/social-module-toggle
8 files changed +64
-49
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ This is an alpha version! The changes listed here are not final.
22
22
23
23
### Fixed
24
24
- Clean up Social admin page unit tests
25
+ - Social | Avoid calling the settings endpoint on Social admin page if not relevant
25
26
26
27
## [ 0.79.0] - 2025-02-24
27
28
### Added
Original file line number Diff line number Diff line change @@ -81,8 +81,10 @@ describe( 'SocialAdminPage', () => {
81
81
82
82
describe ( 'Social Notes toggle' , ( ) => {
83
83
it ( 'should show when plugin is active and module is enabled' , ( ) => {
84
+ mockScriptData ( ) ;
84
85
render ( < SocialAdminPage /> ) ;
85
86
expect ( screen . getByText ( 'Enable Social Notes' ) ) . toBeInTheDocument ( ) ;
87
+ clearMockedScriptData ( ) ;
86
88
} ) ;
87
89
88
90
it ( 'should not show when plugin is not active' , ( ) => {
@@ -107,7 +109,7 @@ describe( 'SocialAdminPage', () => {
107
109
} ,
108
110
} ) ;
109
111
render ( < SocialAdminPage /> ) ;
110
- expect ( screen . queryByTestId ( 'social-notes-toggle ' ) ) . not . toBeInTheDocument ( ) ;
112
+ expect ( screen . queryByText ( 'Enable Social Notes ' ) ) . not . toBeInTheDocument ( ) ;
111
113
clearMockedScriptData ( ) ;
112
114
} ) ;
113
115
} ) ;
Original file line number Diff line number Diff line change @@ -5,18 +5,15 @@ import {
5
5
getRedirectUrl ,
6
6
useBreakpointMatch ,
7
7
} from '@automattic/jetpack-components' ;
8
- import {
9
- getScriptData ,
10
- isWpcomPlatformSite ,
11
- currentUserCan ,
12
- } from '@automattic/jetpack-script-data' ;
8
+ import { getScriptData , isWpcomPlatformSite } from '@automattic/jetpack-script-data' ;
13
9
import { ExternalLink } from '@wordpress/components' ;
14
10
import { useSelect , useDispatch } from '@wordpress/data' ;
15
11
import { __ , _x } from '@wordpress/i18n' ;
16
12
import clsx from 'clsx' ;
17
13
import React , { useCallback } from 'react' ;
18
14
import { store as socialStore } from '../../../../social-store' ;
19
15
import { getSocialScriptData , hasSocialPaidFeatures } from '../../../../utils' ;
16
+ import { canToggleSocialModule } from '../../../../utils/misc' ;
20
17
import ConnectionManagement from '../../../connection-management' ;
21
18
import ToggleSection from '../toggle-section' ;
22
19
import styles from './styles.module.scss' ;
@@ -81,7 +78,7 @@ const SocialModuleToggle: React.FC = () => {
81
78
) : null ;
82
79
} ;
83
80
84
- const hideToggle = is_wpcom || ! currentUserCan ( 'manage_modules' ) ;
81
+ const hideToggle = ! canToggleSocialModule ( ) ;
85
82
return (
86
83
< ToggleSection
87
84
hideToggle = { hideToggle }
Original file line number Diff line number Diff line change @@ -7,16 +7,40 @@ import { getSocialScriptData } from '../utils';
7
7
* Hydrate the data stores
8
8
*/
9
9
export async function hydrateStores ( ) {
10
+ const {
11
+ addEntities,
12
+ receiveEntityRecords,
13
+ // @ts -expect-error finishResolution exists but it's not typed
14
+ finishResolution,
15
+ } = dispatch ( coreStore ) ;
16
+
17
+ const socialToggleBase = getSocialScriptData ( ) ?. api_paths ?. socialToggleBase ;
18
+
19
+ const jetpackEntities = select ( coreStore ) . getEntitiesConfig ( 'jetpack/v4' ) ;
20
+ if ( ! jetpackEntities . some ( ( { name } ) => name === socialToggleBase ) ) {
21
+ await addEntities ( [
22
+ {
23
+ kind : 'jetpack/v4' ,
24
+ name : socialToggleBase ,
25
+ baseURL : `/jetpack/v4/${ socialToggleBase } ` ,
26
+ label : __ ( 'Social Settings' , 'jetpack-publicize-components' ) ,
27
+ } ,
28
+ ] ) ;
29
+
30
+ // @ts -expect-error Only 3 arguments are required, rest are optional but types expect 7
31
+ await receiveEntityRecords (
32
+ 'jetpack/v4' ,
33
+ socialToggleBase ,
34
+ { publicize : getSocialScriptData ( ) ?. is_publicize_enabled } ,
35
+ true
36
+ ) ;
37
+
38
+ await finishResolution ( 'getEntityRecord' , [ 'jetpack/v4' , socialToggleBase ] ) ;
39
+ }
40
+
10
41
const wpcomEntities = select ( coreStore ) . getEntitiesConfig ( 'wpcom/v2' ) ;
11
42
12
43
if ( ! wpcomEntities . some ( ( { name } ) => name === 'publicize/services' ) ) {
13
- const {
14
- addEntities,
15
- receiveEntityRecords,
16
- // @ts -expect-error finishResolution exists but it's not typed
17
- finishResolution,
18
- } = dispatch ( coreStore ) ;
19
-
20
44
await addEntities ( [
21
45
{
22
46
kind : 'wpcom/v2' ,
Original file line number Diff line number Diff line change 1
1
import apiFetch from '@wordpress/api-fetch' ;
2
- import { store as coreStore } from '@wordpress/core-data' ;
3
2
import { store as editorStore } from '@wordpress/editor' ;
4
- import { __ } from '@wordpress/i18n' ;
5
3
import { getSocialScriptData } from '../utils/script-data' ;
6
4
import { normalizeShareStatus } from '../utils/share-status' ;
7
5
import { setConnections } from './actions/connection-data' ;
@@ -77,32 +75,7 @@ export function getPostShareStatus( _postId ) {
77
75
} ;
78
76
}
79
77
80
- /**
81
- * Resolves the social module settings to ensure the core-data entities are registered.
82
- *
83
- * @return {Function } Resolver
84
- */
85
- export function getSocialModuleSettings ( ) {
86
- return async ( { registry } ) => {
87
- const { socialToggleBase } = getSocialScriptData ( ) . api_paths ;
88
-
89
- const jetpackEntities = registry . select ( coreStore ) . getEntitiesConfig ( 'jetpack/v4' ) ;
90
-
91
- if ( ! jetpackEntities . some ( ( { name } ) => name === socialToggleBase ) ) {
92
- await registry . dispatch ( coreStore ) . addEntities ( [
93
- {
94
- kind : 'jetpack/v4' ,
95
- name : socialToggleBase ,
96
- baseURL : `/jetpack/v4/${ socialToggleBase } ` ,
97
- label : __ ( 'Social Settings' , 'jetpack-publicize-components' ) ,
98
- } ,
99
- ] ) ;
100
- }
101
- } ;
102
- }
103
-
104
78
export default {
105
79
getConnections,
106
80
getPostShareStatus,
107
- getSocialModuleSettings,
108
81
} ;
Original file line number Diff line number Diff line change @@ -6,15 +6,18 @@ import { SocialModuleSettings } from '../types';
6
6
/**
7
7
* Returns the Social module settings.
8
8
*/
9
- export const getSocialModuleSettings = createRegistrySelector ( select => ( ) => {
10
- const { socialToggleBase } = getSocialScriptData ( ) . api_paths ;
9
+ export const getSocialModuleSettings = createRegistrySelector (
10
+ select => ( ) : SocialModuleSettings => {
11
+ const { api_paths, is_publicize_enabled } = getSocialScriptData ( ) ;
11
12
12
- const data = select ( coreStore ) . getEntityRecord ( 'jetpack/v4' , socialToggleBase , undefined ) ;
13
+ const data = select ( coreStore ) . getEntityRecord < SocialModuleSettings > (
14
+ 'jetpack/v4' ,
15
+ api_paths . socialToggleBase
16
+ ) ;
13
17
14
- return ( data ?? {
15
- publicize : getSocialScriptData ( ) . is_publicize_enabled ,
16
- } ) as SocialModuleSettings ;
17
- } ) ;
18
+ return data ?? { publicize : is_publicize_enabled } ;
19
+ }
20
+ ) ;
18
21
19
22
/**
20
23
* Returns whether the Social module settings are being saved
Original file line number Diff line number Diff line change
1
+ import { currentUserCan } from '@automattic/jetpack-script-data' ;
1
2
import { store as coreStore } from '@wordpress/core-data' ;
2
3
import { createRegistrySelector } from '@wordpress/data' ;
3
4
import { SocialSettings } from '../../types' ;
@@ -15,7 +16,9 @@ export const isSavingSiteSettings = createRegistrySelector( select => () => {
15
16
* Returns the social settings.
16
17
*/
17
18
export const getSocialSettings = createRegistrySelector ( select => ( ) => {
18
- const data = select ( coreStore ) . getEntityRecord < SocialSettingsFields > ( 'root' , 'site' ) ;
19
+ const data = currentUserCan ( 'manage_options' )
20
+ ? select ( coreStore ) . getEntityRecord < SocialSettingsFields > ( 'root' , 'site' )
21
+ : null ;
19
22
20
23
const { settings } = getSocialScriptData ( ) ;
21
24
Original file line number Diff line number Diff line change
1
+ import { currentUserCan , getScriptData } from '@automattic/jetpack-script-data' ;
2
+
3
+ /**
4
+ * Check if the social module can be toggled.
5
+ *
6
+ * @return Whether the social module can be toggled.
7
+ */
8
+ export function canToggleSocialModule ( ) {
9
+ const is_wpcom = getScriptData ( ) . site . host === 'wpcom' ;
10
+
11
+ return ! is_wpcom && currentUserCan ( 'manage_modules' ) ;
12
+ }
You can’t perform that action at this time.
0 commit comments