From ace4157e0ad6002eb4947cfc4d16450f17fea0b9 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Fri, 17 Jan 2025 12:52:33 +0100 Subject: [PATCH 01/11] Register settings with core --- .../class-settings.php | 97 +++++++++++++++++++ .../social/src/class-jetpack-social.php | 12 +-- 2 files changed, 102 insertions(+), 7 deletions(-) diff --git a/projects/packages/publicize/src/jetpack-social-settings/class-settings.php b/projects/packages/publicize/src/jetpack-social-settings/class-settings.php index 85aa711d47a3b..b9008b3a9de3c 100644 --- a/projects/packages/publicize/src/jetpack-social-settings/class-settings.php +++ b/projects/packages/publicize/src/jetpack-social-settings/class-settings.php @@ -11,11 +11,14 @@ use Automattic\Jetpack\Modules; use Automattic\Jetpack\Publicize\Publicize_Script_Data; use Automattic\Jetpack\Publicize\Social_Image_Generator\Templates; +use Automattic\Jetpack\Social\Note; /** * This class is used to get and update Jetpack_Social_Settings. * Currently supported features: * - Social Image Generator + * - UTM Settings + * - Social Notes */ class Settings { /** @@ -37,6 +40,16 @@ class Settings { 'enabled' => false, ); + const NOTES_CONFIG = 'notes_config'; + + const DEFAULT_NOTES_CONFIG = array( + 'append_link' => true, + ); + + // Legacy named options. + const JETPACK_SOCIAL_NOTE_CPT_ENABLED = 'jetpack-social-note'; + const JETPACK_SOCIAL_SHOW_PRICING_PAGE = 'jetpack-social_show_pricing_page'; + /** * Feature flags. Each item has 3 keys because of the naming conventions: * - flag_name: The name of the feature flag for the option check. @@ -154,6 +167,58 @@ public function register_settings() { ) ); + register_setting( + 'jetpack_social', + self::JETPACK_SOCIAL_SHOW_PRICING_PAGE, + array( + 'type' => 'boolean', + 'default' => true, + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'boolean', + ), + ), + ) + ); + + register_setting( + 'jetpack_social', + self::JETPACK_SOCIAL_NOTE_CPT_ENABLED, + array( + 'type' => 'boolean', + 'default' => false, + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'boolean', + ), + ), + ) + ); + + register_setting( + 'jetpack_social', + self::OPTION_PREFIX . self::NOTES_CONFIG, + array( + 'type' => 'object', + 'default' => self::DEFAULT_NOTES_CONFIG, + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'context' => array( 'view', 'edit' ), + 'properties' => array( + 'append_link' => array( + 'type' => 'boolean', + ), + 'link_format' => array( + 'type' => 'string', + 'enum' => array( 'full_url', 'shortlink', 'permashortcitation' ), + ), + ), + ), + ), + ) + ); + add_filter( 'rest_pre_update_setting', array( $this, 'update_settings' ), 10, 3 ); } @@ -175,6 +240,24 @@ public function get_utm_settings() { return get_option( self::OPTION_PREFIX . self::UTM_SETTINGS, self::DEFAULT_UTM_SETTINGS ); } + /** + * Get the social notes config. + * + * @return array The social notes config. + */ + public function get_social_notes_config() { + return get_option( self::OPTION_PREFIX . self::NOTES_CONFIG, self::DEFAULT_NOTES_CONFIG ); + } + + /** + * Get if the social notes feature is enabled. + * + * @return bool + */ + public function is_social_notes_enabled() { + return get_option( self::JETPACK_SOCIAL_NOTE_CPT_ENABLED, false ); + } + /** * Get the current settings. * @@ -253,10 +336,12 @@ public function get_initial_state() { */ public function update_settings( $updated, $name, $value ) { + // Social Image Generator. if ( self::OPTION_PREFIX . self::IMAGE_GENERATOR_SETTINGS === $name ) { return $this->update_social_image_generator_settings( $value ); } + // UTM Settings. if ( self::OPTION_PREFIX . self::UTM_SETTINGS === $name ) { $current_utm_settings = $this->get_utm_settings(); @@ -267,6 +352,18 @@ public function update_settings( $updated, $name, $value ) { return update_option( self::OPTION_PREFIX . self::UTM_SETTINGS, array_replace_recursive( $current_utm_settings, $value ) ); } + // Social Notes. + if ( self::JETPACK_SOCIAL_NOTE_CPT_ENABLED === $name ) { + // Delete this option, so the rules get flushed in maybe_flush_rewrite_rules when the CPT is registered. + delete_option( Note::FLUSH_REWRITE_RULES_FLUSHED ); + return update_option( self::JETPACK_SOCIAL_NOTE_CPT_ENABLED, (bool) $value ); + } + if ( self::OPTION_PREFIX . self::NOTES_CONFIG === $name ) { + $old_config = $this->get_social_notes_config(); + $new_config = array_merge( $old_config, $value ); + return update_option( self::OPTION_PREFIX . self::NOTES_CONFIG, $new_config ); + } + return $updated; } diff --git a/projects/plugins/social/src/class-jetpack-social.php b/projects/plugins/social/src/class-jetpack-social.php index 6f545e40ea5d3..9b6d4a012d465 100644 --- a/projects/plugins/social/src/class-jetpack-social.php +++ b/projects/plugins/social/src/class-jetpack-social.php @@ -245,14 +245,14 @@ public function set_social_admin_script_data( $data ) { if ( $this->is_connected() ) { - $note = new Automattic\Jetpack\Social\Note(); + $jetpack_social_settings = new Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings(); $data['settings']['socialPlugin'] = array_merge( $data['settings']['socialPlugin'], array( 'show_pricing_page' => self::should_show_pricing_page(), - 'social_notes_enabled' => $note->enabled(), - 'social_notes_config' => $note->get_config(), + 'social_notes_enabled' => $jetpack_social_settings->is_social_notes_enabled(), + 'social_notes_config' => $jetpack_social_settings->get_social_notes_config(), ) ); } @@ -284,8 +284,6 @@ public function initial_state() { $jetpack_social_settings = new Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings(); $initial_state = $jetpack_social_settings->get_initial_state(); - $note = new Automattic\Jetpack\Social\Note(); - $state = array_merge( $state, array( @@ -296,8 +294,8 @@ public function initial_state() { 'isEnhancedPublishingEnabled' => $publicize->has_enhanced_publishing_feature(), 'dismissedNotices' => Dismissed_Notices::get_dismissed_notices(), 'supportedAdditionalConnections' => $publicize->get_supported_additional_connections(), - 'social_notes_enabled' => $note->enabled(), - 'social_notes_config' => $note->get_config(), + 'social_notes_enabled' => $jetpack_social_settings->is_social_notes_enabled(), + 'social_notes_config' => $jetpack_social_settings->get_social_notes_config(), ), 'sharesData' => $publicize->get_publicize_shares_info( Jetpack_Options::get_option( 'id' ) ), ), From c98ec33256d427f731979ad2456f4638b3f8f0a8 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Fri, 17 Jan 2025 12:52:50 +0100 Subject: [PATCH 02/11] Update store to use core settings api --- .../src/social-store/actions/index.ts | 4 ++ .../src/social-store/actions/pricing-page.ts | 16 +++++++ .../src/social-store/actions/social-notes.ts | 33 ++++++++++++++ .../src/social-store/constants.ts | 4 ++ .../src/social-store/selectors/index.ts | 4 ++ .../social-store/selectors/pricing-page.ts | 20 +++++++++ .../social-store/selectors/social-notes.ts | 41 +++++++++++++++++ .../src/social-store/types.ts | 5 +++ .../src/js/components/admin-page/index.jsx | 2 +- .../src/js/components/pricing-page/index.js | 6 +-- .../components/social-notes-toggle/index.tsx | 45 ++++++++----------- 11 files changed, 149 insertions(+), 31 deletions(-) create mode 100644 projects/js-packages/publicize-components/src/social-store/actions/pricing-page.ts create mode 100644 projects/js-packages/publicize-components/src/social-store/actions/social-notes.ts create mode 100644 projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts create mode 100644 projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts diff --git a/projects/js-packages/publicize-components/src/social-store/actions/index.ts b/projects/js-packages/publicize-components/src/social-store/actions/index.ts index a4afab1b499db..40463ed09d6b2 100644 --- a/projects/js-packages/publicize-components/src/social-store/actions/index.ts +++ b/projects/js-packages/publicize-components/src/social-store/actions/index.ts @@ -1,6 +1,8 @@ import * as connectionData from './connection-data'; +import * as pricingPageSettings from './pricing-page'; import * as shareStatus from './share-status'; import * as sigActions from './social-image-generator'; +import * as socialNoteSettings from './social-notes'; import * as socialPluginSettings from './social-plugin-settings'; import * as utmActions from './utm-settings'; @@ -9,6 +11,8 @@ const actions = { ...connectionData, ...sigActions, ...utmActions, + ...socialNoteSettings, + ...pricingPageSettings, ...socialPluginSettings, }; diff --git a/projects/js-packages/publicize-components/src/social-store/actions/pricing-page.ts b/projects/js-packages/publicize-components/src/social-store/actions/pricing-page.ts new file mode 100644 index 0000000000000..e73c2b745d0fa --- /dev/null +++ b/projects/js-packages/publicize-components/src/social-store/actions/pricing-page.ts @@ -0,0 +1,16 @@ +import { store as coreStore } from '@wordpress/core-data'; +import { SHOW_PRICING_PAGE_KEY } from '../constants'; + +/** + * Sets the Show Pricing Page enabled status. + * + * @param isEnabled - The new enabled status. + * @return {Function} A thunk. + */ +export function setShowPricingPage( isEnabled: boolean ) { + return async function ( { registry } ) { + const { saveSite } = registry.dispatch( coreStore ); + + await saveSite( { [ SHOW_PRICING_PAGE_KEY ]: isEnabled } ); + }; +} diff --git a/projects/js-packages/publicize-components/src/social-store/actions/social-notes.ts b/projects/js-packages/publicize-components/src/social-store/actions/social-notes.ts new file mode 100644 index 0000000000000..511a4556bebbb --- /dev/null +++ b/projects/js-packages/publicize-components/src/social-store/actions/social-notes.ts @@ -0,0 +1,33 @@ +import { store as coreStore } from '@wordpress/core-data'; +import { SOCIAL_NOTES_CONFIG_KEY, SOCIAL_NOTES_ENABLED_KEY } from '../constants'; +import { SocialNotesConfig } from '../types'; + +/** + * Sets the Social Notes enabled status. + * + * @param isEnabled - The new enabled status. + * + * @return {Function} A thunk. + */ +export function setSocialNotesEnabled( isEnabled: boolean ) { + return async function ( { registry } ) { + const { saveSite } = registry.dispatch( coreStore ); + + await saveSite( { [ SOCIAL_NOTES_ENABLED_KEY ]: isEnabled } ); + }; +} + +/** + * Updates the Social Notes Config + * + * @param {Partial< SocialNotesConfig >} data - The data to save. + * + * @return {Function} A thunk. + */ +export function updateSocialNotesConfig( data: Partial< SocialNotesConfig > ) { + return async function ( { registry } ) { + const { saveSite } = registry.dispatch( coreStore ); + + await saveSite( { [ SOCIAL_NOTES_CONFIG_KEY ]: data } ); + }; +} diff --git a/projects/js-packages/publicize-components/src/social-store/constants.ts b/projects/js-packages/publicize-components/src/social-store/constants.ts index 355c47d3e90f6..823740e7dc07e 100644 --- a/projects/js-packages/publicize-components/src/social-store/constants.ts +++ b/projects/js-packages/publicize-components/src/social-store/constants.ts @@ -1,2 +1,6 @@ +// See projects/packages/publicize/src/jetpack-social-settings/class-settings.php export const SIG_SETTINGS_KEY = 'jetpack_social_image_generator_settings'; export const UTM_ENABLED_KEY = 'jetpack_social_utm_settings'; +export const SOCIAL_NOTES_ENABLED_KEY = 'jetpack-social-note'; +export const SOCIAL_NOTES_CONFIG_KEY = 'jetpack_social_notes_config'; +export const SHOW_PRICING_PAGE_KEY = 'jetpack-social_show_pricing_page'; diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/index.ts b/projects/js-packages/publicize-components/src/social-store/selectors/index.ts index b7d1c985dda21..d8ea4bd197aeb 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/index.ts +++ b/projects/js-packages/publicize-components/src/social-store/selectors/index.ts @@ -1,8 +1,10 @@ import { store as coreStore } from '@wordpress/core-data'; import { createRegistrySelector } from '@wordpress/data'; import * as connectionDataSelectors from './connection-data'; +import * as pricingPageSelectors from './pricing-page'; import * as shareStatusSelectors from './share-status'; import * as sigSelectors from './social-image-generator'; +import * as socialNoteSelectors from './social-notes'; import * as socialPluginSelectors from './social-plugin-settings'; import * as utmSelectors from './utm-settings'; @@ -21,6 +23,8 @@ const selectors = { isSavingSiteSettings, ...sigSelectors, ...utmSelectors, + ...socialNoteSelectors, + ...pricingPageSelectors, ...socialPluginSelectors, }; diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts b/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts new file mode 100644 index 0000000000000..eb3cf0991296c --- /dev/null +++ b/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts @@ -0,0 +1,20 @@ +import { store as coreStore } from '@wordpress/core-data'; +import { createRegistrySelector } from '@wordpress/data'; +import { getSocialScriptData } from '../../utils'; +import { SHOW_PRICING_PAGE_KEY } from '../constants'; + +/** + * Returns the UTM state. + */ +export const getShouldShowPricingPage = createRegistrySelector( select => () => { + // @ts-expect-error TS2339 - https://github.com/WordPress/gutenberg/issues/67847 + const { getSite } = select( coreStore ); + + const settings = getSite( undefined, { _fields: SHOW_PRICING_PAGE_KEY } ) as boolean; + + // If the settings are not available in the store yet, use the default settings. + return ( + settings?.[ SHOW_PRICING_PAGE_KEY ] ?? + getSocialScriptData().settings.socialPlugin.show_pricing_page + ); +} ) as ( state: object ) => boolean; diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts new file mode 100644 index 0000000000000..b024a60fb6f41 --- /dev/null +++ b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts @@ -0,0 +1,41 @@ +import { store as coreStore } from '@wordpress/core-data'; +import { createRegistrySelector } from '@wordpress/data'; +import { getSocialScriptData } from '../../utils'; +import { SOCIAL_NOTES_CONFIG_KEY, SOCIAL_NOTES_ENABLED_KEY } from '../constants'; +import { SocialNotesConfig } from '../types'; + +/** + * Returns if Social Notes are enabled for the current site. + */ +export const getSocialNotesEnabled = createRegistrySelector( select => () => { + // @ts-expect-error TS2339 - https://github.com/WordPress/gutenberg/issues/67847 + const { getSite } = select( coreStore ); + + const settings = getSite( undefined, { + _fields: SOCIAL_NOTES_ENABLED_KEY, + } ) as boolean; + + // If the settings are not available in the store yet, use the default settings. + return ( + settings?.[ SOCIAL_NOTES_ENABLED_KEY ] ?? + getSocialScriptData().settings.socialPlugin.social_notes_enabled + ); +} ) as ( state: object ) => boolean; + +/** + * Returns the Social Notes Config for the current site. + */ +export const getSocialNotesConfig = createRegistrySelector( select => () => { + // @ts-expect-error TS2339 - https://github.com/WordPress/gutenberg/issues/67847 + const { getSite } = select( coreStore ); + + const settings = getSite( undefined, { + _fields: SOCIAL_NOTES_CONFIG_KEY, + } ) as SocialNotesConfig; + + // If the settings are not available in the store yet, use the default settings. + return ( + settings?.[ SOCIAL_NOTES_CONFIG_KEY ] ?? + getSocialScriptData().settings.socialPlugin.social_notes_config + ); +} ) as ( state: object ) => SocialNotesConfig; diff --git a/projects/js-packages/publicize-components/src/social-store/types.ts b/projects/js-packages/publicize-components/src/social-store/types.ts index fb5701967342a..af86c74a7a9e0 100644 --- a/projects/js-packages/publicize-components/src/social-store/types.ts +++ b/projects/js-packages/publicize-components/src/social-store/types.ts @@ -96,6 +96,11 @@ export type UtmSettingsConfig = { enabled: boolean; }; +export type SocialNotesConfig = { + append_link: boolean; + link_format: 'full_url' | 'shortlink' | 'permashortcitation'; +}; + export type SocialPluginSettings = { publicize_active: boolean; show_pricing_page: boolean; diff --git a/projects/plugins/social/src/js/components/admin-page/index.jsx b/projects/plugins/social/src/js/components/admin-page/index.jsx index 931f5eb6c8e77..c8c8538bf86bc 100644 --- a/projects/plugins/social/src/js/components/admin-page/index.jsx +++ b/projects/plugins/social/src/js/components/admin-page/index.jsx @@ -42,7 +42,7 @@ const Admin = () => { return { isModuleEnabled: settings.publicize_active, - showPricingPage: settings.show_pricing_page, + showPricingPage: store.getShouldShowPricingPage(), isUpdatingJetpackSettings: store.isSavingSocialPluginSettings(), }; } ); diff --git a/projects/plugins/social/src/js/components/pricing-page/index.js b/projects/plugins/social/src/js/components/pricing-page/index.js index 4a42142867b0d..4fad68da8843d 100644 --- a/projects/plugins/social/src/js/components/pricing-page/index.js +++ b/projects/plugins/social/src/js/components/pricing-page/index.js @@ -23,14 +23,14 @@ const PricingPage = ( { onDismiss = () => {} } = {} ) => { const blogID = getScriptData().site.wpcom.blog_id; const siteSuffix = getScriptData().site.suffix; - const { updateSocialPluginSettings } = useDispatch( socialStore ); + const { setShowPricingPage } = useDispatch( socialStore ); const [ isLarge ] = useBreakpointMatch( 'lg' ); const hidePricingPage = useCallback( () => { - updateSocialPluginSettings( { show_pricing_page: false } ); + setShowPricingPage( false ); onDismiss(); - }, [ updateSocialPluginSettings, onDismiss ] ); + }, [ setShowPricingPage, onDismiss ] ); return ( = ( { disabled } ) => { const { isEnabled, notesConfig, isUpdating } = useSelect( select => { const store = select( socialStore ); - const settings = store.getSocialPluginSettings(); return { - isEnabled: settings.social_notes_enabled, - notesConfig: settings.social_notes_config, - isUpdating: store.isSavingSocialPluginSettings(), + isEnabled: store.getSocialNotesEnabled(), + notesConfig: store.getSocialNotesConfig(), + isUpdating: store.isSavingSiteSettings(), }; }, [] ); @@ -49,46 +48,38 @@ const SocialNotesToggle: React.FC< SocialNotesToggleProps > = ( { disabled } ) = const [ isSmall ] = useBreakpointMatch( 'sm' ); - const { updateSocialPluginSettings } = useDispatch( socialStore ); + const { setSocialNotesEnabled, updateSocialNotesConfig } = useDispatch( socialStore ); const toggleStatus = useCallback( async () => { - handleStateUpdating( () => - updateSocialPluginSettings( { - social_notes_enabled: ! isEnabled, - } ) - ); - }, [ isEnabled, updateSocialPluginSettings ] ); + handleStateUpdating( () => setSocialNotesEnabled( ! isEnabled ) ); + }, [ isEnabled, setSocialNotesEnabled ] ); const onToggleAppendLink = useCallback( ( append_link: boolean ) => { handleStateUpdating( () => - updateSocialPluginSettings( { - social_notes_config: { - ...notesConfig, - append_link, - }, + updateSocialNotesConfig( { + ...notesConfig, + append_link, } ), setIsAppendLinkToggleUpdating ); }, - [ notesConfig, updateSocialPluginSettings ] + [ notesConfig, updateSocialNotesConfig ] ); const onChangeLinkFormat = useCallback( ( link_format: string ) => { handleStateUpdating( () => - updateSocialPluginSettings( { - social_notes_config: { - ...notesConfig, - link_format: link_format as ( typeof notesConfig )[ 'link_format' ], - }, + updateSocialNotesConfig( { + ...notesConfig, + link_format: link_format as ( typeof notesConfig )[ 'link_format' ], } ), setIsLinkFormatUpdating ); }, - [ notesConfig, updateSocialPluginSettings ] + [ notesConfig, updateSocialNotesConfig ] ); const appendLink = notesConfig.append_link ?? true; @@ -122,23 +113,23 @@ const SocialNotesToggle: React.FC< SocialNotesToggleProps > = ( { disabled } ) = { __( 'Create a note', 'jetpack-social' ) } - { isEnabled && ! isUpdating ? ( + { isEnabled ? (
- { appendLink && ! isAppendLinkToggleUpdating ? ( + { appendLink ? ( Date: Fri, 17 Jan 2025 12:53:40 +0100 Subject: [PATCH 03/11] changelog --- .../changelog/refactor-social-note-settings-core | 4 ++++ .../publicize/changelog/refactor-social-note-settings-core | 4 ++++ .../social/changelog/refactor-social-note-settings-core | 4 ++++ 3 files changed, 12 insertions(+) create mode 100644 projects/js-packages/publicize-components/changelog/refactor-social-note-settings-core create mode 100644 projects/packages/publicize/changelog/refactor-social-note-settings-core create mode 100644 projects/plugins/social/changelog/refactor-social-note-settings-core diff --git a/projects/js-packages/publicize-components/changelog/refactor-social-note-settings-core b/projects/js-packages/publicize-components/changelog/refactor-social-note-settings-core new file mode 100644 index 0000000000000..cb6e8105e2e53 --- /dev/null +++ b/projects/js-packages/publicize-components/changelog/refactor-social-note-settings-core @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Refactored Social Note settings to use core diff --git a/projects/packages/publicize/changelog/refactor-social-note-settings-core b/projects/packages/publicize/changelog/refactor-social-note-settings-core new file mode 100644 index 0000000000000..cb6e8105e2e53 --- /dev/null +++ b/projects/packages/publicize/changelog/refactor-social-note-settings-core @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Refactored Social Note settings to use core diff --git a/projects/plugins/social/changelog/refactor-social-note-settings-core b/projects/plugins/social/changelog/refactor-social-note-settings-core new file mode 100644 index 0000000000000..cb6e8105e2e53 --- /dev/null +++ b/projects/plugins/social/changelog/refactor-social-note-settings-core @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Refactored Social Note settings to use core From 493109387aa01c506334bcd6e44187d617cc6840 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Fri, 17 Jan 2025 13:04:17 +0100 Subject: [PATCH 04/11] Remove deprecated code --- projects/plugins/social/src/class-note.php | 45 --------------- .../src/class-rest-settings-controller.php | 55 +------------------ .../tests/php/test-class-jetpack-social.php | 20 ------- 3 files changed, 1 insertion(+), 119 deletions(-) diff --git a/projects/plugins/social/src/class-note.php b/projects/plugins/social/src/class-note.php index b03256fd62a83..c4a3e5c98bd44 100644 --- a/projects/plugins/social/src/class-note.php +++ b/projects/plugins/social/src/class-note.php @@ -189,51 +189,6 @@ public function maybe_flush_rewrite_rules( $force = false ) { } } - /** - * Set whether or not the Notes feature is enabled. - * - * @param boolean $enabled Whether or not the Notes feature is enabled. - */ - public function set_enabled( $enabled ) { - if ( $enabled === self::enabled() ) { - return; - } - - if ( $enabled ) { - update_option( self::JETPACK_SOCIAL_NOTE_CPT, true ); - } else { - delete_option( self::JETPACK_SOCIAL_NOTE_CPT ); - } - // Delete this option, so the rules get flushe in maybe_flush_rewrite_rules when the CPT is registered. - delete_option( self::FLUSH_REWRITE_RULES_FLUSHED ); - } - - /** - * Get the social notes config. - * - * @return array The social notes config. - */ - public function get_config() { - return get_option( - self::JETPACK_SOCIAL_NOTES_CONFIG, - // Append link by default. - array( - 'append_link' => true, - ) - ); - } - - /** - * Update social notes config - * - * @param array $config The config to update. - */ - public function update_config( $config ) { - $old_config = get_option( self::JETPACK_SOCIAL_NOTES_CONFIG, array() ); - $new_config = array_merge( $old_config, $config ); - update_option( self::JETPACK_SOCIAL_NOTES_CONFIG, $new_config ); - } - /** * Use the_title hook so we show the social note's exceprt in the post list view. * diff --git a/projects/plugins/social/src/class-rest-settings-controller.php b/projects/plugins/social/src/class-rest-settings-controller.php index c95918a08a269..9a265d9bcd8a3 100644 --- a/projects/plugins/social/src/class-rest-settings-controller.php +++ b/projects/plugins/social/src/class-rest-settings-controller.php @@ -106,20 +106,6 @@ public function get_item( $request ) { $data['publicize_active'] = Jetpack_Social::is_publicize_active(); } - if ( rest_is_field_included( 'show_pricing_page', $fields ) ) { - $data['show_pricing_page'] = Jetpack_Social::should_show_pricing_page(); - } - - $note = new Note(); - - if ( rest_is_field_included( 'social_notes_enabled', $fields ) ) { - $data['social_notes_enabled'] = $note->enabled(); - } - - if ( rest_is_field_included( 'social_notes_config', $fields ) ) { - $data['social_notes_config'] = $note->get_config(); - } - return $this->prepare_item_for_response( $data, $request ); } @@ -132,8 +118,6 @@ public function update_item( $request ) { $params = $request->get_params(); $settings = $this->get_endpoint_args_for_item_schema( $request->get_method() ); - $note = new Note(); - foreach ( array_keys( $settings ) as $name ) { if ( ! array_key_exists( $name, $params ) ) { continue; @@ -146,15 +130,6 @@ public function update_item( $request ) { return $updated; } break; - case 'show_pricing_page': - update_option( Jetpack_Social::JETPACK_SOCIAL_SHOW_PRICING_PAGE_OPTION, (int) $params[ $name ] ); - break; - case 'social_notes_enabled': - $note->set_enabled( (bool) $params[ $name ] ); - break; - case 'social_notes_config': - $note->update_config( $params[ $name ] ); - break; } } @@ -222,39 +197,11 @@ public function get_item_schema() { 'title' => 'system_status', 'type' => 'object', 'properties' => array( - 'publicize_active' => array( + 'publicize_active' => array( 'description' => __( 'Is the publicize module enabled?', 'jetpack-social' ), 'type' => 'boolean', 'context' => array( 'view', 'edit' ), ), - 'show_pricing_page' => array( - 'description' => __( 'Should we show the pricing page?', 'jetpack-social' ), - 'type' => 'boolean', - 'context' => array( 'view', 'edit' ), - ), - 'social_notes_enabled' => array( - 'description' => __( 'Is the social notes feature enabled?', 'jetpack-social' ), - 'type' => 'boolean', - 'context' => array( 'view', 'edit' ), - ), - 'social_notes_config' => array( - 'description' => __( 'The social notes configuration', 'jetpack-social' ), - 'type' => 'object', - 'context' => array( 'view', 'edit' ), - 'properties' => array( - 'append_link' => array( - 'description' => __( 'Whether to append the post link when sharing the note.', 'jetpack-social' ), - 'type' => 'boolean', - 'context' => array( 'view', 'edit' ), - ), - 'link_format' => array( - 'description' => __( 'Link format', 'jetpack-social' ), - 'type' => 'string', - 'enum' => array( 'full_url', 'shortlink', 'permashortcitation' ), - 'context' => array( 'view', 'edit' ), - ), - ), - ), ), ); return $this->add_additional_fields_schema( $schema ); diff --git a/projects/plugins/social/tests/php/test-class-jetpack-social.php b/projects/plugins/social/tests/php/test-class-jetpack-social.php index e43efb68a8388..e3fa14083c4f5 100644 --- a/projects/plugins/social/tests/php/test-class-jetpack-social.php +++ b/projects/plugins/social/tests/php/test-class-jetpack-social.php @@ -6,7 +6,6 @@ */ use Automattic\Jetpack\Connection\Manager as Connection_Manager; -use Automattic\Jetpack\Social\Note; use WorDBless\BaseTestCase; /** @@ -72,23 +71,4 @@ public function test_publicize_not_configured() { $this->assertSame( 0, did_action( 'jetpack_feature_publicize_enabled' ) ); } - - /** - * Test the social notes feature. - */ - public function test_social_notes() { - $note = new Note(); - $note->init(); - $this->assertEmpty( get_option( Note::FLUSH_REWRITE_RULES_FLUSHED ) ); - update_option( Note::JETPACK_SOCIAL_NOTE_CPT, true ); - $note->init(); - $this->assertTrue( get_option( Note::FLUSH_REWRITE_RULES_FLUSHED ) ); - $note->set_enabled( false ); - $this->assertFalse( $note->enabled() ); - $note->init(); - $this->assertEmpty( get_option( Note::FLUSH_REWRITE_RULES_FLUSHED ) ); - $note->set_enabled( true ); - $note->init(); - $this->assertTrue( get_option( Note::FLUSH_REWRITE_RULES_FLUSHED ) ); - } } From 6377ca1b1752775e385783e28397e237e514f12f Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Fri, 17 Jan 2025 13:06:07 +0100 Subject: [PATCH 05/11] Fix some tests --- .../src/social-store/selectors/pricing-page.ts | 2 +- .../src/social-store/selectors/social-notes.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts b/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts index eb3cf0991296c..3f867e8b65103 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts +++ b/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts @@ -15,6 +15,6 @@ export const getShouldShowPricingPage = createRegistrySelector( select => () => // If the settings are not available in the store yet, use the default settings. return ( settings?.[ SHOW_PRICING_PAGE_KEY ] ?? - getSocialScriptData().settings.socialPlugin.show_pricing_page + getSocialScriptData().settings?.socialPlugin?.show_pricing_page ); } ) as ( state: object ) => boolean; diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts index b024a60fb6f41..08b79ca3f3806 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts +++ b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts @@ -18,7 +18,7 @@ export const getSocialNotesEnabled = createRegistrySelector( select => () => { // If the settings are not available in the store yet, use the default settings. return ( settings?.[ SOCIAL_NOTES_ENABLED_KEY ] ?? - getSocialScriptData().settings.socialPlugin.social_notes_enabled + getSocialScriptData().settings?.socialPlugin?.social_notes_enabled ); } ) as ( state: object ) => boolean; @@ -36,6 +36,6 @@ export const getSocialNotesConfig = createRegistrySelector( select => () => { // If the settings are not available in the store yet, use the default settings. return ( settings?.[ SOCIAL_NOTES_CONFIG_KEY ] ?? - getSocialScriptData().settings.socialPlugin.social_notes_config + getSocialScriptData().settings?.socialPlugin?.social_notes_config ); } ) as ( state: object ) => SocialNotesConfig; From 53e5624f59fa00e8caf2693a054824d63e6189df Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Fri, 17 Jan 2025 16:32:20 +0100 Subject: [PATCH 06/11] Fix constant issue --- .../src/jetpack-social-settings/class-settings.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/projects/packages/publicize/src/jetpack-social-settings/class-settings.php b/projects/packages/publicize/src/jetpack-social-settings/class-settings.php index b9008b3a9de3c..a870fb88b9615 100644 --- a/projects/packages/publicize/src/jetpack-social-settings/class-settings.php +++ b/projects/packages/publicize/src/jetpack-social-settings/class-settings.php @@ -11,7 +11,6 @@ use Automattic\Jetpack\Modules; use Automattic\Jetpack\Publicize\Publicize_Script_Data; use Automattic\Jetpack\Publicize\Social_Image_Generator\Templates; -use Automattic\Jetpack\Social\Note; /** * This class is used to get and update Jetpack_Social_Settings. @@ -47,8 +46,9 @@ class Settings { ); // Legacy named options. - const JETPACK_SOCIAL_NOTE_CPT_ENABLED = 'jetpack-social-note'; - const JETPACK_SOCIAL_SHOW_PRICING_PAGE = 'jetpack-social_show_pricing_page'; + const JETPACK_SOCIAL_NOTE_CPT_ENABLED = 'jetpack-social-note'; + const JETPACK_SOCIAL_SHOW_PRICING_PAGE = 'jetpack-social_show_pricing_page'; + const NOTES_FLUSH_REWRITE_RULES_FLUSHED = 'jetpack_social_rewrite_rules_flushed'; /** * Feature flags. Each item has 3 keys because of the naming conventions: @@ -355,7 +355,7 @@ public function update_settings( $updated, $name, $value ) { // Social Notes. if ( self::JETPACK_SOCIAL_NOTE_CPT_ENABLED === $name ) { // Delete this option, so the rules get flushed in maybe_flush_rewrite_rules when the CPT is registered. - delete_option( Note::FLUSH_REWRITE_RULES_FLUSHED ); + delete_option( self::NOTES_FLUSH_REWRITE_RULES_FLUSHED ); return update_option( self::JETPACK_SOCIAL_NOTE_CPT_ENABLED, (bool) $value ); } if ( self::OPTION_PREFIX . self::NOTES_CONFIG === $name ) { From e1cf045decf07724283f3ed081a1916d2fc69c55 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Fri, 17 Jan 2025 17:22:39 +0100 Subject: [PATCH 07/11] Cast pricing_page option to int as before --- .../publicize/src/jetpack-social-settings/class-settings.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/projects/packages/publicize/src/jetpack-social-settings/class-settings.php b/projects/packages/publicize/src/jetpack-social-settings/class-settings.php index a870fb88b9615..4354e06ed08d4 100644 --- a/projects/packages/publicize/src/jetpack-social-settings/class-settings.php +++ b/projects/packages/publicize/src/jetpack-social-settings/class-settings.php @@ -364,6 +364,10 @@ public function update_settings( $updated, $name, $value ) { return update_option( self::OPTION_PREFIX . self::NOTES_CONFIG, $new_config ); } + if ( self::JETPACK_SOCIAL_SHOW_PRICING_PAGE === $name ) { + return update_option( self::JETPACK_SOCIAL_SHOW_PRICING_PAGE, (int) $value ); + } + return $updated; } From 4532ac838123ec87b60f63b70edd7fa06e7f8101 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Fri, 17 Jan 2025 17:23:48 +0100 Subject: [PATCH 08/11] Remove boolean castings --- .../src/social-store/selectors/pricing-page.ts | 2 +- .../src/social-store/selectors/social-notes.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts b/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts index 3f867e8b65103..ac531f2ebb98b 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts +++ b/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts @@ -17,4 +17,4 @@ export const getShouldShowPricingPage = createRegistrySelector( select => () => settings?.[ SHOW_PRICING_PAGE_KEY ] ?? getSocialScriptData().settings?.socialPlugin?.show_pricing_page ); -} ) as ( state: object ) => boolean; +} ); diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts index 08b79ca3f3806..0413d3e0b3707 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts +++ b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts @@ -20,7 +20,7 @@ export const getSocialNotesEnabled = createRegistrySelector( select => () => { settings?.[ SOCIAL_NOTES_ENABLED_KEY ] ?? getSocialScriptData().settings?.socialPlugin?.social_notes_enabled ); -} ) as ( state: object ) => boolean; +} ); /** * Returns the Social Notes Config for the current site. From 43c3cf447ea40fc070cb5fdb97f46542382e6d13 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Mon, 20 Jan 2025 12:35:57 +0100 Subject: [PATCH 09/11] Remove casting --- .../src/social-store/selectors/social-notes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts index 0413d3e0b3707..dc560d7edd518 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts +++ b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts @@ -38,4 +38,4 @@ export const getSocialNotesConfig = createRegistrySelector( select => () => { settings?.[ SOCIAL_NOTES_CONFIG_KEY ] ?? getSocialScriptData().settings?.socialPlugin?.social_notes_config ); -} ) as ( state: object ) => SocialNotesConfig; +} ); From 11a62d022fa0dc6cebc57596cc1a1b12cb4831eb Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Tue, 21 Jan 2025 09:49:21 +0100 Subject: [PATCH 10/11] Fix TS errors --- .../src/social-store/selectors/pricing-page.ts | 3 +-- .../src/social-store/selectors/social-notes.ts | 8 ++------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts b/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts index ac531f2ebb98b..c387b6f5a4059 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts +++ b/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts @@ -7,10 +7,9 @@ import { SHOW_PRICING_PAGE_KEY } from '../constants'; * Returns the UTM state. */ export const getShouldShowPricingPage = createRegistrySelector( select => () => { - // @ts-expect-error TS2339 - https://github.com/WordPress/gutenberg/issues/67847 const { getSite } = select( coreStore ); - const settings = getSite( undefined, { _fields: SHOW_PRICING_PAGE_KEY } ) as boolean; + const settings = getSite( undefined, { _fields: SHOW_PRICING_PAGE_KEY } ); // If the settings are not available in the store yet, use the default settings. return ( diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts index dc560d7edd518..1256f69a32a24 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts +++ b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts @@ -2,19 +2,16 @@ import { store as coreStore } from '@wordpress/core-data'; import { createRegistrySelector } from '@wordpress/data'; import { getSocialScriptData } from '../../utils'; import { SOCIAL_NOTES_CONFIG_KEY, SOCIAL_NOTES_ENABLED_KEY } from '../constants'; -import { SocialNotesConfig } from '../types'; /** * Returns if Social Notes are enabled for the current site. */ export const getSocialNotesEnabled = createRegistrySelector( select => () => { - // @ts-expect-error TS2339 - https://github.com/WordPress/gutenberg/issues/67847 const { getSite } = select( coreStore ); const settings = getSite( undefined, { _fields: SOCIAL_NOTES_ENABLED_KEY, - } ) as boolean; - + } ); // If the settings are not available in the store yet, use the default settings. return ( settings?.[ SOCIAL_NOTES_ENABLED_KEY ] ?? @@ -26,12 +23,11 @@ export const getSocialNotesEnabled = createRegistrySelector( select => () => { * Returns the Social Notes Config for the current site. */ export const getSocialNotesConfig = createRegistrySelector( select => () => { - // @ts-expect-error TS2339 - https://github.com/WordPress/gutenberg/issues/67847 const { getSite } = select( coreStore ); const settings = getSite( undefined, { _fields: SOCIAL_NOTES_CONFIG_KEY, - } ) as SocialNotesConfig; + } ); // If the settings are not available in the store yet, use the default settings. return ( From 44f8c61e888886d3ae649b33a9c2fd9546d4b5e5 Mon Sep 17 00:00:00 2001 From: Gergely Juhasz Date: Tue, 21 Jan 2025 11:26:03 +0100 Subject: [PATCH 11/11] Address comments on function names --- .../src/social-store/actions/social-notes.ts | 2 +- .../src/social-store/selectors/pricing-page.ts | 4 ++-- .../src/social-store/selectors/social-notes.ts | 2 +- .../publicize-components/src/social-store/types.ts | 3 +++ .../plugins/social/src/js/components/admin-page/index.jsx | 2 +- .../src/js/components/social-notes-toggle/index.tsx | 8 ++++---- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/projects/js-packages/publicize-components/src/social-store/actions/social-notes.ts b/projects/js-packages/publicize-components/src/social-store/actions/social-notes.ts index 511a4556bebbb..b21fcdb660bd9 100644 --- a/projects/js-packages/publicize-components/src/social-store/actions/social-notes.ts +++ b/projects/js-packages/publicize-components/src/social-store/actions/social-notes.ts @@ -9,7 +9,7 @@ import { SocialNotesConfig } from '../types'; * * @return {Function} A thunk. */ -export function setSocialNotesEnabled( isEnabled: boolean ) { +export function toggleSocialNotes( isEnabled: boolean ) { return async function ( { registry } ) { const { saveSite } = registry.dispatch( coreStore ); diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts b/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts index c387b6f5a4059..6eb876bd8a649 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts +++ b/projects/js-packages/publicize-components/src/social-store/selectors/pricing-page.ts @@ -4,9 +4,9 @@ import { getSocialScriptData } from '../../utils'; import { SHOW_PRICING_PAGE_KEY } from '../constants'; /** - * Returns the UTM state. + * Returns the Show Pricing Page enabled status for the current site. */ -export const getShouldShowPricingPage = createRegistrySelector( select => () => { +export const shouldShowPricingPage = createRegistrySelector( select => () => { const { getSite } = select( coreStore ); const settings = getSite( undefined, { _fields: SHOW_PRICING_PAGE_KEY } ); diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts index 1256f69a32a24..c7efb4aebd6ec 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts +++ b/projects/js-packages/publicize-components/src/social-store/selectors/social-notes.ts @@ -6,7 +6,7 @@ import { SOCIAL_NOTES_CONFIG_KEY, SOCIAL_NOTES_ENABLED_KEY } from '../constants' /** * Returns if Social Notes are enabled for the current site. */ -export const getSocialNotesEnabled = createRegistrySelector( select => () => { +export const isSocialNotesEnabled = createRegistrySelector( select => () => { const { getSite } = select( coreStore ); const settings = getSite( undefined, { diff --git a/projects/js-packages/publicize-components/src/social-store/types.ts b/projects/js-packages/publicize-components/src/social-store/types.ts index 7e434b2e75883..22c8aff406487 100644 --- a/projects/js-packages/publicize-components/src/social-store/types.ts +++ b/projects/js-packages/publicize-components/src/social-store/types.ts @@ -150,4 +150,7 @@ export type SocialPluginSettings = { export type SocialSettingsFields = { jetpack_social_image_generator_settings: SocialImageGeneratorConfig; jetpack_social_utm_settings: UtmSettingsConfig; + [ 'jetpack-social-note' ]: boolean; + jetpack_social_notes_config: SocialNotesConfig; + [ 'jetpack-social_show_pricing_page' ]: boolean; }; diff --git a/projects/plugins/social/src/js/components/admin-page/index.jsx b/projects/plugins/social/src/js/components/admin-page/index.jsx index c8c8538bf86bc..0c074c6e874bf 100644 --- a/projects/plugins/social/src/js/components/admin-page/index.jsx +++ b/projects/plugins/social/src/js/components/admin-page/index.jsx @@ -42,7 +42,7 @@ const Admin = () => { return { isModuleEnabled: settings.publicize_active, - showPricingPage: store.getShouldShowPricingPage(), + showPricingPage: store.shouldShowPricingPage(), isUpdatingJetpackSettings: store.isSavingSocialPluginSettings(), }; } ); diff --git a/projects/plugins/social/src/js/components/social-notes-toggle/index.tsx b/projects/plugins/social/src/js/components/social-notes-toggle/index.tsx index 14d7b190592f3..258476f3b2802 100644 --- a/projects/plugins/social/src/js/components/social-notes-toggle/index.tsx +++ b/projects/plugins/social/src/js/components/social-notes-toggle/index.tsx @@ -35,7 +35,7 @@ const SocialNotesToggle: React.FC< SocialNotesToggleProps > = ( { disabled } ) = const store = select( socialStore ); return { - isEnabled: store.getSocialNotesEnabled(), + isEnabled: store.isSocialNotesEnabled(), notesConfig: store.getSocialNotesConfig(), isUpdating: store.isSavingSiteSettings(), }; @@ -48,11 +48,11 @@ const SocialNotesToggle: React.FC< SocialNotesToggleProps > = ( { disabled } ) = const [ isSmall ] = useBreakpointMatch( 'sm' ); - const { setSocialNotesEnabled, updateSocialNotesConfig } = useDispatch( socialStore ); + const { toggleSocialNotes, updateSocialNotesConfig } = useDispatch( socialStore ); const toggleStatus = useCallback( async () => { - handleStateUpdating( () => setSocialNotesEnabled( ! isEnabled ) ); - }, [ isEnabled, setSocialNotesEnabled ] ); + handleStateUpdating( () => toggleSocialNotes( ! isEnabled ) ); + }, [ isEnabled, toggleSocialNotes ] ); const onToggleAppendLink = useCallback( ( append_link: boolean ) => {