'',
'post_id' => null,
),
$atts,
'cfstudio'
);
if ( '' === $atts['field'] ) {
return '';
}
$post_id = cfstudio_resolve_post_id( $atts['post_id'] );
if ( ! $post_id ) {
return '';
}
// Respect post visibility: don't leak fields of drafts/private posts.
// (Options page values are site-wide and always public.)
if ( is_string( $post_id ) && 0 === strpos( $post_id, 'user_' ) && ! current_user_can( 'edit_user', (int) substr( $post_id, 5 ) ) ) {
return ''; // User meta is private unless you may edit that user.
}
if ( is_int( $post_id ) ) {
$post = get_post( $post_id );
if ( ! $post || ( 'publish' !== $post->post_status && ! current_user_can( 'read_post', $post_id ) ) ) {
return '';
}
}
$field = CFStudio_Field_Group::find_field(
cfstudio_groups_for_context( $post_id ),
sanitize_key( $atts['field'] )
);
if ( ! $field ) {
return '';
}
$value = cfstudio_get_field( $field['name'], $post_id );
return self::render_field( $field, $value );
}
/**
* Render one formatted value according to its field config.
*
* @param array $field Field config.
* @param mixed $value Value as returned by cfstudio_get_field().
* @return string HTML (already escaped).
*/
public static function render_field( $field, $value ) {
if ( null === $value || '' === $value || array() === $value ) {
return '';
}
$type = $field['type'] ?? 'text';
switch ( $type ) {
case 'repeater':
return self::render_rows( $field['sub_fields'] ?? array(), $value, 'cfs-out-repeater' );
case 'flexible_content':
$html = '
';
foreach ( (array) $value as $row ) {
$layout = null;
foreach ( (array) ( $field['layouts'] ?? array() ) as $candidate ) {
if ( ( $candidate['name'] ?? '' ) === ( $row[ CFStudio_Field_Flexible::LAYOUT_KEY ] ?? '' ) ) {
$layout = $candidate;
break;
}
}
if ( ! $layout ) {
continue;
}
$html .= '';
$html .= '' . esc_html( $layout['label'] ?? $layout['name'] ) . '
';
$html .= self::render_cells( $layout['sub_fields'] ?? array(), $row );
$html .= '';
}
return $html . '
';
case 'wysiwyg':
return '' . wpautop( wp_kses_post( (string) $value ) ) . '
';
case 'image':
if ( is_array( $value ) && ! empty( $value['url'] ) ) {
return '
';
}
if ( is_string( $value ) && '' !== $value ) {
return '
';
}
return is_scalar( $value ) ? '' . esc_html( (string) $value ) . '' : '';
case 'gallery':
$html = '';
foreach ( (array) $value as $img ) {
$src = is_array( $img ) ? ( $img['sizes']['medium'] ?? $img['url'] ?? '' ) : ( is_numeric( $img ) ? wp_get_attachment_image_url( (int) $img, 'medium' ) : $img );
$alt = is_array( $img ) ? ( $img['alt'] ?? '' ) : '';
if ( $src ) {
$html .= '
 . ')
';
}
}
return $html . '
';
case 'relationship':
$html = '';
case 'taxonomy':
$terms = is_array( $value ) ? $value : array( $value );
$html = '';
foreach ( $terms as $term ) {
$term = is_object( $term ) ? $term : get_term( absint( $term ), $field['taxonomy'] ?? 'category' );
if ( $term && ! is_wp_error( $term ) ) {
$link = get_term_link( $term );
$html .= '- ' . ( is_wp_error( $link ) ? esc_html( $term->name ) : '' . esc_html( $term->name ) . '' ) . '
';
}
}
return $html . '
';
case 'group':
return '' . self::render_cells( $field['sub_fields'] ?? array(), (array) $value ) . '
';
case 'post_object':
case 'user':
case 'page_link':
$html = '';
$list = ( is_array( $value ) && isset( $value[0] ) ) ? $value : array( $value );
foreach ( $list as $item ) {
if ( 'post_object' === $type ) {
$id = is_object( $item ) ? $item->ID : absint( $item );
$html .= $id ? '- ' . esc_html( get_the_title( $id ) ) . '
' : '';
} elseif ( 'page_link' === $type ) {
$html .= '- ' . esc_html( (string) $item ) . '
';
} else {
$name = is_object( $item ) ? $item->display_name : ( is_array( $item ) ? ( $item['display_name'] ?? '' ) : (string) get_the_author_meta( 'display_name', absint( $item ) ) );
$html .= '- ' . esc_html( $name ) . '
';
}
}
return $html . '
';
case 'email':
return '' . esc_html( (string) $value ) . '';
case 'url':
return '' . esc_html( (string) $value ) . '';
case 'link':
$url = is_array( $value ) ? ( $value['url'] ?? '' ) : (string) $value;
if ( '' === $url ) {
return '';
}
$title = is_array( $value ) && ! empty( $value['title'] ) ? $value['title'] : $url;
$target = ( is_array( $value ) && '_blank' === ( $value['target'] ?? '' ) ) ? ' target="_blank" rel="noopener"' : '';
return '' . esc_html( $title ) . '';
case 'file':
$url = is_array( $value ) ? ( $value['url'] ?? '' ) : ( is_numeric( $value ) ? wp_get_attachment_url( (int) $value ) : (string) $value );
$title = is_array( $value ) ? ( $value['filename'] ?? $url ) : wp_basename( (string) $url );
return $url ? '' . esc_html( $title ) . '' : '';
case 'color_picker':
return ' ' . esc_html( (string) $value ) . '';
case 'google_map':
return is_array( $value ) && isset( $value['lat'], $value['lng'] ) ? CFStudio_Field_Google_Map::embed_html( $value, (int) ( $field['height'] ?? 300 ) ) : '';
case 'oembed':
return '' . $value . '
'; // phpcs:ignore WordPress.Security.EscapeOutput -- core-filtered oEmbed HTML.
case 'checkbox':
case 'radio':
case 'button_group':
$items = array();
foreach ( (array) $value as $item ) {
$items[] = is_array( $item ) ? ( $item['label'] ?? $item['value'] ?? '' ) : (string) $item;
}
return '' . esc_html( implode( ', ', $items ) ) . '';
case 'true_false':
return '' . ( $value ? esc_html__( 'Yes', 'fieldloom' ) : esc_html__( 'No', 'fieldloom' ) ) . '';
case 'textarea':
return '' . nl2br( esc_html( (string) $value ) ) . '
';
default:
if ( is_array( $value ) ) {
return '' . esc_html( implode( ', ', array_map( 'strval', $value ) ) ) . '';
}
return '' . esc_html( (string) $value ) . '';
}
}
/**
* Rows of a repeater as a list of s.
*/
protected static function render_rows( $sub_fields, $rows, $class ) {
$html = '
';
foreach ( (array) $rows as $row ) {
$html .= '
' . self::render_cells( $sub_fields, (array) $row ) . '
';
}
return $html . '
';
}
/**
* The labelled cells of one row.
*/
protected static function render_cells( $sub_fields, $row ) {
$html = '';
foreach ( (array) $sub_fields as $sub ) {
if ( empty( $sub['name'] ) ) {
continue;
}
$cell = self::render_field( $sub, $row[ $sub['name'] ] ?? null );
if ( '' === $cell ) {
continue;
}
$html .= '
'
. '' . esc_html( $sub['label'] ?? $sub['name'] ) . ': '
. $cell . '
';
}
return $html;
}
}
CFStudio_Shortcode::init();