Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/paid-memberships-pro/blocks/src/sidebar/index.js
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
/**
2
+
* Require Membership sidebar panel.
3
+
*/
4
+
5
+
import apiFetch from '@wordpress/api-fetch';
6
+
import { register } from '@wordpress/data';
7
+
8
+
function pmproCustomStore() {
9
+
return {
10
+
name: 'pmpro/require-membership',
11
+
instantiate: () => {
12
+
const listeners = new Set();
13
+
const storeData = { restrictedLevels: [] };
14
+
15
+
function storeChanged() {
16
+
for ( const listener of listeners ) {
17
+
listener();
18
+
}
19
+
}
20
+
21
+
function subscribe( listener ) {
22
+
listeners.add( listener );
23
+
return () => listeners.delete( listener );
24
+
}
25
+
26
+
const selectors = {
27
+
getRestrictedLevels() {
28
+
return storeData['restrictedLevels'];
29
+
},
30
+
};
31
+
32
+
const actions = {
33
+
setRestrictedLevels( restrictedLevels ) {
34
+
storeData['restrictedLevels'] = restrictedLevels;
35
+
storeChanged();
36
+
},fetchRestrictedLevels() {
37
+
apiFetch( { path: 'pmpro/v1/post_restrictions/?post_id=' + pmpro_block_editor_sidebar.post_id } )
38
+
.then( ( data ) => {
39
+
// Set the restricted levels to the membership_id values.
40
+
actions.setRestrictedLevels( data.map( ( item ) => item.membership_id ) );
41
+
storeChanged();
42
+
} )
43
+
.catch( ( error ) => {
44
+
console.error( error );
45
+
} );
46
+
},saveRestrictedLevels() {
47
+
apiFetch( {
48
+
path: 'pmpro/v1/post_restrictions/',
49
+
method: 'POST',
50
+
data: {
51
+
post_id: pmpro_block_editor_sidebar.post_id,
52
+
level_ids: storeData['restrictedLevels'],
53
+
},
54
+
} );
55
+
}
56
+
};
57
+
actions.fetchRestrictedLevels();
58
+
59
+
return {
60
+
getSelectors: () => selectors,
61
+
getActions: () => actions,
62
+
subscribe,
63
+
};
64
+
},
65
+
};
66
+
}
67
+
register( pmproCustomStore() );
68
+
69
+
( function ( wp ) {
70
+
const { __ } = wp.i18n;
71
+
const { registerPlugin } = wp.plugins;
72
+
const { PluginDocumentSettingPanel } = wp.editPost;
73
+
const { Component } = wp.element;
74
+
const { Spinner, CheckboxControl } = wp.components;
75
+
76
+
const { withSelect, withDispatch, dispatch } = wp.data;
77
+
const { compose } = wp.compose;
78
+
79
+
const RequireMembershipControl = compose(
80
+
withDispatch( function ( dispatch, props ) {
81
+
return {
82
+
setRestrictedLevelsValue: function ( value ) {
83
+
dispatch( 'pmpro/require-membership' ).setRestrictedLevels( value );
84
+
// Add another action to update a fake meta value to force the save button to enable.
85
+
dispatch( 'core/editor' ).editPost( { meta: { pmpro_force_save_enable: '1' } } );
86
+
},
87
+
};
88
+
} ),
89
+
withSelect( function ( select, props ) {
90
+
return {
91
+
restrictedLevels: select( 'pmpro/require-membership' ).getRestrictedLevels(),
92
+
};
93
+
} )
94
+
)( function ( props ) {
95
+
const level_checkboxes = props.levels.map(
96
+
( level ) => {
97
+
return (
98
+
<CheckboxControl
99
+
key={ level.id }
100
+
label={ level.name }
101
+
checked={ props.restrictedLevels.includes( level.id ) }
102
+
onChange={ () => {
103
+
let newValue = [...props.restrictedLevels];
104
+
if ( newValue.includes( level.id ) ) {
105
+
newValue = newValue.filter(
106
+
( item ) => item !== level.id
107
+
);
108
+
} else {
109
+
newValue.push( level.id )
110
+
}
111
+
props.setRestrictedLevelsValue( newValue );
112
+
} }
113
+
/>
114
+
)
115
+
}
116
+
);
117
+
return (
118
+
<fragment>
119
+
{
120
+
// Add buttons to select all or none.
121
+
level_checkboxes.length > 1 &&
122
+
<p> { __( 'Select', 'paid-memberships-pro' ) + ': ' }
123
+
<button className="button-link" onClick={ () => {
124
+
props.setRestrictedLevelsValue( props.levels.map( ( level ) => level.id ) );
125
+
} }>{ __( 'All', 'paid-memberships-pro' ) }</button>{ ' | ' }
126
+
<button className="button-link" onClick={ () => {
127
+
props.setRestrictedLevelsValue( [] );
128
+
} }>{__( 'None', 'paid-memberships-pro' ) }</button>
129
+
</p>
130
+
}
131
+
{
132
+
level_checkboxes.length > 6 ? (
133
+
<div className="pmpro-block-inspector-scrollable">
134
+
{ level_checkboxes }
135
+
</div>
136
+
) : (
137
+
level_checkboxes
138
+
)
139
+
}
140
+
</fragment>
141
+
);
142
+
} );
143
+
144
+
// Whenever a post is saved, call the saveRestrictedLevels action.
145
+
// Adapted from here to ensure API is only called once: https://github.com/WordPress/gutenberg/issues/17632#issuecomment-819379829
146
+
/**
147
+
* Consults values to determine whether the editor is busy saving a post.
148
+
* Includes checks on whether the save button is busy.
149
+
*
150
+
* @returns {boolean} Whether the editor is on a busy save state.
151
+
*/
152
+
function isSavingPost() {
153
+
154
+
// State data necessary to establish if a save is occurring.
155
+
const isSaving = wp.data.select('core/editor').isSavingPost() || wp.data.select('core/editor').isAutosavingPost();
156
+
const isSaveable = wp.data.select('core/editor').isEditedPostSaveable();
157
+
const isPostSavingLocked = wp.data.select('core/editor').isPostSavingLocked();
158
+
const hasNonPostEntityChanges = wp.data.select('core/editor').hasNonPostEntityChanges();
159
+
const isAutoSaving = wp.data.select('core/editor').isAutosavingPost();
160
+
const isButtonDisabled = isSaving || !isSaveable || isPostSavingLocked;
161
+
162
+
// Reduces state into checking whether the post is saving and that the save button is disabled.
163
+
const isBusy = !isAutoSaving && isSaving;
164
+
const isNotInteractable = isButtonDisabled && ! hasNonPostEntityChanges;
165
+
166
+
return isBusy && isNotInteractable;
167
+
}
168
+
169
+
// Current saving state. isSavingPost is defined above.
170
+
var wasSaving = isSavingPost();
171
+
wp.data.subscribe( function () {
172
+
// New saving state
173
+
let isSaving = isSavingPost();
174
+
175
+
// It is done saving if it was saving and it no longer is.
176
+
let isDoneSaving = wasSaving && !isSaving;
177
+
178
+
// Update value for next use.
179
+
wasSaving = isSaving;
180
+
if ( isDoneSaving ) {
181
+
dispatch( 'pmpro/require-membership' ).saveRestrictedLevels();
182
+
}
183
+
} );
184
+
185
+
class PMProSidebar extends Component {
186
+
constructor( props ) {
187
+
super( props );
188
+
this.state = {
189
+
levelList: [],
190
+
loadingLevels: true,
191
+
};
192
+
}
193
+
194
+
componentDidMount() {
195
+
this.fetchlevels();
196
+
}
197
+
198
+
fetchlevels() {
199
+
apiFetch( {
200
+
path: 'pmpro/v1/membership_levels',
201
+
} ).then( ( data ) => {
202
+
// If data is an object, convert to associative array
203
+
if (typeof data === 'object') {
204
+
data = Object.keys(data).map(function(key) {
205
+
return data[key];
206
+
});
207
+
}
208
+
this.setState( {
209
+
levelList: data,
210
+
loadingLevels: false,
211
+
} );
212
+
} ).catch( ( error ) => {
213
+
this.setState( {
214
+
levelList: error,
215
+
loadingLevels: false,
216
+
} );
217
+
} );
218
+
}
219
+
220
+
render() {
221
+
var sidebar_content = <Spinner />;
222
+
if ( ! this.state.loadingLevels ) {
223
+
if ( ! Array.isArray( this.state.levelList ) ) {
224
+
sidebar_content = <p>{ __('Error retrieving membership levels.', 'restrict-with-stripe') + ' ' + this.state.levelList }</p>;
225
+
} else if ( this.state.levelList.length === 0 ) {
226
+
sidebar_content = <p>{ __('No levels found. Please create a level to restrict content.', 'paid-memberships-pro') }</p>;
227
+
} else {
228
+
sidebar_content = <div>
229
+
<RequireMembershipControl
230
+
label={ __( 'Membership Levels', 'paid-memberships-pro' ) }
231
+
levels={ this.state.levelList }
232
+
/>
233
+
</div>;
234
+
}
235
+
}
236
+
237
+
return (
238
+
<PluginDocumentSettingPanel name="pmpro-sidebar-panel" title={ __( 'Require Membership', 'paid-memberships-pro' ) } >
239
+
{sidebar_content}
240
+
</PluginDocumentSettingPanel>
241
+
);
242
+
}
243
+
}
244
+
245
+
registerPlugin( 'pmpro-sidebar', {
246
+
icon: 'lock',
247
+
render: PMProSidebar,
248
+
} );
249
+
} )( window.wp );
250
+