cmb2-custom-credit-field.php
3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Cmb2 Credit fields.
*
* @package Badgeos
* @subpackage Points
* @author LearningTimes
* @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
* @link https://credly.com/
*/
/**
* CMB2 Custom Credit Field Type.
*
* @param string $value value.
* @package CMB2 Custom Credit Field.
*/
function cmb2_get_state_options( $value = false ) {
$settings = ! empty( badgeos_utilities::get_option( 'badgeos_settings' ) ) ? badgeos_utilities::get_option( 'badgeos_settings' ) : array();
$credits = get_posts(
array(
'post_type' => trim( $settings['points_main_post_type'] ),
'posts_per_page' => -1,
'suppress_filters' => false,
'orderby' => 'ID',
'order' => 'ASC',
)
);
if ( count( $credits ) > 0 ) {
$state_options = '';
} else {
$state_options = '<option value="0" selected>' . esc_html__( 'Point Type', 'badgeos' ) . '</option>';
}
foreach ( $credits as $credit ) {
$plural_name = badgeos_utilities::get_post_meta( $credit->ID, '_point_plural_name', true );
$post_type_plural = '';
if ( ! empty( $plural_name ) ) {
$post_type_plural = $plural_name;
} else {
$post_type_plural = $credit->post_title;
}
$state_options .= '<option value="' . esc_attr( $credit->ID ) . '" ' . selected( $value, $credit->ID, false ) . '>' . esc_html( $post_type_plural ) . '</option>';
}
return $state_options;
}
/**
* Adds a custom field type for two point fields.
*
* @param object $field The CMB2_Field type object.
* @param string $value The saved (and escaped) value.
* @param int $object_id The current post ID.
* @param string $object_type The current object type.
* @param object $field_type The CMB2_Types object.
* @return void
*/
function cmb2_render_custom_credit_field( $field, $value, $object_id, $object_type, $field_type ) {
/**
* Make sure we specify each part of the value we need.
*/
$value = wp_parse_args(
$value,
array(
$field->args['_name'] => '',
$field->args['_name'] . '_type' => '',
)
);
?>
<div class="alignleft">
<?php
echo $field_type->input(
array(
'class' => esc_attr( 'cmb_text_small' ),
'name' => esc_attr( $field_type->_name( '[' . $field->args['_name'] . ']' ) ),
'id' => esc_attr( $field_type->_id( $field->args['_name'] ) ),
'value' => esc_attr( $value[ $field->args['_name'] ] ),
'desc' => '',
)
);
?>
</div>
<div class="alignleft">
<?php
echo $field_type->select(
array(
'name' => esc_attr( $field_type->_name( '[' . $field->args['_name'] . '_type]' ) ),
'id' => esc_attr( $field_type->_id( $field->args['_name'] . '_type' ) ),
'options' => cmb2_get_state_options( $value[ $field->args['_name'] . '_type' ] ),
'desc' => '',
)
);
?>
</div>
<?php
}
add_action( 'cmb2_render_credit_field', 'cmb2_render_custom_credit_field', 10, 5 );
/**
* Sanitize the selected value.
*
* @param string $override_value override_value.
* @param string $value value.
*
* @return value
*/
function cmb2_sanitize_custom_credit_field_callback( $override_value, $value ) {
if ( is_array( $value ) ) {
foreach ( $value as $key => $saved_value ) {
$value[ $key ] = sanitize_text_field( $saved_value );
}
return $value;
}
return $value;
}
add_filter( 'cmb2_sanitize_credit_field', 'cmb2_sanitize_custom_credit_field_callback', 10, 2 );