| 1 |
<?php |
| 2 |
/** |
| 3 |
* CMB2 GamiPress Points Field Type |
| 4 |
* |
| 5 |
* @package GamiPress|CMB2_GamiPress_Points_Field_Type |
| 6 |
*/ |
| 7 |
|
| 8 |
if( ! function_exists( 'cmb2_render_gamipress_points_field_type' ) ) : |
| 9 |
|
| 10 |
/** |
| 11 |
* Adds a custom field type for GamiPress points. |
| 12 |
* |
| 13 |
* @param object $field The CMB2_Field type object. |
| 14 |
* @param string $value The saved (and escaped) value. |
| 15 |
* @param int $object_id The current post ID. |
| 16 |
* @param string $object_type The current object type. |
| 17 |
* @param CMB2_Types $field_type The CMB2_Types object. |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
function cmb2_render_gamipress_points_field_type( $field, $value, $object_id, $object_type, $field_type ) { |
| 22 |
|
| 23 |
global $ct_cmb2_override; |
| 24 |
|
| 25 |
$post_type_meta_key = $field->args['id'] . '_type'; |
| 26 |
|
| 27 |
if( isset( $field->args['points_type_key'] ) ) { |
| 28 |
$post_type_meta_key = $field->args['points_type_key']; |
| 29 |
} |
| 30 |
|
| 31 |
// Support for CT objects |
| 32 |
if( $ct_cmb2_override === true ) { |
| 33 |
$points_type = ct_get_object_meta( $object_id, $post_type_meta_key, true ); |
| 34 |
} else { |
| 35 |
$points_type = get_post_meta( $object_id, $post_type_meta_key, true ); |
| 36 |
} |
| 37 |
|
| 38 |
// Grab our points types as a html |
| 39 |
$points_types_options = '<option value="" class="points-type-placeholder">' . __( 'Choose the Points Type', 'gamipress' ) . '</option>'; |
| 40 |
|
| 41 |
foreach( gamipress_get_points_types() as $slug => $data ) { |
| 42 |
$points_types_options .= '<option value="' . $slug . '" class="' . $slug . '" ' . selected( $points_type, $slug, false ) . '>' . $data['plural_name'] . '</option>'; |
| 43 |
} ?> |
| 44 |
|
| 45 |
<div class="cmb-inline"> |
| 46 |
<ul> |
| 47 |
<li> |
| 48 |
<?php echo $field_type->input( array( |
| 49 |
'name' => $field_type->_name(), |
| 50 |
'id' => $field_type->_id(), |
| 51 |
'value' => $value, |
| 52 |
'desc' => '', |
| 53 |
'type' => 'number', |
| 54 |
'step' => 1, |
| 55 |
'min' => 0, |
| 56 |
'class' => 'medium-text', |
| 57 |
'placeholder' => 0 |
| 58 |
) ); ?> |
| 59 |
</li> |
| 60 |
<li> |
| 61 |
<?php echo $field_type->select( array( |
| 62 |
'name' => $post_type_meta_key, |
| 63 |
'id' => $post_type_meta_key, |
| 64 |
'value' => $points_type, |
| 65 |
'desc' => '', |
| 66 |
'options' => $points_types_options, |
| 67 |
'onchange' => 'this.className=this.options[this.selectedIndex].className', |
| 68 |
) ); ?> |
| 69 |
</li> |
| 70 |
</ul> |
| 71 |
</div> |
| 72 |
<?php |
| 73 |
|
| 74 |
echo $field_type->_desc( true ); |
| 75 |
|
| 76 |
} |
| 77 |
add_action( 'cmb2_render_gamipress_points', 'cmb2_render_gamipress_points_field_type', 10, 5 ); |
| 78 |
|
| 79 |
/** |
| 80 |
* After save GamiPress points field type, needs to store the points type value too |
| 81 |
* |
| 82 |
* @param string $field_id the current field id paramater. |
| 83 |
* @param bool $updated Whether the metadata update action occurred. |
| 84 |
* @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
| 85 |
* @param CMB2_Field object $field This field object |
| 86 |
*/ |
| 87 |
function cmb2_save_gamipress_points_field_type( $field_id, $updated, $action, $field ) { |
| 88 |
|
| 89 |
global $ct_cmb2_override; |
| 90 |
|
| 91 |
if( $field->args['type'] !== 'gamipress_points' ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
$post_type_meta_key = $field->args['id'] . '_type'; |
| 96 |
|
| 97 |
if( isset( $field->args['points_type_key'] ) ) { |
| 98 |
$post_type_meta_key = $field->args['points_type_key']; |
| 99 |
} |
| 100 |
|
| 101 |
if ( isset( $_REQUEST[$post_type_meta_key] ) ) { |
| 102 |
$value = sanitize_text_field( $_REQUEST[$post_type_meta_key] ); |
| 103 |
|
| 104 |
if( $ct_cmb2_override === true ) { |
| 105 |
ct_update_object_meta( $field->object_id, $post_type_meta_key, $value ); |
| 106 |
} else { |
| 107 |
update_metadata( $field->object_type, $field->object_id, $post_type_meta_key, $value ); |
| 108 |
} |
| 109 |
} else { |
| 110 |
return; |
| 111 |
} |
| 112 |
} |
| 113 |
add_action( 'cmb2_save_field', 'cmb2_save_gamipress_points_field_type', 10, 4 ); |
| 114 |
|
| 115 |
endif; |