| 1 |
<?php |
| 2 |
/** |
| 3 |
* CMB2 Hookup Field |
| 4 |
* |
| 5 |
* Adds necessary hooks for certain field types. |
| 6 |
* |
| 7 |
* @since 2.11.0 |
| 8 |
* |
| 9 |
* @category WordPress_Plugin |
| 10 |
* @package CMB2 |
| 11 |
* @author CMB2 team |
| 12 |
* @license GPL-2.0+ |
| 13 |
* @link https://cmb2.io |
| 14 |
*/ |
| 15 |
class CMB2_Hookup_Field { |
| 16 |
|
| 17 |
/** |
| 18 |
* Field id. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
* @since 2.11.0 |
| 22 |
*/ |
| 23 |
protected $field_id; |
| 24 |
|
| 25 |
/** |
| 26 |
* CMB2 object id. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
* @since 2.11.0 |
| 30 |
*/ |
| 31 |
protected $cmb_id; |
| 32 |
|
| 33 |
/** |
| 34 |
* The object type we are performing the hookup for |
| 35 |
* |
| 36 |
* @var string |
| 37 |
* @since 2.11.0 |
| 38 |
*/ |
| 39 |
protected $object_type = 'post'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Initialize all hooks for the given field. |
| 43 |
* |
| 44 |
* @since 2.11.0 |
| 45 |
* @param array $field The field arguments array. |
| 46 |
* @param CMB2 $cmb The CMB2 object. |
| 47 |
* @return array The field arguments array. |
| 48 |
*/ |
| 49 |
public static function init( $field, CMB2 $cmb ) { |
| 50 |
switch ( $field['type'] ) { |
| 51 |
case 'file': |
| 52 |
case 'file_list': |
| 53 |
// Initiate attachment JS hooks. |
| 54 |
add_filter( 'wp_prepare_attachment_for_js', array( 'CMB2_Type_File_Base', 'prepare_image_sizes_for_js' ), 10, 3 ); |
| 55 |
break; |
| 56 |
|
| 57 |
case 'oembed': |
| 58 |
// Initiate oembed Ajax hooks. |
| 59 |
cmb2_ajax(); |
| 60 |
break; |
| 61 |
|
| 62 |
case 'group': |
| 63 |
if ( empty( $field['render_row_cb'] ) ) { |
| 64 |
$field['render_row_cb'] = array( $cmb, 'render_group_callback' ); |
| 65 |
} |
| 66 |
break; |
| 67 |
case 'colorpicker': |
| 68 |
// https://github.com/JayWood/CMB2_RGBa_Picker |
| 69 |
// Dequeue the rgba_colorpicker custom field script if it is used, |
| 70 |
// since we now enqueue our own more current version. |
| 71 |
add_action( 'admin_enqueue_scripts', array( 'CMB2_Type_Colorpicker', 'dequeue_rgba_colorpicker_script' ), 99 ); |
| 72 |
break; |
| 73 |
|
| 74 |
case 'text_datetime_timestamp_timezone': |
| 75 |
foreach ( $cmb->box_types() as $object_type ) { |
| 76 |
if ( ! $cmb->is_supported_core_object_type( $object_type ) ) { |
| 77 |
// Ignore post-types... |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
if ( empty( $field['field_hookup_instance'][ $object_type ] ) ) { |
| 82 |
$instance = new self( $field, $object_type, $cmb ); |
| 83 |
$method = 'options-page' === $object_type |
| 84 |
? 'text_datetime_timestamp_timezone_option_back_compat' |
| 85 |
: 'text_datetime_timestamp_timezone_back_compat'; |
| 86 |
|
| 87 |
$field['field_hookup_instance'][ $object_type ] = array( $instance, $method ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( false === $field['field_hookup_instance'][ $object_type ] ) { |
| 91 |
// If set to false, no need to filter. |
| 92 |
// This can be set if you have updated your use of the field type value to |
| 93 |
// assume the JSON value. |
| 94 |
continue; |
| 95 |
} |
| 96 |
|
| 97 |
if ( 'options-page' === $object_type ) { |
| 98 |
$option_name = $cmb->object_id(); |
| 99 |
add_filter( "pre_option_{$option_name}", $field['field_hookup_instance'][ $object_type ], 10, 3 ); |
| 100 |
continue; |
| 101 |
} |
| 102 |
|
| 103 |
add_filter( "get_{$object_type}_metadata", $field['field_hookup_instance'][ $object_type ], 10, 5 ); |
| 104 |
} |
| 105 |
break; |
| 106 |
} |
| 107 |
|
| 108 |
return $field; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Constructor |
| 113 |
* |
| 114 |
* @since 2.11.0 |
| 115 |
* @param CMB2 $cmb The CMB2 object to hookup. |
| 116 |
*/ |
| 117 |
public function __construct( $field, $object_type, CMB2 $cmb ) { |
| 118 |
$this->field_id = $field['id']; |
| 119 |
$this->object_type = $object_type; |
| 120 |
$this->cmb_id = $cmb->cmb_id; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Adds a back-compat shim for text_datetime_timestamp_timezone field type values. |
| 125 |
* |
| 126 |
* Handles old serialized DateTime values, as well as the new JSON formatted values. |
| 127 |
* |
| 128 |
* @since 2.11.0 |
| 129 |
* |
| 130 |
* @param mixed $value The value of the metadata. |
| 131 |
* @param int $object_id ID of the object metadata is for. |
| 132 |
* @param string $meta_key Meta key. |
| 133 |
* @param bool $single Whether to return a single value. |
| 134 |
* @param string $meta_type Type of object metadata is for. |
| 135 |
* @return mixed Maybe reserialized value. |
| 136 |
*/ |
| 137 |
public function text_datetime_timestamp_timezone_back_compat( $value, $object_id, $meta_key, $single, $meta_type ) { |
| 138 |
if ( $meta_key === $this->field_id ) { |
| 139 |
remove_filter( "get_{$meta_type}_metadata", array( $this, __FUNCTION__ ), 10, 5 ); |
| 140 |
$value = get_metadata( $meta_type, $object_id, $meta_key, $single ); |
| 141 |
add_filter( "get_{$meta_type}_metadata", array( $this, __FUNCTION__ ), 10, 5 ); |
| 142 |
|
| 143 |
$value = $this->reserialize_safe_value( $value ); |
| 144 |
} |
| 145 |
|
| 146 |
return $value; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Adds a back-compat shim for text_datetime_timestamp_timezone field type values on options pages. |
| 151 |
* |
| 152 |
* Handles old serialized DateTime values, as well as the new JSON formatted values. |
| 153 |
* |
| 154 |
* @since 2.11.0 |
| 155 |
* |
| 156 |
* @param mixed $value The value of the option. |
| 157 |
* @param string $option Option name. |
| 158 |
* @param mixed $default_value Default value. |
| 159 |
* @return mixed The updated value. |
| 160 |
*/ |
| 161 |
public function text_datetime_timestamp_timezone_option_back_compat( $value, $option, $default_value ) { |
| 162 |
remove_filter( "pre_option_{$option}", array( $this, __FUNCTION__ ), 10, 3 ); |
| 163 |
$value = get_option( $option, $default_value ); |
| 164 |
add_filter( "pre_option_{$option}", array( $this, __FUNCTION__ ), 10, 3 ); |
| 165 |
|
| 166 |
if ( ! empty( $value ) && is_array( $value ) ) { |
| 167 |
|
| 168 |
// Loop fields and update values for all text_datetime_timestamp_timezone fields. |
| 169 |
foreach ( CMB2_Boxes::get( $this->cmb_id )->prop( 'fields' ) as $field ) { |
| 170 |
if ( |
| 171 |
'text_datetime_timestamp_timezone' === $field['type'] |
| 172 |
&& ! empty( $value[ $field['id'] ] ) |
| 173 |
) { |
| 174 |
$value[ $field['id'] ] = $this->reserialize_safe_value( $value[ $field['id'] ] ); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return $value; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Reserialize a value to a safe serialized DateTime value. |
| 184 |
* |
| 185 |
* @since 2.11.0 |
| 186 |
* |
| 187 |
* @param mixed $value The value to check. |
| 188 |
* @return mixed The value, possibly reserialized. |
| 189 |
*/ |
| 190 |
protected function reserialize_safe_value( $value ) { |
| 191 |
if ( is_array( $value ) ) { |
| 192 |
return array_map( array( $this, 'reserialize_safe_value' ), $value ); |
| 193 |
} |
| 194 |
|
| 195 |
$updated_val = CMB2_Utils::get_datetime_from_value( $value ); |
| 196 |
$value = $updated_val ? serialize( $updated_val ) : ''; |
| 197 |
|
| 198 |
return $value; |
| 199 |
} |
| 200 |
} |
| 201 |
|