PluginProbe ʕ •ᴥ•ʔ
FAPI Member / 2.2.31
FAPI Member v2.2.31
2.2.33 2.2.32 trunk 1.9.47 2.1.18 2.2.24 2.2.25 2.2.26 2.2.28 2.2.29 2.2.30 2.2.31
fapi-member / multiple-blocks / includes / block-editor / blocks / block-extender / index.js
fapi-member / multiple-blocks / includes / block-editor / blocks / block-extender Last commit date
block.json 1 month ago index.js 1 month ago
index.js
208 lines
1 const { addFilter } = wp.hooks;
2 const { __ } = wp.i18n;
3 const { createHigherOrderComponent } = wp.compose;
4 const { Fragment, useState } = wp.element;
5 import { InspectorControls } from '@wordpress/block-editor';
6 const { PanelBody, CheckboxControl, RadioControl } = wp.components;
7 import apiFetch from '@wordpress/api-fetch';
8
9 const disabledBlocks = [];
10 let sectionAndLevels = [];
11
12 const controller =
13 typeof AbortController === 'undefined' ? undefined : new AbortController();
14
15 apiFetch( {
16 path: '/?rest_route=/fapi/v1/sections-simple',
17 signal: controller?.signal,
18 } )
19 .then( ( posts ) => {
20 sectionAndLevels = posts;
21 } )
22 .catch( ( error ) => {
23 sectionAndLevels = [];
24 console.error( error );
25 // If the browser doesn't support AbortController then the code below will never log.
26 // However, in most cases this should be fine as it can be considered to be a progressive enhancement.
27 if ( error.name === 'AbortError' ) {
28 console.error( 'Request has been aborted' );
29 }
30 } );
31
32 const addFapiSectionAndLevels = ( settings ) => {
33 if ( ! settings.attributes ) {
34 return settings;
35 }
36
37 if ( disabledBlocks.includes( settings.name ) ) {
38 return settings;
39 }
40
41 settings.attributes = {
42 ...settings.attributes,
43 fapiSectionAndLevels: {
44 type: 'string',
45 default: '[]',
46 },
47 hasSectionOrLevel: {
48 type: 'string',
49 default: '',
50 },
51 };
52
53 return settings;
54 };
55
56 addFilter(
57 'blocks.registerBlockType',
58 'fapi-member/fapi-section-and-level-attributes',
59 addFapiSectionAndLevels
60 );
61
62 const withFapiSectionAndLevels = createHigherOrderComponent( ( BlockEdit ) => {
63 return ( props ) => {
64 if ( disabledBlocks.includes( props.name ) ) {
65 return <BlockEdit { ...props } />;
66 }
67
68 if ( ! props.attributes.hasOwnProperty( 'hasSectionOrLevel' ) ) {
69 props.attributes.hasSectionOrLevel = '';
70 }
71
72 if ( ! props.attributes.hasOwnProperty( 'fapiSectionAndLevels' ) ) {
73 props.attributes.fapiSectionAndLevels = '[]';
74 }
75
76 const [ option, setOption ] = useState(
77 props.attributes.hasSectionOrLevel
78 );
79 const [ state, setState ] = useState(
80 JSON.parse( props.attributes.fapiSectionAndLevels )
81 );
82
83 const checkOption = ( sectionOrLevelId, checked, setState ) => {
84 const fapiSectionAndLevels = JSON.parse(
85 props.attributes.fapiSectionAndLevels
86 );
87
88 if ( checked === false ) {
89 const index = fapiSectionAndLevels.indexOf( sectionOrLevelId );
90
91 if ( index > -1 ) {
92 fapiSectionAndLevels.splice( index, 1 );
93 }
94 } else {
95 fapiSectionAndLevels.push( sectionOrLevelId );
96 }
97
98 props.setAttributes( {
99 fapiSectionAndLevels: JSON.stringify( fapiSectionAndLevels ),
100 } );
101
102 setState( fapiSectionAndLevels );
103 };
104
105 return (
106 <Fragment>
107 <BlockEdit { ...props } />
108 <InspectorControls>
109 <PanelBody
110 title={ __( 'FAPI Member', 'fapi-member' ) }
111 initialOpen={ true }
112 >
113 <RadioControl
114 label={ __(
115 'Zobrazit blok pokud návštěvník',
116 'fapi-member'
117 ) }
118 help={ __(
119 'Obsah se zobrazí v případě že člen je/není přiřazený v členské sekci nebo úrovni nebo všem návštěvníkům.',
120 'fapi-member'
121 ) }
122 selected={ option }
123 options={ [
124 {
125 label: __(
126 'je člen sekce/úrovně',
127 'fapi-member'
128 ),
129 value: '1',
130 },
131 {
132 label: __(
133 'není členem sekce/úrovně',
134 'fapi-member'
135 ),
136 value: '0',
137 },
138 {
139 label: __(
140 'zobrazit všem návštěvníkům (vybrané sekce a urovně se ignorují)',
141 'fapi-member'
142 ),
143 value: '',
144 },
145 ] }
146 onChange={ ( value ) => {
147 props.setAttributes( {
148 hasSectionOrLevel: value,
149 } );
150 return setOption( value );
151 } }
152 />
153 { sectionAndLevels.map( ( sectionAndLevel ) => {
154 return (
155 <CheckboxControl
156 key={ sectionAndLevel.id }
157 label={ sectionAndLevel.name }
158 checked={ state.includes(
159 sectionAndLevel.id
160 ) }
161 value={ sectionAndLevel.id }
162 onChange={ ( checked ) => {
163 checkOption(
164 sectionAndLevel.id,
165 checked,
166 setState
167 );
168 } }
169 />
170 );
171 } ) }
172 </PanelBody>
173 </InspectorControls>
174 </Fragment>
175 );
176 };
177 }, 'withFapiSectionAndLevels' );
178
179 addFilter(
180 'editor.BlockEdit',
181 'fapi-member-core-block-extender/section-and-levels',
182 withFapiSectionAndLevels
183 );
184
185 const addFapiMemberExtraProps = ( saveElementProps, blockType, attributes ) => {
186 if ( disabledBlocks.includes( blockType ) ) {
187 return saveElementProps;
188 }
189
190 if ( saveElementProps.hasSectionOrLevel ) {
191 saveElementProps.hasSectionOrLevel = attributes.hasSectionOrLevel;
192 }
193
194 if ( saveElementProps.fapiSectionAndLevels ) {
195 saveElementProps.fapiSectionAndLevels = JSON.stringify(
196 attributes.fapiSectionAndLevels
197 );
198 }
199
200 return saveElementProps;
201 };
202
203 addFilter(
204 'blocks.getSaveContent.extraProps',
205 'fapi-member-core-block-extender/get-save-content-extra-props',
206 addFapiMemberExtraProps
207 );
208