3, 'title' => __( 'Custom field', 'customfields-com' ), 'category' => 'widgets', 'editor_script' => 'customfields-block', 'attributes' => array( 'name' => array( 'type' => 'string', 'default' => '' ), 'tag' => array( 'type' => 'string', 'default' => 'div' ) ), 'uses_context' => array( 'postId', 'postType' ), 'render_callback' => array( __CLASS__, 'render' ) ) ); if ( function_exists( 'register_block_bindings_source' ) ) { register_block_bindings_source( 'customfields/value', array( 'label' => __( 'CustomFields.com', 'customfields-com' ), 'uses_context' => array( 'postId' ), 'get_value_callback' => array( __CLASS__, 'binding' ) ) ); } } public static function editor() { global $post; $fields = array(); if ( $post && current_user_can( 'edit_post', $post->ID ) ) { foreach ( customfields_get_groups() as $group ) { if ( ! Editor::matches( $group, $post ) ) { continue; } foreach ( $group['fields'] as $field ) { if ( in_array( $field['type'], array( 'repeater', 'group', 'flexible_content', 'clone' ), true ) ) { continue; } $fields[] = array( 'label' => $group['title'] . ' / ' . $field['label'], 'value' => $field['name'] ); } } } wp_localize_script( 'customfields-block', 'CustomFieldsBlock', array( 'fields' => $fields ) ); } public static function accessible( $id ) { $post = get_post( $id ); return $post && ! post_password_required( $post ) && ( is_post_publicly_viewable( $post ) || current_user_can( 'read_post', $id ) ); } public static function definition( $name, $id ) { foreach ( customfields_get_groups() as $group ) { if ( ! Editor::matches( $group, get_post( $id ) ) ) { continue; } foreach ( $group['fields'] as $field ) { if ( $field['name'] === $name ) { return $field; } } } return null; } public static function binding( $args, $block, $attribute ) { $id = (int) ( $block->context['postId'] ?? get_the_ID() ); $name = $args['name'] ?? ''; if ( ! is_string( $name ) || ! self::accessible( $id ) ) { return null; } $field = self::definition( $name, $id ); if ( ! $field || ! in_array( $attribute, array( 'content', 'url', 'title', 'alt' ), true ) ) { return null; } $value = customfields_get( $name, $id ); if ( 'image' === $field['type'] && 'url' === $attribute ) { return wp_get_attachment_image_url( (int) $value, 'large' ) ?: null; } if ( ! is_scalar( $value ) ) { return null; } return 'url' === $attribute ? esc_url( $value ) : esc_html( (string) $value ); } public static function render( $attributes, $content = '', $block = null ) { $id = (int) ( $block->context['postId'] ?? get_the_ID() ); $name = $attributes['name'] ?? ''; if ( ! is_string( $name ) || ! self::accessible( $id ) ) { return ''; } $field = self::definition( $name, $id ); if ( ! $field ) { return ''; } $value = customfields_get( $name, $id ); $html = ''; if ( 'image' === $field['type'] ) { $html = wp_get_attachment_image( (int) $value, 'large' ); } elseif ( 'file' === $field['type'] ) { $url = wp_get_attachment_url( (int) $value ); if ( $url ) { $html = '' . esc_html( get_the_title( (int) $value ) ?: $field['label'] ) . ''; } } elseif ( 'wysiwyg' === $field['type'] && is_string( $value ) ) { $html = wp_kses_post( $value ); } elseif ( 'gallery' === $field['type'] && is_array( $value ) ) { foreach ( $value as $attachment ) { $html .= wp_get_attachment_image( (int) $attachment, 'medium' ); } } elseif ( in_array( $field['type'], array( 'multiselect', 'checkboxes' ), true ) && is_array( $value ) ) { foreach ( $value as $choice ) { if ( ! is_scalar( $choice ) ) { return ''; } } $html = esc_html( implode( ', ', array_map( static function ( $choice ) use ( $field ) { return $field['choices'][ (string) $choice ] ?? (string) $choice; }, $value ) ) ); } elseif ( is_scalar( $value ) ) { $html = esc_html( is_bool( $value ) ? ( $value ? '1' : '0' ) : (string) $value ); } if ( '' === $html ) { return ''; } $tag = in_array( $attributes['tag'] ?? '', array( 'div', 'p', 'span', 'h2', 'h3' ), true ) ? $attributes['tag'] : 'div'; // Standalone previews, listings and builder widgets have no block-support context. // Only the actual value block may inherit its current WordPress wrapper attributes. $wrapper = $block instanceof \WP_Block && 'customfields/value' === $block->name && is_array( \WP_Block_Supports::$block_to_render ) && \WP_Block_Supports::$block_to_render === $block->parsed_block ? get_block_wrapper_attributes() : ''; return '<' . $tag . ' ' . $wrapper . '>' . $html . ''; } }