ob-metabox.php
17 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* BadgeOS Open Badge Options
*
* @package BadgeOS
* @subpackage Open Badge
* @author Learning Times
* @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class open_badge_metabox {
/**
* Instantiate the Opne Badge.
*/
public function __construct() {
// Badge Metabox
add_action( 'add_meta_boxes', array( $this, 'open_badge_metabox_add' ) );
add_action( 'save_post', array( $this, 'open_badge_metabox_save' ) );
add_action( 'wp_ajax_badgeos_validate_open_badge', array( $this, 'badgeos_validate_open_badge' ) );
add_action( 'wp_ajax_nopriv_badgeos_validate_open_badge', array( $this, 'badgeos_validate_open_badge' ) );
add_action( 'wp_ajax_bos_ob_convert_to_open_badge', array( $this, 'convert_to_open_badge' ) );
add_action( 'wp_ajax_nopriv_bos_ob_convert_to_open_badge', array( $this, 'convert_to_open_badge' ) );
add_filter( 'badgeos_after_earned_achievement', array( $this, 'badgeos_after_earned_achievement_cb' ), 10, 2 );
}
public function badgeos_after_earned_achievement_cb( $return, $achievement ) {
$achievement_id = $achievement->ID;
$entry_id = $achievement->entry_id;
$rec_type = $achievement->rec_type;
ob_start();
$open_badge_enable_baking = badgeos_get_option_open_badge_enable_baking( $achievement_id );
?>
<div class="bos_ob_achievement_details">
<div class="cob-ss-wrap">
<?php if ( $open_badge_enable_baking && $rec_type !== 'open_badge' ) { ?>
<div class="bos_ob_convert_to_ob">
<button type="submit" class="bos_ob_convert_to_ob_btn" value="<?php echo esc_attr( $entry_id ); ?>">
<?php echo esc_html( apply_filters( 'bos_ob_convert_to_ob_label', esc_html__( 'Convert to OpenBadge', 'badgeos' ) ) ); ?>
</button>
</div>
<?php } elseif ( $open_badge_enable_baking ) { ?>
<div class="badgeos_ob_verifcation_wrapper_div">
<input class="badgeos-ob-verification-buttons" id="open-badgeos-verification" href="javascript:;" data-bg="<?php echo esc_attr( $achievement_id ); ?>" data-eid="<?php echo esc_attr( $entry_id ); ?>" data-uid="<?php echo esc_attr( $achievement->user_id ); ?>" class="verify-open-badge" value="<?php echo esc_html__( 'Verify', 'badgeos' ); ?>" type="button" />
</div>
<?php } ?>
</div>
</div>
<?php
return $return . ob_get_clean();
}
/**
* Convert to existing normal Badge to Open Badge Standard.
*/
public function convert_to_open_badge() {
if ( ! wp_doing_ajax() ) {
return;
}
if ( ! isset( $_POST['entry_id'] ) ) {
return;
}
$entry_id = absint( $_POST['entry_id'] );
if ( empty( $entry_id ) ) {
return;
}
$response = array(
'status' => 'error',
'message' => esc_html__( 'An unknown error occurred while processing your request', 'badgeos' ),
);
$user_id = get_current_user_id();
$result = badgeos_ob_get_achievement_entry( $entry_id );
if ( ! empty( $result ) ) {
$object_id = $result['ID'];
if ( badgeos_get_option_open_badge_enable_baking( $object_id ) ) {
$GLOBALS['badgeos_open_badge']->bake_user_badge( $entry_id, $user_id, $object_id );
$response = array(
'status' => 'success',
'message' => esc_html__( 'Success', 'badgeos' ),
);
}
}
if ( $response['status'] === 'error' ) {
wp_send_json( $response, 400 );
}
wp_send_json( $response );
}
public function is_open_badge( $entry_id, $user_id ) {
$response = false;
if ( isset( $entry_id ) ) {
$entry_id = absint( $entry_id );
if ( ! empty( $entry_id ) ) {
$result = badgeos_ob_get_achievement_entry( $entry_id );
if ( ! empty( $result ) ) {
$achievement_id = $result['ID'];
if ( badgeos_get_option_open_badge_enable_baking( $achievement_id ) ) {
$response = $result['rec_type'] === 'open_badge';
}
}
}
}
return $response;
}
/**
* Validate badgeOS data
*/
public function badgeos_validate_open_badge() {
global $wpdb;
$achievement_id = 0;
if ( ! empty( $_REQUEST['bg'] ) ) {
$achievement_id = sanitize_text_field( wp_unslash( $_REQUEST['bg'] ) );
}
$entry_id = 0;
if ( ! empty( $_REQUEST['eid'] ) ) {
$entry_id = sanitize_text_field( wp_unslash( $_REQUEST['eid'] ) );
}
$user_id = 0;
if ( ! empty( $_REQUEST['uid'] ) ) {
$user_id = sanitize_text_field( wp_unslash( $_REQUEST['uid'] ) );
}
$type = 'issued_on';
if ( ! empty( $_REQUEST['type'] ) ) {
$type = sanitize_text_field( wp_unslash( $_REQUEST['type'] ) );
}
$date_format = badgeos_utilities::get_option( 'date_format', true );
$time_format = badgeos_utilities::get_option( 'time_format', true );
$msg = array(
'type' => 'error',
'result' => 0,
'message' => esc_html__( 'We are unable to serve ur request. Please, try back later.', 'badgeos' ),
);
$table_name = $wpdb->prefix . 'badgeos_achievements';
switch ( $type ) {
case 'issued_on':
$recs = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE entry_id=%d", $entry_id ) );
if ( count( $recs ) > 0 ) {
$msg = array(
'type' => 'success',
'result' => 1,
'message' => esc_html__( 'Issued On ', 'badgeos' ) . date( $date_format . ' ' . $time_format, strtotime( $recs[0]->date_earned ) ),
);
} else {
$msg = array(
'type' => 'failed',
'result' => 0,
'message' => esc_html__( 'No Issued Date Found.', 'badgeos' ),
);
}
break;
case 'issued_by':
$recs = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE entry_id=%d", $entry_id ) );
if ( count( $recs ) > 0 ) {
$site_title = get_bloginfo( 'name' );
$msg = array(
'type' => 'success',
'result' => 1,
'message' => esc_html__( 'Issued by', 'badgeos' ) . ' ' . $site_title,
);
} else {
$msg = array(
'type' => 'failed',
'result' => 0,
'message' => esc_html__( 'Issued by some other website.', 'badgeos' ),
);
}
break;
case 'issued_using':
$recs = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE entry_id=%d", $entry_id ) );
if ( count( $recs ) > 0 ) {
$site_title = get_bloginfo( 'name' );
$msg = array(
'type' => 'success',
'result' => 1,
'message' => esc_html__( 'Issued using BadgeOS', 'badgeos' ),
);
} else {
$msg = array(
'type' => 'failed',
'result' => 0,
'message' => esc_html__( 'Issued using some other plateform.', 'badgeos' ),
);
}
break;
case 'issued_to':
$recs = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE entry_id=%d", $entry_id ) );
if ( count( $recs ) > 0 ) {
$user_id = $recs[0]->user_id;
$user_info = get_userdata( $user_id );
if ( $user_info ) {
$display_name = $user_info->display_name;
$msg = array(
'type' => 'success',
'result' => 1,
'message' => esc_html__( 'Issued to ', 'badgeos' ) . ' ' . $display_name,
);
} else {
$msg = array(
'type' => 'failed',
'result' => 0,
'message' => esc_html__( 'Not issued to this user.', 'badgeos' ),
);
}
} else {
$msg = array(
'type' => 'failed',
'result' => 0,
'message' => esc_html__( 'Not issued to this user.', 'badgeos' ),
);
}
break;
case 'expiry_date':
$recs = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE entry_id=%d", $entry_id ) );
$open_badge_expiration = ( badgeos_utilities::get_post_meta( $recs[0]->ID, '_open_badge_expiration', true ) ? badgeos_utilities::get_post_meta( $recs[0]->ID, '_open_badge_expiration', true ) : '0' );
$open_badge_expiration_type = ( badgeos_utilities::get_post_meta( $recs[0]->ID, '_open_badge_expiration_type', true ) ? badgeos_utilities::get_post_meta( $recs[0]->ID, '_open_badge_expiration_type', true ) : '0' );
if ( intval( $open_badge_expiration ) > 0 ) {
$recs = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE entry_id=%d", $entry_id ) );
if ( count( $recs ) > 0 ) {
$badge_date = strtotime( $recs[0]->date_earned );
if ( ! empty( $badge_date ) ) {
$badge_expiry = strtotime( '+' . $open_badge_expiration . ' ' . $open_badge_expiration_type, $badge_date );
if ( $badge_expiry >= time() ) {
$msg = array(
'type' => 'success',
'result' => 1,
'message' => esc_html__( 'Expiry date is', 'badgeos' ) . ' ' . date( $date_format . ' ' . $time_format, $badge_expiry ),
);
} else {
$msg = array(
'type' => 'success',
'result' => 0,
'message' => esc_html__( 'Badge is expired on', 'badgeos' ) . ' ' . date( $date_format . ' ' . $time_format, $badge_expiry ),
);
}
} else {
$msg = array(
'type' => 'failed',
'result' => 0,
'message' => esc_html__( 'Badge is expired', 'badgeos' ),
);
}
} else {
$msg = array(
'type' => 'notfound',
'result' => 0,
'message' => esc_html__( 'Badge is not found', 'badgeos' ),
);
}
} else {
$recs = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE entry_id=%d", $entry_id ) );
if ( count( $recs ) > 0 ) {
$msg = array(
'type' => 'success',
'result' => 1,
'message' => esc_html__( 'No Expiry Date', 'badgeos' ),
);
} else {
$msg = array(
'type' => 'failed',
'result' => 0,
'message' => esc_html__( 'Badge is not found.', 'badgeos' ),
);
}
}
}
$msg['verified_label'] = esc_html__( 'Verified', 'badgeos' );
$msg['notverified_label'] = esc_html__( 'Not Verified', 'badgeos' );
wp_send_json( $msg );
}
/**
* Add a Open Badge Settings metabox on the badge CPT
*
* @return void
*/
public function open_badge_metabox_add() {
foreach ( badgeos_get_achievement_types_slugs() as $achievement_type ) {
add_meta_box( 'badgeos_social_share_meta_box', esc_html__( 'Open Badge Options', 'badgeos' ), array( $this, 'open_badge_metabox_show' ), $achievement_type, 'advanced', 'default' );
}
}
/**
* Output a Open Badge Settings metabox on the badge CPT
*
* @return void
*/
public function open_badge_metabox_show() {
global $post;
$open_badge_enable_baking = badgeos_get_option_open_badge_enable_baking( $post->ID );
$open_badge_criteria = ( badgeos_utilities::get_post_meta( $post->ID, '_open_badge_criteria', true ) ? badgeos_utilities::get_post_meta( $post->ID, '_open_badge_criteria', true ) : '' );
$open_badge_include_evidence = ( badgeos_utilities::get_post_meta( $post->ID, '_open_badge_include_evidence', true ) ? badgeos_utilities::get_post_meta( $post->ID, '_open_badge_include_evidence', true ) : 'false' );
$open_badge_expiration = ( badgeos_utilities::get_post_meta( $post->ID, '_open_badge_expiration', true ) ? badgeos_utilities::get_post_meta( $post->ID, '_open_badge_expiration', true ) : '0' );
$open_badge_expiration_type = ( badgeos_utilities::get_post_meta( $post->ID, '_open_badge_expiration_type', true ) ? badgeos_utilities::get_post_meta( $post->ID, '_open_badge_expiration_type', true ) : '0' );
?>
<input type="hidden" name="open_badge_nonce" value="<?php echo esc_attr( wp_create_nonce( 'open_badge' ) ); ?>" />
<table class="form-table">
<tr valign="top">
<td colspan="2"><?php esc_html__( "This setting makes the earned badge for this achievement sharable on social networks, such as Facebook, Twitter, LinkedIn, Mozilla Backpack, or the badge earner's own blog or site.", 'badgeos' ); ?></td>
</tr>
<tr valign="top">
<th scope="row"><label for="open_badge_enable_baking"><?php esc_html_e( 'Enable Badge Baking', 'badgeos' ); ?></label></th>
<td>
<select id="open_badge_enable_baking" name="open_badge_enable_baking">
<option value="1" <?php selected( $open_badge_enable_baking, true ); ?>><?php esc_html_e( 'Yes', 'badgeos' ); ?></option>
<option value="0" <?php selected( $open_badge_enable_baking, false ); ?>><?php esc_html_e( 'No', 'badgeos' ); ?></option>
</select>
</td>
</tr>
</table>
<div id="open-badge-setting-section">
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="open_badge_criteria"><?php esc_html_e( 'Criteria', 'badgeos' ); ?></label></th>
<td>
<input type="text" id="open_badge_criteria" readonly="readonly" name="open_badge_criteria" value="<?php echo esc_attr( $open_badge_criteria ); ?>" class="widefat" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="open_badge_include_evidence"><?php esc_html_e( 'Include Evidence', 'badgeos' ); ?></label></th>
<td>
<select id="open_badge_include_evidence" name="open_badge_include_evidence">
<option value="1" <?php selected( $open_badge_include_evidence, 'true' ); ?>><?php esc_html_e( 'Yes', 'badgeos' ); ?></option>
<option value="0" <?php selected( $open_badge_include_evidence, 'false' ); ?>><?php esc_html_e( 'No', 'badgeos' ); ?></option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="open_badge_expiration"><?php esc_html_e( 'Expiration', 'badgeos' ); ?></label></th>
<td>
<input type="number" id="open_badge_expiration" name="open_badge_expiration" value="<?php echo esc_attr( $open_badge_expiration ); ?>" class="date_picker_class" />
<select id="open_badge_expiration_type" name="open_badge_expiration_type">
<option value="Day" <?php selected( $open_badge_expiration_type, 'Day' ); ?>><?php esc_html_e( 'Day(s)', 'badgeos' ); ?></option>
<option value="Month" <?php selected( $open_badge_expiration_type, 'Month' ); ?>><?php esc_html_e( 'Month(s)', 'badgeos' ); ?></option>
<option value="Year" <?php selected( $open_badge_expiration_type, 'Year' ); ?>><?php esc_html_e( 'Year(s)', 'badgeos' ); ?></option>
</select>
<p><?php esc_html_e( 'Enter zero or leave empty for no expiry limit.', 'badgeos' ); ?></p>
</td>
</tr>
</table>
<p><strong><?php esc_html_e( 'Note', 'badgeos' ); ?></strong>: <?php esc_html_e( 'If Badge Baking is enabled, upload only PNG image on featured image option.', 'badgeos' ); ?></p>
</div>
<?php
}
/**
* Save our open Badge Settings metabox.
*
* @param int $post_id The ID of the given post.
*
* @return int Return the post ID of the post we're running on.
*/
public function open_badge_metabox_save( $post_id = 0 ) {
// Verify nonce.
if ( ! isset( $_POST['open_badge_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['open_badge_nonce'] ) ), 'open_badge' ) ) {
return $post_id;
}
// Make sure we're not doing an autosave.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Make sure this isn't a post revision.
if ( wp_is_post_revision( $post_id ) ) {
return $post_id;
}
// Check user permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
// Sanitize our fields.
$fields = $this->open_badge_metabox_sanitize_fields();
// Save our meta.
$meta = $this->open_badge_metabox_save_meta( $post_id, $fields );
return $post_id;
}
/**
* Save the meta fields from our metabox
*
* @param int $post_id Post ID
* @param array $fields An array of fields in the metabox
*
* @return bool Return true
*/
private function open_badge_metabox_save_meta( $post_id = 0, $fields = array() ) {
badgeos_utilities::update_post_meta( $post_id, '_open_badge_enable_baking', $fields['open_badge_enable_baking'] );
badgeos_utilities::update_post_meta( $post_id, '_open_badge_criteria', get_permalink( $post_id ) );
badgeos_utilities::update_post_meta( $post_id, '_open_badge_include_evidence', $fields['open_badge_include_evidence'] );
badgeos_utilities::update_post_meta( $post_id, '_open_badge_expiration', $fields['open_badge_expiration'] );
badgeos_utilities::update_post_meta( $post_id, '_open_badge_expiration_type', $fields['open_badge_expiration_type'] );
return true;
}
/**
* Sanitize our metabox fields.
*
* @return array An array of sanitized fields from our metabox.
*/
private function open_badge_metabox_sanitize_fields() {
$fields = array();
// Sanitize our input fields
$fields['open_badge_enable_baking'] = isset( $_POST['open_badge_enable_baking'] ) && $_POST['open_badge_enable_baking'] ? true : false;
$fields['open_badge_criteria'] = isset( $_POST['open_badge_criteria'] ) ? sanitize_text_field( wp_unslash( $_POST['open_badge_criteria'] ) ) : '';
$fields['open_badge_include_evidence'] = isset( $_POST['open_badge_include_evidence'] ) && $_POST['open_badge_include_evidence'] ? true : false;
$fields['open_badge_expiration'] = isset( $_POST['open_badge_expiration'] ) ? sanitize_text_field( wp_unslash( $_POST['open_badge_expiration'] ) ) : '';
$fields['open_badge_expiration_type'] = isset( $_POST['open_badge_expiration_type'] ) ? sanitize_text_field( wp_unslash( $_POST['open_badge_expiration_type'] ) ) : '';
return $fields;
}
}
new open_badge_metabox();