CMB2自定义可重复字段丢失数据,如何修复?

时间:2017-06-10 05:04:37

标签: php wordpress cmb2

我有一个名为pb_info的自定义cmb2字段,它由4个不同的字段缩写(选择),课程代码(文本),pe点(数字),pe小时(数字)组成。我想在可重复的组内使用它作为组字段。我的代码工作正常,直到我插入2行数据,但它错过了第2行,这意味着从第3行它丢失数据。当我检查它时,我从第3行看到输入字段的id没有改变。

这是我的代码:

注册字段行为:

 function cmb2_render_pb_info_field_callback( $field, $value, $object_id, $object_type, $field_type ) {
    $value = wp_parse_args( $value, array(
    'pb_abbreviation' => '',
    'pb_crs_cd' => '',
    'pe_pnt'  => '',
    'pe_hr'     => '',
    ) );

?>
<div class="alignleft"><p><label for="<?php echo $field_type->_id( '_pb_abbreviation' ); ?>'">PB Abbreviation</label></p>
    <?php 
        echo $field_type->select( array(
        'name'    => $field_type->_name( '[pb_abbreviation]' ),
        'id'      => $field_type->_id( '_pb_abbreviation' ),
        'options' => cmb2_get_abbr_options( $value['pb_abbreviation'] ),
        'desc'    => '',

        ) ); 
    ?>
</div>
<div class="alignleft use_maxlength_validator"><p><label for="<?php echo $field_type->_id( '_pb_crs_cd' ); ?>'">PB Course Code</label></p>
    <?php echo $field_type->input( array(
        'class' => 'cmb_text_small',
        'name'  => $field_type->_name( '[pb_crs_cd]' ),
        'id'    => $field_type->_id( '_pb_crs_cd' ),
        'value' => $value['pb_crs_cd'],
        'maxlength' => 20,
        'desc'  => '',
    ) ); ?>
</div>
<div class="alignleft pb_credit"><p><label for="<?php echo $field_type->_id( '_pe_pnt' ); ?>'">PE Point</label></p>
    <?php echo $field_type->input( array(
        'class' => 'cmb_text_small',
        'name'  => $field_type->_name( '[pe_pnt]' ),
        'id'    => $field_type->_id( '_pe_pnt' ),
        'value' => $value['pe_pnt'],
        'desc'  => '',
    ) ); ?>
</div>
<div class="alignleft pb_hour"><p><label for="<?php echo $field_type->_id( '_pe_hr' ); ?>'">PE Hour</label></p>
    <?php echo $field_type->input( array(
        'class' => 'cmb_text_small',
        'name'  => $field_type->_name( '[pe_hr]' ),
        'id'    => $field_type->_id( '_pe_hr' ),
        'value' => $value['pe_hr'],
        'desc'  => '',
    ) ); ?>
</div>
<br class="clear">
<?php
    echo $field_type->_desc( true );

}
add_filter( 'cmb2_render_pb_info', 'cmb2_render_pb_info_field_callback', 10, 5 );
add_filter( 'cmb2_sanitize_pb_info', 'maybe_save_split_pb_values', 12, 4 );
/**
    * The following snippets are required for allowing the pb_info field
    * to work as a repeatable field, or in a repeatable group
*/
add_filter( 'cmb2_sanitize_pb_info','pb_sanitize', 10, 5 );
add_filter( 'cmb2_types_esc_pb_info', 'pb_escape', 10, 4 );


/**
    * Optionally save the Pb Info values into separate fields
*/

function pb_sanitize( $check, $meta_value, $object_id, $field_args, $sanitize_object ) {
    if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) { return $check;  }
    foreach ( $meta_value as $key => $val ) {
        $meta_value[ $key ] = array_filter( array_map( 'sanitize_text_field', $val ) );
    }
    return array_filter($meta_value);
}

function maybe_save_split_pb_values( $override_value, $value, $object_id, $field_args ) {

    foreach ($override_value as $key => $data) {
        if($data['pb_crs_cd'] == '' && $data['pe_hr'] == '' && $data['pe_pnt'] == '' ){
            unset($override_value[$key]);
        }
    }
    if ( ! isset( $field_args['split_values'] ) || ! $field_args['split_values'] ) {
        // Don't do the override
        return $override_value;
    }
    foreach ($value as $key => $data) {
        if($data['pb_crs_cd'] == '' && $data['pe_hr'] == '' && $data['pe_pnt'] == '' ){
            unset($value[$key]);
        }
    }
    $pb_info_keys = array( 'pb_abbreviation', 'pb_crs_cd', 'pe_pnt', 'pe_hr' );
    foreach ( $pb_info_keys as $key ) {
        if ( ! empty( $value[ $key ] ) ) {
            update_post_meta( $object_id, $field_args['id'] . 'addr_'. $key, sanitize_text_field( $value[ $key ] ) );
        }
    }
    remove_filter( 'cmb2_sanitize__pb_info', 'pb_sanitize', 10, 5 );
    return true;
}

function pb_escape( $check, $meta_value, $field_args, $field_object ) {
    if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) { return $check;  }
    foreach ( $meta_value as $key => $val ) { $meta_value[ $key ] = array_filter( array_map( 'esc_attr', $val ) );  }
    return array_filter($meta_value);
}

function cmb2_get_abbr_options( $value = false ) {
    $terms = get_terms( 'professional-body', array( 'hide_empty' => false, ) );
    $pb_abbreviation = '';
    $meta_key = "abbreviation";
    foreach ( $terms  as $term ) {
        $pb_abbreviation .= '<option value="'.  $term->term_id .'" '. selected( $value, $term->term_id, false ) .'>'. get_pb_meta( $term->term_id, $meta_key) .'</option>';
    }
    return $pb_abbreviation;
}

字段初始化为:

$lu = new_cmb2_box( array(
    'id' => $prefix.'mcptr{#}',
    'title' => _('Round Informations'),
    'object_types' => array('mcpt'),
    'context'       => 'normal',
    'priority'      => 'high',
    'show_names'    => true,
    ));
    $group_field_id = $lu->add_field( array(
    'id'          => $prefix.'mcptre',
    'class'       => $prefix.'mcptgrp',
    'type'        => 'group',
    'repeatable'  => true, 
    'options'     => array(
    'group_title'   => __( 'Rounds {#}', 'cmb2' ),
    'closed'     => false,
    'add_button'    => __( 'Another Round', 'cmb2' ),
    'remove_button' => __( 'Remove Round', 'cmb2' ),
    'sortable'      => true,
    ),
    ) );

   $lu->add_group_field( $group_field_id, array(
    'name'             => 'PB Info',
    'id'               => $prefix.'pb_info_field',
    'type'              => 'pb_info',
    'repeatable'       => true,
    'classes' => 'pb_lu_info'
    ) );

那么我怎么能解决这个问题,我从来没有提到要在第2行或第3行做一些特别的事情,但不知怎的,它需要2行值,而在第3行之后它会停止更改id。

提前致谢。

0 个答案:

没有答案