Skip to content

Commit

Permalink
Run the field-specific actions for all fields (including fields in gr…
Browse files Browse the repository at this point in the history
…oups). Fixes CMB2#1157
  • Loading branch information
jtsternberg committed Sep 3, 2019
1 parent 7d7ee94 commit 7cb3e00
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,8 @@ All notable changes to this project will be documented in this file.
### Bug Fixes
* Javascript: Correctly pass the newly created row to the `cmb2_add_row` triggered event.
* Use `CMB2_Field::get_rest_value()` to get values for fields in the post REST API endpoints ([#1284](https://github.com/CMB2/CMB2/issues/1284)).
* Fix issue where oEmbed fields' live-preview would not work if the field was added within a group, along with some other similarly related issues. Fixes [#1157](https://github.com/CMB2/CMB2/issues/1157).


## [2.6.0 - 2019-01-18](https://github.com/CMB2/CMB2/releases/tag/v2.6.0)

Expand Down
96 changes: 54 additions & 42 deletions includes/CMB2.php
Expand Up @@ -1467,10 +1467,60 @@ public function add_field( array $field, $position = 0 ) {
return false;
}

// Perform some field-type-specific initiation actions.
$this->_add_field_to_array(
$field,
$this->meta_box['fields'],
$position
);

return $field['id'];
}

/**
* Add a field to a group
*
* @since 2.0.0
* @param string $parent_field_id The field id of the group field to add the field.
* @param array $field Metabox field config array.
* @param int $position (optional) Position of metabox. 1 for first, etc.
* @return mixed Array of parent/field ids or false.
*/
public function add_group_field( $parent_field_id, array $field, $position = 0 ) {
if ( ! array_key_exists( $parent_field_id, $this->meta_box['fields'] ) ) {
return false;
}

$parent_field = $this->meta_box['fields'][ $parent_field_id ];

if ( 'group' !== $parent_field['type'] ) {
return false;
}

if ( ! isset( $parent_field['fields'] ) ) {
$this->meta_box['fields'][ $parent_field_id ]['fields'] = array();
}

$this->_add_field_to_array(
$field,
$this->meta_box['fields'][ $parent_field_id ]['fields'],
$position
);

return array( $parent_field_id, $field['id'] );
}

/**
* Perform some field-type-specific initiation actions.
*
* @since 2.7.0
* @param array $field Metabox field config array.
* @return void
*/
protected function field_actions( $field ) {
switch ( $field['type'] ) {
case 'file':
case 'file_list':

// Initiate attachment JS hooks.
add_filter( 'wp_prepare_attachment_for_js', array( 'CMB2_Type_File_Base', 'prepare_image_sizes_for_js' ), 10, 3 );
break;
Expand All @@ -1486,6 +1536,7 @@ public function add_field( array $field, $position = 0 ) {
}
break;
case 'colorpicker':

// https://github.com/JayWood/CMB2_RGBa_Picker
// Dequeue the rgba_colorpicker custom field script if it is used,
// since we now enqueue our own more current version.
Expand All @@ -1500,14 +1551,6 @@ public function add_field( array $field, $position = 0 ) {
if ( isset( $field['taxonomy'] ) && ! empty( $field['remove_default'] ) ) {
$this->tax_metaboxes_to_remove[ $field['taxonomy'] ] = $field['taxonomy'];
}

$this->_add_field_to_array(
$field,
$this->meta_box['fields'],
$position
);

return $field['id'];
}

/**
Expand All @@ -1530,39 +1573,6 @@ protected function define_field_column( array $field ) {
return $field;
}

/**
* Add a field to a group
*
* @since 2.0.0
* @param string $parent_field_id The field id of the group field to add the field.
* @param array $field Metabox field config array.
* @param int $position (optional) Position of metabox. 1 for first, etc.
* @return mixed Array of parent/field ids or false.
*/
public function add_group_field( $parent_field_id, array $field, $position = 0 ) {
if ( ! array_key_exists( $parent_field_id, $this->meta_box['fields'] ) ) {
return false;
}

$parent_field = $this->meta_box['fields'][ $parent_field_id ];

if ( 'group' !== $parent_field['type'] ) {
return false;
}

if ( ! isset( $parent_field['fields'] ) ) {
$this->meta_box['fields'][ $parent_field_id ]['fields'] = array();
}

$this->_add_field_to_array(
$field,
$this->meta_box['fields'][ $parent_field_id ]['fields'],
$position
);

return array( $parent_field_id, $field['id'] );
}

/**
* Add a field array to a fields array in desired position
*
Expand All @@ -1572,6 +1582,8 @@ public function add_group_field( $parent_field_id, array $field, $position = 0 )
* @param integer $position Optionally specify a position in the array to be inserted.
*/
protected function _add_field_to_array( $field, &$fields, $position = 0 ) {
$this->field_actions( $field );

if ( $position ) {
CMB2_Utils::array_insert( $fields, array( $field['id'] => $field ), $position );
} else {
Expand Down

0 comments on commit 7cb3e00

Please sign in to comment.