Skip to content

Commit 7cb3e00

Browse files
committed
Run the field-specific actions for all fields (including fields in groups). Fixes CMB2#1157
1 parent 7d7ee94 commit 7cb3e00

File tree

2 files changed

+56
-42
lines changed

2 files changed

+56
-42
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ All notable changes to this project will be documented in this file.
2121
### Bug Fixes
2222
* Javascript: Correctly pass the newly created row to the `cmb2_add_row` triggered event.
2323
* 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)).
24+
* 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).
25+
2426

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

includes/CMB2.php

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,10 +1467,60 @@ public function add_field( array $field, $position = 0 ) {
14671467
return false;
14681468
}
14691469

1470-
// Perform some field-type-specific initiation actions.
1470+
$this->_add_field_to_array(
1471+
$field,
1472+
$this->meta_box['fields'],
1473+
$position
1474+
);
1475+
1476+
return $field['id'];
1477+
}
1478+
1479+
/**
1480+
* Add a field to a group
1481+
*
1482+
* @since 2.0.0
1483+
* @param string $parent_field_id The field id of the group field to add the field.
1484+
* @param array $field Metabox field config array.
1485+
* @param int $position (optional) Position of metabox. 1 for first, etc.
1486+
* @return mixed Array of parent/field ids or false.
1487+
*/
1488+
public function add_group_field( $parent_field_id, array $field, $position = 0 ) {
1489+
if ( ! array_key_exists( $parent_field_id, $this->meta_box['fields'] ) ) {
1490+
return false;
1491+
}
1492+
1493+
$parent_field = $this->meta_box['fields'][ $parent_field_id ];
1494+
1495+
if ( 'group' !== $parent_field['type'] ) {
1496+
return false;
1497+
}
1498+
1499+
if ( ! isset( $parent_field['fields'] ) ) {
1500+
$this->meta_box['fields'][ $parent_field_id ]['fields'] = array();
1501+
}
1502+
1503+
$this->_add_field_to_array(
1504+
$field,
1505+
$this->meta_box['fields'][ $parent_field_id ]['fields'],
1506+
$position
1507+
);
1508+
1509+
return array( $parent_field_id, $field['id'] );
1510+
}
1511+
1512+
/**
1513+
* Perform some field-type-specific initiation actions.
1514+
*
1515+
* @since 2.7.0
1516+
* @param array $field Metabox field config array.
1517+
* @return void
1518+
*/
1519+
protected function field_actions( $field ) {
14711520
switch ( $field['type'] ) {
14721521
case 'file':
14731522
case 'file_list':
1523+
14741524
// Initiate attachment JS hooks.
14751525
add_filter( 'wp_prepare_attachment_for_js', array( 'CMB2_Type_File_Base', 'prepare_image_sizes_for_js' ), 10, 3 );
14761526
break;
@@ -1486,6 +1536,7 @@ public function add_field( array $field, $position = 0 ) {
14861536
}
14871537
break;
14881538
case 'colorpicker':
1539+
14891540
// https://github.com/JayWood/CMB2_RGBa_Picker
14901541
// Dequeue the rgba_colorpicker custom field script if it is used,
14911542
// since we now enqueue our own more current version.
@@ -1500,14 +1551,6 @@ public function add_field( array $field, $position = 0 ) {
15001551
if ( isset( $field['taxonomy'] ) && ! empty( $field['remove_default'] ) ) {
15011552
$this->tax_metaboxes_to_remove[ $field['taxonomy'] ] = $field['taxonomy'];
15021553
}
1503-
1504-
$this->_add_field_to_array(
1505-
$field,
1506-
$this->meta_box['fields'],
1507-
$position
1508-
);
1509-
1510-
return $field['id'];
15111554
}
15121555

15131556
/**
@@ -1530,39 +1573,6 @@ protected function define_field_column( array $field ) {
15301573
return $field;
15311574
}
15321575

1533-
/**
1534-
* Add a field to a group
1535-
*
1536-
* @since 2.0.0
1537-
* @param string $parent_field_id The field id of the group field to add the field.
1538-
* @param array $field Metabox field config array.
1539-
* @param int $position (optional) Position of metabox. 1 for first, etc.
1540-
* @return mixed Array of parent/field ids or false.
1541-
*/
1542-
public function add_group_field( $parent_field_id, array $field, $position = 0 ) {
1543-
if ( ! array_key_exists( $parent_field_id, $this->meta_box['fields'] ) ) {
1544-
return false;
1545-
}
1546-
1547-
$parent_field = $this->meta_box['fields'][ $parent_field_id ];
1548-
1549-
if ( 'group' !== $parent_field['type'] ) {
1550-
return false;
1551-
}
1552-
1553-
if ( ! isset( $parent_field['fields'] ) ) {
1554-
$this->meta_box['fields'][ $parent_field_id ]['fields'] = array();
1555-
}
1556-
1557-
$this->_add_field_to_array(
1558-
$field,
1559-
$this->meta_box['fields'][ $parent_field_id ]['fields'],
1560-
$position
1561-
);
1562-
1563-
return array( $parent_field_id, $field['id'] );
1564-
}
1565-
15661576
/**
15671577
* Add a field array to a fields array in desired position
15681578
*
@@ -1572,6 +1582,8 @@ public function add_group_field( $parent_field_id, array $field, $position = 0 )
15721582
* @param integer $position Optionally specify a position in the array to be inserted.
15731583
*/
15741584
protected function _add_field_to_array( $field, &$fields, $position = 0 ) {
1585+
$this->field_actions( $field );
1586+
15751587
if ( $position ) {
15761588
CMB2_Utils::array_insert( $fields, array( $field['id'] => $field ), $position );
15771589
} else {

0 commit comments

Comments
 (0)