Skip to content

Commit

Permalink
Add $data_to_save property to CMB2_Field so that data can be used in …
Browse files Browse the repository at this point in the history
…CMB2_Sanitize instead of $_POST
  • Loading branch information
jtsternberg committed Oct 2, 2015
1 parent 9bd3134 commit a64c092
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 11 additions & 3 deletions includes/CMB2_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ class CMB2_Field {
*/
protected $callback_results = array();

/**
* Array of key => value data for saving. Likely $_POST data.
* @var array
* @since 2.0.0
*/
public $data_to_save = array();

/**
* Constructs our field object
* @since 1.1.0
Expand Down Expand Up @@ -449,10 +456,11 @@ public function sanitization_cb( $meta_value ) {
* @param array $data_to_save $_POST data to check
* @return bool Result of save
*/
public function save_field_from_data( $data_to_save ) {
public function save_field_from_data( array $data_to_save ) {
$this->data_to_save = $data_to_save;

$meta_value = isset( $data_to_save[ $this->id( true ) ] )
? $data_to_save[ $this->id( true ) ]
$meta_value = isset( $this->data_to_save[ $this->id( true ) ] )
? $this->data_to_save[ $this->id( true ) ]
: null;

return $this->save_field( $meta_value );
Expand Down
10 changes: 6 additions & 4 deletions includes/CMB2_Sanitize.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,17 @@ public function _save_file_id() {
$id_key = $field->_id();
$id_val_old = $field->escaped_value( 'absint' );

$alldata = $this->field->data_to_save;

if ( $group ) {
// Check group $_POST data
// Check group $alldata data
$i = $group->index;
$base_id = $group->_id();
$id_val = isset( $_POST[ $base_id ][ $i ][ $id_key ] ) ? absint( $_POST[ $base_id ][ $i ][ $id_key ] ) : 0;
$id_val = isset( $alldata[ $base_id ][ $i ][ $id_key ] ) ? absint( $alldata[ $base_id ][ $i ][ $id_key ] ) : 0;

} else {
// Check standard $_POST data
$id_val = isset( $_POST[ $field->id() ] ) ? $_POST[ $field->id() ] : null;
// Check standard $alldata data
$id_val = isset( $alldata[ $field->id() ] ) ? $alldata[ $field->id() ] : null;

}

Expand Down

0 comments on commit a64c092

Please sign in to comment.