| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
|
| 6 |
/** |
| 7 |
* Handles custom field typea. |
| 8 |
*/ |
| 9 |
class WpGetApi_Parameter_Field extends CMB2_Type_Base { |
| 10 |
|
| 11 |
|
| 12 |
public static function init_parameter() { |
| 13 |
add_filter( 'cmb2_render_class_parameter', array( __CLASS__, 'class_name' ) ); |
| 14 |
add_filter( 'cmb2_sanitize_parameter', array( __CLASS__, 'maybe_save_split_values' ), 12, 4 ); |
| 15 |
/** |
| 16 |
* The following snippets are required for allowing the users field |
| 17 |
* to work as a repeatable field, or in a repeatable group |
| 18 |
*/ |
| 19 |
add_filter( 'cmb2_sanitize_parameter', array( __CLASS__, 'sanitize' ), 10, 5 ); |
| 20 |
add_filter( 'cmb2_types_esc_parameter', array( __CLASS__, 'escape' ), 10, 4 ); |
| 21 |
} |
| 22 |
|
| 23 |
public static function class_name() { return __CLASS__; } |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Handles outputting the users field. |
| 28 |
*/ |
| 29 |
public function render() { |
| 30 |
|
| 31 |
// make sure we assign each part of the value we need. |
| 32 |
$value = wp_parse_args( $this->field->escaped_value(), array( |
| 33 |
'name' => '', |
| 34 |
'value' => '', |
| 35 |
) ); |
| 36 |
|
| 37 |
ob_start(); |
| 38 |
// Do html |
| 39 |
?> |
| 40 |
|
| 41 |
<div class="name input-wrap"> |
| 42 |
<label for="<?php esc_attr_e( $this->_id( '_name' ) ); ?>"> |
| 43 |
<?php esc_html_e( $this->_text( 'parameter_name_text', __( 'Name', 'wpgetapi' ) ) ); ?> |
| 44 |
</label> |
| 45 |
|
| 46 |
<?php echo $this->types->input( array( |
| 47 |
'name' => $this->_name( '[name]' ), |
| 48 |
'id' => $this->_id( '_name' ), |
| 49 |
'value' => $value['name'], |
| 50 |
'desc' => '', |
| 51 |
'placeholder' => __( 'Name of the parameter', 'wpgetapi' ), |
| 52 |
) ); ?> |
| 53 |
</div><!-- |
| 54 |
|
| 55 |
--><div class="value input-wrap"> |
| 56 |
<label for="<?php esc_attr_e( $this->_id( '_value' ) ); ?>"> |
| 57 |
<?php esc_html_e( $this->_text( 'parameter_value_text', __( 'Value', 'wpgetapi' ) ) ); ?> |
| 58 |
</label> |
| 59 |
|
| 60 |
<?php echo $this->types->textarea( array( |
| 61 |
'name' => $this->_name( '[value]' ), |
| 62 |
'id' => $this->_id( '_value' ), |
| 63 |
'value' => stripslashes($value['value']), |
| 64 |
'desc' => '', |
| 65 |
'rows' => 2, |
| 66 |
'placeholder' => __( 'Value of the parameter', 'wpgetapi' ), |
| 67 |
) ); ?> |
| 68 |
</div> |
| 69 |
|
| 70 |
<?php |
| 71 |
|
| 72 |
// grab the data from the output buffer. |
| 73 |
return $this->rendered( ob_get_clean() ); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Optionally save the values into separate fields |
| 79 |
*/ |
| 80 |
public static function maybe_save_split_values( $override_value, $value, $object_id, $field_args ) { |
| 81 |
|
| 82 |
// Don't do the override |
| 83 |
if ( ! isset( $field_args['split_values'] ) || ! $field_args['split_values'] ) |
| 84 |
return $override_value; |
| 85 |
|
| 86 |
$encryption = new WpGetApi_Encryption(); |
| 87 |
|
| 88 |
$parameter_keys = array( 'name', 'value' ); |
| 89 |
foreach ( $parameter_keys as $key ) { |
| 90 |
if ( ! empty( $value[ $key ] ) ) { |
| 91 |
$updated_value = $encryption->encrypt( $value[ $key ] ); |
| 92 |
update_post_meta( $object_id, $field_args['id'] . 'parameter_'. $key, $updated_value ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
remove_filter( 'cmb2_sanitize_parameter', array( __CLASS__, 'sanitize' ), 10, 5 ); |
| 97 |
// Tell CMB2 we already did the update |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
public static function sanitize( $check, $meta_value, $object_id, $field_args, $sanitize_object ) { |
| 103 |
|
| 104 |
// if not repeatable, bail out. |
| 105 |
if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) |
| 106 |
return $check; |
| 107 |
|
| 108 |
$encryption = new WpGetApi_Encryption(); |
| 109 |
|
| 110 |
foreach ( $meta_value as $i => $param ) { |
| 111 |
|
| 112 |
// if both empty, unset and continue |
| 113 |
if( $param['name'] == '' && $param['value'] == '' ) { |
| 114 |
unset( $meta_value[ $i ] ); |
| 115 |
continue; |
| 116 |
} |
| 117 |
|
| 118 |
foreach ( $param as $key => $val ) { |
| 119 |
$meta_value[ $i ][ $key ] = $key === 'name' ? $val : $encryption->encrypt( $val ); |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
return array_filter($meta_value); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
public static function escape( $check, $meta_value, $field_args, $field_object ) { |
| 129 |
|
| 130 |
// if not repeatable, bail out. |
| 131 |
if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) |
| 132 |
return $check; |
| 133 |
|
| 134 |
$encryption = new WpGetApi_Encryption(); |
| 135 |
|
| 136 |
foreach ( $meta_value as $i => $param ) { |
| 137 |
|
| 138 |
// if both empty, unset and continue |
| 139 |
if( $param['name'] == '' && $param['value'] == '' ) { |
| 140 |
unset( $meta_value[ $i ] ); |
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
foreach ( $param as $key => $val ) { |
| 145 |
$meta_value[ $i ][ $key ] = $key === 'name' ? $val : $encryption->decrypt( $val ); |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
return array_filter($meta_value); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
} |