1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
<?php
abstract class CMB2_Type_Taxonomy_Base extends CMB2_Type_Multi_Base {
protected $parent = null;
public function get_object_terms() {
switch ( $this->field->object_type ) {
case 'options-page':
case 'term':
return $this->options_terms();
case 'post':
return get_the_terms( $this->field->object_id, $this->field->args( 'taxonomy' ) );
default:
return $this->non_post_object_terms();
}
}
public function options_terms() {
if ( empty( $this->field->value ) ) {
return array();
}
$terms = (array) $this->field->value;
foreach ( $terms as $index => $term ) {
$terms[ $index ] = get_term_by( 'slug', $term, $this->field->args( 'taxonomy' ) );
}
return $terms;
}
public function non_post_object_terms() {
$object_id = $this->field->object_id;
$taxonomy = $this->field->args( 'taxonomy' );
$cache_key = "cmb-cache-{$taxonomy}-{$object_id}";
$cached = get_transient( $cache_key );
if ( ! $cached ) {
$cached = wp_get_object_terms( $object_id, $taxonomy );
set_transient( $cache_key, $cached, 60 );
}
return $cached;
}
public function get_terms() {
$args = array(
'taxonomy' => $this->field->args( 'taxonomy' ),
'hide_empty' => false,
);
if ( null !== $this->parent ) {
$args['parent'] = $this->parent;
}
$args = wp_parse_args( $this->field->prop( 'query_args', array() ), $args );
return CMB2_Utils::wp_at_least( '4.5.0' )
? get_terms( $args )
: get_terms( $this->field->args( 'taxonomy' ), http_build_query( $args ) );
}
protected function no_terms_result( $error, $tag = 'li' ) {
if ( is_wp_error( $error ) ) {
$message = $error->get_error_message();
$data = 'data-error="' . esc_attr( $error->get_error_code() ) . '"';
} else {
$message = $this->_text( 'no_terms_text', esc_html__( 'No terms', 'cmb2' ) );
$data = '';
}
$this->field->args['select_all_button'] = false;
return sprintf( '<%3$s><label %1$s>%2$s</label></%3$s>', $data, esc_html( $message ), $tag );
}
public function get_object_term_or_default() {
$saved_terms = $this->get_object_terms();
return is_wp_error( $saved_terms ) || empty( $saved_terms )
? $this->field->get_default()
: array_shift( $saved_terms )->slug;
}
protected function loop_terms( $all_terms, $saved_terms ) {
return '';
}
protected function build_children( $parent_term, $saved ) {
if ( empty( $parent_term->term_id ) ) {
return '';
}
$this->parent = $parent_term->term_id;
$terms = $this->get_terms();
$options = '';
if ( ! empty( $terms ) && is_array( $terms ) ) {
$options .= $this->child_option_output( $terms, $saved );
}
return $options;
}
public function child_option_output( $terms, $saved ) {
$output = '<li class="cmb2-indented-hierarchy"><ul>';
$output .= $this->loop_terms( $terms, $saved );
$output .= '</ul></li>';
return $output;
}
}