2024-08-09 15:44:10 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2024-10-02 14:08:49 +00:00
|
|
|
import { dispatch, resolveSelect, select, useSelect } from '@wordpress/data';
|
2024-08-09 15:44:10 +00:00
|
|
|
import { useContext, useEffect } from '@wordpress/element';
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
// eslint-disable-next-line @woocommerce/dependency-group
|
|
|
|
import { store as coreStore } from '@wordpress/core-data';
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
// eslint-disable-next-line @woocommerce/dependency-group
|
|
|
|
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
// eslint-disable-next-line @woocommerce/dependency-group
|
|
|
|
import { unlock } from '@wordpress/edit-site/build-module/lock-unlock';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { FontFamily, FontFace } from '../../types/font';
|
|
|
|
import { installFontFamilies } from '../utils/fonts';
|
|
|
|
import { FONT_FAMILIES_TO_INSTALL } from '../sidebar/global-styles/font-pairing-variations/constants';
|
|
|
|
import { OptInContext, OPTIN_FLOW_STATUS } from './context';
|
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
type FontFamilies = {
|
|
|
|
custom: Array< FontFamily >;
|
|
|
|
theme: Array< FontFamily >;
|
|
|
|
};
|
|
|
|
|
2024-08-09 15:44:10 +00:00
|
|
|
const { useGlobalSetting } = unlock( blockEditorPrivateApis );
|
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
async function installPatterns() {
|
|
|
|
await apiFetch( {
|
|
|
|
path: '/wc-admin/patterns',
|
|
|
|
method: 'POST',
|
|
|
|
} );
|
2024-08-09 15:44:10 +00:00
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
// @ts-expect-error -- No types for this exist yet.
|
|
|
|
await dispatch( coreStore ).invalidateResolutionForStoreSelector(
|
|
|
|
'getBlockPatterns'
|
|
|
|
);
|
|
|
|
}
|
2024-08-09 15:44:10 +00:00
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
async function installFonts(
|
|
|
|
enabledFontFamilies: FontFamilies
|
|
|
|
): Promise< FontFamilies > {
|
|
|
|
await installFontFamilies();
|
|
|
|
|
|
|
|
const globalStylesId =
|
2024-08-09 15:44:10 +00:00
|
|
|
// @ts-expect-error No types for this exist yet.
|
2024-10-02 14:08:49 +00:00
|
|
|
select( coreStore ).__experimentalGetCurrentGlobalStylesId();
|
2024-08-09 15:44:10 +00:00
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
const installedFontFamilies = ( await resolveSelect(
|
|
|
|
coreStore
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
).getEntityRecords( 'postType', 'wp_font_family', {
|
|
|
|
_embed: true,
|
|
|
|
per_page: -1,
|
|
|
|
} ) ) as Array< {
|
|
|
|
id: number;
|
|
|
|
font_family_settings: FontFamily;
|
|
|
|
_embedded: {
|
|
|
|
font_faces: Array< {
|
|
|
|
font_face_settings: FontFace;
|
|
|
|
} >;
|
|
|
|
};
|
|
|
|
} >;
|
|
|
|
|
|
|
|
const parsedInstalledFontFamilies = ( installedFontFamilies || [] ).map(
|
|
|
|
( fontFamilyPost ) => {
|
|
|
|
return {
|
|
|
|
id: fontFamilyPost.id,
|
|
|
|
...fontFamilyPost.font_family_settings,
|
|
|
|
fontFace:
|
|
|
|
fontFamilyPost?._embedded?.font_faces.map(
|
|
|
|
( face ) => face.font_face_settings
|
|
|
|
) || [],
|
2024-08-09 15:44:10 +00:00
|
|
|
};
|
2024-10-02 14:08:49 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const { custom } = enabledFontFamilies;
|
|
|
|
|
|
|
|
const enabledFontSlugs = [
|
|
|
|
...( custom ? custom.map( ( font ) => font.slug ) : [] ),
|
|
|
|
];
|
|
|
|
|
|
|
|
const fontFamiliesToEnable = parsedInstalledFontFamilies.reduce(
|
|
|
|
( acc, font ) => {
|
|
|
|
if (
|
|
|
|
enabledFontSlugs.includes( font.slug ) ||
|
|
|
|
FONT_FAMILIES_TO_INSTALL[ font.slug ] === undefined
|
|
|
|
) {
|
|
|
|
return acc;
|
2024-08-09 15:44:10 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
return [ ...acc, { ...font } ];
|
|
|
|
},
|
|
|
|
[] as Array< FontFamily >
|
|
|
|
);
|
2024-08-09 15:44:10 +00:00
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
const {
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
|
|
|
|
} = dispatch( coreStore );
|
|
|
|
|
|
|
|
saveSpecifiedEntityEdits( 'root', 'globalStyles', globalStylesId, [
|
|
|
|
'settings.typography.fontFamilies',
|
|
|
|
] );
|
|
|
|
|
|
|
|
return {
|
|
|
|
...enabledFontFamilies,
|
|
|
|
custom: [
|
|
|
|
...( enabledFontFamilies.custom ?? [] ),
|
|
|
|
...( fontFamiliesToEnable ?? [] ),
|
|
|
|
],
|
2024-08-09 15:44:10 +00:00
|
|
|
};
|
2024-10-02 14:08:49 +00:00
|
|
|
}
|
2024-08-09 15:44:10 +00:00
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
export const OptInSubscribe = () => {
|
|
|
|
const { setOptInFlowStatus } = useContext( OptInContext );
|
2024-08-09 15:44:10 +00:00
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
const [ enabledFontFamilies, setFontFamilies ]: [
|
|
|
|
FontFamilies,
|
|
|
|
( font: FontFamilies ) => void
|
|
|
|
] = useGlobalSetting( 'typography.fontFamilies' );
|
2024-08-09 15:44:10 +00:00
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
const isOptedIn = useSelect( ( selectStore ) => {
|
|
|
|
const allowTracking = selectStore( OPTIONS_STORE_NAME ).getOption(
|
|
|
|
'woocommerce_allow_tracking'
|
|
|
|
);
|
|
|
|
return allowTracking === 'yes';
|
|
|
|
}, [] );
|
2024-08-09 15:44:10 +00:00
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
useEffect(
|
|
|
|
function optedInListener() {
|
|
|
|
if ( ! isOptedIn ) return;
|
2024-08-09 15:44:10 +00:00
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
async function installPatternsAndFonts() {
|
|
|
|
await installPatterns();
|
|
|
|
const fontFamilies = await installFonts( enabledFontFamilies );
|
|
|
|
setFontFamilies( fontFamilies );
|
2024-08-09 15:44:10 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 14:08:49 +00:00
|
|
|
setOptInFlowStatus( OPTIN_FLOW_STATUS.LOADING );
|
|
|
|
installPatternsAndFonts().finally( () => {
|
|
|
|
setOptInFlowStatus( OPTIN_FLOW_STATUS.DONE );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
// We don't want to run this effect on every render, only when `woocommerce_allow_tracking` changes.
|
2024-08-09 15:44:10 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-10-02 14:08:49 +00:00
|
|
|
[ isOptedIn ]
|
|
|
|
);
|
2024-08-09 15:44:10 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|