/
index.js
133 lines (118 loc) · 3.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* External dependencies
*/
import { mergeWith } from 'lodash';
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { getBlockType, registerBlockType, unregisterBlockType } from '@wordpress/blocks';
import { _x } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import * as container from './blocks/container';
import * as subscriberView from './blocks/subscriber-view';
import * as loggedOutView from './blocks/logged-out-view';
import * as buttons from './blocks/buttons';
import * as loginButton from './blocks/login-button';
/**
* Function to register an individual block.
*
* @typedef {import('@wordpress/blocks').BlockConfiguration} BlockConfiguration
*
* @typedef {object} Block
* @property {string} name
* @property {string} category
* @property {BlockConfiguration} settings
*
* @param {Block} block The block to be registered.
*/
const registerBlock = ( block ) => {
if ( ! block ) {
return;
}
const { name, category, settings } = block;
registerBlockType( name, {
category,
...settings,
} );
};
/**
* Updates the settings of the given block.
*
* @param name {string} Block name
* @param settings {object} Block settings
*/
const updateBlockType = ( name, settings ) => {
const blockType = getBlockType( name );
if ( ! blockType ) {
return;
}
unregisterBlockType( name );
const updatedSettings = mergeWith( {}, blockType, settings, ( objValue, srcValue ) => {
if ( Array.isArray( objValue ) ) {
return objValue.concat( srcValue );
}
} );
registerBlockType( name, updatedSettings );
};
/**
* Gets the current status of the Memberships module.
*
* @returns {Promise} Memberships status
*/
const getMembershipsStatus = async () => {
try {
return apiFetch( { path: '/wpcom/v2/memberships/status' } );
} catch {
return null;
}
};
/**
* Appends a "paid" tag to the Premium Content block title if site requires an upgrade.
*
* @param membershipsStatus {object} Memberships status
*/
const addPaidBlockFlags = ( membershipsStatus ) => {
if ( ! membershipsStatus.should_upgrade_to_access_memberships ) {
return;
}
const paidFlag = _x(
'paid',
'Short label appearing near a block requiring a paid plan',
'full-site-editing'
);
updateBlockType( container.name, { title: `${ container.settings.title } (${ paidFlag })` } );
};
/**
* Hides the buttons block from the inserter if the Memberships module is not set up.
*
* @param membershipsStatus {object} Memberships status
*/
const hideButtonsIfMembershipsNotSetUp = ( membershipsStatus ) => {
if (
! membershipsStatus.should_upgrade_to_access_memberships &&
membershipsStatus.connected_account_id
) {
return;
}
updateBlockType( buttons.name, { supports: { inserter: false } } );
updateBlockType( loginButton.name, { supports: { inserter: false } } );
};
/**
* Configures the Premium Content blocks.
*/
const configurePremiumContentBlocks = async () => {
const membershipsStatus = await getMembershipsStatus();
addPaidBlockFlags( membershipsStatus );
hideButtonsIfMembershipsNotSetUp( membershipsStatus );
};
/**
* Function to register Premium Content blocks.
*/
export const registerPremiumContentBlocks = () => {
[ container, loggedOutView, subscriberView, buttons, loginButton ].forEach( registerBlock );
};
registerPremiumContentBlocks();
configurePremiumContentBlocks();