|
<?php |
|
|
|
class Our_CMB2_Hooks { |
|
/** |
|
* Init hooks. |
|
*/ |
|
public function init() { |
|
// Support multidimensional fields. |
|
add_filter( 'cmb2_override_meta_save', array( $this, 'multidimensional_save_data' ), 10, 4 ); |
|
add_filter( 'cmb2_override_meta_value', array( $this, 'multidimensional_get_data' ), 10, 4 ); |
|
add_filter( 'cmb2_override_meta_remove', array( $this, 'multidimensional_remove_data' ), 10, 4 ); |
|
} |
|
|
|
/** |
|
* Filter whether to override saving of meta value. |
|
* |
|
* @param null|bool $check Whether to allow updating metadata for the given type. |
|
* @param array $args Array of data about current field including. |
|
* @param array $field_args All field argument. |
|
* @param CMB2_Field $field This field objec. |
|
* @return mixed |
|
*/ |
|
public function multidimensional_save_data( $check, $args, $field_args, $field ) { |
|
$no_override = null; |
|
$id_data = Multidimensional::split( $args['field_id'] ); |
|
|
|
// Tell CMB2 that we no override value. |
|
if ( empty( $id_data['keys'] ) ) { |
|
return $no_override; |
|
} |
|
|
|
// Options page updating... |
|
if ( 'options-page' === $args['type'] || empty( $args['id'] ) ) { |
|
$options = cmb2_options( $args['id'] )->get( $id_data['base'], array() ); |
|
$repace_value = $args['value']; |
|
|
|
if ( ! $args['single'] ) { |
|
$repace_value = Multidimensional::get( $options, $id_data['keys'], array() ); |
|
$repace_value[] = $args['value']; |
|
$repace_value = array_unique( $repace_value ); |
|
} |
|
|
|
$update_values = Multidimensional::replace( $options, $id_data['keys'], $repace_value ); |
|
return cmb2_options( $args['id'] )->update( $id_data['base'], $update_values, false, true ); |
|
} |
|
|
|
// Update metadata... |
|
// NOTE: Maybe have bugs in this case, see: CMB2_Field::update_data(). |
|
$metadata = get_metadata( $args['type'], $args['id'], $id_data['base'], true ); |
|
|
|
// Delete meta if we have an empty array. |
|
if ( is_array( $args['value'] ) && empty( $args['value'] ) ) { |
|
$update_values = Multidimensional::delete( $metadata, $id_data['keys'] ); |
|
} else { |
|
$update_values = Multidimensional::replace( $metadata, $id_data['keys'], $args['value'] ); |
|
} |
|
|
|
return update_metadata( $args['type'], $args['id'], $id_data['base'], $update_values ); |
|
} |
|
|
|
/** |
|
* Filter whether to override saving of meta value. |
|
* |
|
* @param null|bool $delete Whether to allow metadata deletion of the given type. |
|
* @param array $args Array of data about current field. |
|
* @param int|string $field_args All field arguments. |
|
* @param CMB2_Field $field CMB2_Field object. |
|
* @return mixed |
|
*/ |
|
public function multidimensional_remove_data( $delete, $args, $field_args, $field ) { |
|
$no_override = null; |
|
$id_data = Multidimensional::split( $args['field_id'] ); |
|
|
|
// Tell CMB2 that we no override value. |
|
if ( empty( $id_data['keys'] ) ) { |
|
return $no_override; |
|
} |
|
|
|
// Handler delete options. |
|
if ( 'options-page' === $args['type'] || empty( $args['id'] ) ) { |
|
$options = cmb2_options( $args['id'] )->get( $id_data['base'], array() ); |
|
|
|
Multidimensional::delete( $options, $id_data['keys'] ); |
|
|
|
return cmb2_options( $args['id'] )->update( $id_data['base'], $options, false, true ); |
|
} |
|
|
|
// Handler delete metadata. |
|
$metadata = get_metadata( $args['type'], $args['id'], $id_data['base'], true ); |
|
|
|
Multidimensional::delete( $metadata, $id_data['keys'] ); |
|
|
|
return update_metadata( $args['type'], $args['id'], $id_data['base'], $metadata ); |
|
} |
|
|
|
/** |
|
* Filter whether to override saving of meta value. |
|
* |
|
* @param null|bool $value The meta value should return. |
|
* @param int|string $object_id Object ID. |
|
* @param array $args An array of arguments for retrieving data. |
|
* @param CMB2_Field $field CMB2_Field object. |
|
* @return mixed |
|
*/ |
|
public function multidimensional_get_data( $value, $object_id, $args, $field ) { |
|
$no_override = 'cmb2_field_no_override_val'; |
|
$id_data = Multidimensional::split( $args['field_id'] ); |
|
|
|
// Tell CMB2 that we no override value. |
|
if ( empty( $id_data['keys'] ) ) { |
|
return $no_override; |
|
} |
|
|
|
if ( 'options-page' === $args['type'] ) { |
|
$metadata = cmb2_options( $args['id'] )->get( $id_data['base'] ); |
|
} else { |
|
// NOTE: Maybe have bugs in this case, see: CMB2_Field::get_data(). |
|
$metadata = get_metadata( $args['type'], $args['id'], $id_data['base'], true ); |
|
} |
|
|
|
return Multidimensional::get( $metadata, $id_data['keys'] ); |
|
} |
|
} |