meta-boxes.php
6.73 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* Admin Meta Boxes.
*
* @package BadgeOS
* @subpackage Admin
* @author LearningTimes, LLC
* @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
* @link https://credly.com
*/
/**
* Register custom meta boxes for BadgeOS achievement type.
*/
function badgeos_achievment_type_metaboxes() {
// Start with an underscore to hide fields from custom fields list.
$prefix = '_badgeos_';
// Grab our achievement types as an array.
$achievement_types = badgeos_get_achievement_types_slugs();
// Setup our $post_id, if available.
$post_id = isset( $_GET['post'] ) ? sanitize_text_field( wp_unslash( $_GET['post'] ) ) : 0;
$badgeos_settings = ! empty( badgeos_utilities::get_option( 'badgeos_settings' ) ) ? badgeos_utilities::get_option( 'badgeos_settings' ) : array();
// New Achievement Types.
$cmb_obj = new_cmb2_box(
array(
'id' => 'achievement_type_data',
'title' => esc_html__( 'Achievement Type Data', 'badgeos' ),
'object_types' => array( $badgeos_settings['achievement_main_post_type'] ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left.
)
);
$cmb_obj->add_field(
array(
'name' => __( 'Plural Name', 'badgeos' ),
'desc' => __( 'The plural name for this achievement (Title is singular name).', 'badgeos' ),
'id' => $prefix . 'plural_name',
'type' => 'text_medium',
)
);
$cmb_obj->add_field(
array(
'name' => __( 'Show in Menu?', 'badgeos' ),
'desc' => ' ' . __( 'Yes, show this achievement in the BadgeOS menu.', 'badgeos' ),
'id' => $prefix . 'show_in_menu',
'type' => 'checkbox',
)
);
}
add_action( 'cmb2_admin_init', 'badgeos_achievment_type_metaboxes' );
/**
* Add Custom field type.
*
* @since 1.0.0
* @param object $field_object field object.
* @param object $escaped_value escaped value.
* @param object $object_id object id.
* @param object $object_type object type.
* @param object $field_type_object field type object.
*/
function sch_cmb_render_text_only( $field_object, $escaped_value, $object_id, $object_type, $field_type_object ) {
echo $field_object->args['desc'];
}
add_action( 'cmb2_render_text_only', 'sch_cmb_render_text_only', 10, 5 );
/**
* Register custom meta boxes for Badgeos achievements.
*/
function badgeos_achievment_metaboxes() {
// Start with an underscore to hide fields from custom fields list.
$prefix = '_badgeos_';
$badgeos_settings = ! empty( badgeos_utilities::get_option( 'badgeos_settings' ) ) ? badgeos_utilities::get_option( 'badgeos_settings' ) : array();
// Grab our achievement types as an array.
$achievement_types_temp = badgeos_get_achievement_types_slugs();
$achievement_types = array();
if ( $achievement_types_temp ) {
foreach ( $achievement_types_temp as $key => $ach ) {
if ( ! empty( $ach ) && trim( $badgeos_settings['achievement_step_post_type'] ) !== $ach ) {
if ( ! empty( $ach ) && 'step' !== $ach ) {
$achievement_types[] = $ach;
}
}
}
}
// Setup our $post_id, if available.
$post_id = isset( $_GET['post'] ) ? sanitize_text_field( wp_unslash( $_GET['post'] ) ) : 0;
// New Achievement Types.
$cmb_obj = new_cmb2_box(
array(
'id' => 'achievement_data',
'title' => esc_html__( 'Achievement Data', 'badgeos' ),
'object_types' => $achievement_types, // Post type.
'context' => 'advanced',
'priority' => 'high',
'show_names' => true, // Show field names on the left.
)
);
$cmb_obj->add_field(
array(
'name' => esc_html__( 'Points Awarded', 'badgeos' ),
'desc' => ' ' . esc_html__( 'Points awarded for earning this achievement (optional). Leave empty if no points are awarded.', 'badgeos' ),
'id' => $prefix . 'points',
'type' => 'credit_field',
)
);
$cmb_obj->add_field(
array(
'name' => esc_html__( 'Earned By:', 'badgeos' ),
'desc' => esc_html__( 'How this achievement can be earned.', 'badgeos' ),
'id' => $prefix . 'earned_by',
'type' => 'select',
'options' => apply_filters(
'badgeos_achievement_earned_by',
array(
'triggers' => esc_html__( 'Completing Steps', 'badgeos' ),
'points' => esc_html__( 'Minimum Number of Points' ),
'admin' => esc_html__( 'Admin-awarded Only', 'badgeos' ),
)
),
)
);
$cmb_obj->add_field(
array(
'name' => esc_html__( 'Minimum Points Requried', 'badgeos' ),
'desc' => ' ' . esc_html__( 'Fewest number of points required for earning this achievement.', 'badgeos' ),
'id' => $prefix . 'points_required',
'type' => 'credit_field',
)
);
$cmb_obj->add_field(
array(
'name' => esc_html__( 'Sequential Steps', 'badgeos' ),
'desc' => ' ' . esc_html__( 'Yes, steps must be completed in order.', 'badgeos' ),
'id' => $prefix . 'sequential',
'type' => 'checkbox',
)
);
$cmb_obj->add_field(
array(
'name' => esc_html__( 'Show Earners', 'badgeos' ),
'desc' => ' ' . esc_html__( 'Yes, display a list of users who have earned this achievement.', 'badgeos' ),
'id' => $prefix . 'show_earners',
'type' => 'checkbox',
)
);
$note_text = __( 'Displayed after achievement is earned. If you would like the message to appear as a pop-up, please install <a href="https://badgeos.org/downloads/congratulations/" target="_blank">Congratulations Add-On</a>.', 'badgeos' );
$cmb_obj->add_field(
array(
'name' => esc_html__( 'Congratulations Text', 'badgeos' ),
'desc' => $note_text,
'id' => $prefix . 'congratulations_text',
'type' => 'textarea',
)
);
$cmb_obj->add_field(
array(
'name' => esc_html__( 'Revoke Badge', 'badgeos' ),
'desc' => ' ' . esc_html__( 'revoke, this badge if the points are deducted.', 'badgeos' ),
'id' => $prefix . 'revoke_badge_point_loss',
'type' => 'checkbox',
)
);
$cmb_obj->add_field(
array(
'name' => esc_html__( 'Maximum Earnings', 'badgeos' ),
'desc' => ' ' . esc_html__( 'Number of times a user can earn this badge (set to -1 for no maximum).', 'badgeos' ),
'id' => $prefix . 'maximum_earnings',
'type' => 'text_small',
'std' => '1',
)
);
$cmb_obj->add_field(
array(
'name' => esc_html__( 'Award badge on achieving 1 step out of many', 'badgeos' ),
'desc' => ' ' . esc_html__( 'Achievement is achievable by doing 1 step out of many ones under the required section.', 'badgeos' ),
'id' => $prefix . 'award_by_one_step_out_of_many',
'type' => 'checkbox',
'std' => '1',
)
);
$cmb_obj->add_field(
array(
'name' => esc_html__( 'Hidden?', 'badgeos' ),
'desc' => '',
'id' => $prefix . 'hidden',
'type' => 'select',
'options' => array(
'show' => esc_html__( 'Show to User', 'badgeos' ),
'hidden' => esc_html__( 'Hidden to User', 'badgeos' ),
),
)
);
}
add_action( 'cmb2_admin_init', 'badgeos_achievment_metaboxes' );