PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.14
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.14
2.0.14 2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / user-field / index.js

index.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.14, at blocks/user-field/index.js

275 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 ( function () {
2 'use strict';
3
4 var el = wp.element.createElement;
5 var Fragment = wp.element.Fragment;
6 var useEffect = wp.element.useEffect;
7 var useState = wp.element.useState;
8 var __ = wp.i18n.__;
9 var registerBlockType = wp.blocks.registerBlockType;
10 var InspectorControls = wp.blockEditor.InspectorControls;
11 var useBlockProps = wp.blockEditor.useBlockProps;
12 var PanelColorSettings = wp.blockEditor.PanelColorSettings;
13 var PanelBody = wp.components.PanelBody;
14 var SelectControl = wp.components.SelectControl;
15 var TextControl = wp.components.TextControl;
16 var ToggleControl = wp.components.ToggleControl;
17
18 // Lazy lookup so the typography control is resolved at render time
19 function getTypographyControl() {
20 return ( typeof window.bkbgTypographyControl !== 'undefined' ) ? window.bkbgTypographyControl : null;
21 }
22 function getTypoCssVars() {
23 return ( typeof window.bkbgTypoCssVars !== 'undefined' ) ? window.bkbgTypoCssVars : function () { return {}; };
24 }
25
26 var FIELD_OPTIONS = [
27 { label: __( 'First name', 'blockenberg' ), value: 'first_name' },
28 { label: __( 'Last name', 'blockenberg' ), value: 'last_name' },
29 { label: __( 'Display name', 'blockenberg' ), value: 'display_name' },
30 { label: __( 'Nickname', 'blockenberg' ), value: 'nickname' },
31 { label: __( 'Username', 'blockenberg' ), value: 'user_login' },
32 { label: __( 'Email', 'blockenberg' ), value: 'user_email' },
33 { label: __( 'Website', 'blockenberg' ), value: 'user_url' },
34 { label: __( 'Biographical info', 'blockenberg' ), value: 'description' }
35 ];
36
37 var TAG_OPTIONS = [
38 { label: 'span', value: 'span' },
39 { label: 'p', value: 'p' },
40 { label: 'div', value: 'div' },
41 { label: 'h2', value: 'h2' },
42 { label: 'h3', value: 'h3' },
43 { label: 'h4', value: 'h4' },
44 { label: 'strong', value: 'strong' },
45 { label: 'em', value: 'em' }
46 ];
47
48 var SAMPLE = {
49 first_name: 'Alex',
50 last_name: 'Rivera',
51 display_name: 'Alex Rivera',
52 nickname: 'alexr',
53 user_login: 'alex',
54 user_email: '[email protected]',
55 user_url: 'https://example.com',
56 description: 'Writer and educator.'
57 };
58
59 function fieldLabel( field ) {
60 for ( var i = 0; i < FIELD_OPTIONS.length; i++ ) {
61 if ( FIELD_OPTIONS[ i ].value === field ) {
62 return FIELD_OPTIONS[ i ].label;
63 }
64 }
65 return field;
66 }
67
68 function pickValue( user, field ) {
69 if ( ! user ) {
70 return '';
71 }
72 if ( field === 'user_login' ) {
73 return user.username || user.slug || user.name || '';
74 }
75 if ( field === 'user_email' ) {
76 return user.email || '';
77 }
78 if ( field === 'user_url' ) {
79 return user.url || '';
80 }
81 if ( field === 'display_name' ) {
82 return user.name || '';
83 }
84 if ( user.meta && user.meta[ field ] ) {
85 return user.meta[ field ];
86 }
87 if ( user[ field ] ) {
88 return user[ field ];
89 }
90 return SAMPLE[ field ] || '';
91 }
92
93 /**
94 * Build wrapper style: layout/color + typography CSS vars.
95 * Typo vars (from TypographyControl) override --bkbg-uf-size / --bkbg-uf-w fallbacks in CSS.
96 */
97 function buildWrapStyle( a ) {
98 var s = {
99 '--bkbg-uf-align': a.textAlign || 'left',
100 '--bkbg-uf-size': ( a.fontSize || 16 ) + 'px',
101 '--bkbg-uf-w': a.fontWeight || '400'
102 };
103 if ( a.textColor ) {
104 s[ '--bkbg-uf-color' ] = a.textColor;
105 }
106 var _tv = getTypoCssVars();
107 Object.assign( s, _tv( a.typoText || {}, '--bkbg-uf-' ) );
108 return s;
109 }
110
111 registerBlockType( 'blockenberg/user-field', {
112 edit: function ( props ) {
113 var a = props.attributes;
114 var setAttributes = props.setAttributes;
115 var userState = useState( null );
116 var user = userState[ 0 ];
117 var setUser = userState[ 1 ];
118
119 useEffect( function () {
120 if ( ! wp.apiFetch ) {
121 return;
122 }
123 var cancelled = false;
124 wp.apiFetch( { path: '/wp/v2/users/me?context=edit' } )
125 .then( function ( data ) {
126 if ( ! cancelled ) {
127 setUser( data );
128 }
129 } )
130 .catch( function () {
131 if ( ! cancelled ) {
132 setUser( null );
133 }
134 } );
135 return function () {
136 cancelled = true;
137 };
138 }, [] );
139
140 var live = pickValue( user, a.field );
141 var previewValue = live || SAMPLE[ a.field ] || fieldLabel( a.field );
142 var previewText = ( a.prefix || '' ) + previewValue + ( a.suffix || '' );
143 var isBlockTag = [ 'p', 'div', 'h2', 'h3', 'h4' ].indexOf( a.htmlTag ) !== -1;
144
145 var blockProps = useBlockProps( {
146 className: 'bkbg-user-field-editor' + ( isBlockTag ? ' bkbg-user-field--block' : '' ),
147 style: buildWrapStyle( a )
148 } );
149
150 return el(
151 Fragment,
152 null,
153 el(
154 InspectorControls,
155 null,
156 el(
157 PanelBody,
158 { title: __( 'Field', 'blockenberg' ), initialOpen: true },
159 el( SelectControl, {
160 label: __( 'Profile field', 'blockenberg' ),
161 value: a.field,
162 options: FIELD_OPTIONS,
163 onChange: function ( v ) { setAttributes( { field: v } ); },
164 help: __( 'Shows the logged-in user’s value on the front end.', 'blockenberg' ),
165 __nextHasNoMarginBottom: true
166 } ),
167 el( TextControl, {
168 label: __( 'Prefix', 'blockenberg' ),
169 value: a.prefix,
170 onChange: function ( v ) { setAttributes( { prefix: v } ); },
171 placeholder: __( 'e.g. Hello, ', 'blockenberg' ),
172 __nextHasNoMarginBottom: true
173 } ),
174 el( TextControl, {
175 label: __( 'Suffix', 'blockenberg' ),
176 value: a.suffix,
177 onChange: function ( v ) { setAttributes( { suffix: v } ); },
178 placeholder: __( 'e.g. !', 'blockenberg' ),
179 __nextHasNoMarginBottom: true
180 } ),
181 el( SelectControl, {
182 label: __( 'HTML tag', 'blockenberg' ),
183 value: a.htmlTag,
184 options: TAG_OPTIONS,
185 onChange: function ( v ) { setAttributes( { htmlTag: v } ); },
186 __nextHasNoMarginBottom: true
187 } ),
188 el( SelectControl, {
189 label: __( 'Alignment', 'blockenberg' ),
190 value: a.textAlign,
191 options: [
192 { label: __( 'Left', 'blockenberg' ), value: 'left' },
193 { label: __( 'Center', 'blockenberg' ), value: 'center' },
194 { label: __( 'Right', 'blockenberg' ), value: 'right' }
195 ],
196 onChange: function ( v ) { setAttributes( { textAlign: v } ); },
197 __nextHasNoMarginBottom: true
198 } )
199 ),
200 el(
201 PanelBody,
202 { title: __( 'Guests & empty values', 'blockenberg' ), initialOpen: false },
203 el( ToggleControl, {
204 label: __( 'Hide when value is empty', 'blockenberg' ),
205 checked: a.hideWhenEmpty,
206 onChange: function ( v ) { setAttributes( { hideWhenEmpty: v } ); },
207 __nextHasNoMarginBottom: true
208 } ),
209 el( ToggleControl, {
210 label: __( 'Hide entirely for logged-out visitors', 'blockenberg' ),
211 checked: a.hideForGuests,
212 onChange: function ( v ) { setAttributes( { hideForGuests: v } ); },
213 __nextHasNoMarginBottom: true
214 } ),
215 ! a.hideForGuests && el( TextControl, {
216 label: __( 'Fallback text for guests', 'blockenberg' ),
217 value: a.guestFallback,
218 onChange: function ( v ) { setAttributes( { guestFallback: v } ); },
219 help: __( 'Shown when the visitor is not logged in (leave blank to show nothing if “Hide when empty” is on).', 'blockenberg' ),
220 __nextHasNoMarginBottom: true
221 } )
222 ),
223 el(
224 PanelBody,
225 { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
226 ( function () {
227 var TC = getTypographyControl();
228 if ( ! TC ) {
229 return el( 'p', { style: { color: '#999', fontSize: '12px', padding: '8px 0' } }, __( 'Typography control loading…', 'blockenberg' ) );
230 }
231 return el( TC, {
232 label: __( 'Text Typography', 'blockenberg' ),
233 value: a.typoText || {},
234 onChange: function ( v ) { setAttributes( { typoText: v } ); }
235 } );
236 } )()
237 ),
238 el( PanelColorSettings, {
239 title: __( 'Color', 'blockenberg' ),
240 initialOpen: false,
241 colorSettings: [
242 {
243 value: a.textColor,
244 onChange: function ( v ) { setAttributes( { textColor: v || '' } ); },
245 label: __( 'Text color', 'blockenberg' )
246 }
247 ]
248 } )
249 ),
250 el(
251 'div',
252 blockProps,
253 el(
254 a.htmlTag || 'span',
255 { className: 'bkbg-user-field' + ( isBlockTag ? ' bkbg-user-field--block' : '' ) },
256 previewText
257 ),
258 el(
259 'span',
260 { className: 'bkbg-user-field-editor__hint' },
261 live
262 ? __( 'Preview uses your account. Front end shows each visitor’s own profile field.', 'blockenberg' )
263 : __( 'Sample preview — front end shows the logged-in user’s real value.', 'blockenberg' )
264 )
265 )
266 );
267 },
268
269 // Dynamic block — HTML is generated in PHP on every request.
270 save: function () {
271 return null;
272 }
273 } );
274 }() );
275