CMB2_Type_Base

CMB base field type.

Defined (1)

The class is defined in the following location(s).

/includes/types/CMB2_Type_Base.php  
  1. abstract class CMB2_Type_Base { 
  2.  
  3. /** 
  4. * The CMB2_Types object 
  5. * @var CMB2_Types 
  6. */ 
  7. public $types; 
  8.  
  9. /** 
  10. * Arguments for use in the render method 
  11. * @var array 
  12. */ 
  13. public $args; 
  14.  
  15. /** 
  16. * Rendered output (if 'rendered' argument is set to false) 
  17. * @var string 
  18. */ 
  19. protected $rendered = ''; 
  20.  
  21. /** 
  22. * Constructor 
  23. * @since 2.2.2 
  24. * @param CMB2_Types $types 
  25. * @param array $args 
  26. */ 
  27. public function __construct( CMB2_Types $types, $args = array() ) { 
  28. $this->types = $types; 
  29. $args['rendered'] = isset( $args['rendered'] ) ? (bool) $args['rendered'] : true; 
  30. $this->args = $args; 
  31.  
  32. /** 
  33. * Handles rendering this field type. 
  34. * @since 2.2.2 
  35. * @return string Rendered field type. 
  36. */ 
  37. abstract public function render(); 
  38.  
  39. /** 
  40. * Stores the rendered field output. 
  41. * @since 2.2.2 
  42. * @param string|CMB2_Type_Base $rendered Rendered output. 
  43. * @return string|CMB2_Type_Base Rendered output or this object. 
  44. */ 
  45. public function rendered( $rendered ) { 
  46. if ( $this->args['rendered'] ) { 
  47. return is_a( $rendered, __CLASS__ ) ? $rendered->rendered : $rendered; 
  48.  
  49. $this->rendered = is_a( $rendered, __CLASS__ ) ? $rendered->rendered : $rendered; 
  50.  
  51. return $this; 
  52.  
  53. /** 
  54. * Returns the stored rendered field output. 
  55. * @since 2.2.2 
  56. * @return string Stored rendered output (if 'rendered' argument is set to false). 
  57. */ 
  58. public function get_rendered() { 
  59. return $this->rendered; 
  60.  
  61. /** 
  62. * Handles parsing and filtering attributes while preserving any passed in via field config. 
  63. * @since 1.1.0 
  64. * @param string $element Element for filter 
  65. * @param array $type_defaults Type default arguments 
  66. * @param array $type_args Type override arguments 
  67. * @return array Parsed and filtered arguments 
  68. */ 
  69. public function parse_args( $element, $type_defaults, $type_args = array() ) { 
  70. $type_args = empty( $type_args ) ? $this->args : $type_args; 
  71.  
  72. $field_overrides = $this->field->args( 'attributes' ); 
  73.  
  74. $args = ! empty( $field_overrides ) 
  75. ? wp_parse_args( $field_overrides, $type_args ) 
  76. : $type_args; 
  77.  
  78. /** 
  79. * Filter attributes for a field type. 
  80. * The dynamic portion of the hook name, $element, refers to the field type. 
  81. * @since 1.1.0 
  82. * @param array $args The array of attribute arguments. 
  83. * @param array $type_defaults The array of default values. 
  84. * @param array $field The `CMB2_Field` object. 
  85. * @param object $field_type_object This `CMB2_Types` object. 
  86. */ 
  87. $args = apply_filters( "cmb2_{$element}_attributes", $args, $type_defaults, $this->field, $this->types ); 
  88.  
  89. return wp_parse_args( $args, $type_defaults ); 
  90.  
  91. /** 
  92. * Fall back to CMB2_Types methods 
  93. * @param string $field 
  94. * @throws Exception Throws an exception if the field is invalid. 
  95. * @return mixed 
  96. */ 
  97. public function __call( $name, $arguments ) { 
  98. switch ( $name ) { 
  99. case '_id': 
  100. case '_name': 
  101. case '_desc': 
  102. case '_text': 
  103. case 'concat_attrs': 
  104. return call_user_func_array( array( $this->types, $name ), $arguments ); 
  105. default: 
  106. throw new Exception( sprintf( esc_html__( 'Invalid %1$s method: %2$s', 'give' ), __CLASS__, $name ) ); 
  107.  
  108. /** 
  109. * Magic getter for our object. 
  110. * @param string $field 
  111. * @throws Exception Throws an exception if the field is invalid. 
  112. * @return mixed 
  113. */ 
  114. public function __get( $field ) { 
  115. switch ( $field ) { 
  116. case 'field': 
  117. return $this->types->field; 
  118. default: 
  119. throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'give' ), __CLASS__, $field ) ); 
  120.