Skip to content

Commit aa40ce3

Browse files
authored
Social: Refactor Social Note settings to use core (#41153)
* Register settings with core * Update store to use core settings api * changelog * Remove deprecated code * Fix some tests * Fix constant issue * Cast pricing_page option to int as before * Remove boolean castings * Remove casting * Fix TS errors * Address comments on function names
1 parent a50939c commit aa40ce3

File tree

19 files changed

+266
-157
lines changed

19 files changed

+266
-157
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
Refactored Social Note settings to use core

projects/js-packages/publicize-components/src/social-store/actions/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as connectionData from './connection-data';
2+
import * as pricingPageSettings from './pricing-page';
23
import * as shareStatus from './share-status';
34
import * as sigActions from './social-image-generator';
5+
import * as socialNoteSettings from './social-notes';
46
import * as socialPluginSettings from './social-plugin-settings';
57
import * as utmActions from './utm-settings';
68

@@ -9,6 +11,8 @@ const actions = {
911
...connectionData,
1012
...sigActions,
1113
...utmActions,
14+
...socialNoteSettings,
15+
...pricingPageSettings,
1216
...socialPluginSettings,
1317
};
1418

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { store as coreStore } from '@wordpress/core-data';
2+
import { SHOW_PRICING_PAGE_KEY } from '../constants';
3+
4+
/**
5+
* Sets the Show Pricing Page enabled status.
6+
*
7+
* @param isEnabled - The new enabled status.
8+
* @return {Function} A thunk.
9+
*/
10+
export function setShowPricingPage( isEnabled: boolean ) {
11+
return async function ( { registry } ) {
12+
const { saveSite } = registry.dispatch( coreStore );
13+
14+
await saveSite( { [ SHOW_PRICING_PAGE_KEY ]: isEnabled } );
15+
};
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { store as coreStore } from '@wordpress/core-data';
2+
import { SOCIAL_NOTES_CONFIG_KEY, SOCIAL_NOTES_ENABLED_KEY } from '../constants';
3+
import { SocialNotesConfig } from '../types';
4+
5+
/**
6+
* Sets the Social Notes enabled status.
7+
*
8+
* @param isEnabled - The new enabled status.
9+
*
10+
* @return {Function} A thunk.
11+
*/
12+
export function toggleSocialNotes( isEnabled: boolean ) {
13+
return async function ( { registry } ) {
14+
const { saveSite } = registry.dispatch( coreStore );
15+
16+
await saveSite( { [ SOCIAL_NOTES_ENABLED_KEY ]: isEnabled } );
17+
};
18+
}
19+
20+
/**
21+
* Updates the Social Notes Config
22+
*
23+
* @param {Partial< SocialNotesConfig >} data - The data to save.
24+
*
25+
* @return {Function} A thunk.
26+
*/
27+
export function updateSocialNotesConfig( data: Partial< SocialNotesConfig > ) {
28+
return async function ( { registry } ) {
29+
const { saveSite } = registry.dispatch( coreStore );
30+
31+
await saveSite( { [ SOCIAL_NOTES_CONFIG_KEY ]: data } );
32+
};
33+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
// See projects/packages/publicize/src/jetpack-social-settings/class-settings.php
12
export const SIG_SETTINGS_KEY = 'jetpack_social_image_generator_settings';
23
export const UTM_ENABLED_KEY = 'jetpack_social_utm_settings';
4+
export const SOCIAL_NOTES_ENABLED_KEY = 'jetpack-social-note';
5+
export const SOCIAL_NOTES_CONFIG_KEY = 'jetpack_social_notes_config';
6+
export const SHOW_PRICING_PAGE_KEY = 'jetpack-social_show_pricing_page';

projects/js-packages/publicize-components/src/social-store/selectors/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { store as coreStore } from '@wordpress/core-data';
22
import { createRegistrySelector } from '@wordpress/data';
33
import * as connectionDataSelectors from './connection-data';
4+
import * as pricingPageSelectors from './pricing-page';
45
import * as shareStatusSelectors from './share-status';
56
import * as sigSelectors from './social-image-generator';
7+
import * as socialNoteSelectors from './social-notes';
68
import * as socialPluginSelectors from './social-plugin-settings';
79
import * as utmSelectors from './utm-settings';
810

@@ -19,6 +21,8 @@ const selectors = {
1921
isSavingSiteSettings,
2022
...sigSelectors,
2123
...utmSelectors,
24+
...socialNoteSelectors,
25+
...pricingPageSelectors,
2226
...socialPluginSelectors,
2327
};
2428

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { store as coreStore } from '@wordpress/core-data';
2+
import { createRegistrySelector } from '@wordpress/data';
3+
import { getSocialScriptData } from '../../utils';
4+
import { SHOW_PRICING_PAGE_KEY } from '../constants';
5+
6+
/**
7+
* Returns the Show Pricing Page enabled status for the current site.
8+
*/
9+
export const shouldShowPricingPage = createRegistrySelector( select => () => {
10+
const { getSite } = select( coreStore );
11+
12+
const settings = getSite( undefined, { _fields: SHOW_PRICING_PAGE_KEY } );
13+
14+
// If the settings are not available in the store yet, use the default settings.
15+
return (
16+
settings?.[ SHOW_PRICING_PAGE_KEY ] ??
17+
getSocialScriptData().settings?.socialPlugin?.show_pricing_page
18+
);
19+
} );
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { store as coreStore } from '@wordpress/core-data';
2+
import { createRegistrySelector } from '@wordpress/data';
3+
import { getSocialScriptData } from '../../utils';
4+
import { SOCIAL_NOTES_CONFIG_KEY, SOCIAL_NOTES_ENABLED_KEY } from '../constants';
5+
6+
/**
7+
* Returns if Social Notes are enabled for the current site.
8+
*/
9+
export const isSocialNotesEnabled = createRegistrySelector( select => () => {
10+
const { getSite } = select( coreStore );
11+
12+
const settings = getSite( undefined, {
13+
_fields: SOCIAL_NOTES_ENABLED_KEY,
14+
} );
15+
// If the settings are not available in the store yet, use the default settings.
16+
return (
17+
settings?.[ SOCIAL_NOTES_ENABLED_KEY ] ??
18+
getSocialScriptData().settings?.socialPlugin?.social_notes_enabled
19+
);
20+
} );
21+
22+
/**
23+
* Returns the Social Notes Config for the current site.
24+
*/
25+
export const getSocialNotesConfig = createRegistrySelector( select => () => {
26+
const { getSite } = select( coreStore );
27+
28+
const settings = getSite( undefined, {
29+
_fields: SOCIAL_NOTES_CONFIG_KEY,
30+
} );
31+
32+
// If the settings are not available in the store yet, use the default settings.
33+
return (
34+
settings?.[ SOCIAL_NOTES_CONFIG_KEY ] ??
35+
getSocialScriptData().settings?.socialPlugin?.social_notes_config
36+
);
37+
} );

projects/js-packages/publicize-components/src/social-store/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ export type UtmSettingsConfig = {
132132
enabled: boolean;
133133
};
134134

135+
export type SocialNotesConfig = {
136+
append_link: boolean;
137+
link_format: 'full_url' | 'shortlink' | 'permashortcitation';
138+
};
139+
135140
export type SocialPluginSettings = {
136141
publicize_active: boolean;
137142
show_pricing_page: boolean;
@@ -145,4 +150,7 @@ export type SocialPluginSettings = {
145150
export type SocialSettingsFields = {
146151
jetpack_social_image_generator_settings: SocialImageGeneratorConfig;
147152
jetpack_social_utm_settings: UtmSettingsConfig;
153+
[ 'jetpack-social-note' ]: boolean;
154+
jetpack_social_notes_config: SocialNotesConfig;
155+
[ 'jetpack-social_show_pricing_page' ]: boolean;
148156
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
Refactored Social Note settings to use core

0 commit comments

Comments
 (0)