| 1 |
<?php |
| 2 |
/** |
| 3 |
* User Field block — server render for the logged-in user's profile data. |
| 4 |
* |
| 5 |
* @package Blockenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Allowed profile fields and how to resolve them. |
| 12 |
* |
| 13 |
* @return array<string, string> field => label (for docs / editor parity). |
| 14 |
*/ |
| 15 |
function bkbg_user_field_allowed_fields() { |
| 16 |
return array( |
| 17 |
'first_name' => __( 'First name', 'blockenberg' ), |
| 18 |
'last_name' => __( 'Last name', 'blockenberg' ), |
| 19 |
'display_name' => __( 'Display name', 'blockenberg' ), |
| 20 |
'nickname' => __( 'Nickname', 'blockenberg' ), |
| 21 |
'user_login' => __( 'Username', 'blockenberg' ), |
| 22 |
'user_email' => __( 'Email', 'blockenberg' ), |
| 23 |
'user_url' => __( 'Website', 'blockenberg' ), |
| 24 |
'description' => __( 'Biographical info', 'blockenberg' ), |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Resolve a single field value for a WP_User. |
| 30 |
* |
| 31 |
* @param WP_User $user User object. |
| 32 |
* @param string $field Field key. |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
function bkbg_user_field_get_value( $user, $field ) { |
| 36 |
if ( ! ( $user instanceof WP_User ) ) { |
| 37 |
return ''; |
| 38 |
} |
| 39 |
|
| 40 |
$allowed = bkbg_user_field_allowed_fields(); |
| 41 |
if ( ! isset( $allowed[ $field ] ) ) { |
| 42 |
$field = 'first_name'; |
| 43 |
} |
| 44 |
|
| 45 |
switch ( $field ) { |
| 46 |
case 'user_login': |
| 47 |
return (string) $user->user_login; |
| 48 |
case 'user_email': |
| 49 |
return (string) $user->user_email; |
| 50 |
case 'user_url': |
| 51 |
return (string) $user->user_url; |
| 52 |
case 'display_name': |
| 53 |
return (string) $user->display_name; |
| 54 |
case 'nickname': |
| 55 |
case 'first_name': |
| 56 |
case 'last_name': |
| 57 |
case 'description': |
| 58 |
return (string) get_user_meta( $user->ID, $field, true ); |
| 59 |
default: |
| 60 |
return ''; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Convert a TypographyControl object to CSS custom properties (mirrors bkbgTypoCssVars). |
| 66 |
* |
| 67 |
* @param array $typo Typography attribute object. |
| 68 |
* @param string $prefix CSS variable prefix, e.g. '--bkbg-uf-'. |
| 69 |
* @return array<string, string> property => value (without trailing semicolon). |
| 70 |
*/ |
| 71 |
function bkbg_user_field_typo_css_vars( $typo, $prefix ) { |
| 72 |
if ( ! is_array( $typo ) || '' === $prefix ) { |
| 73 |
return array(); |
| 74 |
} |
| 75 |
|
| 76 |
$vars = array(); |
| 77 |
$unit_ok = array( 'px', 'em', 'rem', '%', 'vh', 'vw' ); |
| 78 |
$transforms = array( 'none', 'capitalize', 'uppercase', 'lowercase' ); |
| 79 |
$styles = array( 'normal', 'italic', 'oblique' ); |
| 80 |
$decors = array( 'none', 'underline', 'overline', 'line-through' ); |
| 81 |
|
| 82 |
if ( ! empty( $typo['family'] ) ) { |
| 83 |
$family = sanitize_text_field( (string) $typo['family'] ); |
| 84 |
// Strip quotes so the CSS value stays well-formed inside style="". |
| 85 |
$family = str_replace( array( "'", '"' ), '', $family ); |
| 86 |
if ( $family ) { |
| 87 |
$vars[ $prefix . 'font-family' ] = "'" . $family . "', sans-serif"; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if ( isset( $typo['weight'] ) && '' !== $typo['weight'] && null !== $typo['weight'] ) { |
| 92 |
$weight = preg_replace( '/[^0-9a-zA-Z]/', '', (string) $typo['weight'] ); |
| 93 |
if ( $weight ) { |
| 94 |
$vars[ $prefix . 'font-weight' ] = $weight; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! empty( $typo['transform'] ) && in_array( $typo['transform'], $transforms, true ) ) { |
| 99 |
$vars[ $prefix . 'text-transform' ] = $typo['transform']; |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! empty( $typo['style'] ) && in_array( $typo['style'], $styles, true ) ) { |
| 103 |
$vars[ $prefix . 'font-style' ] = $typo['style']; |
| 104 |
} |
| 105 |
|
| 106 |
if ( ! empty( $typo['decoration'] ) && in_array( $typo['decoration'], $decors, true ) ) { |
| 107 |
$vars[ $prefix . 'text-decoration' ] = $typo['decoration']; |
| 108 |
} |
| 109 |
|
| 110 |
$size_unit = isset( $typo['sizeUnit'] ) && in_array( $typo['sizeUnit'], $unit_ok, true ) ? $typo['sizeUnit'] : 'px'; |
| 111 |
foreach ( array( 'sizeDesktop' => 'font-size-d', 'sizeTablet' => 'font-size-t', 'sizeMobile' => 'font-size-m' ) as $key => $suffix ) { |
| 112 |
if ( isset( $typo[ $key ] ) && '' !== $typo[ $key ] && null !== $typo[ $key ] && is_numeric( $typo[ $key ] ) ) { |
| 113 |
$vars[ $prefix . $suffix ] = floatval( $typo[ $key ] ) . $size_unit; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
$lh_unit = isset( $typo['lineHeightUnit'] ) && in_array( $typo['lineHeightUnit'], $unit_ok, true ) ? $typo['lineHeightUnit'] : 'px'; |
| 118 |
foreach ( array( 'lineHeightDesktop' => 'line-height-d', 'lineHeightTablet' => 'line-height-t', 'lineHeightMobile' => 'line-height-m' ) as $key => $suffix ) { |
| 119 |
if ( isset( $typo[ $key ] ) && '' !== $typo[ $key ] && null !== $typo[ $key ] && is_numeric( $typo[ $key ] ) ) { |
| 120 |
$vars[ $prefix . $suffix ] = floatval( $typo[ $key ] ) . $lh_unit; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
$ls_unit = isset( $typo['letterSpacingUnit'] ) && in_array( $typo['letterSpacingUnit'], $unit_ok, true ) ? $typo['letterSpacingUnit'] : 'px'; |
| 125 |
foreach ( array( 'letterSpacingDesktop' => 'letter-spacing-d', 'letterSpacingTablet' => 'letter-spacing-t', 'letterSpacingMobile' => 'letter-spacing-m' ) as $key => $suffix ) { |
| 126 |
if ( isset( $typo[ $key ] ) && '' !== $typo[ $key ] && null !== $typo[ $key ] && is_numeric( $typo[ $key ] ) ) { |
| 127 |
$val = floatval( $typo[ $key ] ) . $ls_unit; |
| 128 |
$vars[ $prefix . $suffix ] = $val; |
| 129 |
if ( 'letterSpacingDesktop' === $key ) { |
| 130 |
$vars[ $prefix . 'letter-spacing' ] = $val; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
$ws_unit = isset( $typo['wordSpacingUnit'] ) && in_array( $typo['wordSpacingUnit'], $unit_ok, true ) ? $typo['wordSpacingUnit'] : 'px'; |
| 136 |
foreach ( array( 'wordSpacingDesktop' => 'word-spacing-d', 'wordSpacingTablet' => 'word-spacing-t', 'wordSpacingMobile' => 'word-spacing-m' ) as $key => $suffix ) { |
| 137 |
if ( isset( $typo[ $key ] ) && '' !== $typo[ $key ] && null !== $typo[ $key ] && is_numeric( $typo[ $key ] ) ) { |
| 138 |
$val = floatval( $typo[ $key ] ) . $ws_unit; |
| 139 |
$vars[ $prefix . $suffix ] = $val; |
| 140 |
if ( 'wordSpacingDesktop' === $key ) { |
| 141 |
$vars[ $prefix . 'word-spacing' ] = $val; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return $vars; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Render callback for blockenberg/user-field. |
| 151 |
* |
| 152 |
* @param array $attributes Block attributes. |
| 153 |
* @param string $content Block content (unused). |
| 154 |
* @param WP_Block $block Block instance. |
| 155 |
* @return string |
| 156 |
*/ |
| 157 |
function bkbg_render_user_field_block( $attributes, $content = '', $block = null ) { |
| 158 |
$defaults = array( |
| 159 |
'field' => 'first_name', |
| 160 |
'prefix' => '', |
| 161 |
'suffix' => '', |
| 162 |
'guestFallback' => '', |
| 163 |
'hideWhenEmpty' => true, |
| 164 |
'hideForGuests' => false, |
| 165 |
'htmlTag' => 'span', |
| 166 |
'textAlign' => 'left', |
| 167 |
'textColor' => '', |
| 168 |
'fontSize' => 16, |
| 169 |
'fontWeight' => '400', |
| 170 |
'typoText' => array(), |
| 171 |
); |
| 172 |
$a = wp_parse_args( is_array( $attributes ) ? $attributes : array(), $defaults ); |
| 173 |
|
| 174 |
$allowed_tags = array( 'span', 'p', 'div', 'h2', 'h3', 'h4', 'strong', 'em' ); |
| 175 |
$tag = in_array( $a['htmlTag'], $allowed_tags, true ) ? $a['htmlTag'] : 'span'; |
| 176 |
|
| 177 |
$is_logged_in = is_user_logged_in(); |
| 178 |
$value = ''; |
| 179 |
|
| 180 |
if ( $is_logged_in ) { |
| 181 |
$value = bkbg_user_field_get_value( wp_get_current_user(), $a['field'] ); |
| 182 |
} else { |
| 183 |
if ( ! empty( $a['hideForGuests'] ) ) { |
| 184 |
return ''; |
| 185 |
} |
| 186 |
$value = (string) $a['guestFallback']; |
| 187 |
} |
| 188 |
|
| 189 |
$value = trim( $value ); |
| 190 |
|
| 191 |
if ( '' === $value && ! empty( $a['hideWhenEmpty'] ) ) { |
| 192 |
return ''; |
| 193 |
} |
| 194 |
|
| 195 |
$prefix = (string) $a['prefix']; |
| 196 |
$suffix = (string) $a['suffix']; |
| 197 |
$text = $prefix . $value . $suffix; |
| 198 |
|
| 199 |
if ( '' === trim( $text ) ) { |
| 200 |
return ''; |
| 201 |
} |
| 202 |
|
| 203 |
$align = in_array( $a['textAlign'], array( 'left', 'center', 'right' ), true ) ? $a['textAlign'] : 'left'; |
| 204 |
$css = array( |
| 205 |
'--bkbg-uf-align:' . $align, |
| 206 |
'--bkbg-uf-size:' . absint( $a['fontSize'] ) . 'px', |
| 207 |
'--bkbg-uf-w:' . preg_replace( '/[^0-9a-zA-Z]/', '', (string) $a['fontWeight'] ), |
| 208 |
); |
| 209 |
|
| 210 |
if ( ! empty( $a['textColor'] ) ) { |
| 211 |
$color = sanitize_text_field( $a['textColor'] ); |
| 212 |
if ( preg_match( '/^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/', $color ) |
| 213 |
|| preg_match( '/^rgba?\([\d\s.,%]+\)$/', $color ) ) { |
| 214 |
$css[] = '--bkbg-uf-color:' . $color; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
$typo_vars = bkbg_user_field_typo_css_vars( |
| 219 |
is_array( $a['typoText'] ) ? $a['typoText'] : array(), |
| 220 |
'--bkbg-uf-' |
| 221 |
); |
| 222 |
foreach ( $typo_vars as $prop => $val ) { |
| 223 |
$css[] = $prop . ':' . $val; |
| 224 |
} |
| 225 |
|
| 226 |
$is_block_tag = in_array( $tag, array( 'p', 'div', 'h2', 'h3', 'h4' ), true ); |
| 227 |
$class = 'bkbg-user-field' . ( $is_block_tag ? ' bkbg-user-field--block' : '' ); |
| 228 |
|
| 229 |
$wrapper_attributes = get_block_wrapper_attributes( |
| 230 |
array( |
| 231 |
'class' => $class, |
| 232 |
'style' => implode( ';', array_filter( $css ) ), |
| 233 |
) |
| 234 |
); |
| 235 |
|
| 236 |
return sprintf( |
| 237 |
'<%1$s %2$s>%3$s</%1$s>', |
| 238 |
$tag, |
| 239 |
$wrapper_attributes, |
| 240 |
esc_html( $text ) |
| 241 |
); |
| 242 |
} |
| 243 |
|
| 244 |
add_filter( |
| 245 |
'register_block_type_args', |
| 246 |
function ( $args, $block_type ) { |
| 247 |
if ( 'blockenberg/user-field' === $block_type ) { |
| 248 |
$args['render_callback'] = 'bkbg_render_user_field_block'; |
| 249 |
} |
| 250 |
return $args; |
| 251 |
}, |
| 252 |
5, |
| 253 |
2 |
| 254 |
); |
| 255 |
|