Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Social: Refactor Social Note settings to use core #41153

Merged
merged 13 commits into from
Jan 21, 2025
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Refactored Social Note settings to use core
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -9,6 +11,8 @@ const actions = {
...connectionData,
...sigActions,
...utmActions,
...socialNoteSettings,
...pricingPageSettings,
...socialPluginSettings,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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 } );
};
}
Original file line number Diff line number Diff line change
@@ -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 ) {
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
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 } );
};
}
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -21,6 +23,8 @@ const selectors = {
isSavingSiteSettings,
...sigSelectors,
...utmSelectors,
...socialNoteSelectors,
...pricingPageSelectors,
...socialPluginSelectors,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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 => () => {
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
// @ts-expect-error TS2339 - https://github.com/WordPress/gutenberg/issues/67847
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
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
);
} );
Original file line number Diff line number Diff line change
@@ -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 => () => {
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
// @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
);
} );

/**
* 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;
gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export type UtmSettingsConfig = {
enabled: boolean;
};

export type SocialNotesConfig = {
append_link: boolean;
link_format: 'full_url' | 'shortlink' | 'permashortcitation';
};

gmjuhasz marked this conversation as resolved.
Show resolved Hide resolved
export type SocialPluginSettings = {
publicize_active: boolean;
show_pricing_page: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Refactored Social Note settings to use core
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* This class is used to get and update Jetpack_Social_Settings.
* Currently supported features:
* - Social Image Generator
* - UTM Settings
* - Social Notes
*/
class Settings {
/**
Expand All @@ -37,6 +39,17 @@ 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';
const NOTES_FLUSH_REWRITE_RULES_FLUSHED = 'jetpack_social_rewrite_rules_flushed';

/**
* Feature flags. Each item has 3 keys because of the naming conventions:
* - flag_name: The name of the feature flag for the option check.
Expand Down Expand Up @@ -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 );
}

Expand All @@ -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.
*
Expand Down Expand Up @@ -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();

Expand All @@ -267,6 +352,22 @@ 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( 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 ) {
$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 );
}

if ( self::JETPACK_SOCIAL_SHOW_PRICING_PAGE === $name ) {
return update_option( self::JETPACK_SOCIAL_SHOW_PRICING_PAGE, (int) $value );
}

return $updated;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Refactored Social Note settings to use core
12 changes: 5 additions & 7 deletions projects/plugins/social/src/class-jetpack-social.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
);
}
Expand Down Expand Up @@ -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(
Expand All @@ -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' ) ),
),
Expand Down
Loading
Loading