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
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

+4
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

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+
}
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+
}
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

+4
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

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+
} );
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

+8
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
};
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/packages/publicize/src/jetpack-social-settings/class-settings.php

+101
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* This class is used to get and update Jetpack_Social_Settings.
1717
* Currently supported features:
1818
* - Social Image Generator
19+
* - UTM Settings
20+
* - Social Notes
1921
*/
2022
class Settings {
2123
/**
@@ -37,6 +39,17 @@ class Settings {
3739
'enabled' => false,
3840
);
3941

42+
const NOTES_CONFIG = 'notes_config';
43+
44+
const DEFAULT_NOTES_CONFIG = array(
45+
'append_link' => true,
46+
);
47+
48+
// Legacy named options.
49+
const JETPACK_SOCIAL_NOTE_CPT_ENABLED = 'jetpack-social-note';
50+
const JETPACK_SOCIAL_SHOW_PRICING_PAGE = 'jetpack-social_show_pricing_page';
51+
const NOTES_FLUSH_REWRITE_RULES_FLUSHED = 'jetpack_social_rewrite_rules_flushed';
52+
4053
/**
4154
* Feature flags. Each item has 3 keys because of the naming conventions:
4255
* - flag_name: The name of the feature flag for the option check.
@@ -154,6 +167,58 @@ public function register_settings() {
154167
)
155168
);
156169

170+
register_setting(
171+
'jetpack_social',
172+
self::JETPACK_SOCIAL_SHOW_PRICING_PAGE,
173+
array(
174+
'type' => 'boolean',
175+
'default' => true,
176+
'show_in_rest' => array(
177+
'schema' => array(
178+
'type' => 'boolean',
179+
),
180+
),
181+
)
182+
);
183+
184+
register_setting(
185+
'jetpack_social',
186+
self::JETPACK_SOCIAL_NOTE_CPT_ENABLED,
187+
array(
188+
'type' => 'boolean',
189+
'default' => false,
190+
'show_in_rest' => array(
191+
'schema' => array(
192+
'type' => 'boolean',
193+
),
194+
),
195+
)
196+
);
197+
198+
register_setting(
199+
'jetpack_social',
200+
self::OPTION_PREFIX . self::NOTES_CONFIG,
201+
array(
202+
'type' => 'object',
203+
'default' => self::DEFAULT_NOTES_CONFIG,
204+
'show_in_rest' => array(
205+
'schema' => array(
206+
'type' => 'object',
207+
'context' => array( 'view', 'edit' ),
208+
'properties' => array(
209+
'append_link' => array(
210+
'type' => 'boolean',
211+
),
212+
'link_format' => array(
213+
'type' => 'string',
214+
'enum' => array( 'full_url', 'shortlink', 'permashortcitation' ),
215+
),
216+
),
217+
),
218+
),
219+
)
220+
);
221+
157222
add_filter( 'rest_pre_update_setting', array( $this, 'update_settings' ), 10, 3 );
158223
}
159224

@@ -175,6 +240,24 @@ public function get_utm_settings() {
175240
return get_option( self::OPTION_PREFIX . self::UTM_SETTINGS, self::DEFAULT_UTM_SETTINGS );
176241
}
177242

243+
/**
244+
* Get the social notes config.
245+
*
246+
* @return array The social notes config.
247+
*/
248+
public function get_social_notes_config() {
249+
return get_option( self::OPTION_PREFIX . self::NOTES_CONFIG, self::DEFAULT_NOTES_CONFIG );
250+
}
251+
252+
/**
253+
* Get if the social notes feature is enabled.
254+
*
255+
* @return bool
256+
*/
257+
public function is_social_notes_enabled() {
258+
return get_option( self::JETPACK_SOCIAL_NOTE_CPT_ENABLED, false );
259+
}
260+
178261
/**
179262
* Get the current settings.
180263
*
@@ -253,10 +336,12 @@ public function get_initial_state() {
253336
*/
254337
public function update_settings( $updated, $name, $value ) {
255338

339+
// Social Image Generator.
256340
if ( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS === $name ) {
257341
return $this->update_social_image_generator_settings( $value );
258342
}
259343

344+
// UTM Settings.
260345
if ( self::OPTION_PREFIX . self::UTM_SETTINGS === $name ) {
261346
$current_utm_settings = $this->get_utm_settings();
262347

@@ -267,6 +352,22 @@ public function update_settings( $updated, $name, $value ) {
267352
return update_option( self::OPTION_PREFIX . self::UTM_SETTINGS, array_replace_recursive( $current_utm_settings, $value ) );
268353
}
269354

355+
// Social Notes.
356+
if ( self::JETPACK_SOCIAL_NOTE_CPT_ENABLED === $name ) {
357+
// Delete this option, so the rules get flushed in maybe_flush_rewrite_rules when the CPT is registered.
358+
delete_option( self::NOTES_FLUSH_REWRITE_RULES_FLUSHED );
359+
return update_option( self::JETPACK_SOCIAL_NOTE_CPT_ENABLED, (bool) $value );
360+
}
361+
if ( self::OPTION_PREFIX . self::NOTES_CONFIG === $name ) {
362+
$old_config = $this->get_social_notes_config();
363+
$new_config = array_merge( $old_config, $value );
364+
return update_option( self::OPTION_PREFIX . self::NOTES_CONFIG, $new_config );
365+
}
366+
367+
if ( self::JETPACK_SOCIAL_SHOW_PRICING_PAGE === $name ) {
368+
return update_option( self::JETPACK_SOCIAL_SHOW_PRICING_PAGE, (int) $value );
369+
}
370+
270371
return $updated;
271372
}
272373

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/plugins/social/src/class-jetpack-social.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,14 @@ public function set_social_admin_script_data( $data ) {
245245

246246
if ( $this->is_connected() ) {
247247

248-
$note = new Automattic\Jetpack\Social\Note();
248+
$jetpack_social_settings = new Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings();
249249

250250
$data['settings']['socialPlugin'] = array_merge(
251251
$data['settings']['socialPlugin'],
252252
array(
253253
'show_pricing_page' => self::should_show_pricing_page(),
254-
'social_notes_enabled' => $note->enabled(),
255-
'social_notes_config' => $note->get_config(),
254+
'social_notes_enabled' => $jetpack_social_settings->is_social_notes_enabled(),
255+
'social_notes_config' => $jetpack_social_settings->get_social_notes_config(),
256256
)
257257
);
258258
}
@@ -284,8 +284,6 @@ public function initial_state() {
284284
$jetpack_social_settings = new Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings();
285285
$initial_state = $jetpack_social_settings->get_initial_state();
286286

287-
$note = new Automattic\Jetpack\Social\Note();
288-
289287
$state = array_merge(
290288
$state,
291289
array(
@@ -296,8 +294,8 @@ public function initial_state() {
296294
'isEnhancedPublishingEnabled' => $publicize->has_enhanced_publishing_feature(),
297295
'dismissedNotices' => Dismissed_Notices::get_dismissed_notices(),
298296
'supportedAdditionalConnections' => $publicize->get_supported_additional_connections(),
299-
'social_notes_enabled' => $note->enabled(),
300-
'social_notes_config' => $note->get_config(),
297+
'social_notes_enabled' => $jetpack_social_settings->is_social_notes_enabled(),
298+
'social_notes_config' => $jetpack_social_settings->get_social_notes_config(),
301299
),
302300
'sharesData' => $publicize->get_publicize_shares_info( Jetpack_Options::get_option( 'id' ) ),
303301
),

0 commit comments

Comments
 (0)