label. * * @return array */ abstract protected function choices(): array; /** * Formatted value for one ID when return_format is 'array'. */ abstract protected function item( int $id ): ?array; /** * Shortcode attribute this field maps to (service_id / category_id). */ abstract protected function shortcode_attr(): string; public function __construct() { $this->category = 'relational'; $this->defaults = [ 'multiple' => 0, 'allow_null' => 1, 'return_format' => 'id', 'ui' => 1, ]; parent::__construct(); } public function render_field_settings( $field ) { acf_render_field_setting( $field, [ 'label' => __( 'Select multiple', 'vizyt' ), 'instructions' => '', 'name' => 'multiple', 'type' => 'true_false', 'ui' => 1, ] ); acf_render_field_setting( $field, [ 'label' => __( 'Allow empty', 'vizyt' ), 'name' => 'allow_null', 'type' => 'true_false', 'ui' => 1, ] ); acf_render_field_setting( $field, [ 'label' => __( 'Return format', 'vizyt' ), 'name' => 'return_format', 'type' => 'radio', 'layout' => 'horizontal', 'choices' => [ 'id' => __( 'ID', 'vizyt' ), 'array' => __( 'Data array', 'vizyt' ), 'shortcode' => __( 'Booking shortcode', 'vizyt' ), ], ] ); } public function render_field( $field ) { $values = array_map( 'intval', (array) ( $field['value'] ?? [] ) ); $multiple = ! empty( $field['multiple'] ); $name = $field['name'] . ( $multiple ? '[]' : '' ); $html = sprintf( '', Kses::allowed_html() ); } public function update_value( $value, $post_id, $field ) { if ( is_array( $value ) ) { return array_values( array_filter( array_map( 'intval', $value ) ) ); } return '' === $value || null === $value ? '' : (int) $value; } public function format_value( $value, $post_id, $field ) { if ( '' === $value || null === $value || [] === $value ) { return ! empty( $field['multiple'] ) ? [] : null; } $ids = array_map( 'intval', (array) $value ); $out = []; foreach ( $ids as $id ) { switch ( $field['return_format'] ?? 'id' ) { case 'array': $item = $this->item( $id ); if ( $item ) { $out[] = $item; } break; case 'shortcode': $out[] = '[vizyt_booking ' . $this->shortcode_attr() . '="' . $id . '"]'; break; default: $out[] = $id; } } return ! empty( $field['multiple'] ) ? $out : ( $out[0] ?? null ); } }