CMB2 Documentation
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hooks
  • Download Docs
  • Github

Packages

  • CMB2
  • Demo
    • CMB2
  • None
  • Tests
    • CMB2

Classes

  • CMB2
  • CMB2_Ajax
  • CMB2_Base
  • CMB2_Bootstrap_2101
  • CMB2_Boxes
  • CMB2_Display_Checkbox
  • CMB2_Display_Colorpicker
  • CMB2_Display_File
  • CMB2_Display_File_List
  • CMB2_Display_Multicheck
  • CMB2_Display_oEmbed
  • CMB2_Display_Select
  • CMB2_Display_Taxonomy_Multicheck
  • CMB2_Display_Taxonomy_Radio
  • CMB2_Display_Text_Date
  • CMB2_Display_Text_Date_Timezone
  • CMB2_Display_Text_Money
  • CMB2_Display_Text_Time
  • CMB2_Display_Text_Url
  • CMB2_Display_Textarea
  • CMB2_Display_Textarea_Code
  • CMB2_Field
  • CMB2_Field_Display
  • CMB2_Hookup
  • CMB2_Hookup_Base
  • CMB2_Integration_Box
  • CMB2_JS
  • CMB2_Option
  • CMB2_Options
  • CMB2_Options_Hookup
  • CMB2_REST
  • CMB2_REST_Controller
  • CMB2_REST_Controller_Boxes
  • CMB2_REST_Controller_Fields
  • CMB2_Sanitize
  • CMB2_Show_Filters
  • CMB2_Type_Base
  • CMB2_Type_Checkbox
  • CMB2_Type_Colorpicker
  • CMB2_Type_Counter_Base
  • CMB2_Type_File
  • CMB2_Type_File_Base
  • CMB2_Type_File_List
  • CMB2_Type_Multi_Base
  • CMB2_Type_Multicheck
  • CMB2_Type_Oembed
  • CMB2_Type_Picker_Base
  • CMB2_Type_Radio
  • CMB2_Type_Select
  • CMB2_Type_Select_Timezone
  • CMB2_Type_Taxonomy_Base
  • CMB2_Type_Taxonomy_Multicheck
  • CMB2_Type_Taxonomy_Multicheck_Hierarchical
  • CMB2_Type_Taxonomy_Radio
  • CMB2_Type_Taxonomy_Radio_Hierarchical
  • CMB2_Type_Taxonomy_Select
  • CMB2_Type_Taxonomy_Select_Hierarchical
  • CMB2_Type_Text
  • CMB2_Type_Text_Date
  • CMB2_Type_Text_Datetime_Timestamp
  • CMB2_Type_Text_Datetime_Timestamp_Timezone
  • CMB2_Type_Text_Time
  • CMB2_Type_Textarea
  • CMB2_Type_Textarea_Code
  • CMB2_Type_Title
  • CMB2_Type_Wysiwyg
  • CMB2_Types
  • CMB2_Utils

Hooks

  • Hook Reference
  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
/**
 * CMB Taxonomy base field type
 *
 * @since  2.2.2
 *
 * @category  WordPress_Plugin
 * @package   CMB2
 * @author    CMB2 team
 * @license   GPL-2.0+
 * @link      https://cmb2.io
 */
abstract class CMB2_Type_Taxonomy_Base extends CMB2_Type_Multi_Base {

    /**
     * Parent term ID when looping hierarchical terms.
     *
     * @var integer|null
     */
    protected $parent = null;

    /**
     * Checks if we can get a post object, and if so, uses `get_the_terms` which utilizes caching.
     *
     * @since  1.0.2
     * @return mixed Array of terms on success
     */
    public function get_object_terms() {
        switch ( $this->field->object_type ) {
            case 'options-page':
            case 'term':
                return $this->options_terms();
            case 'post':
                // WP caches internally so it's better to use
                return get_the_terms( $this->field->object_id, $this->field->args( 'taxonomy' ) );

            default:
                return $this->non_post_object_terms();
        }
    }

    /**
     * Gets the term objects for the terms stored via options boxes.
     *
     * @since  2.2.4
     * @return mixed Array of terms on success
     */
    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;
    }

    /**
     * For non-post objects, wraps the call to wp_get_object_terms with transient caching.
     *
     * @since  2.2.4
     * @return mixed Array of terms on success
     */
    public function non_post_object_terms() {
        $object_id = $this->field->object_id;
        $taxonomy = $this->field->args( 'taxonomy' );

        $cache_key = "cmb-cache-{$taxonomy}-{$object_id}";

        // Check cache
        $cached = get_transient( $cache_key );

        if ( ! $cached ) {
            $cached = wp_get_object_terms( $object_id, $taxonomy );
            // Do our own (minimal) caching. Long enough for a page-load.
            set_transient( $cache_key, $cached, 60 );
        }

        return $cached;
    }

    /**
     * Wrapper for `get_terms` to account for changes in WP 4.6 where taxonomy is expected
     * as part of the arguments.
     *
     * @since  2.2.2
     * @return mixed Array of terms on success
     */
    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;
    }

    /**
     * Takes a list of all tax terms and outputs.
     *
     * @since  2.2.5
     *
     * @param  array  $all_terms   Array of all terms.
     * @param  array|string $saved Array of terms set to the object, or single term slug.
     *
     * @return string              List of terms.
     */
    protected function loop_terms( $all_terms, $saved_terms ) {
        return '';
    }

    /**
     * Build children hierarchy.
     *
     * @param  object       $parent_term The parent term object.
     * @param  array|string $saved       Array of terms set to the object, or single term slug.
     *
     * @return string                    List of terms.
     */
    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;
    }

    /**
     * Build child terms output.
     *
     * @since  2.6.1
     *
     * @param  array        $terms Array of child terms.
     * @param  array|string $saved Array of terms set to the object, or single term slug.
     *
     * @return string              Child option output.
     */
    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;
    }

}
CMB2 Documentation API documentation generated by ApiGen