name = 'taxonomy'; $this->label = 'Taxonomy'; $this->category = 'relational'; } public function render( $field, $value = '', $name = null ) { $name = $name ?: "metaforge[{$field['name']}]"; $dynamic_source = sanitize_key( $field['dynamic_source'] ?? '' ); $appearance = $field['appearance'] ?? 'select'; // Backward compatibility for the old 'multiple' toggle if ( isset( $field['multiple'] ) && empty( $field['appearance'] ) ) { $appearance = ! empty( $field['multiple'] ) ? 'multi_select' : 'select'; } $is_multiple = in_array( $appearance, array( 'checkbox', 'multi_select' ) ); $name_attr = $is_multiple ? $name . '[]' : $name; $val_array = is_array( $value ) ? $value : (array) $value; // --- Dynamic mode: linked to a post_type_selector field --------------- if ( ! empty( $dynamic_source ) ) { echo '
'; echo ''; echo '
'; // Render a container or select based on appearance if ( $appearance === 'checkbox' || $appearance === 'radio' ) { $list_class = $appearance === 'checkbox' ? 'mfe-choice-list mfe-checkbox-list mfe-tax-target' : 'mfe-choice-list mfe-radio-list mfe-tax-target'; // Container for AJAX to inject checkboxes/radios echo '
'; if ( ! empty( $val_array ) ) { // Render pre-selected terms so they aren't lost before AJAX completes foreach ( $val_array as $term_id ) { $term = get_term( intval( $term_id ) ); if ( $term && ! is_wp_error( $term ) ) { $type = $appearance === 'checkbox' ? 'checkbox' : 'radio'; echo ''; } } } echo '
'; } else { // Render select (single or multiple) $placeholder = '— Select ' . esc_html( $field['label'] ) . ' —'; if ( $appearance === 'multi_select' ) { echo ''; echo ''; } if ( ! empty( $val_array ) ) { foreach ( $val_array as $term_id ) { $term = get_term( intval( $term_id ) ); if ( $term && ! is_wp_error( $term ) ) { echo ''; } } } echo ''; } echo 'Loading…'; echo '
'; echo '
'; return; } // --- Static mode: taxonomy slug set manually in field options --------- $taxonomy = 'category'; if ( ! empty( $field['taxonomy_slug'] ) ) { $taxonomy = sanitize_key( $field['taxonomy_slug'] ); } elseif ( ! empty( $field['options_raw'] ) ) { $lines = explode( "\n", trim( $field['options_raw'] ) ); if ( ! empty( $lines[0] ) ) { $taxonomy = sanitize_key( trim( $lines[0] ) ); } } $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, ) ); echo '
'; echo ''; echo '
'; if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { if ( $appearance === 'checkbox' || $appearance === 'radio' ) { $list_class = $appearance === 'checkbox' ? 'mfe-choice-list mfe-checkbox-list' : 'mfe-choice-list mfe-radio-list'; echo '
'; $type = $appearance === 'checkbox' ? 'checkbox' : 'radio'; foreach ( $terms as $term ) { $is_selected = in_array( $term->term_id, $val_array ); echo ''; } echo '
'; } else { $placeholder = '— Select ' . esc_html( $field['label'] ) . ' —'; if ( $appearance === 'multi_select' ) { echo ''; echo ''; } foreach ( $terms as $term ) { $is_selected = in_array( $term->term_id, $val_array ); echo ''; } echo ''; } } else { echo 'No terms found for taxonomy: ' . esc_html( $taxonomy ) . ''; } echo '
'; echo '
'; } public function sanitize( $value, $field ) { if ( is_array( $value ) ) { return array_map( 'intval', $value ); } return intval( $value ); } }