Skip to content

Commit

Permalink
Allow "select" with "multiple" attribute can be cloned
Browse files Browse the repository at this point in the history
See previous commit for same notes about saving meta value
  • Loading branch information
rilwis committed Oct 13, 2012
1 parent 8fcd586 commit a3362c8
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions inc/fields/select.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,68 @@ static function html( $html, $meta, $field )

return $html;
}

/**
* Get meta value
* If field is cloneable, value is saved as a single entry in DB
* Otherwise value is saved as multiple entries (for backward compatibility)
*
* @see "save" method for better understanding
*
* TODO: A good way to ALWAYS save values in single entry in DB, while maintaining backward compatibility
*
* @param $meta
* @param $post_id
* @param $saved
* @param $field
*
* @return array
*/
static function meta( $meta, $post_id, $saved, $field )
{
$single = $field['clone'] || !$field['multiple'];
return (array) get_post_meta( $post_id, $field['id'], $single );
}

/**
* Save meta value
* If field is cloneable, value is saved as a single entry in DB
* Otherwise value is saved as multiple entries (for backward compatibility)
*
* TODO: A good way to ALWAYS save values in single entry in DB, while maintaining backward compatibility
*
* @param $new
* @param $old
* @param $post_id
* @param $field
*/
static function save( $new, $old, $post_id, $field )
{
if ( !$field['clone'] )
{
RW_Meta_Box::save( $new, $old, $post_id, $field );
return;
}

if ( empty( $new ) )
delete_post_meta( $post_id, $field['id'] );
else
update_post_meta( $post_id, $field['id'], $new );
}

/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize_field( $field )
{
$field['field_name'] = $field['id'];
if ( !$field['clone'] && $field['multiple'] )
$field['field_name'] .= '[]';
return $field;
}
}
}

0 comments on commit a3362c8

Please sign in to comment.