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

Repeatable custom field type stopped working in the latest version (2.2.6) #1054

Closed
domlavd opened this issue Nov 24, 2017 · 5 comments
Closed

Comments

@domlavd
Copy link

domlavd commented Nov 24, 2017

I made a custom field type that has 3 text input. It was working fine in the older version that I had (2.2.5.3).

Once I upgraded to the latest version, it stopped working.
Only the first iteration is saved. I can't add an other one.

@tw2113
Copy link
Contributor

tw2113 commented Nov 24, 2017

Have you checked your browser's developer console for possible js errors going on that may be playing a part in this issue?

Can you provide your CMB2 configuration so we can test things out on our end and possible help track down the issue, if we manage to recreate.

@domlavd
Copy link
Author

domlavd commented Nov 24, 2017

I don't have any js errors

Custom field type :
class CMB2_Render_CentreInfo_Field extends CMB2_Type_Base {
public static function init() {
add_filter( 'cmb2_render_class_centreinfo', array( CLASS, 'class_name' ) );
add_filter( 'cmb2_sanitize_centreinfo', array( CLASS, 'maybe_save_split_values' ), 12, 4 );
add_filter( 'cmb2_sanitize_centreinfo', array( CLASS, 'sanitize' ), 10, 5 );
add_filter( 'cmb2_types_esc_centreinfo', array( CLASS, 'escape' ), 10, 4 );
}
public static function class_name() { return CLASS; }

public function render() {
	$value = wp_parse_args( $this->field->escaped_value(), array(
		'type'			=> 'centreinfo',
		'centre-name'	=> '',
		'phone' 		=> '',
		'email'      	=> '',
	) );
	
	ob_start();
	?>
	<div class="alignleft"><p><label for="<?php echo $this->_id( '_centre_name' ); ?>"><?php echo esc_html( $this->_text( '_centre_name_text', 'Nom du centre' ) ); ?></label></p>
		<?php echo $this->types->input( array(
			'name'  => $this->_name( '[centre-name]' ),
			'id'    => $this->_id( '_centre_name' ),
			'value' => $value['centre-name'],
			'desc'  => '',
		) ); ?>
	</div>
	<div class="alignleft"><p><label for="<?php echo $this->_id( '_phone' ); ?>'"><?php echo esc_html( $this->_text( '_phone_text', 'Télépĥone' ) ); ?></label></p>
		<?php echo $this->types->input( array(
			'name'  => $this->_name( '[phone]' ),
			'class' => 'cmb_text_small',
			'id'    => $this->_id( '_phone' ),
			'value' => $value['phone'],
			'desc'  => '',
		) ); ?>
	</div>
	<div class="alignleft"><p><label for="<?php echo $this->_id( '_email' ); ?>'"><?php echo esc_html( $this->_text( '_email_text', 'Email' ) ); ?></label></p>
		<?php echo $this->types->input( array(
			'name'  => $this->_name( '[email]' ),
			'class' => 'cmb_text_small',
			'id'    => $this->_id( '_email' ),
			'value' => $value['email'],
			'desc'  => '',
		) ); ?>
	</div>
	<p class="clear">
		<?php echo $this->_desc();?>
	</p>
	<?php
	return $this->rendered( ob_get_clean() );
}

public static function maybe_save_split_values( $override_value, $value, $object_id, $field_args ) {
	if ( ! isset( $field_args['split_values'] ) || ! $field_args['split_values'] ) {
		return $override_value;
	}
	$centreinfo_keys = array( 'centre-name', 'phone', 'email' );
	foreach ( $centreinfo_keys as $key ) {
		if ( ! empty( $value[ $key ] ) ) {
			update_post_meta( $object_id, $field_args['id'] . 'centinfo_'. $key, sanitize_text_field( $value[ $key ] ) );
		}
	}
	remove_filter( 'cmb2_sanitize_address', array( __CLASS__, 'sanitize' ), 10, 5 );

	return true;
}
public static function 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);
}
public static function 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_init_centreinfo_field() {
CMB2_Render_CentreInfo_Field::init();
}
add_action( 'cmb2_init', 'cmb2_init_centreinfo_field' );

}

Post type :
function post_type_region() {

$labels = array(
	'name'                  => _x( 'Région', 'Post Type General Name', 'text_domain' ),
	'singular_name'         => _x( 'Région', 'Post Type Singular Name', 'text_domain' ),
	'menu_name'             => __( 'Région', 'text_domain' ),
	'name_admin_bar'        => __( 'Post Type', 'text_domain' ),
	'archives'              => __( 'Item Archives', 'text_domain' ),
	'attributes'            => __( 'Item Attributes', 'text_domain' ),
	'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
	'all_items'             => __( 'Toutes les régions', 'text_domain' ),
	'add_new_item'          => __( 'Ajouter une région', 'text_domain' ),
	'add_new'               => __( 'Ajouter une région', 'text_domain' ),
	'new_item'              => __( 'New Item', 'text_domain' ),
	'edit_item'             => __( 'Edit Item', 'text_domain' ),
	'update_item'           => __( 'Update Item', 'text_domain' ),
	'view_item'             => __( 'View Item', 'text_domain' ),
	'view_items'            => __( 'View Items', 'text_domain' ),
	'search_items'          => __( 'Search Item', 'text_domain' ),
	'not_found'             => __( 'Not found', 'text_domain' ),
	'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
	'featured_image'        => __( 'Featured Image', 'text_domain' ),
	'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
	'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
	'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
	'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
	'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
	'items_list'            => __( 'Items list', 'text_domain' ),
	'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
	'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
);
$args = array(
	'labels'                => $labels,
	'label'                 => __( 'Région', 'text_domain' ),
	'description'           => __( 'Région', 'text_domain' ),
	'public'                => true,
	//'rewrite'				=> array( 'slug' => 'cours', 'with_front' => true ),
	'has_archive'			=> true,
	'publicly_queryable'    => true,
	'query_var'				=> true,
	'supports'              => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
	'hierarchical'          => false,
	'menu_position'         => 2,
	'exclude_from_search'   => false,
	'show_ui'               => true,
	'show_in_menu'          => true,
	'menu_icon'             => 'dashicons-location-alt',
	'show_in_admin_bar'     => true,
	'show_in_nav_menus'     => true,
	'capability_type'       => 'post',
);
register_post_type( 'region', $args );

}
add_action( 'init', 'post_type_region', 0 );

}

function cmb2_region() {

$cmb = new_cmb2_box( array(
	'id'           => $prefix . 'centres_informations_provinciaux',
	'title'        => esc_html__( 'Centres d\'informations provinciaux', 'cmb2' ),
	'object_types' => array( 'region', ),
) );
$cmb->add_field( array(
	'id'          => 'centres_informations_provinciaux_location',
	'type'        => 'centreinfo',
	'repeatable' => true,
	'text' => array(
		'add_row_text' => 'Ajouter un autre centre d\'information',
	),
) );

}
add_action( 'cmb2_admin_init', 'cmb2_region' );

@tw2113
Copy link
Contributor

tw2113 commented Nov 24, 2017

@domlavd looks like Justin has pushed up some tweaks in regards to this. If you want to help confirm the fix, you test your instance with a copy of CMB2 trunk branch.

@jtsternberg
Copy link
Member

Just pushed the release. You should see 2.2.6.1 available in your WP dashboard.

@domlavd
Copy link
Author

domlavd commented Nov 24, 2017

It's working now.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants