field = $field; } /** * Default fallback. Allows rendering fields via "cmb2_render_$fieldtype" hook * @since 1.0.0 * @param string $fieldtype Non-existent field type name * @param array $arguments All arguments passed to the method */ public function __call( $fieldtype, $arguments ) { // Check for methods to be proxied to the CMB2_Type_Base object. if ( $exists = $this->maybe_proxy_method( $fieldtype, $arguments ) ) { return $exists['value']; } // Check for custom field type class. if ( $object = $this->maybe_custom_field_object( $fieldtype, $arguments ) ) { return $object->render(); } /** * Pass non-existent field types through an action. * * The dynamic portion of the hook name, $fieldtype, refers to the field type. * * @param array $field The passed in `CMB2_Field` object * @param mixed $escaped_value The value of this field escaped. * It defaults to `sanitize_text_field`. * If you need the unescaped value, you can access it * via `$field->value()` * @param int $object_id The ID of the current object * @param string $object_type The type of object you are working with. * Most commonly, `post` (this applies to all post-types), * but could also be `comment`, `user` or `options-page`. * @param object $field_type_object This `CMB2_Types` object */ do_action( "cmb2_render_{$fieldtype}", $this->field, $this->field->escaped_value(), $this->field->object_id, $this->field->object_type, $this ); } /** * Render a field (and handle repeatable) * @since 1.1.0 */ public function render() { if ( $this->field->args( 'repeatable' ) ) { $this->render_repeatable_field(); } else { $this->_render(); } } /** * Render a field type * @since 1.1.0 */ protected function _render() { $this->field->peform_param_callback( 'before_field' ); echo $this->{$this->field->type()}(); $this->field->peform_param_callback( 'after_field' ); } /** * Proxies the method call to the CMB2_Type_Base object, if it exists, otherwise returns a default fallback value. * * @since 2.2.2 * * @param string $method Method to call on the CMB2_Type_Base object. * @param mixed $default Default fallback value if method is not found. * @param array $args Optional arguments to pass to proxy method. * * @return mixed Results from called method. */ protected function proxy_method( $method, $default, $args = array() ) { if ( ! is_object( $this->type ) ) { $this->guess_type_object( $method ); } if ( is_object( $this->type ) && method_exists( $this->type, $method ) ) { return empty( $args ) ? $this->type->$method() : call_user_func_array( array( $this->type, $method ), $args ); } return $default; } /** * If no CMB2_Types::$type object is initiated when a proxy method is called, it means * it's a custom field type (which SHOULD be instantiating a Type), but let's try and * guess the type object for them and instantiate it. * * @since 2.2.3 * * @param string $method Method attempting to be called on the CMB2_Type_Base object. */ protected function guess_type_object( $method ) { $fieldtype = $this->field->type(); // Try to "guess" the Type object based on the method requested. switch ( $method ) { case 'select_option': case 'list_input': case 'list_input_checkbox': case 'concat_items': $this->get_new_render_type( $fieldtype, 'CMB2_Type_Select' ); break; case 'is_valid_img_ext': case 'img_status_output': case 'file_status_output': $this->get_new_render_type( $fieldtype, 'CMB2_Type_File_Base' ); break; case 'parse_picker_options': $this->get_new_render_type( $fieldtype, 'CMB2_Type_Text_Date' ); break; case 'get_object_terms': case 'get_terms': $this->get_new_render_type( $fieldtype, 'CMB2_Type_Taxonomy_Multicheck' ); break; case 'date_args': case 'time_args': $this->get_new_render_type( $fieldtype, 'CMB2_Type_Text_Datetime_Timestamp' ); break; case 'parse_args': $this->get_new_render_type( $fieldtype, 'CMB2_Type_Text' ); break; } return null !== $this->type; } /** * Check for methods to be proxied to the CMB2_Type_Base object. * @since 2.2.4 * @param string $method The possible method to proxy. * @param array $arguments All arguments passed to the method. * @return bool|array False if not proxied, else array with 'value' key being the return of the method. */ public function maybe_proxy_method( $method, $arguments ) { $exists = false; $proxied = array( 'get_object_terms' => array(), 'is_valid_img_ext' => false, 'parse_args' => array(), 'concat_items' => '', 'select_option' => '', 'list_input' => '', 'list_input_checkbox' => '', 'img_status_output' => '', 'file_status_output' => '', 'parse_picker_options' => array(), ); if ( isset( $proxied[ $method ] ) ) { $exists = array( // Ok, proxy the method call to the CMB2_Type_Base object. 'value' => $this->proxy_method( $method, $proxied[ $method ], $arguments ), ); } return $exists; } /** * Checks for a custom field CMB2_Type_Base class to use for rendering. * @since 2.2.4 * @param string $fieldtype Non-existent field type name * @param array $args Optional field arguments. * @return CMB2_Type_Base Type object. */ public function maybe_custom_field_object( $fieldtype, $args = array() ) { if ( $render_class_name = $this->get_render_type_class( $fieldtype ) ) { $this->type = new $render_class_name( $this, $args ); if ( ! ( $this->type instanceof CMB2_Type_Base ) ) { throw new Exception( __( 'Custom CMB2 field type classes must extend CMB2_Type_Base.', 'ascend' ) ); } } return $this->type; } /** * Gets the render type CMB2_Type_Base object to use for rendering the field. * @since 2.2.4 * @param string $fieldtype The type of field being rendered. * @param string $render_class_name The default field type class to use. Defaults to null. * @param array $args Optional arguments to pass to type class. * @param mixed $additional Optional additional argument to pass to type class. * @return CMB2_Type_Base Type object. */ public function get_new_render_type( $fieldtype, $render_class_name = null, $args = array(), $additional = '' ) { $render_class_name = $this->get_render_type_class( $fieldtype, $render_class_name ); $this->type = new $render_class_name( $this, $args, $additional ); return $this->type; } /** * Checks for the render type class to use for rendering the field. * @since 2.2.4 * @param string $fieldtype The type of field being rendered. * @param string $render_class_name The default field type class to use. Defaults to null. * @return string The field type class to use. */ public function get_render_type_class( $fieldtype, $render_class_name = null ) { $render_class_name = $this->field->args( 'render_class' ) ? $this->field->args( 'render_class' ) : $render_class_name; if ( has_action( "cmb2_render_class_{$fieldtype}" ) ) { /** * Filters the custom field type class used for rendering the field. Class is required to extend CMB2_Type_Base. * * The dynamic portion of the hook name, $fieldtype, refers to the (custom) field type. * * @since 2.2.4 * * @param string $render_class_name The custom field type class to use. Default null. * @param object $field_type_object This `CMB2_Types` object. */ $render_class_name = apply_filters( "cmb2_render_class_{$fieldtype}", $render_class_name, $this ); } return $render_class_name && class_exists( $render_class_name ) ? $render_class_name : false; } /** * Retrieve text parameter from field's options array (if it has one), or use fallback text * @since 2.0.0 * @param string $text_key Key in field's options array * @param string $fallback Fallback text * @return string Text */ public function _text( $text_key, $fallback = '' ) { return $this->field->get_string( $text_key, $fallback ); } /** * Determine a file's extension * @since 1.0.0 * @param string $file File url * @return string|false File extension or false */ public function get_file_ext( $file ) { return CMB2_Utils::get_file_ext( $file ); } /** * Get the file name from a url * @since 2.0.0 * @param string $value File url or path * @return string File name */ public function get_file_name_from_path( $value ) { return CMB2_Utils::get_file_name_from_path( $value ); } /** * Combines attributes into a string for a form element * @since 1.1.0 * @param array $attrs Attributes to concatenate * @param array $attr_exclude Attributes that should NOT be concatenated * @return string String of attributes for form element */ public function concat_attrs( $attrs, $attr_exclude = array() ) { return CMB2_Utils::concat_attrs( $attrs, $attr_exclude ); } /** * Generates repeatable field table markup * @since 1.0.0 */ public function render_repeatable_field() { $table_id = $this->field->id() . '_repeat'; $this->_desc( true, true, true ); ?>
iterator = 0; } /** * Generates repeatable field rows * @since 1.1.0 */ public function repeatable_rows() { $meta_value = array_filter( (array) $this->field->escaped_value() ); // check for default content $default = $this->field->get_default(); // check for saved data if ( ! empty( $meta_value ) ) { $meta_value = is_array( $meta_value ) ? array_filter( $meta_value ) : $meta_value; $meta_value = ! empty( $meta_value ) ? $meta_value : $default; } else { $meta_value = $default; } // Loop value array and add a row if ( ! empty( $meta_value ) ) { $count = count( $meta_value ); foreach ( (array) $meta_value as $val ) { $this->field->escaped_value = $val; $this->repeat_row( $count < 2 ); $this->iterator++; } } else { // If value is empty (including empty array), then clear the value. $this->field->escaped_value = $this->field->value = null; // Otherwise add one row $this->repeat_row( true ); } // Then add an empty row $this->field->escaped_value = ''; $this->iterator = $this->iterator ? $this->iterator : 1; $this->repeat_row( false, 'empty-row hidden' ); } /** * Generates a repeatable row's markup * @since 1.1.0 * @param bool $disable_remover Whether remove button should be disabled * @param string $class Repeatable table row's class */ protected function repeat_row( $disable_remover = false, $class = 'cmb-repeat-row' ) { $disabled = $disable_remover ? ' button-disabled' : ''; ?>