field = $field; } /** * Default fallback. Allows rendering fields via "cmb2_render_$name" hook * @since 1.0.0 * @param string $name Non-existent method name * @param array $arguments All arguments passed to the method */ public function __call( $name, $arguments ) { /** * Pass non-existent field types through an action * * The dynamic portion of the hook name, $name, 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_$name", $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' ); } /** * 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() { $object_id = $this->field->object_id; $taxonomy = $this->field->args( 'taxonomy' ); if ( ! $post = get_post( $object_id ) ) { $cache_key = "cmb-cache-{$taxonomy}-{$object_id}"; // Check cache $cached = get_transient( $cache_key ); if ( $cached ) { return $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; } // WP caches internally so it's better to use return get_the_terms( $post, $taxonomy ); } /** * Retrieve text parameter from field's options array (if it has one), or use fallback text * @since 2.0.0 * @param string $option_key Key in field's options array * @param string $fallback Fallback text * @return string Text */ public function _text( $option_key, $fallback ) { $has_string_param = $this->field->options( $option_key ); return $has_string_param ? $has_string_param : $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 ) { $parsed = @parse_url( $file, PHP_URL_PATH ); return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false; } /** * 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 ) { $parts = explode( '/', $value ); return is_array( $parts ) ? end( $parts ) : $value; } /** * Determines if a file has a valid image extension * @since 1.0.0 * @param string $file File url * @return bool Whether file has a valid image extension */ public function is_valid_img_ext( $file ) { $file_ext = $this->get_file_ext( $file ); $is_valid_types = apply_filters( 'cmb2_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif', 'ico', 'icon' ) ); $is_valid = $file_ext && in_array( $file_ext, (array) $is_valid_types ); return (bool) apply_filters( 'cmb2_' . $this->field->id() . '_is_valid_img_ext', $is_valid, $file, $file_ext ); } /** * Handles parsing and filtering attributes while preserving any passed in via field config. * @since 1.1.0 * @param array $args Override arguments * @param string $element Element for filter * @param array $defaults Default arguments * @return array Parsed and filtered arguments */ public function parse_args( $args, $element, $defaults ) { return wp_parse_args( apply_filters( "cmb2_{$element}_attributes", $this->field->maybe_set_attributes( $args ), $defaults, $this->field, $this ), $defaults ); } /** * 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() ) { $attributes = ''; foreach ( $attrs as $attr => $val ) { $excluded = in_array( $attr, (array) $attr_exclude, true ); $empty = false === $val && 'value' !== $attr; if ( ! $excluded && ! $empty ) { // if data attribute, use single quote wraps, else double $quotes = false !== stripos( $attr, 'data-' ) ? "'" : '"'; $attributes .= sprintf( ' %1$s=%3$s%2$s%3$s', $attr, $val, $quotes ); } } return $attributes; } /** * Generates html for concatenated items * @since 1.1.0 * @param array $args Optional arguments * @return string Concatenated html items */ public function concat_items( $args = array() ) { $method = isset( $args['method'] ) ? $args['method'] : 'select_option'; unset( $args['method'] ); $value = $this->field->escaped_value() ? $this->field->escaped_value() : $this->field->args( 'default' ); $concatenated_items = ''; $i = 1; $options = array(); if ( $option_none = $this->field->args( 'show_option_none' ) ) { $options[ '' ] = $option_none; } $options = $options + (array) $this->field->options(); foreach ( $options as $opt_value => $opt_label ) { // Clone args & modify for just this item $a = $args; $a['value'] = $opt_value; $a['label'] = $opt_label; // Check if this option is the value of the input if ( $value == $opt_value ) { $a['checked'] = 'checked'; } $concatenated_items .= $this->$method( $a, $i++ ); } return $concatenated_items; } /** * Generates html for an option element * @since 1.1.0 * @param array $args Arguments array containing value, label, and checked boolean * @return string Generated option element html */ public function select_option( $args = array() ) { return sprintf( "\t" . '', $args['value'], selected( isset( $args['checked'] ) && $args['checked'], true, false ), $args['label'] ) . "\n"; } /** * Generates html for list item with input * @since 1.1.0 * @param array $args Override arguments * @param int $i Iterator value * @return string Gnerated list item html */ public function list_input( $args = array(), $i ) { $a = $this->parse_args( $args, 'list_input', array( 'type' => 'radio', 'class' => 'cmb2-option', 'name' => $this->_name(), 'id' => $this->_id( $i ), 'value' => $this->field->escaped_value(), 'label' => '', ) ); return sprintf( "\t" . '
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->args( '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 { // 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' : ''; ?>
%s', $this->textarea( array( 'class' => 'cmb2-textarea-code', 'desc' => '' . $this->_desc( true ) ) ) ); } public function wysiwyg( $args = array() ) { $a = $this->parse_args( $args, 'input', array( 'id' => $this->_id(), 'value' => $this->field->escaped_value( 'stripslashes' ), 'desc' => $this->_desc( true ), 'options' => $this->field->options(), ) ); wp_editor( $a['value'], $a['id'], $a['options'] ); echo $a['desc']; } public function text_date( $args = array() ) { $args = wp_parse_args( $args, array( 'class' => 'cmb2-text-small cmb2-datepicker', 'value' => $this->field->get_timestamp_format(), 'desc' => $this->_desc(), 'js_dependencies' => array( 'jquery-ui-core', 'jquery-ui-datepicker' ), ) ); if ( false === strpos( $args['class'], 'timepicker' ) ) { $this->parse_picker_options( 'date' ); } return $this->input( $args ); } // Alias for text_date public function text_date_timestamp( $args = array() ) { return $this->text_date( $args ); } public function text_time( $args = array() ) { $args = wp_parse_args( $args, array( 'class' => 'cmb2-timepicker text-time', 'value' => $this->field->get_timestamp_format( 'time_format' ), 'js_dependencies' => array( 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-datetimepicker' ), ) ); $this->parse_picker_options( 'time' ); return $this->text_date( $args ); } public function text_datetime_timestamp( $args = array() ) { $args = wp_parse_args( $args, array( 'value' => $this->field->escaped_value(), 'desc' => $this->_desc(), 'datepicker' => array(), 'timepicker' => array(), ) ); if ( empty( $args['value'] ) ) { $args['value'] = $this->field->escaped_value(); // This will be used if there is a select_timezone set for this field $tz_offset = $this->field->field_timezone_offset(); if ( ! empty( $tz_offset ) ) { $args['value'] -= $tz_offset; } } $has_good_value = ! empty( $args['value'] ) && ! is_array( $args['value'] ); $date_args = wp_parse_args( $args['datepicker'], array( 'class' => 'cmb2-text-small cmb2-datepicker', 'name' => $this->_name( '[date]' ), 'id' => $this->_id( '_date' ), 'value' => $has_good_value ? $this->field->get_timestamp_format( 'date_format', $args['value'] ) : '', 'desc' => '', ) ); // Let's get the date-format, and set it up as a data attr for the field. $date_args = $this->parse_picker_options( 'date', $date_args ); $time_args = wp_parse_args( $args['timepicker'], array( 'class' => 'cmb2-timepicker text-time', 'name' => $this->_name( '[time]' ), 'id' => $this->_id( '_time' ), 'value' => $has_good_value ? $this->field->get_timestamp_format( 'time_format', $args['value'] ) : '', 'desc' => $args['desc'], 'js_dependencies' => array( 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-datetimepicker' ), ) ); // Let's get the time-format, and set it up as a data attr for the field. $time_args = $this->parse_picker_options( 'time', $time_args ); return $this->input( $date_args ) . "\n" . $this->input( $time_args ); } public function text_datetime_timestamp_timezone( $args = array() ) { $args = wp_parse_args( $args, array( 'value' => $this->field->escaped_value(), 'desc' => $this->_desc( true ), 'text_datetime_timestamp' => array(), 'select_timezone' => array(), ) ); $args['value'] = $this->field->escaped_value(); if ( is_array( $args['value'] ) ) { $args['value'] = ''; } $datetime = maybe_unserialize( $args['value'] ); $value = $tzstring = ''; if ( $datetime && $datetime instanceof DateTime ) { $tz = $datetime->getTimezone(); $tzstring = $tz->getName(); $value = $datetime->getTimestamp(); } $timestamp_args = wp_parse_args( $args['text_datetime_timestamp'], array( 'desc' => '', 'value' => $value, ) ); $timezone_args = wp_parse_args( $args['select_timezone'], array( 'class' => 'cmb2_select cmb2-select-timezone', 'name' => $this->_name( '[timezone]' ), 'id' => $this->_id( '_timezone' ), 'options' => wp_timezone_choice( $tzstring ), 'desc' => $args['desc'], ) ); return $this->text_datetime_timestamp( $timestamp_args ) . "\n" . $this->select( $timezone_args ); } public function select_timezone() { $this->field->args['default'] = $this->field->args( 'default' ) ? $this->field->args( 'default' ) : cmb2_utils()->timezone_string(); return $this->select( array( 'class' => 'cmb2_select cmb2-select-timezone', 'options' => wp_timezone_choice( $this->field->escaped_value() ), 'desc' => $this->_desc(), ) ); } public function colorpicker( $args = array(), $meta_value = '' ) { $meta_value = $meta_value ? $meta_value : $this->field->escaped_value(); $hex_color = '(([a-fA-F0-9]){3}){1,2}$'; if ( preg_match( '/^' . $hex_color . '/i', $meta_value ) ) { // Value is just 123abc, so prepend # $meta_value = '#' . $meta_value; } elseif ( ! preg_match( '/^#' . $hex_color . '/i', $meta_value ) ) { // Value doesn't match #123abc, so sanitize to just # $meta_value = '#'; } wp_enqueue_style( 'wp-color-picker' ); $args = wp_parse_args( $args, array( 'class' => 'cmb2-colorpicker cmb2-text-small', 'value' => $meta_value, 'js_dependencies' => 'wp-color-picker', ) ); return $this->input( $args ); } public function title( $args = array() ) { $a = $this->parse_args( $args, 'title', array( 'tag' => $this->field->object_type == 'post' ? 'h5' : 'h3', 'class' => 'cmb2-metabox-title', 'name' => $this->field->args( 'name' ), 'desc' => $this->_desc( true ), ) ); return sprintf( '<%1$s class="%2$s">%3$s%1$s>%4$s', $a['tag'], $a['class'], $a['name'], $a['desc'] ); } public function select( $args = array() ) { $a = $this->parse_args( $args, 'select', array( 'class' => 'cmb2_select', 'name' => $this->_name(), 'id' => $this->_id(), 'desc' => $this->_desc( true ), 'options' => $this->concat_items(), ) ); $attrs = $this->concat_attrs( $a, array( 'desc', 'options' ) ); return sprintf( '%s', $attrs, $a['options'], $a['desc'] ); } public function taxonomy_select() { $names = $this->get_object_terms(); $saved_term = is_wp_error( $names ) || empty( $names ) ? $this->field->args( 'default' ) : $names[key( $names )]->slug; $terms = get_terms( $this->field->args( 'taxonomy' ), 'hide_empty=0' ); $options = ''; $option_none = $this->field->args( 'show_option_none' ); if ( ! empty( $option_none ) ) { $option_none_value = apply_filters( 'cmb2_taxonomy_select_default_value', '' ); $option_none_value = apply_filters( "cmb2_taxonomy_select_{$this->_id()}_default_value", $option_none_value ); $options .= $this->select_option( array( 'label' => $option_none, 'value' => $option_none_value, 'checked' => $saved_term == $option_none_value, ) ); } foreach ( $terms as $term ) { $options .= $this->select_option( array( 'label' => $term->name, 'value' => $term->slug, 'checked' => $saved_term === $term->slug, ) ); } return $this->select( array( 'options' => $options ) ); } public function radio( $args = array(), $type = 'radio' ) { $a = $this->parse_args( $args, $type, array( 'class' => 'cmb2-radio-list cmb2-list', 'options' => $this->concat_items( array( 'label' => 'test', 'method' => 'list_input' ) ), 'desc' => $this->_desc( true ), ) ); return sprintf( '