Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds nested tab support. This allows fields in nested tabs to be saved. #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion inc/cmb2-tabs.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@ public function render_fields( $args, $fields, $object_id ) {
}


/**
* Recursively checks if a field has tabs, if it does, retrieve all of the fields from each tab, then repeat
*
* @param $field
*/
public static function get_fields_from_nested_tabs( $field ) {
$fields = array();

if ( isset( $field['tabs'] ) ) {
foreach ( $field['tabs']['tabs'] as $tab_field_tab ) {
if ( isset( $tab_field_tab['fields'] ) ) {
foreach ( $tab_field_tab['fields'] as $tab_field_tab_field ) {
$fields[] = $tab_field_tab_field;

$fields_in_nested_tabs = self::get_fields_from_nested_tabs( $tab_field_tab_field );

foreach ( $fields_in_nested_tabs as $field_in_nested_tabs ) {
$fields[] = $field_in_nested_tabs;
}
}
}
}
}

return $fields;
}


/**
* Hook: Save field values
*
Expand All @@ -116,10 +144,23 @@ public static function save( $override_value, $value, $post_id, $data ) {
$CMB2 = new \CMB2( $setting_fields, $post_id );

if ( $CMB2->is_options_page_mb() ) {
$fields_to_check = array();

foreach ( $tab['fields'] as $tab_field ) {
$fields_to_check[] = $tab_field;

$fields_in_nested_tabs = self::get_fields_from_nested_tabs( $tab_field );

foreach ( $fields_in_nested_tabs as $field_in_nested_tabs ) {
$fields_to_check[] = $field_in_nested_tabs;
}
}


$cmb2_options = cmb2_options( $post_id );
$id_fields = array_map( function( $field ) {
return $field['id'];
}, $tab['fields'] );
}, $fields_to_check );

foreach ( $_POST as $key => $value ) {
if ( array_search( $key, $id_fields ) !== false ) {
Expand Down