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 
<?php
/**
 * CMB 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_Base {

    /**
     * The CMB2_Types object
     *
     * @var CMB2_Types
     */
    public $types;

    /**
     * Arguments for use in the render method
     *
     * @var array
     */
    public $args;

    /**
     * Rendered output (if 'rendered' argument is set to false)
     *
     * @var string
     */
    protected $rendered = '';

    /**
     * Constructor
     *
     * @since 2.2.2
     * @param CMB2_Types $types Object for the field type.
     * @param array      $args  Array of arguments for the type.
     */
    public function __construct( CMB2_Types $types, $args = array() ) {
        $this->types      = $types;
        $args['rendered'] = isset( $args['rendered'] ) ? (bool) $args['rendered'] : true;
        $this->args       = $args;
    }

    /**
     * Handles rendering this field type.
     *
     * @since  2.2.2
     * @return string  Rendered field type.
     */
    abstract public function render();

    /**
     * Stores the rendered field output.
     *
     * @since  2.2.2
     * @param  string|CMB2_Type_Base $rendered Rendered output.
     * @return string|CMB2_Type_Base           Rendered output or this object.
     */
    public function rendered( $rendered ) {
        $this->field->register_js_data();

        if ( $this->args['rendered'] ) {
            return is_a( $rendered, __CLASS__ ) ? $rendered->rendered : $rendered;
        }

        $this->rendered = is_a( $rendered, __CLASS__ ) ? $rendered->rendered : $rendered;

        return $this;
    }

    /**
     * Returns the stored rendered field output.
     *
     * @since  2.2.2
     * @return string Stored rendered output (if 'rendered' argument is set to false).
     */
    public function get_rendered() {
        return $this->rendered;
    }

    /**
     * Handles parsing and filtering attributes while preserving any passed in via field config.
     *
     * @since  1.1.0
     * @param  string $element        Element for filter.
     * @param  array  $type_defaults  Type default arguments.
     * @param  array  $type_overrides Type override arguments.
     * @return array                  Parsed and filtered arguments.
     */
    public function parse_args( $element, $type_defaults, $type_overrides = array() ) {
        $args = $this->parse_args_from_overrides( $type_overrides );

        /**
         * Filter attributes for a field type.
         * The dynamic portion of the hook name, $element, refers to the field type.
         *
         * @since 1.1.0
         * @param array  $args              The array of attribute arguments.
         * @param array  $type_defaults     The array of default values.
         * @param array  $field             The `CMB2_Field` object.
         * @param object $field_type_object This `CMB2_Types` object.
         */
        $args = apply_filters( "cmb2_{$element}_attributes", $args, $type_defaults, $this->field, $this->types );

        $args = wp_parse_args( $args, $type_defaults );

        if ( ! empty( $args['js_dependencies'] ) ) {
            $this->field->add_js_dependencies( $args['js_dependencies'] );
        }

        return $args;
    }

    /**
     * Handles parsing and filtering attributes while preserving any passed in via field config.
     *
     * @since  2.2.4
     * @param  array $type_overrides Type override arguments.
     * @return array                 Parsed arguments
     */
    protected function parse_args_from_overrides( $type_overrides = array() ) {
        $type_overrides = empty( $type_overrides ) ? $this->args : $type_overrides;

        if ( true !== $this->field->args( 'disable_hash_data_attribute' ) ) {
            $type_overrides['data-hash'] = $this->field->hash_id();
        }

        $field_overrides = $this->field->args( 'attributes' );

        return ! empty( $field_overrides )
            ? wp_parse_args( $field_overrides, $type_overrides )
            : $type_overrides;
    }

    /**
     * Fall back to CMB2_Types methods
     *
     * @param  string $method    Method name being invoked.
     * @param  array  $arguments Arguments passed for the method.
     * @throws Exception Throws an exception if the field is invalid.
     * @return mixed
     */
    public function __call( $method, $arguments ) {
        switch ( $method ) {
            case '_id':
            case '_name':
            case '_desc':
            case '_text':
            case 'concat_attrs':
                return call_user_func_array( array( $this->types, $method ), $arguments );
            default:
                throw new Exception( sprintf( esc_html__( 'Invalid %1$s method: %2$s', 'cmb2' ), __CLASS__, $method ) );
        }
    }

    /**
     * Magic getter for our object.
     *
     * @param string $field Property being requested.
     * @throws Exception Throws an exception if the field is invalid.
     * @return mixed
     */
    public function __get( $field ) {
        switch ( $field ) {
            case 'field':
                return $this->types->field;
            default:
                throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'cmb2' ), __CLASS__, $field ) );
        }
    }

}
CMB2 Documentation API documentation generated by ApiGen