/** * Return the field markup for the front-end. * * @return string Field markup */ public function display() { ob_start(); foreach ($this->ordered_terms as $term) { wpas_hierarchical_taxonomy_dropdown_options($term, $this->populate()); } $options = ob_get_contents(); ob_end_clean(); return sprintf('<label {{label_atts}}>{{label}}</label><select {{atts}}><option value="">%s</option>%s</select>', __('Please select', 'awesome-support'), $options); }
/** * Recursively displays hierarchical options into a select dropdown. * * @since 3.0.1 * * @param object $term The term to display * @param string $value The value to compare against * @param int $level The current level in the drop-down hierarchy * * @return void */ function wpas_hierarchical_taxonomy_dropdown_options($term, $value, $level = 1) { $option = ''; /* Add a visual indication that this is a child term */ if (1 !== $level) { for ($i = 1; $i < $level - 1; $i++) { $option .= ' '; } $option .= '∟ '; } $option .= apply_filters('wpas_hierarchical_taxonomy_dropdown_options_label', $term->name, $term, $value, $level); ?> <option value="<?php echo $term->term_id; ?> " <?php if ((int) $value === (int) $term->term_id || $value === $term->slug) { echo 'selected="selected"'; } ?> ><?php echo $option; ?> </option> <?php if (isset($term->children) && !empty($term->children)) { ++$level; foreach ($term->children as $child) { wpas_hierarchical_taxonomy_dropdown_options($child, $value, $level); } } }
/** * "Fake" taxonomy select. * * @param array $field Field options * @since 3.0.0 */ public static function taxonomy($field) { global $post; $field_id = 'wpas_' . $field['name']; $label = wpas_get_field_title($field); $current = get_the_terms($post->ID, sanitize_text_field($field['name'])); $terms = get_terms(sanitize_text_field($field['name']), array('hide_empty' => 0)); $value = ''; $ordered_terms = array(); if (is_array($current)) { foreach ($current as $term) { $value = $term->slug; } } /* In case the taxonomy does not exist */ if (is_wp_error($terms)) { return; } /** * Re-order the terms hierarchically. */ wpas_sort_terms_hierarchicaly($terms, $ordered_terms); ?> <div <?php wpas_get_field_container_class($field_id); ?> id="<?php echo $field_id; ?> _container"> <label for="<?php echo $field_id; ?> "><strong><?php echo $label; ?> </strong></label> <?php if (!is_admin() || current_user_can($field['args']['capability'])) { ?> <select name="<?php echo $field_id; ?> " id="<?php echo $field_id; ?> " <?php wpas_get_field_class($field_id); ?> > <option value=""><?php _e('Please select', 'wpas'); ?> </option> <?php foreach ($ordered_terms as $term) { wpas_hierarchical_taxonomy_dropdown_options($term, $value); } ?> </select> <?php } else { ?> <p id="<?php echo $field_id; ?> "><?php echo $value; ?> </p> <?php } if (isset($field['args']['desc']) && '' != $field['args']['desc'] && WPAS_FIELDS_DESC) { ?> <p class="<?php echo is_admin() ? 'description' : 'wpas-help-block'; ?> "><?php echo wp_kses($field['args']['desc']); ?> </p><?php } ?> </div> <?php }