setting = $field_object->args( 'tabs' );
$this->object_id = $object_id;
// Set layout
$layout = empty( $this->setting['layout'] ) ? 'ui-tabs-horizontal' : "ui-tabs-{$this->setting['layout']}";
$default_data = version_compare( CMB2_VERSION, '2.2.2', '>=' ) ? array(
'class' => "dtheme-cmb2-tabs $layout",
) : $field_type_object->parse_args( $field_object->data_args(), 'tabs', array(
'class' => "dtheme-cmb2-tabs $layout",
) );
// Render field
echo sprintf( '
%s
', $field_type_object->concat_attrs( $default_data, array(
'value',
'name',
'type'
) ), $this->get_tabs() );
}
/**
* Render tabs
*
* @return string
*/
public function get_tabs() {
ob_start();
?>
setting['tabs'] as $key => $tab ): ?>
setting['tabs'] as $key => $tab ): ?>
render_fields( $this->setting['config'], $tab['fields'], $this->object_id );
?>
$fields ) );
$CMB2 = new \CMB2( $setting_fields, $object_id );
foreach ( $fields as $key_field => $field ) {
if ( $CMB2->is_options_page_mb() ) {
$CMB2->object_type( $args['object_type'] );
}
// Cmb2 render field
$CMB2->render_field( $field );
}
}
/**
* Hook: Save field values
*
* @param $override_value
* @param $value
* @param $post_id
* @param $data
*/
public static function save( $override_value, $value, $post_id, $data ) {
foreach ( $data['tabs']['tabs'] as $tab ) {
$setting_fields = array_merge( $data['tabs']['config'], array( 'fields' => $tab['fields'] ) );
$CMB2 = new \CMB2( $setting_fields, $post_id );
if ( $CMB2->is_options_page_mb() ) {
$cmb2_options = cmb2_options( $post_id );
$id_fields = array_map( function( $field ) {
return $field['id'];
}, $tab['fields'] );
foreach ( $_POST as $key => $value ) {
if ( array_search( $key, $id_fields ) !== false ) {
$cmb2_options->update( $key, $value );
}
}
} else {
$CMB2->save_fields();
}
}
}
}
new CMB2_Tabs();
/**
* Type Typography
*/
// Text Transform
function nnfy_text_transform_options( $value = false ) {
$text_transform_options_list = array(
'' => esc_html__( 'Select', '99fy' ),
'lowercase' => esc_html__( 'lowercase', '99fy' ),
'uppercase' => esc_html__( 'UPPERCASE', '99fy' ),
'capitalize' => esc_html__( 'Capitalize', '99fy' ),
'none' => esc_html__( 'none', '99fy' ),
'inherit' => esc_html__( 'inherit', '99fy' ),
);
$text_transform_options = '';
foreach ( $text_transform_options_list as $text_transform_key => $text_transform ) {
$text_transform_options .= '';
}
return $text_transform_options;
}
// Font Weight
function nnfy_font_weight_options( $value = false ) {
$font_weight_options_list = array(
'' => esc_html__( 'Select', '99fy' ),
'100' => esc_html__( '100', '99fy' ),
'200' => esc_html__( '200', '99fy' ),
'300' => esc_html__( '300', '99fy' ),
'400' => esc_html__( '400', '99fy' ),
'500' => esc_html__( '500', '99fy' ),
'600' => esc_html__( '600', '99fy' ),
'700' => esc_html__( '700', '99fy' ),
'800' => esc_html__( '800', '99fy' ),
'900' => esc_html__( '900', '99fy' ),
);
$font_weight_options = '';
foreach ( $font_weight_options_list as $font_weight_key => $font_weight ) {
$font_weight_options .= '';
}
return $font_weight_options;
}
// Font Style
function nnfy_font_style_options( $value = false ) {
$font_style_options_list = array(
'' => esc_html__( 'Select', '99fy' ),
'normal' => esc_html__( 'Normal', '99fy' ),
'italic' => esc_html__( 'Italic', '99fy' ),
);
$font_style_options = '';
foreach ( $font_style_options_list as $font_style_key => $font_style ) {
$font_style_options .= '';
}
return $font_style_options;
}
// Render typography Field
function nnfy_render_typography_field_callback( $field, $value, $object_id, $object_type, $field_type ) {
// make sure we specify each part of the value we need.
$value = wp_parse_args( $value, array(
'font-family' => '',
'font-size' => '',
'line-height' => '',
'text-transform' => '',
'font-weight' => '',
'font-style' => '',
'letter-spacing' => '',
'font-style' => '',
'font-color' => '',
) );
?>
input( array(
'name' => $field_type->_name( '[font-family]' ),
'id' => $field_type->_id( '_font-family' ),
'value' => $value['font-family'],
'desc' => '',
'type' => 'text',
) ); ?>
input( array(
'name' => $field_type->_name( '[font-size]' ),
'id' => $field_type->_id( '_font-size' ),
'value' => $value['font-size'],
'desc' => '',
'type' => 'text',
) ); ?>
input( array(
'name' => $field_type->_name( '[line-height]' ),
'id' => $field_type->_id( '_line-height' ),
'value' => $value['line-height'],
'desc' => '',
'type' => 'text',
) ); ?>
input( array(
'name' => $field_type->_name( '[letter-spacing]' ),
'id' => $field_type->_id( '_letter-spacing' ),
'value' => $value['letter-spacing'],
'desc' => '',
'type' => 'text',
) ); ?>
select( array(
'name' => $field_type->_name( '[text-transform]' ),
'id' => $field_type->_id( '_text-transform' ),
'value' => $value['text-transform'],
'desc' => '',
'options' => nnfy_text_transform_options( $value['text-transform'] ),
) ); ?>
select( array(
'name' => $field_type->_name( '[font-weight]' ),
'id' => $field_type->_id( '_font-weight' ),
'value' => $value['font-weight'],
'desc' => '',
'options' => nnfy_font_weight_options( $value['font-weight'] ),
) ); ?>
select( array(
'name' => $field_type->_name( '[font-style]' ),
'id' => $field_type->_id( '_font-style' ),
'value' => $value['font-style'],
'desc' => '',
'options' => nnfy_font_style_options( $value['font-style'] ),
) ); ?>
input( array(
'name' => $field_type->_name( '[font-color]' ),
'id' => $field_type->_id( '_font-color' ),
'value' => $value['font-color'],
'desc' => '',
'type' => 'text',
'class' => 'cmb2-colorpicker',
'data-alpha' => 'true'
) ); ?>
_desc( true );
}
add_filter( 'cmb2_render_typography', 'nnfy_render_typography_field_callback', 10, 5 );
/**
* Type Background
*/
// Background Repeat
function nnfy_background_repeat_options( $value = false ) {
$background_repeat_options_list = array(
'' => esc_html__( 'Select', '99fy' ),
'repeat' => esc_html__( 'Repeat', '99fy' ),
'repeat-x' => esc_html__( 'Repeat-X', '99fy' ),
'repeat-y' => esc_html__( 'Repeat-Y', '99fy' ),
'no-repeat' => esc_html__( 'No-Repeat', '99fy' ),
'initial' => esc_html__( 'Initial', '99fy' ),
'inherit' => esc_html__( 'Inherit', '99fy' ),
);
$background_repeat_options = '';
foreach ( $background_repeat_options_list as $bg_repeat_key => $background_repeat ) {
$background_repeat_options .= '';
}
return $background_repeat_options;
}
// Background Size
function nnfy_background_size_options( $value = false ) {
$background_size_options_list = array(
'' => esc_html__( 'Select', '99fy' ),
'cover' => esc_html__( 'Cover', '99fy' ),
'contain' => esc_html__( 'Contain', '99fy' ),
'inherit' => esc_html__( 'Inherit', '99fy' ),
'initial' => esc_html__( 'Initial', '99fy' ),
'unset' => esc_html__( 'Unset', '99fy' ),
);
$background_size_options = '';
foreach ( $background_size_options_list as $bg_size_key => $background_size ) {
$background_size_options .= '';
}
return $background_size_options;
}
// Background Attachment
function nnfy_background_attachment_options( $value = false ) {
$background_attachment_options_list = array(
'' => esc_html__( 'Select', '99fy' ),
'scroll' => esc_html__( 'Scroll', '99fy' ),
'fixed' => esc_html__( 'Fixed', '99fy' ),
'local' => esc_html__( 'Local', '99fy' ),
'inherit' => esc_html__( 'Inherit', '99fy' ),
'initial' => esc_html__( 'Initial', '99fy' ),
'unset' => esc_html__( 'Unset', '99fy' ),
);
$background_attachment_options = '';
foreach ( $background_attachment_options_list as $bg_attachment_key => $background_attachment ) {
$background_attachment_options .= '';
}
return $background_attachment_options;
}
// Background Position
function nnfy_background_position_options( $value = false ) {
$background_position_options_list = array(
'' => esc_html__( 'Select', '99fy' ),
'left top' => esc_html__( 'Left Top', '99fy' ),
'left center' => esc_html__( 'Left Center', '99fy' ),
'left bottom' => esc_html__( 'Left Bottom', '99fy' ),
'center top' => esc_html__( 'Center Top', '99fy' ),
'center center' => esc_html__( 'Center Center', '99fy' ),
'center bottom' => esc_html__( 'Center Bottom', '99fy' ),
'right top' => esc_html__( 'Right Top', '99fy' ),
'right center' => esc_html__( 'Right Center', '99fy' ),
'right bottom' => esc_html__( 'Right Bottom', '99fy' ),
);
$background_position_options = '';
foreach ( $background_position_options_list as $bg_position_key => $background_position ) {
$background_position_options .= '';
}
return $background_position_options;
}
// Render Background Field
function nnfy_render_background_field_callback( $field, $value, $object_id, $object_type, $field_type ) {
// make sure we specify each part of the value we need.
$value = wp_parse_args( $value, array(
'background-color' => '',
'background-image' => '',
'background-repeat' => '',
'background-size' => '',
'background-attachment' => '',
'background-position' => '',
) );
?>
input( array(
'name' => $field_type->_name( '[background-color]' ),
'id' => $field_type->_id( '_background-color' ),
'value' => $value['background-color'],
'desc' => '',
'type' => 'text',
'class' => 'cmb2-colorpicker',
'data-alpha' => 'true',
) ); ?>
select( array(
'name' => $field_type->_name( '[background-repeat]' ),
'id' => $field_type->_id( '_background-repeat' ),
'value' => $value['background-repeat'],
'desc' => '',
'class' => 'nnfy-select',
'options' => nnfy_background_repeat_options( $value['background-repeat'] ),
) ); ?>
select( array(
'name' => $field_type->_name( '[background-size]' ),
'id' => $field_type->_id( '_background-size' ),
'value' => $value['background-size'],
'desc' => '',
'class' => 'nnfy-select',
'options' => nnfy_background_size_options( $value['background-size'] ),
) ); ?>
select( array(
'name' => $field_type->_name( '[background-attachment]' ),
'id' => $field_type->_id( '_background-attachment' ),
'value' => $value['background-attachment'],
'desc' => '',
'class' => 'nnfy-select',
'options' => nnfy_background_attachment_options( $value['background-attachment'] ),
) ); ?>
select( array(
'name' => $field_type->_name('[background-position]'),
'id' => $field_type->_id('_background-position'),
'value' => $value['background-position'],
'desc' => '',
'class' => 'nnfy-select',
'options' => nnfy_background_position_options( $value['background-position'] ),
) ); ?>
input( array(
'type' => 'text',
'name' => $field_type->_name('[background-image]'),
'id' => $field_type->_id('_background-image'),
'class' => 'cmb2-upload-file regular-text',
'size' => '45',
'desc' => '',
'class' => 'nnfy-select',
'data-previewsize' => '[350,350]',
'data-sizename' => 'large',
'data-queryargs' => '',
'value' => $value['background-image'],
) ); ?>
input( array(
'type' => 'button',
'class' => 'cmb2-upload-button button-secondary',
'desc' => '',
'value' => esc_html__( 'Add or Upload File', '99fy'),
) ); ?>
input( array(
'type' => 'hidden',
'name' => $field_type->_name( '_background-image-hidden' ),
'id' => $field_type->_id( '_background-image-hidden' ),
'class' => 'cmb2-upload-file-id',
'value' => '',
'desc' => '',
) ); ?>
_desc( true );
}
add_filter( 'cmb2_render_background', 'nnfy_render_background_field_callback', 10, 5 );
/**
* Type Padding
*/
function nnfy_render_padding_field_callback( $field, $value, $object_id, $object_type, $field_type ) {
// make sure we specify each part of the value we need.
$value = wp_parse_args( $value, array(
'padding-top' => '',
'padding-right' => '',
'padding-bottom' => '',
'padding-left' => '',
) );
?>
input( array(
'name' => $field_type->_name( '[padding-top]' ),
'id' => $field_type->_id( '_padding-top' ),
'value' => $value['padding-top'],
'desc' => '',
'type' => 'text',
) ); ?>
input( array(
'name' => $field_type->_name( '[padding-right]' ),
'id' => $field_type->_id( '_padding-right' ),
'value' => $value['padding-right'],
'desc' => '',
'type' => 'text',
) ); ?>
input( array(
'name' => $field_type->_name( '[padding-bottom]' ),
'id' => $field_type->_id( '_padding-bottom' ),
'value' => $value['padding-bottom'],
'desc' => '',
'type' => 'text',
) ); ?>
input( array(
'name' => $field_type->_name( '[padding-left]' ),
'id' => $field_type->_id( '_padding-left' ),
'value' => $value['padding-left'],
'desc' => '',
'type' => 'text',
) ); ?>
_desc( true );
}
add_filter( 'cmb2_render_padding', 'nnfy_render_padding_field_callback', 10, 5 );
/**
* Type Margin
*/
function nnfy_render_margin_field_callback( $field, $value, $object_id, $object_type, $field_type ) {
// make sure we specify each part of the value we need.
$value = wp_parse_args( $value, array(
'margin-top' => '',
'margin-right' => '',
'margin-bottom' => '',
'margin-left' => '',
) );
?>
input( array(
'name' => $field_type->_name( '[margin-top]' ),
'id' => $field_type->_id( '_margin-top' ),
'value' => $value['margin-top'],
'desc' => '',
'type' => 'text',
) ); ?>
input( array(
'name' => $field_type->_name( '[margin-right]' ),
'id' => $field_type->_id( '_margin-right' ),
'value' => $value['margin-right'],
'desc' => '',
'type' => 'text',
) ); ?>
input( array(
'name' => $field_type->_name( '[margin-bottom]' ),
'id' => $field_type->_id( '_margin-bottom' ),
'value' => $value['margin-bottom'],
'desc' => '',
'type' => 'text',
) ); ?>
input( array(
'name' => $field_type->_name( '[margin-left]' ),
'id' => $field_type->_id( '_margin-left' ),
'value' => $value['margin-left'],
'desc' => '',
'type' => 'text',
) ); ?>
_desc( true );
}
add_filter( 'cmb2_render_margin', 'nnfy_render_margin_field_callback', 10, 5 );
/**
* Image Select
*/
function cmb2_render_image_select( $field, $escaped_value, $object_id, $object_type, $field_type_object ) {
$conditional_value =(isset($field->args['attributes']['data-conditional-value'])?'data-conditional-value="' .esc_attr($field->args['attributes']['data-conditional-value']).'"':'');
$conditional_id =(isset($field->args['attributes']['data-conditional-id'])?' data-conditional-id="'.esc_attr($field->args['attributes']['data-conditional-id']).'"':'');
$default_value = $field->args['default'];
$image_select = '';
$image_select .= $field_type_object->_desc( true );
echo $image_select;
}add_action( 'cmb2_render_image_select', 'cmb2_render_image_select', 10, 5 );
/**
* Type hash_colorpicker
*/
function nnfy_render_hash_colorpicker_field_callback( $field, $value, $object_id, $object_type, $field_type ) {
// make sure we specify each part of the value we need.
$value = wp_parse_args( $value, array(
'color' => '',
) );
?>
input( array(
'name' => $field_type->_name( '[color]' ),
'id' => $field_type->_id( '_color' ),
'value' => $value['color'],
'desc' => '',
'type' => 'text',
'class' => 'cmb2-colorpicker',
'data-alpha' => 'true'
) ); ?>
_desc( true );
}
add_filter( 'cmb2_render_hash_colorpicker', 'nnfy_render_hash_colorpicker_field_callback', 10, 5 );
/**
* Conditional Fields
*/
if ( ! class_exists( 'nnfy_cmb2_conditionals' ) ) {
/**
* nnfy_cmb2_conditionals Plugin.
*/
class nnfy_cmb2_conditionals {
/**
* Priority on which our actions are hooked in.
*
* @const int
*/
const PRIORITY = 99999;
/**
* Version number of the plugin.
*
* @const string
*/
const VERSION = '1.0.4';
/**
* CMB2 Form elements which can be set to "required".
*
* @var array
*/
protected $nnfy_maybe_required_form_elms = array(
'list_input',
'input',
'textarea',
'input',
'select',
'checkbox',
'radio',
'radio_inline',
'taxonomy_radio',
'taxonomy_multicheck',
'multicheck_inline',
);
/**
* Constructor - Set up the actions for the plugin.
*/
public function __construct() {
if ( ! defined( 'CMB2_LOADED' ) || false === CMB2_LOADED ) {
return;
}
add_action( 'admin_init', array( $this, 'admin_init' ), self::PRIORITY );
foreach ( $this->nnfy_maybe_required_form_elms as $element ) {
add_filter( "cmb2_{$element}_attributes", array( $this, 'nnfy_maybe_set_required_attribute' ), self::PRIORITY );
}
}
/**
* Ensure valid html for the required attribute.
*
* @param array $args Array of HTML attributes.
*
* @return array
*/
public function nnfy_maybe_set_required_attribute( $args ) {
if ( ! isset( $args['required'] ) ) {
return $args;
}
// Comply with HTML specs.
if ( true === $args['required'] ) {
$args['required'] = 'required';
}
return $args;
}
/**
* Hook in the filtering of the data being saved.
*/
public function admin_init() {
$cmb2_boxes = CMB2_Boxes::get_all();
foreach ( $cmb2_boxes as $cmb_id => $cmb2_box ) {
add_action(
"cmb2_{$cmb2_box->object_type()}_process_fields_{$cmb_id}",
array( $this, 'filter_data_to_save' ),
self::PRIORITY,
2
);
}
}
/**
* Filter the data received from the form in order to remove those values
* which are not suppose to be enabled to edit according to the declared conditionals.
*
* @param \CMB2 $cmb2 An instance of the CMB2 class.
* @param int $object_id The id of the object being saved, could post_id, comment_id, user_id.
*
* The potentially adjusted array is returned via reference $cmb2.
*/
public function filter_data_to_save( CMB2 $cmb2, $object_id ) {
foreach ( $cmb2->prop( 'fields' ) as $field_args ) {
if ( ! ( 'group' === $field_args['type'] || ( array_key_exists( 'attributes', $field_args ) && array_key_exists( 'data-conditional-id', $field_args['attributes'] ) ) ) ) {
continue;
}
if ( 'group' === $field_args['type'] ) {
foreach ( $field_args['fields'] as $group_field ) {
if ( ! ( array_key_exists( 'attributes', $group_field ) && array_key_exists( 'data-conditional-id', $group_field['attributes'] ) ) ) {
continue;
}
$field_id = $group_field['id'];
$conditional_id = $group_field['attributes']['data-conditional-id'];
$decoded_conditional_id = @json_decode( $conditional_id );
if ( $decoded_conditional_id ) {
$conditional_id = $decoded_conditional_id;
}
if ( is_array( $conditional_id ) && ! empty( $conditional_id ) && ! empty( $cmb2->data_to_save[ $conditional_id[0] ] ) ) {
foreach ( $cmb2->data_to_save[ $conditional_id[0] ] as $key => $group_data ) {
$cmb2->data_to_save[ $conditional_id[0] ][ $key ] = $this->filter_field_data_to_save( $group_data, $field_id, $conditional_id[1], $group_field['attributes'] );
}
}
continue;
}
} else {
$field_id = $field_args['id'];
$conditional_id = $field_args['attributes']['data-conditional-id'];
$cmb2->data_to_save = $this->filter_field_data_to_save( $cmb2->data_to_save, $field_id, $conditional_id, $field_args['attributes'] );
}
}
}
/**
* Determine if the data for one individual field should be saved or not.
*
* @param array $data_to_save The received $_POST data.
* @param string $field_id The CMB2 id of this field.
* @param string $conditional_id The CMB2 id of the field this field is conditional on.
* @param array $attributes The CMB2 field attributes.
*
* @return array Array of data to save.
*/
protected function filter_field_data_to_save( $data_to_save, $field_id, $conditional_id, $attributes ) {
if ( array_key_exists( 'data-conditional-value', $attributes ) ) {
$conditional_value = $attributes['data-conditional-value'];
$decoded_conditional_value = @json_decode( $conditional_value );
if ( $decoded_conditional_value ) {
$conditional_value = $decoded_conditional_value;
}
if ( ! isset( $data_to_save[ $conditional_id ] ) ) {
if ( 'off' !== $conditional_value ) {
unset( $data_to_save[ $field_id ] );
}
return $data_to_save;
}
if ( ( ! is_array( $conditional_value ) && ! is_array( $data_to_save[ $conditional_id ] ) ) && $data_to_save[ $conditional_id ] != $conditional_value ) {
unset( $data_to_save[ $field_id ] );
return $data_to_save;
}
if ( is_array( $conditional_value ) || is_array( $data_to_save[ $conditional_id ] ) ) {
$match = array_intersect( (array) $conditional_value, (array) $data_to_save[ $conditional_id ] );
if ( empty( $match ) ) {
unset( $data_to_save[ $field_id ] );
return $data_to_save;
}
}
}
if ( ! isset( $data_to_save[ $conditional_id ] ) || ! $data_to_save[ $conditional_id ] ) {
unset( $data_to_save[ $field_id ] );
}
return $data_to_save;
}
} /* End of class. */
if ( ! function_exists( 'cmb2_conditionals_init' ) ) {
/**
* Initialize the class.
*/
function cmb2_conditionals_init() {
$cmb2_conditionals = new nnfy_cmb2_conditionals();
}
}
} /* End of class-exists wrapper. */