Please note when checkbox is not checked then key
is not set in posted form data.
So when you uncheck show_post_count
, posted array does not has this key and in update()
function you are parsing it with default value.
$new_instance = wp_parse_args( (array) $new_instance, self::get_defaults() );
Thus $instance['show_post_count']
will have 1
always even when you uncheck it.
Solution:
Please give the priority to user input and if something is missing then fill it with default arguments. The updated function will look like!
public function update( $new_instance, $instance ) {
$instance['title'] = trim(strip_tags($new_instance['title']));
$instance['type'] = $new_instance['type'];
$instance['limit'] = intval( $new_instance['limit'] );
$instance['limit'] = $instance['limit'] === 0 ? '' : $instance['limit'];
$instance['format'] = $new_instance['format'];
//Add isset to check if key is set!
$instance['show_post_count'] = isset($new_instance['show_post_count']) ? 1 : 0;
$instance['order'] = $new_instance['order'];
//Fill with default value if any key is missing!
$updated_instance = wp_parse_args( (array) $instance, self::get_defaults() );
return $updated_instance;
}